You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2022/02/01 17:59:11 UTC

[GitHub] [pinot] Jackie-Jiang commented on a change in pull request #8100: add least and greatest functions

Jackie-Jiang commented on a change in pull request #8100:
URL: https://github.com/apache/pinot/pull/8100#discussion_r796854964



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/SelectTupleElementTransformFunction.java
##########
@@ -0,0 +1,100 @@
+/**
+ * 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.operator.transform.function;
+
+import java.util.Comparator;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public abstract class SelectTupleElementTransformFunction extends BaseTransformFunction {
+
+  private static final EnumSet<FieldSpec.DataType> SUPPORTED_DATATYPES = EnumSet.of(FieldSpec.DataType.INT,
+      FieldSpec.DataType.LONG, FieldSpec.DataType.FLOAT, FieldSpec.DataType.DOUBLE, FieldSpec.DataType.TIMESTAMP);
+
+  private static final Comparator<FieldSpec.DataType> DOMINANCE_HIERARCHY = Comparator.comparingInt(Enum::ordinal);
+
+  private static final EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> ACCEPTABLE_COMBINATIONS =
+      createAcceptableCombinations();
+
+  private final String _name;
+
+  protected List<TransformFunction> _arguments;
+  private TransformResultMetadata _resultMetadata;
+
+  public SelectTupleElementTransformFunction(String name) {
+    _name = name;
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    if (arguments.isEmpty()) {
+      throw new IllegalArgumentException(_name + " takes at least one argument");
+    }
+    FieldSpec.DataType dataType = null;
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction argument = arguments.get(i);
+      TransformResultMetadata metadata = argument.getResultMetadata();
+      if (!metadata.isSingleValue()) {
+        throw new IllegalArgumentException(argument.getName() + " at position " + i + " is not single value");
+      }
+      FieldSpec.DataType argumentType = metadata.getDataType();
+      if (!SUPPORTED_DATATYPES.contains(argumentType)) {
+        throw new IllegalArgumentException(argumentType + " not supported. " + SUPPORTED_DATATYPES + " are supported.");
+      }
+      if (dataType == null) {
+        dataType = argumentType;
+      } else if (ACCEPTABLE_COMBINATIONS.get(dataType).contains(argumentType)) {
+        if (DOMINANCE_HIERARCHY.compare(dataType, argumentType) < 0) {

Review comment:
       This will turn `LONG` into `FLOAT`, which seems wrong to me

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/SelectTupleElementTransformFunction.java
##########
@@ -0,0 +1,100 @@
+/**
+ * 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.operator.transform.function;
+
+import java.util.Comparator;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public abstract class SelectTupleElementTransformFunction extends BaseTransformFunction {
+
+  private static final EnumSet<FieldSpec.DataType> SUPPORTED_DATATYPES = EnumSet.of(FieldSpec.DataType.INT,
+      FieldSpec.DataType.LONG, FieldSpec.DataType.FLOAT, FieldSpec.DataType.DOUBLE, FieldSpec.DataType.TIMESTAMP);
+
+  private static final Comparator<FieldSpec.DataType> DOMINANCE_HIERARCHY = Comparator.comparingInt(Enum::ordinal);
+
+  private static final EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> ACCEPTABLE_COMBINATIONS =
+      createAcceptableCombinations();
+
+  private final String _name;
+
+  protected List<TransformFunction> _arguments;
+  private TransformResultMetadata _resultMetadata;
+
+  public SelectTupleElementTransformFunction(String name) {
+    _name = name;
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    if (arguments.isEmpty()) {
+      throw new IllegalArgumentException(_name + " takes at least one argument");
+    }
+    FieldSpec.DataType dataType = null;
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction argument = arguments.get(i);
+      TransformResultMetadata metadata = argument.getResultMetadata();
+      if (!metadata.isSingleValue()) {
+        throw new IllegalArgumentException(argument.getName() + " at position " + i + " is not single value");
+      }
+      FieldSpec.DataType argumentType = metadata.getDataType();
+      if (!SUPPORTED_DATATYPES.contains(argumentType)) {
+        throw new IllegalArgumentException(argumentType + " not supported. " + SUPPORTED_DATATYPES + " are supported.");
+      }
+      if (dataType == null) {
+        dataType = argumentType;
+      } else if (ACCEPTABLE_COMBINATIONS.get(dataType).contains(argumentType)) {
+        if (DOMINANCE_HIERARCHY.compare(dataType, argumentType) < 0) {
+          dataType = argumentType;
+        }

Review comment:
       We might want an else clause here to reject unacceptable combinations

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/SelectTupleElementTransformFunction.java
##########
@@ -0,0 +1,100 @@
+/**
+ * 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.operator.transform.function;
+
+import java.util.Comparator;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public abstract class SelectTupleElementTransformFunction extends BaseTransformFunction {
+
+  private static final EnumSet<FieldSpec.DataType> SUPPORTED_DATATYPES = EnumSet.of(FieldSpec.DataType.INT,
+      FieldSpec.DataType.LONG, FieldSpec.DataType.FLOAT, FieldSpec.DataType.DOUBLE, FieldSpec.DataType.TIMESTAMP);
+
+  private static final Comparator<FieldSpec.DataType> DOMINANCE_HIERARCHY = Comparator.comparingInt(Enum::ordinal);
+
+  private static final EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> ACCEPTABLE_COMBINATIONS =
+      createAcceptableCombinations();
+
+  private final String _name;
+
+  protected List<TransformFunction> _arguments;
+  private TransformResultMetadata _resultMetadata;
+
+  public SelectTupleElementTransformFunction(String name) {
+    _name = name;
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    if (arguments.isEmpty()) {
+      throw new IllegalArgumentException(_name + " takes at least one argument");
+    }
+    FieldSpec.DataType dataType = null;
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction argument = arguments.get(i);
+      TransformResultMetadata metadata = argument.getResultMetadata();
+      if (!metadata.isSingleValue()) {
+        throw new IllegalArgumentException(argument.getName() + " at position " + i + " is not single value");
+      }
+      FieldSpec.DataType argumentType = metadata.getDataType();
+      if (!SUPPORTED_DATATYPES.contains(argumentType)) {
+        throw new IllegalArgumentException(argumentType + " not supported. " + SUPPORTED_DATATYPES + " are supported.");
+      }
+      if (dataType == null) {
+        dataType = argumentType;
+      } else if (ACCEPTABLE_COMBINATIONS.get(dataType).contains(argumentType)) {
+        if (DOMINANCE_HIERARCHY.compare(dataType, argumentType) < 0) {
+          dataType = argumentType;
+        }
+      }
+    }
+    _resultMetadata = new TransformResultMetadata(dataType, true, false);
+    _arguments = arguments;
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return _resultMetadata;
+  }
+
+  @Override
+  public String getName() {
+    return _name;
+  }
+
+  private static EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> createAcceptableCombinations() {
+    EnumMap<FieldSpec.DataType, EnumSet<FieldSpec.DataType>> combinations = new EnumMap<>(FieldSpec.DataType.class);
+    combinations.put(FieldSpec.DataType.TIMESTAMP, EnumSet.of(FieldSpec.DataType.TIMESTAMP, FieldSpec.DataType.LONG));

Review comment:
       Should we allow `TIMESTAMP` to be compared with floating numbers (use `double` to compare)?

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArithmeticFunctions.java
##########
@@ -54,15 +54,27 @@ public static double mod(double a, double b) {
   }
 
   @ScalarFunction
-  public static double min(double a, double b) {
+  public static double least(double a, double b) {
     return Double.min(a, b);
   }
 
   @ScalarFunction
-  public static double max(double a, double b) {
+  public static double greatest(double a, double b) {
     return Double.max(a, b);
   }
 
+  @Deprecated

Review comment:
       We should deprecate them as they are not standard sql. Let's add some release note in the PR description

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/TransformFunctionType.java
##########
@@ -96,7 +96,10 @@
   ST_WITHIN("ST_Within"),
 
   // Geo indexing
-  GEOTOH3("geoToH3");
+  GEOTOH3("geoToH3"),
+
+  LEAST("least"),

Review comment:
       Suggest moving it above to be along with other arithmetic functions




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org