This is an automated email from the ASF dual-hosted git repository. rongr pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push: new 4a8fac2800 [feature] coalesce scalar (#9487) 4a8fac2800 is described below commit 4a8fac2800f4f08525a83b1a9b0e790121db8e56 Author: Yao Liu <y...@startree.ai> AuthorDate: Mon Oct 3 16:49:37 2022 -0700 [feature] coalesce scalar (#9487) * coalesce scalar --- .../common/function/scalar/ObjectFunctions.java | 51 ++++++++++++++++++++++ .../core/data/function/ObjectFunctionsTest.java | 31 +++++++++++++ .../PostAggregationFunctionTest.java | 10 +++++ 3 files changed, 92 insertions(+) diff --git a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ObjectFunctions.java b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ObjectFunctions.java index 4053b2267b..88250df4fc 100644 --- a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ObjectFunctions.java +++ b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ObjectFunctions.java @@ -50,4 +50,55 @@ public class ObjectFunctions { public static boolean isNotDistinctFrom(@Nullable Object obj1, @Nullable Object obj2) { return !isDistinctFrom(obj1, obj2); } + + @Nullable + public static Object coalesce(@Nullable Object obj) { + return coalesceVar(obj); + } + + @Nullable + @ScalarFunction(nullableParameters = true) + public static Object coalesce(@Nullable Object obj1, @Nullable Object obj2) { + return coalesceVar(obj1, obj2); + } + + @Nullable + @ScalarFunction(nullableParameters = true) + public static Object coalesce(@Nullable Object obj1, @Nullable Object obj2, @Nullable Object obj3) { + return coalesceVar(obj1, obj2, obj3); + } + + @Nullable + @ScalarFunction(nullableParameters = true) + public static Object coalesce(@Nullable Object obj1, @Nullable Object obj2, @Nullable Object obj3, + @Nullable Object obj4) { + return coalesceVar(obj1, obj2, obj3, obj4); + } + + @Nullable + @ScalarFunction(nullableParameters = true) + public static Object coalesce(@Nullable Object obj1, @Nullable Object obj2, @Nullable Object obj3, + @Nullable Object obj4, @Nullable Object obj5) { + return coalesceVar(obj1, obj2, obj3, obj4, obj5); + } + + @Nullable + private static Object coalesceVar(Object... objects) { + for (Object o : objects) { + if (o != null) { + return o; + } + } + return null; + } + + @Nullable + private static Object coalesce(Object... objects) { + for (Object o : objects) { + if (o != null) { + return o; + } + } + return null; + } } diff --git a/pinot-core/src/test/java/org/apache/pinot/core/data/function/ObjectFunctionsTest.java b/pinot-core/src/test/java/org/apache/pinot/core/data/function/ObjectFunctionsTest.java index f2b2153980..e324352a53 100644 --- a/pinot-core/src/test/java/org/apache/pinot/core/data/function/ObjectFunctionsTest.java +++ b/pinot-core/src/test/java/org/apache/pinot/core/data/function/ObjectFunctionsTest.java @@ -82,6 +82,37 @@ public class ObjectFunctionsTest { inputs.add(new Object[]{"isDistinctFrom(value1,value2)", Lists.newArrayList("value1", "value2"), equal, false}); inputs.add(new Object[]{"isNotDistinctFrom(value1,value2)", Lists.newArrayList("value1", "value2"), equal, true}); + // all nulls + GenericRow nullRows2 = new GenericRow(); + nullRows2.putValue("null0", null); + nullRows2.putValue("null1", null); + inputs.add(new Object[]{"coalesce(null0,null1)", Lists.newArrayList("null0", "null1"), nullRows, null}); + + // one not null + GenericRow oneValue = new GenericRow(); + oneValue.putValue("null0", null); + oneValue.putValue("null1", null); + oneValue.putValue("null2", null); + oneValue.putValue("value1", 1); + oneValue.putValue("value2", null); + + inputs.add(new Object[]{ + "coalesce(null0,null1, null2, value1, value2)", Lists.newArrayList("null0", "null1", "null2", "value1", + "value2"), oneValue, 1 + }); + + // all not null + GenericRow allValues = new GenericRow(); + allValues.putValue("value1", "1"); + allValues.putValue("value2", 2); + allValues.putValue("value3", 3); + allValues.putValue("value4", 4); + allValues.putValue("value5", 5); + + inputs.add(new Object[]{ + "coalesce(value1,value2,value3,value4,value5)", Lists.newArrayList("value1", "value2", "value3", "value4", + "value5"), allValues, "1" + }); return inputs.toArray(new Object[0][]); } } diff --git a/pinot-core/src/test/java/org/apache/pinot/core/query/postaggregation/PostAggregationFunctionTest.java b/pinot-core/src/test/java/org/apache/pinot/core/query/postaggregation/PostAggregationFunctionTest.java index d77bde48f1..0c7b0e3e52 100644 --- a/pinot-core/src/test/java/org/apache/pinot/core/query/postaggregation/PostAggregationFunctionTest.java +++ b/pinot-core/src/test/java/org/apache/pinot/core/query/postaggregation/PostAggregationFunctionTest.java @@ -27,6 +27,7 @@ import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertNull; public class PostAggregationFunctionTest { @@ -91,5 +92,14 @@ public class PostAggregationFunctionTest { assertFalse((Boolean) function.invoke(new Object[]{null, "a"})); assertFalse((Boolean) function.invoke(new Object[]{"a", null})); assertFalse((Boolean) function.invoke(new Object[]{"a", "b"})); + + // Coalesce + function = new PostAggregationFunction("coalesce", new ColumnDataType[]{ColumnDataType.INT, ColumnDataType.STRING, + ColumnDataType.BOOLEAN}); + assertEquals(function.getResultType(), ColumnDataType.OBJECT); + assertNull(function.invoke(new Object[]{null, null, null})); + assertEquals(function.invoke(new Object[]{null, "1", null}), "1"); + assertEquals(function.invoke(new Object[]{1, "2", false}), 1); + assertEquals(function.invoke(new Object[]{null, null, true}), true); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org