You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by ab...@apache.org on 2025/05/07 00:12:19 UTC
(druid) branch json_merge_null_bug created (now 4d547aed822)
This is an automated email from the ASF dual-hosted git repository.
abhishekrb pushed a change to branch json_merge_null_bug
in repository https://gitbox.apache.org/repos/asf/druid.git
at 4d547aed822 Add SQL-compliant handling of json_merge() function with null expressions.
This branch includes the following new commits:
new 4d547aed822 Add SQL-compliant handling of json_merge() function with null expressions.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
(druid) 01/01: Add SQL-compliant handling of json_merge() function with null expressions.
Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
abhishekrb pushed a commit to branch json_merge_null_bug
in repository https://gitbox.apache.org/repos/asf/druid.git
commit 4d547aed822a971e0d70e7c0a5f6979f4ee10318
Author: Abhishek Balaji Radhakrishnan <ab...@gmail.com>
AuthorDate: Tue May 6 16:49:51 2025 -0700
Add SQL-compliant handling of json_merge() function with null expressions.
The json_merge() function fail with a query out with JSON_MERGE(null, null):
Function[json_merge] invalid input expected STRING but got STRING instead
The behavior in both mysql and postgres is that the function returns NULL
if any argument is NULL. This change adds that to align and make it SQL-compliant.
---
.../query/expression/NestedDataExpressions.java | 27 +++++-------
.../expression/NestedDataExpressionsTest.java | 51 +++++++++++++++++++++-
2 files changed, 62 insertions(+), 16 deletions(-)
diff --git a/processing/src/main/java/org/apache/druid/query/expression/NestedDataExpressions.java b/processing/src/main/java/org/apache/druid/query/expression/NestedDataExpressions.java
index 0926ce78e0a..5a384994f69 100644
--- a/processing/src/main/java/org/apache/druid/query/expression/NestedDataExpressions.java
+++ b/processing/src/main/java/org/apache/druid/query/expression/NestedDataExpressions.java
@@ -138,18 +138,13 @@ public class NestedDataExpressions
public ExprEval eval(ObjectBinding bindings)
{
ExprEval arg = args.get(0).eval(bindings);
- Object obj;
-
- if (arg.value() == null) {
- throw JsonMergeExprMacro.this.validationFailed(
- "invalid input expected %s but got %s instead",
- ExpressionType.STRING,
- arg.type()
- );
+ String argAsJson = getArgAsJson(arg);
+ if (argAsJson == null) {
+ return ExprEval.ofComplex(ExpressionType.NESTED_DATA, null);
}
-
+ Object obj;
try {
- obj = jsonMapper.readValue(getArgAsJson(arg), Object.class);
+ obj = jsonMapper.readValue(argAsJson, Object.class);
}
catch (JsonProcessingException e) {
throw JsonMergeExprMacro.this.processingFailed(e, "bad string input [%s]", arg.asString());
@@ -159,12 +154,13 @@ public class NestedDataExpressions
for (int i = 1; i < args.size(); i++) {
ExprEval argSub = args.get(i).eval(bindings);
-
+ String str = getArgAsJson(argSub);
+ if (str == null) {
+ return ExprEval.ofComplex(ExpressionType.NESTED_DATA, null);
+ }
try {
- String str = getArgAsJson(argSub);
- if (str != null) {
- obj = updater.readValue(str);
- }
+ obj = updater.readValue(str);
+ updater = jsonMapper.readerForUpdating(obj);
}
catch (JsonProcessingException e) {
throw JsonMergeExprMacro.this.processingFailed(e, "bad string input [%s]", argSub.asString());
@@ -181,6 +177,7 @@ public class NestedDataExpressions
return ExpressionType.NESTED_DATA;
}
+ @Nullable
private String getArgAsJson(ExprEval arg)
{
if (arg.value() == null) {
diff --git a/processing/src/test/java/org/apache/druid/query/expression/NestedDataExpressionsTest.java b/processing/src/test/java/org/apache/druid/query/expression/NestedDataExpressionsTest.java
index 269f4b63372..b2267b4b06d 100644
--- a/processing/src/test/java/org/apache/druid/query/expression/NestedDataExpressionsTest.java
+++ b/processing/src/test/java/org/apache/druid/query/expression/NestedDataExpressionsTest.java
@@ -123,7 +123,7 @@ public class NestedDataExpressionsTest extends InitializedNullHandlingTest
expr = Parser.parse("json_merge('{\"a\":\"x\"}', null)", MACRO_TABLE);
eval = expr.eval(inputBindings);
- Assert.assertEquals("{\"a\":\"x\"}", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
expr = Parser.parse("json_merge('{\"a\":\"x\"}','{\"b\":\"y\"}','{\"c\":[1,2,3]}')", MACRO_TABLE);
@@ -147,6 +147,55 @@ public class NestedDataExpressionsTest extends InitializedNullHandlingTest
Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
}
+ @Test
+ public void testJsonMergeWithNullAndEmptyExpressions() throws JsonProcessingException
+ {
+ Expr expr = Parser.parse("json_merge(null, null)", MACRO_TABLE);
+ ExprEval eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge(null, '{\"a\":\"x\"}')", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge('{\"a\":\"x\"}', null)", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge('{\"a\":\"x\"}', null, null, null)", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge('{\"a\":\"x\"}', null, null, json_object())", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge(json_object(), json_object(), json_object())", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("{}", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge(json_object(), json_object(), json_object(), null)", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("null", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge(json_object(), json_object(), json_object(), coalesce(null, '{}'))", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("{}", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+
+ expr = Parser.parse("json_merge(coalesce(null, '{}'), '{\"a\":\"x\"}')", MACRO_TABLE);
+ eval = expr.eval(inputBindings);
+ Assert.assertEquals("{\"a\":\"x\"}", JSON_MAPPER.writeValueAsString(eval.value()));
+ Assert.assertEquals(ExpressionType.NESTED_DATA, eval.type());
+ }
+
@Test
public void testJsonMergeOverflow() throws JsonProcessingException
{
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org