Search in sources :

Example 1 with SqlReturnTypeInference

use of org.apache.calcite.sql.type.SqlReturnTypeInference in project calcite by apache.

the class CalciteCatalogReader method infer.

private static SqlReturnTypeInference infer(final ScalarFunction function) {
    return new SqlReturnTypeInference() {

        public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
            final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
            final RelDataType type;
            if (function instanceof ScalarFunctionImpl) {
                type = ((ScalarFunctionImpl) function).getReturnType(typeFactory, opBinding);
            } else {
                type = function.getReturnType(typeFactory);
            }
            return toSql(typeFactory, type);
        }
    };
}
Also used : SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) ScalarFunctionImpl(org.apache.calcite.schema.impl.ScalarFunctionImpl) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 2 with SqlReturnTypeInference

use of org.apache.calcite.sql.type.SqlReturnTypeInference in project beam by apache.

the class AggregateScanConverter method convertAggCall.

private AggregateCall convertAggCall(ResolvedComputedColumn computedColumn, int columnRefOff, int groupCount, RelNode input) {
    ResolvedAggregateFunctionCall aggregateFunctionCall = (ResolvedAggregateFunctionCall) computedColumn.getExpr();
    // Reject AVG(INT64)
    if (aggregateFunctionCall.getFunction().getName().equals("avg")) {
        FunctionSignature signature = aggregateFunctionCall.getSignature();
        if (signature.getFunctionArgumentList().get(0).getType().getKind().equals(TypeKind.TYPE_INT64)) {
            throw new UnsupportedOperationException(AVG_ILLEGAL_LONG_INPUT_TYPE);
        }
    }
    // Reject aggregation DISTINCT
    if (aggregateFunctionCall.getDistinct()) {
        throw new UnsupportedOperationException("Does not support " + aggregateFunctionCall.getFunction().getSqlName() + " DISTINCT. 'SELECT DISTINCT' syntax could be used to deduplicate before" + " aggregation.");
    }
    final SqlAggFunction sqlAggFunction;
    if (aggregateFunctionCall.getFunction().getGroup().equals(BeamZetaSqlCatalog.USER_DEFINED_JAVA_AGGREGATE_FUNCTIONS)) {
        // Create a new operator for user-defined functions.
        SqlReturnTypeInference typeInference = x -> ZetaSqlCalciteTranslationUtils.toCalciteType(aggregateFunctionCall.getFunction().getSignatureList().get(0).getResultType().getType(), // TODO(BEAM-9514) set nullable=true
        false, getCluster().getRexBuilder());
        UdafImpl<?, ?, ?> impl = new UdafImpl<>(getExpressionConverter().userFunctionDefinitions.javaAggregateFunctions().get(aggregateFunctionCall.getFunction().getNamePath()));
        sqlAggFunction = SqlOperators.createUdafOperator(aggregateFunctionCall.getFunction().getName(), typeInference, impl);
    } else {
        // Look up builtin functions in SqlOperatorMappingTable.
        sqlAggFunction = (SqlAggFunction) SqlOperatorMappingTable.create(aggregateFunctionCall);
        if (sqlAggFunction == null) {
            throw new UnsupportedOperationException("Does not support ZetaSQL aggregate function: " + aggregateFunctionCall.getFunction().getName());
        }
    }
    List<Integer> argList = new ArrayList<>();
    ResolvedAggregateFunctionCall expr = ((ResolvedAggregateFunctionCall) computedColumn.getExpr());
    List<ZetaSQLResolvedNodeKind.ResolvedNodeKind> resolvedNodeKinds = Arrays.asList(RESOLVED_CAST, RESOLVED_COLUMN_REF, RESOLVED_GET_STRUCT_FIELD);
    for (int i = 0; i < expr.getArgumentList().size(); i++) {
        // Throw an error if aggregate function's input isn't either a ColumnRef or a cast(ColumnRef).
        // TODO: is there a general way to handle aggregation calls conversion?
        ZetaSQLResolvedNodeKind.ResolvedNodeKind resolvedNodeKind = expr.getArgumentList().get(i).nodeKind();
        if (i == 0 && resolvedNodeKinds.contains(resolvedNodeKind)) {
            argList.add(columnRefOff);
        } else if (i > 0 && resolvedNodeKind == RESOLVED_LITERAL) {
            continue;
        } else {
            throw new UnsupportedOperationException("Aggregate function only accepts Column Reference or CAST(Column Reference) as the first argument and " + "Literals as subsequent arguments as its inputs");
        }
    }
    String aggName = getTrait().resolveAlias(computedColumn.getColumn());
    return AggregateCall.create(sqlAggFunction, false, false, false, argList, -1, null, RelCollations.EMPTY, groupCount, input, // When we pass null as the return type, Calcite infers it for us.
    null, aggName);
}
Also used : ZetaSQLResolvedNodeKind(com.google.zetasql.ZetaSQLResolvedNodeKind) IntStream(java.util.stream.IntStream) AggregateCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall) ZetaSqlCalciteTranslationUtils(org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlCalciteTranslationUtils) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Arrays(java.util.Arrays) ResolvedNode(com.google.zetasql.resolvedast.ResolvedNode) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) ResolvedAggregateFunctionCall(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateFunctionCall) ResolvedAggregateScan(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateScan) RelCollations(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollations) ArrayList(java.util.ArrayList) ImmutableBitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet) SqlAggFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlAggFunction) RESOLVED_COLUMN_REF(com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_COLUMN_REF) LogicalProject(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalProject) FunctionSignature(com.google.zetasql.FunctionSignature) RESOLVED_CAST(com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_CAST) RESOLVED_GET_STRUCT_FIELD(com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_GET_STRUCT_FIELD) RESOLVED_LITERAL(com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_LITERAL) Collectors(java.util.stream.Collectors) LogicalAggregate(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalAggregate) List(java.util.List) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) ResolvedComputedColumn(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedComputedColumn) ResolvedExpr(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedExpr) TypeKind(com.google.zetasql.ZetaSQLType.TypeKind) SqlReturnTypeInference(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlReturnTypeInference) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) BeamZetaSqlCatalog(org.apache.beam.sdk.extensions.sql.zetasql.BeamZetaSqlCatalog) Collections(java.util.Collections) ArrayList(java.util.ArrayList) SqlAggFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlAggFunction) FunctionSignature(com.google.zetasql.FunctionSignature) UdafImpl(org.apache.beam.sdk.extensions.sql.impl.UdafImpl) ResolvedAggregateFunctionCall(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateFunctionCall) SqlReturnTypeInference(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlReturnTypeInference) ZetaSQLResolvedNodeKind(com.google.zetasql.ZetaSQLResolvedNodeKind) ZetaSQLResolvedNodeKind(com.google.zetasql.ZetaSQLResolvedNodeKind)

