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/09/02 21:29:48 UTC

[GitHub] [pinot] 61yao opened a new pull request, #9330: [Feature] Not Operator Transformation

61yao opened a new pull request, #9330:
URL: https://github.com/apache/pinot/pull/9330

   Negate a boolean expression in transform. This transform function only takes a single boolean/int argument.


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


[GitHub] [pinot] walterddr merged pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
walterddr merged PR #9330:
URL: https://github.com/apache/pinot/pull/9330


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


[GitHub] [pinot] 61yao commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
61yao commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r964040399


##########
pinot-core/src/test/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunctionTest.java:
##########
@@ -0,0 +1,71 @@
+/**
+ * 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 org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.request.context.RequestContextUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class NotOperatorTransformFunctionTest extends BaseTransformFunctionTest {
+  // Test not true returns false.
+  @Test
+  public void testNotTrueOperatorTransformFunction() {
+    ExpressionContext expr =
+        RequestContextUtils.getExpression(String.format("Not (%d = %d)", _intSVValues[0], _intSVValues[0]));
+    TransformFunction func = TransformFunctionFactory.get(expr, _dataSourceMap);
+    Assert.assertEquals(func.getName(), "not");
+    int[] notTrueExpectedIntValues = new int[NUM_ROWS];
+    for (int i = 0; i < NUM_ROWS; i++) {
+      notTrueExpectedIntValues[i] = 0;
+    }
+    testTransformFunction(func, notTrueExpectedIntValues);
+  }
+
+  // Test not false returns true.
+  @Test
+  public void testNotFalseOperatorTransformFunction() {
+    ExpressionContext expr =
+        RequestContextUtils.getExpression(String.format("Not (%d != %d)", _intSVValues[0], _intSVValues[0]));
+    TransformFunction func = TransformFunctionFactory.get(expr, _dataSourceMap);
+    Assert.assertEquals(func.getName(), "not");
+    int[] notTrueExpectedIntValues = new int[NUM_ROWS];
+    for (int i = 0; i < NUM_ROWS; i++) {
+      notTrueExpectedIntValues[i] = 1;
+    }
+    testTransformFunction(func, notTrueExpectedIntValues);
+  }

Review Comment:
   Done



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


[GitHub] [pinot] 61yao commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
61yao commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r964017012


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {
+      _results = new int[numDocs];
+    }
+    ArrayCopyUtils.copy(_argument.transformToIntValuesSV(projectionBlock), _results, numDocs);
+    for (int i = 0; i < numDocs; i++) {
+      _results[i] = getLogicalNegate(_results[i]);
+    }
+    return _results;
+  }
+
+  private static int getLogicalNegate(int val) {
+    if (val == 0) {
+      return 1;
+    }
+    return 0;
+  }
+
+  private TransformFunction _argument;

Review Comment:
   Done.



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {

Review Comment:
   Done.



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


[GitHub] [pinot] 61yao commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
61yao commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r964041018


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)

Review Comment:
   Added another usage without braces.



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


[GitHub] [pinot] walterddr commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
walterddr commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r962081506


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)

Review Comment:
   sample usage and sample syntax is not the same here. `(` and `)` should be optional. 



##########
pinot-common/src/main/java/org/apache/pinot/common/function/TransformFunctionType.java:
##########
@@ -65,6 +65,7 @@ public enum TransformFunctionType {
 
   AND("and"),
   OR("or"),
+  NOT("not"),

Review Comment:
   let's add a comment indicating `NOT_EQUAL` and `NOT_IN` are not covered by this branch



##########
pinot-core/src/test/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunctionTest.java:
##########
@@ -0,0 +1,71 @@
+/**
+ * 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 org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.request.context.RequestContextUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class NotOperatorTransformFunctionTest extends BaseTransformFunctionTest {
+  // Test not true returns false.
+  @Test
+  public void testNotTrueOperatorTransformFunction() {
+    ExpressionContext expr =
+        RequestContextUtils.getExpression(String.format("Not (%d = %d)", _intSVValues[0], _intSVValues[0]));
+    TransformFunction func = TransformFunctionFactory.get(expr, _dataSourceMap);
+    Assert.assertEquals(func.getName(), "not");
+    int[] notTrueExpectedIntValues = new int[NUM_ROWS];
+    for (int i = 0; i < NUM_ROWS; i++) {
+      notTrueExpectedIntValues[i] = 0;
+    }
+    testTransformFunction(func, notTrueExpectedIntValues);
+  }
+
+  // Test not false returns true.
+  @Test
+  public void testNotFalseOperatorTransformFunction() {
+    ExpressionContext expr =
+        RequestContextUtils.getExpression(String.format("Not (%d != %d)", _intSVValues[0], _intSVValues[0]));
+    TransformFunction func = TransformFunctionFactory.get(expr, _dataSourceMap);
+    Assert.assertEquals(func.getName(), "not");
+    int[] notTrueExpectedIntValues = new int[NUM_ROWS];
+    for (int i = 0; i < NUM_ROWS; i++) {
+      notTrueExpectedIntValues[i] = 1;
+    }
+    testTransformFunction(func, notTrueExpectedIntValues);
+  }

Review Comment:
   can you also add a test for values?
   
   e.g. 
   ```
           RequestContextUtils.getExpression(String.format("Not (%s != %d)", INT_SV_COLUMN, _intSVValues[0]));
   ```
   and see if it works (as they should because the expression result in a binary result



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


[GitHub] [pinot] 61yao commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
61yao commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r964017214


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {
+      _results = new int[numDocs];
+    }
+    ArrayCopyUtils.copy(_argument.transformToIntValuesSV(projectionBlock), _results, numDocs);
+    for (int i = 0; i < numDocs; i++) {
+      _results[i] = getLogicalNegate(_results[i]);
+    }

Review Comment:
   Done.



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


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r962068859


##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {
+      _results = new int[numDocs];
+    }
+    ArrayCopyUtils.copy(_argument.transformToIntValuesSV(projectionBlock), _results, numDocs);
+    for (int i = 0; i < numDocs; i++) {
+      _results[i] = getLogicalNegate(_results[i]);
+    }

Review Comment:
   We can avoid assigning the value twice for better performance
   ```suggestion
       int[] intValues = _argument.transformToIntValuesSV(projectionBlock);
       for (int i = 0; i < numDocs; i++) {
         _results[i] = getLogicalNegate(intValues[i]);
       }
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {

Review Comment:
   (minor) The second check is not needed
   ```suggestion
       if (_results == null) {
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/NotOperatorTransformFunction.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.TransformFunctionType;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+
+
+/**
+ * The <code>Not</code> extends implement the Not operator.
+ *
+ * The results are in boolean format and stored as an integer array with 1 represents true and 0 represents false.
+ * It takes a single argument and negates it and the argument has to be a boolean/integer.
+ *
+ * Expected result:
+ * Not (1 = 1) | 0
+ * Not 1       | 0
+ *
+ * SQL Syntax:
+ *    Not <Boolean Expression>
+ *
+ * Sample Usage:
+ *    Not(booleanA)
+ */
+public class NotOperatorTransformFunction extends BaseTransformFunction {
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 1, "Exact 1 argument1 is required for not transform function");
+    TransformResultMetadata argumentMetadata = arguments.get(0).getResultMetadata();
+    Preconditions.checkState(
+        argumentMetadata.isSingleValue() && argumentMetadata.getDataType().getStoredType().isNumeric(),
+        "Unsupported argument type. Expecting single-valued boolean/number");
+    _argument = arguments.get(0);
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return BOOLEAN_SV_NO_DICTIONARY_METADATA;
+  }
+
+  @Override
+  public String getName() {
+    return TransformFunctionType.NOT.getName();
+  }
+
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    int numDocs = projectionBlock.getNumDocs();
+    if (_results == null || _results.length < numDocs) {
+      _results = new int[numDocs];
+    }
+    ArrayCopyUtils.copy(_argument.transformToIntValuesSV(projectionBlock), _results, numDocs);
+    for (int i = 0; i < numDocs; i++) {
+      _results[i] = getLogicalNegate(_results[i]);
+    }
+    return _results;
+  }
+
+  private static int getLogicalNegate(int val) {
+    if (val == 0) {
+      return 1;
+    }
+    return 0;
+  }
+
+  private TransformFunction _argument;

Review Comment:
   Let's put the member variables in front of the methods



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


[GitHub] [pinot] 61yao commented on a diff in pull request #9330: [Feature] Not Operator Transformation

Posted by GitBox <gi...@apache.org>.
61yao commented on code in PR #9330:
URL: https://github.com/apache/pinot/pull/9330#discussion_r964041180


##########
pinot-common/src/main/java/org/apache/pinot/common/function/TransformFunctionType.java:
##########
@@ -65,6 +65,7 @@ public enum TransformFunctionType {
 
   AND("and"),
   OR("or"),
+  NOT("not"),

Review Comment:
   Added the comment.



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