Search in sources :

Example 16 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldReturnTrueOnTypeMatch.

@Test
public void testCheckOperandTypesShouldReturnTrueOnTypeMatch() throws NoSuchMethodException {
    Method udfMethod = MyTestPolyUdf.class.getMethod("execute", String.class);
    UdfMetadata udfMetadata = new UdfMetadata("MyTestPoly", "Test Polymorphism UDF.", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.STRING), SamzaSqlFieldType.INT32, false);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.VARCHAR, 12));
    assertTrue(operandTypeChecker.checkOperandTypes(callBinding, true));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 17 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldFailOnTypeMisMatch.

@Test(expected = SamzaSqlValidatorException.class)
public void testCheckOperandTypesShouldFailOnTypeMisMatch() throws NoSuchMethodException {
    Method udfMethod = TestUdfWithWrongTypes.class.getMethod("execute", String.class);
    UdfMetadata udfMetadata = new UdfMetadata("TestUdfWithWrongTypes", "TestUDFClass", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.INT32), SamzaSqlFieldType.INT64, false);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.VARCHAR, 12));
    operandTypeChecker.checkOperandTypes(callBinding, true);
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 18 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project samza by apache.

the class CheckerTest method testCheckOperandTypesShouldReturnFalseWhenThrowOnFailureIsFalse.

@Test
public void testCheckOperandTypesShouldReturnFalseWhenThrowOnFailureIsFalse() throws NoSuchMethodException {
    Method udfMethod = MyTestPolyUdf.class.getMethod("execute", String.class);
    UdfMetadata udfMetadata = new UdfMetadata("MyTestPoly", "Test Polymorphism UDF.", udfMethod, new MapConfig(), ImmutableList.of(SamzaSqlFieldType.STRING), SamzaSqlFieldType.INT32, false);
    Checker operandTypeChecker = Checker.getChecker(1, 3, udfMetadata);
    SqlCallBinding callBinding = Mockito.mock(SqlCallBinding.class);
    Mockito.when(callBinding.getOperandCount()).thenReturn(1);
    Mockito.when(callBinding.getOperandType(0)).thenReturn(new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.VARCHAR, 12));
    assertTrue(operandTypeChecker.checkOperandTypes(callBinding, false));
}
Also used : BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SamzaSqlUdfMethod(org.apache.samza.sql.udfs.SamzaSqlUdfMethod) Method(java.lang.reflect.Method) MapConfig(org.apache.samza.config.MapConfig) UdfMetadata(org.apache.samza.sql.interfaces.UdfMetadata) Test(org.junit.Test)

Example 19 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project flink by apache.

the class SqlValidatorImpl method inferUnknownTypes.

