diff --git a/src/rewrite.rs b/src/rewrite.rs index a0e87d5..542f280 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -71,9 +71,9 @@ fn unnest_json_calls(func: &ScalarFunction) -> Option> { 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; } diff --git a/tests/main.rs b/tests/main.rs index c340fcd..9c30dc9 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -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"; @@ -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]", ]; @@ -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]", ];