Skip to content

Commit

Permalink
fix(query): fix variant get string function auto cast null to SQL NULL (
Browse files Browse the repository at this point in the history
  • Loading branch information
b41sh authored Feb 17, 2025
1 parent f6bcf03 commit aef0499
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/query/functions/src/scalars/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,12 @@ pub fn register(registry: &mut FunctionRegistry) {
}
match get_by_name(val, name, false) {
Some(v) => {
let json_str = cast_to_string(&v);
output.push(&json_str);
if is_null(&v) {
output.push_null();
} else {
let json_str = cast_to_string(&v);
output.push(&json_str);
}
}
None => output.push_null(),
}
Expand All @@ -419,8 +423,12 @@ pub fn register(registry: &mut FunctionRegistry) {
} else {
match get_by_index(val, idx as usize) {
Some(v) => {
let json_str = cast_to_string(&v);
output.push(&json_str);
if is_null(&v) {
output.push_null();
} else {
let json_str = cast_to_string(&v);
output.push(&json_str);
}
}
None => {
output.push_null();
Expand Down Expand Up @@ -632,7 +640,7 @@ pub fn register(registry: &mut FunctionRegistry) {
let mut out_offsets = Vec::new();
match get_by_path(&buf, json_path, &mut out_buf, &mut out_offsets) {
Ok(()) => {
if out_offsets.is_empty() {
if out_offsets.is_empty() || is_null(&out_buf) {
output.push_null();
} else {
let json_str = cast_to_string(&out_buf);
Expand Down
18 changes: 18 additions & 0 deletions src/query/functions/tests/it/scalars/testdata/variant.txt
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,15 @@ output domain : {"{\"b\":2}"..="{\"b\":2}"}
output : '{"b":2}'


ast : json_extract_path_text('{"a":null}', 'a')
raw expr : json_extract_path_text('{"a":null}', 'a')
checked expr : json_extract_path_text<String, String>("{\"a\":null}", "a")
optimized expr : NULL
output type : String NULL
output domain : {NULL}
output : NULL


ast : json_extract_path_text(s, k)
raw expr : json_extract_path_text(s::String, k::String)
checked expr : json_extract_path_text<String, String>(s, k)
Expand Down Expand Up @@ -3283,6 +3292,15 @@ output domain : {NULL}
output : NULL


ast : parse_json('{"k":null}')->>'k'
raw expr : get_string(parse_json('{"k":null}'), 'k')
checked expr : get_string<Variant, String>(parse_json<String>("{\"k\":null}"), "k")
optimized expr : NULL
output type : String NULL
output domain : {NULL}
output : NULL


ast : CAST(('a', 'b') AS VARIANT)->>'2'
raw expr : get_string(CAST(tuple('a', 'b') AS Variant), '2')
checked expr : get_string<Variant, String>(to_variant<T0=Tuple(String, String)><T0>(tuple<String, String>("a", "b")), "2")
Expand Down
2 changes: 2 additions & 0 deletions src/query/functions/tests/it/scalars/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ fn test_get_string_arrow_op(file: &mut impl Write) {
run_ast(file, "parse_json('[1,2,3,4]')->>(2+3)", &[]);
run_ast(file, "parse_json('{\"k\":\"v\"}')->>'k'", &[]);
run_ast(file, "parse_json('{\"k\":\"v\"}')->>'x'", &[]);
run_ast(file, "parse_json('{\"k\":null}')->>'k'", &[]);
run_ast(file, "CAST(('a', 'b') AS VARIANT)->>'2'", &[]);

run_ast(file, "parse_json(s)->>i", &[
Expand Down Expand Up @@ -471,6 +472,7 @@ fn test_json_extract_path_text(file: &mut impl Write) {
);
run_ast(file, "json_extract_path_text('{\"a\":{\"b\":2}}', 'a')", &[
]);
run_ast(file, "json_extract_path_text('{\"a\":null}', 'a')", &[]);

run_ast(file, "json_extract_path_text(s, k)", &[
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ NULL
query T
select parse_json('{"aa": null, "aA": 2, "Aa": 3}')->>'aa'
----
null
NULL

query T
select parse_json(null)->>'aa'
----
NULL

query T
select parse_json('{"key1":null}')->>'key1'
----
NULL

query T
select parse_json('[2.71, 3.14]')[0]
----
Expand Down Expand Up @@ -201,6 +206,10 @@ select json_extract_path_text('{"customer":{"id": 1, "name":"databend", "extras"
----
{"customer":{"extras":["ext","test"],"id":1,"name":"databend"}}

query T
select json_extract_path_text('{"attr":null}', 'attr')
----
NULL

query T
select NULL#>'{0}'
Expand Down

0 comments on commit aef0499

Please sign in to comment.