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 2020/06/03 05:27:22 UTC

[GitHub] [incubator-pinot] mayankshriv commented on a change in pull request #5461: Adding Support for SQL CASE Statement

mayankshriv commented on a change in pull request #5461:
URL: https://github.com/apache/incubator-pinot/pull/5461#discussion_r434311036



##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/BinaryOperatorTransformFunction.java
##########
@@ -0,0 +1,320 @@
+/**
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+/**
+ * <code>BinaryOperatorTransformFunction</code> abstracts common functions for binary operators (=, !=, >=, >, <=, <)
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ */
+public abstract class BinaryOperatorTransformFunction extends BaseTransformFunction {
+
+  protected TransformFunction _leftTransformFunction;
+  protected TransformFunction _rightTransformFunction;
+  protected FieldSpec.DataType _leftDataType;
+  protected FieldSpec.DataType _rightDataType;
+  protected int[] _results;
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    // Check that there are exact 2 arguments
+    if (arguments.size() != 2) {

Review comment:
       Any reason to not use Preconditions.checkArgument()?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/BinaryOperatorTransformFunction.java
##########
@@ -0,0 +1,320 @@
+/**
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+/**
+ * <code>BinaryOperatorTransformFunction</code> abstracts common functions for binary operators (=, !=, >=, >, <=, <)
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ */
+public abstract class BinaryOperatorTransformFunction extends BaseTransformFunction {
+
+  protected TransformFunction _leftTransformFunction;
+  protected TransformFunction _rightTransformFunction;
+  protected FieldSpec.DataType _leftDataType;
+  protected FieldSpec.DataType _rightDataType;
+  protected int[] _results;
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    // Check that there are exact 2 arguments
+    if (arguments.size() != 2) {
+      throw new IllegalArgumentException("Exact 2 arguments are required for binary operator transform function");
+    }
+    _leftTransformFunction = arguments.get(0);
+    _rightTransformFunction = arguments.get(1);
+    _leftDataType = _leftTransformFunction.getResultMetadata().getDataType();
+    _rightDataType = _rightTransformFunction.getResultMetadata().getDataType();
+    switch (_leftDataType) {

Review comment:
       Isn't this as simple as:
   `
   if (leftType == BYTES || rightType == BYTES) {
       Preconditions.checkState(leftType == BYTES && rightType == BYTES)
   }
   `

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/BinaryOperatorTransformFunction.java
##########
@@ -0,0 +1,320 @@
+/**
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+/**
+ * <code>BinaryOperatorTransformFunction</code> abstracts common functions for binary operators (=, !=, >=, >, <=, <)
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ */
+public abstract class BinaryOperatorTransformFunction extends BaseTransformFunction {
+
+  protected TransformFunction _leftTransformFunction;
+  protected TransformFunction _rightTransformFunction;
+  protected FieldSpec.DataType _leftDataType;
+  protected FieldSpec.DataType _rightDataType;
+  protected int[] _results;
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    // Check that there are exact 2 arguments
+    if (arguments.size() != 2) {
+      throw new IllegalArgumentException("Exact 2 arguments are required for binary operator transform function");
+    }
+    _leftTransformFunction = arguments.get(0);
+    _rightTransformFunction = arguments.get(1);
+    _leftDataType = _leftTransformFunction.getResultMetadata().getDataType();
+    _rightDataType = _rightTransformFunction.getResultMetadata().getDataType();
+    switch (_leftDataType) {
+      case INT:
+      case LONG:
+      case FLOAT:
+      case DOUBLE:
+      case STRING:
+        switch (_rightDataType) {
+          case INT:
+          case LONG:
+          case FLOAT:
+          case DOUBLE:
+          case STRING:
+            break;
+          default:
+            throw new IllegalStateException(String.format(
+                "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+                _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+        }
+        break;
+      case BYTES:
+        if (_rightDataType != FieldSpec.DataType.BYTES) {
+          throw new IllegalStateException(String.format(
+              "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+              _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+        }
+        break;
+      default:
+        throw new IllegalStateException(String.format(
+            "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+            _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+    }
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return INT_SV_NO_DICTIONARY_METADATA;
+  }
+
+  protected void fillResultArray(ProjectionBlock projectionBlock) {
+    if (_results == null) {
+      _results = new int[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    int length = projectionBlock.getNumDocs();
+    switch (_leftDataType) {

Review comment:
       The NxN combination makes me wonder if the code explosion is worth the performance gain over using non primitive objects temporarily? Will the usage be in the aggr/group-by path?
   
   

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/BinaryOperatorTransformFunction.java
##########
@@ -0,0 +1,320 @@
+/**
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+/**
+ * <code>BinaryOperatorTransformFunction</code> abstracts common functions for binary operators (=, !=, >=, >, <=, <)
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ */
+public abstract class BinaryOperatorTransformFunction extends BaseTransformFunction {
+
+  protected TransformFunction _leftTransformFunction;
+  protected TransformFunction _rightTransformFunction;
+  protected FieldSpec.DataType _leftDataType;
+  protected FieldSpec.DataType _rightDataType;
+  protected int[] _results;
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    // Check that there are exact 2 arguments
+    if (arguments.size() != 2) {
+      throw new IllegalArgumentException("Exact 2 arguments are required for binary operator transform function");
+    }
+    _leftTransformFunction = arguments.get(0);
+    _rightTransformFunction = arguments.get(1);
+    _leftDataType = _leftTransformFunction.getResultMetadata().getDataType();
+    _rightDataType = _rightTransformFunction.getResultMetadata().getDataType();
+    switch (_leftDataType) {
+      case INT:
+      case LONG:
+      case FLOAT:
+      case DOUBLE:
+      case STRING:
+        switch (_rightDataType) {
+          case INT:
+          case LONG:
+          case FLOAT:
+          case DOUBLE:
+          case STRING:
+            break;
+          default:
+            throw new IllegalStateException(String.format(
+                "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+                _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+        }
+        break;
+      case BYTES:
+        if (_rightDataType != FieldSpec.DataType.BYTES) {
+          throw new IllegalStateException(String.format(
+              "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+              _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+        }
+        break;
+      default:
+        throw new IllegalStateException(String.format(
+            "Unsupported data type for comparison: [Left Transform Function [%s] result type is [%s], Right Transform Function [%s] result type is [%s]]",
+            _leftTransformFunction.getName(), _leftDataType, _rightTransformFunction.getName(), _rightDataType));
+    }
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return INT_SV_NO_DICTIONARY_METADATA;
+  }
+
+  protected void fillResultArray(ProjectionBlock projectionBlock) {
+    if (_results == null) {
+      _results = new int[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    int length = projectionBlock.getNumDocs();
+    switch (_leftDataType) {
+      case INT:
+        int[] leftIntValues = _leftTransformFunction.transformToIntValuesSV(projectionBlock);
+        switch (_rightDataType) {
+          case INT:
+            int[] rightIntValues = _rightTransformFunction.transformToIntValuesSV(projectionBlock);
+            for (int i = 0; i < length; i++) {
+              _results[i] = getBinaryFuncResult(Integer.compare(leftIntValues[i], rightIntValues[i]));
+            }
+            break;
+          case LONG:
+            long[] rightLongValues = _rightTransformFunction.transformToLongValuesSV(projectionBlock);
+            for (int i = 0; i < length; i++) {
+              _results[i] = getBinaryFuncResult(Long.compare(leftIntValues[i], rightLongValues[i]));
+            }
+            break;
+          case FLOAT:
+            float[] rightFloatValues = _rightTransformFunction.transformToFloatValuesSV(projectionBlock);
+            for (int i = 0; i < length; i++) {
+              _results[i] = getBinaryFuncResult(Double.compare(leftIntValues[i], rightFloatValues[i]));
+            }
+            break;
+          case DOUBLE:
+            double[] rightDoubleValues = _rightTransformFunction.transformToDoubleValuesSV(projectionBlock);
+            for (int i = 0; i < length; i++) {
+              _results[i] = getBinaryFuncResult(Double.compare(leftIntValues[i], rightDoubleValues[i]));
+            }
+            break;
+          case STRING:
+            String[] rightStringValues = _rightTransformFunction.transformToStringValuesSV(projectionBlock);
+            for (int i = 0; i < length; i++) {
+              _results[i] = getBinaryFuncResult(
+                  BigDecimal.valueOf(leftIntValues[i]).compareTo(new BigDecimal(rightStringValues[i])));

Review comment:
       Is the Transform function expected to treat operands as numbers and not comparable strings?




----------------------------------------------------------------
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.

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