Search in sources :

Example 61 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project flink by apache.

the class SqlValidatorImpl method validateCall.

public void validateCall(SqlCall call, SqlValidatorScope scope) {
    final SqlOperator operator = call.getOperator();
    if ((call.operandCount() == 0) && (operator.getSyntax() == SqlSyntax.FUNCTION_ID) && !call.isExpanded() && !this.config.sqlConformance().allowNiladicParentheses()) {
        // SqlIdentifier.)
        throw handleUnresolvedFunction(call, (SqlFunction) operator, ImmutableList.of(), null);
    }
    SqlValidatorScope operandScope = scope.getOperandScope(call);
    if (operator instanceof SqlFunction && ((SqlFunction) operator).getFunctionType() == SqlFunctionCategory.MATCH_RECOGNIZE && !(operandScope instanceof MatchRecognizeScope)) {
        throw newValidationError(call, Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
    }
    // Delegate validation to the operator.
    operator.validateCall(call, this, scope, operandScope);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 62 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project flink by apache.

the class OverConvertRule method createBound.

private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) {
    if (bound instanceof CallExpression) {
        CallExpression callExpr = (CallExpression) bound;
        FunctionDefinition func = callExpr.getFunctionDefinition();
        if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE.equals(func)) {
            SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
            return RexWindowBound.create(unbounded, null);
        } else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE.equals(func)) {
            SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
            return RexWindowBound.create(currentRow, null);
        } else {
            throw new IllegalArgumentException("Unexpected expression: " + bound);
        }
    } else if (bound instanceof ValueLiteralExpression) {
        RelDataType returnType = context.getTypeFactory().createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
        SqlOperator sqlOperator = new SqlPostfixOperator(sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null);
        SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
        SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);
        ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
        RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class).map(v -> context.getRelBuilder().literal(v)).orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class)));
        List<RexNode> expressions = new ArrayList<>();
        expressions.add(literalRexNode);
        RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall(returnType, sqlOperator, expressions);
        return RexWindowBound.create(node, rexNode);
    } else {
        throw new TableException("Unexpected expression: " + bound);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) DecimalType(org.apache.flink.table.types.logical.DecimalType) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CallExpression(org.apache.flink.table.expressions.CallExpression) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 63 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project flink by apache.

the class RexNodeJsonDeserializer method deserializeInternalFunction.

private static SqlOperator deserializeInternalFunction(String internalName, SqlSyntax syntax, SerdeContext serdeContext) {
    // Try $FUNC$1
    final Optional<SqlOperator> internalOperator = lookupOptionalSqlOperator(FunctionIdentifier.of(internalName), syntax, serdeContext, false);
    if (internalOperator.isPresent()) {
        return internalOperator.get();
    }
    // Try FUNC
    final String publicName = BuiltInSqlOperator.extractNameFromQualifiedName(internalName);
    final Optional<SqlOperator> latestOperator = lookupOptionalSqlOperator(FunctionIdentifier.of(publicName), syntax, serdeContext, true);
    if (latestOperator.isPresent()) {
        return latestOperator.get();
    }
    throw new TableException(String.format("Could not resolve internal system function '%s'. " + "This is a bug, please file an issue.", internalName));
}
Also used : TableException(org.apache.flink.table.api.TableException) BuiltInSqlOperator(org.apache.flink.table.planner.functions.sql.BuiltInSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) DateString(org.apache.calcite.util.DateString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString)

Example 64 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project flink by apache.

the class RexNodeJsonDeserializer method deserializeCatalogFunction.

private static SqlOperator deserializeCatalogFunction(JsonNode jsonNode, SqlSyntax syntax, SerdeContext serdeContext) {
    final CatalogPlanRestore restoreStrategy = serdeContext.getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
    final FunctionIdentifier identifier = FunctionIdentifier.of(ObjectIdentifierJsonDeserializer.deserialize(jsonNode.required(FIELD_NAME_CATALOG_NAME).asText(), serdeContext));
    switch(restoreStrategy) {
        case ALL:
            {
                final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, false);
                if (lookupOperator.isPresent()) {
                    return lookupOperator.get();
                } else if (jsonNode.has(FIELD_NAME_CLASS)) {
                    return deserializeFunctionClass(jsonNode, serdeContext);
                }
                throw missingFunctionFromCatalog(identifier, false);
            }
        case ALL_ENFORCED:
            {
                if (jsonNode.has(FIELD_NAME_CLASS)) {
                    return deserializeFunctionClass(jsonNode, serdeContext);
                }
                final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, false);
                if (lookupOperator.map(RexNodeJsonDeserializer::isTemporary).orElse(false)) {
                    return lookupOperator.get();
                }
                throw lookupDisabled(identifier);
            }
        case IDENTIFIER:
            final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, true);
            if (lookupOperator.isPresent()) {
                return lookupOperator.get();
            } else {
                throw missingFunctionFromCatalog(identifier, true);
            }
        default:
            throw new TableException("Unsupported restore strategy: " + restoreStrategy);
    }
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) TableException(org.apache.flink.table.api.TableException) Optional(java.util.Optional) BuiltInSqlOperator(org.apache.flink.table.planner.functions.sql.BuiltInSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore)

Example 65 with SqlOperator

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project flink by apache.

the class RexNodeJsonDeserializer method deserializeCall.

private static RexNode deserializeCall(JsonNode jsonNode, SerdeContext serdeContext) throws IOException {
    final SqlOperator operator = deserializeSqlOperator(jsonNode, serdeContext);
    final ArrayNode operandNodes = (ArrayNode) jsonNode.get(FIELD_NAME_OPERANDS);
    final List<RexNode> rexOperands = new ArrayList<>();
    for (JsonNode node : operandNodes) {
        rexOperands.add(deserialize(node, serdeContext));
    }
    final RelDataType callType;
    if (jsonNode.has(FIELD_NAME_TYPE)) {
        final JsonNode typeNode = jsonNode.get(FIELD_NAME_TYPE);
        callType = RelDataTypeJsonDeserializer.deserialize(typeNode, serdeContext);
    } else {
        callType = serdeContext.getRexBuilder().deriveReturnType(operator, rexOperands);
    }
    return serdeContext.getRexBuilder().makeCall(callType, operator, rexOperands);
}
Also used : BuiltInSqlOperator(org.apache.flink.table.planner.functions.sql.BuiltInSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) RelDataType(org.apache.calcite.rel.type.RelDataType) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

SqlOperator (org.apache.calcite.sql.SqlOperator)129 ArrayList (java.util.ArrayList)44 RexNode (org.apache.calcite.rex.RexNode)41 RelDataType (org.apache.calcite.rel.type.RelDataType)25 SqlCall (org.apache.calcite.sql.SqlCall)24 RexCall (org.apache.calcite.rex.RexCall)21 SqlNode (org.apache.calcite.sql.SqlNode)21 SqlKind (org.apache.calcite.sql.SqlKind)15 List (java.util.List)13 SqlFunction (org.apache.calcite.sql.SqlFunction)13 RelNode (org.apache.calcite.rel.RelNode)12 RexBuilder (org.apache.calcite.rex.RexBuilder)11 RexInputRef (org.apache.calcite.rex.RexInputRef)11 NlsString (org.apache.calcite.util.NlsString)11 Test (org.junit.Test)11 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)10 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)10 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)9 SqlOperator (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9