org.apache.flink.table.functions.ScalarFunction Java Examples

The following examples show how to use org.apache.flink.table.functions.ScalarFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HiveModuleTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHiveBuiltInFunction() {
	FunctionDefinition fd = new HiveModule().getFunctionDefinition("reverse").get();

	ScalarFunction func = ((ScalarFunctionDefinition) fd).getScalarFunction();
	HiveSimpleUDF udf = (HiveSimpleUDF) func;

	DataType[] inputType = new DataType[] {
		DataTypes.STRING()
	};

	udf.setArgumentTypesAndConstants(new Object[0], inputType);
	udf.getHiveResultType(new Object[0], inputType);

	udf.open(null);

	assertEquals("cba", udf.eval("abc"));
}
 
Example #2
Source File: HiveScalarSqlFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
private static SqlReturnTypeInference createReturnTypeInference(
		ScalarFunction function, FlinkTypeFactory typeFactory) {
	return opBinding -> {
		List<RelDataType> sqlTypes = opBinding.collectOperandTypes();
		LogicalType[] parameters = UserDefinedFunctionUtils.getOperandTypeArray(opBinding);

		Object[] constantArguments = new Object[sqlTypes.size()];
		for (int i = 0; i < sqlTypes.size(); i++) {
			if (!opBinding.isOperandNull(i, false) && opBinding.isOperandLiteral(i, false)) {
				constantArguments[i] = opBinding.getOperandLiteralValue(
						i, getDefaultExternalClassForType(parameters[i]));
			}
		}
		return invokeGetResultType(function, constantArguments, parameters, typeFactory);
	};
}
 
Example #3
Source File: HiveScalarSqlFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
private static SqlReturnTypeInference createReturnTypeInference(
		ScalarFunction function, FlinkTypeFactory typeFactory) {
	return opBinding -> {
		List<RelDataType> sqlTypes = opBinding.collectOperandTypes();
		LogicalType[] parameters = UserDefinedFunctionUtils.getOperandTypeArray(opBinding);

		Object[] constantArguments = new Object[sqlTypes.size()];
		for (int i = 0; i < sqlTypes.size(); i++) {
			if (!opBinding.isOperandNull(i, false) && opBinding.isOperandLiteral(i, false)) {
				constantArguments[i] = opBinding.getOperandLiteralValue(
						i, getDefaultExternalClassForType(parameters[i]));
			}
		}
		return invokeGetResultType(function, constantArguments, parameters, typeFactory);
	};
}
 
Example #4
Source File: LegacyScalarFunctionConvertRule.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
	FunctionDefinition def = call.getFunctionDefinition();
	if (def instanceof ScalarFunctionDefinition) {
		ScalarFunction scalaFunc = ((ScalarFunctionDefinition) def).getScalarFunction();
		FunctionIdentifier identifier = call.getFunctionIdentifier()
			.orElse(FunctionIdentifier.of(scalaFunc.functionIdentifier()));
		SqlFunction sqlFunction = UserDefinedFunctionUtils.createScalarSqlFunction(
			identifier,
			scalaFunc.toString(),
			scalaFunc,
			context.getTypeFactory());
		return Optional.of(context.getRelBuilder()
			.call(sqlFunction, toRexNodes(context, call.getChildren())));
	}
	return Optional.empty();
}
 
Example #5
Source File: TypeInferenceExtractor.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts a type inference from a {@link ScalarFunction}.
 */
public static TypeInference forScalarFunction(
		DataTypeFactory typeFactory,
		Class<? extends ScalarFunction> function) {
	final FunctionMappingExtractor mappingExtractor = new FunctionMappingExtractor(
		typeFactory,
		function,
		UserDefinedFunctionHelper.SCALAR_EVAL,
		createParameterSignatureExtraction(0),
		null,
		createReturnTypeResultExtraction(),
		createParameterAndReturnTypeVerification());
	return extractTypeInference(mappingExtractor);
}
 
