Search in sources :

Example 1 with BridgingSqlFunction

use of org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction in project flink by apache.

the class FunctionDefinitionConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // built-in functions without implementation are handled separately
    if (definition instanceof BuiltInFunctionDefinition) {
        final BuiltInFunctionDefinition builtInFunction = (BuiltInFunctionDefinition) definition;
        if (!builtInFunction.hasRuntimeImplementation()) {
            return Optional.empty();
        }
    }
    final TypeInference typeInference = definition.getTypeInference(context.getDataTypeFactory());
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    switch(definition.getKind()) {
        case SCALAR:
        case TABLE:
            final List<RexNode> args = call.getChildren().stream().map(context::toRexNode).collect(Collectors.toList());
            final BridgingSqlFunction sqlFunction = BridgingSqlFunction.of(context.getDataTypeFactory(), context.getTypeFactory(), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
            return Optional.of(context.getRelBuilder().call(sqlFunction, args));
        default:
            return Optional.empty();
    }
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with BridgingSqlFunction

use of org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction in project flink by apache.

the class RexNodeJsonSerializer method serializeSqlOperator.

// --------------------------------------------------------------------------------------------
/**
 * Logic shared with {@link AggregateCallJsonSerializer}.
 */
static void serializeSqlOperator(SqlOperator operator, JsonGenerator gen, SerializerProvider serializerProvider, boolean serializeCatalogObjects) throws IOException {
    if (operator.getSyntax() != SqlSyntax.FUNCTION) {
        gen.writeStringField(FIELD_NAME_SYNTAX, calciteToSerializable(operator.getSyntax()).getValue());
    }
    if (operator instanceof BridgingSqlFunction) {
        final BridgingSqlFunction function = (BridgingSqlFunction) operator;
        serializeBridgingSqlFunction(function.getName(), function.getResolvedFunction(), gen, serializerProvider, serializeCatalogObjects);
    } else if (operator instanceof BridgingSqlAggFunction) {
        final BridgingSqlAggFunction function = (BridgingSqlAggFunction) operator;
        serializeBridgingSqlFunction(function.getName(), function.getResolvedFunction(), gen, serializerProvider, serializeCatalogObjects);
    } else if (operator instanceof ScalarSqlFunction || operator instanceof TableSqlFunction || operator instanceof AggSqlFunction) {
        throw legacyException(operator.toString());
    } else {
        // We assume that all regular SqlOperators are internal. Only the function definitions
        // stack is exposed to the user and can thus be external.
        gen.writeStringField(FIELD_NAME_INTERNAL_NAME, BuiltInSqlOperator.toQualifiedName(operator));
    }
}
Also used : ScalarSqlFunction(org.apache.flink.table.planner.functions.utils.ScalarSqlFunction) TableSqlFunction(org.apache.flink.table.planner.functions.utils.TableSqlFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) BridgingSqlAggFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlAggFunction) AggSqlFunction(org.apache.flink.table.planner.functions.utils.AggSqlFunction)

Example 3 with BridgingSqlFunction

use of org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction in project flink by apache.

the class Expander method expanded.

/**
 * Expands identifiers in a given SQL string, returning a {@link Expanded}.
 */
public Expanded expanded(String ori) {
    final Map<SqlParserPos, SqlIdentifier> identifiers = new HashMap<>();
    final Map<String, SqlIdentifier> funcNameToId = new HashMap<>();
    final SqlNode oriNode = planner.parser().parse(ori);
    // parse again because validation is stateful, that means the node tree was probably
    // mutated.
    final SqlNode validated = planner.validate(planner.parser().parse(ori));
    validated.accept(new SqlBasicVisitor<Void>() {

        @Override
        public Void visit(SqlCall call) {
            SqlOperator operator = call.getOperator();
            if (operator instanceof BridgingSqlFunction) {
                final SqlIdentifier functionID = ((BridgingSqlFunction) operator).getSqlIdentifier();
                if (!functionID.isSimple()) {
                    funcNameToId.put(Util.last(functionID.names), functionID);
                }
            }
            return super.visit(call);
        }

        @Override
        public Void visit(SqlIdentifier identifier) {
            // and we stop expanding all of them.
            if (!identifier.names.get(0).startsWith("EXPR$")) {
                identifiers.putIfAbsent(identifier.getParserPosition(), identifier);
            }
            return null;
        }
    });
    return new Expanded(oriNode, identifiers, funcNameToId);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) HashMap(java.util.HashMap) SqlCall(org.apache.calcite.sql.SqlCall) SqlOperator(org.apache.calcite.sql.SqlOperator) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with BridgingSqlFunction

use of org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction in project flink by apache.

the class WrapJsonAggFunctionArgumentsRule method addProjections.

/**
 * Adds (wrapped) projections for affected arguments of the aggregation.
 *
 * <p>Note that we cannot override any of the projections as a field may be used multiple times,
 * and in particular outside of the aggregation call. Therefore, we explicitly add the wrapped
 * projection as an additional one.
 */
private void addProjections(RelOptCluster cluster, RelBuilder relBuilder, List<Integer> affectedArgs) {
    final BridgingSqlFunction operandToStringOperator = of(cluster, JSON_STRING);
    final List<RexNode> projects = new ArrayList<>();
    affectedArgs.stream().map(argIdx -> relBuilder.call(operandToStringOperator, relBuilder.field(argIdx))).forEach(projects::add);
    relBuilder.projectPlus(projects);
}
Also used : Mappings(org.apache.calcite.util.mapping.Mappings) MappingType(org.apache.calcite.util.mapping.MappingType) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelHint(org.apache.calcite.rel.hint.RelHint) FlinkHints(org.apache.flink.table.planner.hint.FlinkHints) TargetMapping(org.apache.calcite.util.mapping.Mappings.TargetMapping) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlJsonArrayAggAggFunction(org.apache.calcite.sql.fun.SqlJsonArrayAggAggFunction) Predicate(java.util.function.Predicate) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) RelRule(org.apache.calcite.plan.RelRule) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RelOptRule(org.apache.calcite.plan.RelOptRule) List(java.util.List) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) JSON_STRING(org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_STRING) SqlJsonObjectAggAggFunction(org.apache.calcite.sql.fun.SqlJsonObjectAggAggFunction) Internal(org.apache.flink.annotation.Internal) AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Collections(java.util.Collections) BridgingSqlFunction.of(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction.of) ArrayList(java.util.ArrayList) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

BridgingSqlFunction (org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction)4 RexNode (org.apache.calcite.rex.RexNode)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)1 RelRule (org.apache.calcite.plan.RelRule)1 RelNode (org.apache.calcite.rel.RelNode)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RelHint (org.apache.calcite.rel.hint.RelHint)1 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 SqlCall (org.apache.calcite.sql.SqlCall)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1