protected void inferUnknownTypes(@Nonnull RelDataType inferredType, @Nonnull SqlValidatorScope scope, @Nonnull SqlNode node) {
    Objects.requireNonNull(inferredType);
    Objects.requireNonNull(scope);
    Objects.requireNonNull(node);
    final SqlValidatorScope newScope = scopes.get(node);
    if (newScope != null) {
        scope = newScope;
    }
    boolean isNullLiteral = SqlUtil.isNullLiteral(node, false);
    if ((node instanceof SqlDynamicParam) || isNullLiteral) {
        if (inferredType.equals(unknownType)) {
            if (isNullLiteral) {
                if (config.typeCoercionEnabled()) {
                    // derive type of null literal
                    deriveType(scope, node);
                    return;
                } else {
                    throw newValidationError(node, RESOURCE.nullIllegal());
                }
            } else {
                throw newValidationError(node, RESOURCE.dynamicParamIllegal());
            }
        }
        // REVIEW:  should dynamic parameter types always be nullable?
        RelDataType newInferredType = typeFactory.createTypeWithNullability(inferredType, true);
        if (SqlTypeUtil.inCharFamily(inferredType)) {
            newInferredType = typeFactory.createTypeWithCharsetAndCollation(newInferredType, inferredType.getCharset(), inferredType.getCollation());
        }
        setValidatedNodeType(node, newInferredType);
    } else if (node instanceof SqlNodeList) {
        SqlNodeList nodeList = (SqlNodeList) node;
        if (inferredType.isStruct()) {
            if (inferredType.getFieldCount() != nodeList.size()) {
                // bust out, and the error will be detected higher up
                return;
            }
        }
        int i = 0;
        for (SqlNode child : nodeList) {
            RelDataType type;
            if (inferredType.isStruct()) {
                type = inferredType.getFieldList().get(i).getType();
                ++i;
            } else {
                type = inferredType;
            }
            inferUnknownTypes(type, scope, child);
        }
    } else if (node instanceof SqlCase) {
        final SqlCase caseCall = (SqlCase) node;
        final RelDataType whenType = caseCall.getValueOperand() == null ? booleanType : unknownType;
        for (SqlNode sqlNode : caseCall.getWhenOperands().getList()) {
            inferUnknownTypes(whenType, scope, sqlNode);
        }
        RelDataType returnType = deriveType(scope, node);
        for (SqlNode sqlNode : caseCall.getThenOperands().getList()) {
            inferUnknownTypes(returnType, scope, sqlNode);
        }
        if (!SqlUtil.isNullLiteral(caseCall.getElseOperand(), false)) {
            inferUnknownTypes(returnType, scope, caseCall.getElseOperand());
        } else {
            setValidatedNodeType(caseCall.getElseOperand(), returnType);
        }
    } else if (node.getKind() == SqlKind.AS) {
        // For AS operator, only infer the operand not the alias
        inferUnknownTypes(inferredType, scope, ((SqlCall) node).operand(0));
    } else if (node instanceof SqlCall) {
        final SqlCall call = (SqlCall) node;
        final SqlOperandTypeInference operandTypeInference = call.getOperator().getOperandTypeInference();
        final SqlCallBinding callBinding = new SqlCallBinding(this, scope, call);
        final List<SqlNode> operands = callBinding.operands();
        final RelDataType[] operandTypes = new RelDataType[operands.size()];
        Arrays.fill(operandTypes, unknownType);
        // instead; for now just eat it
        if (operandTypeInference != null) {
            operandTypeInference.inferOperandTypes(callBinding, inferredType, operandTypes);
        }
        for (int i = 0; i < operands.size(); ++i) {
            final SqlNode operand = operands.get(i);
            if (operand != null) {
                inferUnknownTypes(operandTypes[i], scope, operand);
            }
        }
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlOperandTypeInference(org.apache.calcite.sql.type.SqlOperandTypeInference) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCase(org.apache.calcite.sql.fun.SqlCase) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 20 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project flink by apache.

the class TypeInferenceReturnInference method extractExpectedOutputType.

// --------------------------------------------------------------------------------------------
@Nullable
private RelDataType extractExpectedOutputType(SqlOperatorBinding opBinding) {
    if (opBinding instanceof SqlCallBinding) {
        final SqlCallBinding binding = (SqlCallBinding) opBinding;
        final FlinkCalciteSqlValidator validator = (FlinkCalciteSqlValidator) binding.getValidator();
        return validator.getExpectedOutputType(binding.getCall()).orElse(null);
    }
    return null;
}
Also used : SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) FlinkCalciteSqlValidator(org.apache.flink.table.planner.calcite.FlinkCalciteSqlValidator) Nullable(javax.annotation.Nullable)

Aggregations

SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)23 RelDataType (org.apache.calcite.rel.type.RelDataType)13 SqlNode (org.apache.calcite.sql.SqlNode)9 SqlCall (org.apache.calcite.sql.SqlCall)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 SqlNodeList (org.apache.calcite.sql.SqlNodeList)6 SqlOperator (org.apache.calcite.sql.SqlOperator)6 UdfMetadata (org.apache.samza.sql.interfaces.UdfMetadata)6 SamzaSqlUdfMethod (org.apache.samza.sql.udfs.SamzaSqlUdfMethod)6 BasicSqlType (org.apache.calcite.sql.type.BasicSqlType)5 MapConfig (org.apache.samza.config.MapConfig)5 Test (org.junit.Test)5 List (java.util.List)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)4 SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)4 SqlLiteral (org.apache.calcite.sql.SqlLiteral)4 ExplicitOperatorBinding (org.apache.calcite.sql.ExplicitOperatorBinding)3 SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)3 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)3