Search in sources :

Example 1 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND in project beam by apache.

the class ZetaSqlCalciteTranslationUtils method toRexNode.

// Value conversion: ZetaSQL => Calcite
public static RexNode toRexNode(Value value, RexBuilder rexBuilder) {
    Type type = value.getType();
    if (value.isNull()) {
        return rexBuilder.makeNullLiteral(toCalciteType(type, true, rexBuilder));
    }
    switch(type.getKind()) {
        case TYPE_INT64:
            return rexBuilder.makeExactLiteral(new BigDecimal(value.getInt64Value()), toCalciteType(type, false, rexBuilder));
        case TYPE_DOUBLE:
            // Cannot simply call makeApproxLiteral() because +inf, -inf, and NaN cannot be represented
            // as BigDecimal. So we create wrapper functions here for these three cases such that we can
            // later recognize it and customize its unparsing in BeamBigQuerySqlDialect.
            double val = value.getDoubleValue();
            String wrapperFun = null;
            if (val == Double.POSITIVE_INFINITY) {
                wrapperFun = BeamBigQuerySqlDialect.DOUBLE_POSITIVE_INF_WRAPPER;
            } else if (val == Double.NEGATIVE_INFINITY) {
                wrapperFun = BeamBigQuerySqlDialect.DOUBLE_NEGATIVE_INF_WRAPPER;
            } else if (Double.isNaN(val)) {
                wrapperFun = BeamBigQuerySqlDialect.DOUBLE_NAN_WRAPPER;
            }
            RelDataType returnType = toCalciteType(type, false, rexBuilder);
            if (wrapperFun == null) {
                return rexBuilder.makeApproxLiteral(new BigDecimal(val), returnType);
            } else if (BeamBigQuerySqlDialect.DOUBLE_NAN_WRAPPER.equals(wrapperFun)) {
                // true, which should be false.)
                return rexBuilder.makeCall(SqlOperators.createZetaSqlFunction(wrapperFun, returnType.getSqlTypeName()), rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.random()), returnType));
            } else {
                return rexBuilder.makeCall(SqlOperators.createZetaSqlFunction(wrapperFun, returnType.getSqlTypeName()));
            }
        case TYPE_BOOL:
            return rexBuilder.makeLiteral(value.getBoolValue());
        case TYPE_STRING:
            // If not allow cast, rexBuilder() will only build a literal with CHAR type.
            return rexBuilder.makeLiteral(value.getStringValue(), toCalciteType(type, false, rexBuilder), true);
        case TYPE_BYTES:
            return rexBuilder.makeBinaryLiteral(new ByteString(value.getBytesValue().toByteArray()));
        case TYPE_NUMERIC:
            // its unparsing in BeamBigQuerySqlDialect.
            return rexBuilder.makeCall(SqlOperators.createZetaSqlFunction(BeamBigQuerySqlDialect.NUMERIC_LITERAL_WRAPPER, toCalciteType(type, false, rexBuilder).getSqlTypeName()), rexBuilder.makeExactLiteral(value.getNumericValue(), toCalciteType(type, false, rexBuilder)));
        case TYPE_DATE:
            return rexBuilder.makeDateLiteral(dateValueToDateString(value));
        case TYPE_TIME:
            return rexBuilder.makeTimeLiteral(timeValueToTimeString(value), rexBuilder.getTypeFactory().getTypeSystem().getMaxPrecision(SqlTypeName.TIME));
        case TYPE_DATETIME:
            return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(datetimeValueToTimestampString(value), rexBuilder.getTypeFactory().getTypeSystem().getMaxPrecision(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE));
        case TYPE_TIMESTAMP:
            return rexBuilder.makeTimestampLiteral(timestampValueToTimestampString(value), rexBuilder.getTypeFactory().getTypeSystem().getMaxPrecision(SqlTypeName.TIMESTAMP));
        case TYPE_ARRAY:
            return arrayValueToRexNode(value, rexBuilder);
        case TYPE_STRUCT:
            return structValueToRexNode(value, rexBuilder);
        case // internal only, used for DateTimestampPart
        TYPE_ENUM:
            return enumValueToRexNode(value, rexBuilder);
        default:
            throw new UnsupportedOperationException("Unknown ZetaSQL type: " + type.getKind().name());
    }
}
Also used : Type(com.google.zetasql.Type) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) StructType(com.google.zetasql.StructType) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) TimeString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimeString) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) DateString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.DateString) BigDecimal(java.math.BigDecimal)

Example 2 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND in project beam by apache.

the class ExpressionConverter method convertResolvedFunctionCall.