Example #6
Source File: BatchOperator.java    From Alink with Apache License 2.0 5 votes vote down vote up
public BatchOperator udf(String selectedColName, String outputColName, ScalarFunction scalarFunction,
						 String[] reservedColNames) {
	return linkTo(
		new UDFBatchOp()
			.setSelectedCols(selectedColName)
			.setOutputCol(outputColName)
			.setFunc(scalarFunction)
			.setReservedCols(reservedColNames)
			.setMLEnvironmentId(getMLEnvironmentId())
	);
}
 
Example #7
Source File: BatchOperator.java    From Alink with Apache License 2.0 5 votes vote down vote up
public BatchOperator udf(String selectedColName, String outputColName, ScalarFunction scalarFunction) {
	return linkTo(
		new UDFBatchOp()
			.setSelectedCols(selectedColName)
			.setOutputCol(outputColName)
			.setFunc(scalarFunction)
			.setMLEnvironmentId(getMLEnvironmentId())
	);
}
 
Example #8
Source File: FunctionCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated Use {@link #registerTemporarySystemFunction(String, FunctionDefinition, boolean)} instead.
 */
@Deprecated
public void registerTempSystemScalarFunction(String name, ScalarFunction function) {
	UserDefinedFunctionHelper.prepareInstance(config, function);

	registerTempSystemFunction(
		name,
		new ScalarFunctionDefinition(name, function)
	);
}
 
Example #9
Source File: FunctionCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
public void registerTempCatalogScalarFunction(ObjectIdentifier oi, ScalarFunction function) {
	UserDefinedFunctionHelper.prepareInstance(config, function);

	registerTempCatalogFunction(
		oi,
		new ScalarFunctionDefinition(oi.getObjectName(), function)
	);
}
 
Example #10
Source File: StreamOperator.java    From Alink with Apache License 2.0 5 votes vote down vote up
public StreamOperator udf(String selectedColName, String outputColName, ScalarFunction scalarFunction,
						  String[] reservedColNames) {
	return linkTo(
		new UDFStreamOp()
			.setSelectedCols(selectedColName)
			.setOutputCol(outputColName)
			.setFunc(scalarFunction)
			.setReservedCols(reservedColNames)
			.setMLEnvironmentId(getMLEnvironmentId())
	);
}
 
Example #11
Source File: StreamOperator.java    From Alink with Apache License 2.0 5 votes vote down vote up
public StreamOperator udf(String selectedColName, String outputColName, ScalarFunction scalarFunction) {
	return linkTo(
		new UDFStreamOp()
			.setSelectedCols(selectedColName)
			.setOutputCol(outputColName)
			.setFunc(scalarFunction)
			.setMLEnvironmentId(getMLEnvironmentId())
	);
}
 
Example #12
Source File: AbstractFlinkClient.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private void register(StreamTableEnvironment env, String name, Object function) {
    if (function instanceof TableFunction) {
        env.registerFunction(name, (TableFunction) function);
    } else if (function instanceof AggregateFunction) {
        env.registerFunction(name, (AggregateFunction) function);
    } else if (function instanceof ScalarFunction) {
        env.registerFunction(name, (ScalarFunction) function);
    } else {
        throw new RuntimeException("Unknown UDF {} was found." + name);
    }
    LOGGER.info("register udf, name:{}, class:{}", name, function.getClass());
}
 
Example #13
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerFunctions() {
	if (tableEnv instanceof StreamTableEnvironment) {
		StreamTableEnvironment streamTableEnvironment = (StreamTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				streamTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				streamTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	} else {
		BatchTableEnvironment batchTableEnvironment = (BatchTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				batchTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				batchTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				batchTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	}
}
 
Example #14
Source File: HiveScalarSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ScalarFunction makeFunction(Object[] constantArguments, LogicalType[] argTypes) {
	ScalarFunction clone;
	try {
		clone = InstantiationUtil.clone(function);
	} catch (IOException | ClassNotFoundException e) {
		throw new RuntimeException(e);
	}
	return (ScalarFunction) invokeSetArgs(clone, constantArguments, argTypes);
}
 
Example #15
Source File: HiveScalarSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ScalarFunction makeFunction(Object[] constantArguments, LogicalType[] argTypes) {
	ScalarFunction clone;
	try {
		clone = InstantiationUtil.clone(function);
	} catch (IOException | ClassNotFoundException e) {
		throw new RuntimeException(e);
	}
	return (ScalarFunction) invokeSetArgs(clone, constantArguments, argTypes);
}
 
Example #16
Source File: RexNodeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexNode visit(UnresolvedCallExpression call) {
	FunctionDefinition func = call.getFunctionDefinition();
	switch (func.getKind()) {
		case SCALAR:
			if (func instanceof ScalarFunctionDefinition) {
				ScalarFunction scalaFunc = ((ScalarFunctionDefinition) func).getScalarFunction();
				List<RexNode> child = convertCallChildren(call.getChildren());
				SqlFunction sqlFunction = UserDefinedFunctionUtils.createScalarSqlFunction(
						scalaFunc.functionIdentifier(),
						scalaFunc.toString(),
						scalaFunc,
						typeFactory);
				return relBuilder.call(sqlFunction, child);
			} else {
				FunctionDefinition def = call.getFunctionDefinition();
				if (conversionsOfBuiltInFunc.containsKey(def)) {
					RexNodeConversion conversion = conversionsOfBuiltInFunc.get(def);
					return conversion.convert(call);
				} else {
					throw new UnsupportedOperationException(def.toString());
				}
			}

		default:
			throw new UnsupportedOperationException();
	}
}
 
Example #17
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void registerFunctions() {
	if (tableEnv instanceof StreamTableEnvironment) {
		StreamTableEnvironment streamTableEnvironment = (StreamTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				streamTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				streamTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	} else {
		BatchTableEnvironment batchTableEnvironment = (BatchTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				batchTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				batchTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				batchTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	}
}
 
Example #18
Source File: FunctionCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
public void registerScalarFunction(String name, ScalarFunction function) {
	UserFunctionsTypeHelper.validateInstantiation(function.getClass());
	registerFunction(
		name,
		new ScalarFunctionDefinition(name, function)
	);
}
 
Example #19
Source File: HiveScalarSqlFunction.java    From flink with Apache License 2.0 4 votes vote down vote up
public HiveScalarSqlFunction(
		FunctionIdentifier identifier, ScalarFunction function, FlinkTypeFactory typeFactory) {
	super(identifier, identifier.toString(), function,
			typeFactory, new Some<>(createReturnTypeInference(function, typeFactory)));
	this.function = function;
}
 
Example #20
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void registerFunction(String name, ScalarFunction function) {
	functionCatalog.registerTempSystemScalarFunction(
		name,
		function);
}
 
Example #21
Source File: TypeInferenceExtractorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
static TestSpec forScalarFunction(String description, Class<? extends ScalarFunction> function) {
	return new TestSpec(
		description == null ? function.getSimpleName() : description,
		() -> TypeInferenceExtractor.forScalarFunction(new DataTypeFactoryMock(), function));
}
 
Example #22
Source File: TypeInferenceExtractorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
static TestSpec forScalarFunction(Class<? extends ScalarFunction> function) {
	return forScalarFunction(null, function);
}
 
Example #23
Source File: ExecutionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
private void registerFunctions(Map<String, FunctionDefinition> functions) {
	if (tableEnv instanceof StreamTableEnvironment) {
		StreamTableEnvironment streamTableEnvironment = (StreamTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			// Blink planner uses FLIP-65 functions for scalar and table functions
			// aggregate functions still use the old type inference
			if (environment.getExecution().isBlinkPlanner()) {
				if (v instanceof ScalarFunction || v instanceof TableFunction) {
					streamTableEnvironment.createTemporarySystemFunction(k, (UserDefinedFunction) v);
				} else if (v instanceof AggregateFunction) {
					streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
				} else {
					throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
				}
			}
			// legacy
			else {
				if (v instanceof ScalarFunction) {
					streamTableEnvironment.registerFunction(k, (ScalarFunction) v);
				} else if (v instanceof AggregateFunction) {
					streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
				} else if (v instanceof TableFunction) {
					streamTableEnvironment.registerFunction(k, (TableFunction<?>) v);
				} else {
					throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
				}
			}
		});
	} else {
		BatchTableEnvironment batchTableEnvironment = (BatchTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				batchTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				batchTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				batchTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	}
}
 
Example #24
Source File: FunctionCatalogOperatorTable.java    From flink with Apache License 2.0 4 votes vote down vote up
private Optional<SqlFunction> convertToSqlFunction(
		SqlFunctionCategory category,
		String name,
		FunctionDefinition functionDefinition) {
	if (functionDefinition instanceof AggregateFunctionDefinition) {
		return convertAggregateFunction(name, (AggregateFunctionDefinition) functionDefinition);
	} else if (functionDefinition instanceof ScalarFunctionDefinition) {
		return convertScalarFunction(name, (ScalarFunctionDefinition) functionDefinition);
	} else if (functionDefinition instanceof TableFunctionDefinition &&
			category != null &&
			category.isTableFunction()) {
		return convertTableFunction(name, (TableFunctionDefinition) functionDefinition);
	} else if (functionDefinition instanceof BuiltInFunctionDefinition) {
		return Optional.empty();
	}
	LOG.warn(
		"The new type inference for functions is only supported in the Blink planner. " +
			"Falling back to legacy type inference for function '{}'.",
		functionDefinition.getClass().toString());
	if (functionDefinition instanceof ScalarFunction) {
		return convertToSqlFunction(
			category,
			name,
			new ScalarFunctionDefinition(
				name,
				(ScalarFunction) functionDefinition)
		);
	} else if (functionDefinition instanceof TableFunction) {
		final TableFunction<?> t = (TableFunction<?>) functionDefinition;
		return convertToSqlFunction(
			category,
			name,
			new TableFunctionDefinition(
				name,
				t,
				UserDefinedFunctionHelper.getReturnTypeOfTableFunction(t))
		);
	}
	throw new TableException(
		"The new type inference for functions is only supported in the Blink planner.");
}
 
Example #25
Source File: BatchOperator.java    From Alink with Apache License 2.0 4 votes vote down vote up
public static void registerFunction(String name, ScalarFunction function) {
	MLEnvironmentFactory.getDefault().getBatchTableEnvironment().registerFunction(name, function);
}
 
Example #26
Source File: UDFBatchOp.java    From Alink with Apache License 2.0 4 votes vote down vote up
public ScalarFunction getFunc() {
    return this.func;
}
 
Example #27
Source File: UDFBatchOp.java    From Alink with Apache License 2.0 4 votes vote down vote up
public UDFBatchOp setFunc(ScalarFunction udf) {
    this.func = udf;
    return this;
}
 
Example #28
Source File: StreamOperator.java    From Alink with Apache License 2.0 4 votes vote down vote up
public static void registerFunction(String name, ScalarFunction function) {
	MLEnvironmentFactory.getDefault().getStreamTableEnvironment().registerFunction(name, function);
}
 
Example #29
Source File: UDFStreamOp.java    From Alink with Apache License 2.0 4 votes vote down vote up
public ScalarFunction getFunc() {
    return this.func;
}
 
Example #30
Source File: UDFStreamOp.java    From Alink with Apache License 2.0 4 votes vote down vote up
public UDFStreamOp setFunc(ScalarFunction udf) {
    this.func = udf;
    return this;
}