You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ja...@apache.org on 2022/09/30 22:40:37 UTC
[pinot] branch master updated: distinct from scalar functions (#9486)
This is an automated email from the ASF dual-hosted git repository.
jackie 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 cc14ec9845 distinct from scalar functions (#9486)
cc14ec9845 is described below
commit cc14ec98459d4eba992363c7a39655c13eccef43
Author: Yao Liu <ya...@startree.ai>
AuthorDate: Fri Sep 30 15:40:29 2022 -0700
distinct from scalar functions (#9486)
---
.../common/function/scalar/ObjectFunctions.java | 16 ++++
.../core/data/function/LogicalFunctionsTest.java | 4 +-
.../core/data/function/ObjectFunctionsTest.java | 87 ++++++++++++++++++++++
.../PostAggregationFunctionTest.java | 20 +++++
4 files changed, 125 insertions(+), 2 deletions(-)
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 2c719a4cfb..4053b2267b 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
@@ -34,4 +34,20 @@ public class ObjectFunctions {
public static boolean isNotNull(@Nullable Object obj) {
return !isNull(obj);
}
+
+ @ScalarFunction(nullableParameters = true)
+ public static boolean isDistinctFrom(@Nullable Object obj1, @Nullable Object obj2) {
+ if (obj1 == null && obj2 == null) {
+ return false;
+ }
+ if (obj1 == null || obj2 == null) {
+ return true;
+ }
+ return !obj1.equals(obj2);
+ }
+
+ @ScalarFunction(nullableParameters = true)
+ public static boolean isNotDistinctFrom(@Nullable Object obj1, @Nullable Object obj2) {
+ return !isDistinctFrom(obj1, obj2);
+ }
}
diff --git a/pinot-core/src/test/java/org/apache/pinot/core/data/function/LogicalFunctionsTest.java b/pinot-core/src/test/java/org/apache/pinot/core/data/function/LogicalFunctionsTest.java
index 7266ec91b4..90e7cbf9b4 100644
--- a/pinot-core/src/test/java/org/apache/pinot/core/data/function/LogicalFunctionsTest.java
+++ b/pinot-core/src/test/java/org/apache/pinot/core/data/function/LogicalFunctionsTest.java
@@ -42,13 +42,13 @@ public class LogicalFunctionsTest {
}
@Test(dataProvider = "logicalFunctionDataProvider")
- public void testArithmeticFunctions(String functionExpression, List<String> expectedArguments, GenericRow row,
+ public void testLogicalFunctions(String functionExpression, List<String> expectedArguments, GenericRow row,
Object expectedResult) {
testFunction(functionExpression, expectedArguments, row, expectedResult);
}
@DataProvider(name = "logicalFunctionDataProvider")
- public Object[][] arithmeticFunctionsDataProvider() {
+ public Object[][] logicalFunctionsDataProvider() {
List<Object[]> inputs = new ArrayList<>();
GenericRow row0 = new GenericRow();
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
new file mode 100644
index 0000000000..f2b2153980
--- /dev/null
+++ b/pinot-core/src/test/java/org/apache/pinot/core/data/function/ObjectFunctionsTest.java
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.data.function;
+
+import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.pinot.segment.local.function.InbuiltFunctionEvaluator;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+
+public class ObjectFunctionsTest {
+ private void testFunction(String functionExpression, List<String> expectedArguments, GenericRow row,
+ Object expectedResult) {
+ InbuiltFunctionEvaluator evaluator = new InbuiltFunctionEvaluator(functionExpression);
+ Assert.assertEquals(evaluator.getArguments(), expectedArguments);
+ Assert.assertEquals(evaluator.evaluate(row), expectedResult);
+ }
+
+ @Test(dataProvider = "objectFunctionsDataProvider")
+ public void testObjectFunctions(String functionExpression, List<String> expectedArguments, GenericRow row,
+ Object expectedResult) {
+ testFunction(functionExpression, expectedArguments, row, expectedResult);
+ }
+
+ @DataProvider(name = "objectFunctionsDataProvider")
+ public Object[][] objectFunctionsDataProvider() {
+ List<Object[]> inputs = new ArrayList<>();
+
+ // Both nulls
+ GenericRow nullRows = new GenericRow();
+ nullRows.putValue("null0", null);
+ nullRows.putValue("null1", null);
+ inputs.add(new Object[]{"isDistinctFrom(null0,null1)", Lists.newArrayList("null0", "null1"), nullRows, false});
+ inputs.add(new Object[]{"isNotDistinctFrom(null0,null1)", Lists.newArrayList("null0", "null1"), nullRows, true});
+
+ // Left null
+ GenericRow leftNull = new GenericRow();
+ leftNull.putValue("null0", null);
+ leftNull.putValue("value", 1);
+ inputs.add(new Object[]{"isDistinctFrom(null0,value)", Lists.newArrayList("null0", "value"), leftNull, true});
+ inputs.add(new Object[]{"isNotDistinctFrom(null0,value)", Lists.newArrayList("null0", "value"), leftNull, false});
+
+ // Right null
+ GenericRow rightNull = new GenericRow();
+ rightNull.putValue("value", 1);
+ rightNull.putValue("null0", null);
+ inputs.add(new Object[]{"isDistinctFrom(value,null0)", Lists.newArrayList("value", "null0"), rightNull, true});
+ inputs.add(new Object[]{"isNotDistinctFrom(value,null0)", Lists.newArrayList("value", "null0"), rightNull, false});
+
+ // Both not null and not equal
+ GenericRow notEqual = new GenericRow();
+ notEqual.putValue("value1", 1);
+ notEqual.putValue("value2", 2);
+ inputs.add(new Object[]{"isDistinctFrom(value1,value2)", Lists.newArrayList("value1", "value2"), notEqual, true});
+ inputs.add(
+ new Object[]{"isNotDistinctFrom(value1,value2)", Lists.newArrayList("value1", "value2"), notEqual, false});
+
+ // Both not null and equal
+ GenericRow equal = new GenericRow();
+ equal.putValue("value1", 1);
+ equal.putValue("value2", 1);
+ 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});
+
+ 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 e397ce7343..d77bde48f1 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
@@ -71,5 +71,25 @@ public class PostAggregationFunctionTest {
assertEquals(function.getResultType(), ColumnDataType.BOOLEAN);
assertFalse((Boolean) function.invoke(new Object[]{true}));
assertTrue((Boolean) function.invoke(new Object[]{false}));
+
+ // isDistinctFrom
+ function = new PostAggregationFunction("isDistinctFrom",
+ new ColumnDataType[]{ColumnDataType.STRING, ColumnDataType.STRING});
+ assertEquals(function.getResultType(), ColumnDataType.BOOLEAN);
+ assertFalse((Boolean) function.invoke(new Object[]{null, null}));
+ assertFalse((Boolean) function.invoke(new Object[]{"a", "a"}));
+ assertTrue((Boolean) function.invoke(new Object[]{null, "a"}));
+ assertTrue((Boolean) function.invoke(new Object[]{"a", null}));
+ assertTrue((Boolean) function.invoke(new Object[]{"a", "b"}));
+
+ // isNotDistinctFrom
+ function = new PostAggregationFunction("isNotDistinctFrom",
+ new ColumnDataType[]{ColumnDataType.STRING, ColumnDataType.STRING});
+ assertEquals(function.getResultType(), ColumnDataType.BOOLEAN);
+ assertTrue((Boolean) function.invoke(new Object[]{null, null}));
+ assertTrue((Boolean) function.invoke(new Object[]{"a", "a"}));
+ assertFalse((Boolean) function.invoke(new Object[]{null, "a"}));
+ assertFalse((Boolean) function.invoke(new Object[]{"a", null}));
+ assertFalse((Boolean) function.invoke(new Object[]{"a", "b"}));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org