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/26 07:15:15 UTC

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

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