You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2021/04/22 22:48:59 UTC

[GitHub] [druid] jihoonson opened a new pull request #11152: Less strict operand type check and implicit casting

jihoonson opened a new pull request #11152:
URL: https://github.com/apache/druid/pull/11152


   ### Description
   
   Our current operand type check for function calls is too strict. It allows only when the the operand type is in the same `SqlTypeFamily`. However, implicit casting can be useful in many use cases by avoiding tedious casts in your sql query. This PR loosens the operand type check and allows implicit casting when it's good enough.
   
   <hr>
   
   ##### Key changed/added classes in this PR
    * `OperatorConversions`
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
      - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster.
   


-- 
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@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson merged pull request #11152: Unit test for DefaultOperandTypeChecker

Posted by GitBox <gi...@apache.org>.
jihoonson merged pull request #11152:
URL: https://github.com/apache/druid/pull/11152


   


-- 
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@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on pull request #11152: Unit test for DefaultOperandTypeChecker

Posted by GitBox <gi...@apache.org>.
jihoonson commented on pull request #11152:
URL: https://github.com/apache/druid/pull/11152#issuecomment-828078413


   @abhishekagarwal87 thanks for the review 👍 


-- 
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@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] abhishekagarwal87 commented on a change in pull request #11152: Unit test for DefaultOperandTypeChecker

Posted by GitBox <gi...@apache.org>.
abhishekagarwal87 commented on a change in pull request #11152:
URL: https://github.com/apache/druid/pull/11152#discussion_r620017497



##########
File path: sql/src/test/java/org/apache/druid/sql/calcite/expression/OperatorConversionsTest.java
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.druid.sql.calcite.expression;
+
+import com.google.common.collect.ImmutableList;
+import it.unimi.dsi.fastutil.ints.IntSets;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.runtime.CalciteContextException;
+import org.apache.calcite.runtime.Resources.ExInst;
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.sql.calcite.expression.OperatorConversions.DefaultOperandTypeChecker;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mockito;
+import org.mockito.stubbing.Answer;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+@RunWith(Enclosed.class)
+public class OperatorConversionsTest
+{
+  public static class DefaultOperandTypeCheckerTest
+  {
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void testGetOperandCountRange()
+    {
+      SqlOperandTypeChecker typeChecker = new DefaultOperandTypeChecker(
+          ImmutableList.of(SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER),
+          2,
+          IntSets.EMPTY_SET,
+          null
+      );
+      SqlOperandCountRange countRange = typeChecker.getOperandCountRange();
+      Assert.assertEquals(2, countRange.getMin());
+      Assert.assertEquals(3, countRange.getMax());
+    }
+
+    @Test
+    public void testIsOptional()
+    {
+      SqlOperandTypeChecker typeChecker = new DefaultOperandTypeChecker(
+          ImmutableList.of(SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER),
+          2,
+          IntSets.EMPTY_SET,
+          null
+      );
+      Assert.assertFalse(typeChecker.isOptional(0));
+      Assert.assertFalse(typeChecker.isOptional(1));
+      Assert.assertTrue(typeChecker.isOptional(2));
+    }
+
+    @Test
+    public void testAllowFullOperands()
+    {
+      SqlFunction function = OperatorConversions
+          .operatorBuilder("testAllowFullOperands")
+          .operandTypes(SqlTypeFamily.INTEGER, SqlTypeFamily.DATE)
+          .requiredOperands(1)
+          .returnTypeNonNull(SqlTypeName.CHAR)
+          .build();
+      SqlOperandTypeChecker typeChecker = function.getOperandTypeChecker();
+      Assert.assertTrue(
+          typeChecker.checkOperandTypes(
+              mockCallBinding(
+                  function,
+                  ImmutableList.of(
+                      new OperandSpec(SqlTypeName.INTEGER, false),
+                      new OperandSpec(SqlTypeName.DATE, false)
+                  )
+              ),
+              true
+          )
+      );
+    }
+
+    @Test
+    public void testRequiredOperandsOnly()
+    {
+      SqlFunction function = OperatorConversions
+          .operatorBuilder("testRequiredOperandsOnly")
+          .operandTypes(SqlTypeFamily.INTEGER, SqlTypeFamily.DATE)
+          .requiredOperands(1)
+          .returnTypeNonNull(SqlTypeName.CHAR)
+          .build();
+      SqlOperandTypeChecker typeChecker = function.getOperandTypeChecker();
+      Assert.assertTrue(
+          typeChecker.checkOperandTypes(
+              mockCallBinding(
+                  function,
+                  ImmutableList.of(new OperandSpec(SqlTypeName.INTEGER, false))
+              ),
+              true
+          )
+      );
+    }
+
+    @Test
+    public void testLiteralOperandCheckLiteral()
+    {
+      SqlFunction function = OperatorConversions
+          .operatorBuilder("testLiteralOperandCheckLiteral")
+          .operandTypes(SqlTypeFamily.INTEGER)
+          .requiredOperands(1)
+          .literalOperands(0)
+          .returnTypeNonNull(SqlTypeName.CHAR)
+          .build();
+      SqlOperandTypeChecker typeChecker = function.getOperandTypeChecker();
+      Assert.assertFalse(
+          typeChecker.checkOperandTypes(
+              mockCallBinding(
+                  function,
+                  ImmutableList.of(new OperandSpec(SqlTypeName.INTEGER, false))
+              ),
+              false
+          )
+      );
+    }
+
+    @Test
+    public void testLiteralOperandCheckLiteralThrow()
+    {
+      SqlFunction function = OperatorConversions
+          .operatorBuilder("testLiteralOperandCheckLiteralThrow")
+          .operandTypes(SqlTypeFamily.INTEGER)
+          .requiredOperands(1)
+          .literalOperands(0)
+          .returnTypeNonNull(SqlTypeName.CHAR)
+          .build();
+      SqlOperandTypeChecker typeChecker = function.getOperandTypeChecker();
+      expectedException.expect(CalciteContextException.class);
+      expectedException.expectMessage("Argument to function 'testLiteralOperandCheckLiteralThrow' must be a literal");
+      typeChecker.checkOperandTypes(
+          mockCallBinding(
+              function,
+              ImmutableList.of(new OperandSpec(SqlTypeName.INTEGER, false))
+          ),
+          true
+      );
+    }
+
+    @Test
+    public void testAnyTypeOperand()
+    {
+      SqlFunction function = OperatorConversions
+          .operatorBuilder("testAnyTypeOperand")
+          .operandTypes(SqlTypeFamily.ANY)
+          .requiredOperands(1)
+          .returnTypeNonNull(SqlTypeName.CHAR)
+          .build();
+      SqlOperandTypeChecker typeChecker = function.getOperandTypeChecker();
+      Assert.assertTrue(
+          typeChecker.checkOperandTypes(
+              mockCallBinding(
+                  function,
+                  ImmutableList.of(new OperandSpec(SqlTypeName.DISTINCT, false))
+              ),
+              true
+          )
+      );
+    }
+
+    @Test
+    public void testCastableFromDateTimestampToDatetimeFamily()

Review comment:
       Nice. wasn't expecting that it will pass as well. 




-- 
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@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org