Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ fn unnest_json_calls(func: &ScalarFunction) -> Option<Transformed<Expr>> {
let first_arg = outer_args_iter.next()?;
let inner_func = extract_scalar_function(first_arg)?;

// both json_get and json_as_text would produce new JSON to be processed by the outer
// function so can be inlined
if !matches!(inner_func.func.name(), "json_get" | "json_as_text") {
// only json_get preserves a JSON value for the outer function. json_as_text returns SQL text,
// so flattening through it changes semantics for JSON strings that contain JSON.
if inner_func.func.name() != "json_get" {
return None;
}

Expand Down
20 changes: 18 additions & 2 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,22 @@ async fn test_json_get_union_scalar() {
.await;
}

#[tokio::test]
async fn test_json_as_text_nested_json_string() {
let sql = r#"select json_as_text(json_as_text('{"user_id":"{\"device_id\":\"abc\"}"}', 'user_id'), 'device_id')"#;
let batches = run_query(sql).await.unwrap();

assert_eq!(display_val(batches).await, (DataType::Utf8, "abc".to_string()));
}

#[tokio::test]
async fn test_json_get_str_nested_json_string() {
let sql = r#"select json_get_str(json_as_text('{"user_id":"{\"device_id\":\"abc\"}"}', 'user_id'), 'device_id')"#;
let batches = run_query(sql).await.unwrap();

assert_eq!(display_val(batches).await, (DataType::Utf8, "abc".to_string()));
}

#[tokio::test]
async fn test_json_get_nested_collapsed() {
let sql = "select name, json_get(json_get(json_data, 'foo'), 0) as v from test";
Expand Down Expand Up @@ -1182,7 +1198,7 @@ async fn test_plan_double_arrow_double_nested() {
let lines = logical_plan(r"explain select json_data->>'foo'->>0 from test").await;

let expected = [
"Projection: json_as_text(test.json_data, Utf8(\"foo\"), Int64(0)) AS json_data ->> 'foo' ->> 0",
"Projection: json_as_text(json_as_text(test.json_data, Utf8(\"foo\")) AS json_data ->> 'foo', Int64(0)) AS json_data ->> 'foo' ->> 0",
" TableScan: test projection=[json_data]",
];

Expand Down Expand Up @@ -1255,7 +1271,7 @@ async fn test_plan_double_arrow_double_nested_cast() {

// NB: json_as_text(..)::int is NOT the same as `json_get_int(..)`, hence the cast is not rewritten
let expected = [
"Projection: CAST(json_as_text(test.json_data, Utf8(\"foo\"), Int64(0)) AS json_data ->> 'foo' ->> 0 AS Int32)",
"Projection: CAST(json_as_text(json_as_text(test.json_data, Utf8(\"foo\")) AS json_data ->> 'foo', Int64(0)) AS json_data ->> 'foo' ->> 0 AS Int32)",
" TableScan: test projection=[json_data]",
];

Expand Down
Loading