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();
}
}
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));
}
}
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);
}
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);
}
Aggregations