Search in sources :

Example 21 with SqlCallBinding

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

the class ProcedureNamespace method validateImpl.

public RelDataType validateImpl(RelDataType targetRowType) {
    validator.inferUnknownTypes(validator.unknownType, scope, call);
    final RelDataType type = validator.deriveTypeImpl(scope, call);
    final SqlOperator operator = call.getOperator();
    final SqlCallBinding callBinding = new SqlCallBinding(validator, scope, call);
    if (operator instanceof SqlTableFunction) {
        final SqlTableFunction tableFunction = (SqlTableFunction) operator;
        if (type.getSqlTypeName() != SqlTypeName.CURSOR) {
            throw new IllegalArgumentException("Table function should have CURSOR " + "type, not " + type);
        }
        final SqlReturnTypeInference rowTypeInference = tableFunction.getRowTypeInference();
        RelDataType retType = rowTypeInference.inferReturnType(callBinding);
        return validator.getTypeFactory().createTypeWithNullability(retType, false);
    }
    // special handling of collection tables TABLE(function(...))
    if (SqlUtil.stripAs(enclosingNode).getKind() == SqlKind.COLLECTION_TABLE) {
        return toStruct(type, getNode());
    }
    return type;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlTableFunction(org.apache.calcite.sql.SqlTableFunction)

Example 22 with SqlCallBinding

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

the class SqlInOperator method deriveType.

public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
    final List<SqlNode> operands = call.getOperandList();
    assert operands.size() == 2;
    final SqlNode left = operands.get(0);
    final SqlNode right = operands.get(1);
    final RelDataTypeFactory typeFactory = validator.getTypeFactory();
    RelDataType leftType = validator.deriveType(scope, left);
    RelDataType rightType;
    // Derive type for RHS.
    if (right instanceof SqlNodeList) {
        // Handle the 'IN (expr, ...)' form.
        List<RelDataType> rightTypeList = new ArrayList<>();
        SqlNodeList nodeList = (SqlNodeList) right;
        for (int i = 0; i < nodeList.size(); i++) {
            SqlNode node = nodeList.get(i);
            RelDataType nodeType = validator.deriveType(scope, node);
            rightTypeList.add(nodeType);
        }
        rightType = typeFactory.leastRestrictive(rightTypeList);
        // SQL:2003 Part 2 Section 8.4, <in predicate>).
        if (null == rightType) {
            throw validator.newValidationError(right, RESOURCE.incompatibleTypesInList());
        }
        // Record the RHS type for use by SqlToRelConverter.
        ((SqlValidatorImpl) validator).setValidatedNodeType(nodeList, rightType);
    } else {
        // Handle the 'IN (query)' form.
        rightType = validator.deriveType(scope, right);
    }
    // Now check that the left expression is compatible with the
    // type of the list. Same strategy as the '=' operator.
    // Normalize the types on both sides to be row types
    // for the purposes of compatibility-checking.
    RelDataType leftRowType = SqlTypeUtil.promoteToRowType(typeFactory, leftType, null);
    RelDataType rightRowType = SqlTypeUtil.promoteToRowType(typeFactory, rightType, null);
    final ComparableOperandTypeChecker checker = (ComparableOperandTypeChecker) OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED;
    if (!checker.checkOperandTypes(new ExplicitOperatorBinding(new SqlCallBinding(validator, scope, call), ImmutableList.of(leftRowType, rightRowType)))) {
        throw validator.newValidationError(call, RESOURCE.incompatibleValueType(SqlStdOperatorTable.IN.getName()));
    }
    // on either side.
    return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), anyNullable(leftRowType.getFieldList()) || anyNullable(rightRowType.getFieldList()));
}
Also used : ComparableOperandTypeChecker(org.apache.calcite.sql.type.ComparableOperandTypeChecker) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ArrayList(java.util.ArrayList) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode) ExplicitOperatorBinding(org.apache.calcite.sql.ExplicitOperatorBinding)

Example 23 with SqlCallBinding

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

the class SqlToRelConverter method convertCollectionTable.

protected void convertCollectionTable(Blackboard bb, SqlCall call) {
    final SqlOperator operator = call.getOperator();
    if (operator == SqlStdOperatorTable.TABLESAMPLE) {
        final String sampleName = SqlLiteral.unchain(call.operand(0)).getValueAs(String.class);
        datasetStack.push(sampleName);
        SqlCall cursorCall = call.operand(1);
        SqlNode query = cursorCall.operand(0);
        RelNode converted = convertQuery(query, false, false).rel;
        bb.setRoot(converted, false);
        datasetStack.pop();
        return;
    }
    replaceSubQueries(bb, call, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
    // Expand table macro if possible. It's more efficient than
    // LogicalTableFunctionScan.
    final SqlCallBinding callBinding = new SqlCallBinding(bb.scope.getValidator(), bb.scope, call);
    if (operator instanceof SqlUserDefinedTableMacro) {
        final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator;
        final TranslatableTable table = udf.getTable(typeFactory, callBinding.operands());
        final RelDataType rowType = table.getRowType(typeFactory);
        RelOptTable relOptTable = RelOptTableImpl.create(null, rowType, table, udf.getNameAsId().names);
        RelNode converted = toRel(relOptTable);
        bb.setRoot(converted, true);
        return;
    }
    Type elementType;
    if (operator instanceof SqlUserDefinedTableFunction) {
        SqlUserDefinedTableFunction udtf = (SqlUserDefinedTableFunction) operator;
        elementType = udtf.getElementType(typeFactory, callBinding.operands());
    } else {
        elementType = null;
    }
    RexNode rexCall = bb.convertExpression(call);
    final List<RelNode> inputs = bb.retrieveCursors();
    Set<RelColumnMapping> columnMappings = getColumnMappings(operator);
    LogicalTableFunctionScan callRel = LogicalTableFunctionScan.create(cluster, inputs, rexCall, elementType, validator.getValidatedNodeType(call), columnMappings);
    bb.setRoot(callRel, true);
    afterTableFunction(bb, call, callRel);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) LogicalTableFunctionScan(org.apache.calcite.rel.logical.LogicalTableFunctionScan) JoinType(org.apache.calcite.sql.JoinType) RelDataType(org.apache.calcite.rel.type.RelDataType) JoinRelType(org.apache.calcite.rel.core.JoinRelType) JoinConditionType(org.apache.calcite.sql.JoinConditionType) Type(java.lang.reflect.Type) SemiJoinType(org.apache.calcite.sql.SemiJoinType) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) RelNode(org.apache.calcite.rel.RelNode) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) TranslatableTable(org.apache.calcite.schema.TranslatableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) RelColumnMapping(org.apache.calcite.rel.metadata.RelColumnMapping) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

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