private RexNode convertResolvedFunctionCall(ResolvedFunctionCall functionCall, @Nullable List<ResolvedColumn> columnList, @Nullable List<RelDataTypeField> fieldList, Map<String, RexNode> outerFunctionArguments) {
    final String funGroup = functionCall.getFunction().getGroup();
    final String funName = functionCall.getFunction().getName();
    SqlOperator op = SqlOperatorMappingTable.create(functionCall);
    List<RexNode> operands = new ArrayList<>();
    if (PRE_DEFINED_WINDOW_FUNCTIONS.equals(funGroup)) {
        switch(funName) {
            case FIXED_WINDOW:
            case SESSION_WINDOW:
                // TODO: check size and type of window function argument list.
                // Add ts column reference to operands.
                operands.add(convertRexNodeFromResolvedExpr(functionCall.getArgumentList().get(0), columnList, fieldList, outerFunctionArguments));
                // Add fixed window size or session window gap to operands.
                operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(1)));
                break;
            case SLIDING_WINDOW:
                // Add ts column reference to operands.
                operands.add(convertRexNodeFromResolvedExpr(functionCall.getArgumentList().get(0), columnList, fieldList, outerFunctionArguments));
                // add sliding window emit frequency to operands.
                operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(1)));
                // add sliding window size to operands.
                operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(2)));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported function: " + funName + ". Only support TUMBLE, HOP, and SESSION now.");
        }
    } else if (ZETASQL_FUNCTION_GROUP_NAME.equals(funGroup)) {
        if (op == null) {
            Type returnType = functionCall.getSignature().getResultType().getType();
            if (returnType != null) {
                op = SqlOperators.createZetaSqlFunction(funName, ZetaSqlCalciteTranslationUtils.toCalciteType(returnType, false, rexBuilder()).getSqlTypeName());
            } else {
                throw new UnsupportedOperationException("Does not support ZetaSQL function: " + funName);
            }
        }
        for (ResolvedExpr expr : functionCall.getArgumentList()) {
            operands.add(convertRexNodeFromResolvedExpr(expr, columnList, fieldList, outerFunctionArguments));
        }
    } else if (USER_DEFINED_JAVA_SCALAR_FUNCTIONS.equals(funGroup)) {
        UserFunctionDefinitions.JavaScalarFunction javaScalarFunction = userFunctionDefinitions.javaScalarFunctions().get(functionCall.getFunction().getNamePath());
        ArrayList<RexNode> innerFunctionArguments = new ArrayList<>();
        for (int i = 0; i < functionCall.getArgumentList().size(); i++) {
            ResolvedExpr argExpr = functionCall.getArgumentList().get(i);
            RexNode argNode = convertRexNodeFromResolvedExpr(argExpr, columnList, fieldList, outerFunctionArguments);
            innerFunctionArguments.add(argNode);
        }
        return rexBuilder().makeCall(SqlOperators.createUdfOperator(functionCall.getFunction().getName(), javaScalarFunction.method(), USER_DEFINED_JAVA_SCALAR_FUNCTIONS, javaScalarFunction.jarPath()), innerFunctionArguments);
    } else if (USER_DEFINED_SQL_FUNCTIONS.equals(funGroup)) {
        ResolvedCreateFunctionStmt createFunctionStmt = userFunctionDefinitions.sqlScalarFunctions().get(functionCall.getFunction().getNamePath());
        ResolvedExpr functionExpression = createFunctionStmt.getFunctionExpression();
        ImmutableMap.Builder<String, RexNode> innerFunctionArguments = ImmutableMap.builder();
        for (int i = 0; i < functionCall.getArgumentList().size(); i++) {
            String argName = createFunctionStmt.getArgumentNameList().get(i);
            ResolvedExpr argExpr = functionCall.getArgumentList().get(i);
            RexNode argNode = convertRexNodeFromResolvedExpr(argExpr, columnList, fieldList, outerFunctionArguments);
            innerFunctionArguments.put(argName, argNode);
        }
        return this.convertRexNodeFromResolvedExpr(functionExpression, columnList, fieldList, innerFunctionArguments.build());
    } else {
        throw new UnsupportedOperationException("Does not support function group: " + funGroup);
    }
    SqlOperatorRewriter rewriter = SqlOperatorMappingTable.ZETASQL_FUNCTION_TO_CALCITE_SQL_OPERATOR_REWRITER.get(funName);
    if (rewriter != null) {
        return rewriter.apply(rexBuilder(), operands);
    } else {
        return rexBuilder().makeCall(op, operands);
    }
}
Also used : SqlOperator(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator) RexBuilder(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder) ArrayList(java.util.ArrayList) ResolvedExpr(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedExpr) ResolvedCreateFunctionStmt(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedCreateFunctionStmt) Type(com.google.zetasql.Type) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelRecordType) ResolvedLiteral(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedLiteral) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 3 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND 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 4 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND in project beam by apache.

the class SqlWindowTableFunction method checkOperandTypes.

@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
    // There should only be three operands, and number of operands are checked before
    // this call.
    final SqlNode operand0 = callBinding.operand(0);
    final SqlValidator validator = callBinding.getValidator();
    final RelDataType type = validator.getValidatedNodeType(operand0);
    if (type.getSqlTypeName() != SqlTypeName.ROW) {
        return throwValidationSignatureErrorOrReturnFalse(callBinding, throwOnFailure);
    }
    return true;
}
Also used : SqlValidator(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.validate.SqlValidator) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 5 with AND

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind.AND in project beam by apache.

the class SqlCaseWithValueOperatorRewriter method apply.

@Override
public RexNode apply(RexBuilder rexBuilder, List<RexNode> operands) {
    Preconditions.checkArgument(operands.size() % 2 == 0 && !operands.isEmpty(), "$case_with_value should have an even number of arguments greater than 0 in function call" + " (The value operand, the else operand, and paired when/then operands).");
    SqlOperator op = SqlStdOperatorTable.CASE;
    List<RexNode> newOperands = new ArrayList<>();
    RexNode value = operands.get(0);
    for (int i = 1; i < operands.size() - 2; i += 2) {
        RexNode when = operands.get(i);
        RexNode then = operands.get(i + 1);
        newOperands.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, ImmutableList.of(value, when)));
        newOperands.add(then);
    }
    RexNode elseOperand = Iterables.getLast(operands);
    newOperands.add(elseOperand);
    return rexBuilder.makeCall(op, newOperands);
}
Also used : SqlOperator(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)16 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Serializable (java.io.Serializable)7 Arrays (java.util.Arrays)7 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)7 IOException (java.io.IOException)6 Statement (java.sql.Statement)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Schema (org.apache.beam.sdk.schemas.Schema)6 PCollection (org.apache.beam.sdk.values.PCollection)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 StandardCharsets (java.nio.charset.StandardCharsets)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5