Skip to content

Commit 1c279e4

Browse files
committed
test: some tests and checks
1 parent 9f07158 commit 1c279e4

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/sql/src/statements.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,19 @@ pub fn sql_value_to_value(
260260
SqlValue::DoubleQuotedString(s) | SqlValue::SingleQuotedString(s) => {
261261
parse_string_to_value(column_name, s.clone(), data_type, timezone)?
262262
}
263-
SqlValue::HexStringLiteral(s) => parse_hex_string(s)?,
263+
SqlValue::HexStringLiteral(s) => {
264+
// Should not directly write binary into json column
265+
ensure!(
266+
!matches!(data_type, ConcreteDataType::Json(_)),
267+
ColumnTypeMismatchSnafu {
268+
column_name,
269+
expect: ConcreteDataType::binary_datatype(),
270+
actual: ConcreteDataType::json_datatype(),
271+
}
272+
);
273+
274+
parse_hex_string(s)?
275+
}
264276
SqlValue::Placeholder(s) => return InvalidSqlValueSnafu { value: s }.fail(),
265277

266278
// TODO(dennis): supports binary string
@@ -884,6 +896,16 @@ mod tests {
884896
);
885897
assert!(v.is_err());
886898
assert!(format!("{v:?}").contains("invalid character"), "v is {v:?}",);
899+
900+
let sql_val = SqlValue::DoubleQuotedString("MorningMyFriends".to_string());
901+
let v = sql_value_to_value(
902+
"a",
903+
&ConcreteDataType::json_datatype(),
904+
&sql_val,
905+
None,
906+
None,
907+
);
908+
assert!(v.is_err());
887909
}
888910

889911
#[test]
@@ -1049,6 +1071,36 @@ mod tests {
10491071
}
10501072
}
10511073

1074+
#[test]
1075+
fn test_parse_json_to_jsonb() {
1076+
match parse_string_to_value(
1077+
"json_col",
1078+
r#"{"a": "b"}"#.to_string(),
1079+
&ConcreteDataType::json_datatype(),
1080+
None,
1081+
) {
1082+
Ok(Value::Binary(b)) => {
1083+
assert_eq!(
1084+
b,
1085+
jsonb::parse_value(r#"{"a": "b"}"#.as_bytes())
1086+
.unwrap()
1087+
.to_vec()
1088+
);
1089+
}
1090+
_ => {
1091+
unreachable!()
1092+
}
1093+
}
1094+
1095+
assert!(parse_string_to_value(
1096+
"json_col",
1097+
r#"Nicola Kovac is the best rifler in the world"#.to_string(),
1098+
&ConcreteDataType::json_datatype(),
1099+
None,
1100+
)
1101+
.is_err())
1102+
}
1103+
10521104
#[test]
10531105
pub fn test_parse_column_default_constraint() {
10541106
let bool_value = sqlparser::ast::Value::Boolean(true);

0 commit comments

Comments
 (0)