Example 3 with SqlReturnTypeInference

use of org.apache.calcite.sql.type.SqlReturnTypeInference 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 4 with SqlReturnTypeInference

use of org.apache.calcite.sql.type.SqlReturnTypeInference in project calcite by apache.

the class CalciteCatalogReader method infer.

private static SqlReturnTypeInference infer(final AggregateFunction function) {
    return new SqlReturnTypeInference() {

        public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
            final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
            final RelDataType type = function.getReturnType(typeFactory);
            return toSql(typeFactory, type);
        }
    };
}
Also used : SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)3 SqlReturnTypeInference (org.apache.calcite.sql.type.SqlReturnTypeInference)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 SqlOperatorBinding (org.apache.calcite.sql.SqlOperatorBinding)2 FunctionSignature (com.google.zetasql.FunctionSignature)1 ZetaSQLResolvedNodeKind (com.google.zetasql.ZetaSQLResolvedNodeKind)1 RESOLVED_CAST (com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_CAST)1 RESOLVED_COLUMN_REF (com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_COLUMN_REF)1 RESOLVED_GET_STRUCT_FIELD (com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_GET_STRUCT_FIELD)1 RESOLVED_LITERAL (com.google.zetasql.ZetaSQLResolvedNodeKind.ResolvedNodeKind.RESOLVED_LITERAL)1 TypeKind (com.google.zetasql.ZetaSQLType.TypeKind)1 ResolvedNode (com.google.zetasql.resolvedast.ResolvedNode)1 ResolvedAggregateFunctionCall (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateFunctionCall)1 ResolvedAggregateScan (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedAggregateScan)1 ResolvedComputedColumn (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedComputedColumn)1 ResolvedExpr (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedExpr)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1