@@ -260,7 +260,19 @@ pub fn sql_value_to_value(
260
260
SqlValue :: DoubleQuotedString ( s) | SqlValue :: SingleQuotedString ( s) => {
261
261
parse_string_to_value ( column_name, s. clone ( ) , data_type, timezone) ?
262
262
}
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
+ }
264
276
SqlValue :: Placeholder ( s) => return InvalidSqlValueSnafu { value : s } . fail ( ) ,
265
277
266
278
// TODO(dennis): supports binary string
@@ -884,6 +896,16 @@ mod tests {
884
896
) ;
885
897
assert ! ( v. is_err( ) ) ;
886
898
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( ) ) ;
887
909
}
888
910
889
911
#[ test]
@@ -1049,6 +1071,36 @@ mod tests {
1049
1071
}
1050
1072
}
1051
1073
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
+
1052
1104
#[ test]
1053
1105
pub fn test_parse_column_default_constraint ( ) {
1054
1106
let bool_value = sqlparser:: ast:: Value :: Boolean ( true ) ;
0 commit comments