Java Code Examples for org.apache.flink.table.catalog.CatalogFunction

The following examples show how to use org.apache.flink.table.catalog.CatalogFunction. These examples are extracted from open source projects. 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 Project: flink   Source File: HiveCatalog.java    License: Apache License 2.0 6 votes vote down vote up
private static Function instantiateHiveFunction(ObjectPath functionPath, CatalogFunction function) {

		boolean isGeneric = Boolean.valueOf(function.getProperties().get(CatalogConfig.IS_GENERIC));

		// Hive Function does not have properties map
		// thus, use a prefix in class name to distinguish Flink and Hive functions
		String functionClassName = isGeneric ?
			FLINK_FUNCTION_PREFIX + function.getClassName() :
			function.getClassName();

		return new Function(
			// due to https://issues.apache.org/jira/browse/HIVE-22053, we have to normalize function name ourselves
			HiveStringUtils.normalizeIdentifier(functionPath.getObjectName()),
			functionPath.getDatabaseName(),
			functionClassName,
			null,			// Owner name
			PrincipalType.GROUP,	// Temporarily set to GROUP type because it's required by Hive. May change later
			(int) (System.currentTimeMillis() / 1000),
			FunctionType.JAVA,		// FunctionType only has JAVA now
			new ArrayList<>()		// Resource URIs
		);
	}
 
Example 2
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 6 votes vote down vote up
/** Convert ALTER FUNCTION statement. */
private Operation convertAlterFunction(SqlAlterFunction sqlAlterFunction) {
	if (sqlAlterFunction.isSystemFunction()) {
		throw new ValidationException("Alter temporary system function is not supported");
	}

	FunctionLanguage language = parseLanguage(sqlAlterFunction.getFunctionLanguage());
	CatalogFunction catalogFunction = new CatalogFunctionImpl(
		sqlAlterFunction.getFunctionClassName().getValueAs(String.class),
		language);

	UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlAlterFunction.getFunctionIdentifier());
	ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
	return new AlterCatalogFunctionOperation(
		identifier,
		catalogFunction,
		sqlAlterFunction.isIfExists(),
		sqlAlterFunction.isTemporary()
	);
}
 
Example 3
Source Project: flink   Source File: FunctionITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAlterFunction() throws Exception {
	TableEnvironment tableEnv = getTableEnvironment();
	String create = "create function f3 as 'org.apache.flink.function.TestFunction'";
	String alter = "alter function f3 as 'org.apache.flink.function.TestFunction2'";

	ObjectPath objectPath = new ObjectPath("default_database", "f3");
	assertTrue(tableEnv.getCatalog("default_catalog").isPresent());
	Catalog catalog = tableEnv.getCatalog("default_catalog").get();
	tableEnv.sqlUpdate(create);
	CatalogFunction beforeUpdate = catalog.getFunction(objectPath);
	assertEquals("org.apache.flink.function.TestFunction", beforeUpdate.getClassName());

	tableEnv.sqlUpdate(alter);
	CatalogFunction afterUpdate = catalog.getFunction(objectPath);
	assertEquals("org.apache.flink.function.TestFunction2", afterUpdate.getClassName());
}
 
Example 4
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 6 votes vote down vote up
/** Convert ALTER FUNCTION statement. */
private Operation convertAlterFunction(SqlAlterFunction sqlAlterFunction) {
	if (sqlAlterFunction.isSystemFunction()) {
		throw new ValidationException("Alter temporary system function is not supported");
	}

	FunctionLanguage language = parseLanguage(sqlAlterFunction.getFunctionLanguage());
	CatalogFunction catalogFunction = new CatalogFunctionImpl(
		sqlAlterFunction.getFunctionClassName().getValueAs(String.class),
		language);

	UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlAlterFunction.getFunctionIdentifier());
	ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
	return new AlterCatalogFunctionOperation(
		identifier,
		catalogFunction,
		sqlAlterFunction.isIfExists(),
		sqlAlterFunction.isTemporary()
	);
}
 
Example 5
Source Project: flink   Source File: FunctionITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAlterFunction() throws Exception {
	String create = "create function f3 as 'org.apache.flink.function.TestFunction'";
	String alter = "alter function f3 as 'org.apache.flink.function.TestFunction2'";

	ObjectPath objectPath = new ObjectPath("default_database", "f3");
	assertTrue(tEnv().getCatalog("default_catalog").isPresent());
	Catalog catalog = tEnv().getCatalog("default_catalog").get();
	tEnv().executeSql(create);
	CatalogFunction beforeUpdate = catalog.getFunction(objectPath);
	assertEquals("org.apache.flink.function.TestFunction", beforeUpdate.getClassName());

	tEnv().executeSql(alter);
	CatalogFunction afterUpdate = catalog.getFunction(objectPath);
	assertEquals("org.apache.flink.function.TestFunction2", afterUpdate.getClassName());
}
 
Example 6
Source Project: flink   Source File: HiveCatalog.java    License: Apache License 2.0 5 votes vote down vote up
private static Function instantiateHiveFunction(ObjectPath functionPath, CatalogFunction function) {

		boolean isGeneric = function.isGeneric();

		// Hive Function does not have properties map
		// thus, use a prefix in class name to distinguish Flink and Hive functions
		String functionClassName;
		if (function.getFunctionLanguage().equals(FunctionLanguage.JAVA)) {
			functionClassName = isGeneric ?
				FLINK_FUNCTION_PREFIX + function.getClassName() :
				function.getClassName();
		} else if (function.getFunctionLanguage().equals(FunctionLanguage.PYTHON)) {
			functionClassName = FLINK_PYTHON_FUNCTION_PREFIX + function.getClassName();
		} else {
			throw new UnsupportedOperationException("HiveCatalog supports only creating" +
				" JAVA or PYTHON based function for now");
		}

		return new Function(
			// due to https://issues.apache.org/jira/browse/HIVE-22053, we have to normalize function name ourselves
			functionPath.getObjectName().trim().toLowerCase(),
			functionPath.getDatabaseName(),
			functionClassName,
			null,			// Owner name
			PrincipalType.GROUP,	// Temporarily set to GROUP type because it's required by Hive. May change later
			(int) (System.currentTimeMillis() / 1000),
			FunctionType.JAVA,		// FunctionType only has JAVA now
			new ArrayList<>()		// Resource URIs
		);
	}
 
Example 7
Source Project: flink   Source File: HiveFunctionDefinitionFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FunctionDefinition createFunctionDefinition(String name, CatalogFunction catalogFunction) {
	if (catalogFunction.isGeneric()) {
		return FunctionDefinitionUtil.createFunctionDefinition(name, catalogFunction.getClassName());
	}

	return createFunctionDefinitionFromHiveFunction(name, catalogFunction.getClassName());
}
 
Example 8
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert CREATE FUNCTION statement. */
private Operation convertCreateFunction(SqlCreateFunction sqlCreateFunction) {
	UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlCreateFunction.getFunctionIdentifier());

	if (sqlCreateFunction.isSystemFunction()) {
		return new CreateTempSystemFunctionOperation(
			unresolvedIdentifier.getObjectName(),
			sqlCreateFunction.getFunctionClassName().getValueAs(String.class),
			sqlCreateFunction.isIfNotExists(),
			parseLanguage(sqlCreateFunction.getFunctionLanguage())
		);
	} else {
		FunctionLanguage language = parseLanguage(sqlCreateFunction.getFunctionLanguage());
		CatalogFunction catalogFunction = new CatalogFunctionImpl(
			sqlCreateFunction.getFunctionClassName().getValueAs(String.class),
			language);

		ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);

		return new CreateCatalogFunctionOperation(
			identifier,
			catalogFunction,
			sqlCreateFunction.isIfNotExists(),
			sqlCreateFunction.isTemporary()
		);
	}
}
 
Example 9
Source Project: flink   Source File: AlterCatalogFunctionOperation.java    License: Apache License 2.0 5 votes vote down vote up
public AlterCatalogFunctionOperation(
		ObjectIdentifier functionIdentifier,
		CatalogFunction catalogFunction,
		boolean ifExists,
		boolean isTemporary) {
	this.functionIdentifier = functionIdentifier;
	this.catalogFunction = catalogFunction;
	this.ifExists = ifExists;
	this.isTemporary = isTemporary;
}
 
Example 10
Source Project: flink   Source File: CreateCatalogFunctionOperation.java    License: Apache License 2.0 5 votes vote down vote up
public CreateCatalogFunctionOperation(
	ObjectIdentifier functionIdentifier,
	CatalogFunction catalogFunction,
	boolean ignoreIfExists,
	boolean isTemporary) {
	this.functionIdentifier = functionIdentifier;
	this.catalogFunction = catalogFunction;
	this.ignoreIfExists = ignoreIfExists;
	this.isTemporary = isTemporary;
}
 
Example 11
Source Project: flink   Source File: SqlToOperationConverter.java    License: Apache License 2.0 5 votes vote down vote up
/** Convert CREATE FUNCTION statement. */
private Operation convertCreateFunction(SqlCreateFunction sqlCreateFunction) {
	UnresolvedIdentifier unresolvedIdentifier =
		UnresolvedIdentifier.of(sqlCreateFunction.getFunctionIdentifier());

	if (sqlCreateFunction.isSystemFunction()) {
		return new CreateTempSystemFunctionOperation(
			unresolvedIdentifier.getObjectName(),
			sqlCreateFunction.getFunctionClassName().getValueAs(String.class),
			sqlCreateFunction.isIfNotExists(),
			parseLanguage(sqlCreateFunction.getFunctionLanguage())
		);
	} else {
		FunctionLanguage language = parseLanguage(sqlCreateFunction.getFunctionLanguage());
		CatalogFunction catalogFunction = new CatalogFunctionImpl(
			sqlCreateFunction.getFunctionClassName().getValueAs(String.class),
			language);
		ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);

		return new CreateCatalogFunctionOperation(
			identifier,
			catalogFunction,
			sqlCreateFunction.isIfNotExists(),
			sqlCreateFunction.isTemporary()
		);
	}
}
 
Example 12
Source Project: pulsar-flink   Source File: PulsarCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
    throw new UnsupportedOperationException();
}
 
Example 13
Source Project: pulsar-flink   Source File: PulsarCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void createFunction(ObjectPath functionPath, CatalogFunction function, boolean ignoreIfExists) throws FunctionAlreadyExistException, DatabaseNotExistException, CatalogException {
    throw new UnsupportedOperationException();
}
 
Example 14
Source Project: pulsar-flink   Source File: PulsarCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
    throw new UnsupportedOperationException();
}
 
Example 15
Source Project: flink   Source File: HiveTableFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public FunctionDefinition createFunctionDefinition(String name, CatalogFunction catalogFunction) {
	String functionClassName = catalogFunction.getClassName();

	if (Boolean.valueOf(catalogFunction.getProperties().get(CatalogConfig.IS_GENERIC))) {
		throw new TableException(
			String.format("HiveFunctionDefinitionFactory does not support generic functions %s yet", name));
	}

	Class clazz;
	try {
		clazz = Thread.currentThread().getContextClassLoader().loadClass(functionClassName);

		LOG.info("Successfully loaded Hive udf '{}' with class '{}'", name, functionClassName);
	} catch (ClassNotFoundException e) {
		throw new TableException(
			String.format("Failed to initiate an instance of class %s.", functionClassName), e);
	}

	if (UDF.class.isAssignableFrom(clazz)) {
		LOG.info("Transforming Hive function '{}' into a HiveSimpleUDF", name);

		return new ScalarFunctionDefinition(
			name,
			new HiveSimpleUDF(new HiveFunctionWrapper<>(functionClassName))
		);
	} else if (GenericUDF.class.isAssignableFrom(clazz)) {
		LOG.info("Transforming Hive function '{}' into a HiveGenericUDF", name);

		return new ScalarFunctionDefinition(
			name,
			new HiveGenericUDF(new HiveFunctionWrapper<>(functionClassName))
		);
	} else if (GenericUDTF.class.isAssignableFrom(clazz)) {
		LOG.info("Transforming Hive function '{}' into a HiveGenericUDTF", name);

		HiveGenericUDTF udtf = new HiveGenericUDTF(new HiveFunctionWrapper<>(functionClassName));

		return new TableFunctionDefinition(
			name,
			udtf,
			GenericTypeInfo.of(Row.class)
		);
	} else if (GenericUDAFResolver2.class.isAssignableFrom(clazz) || UDAF.class.isAssignableFrom(clazz)) {
		HiveGenericUDAF udaf;

		if (GenericUDAFResolver2.class.isAssignableFrom(clazz)) {
			LOG.info(
				"Transforming Hive function '{}' into a HiveGenericUDAF with no UDAF bridging and Hive version %s",
				name, hiveVersion);

			udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), false, hiveVersion);
		} else {
			LOG.info(
				"Transforming Hive function '{}' into a HiveGenericUDAF with UDAF bridging and Hive version %s",
				name, hiveVersion);

			udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), true, hiveVersion);
		}

		return new AggregateFunctionDefinition(
			name,
			udaf,
			GenericTypeInfo.of(Object.class),
			GenericTypeInfo.of(GenericUDAFEvaluator.AggregationBuffer.class)
		);
	} else {
		throw new IllegalArgumentException(
			String.format("HiveFunctionDefinitionFactory cannot initiate FunctionDefinition for class %s", functionClassName));
	}
}
 
Example 16
Source Project: bahir-flink   Source File: AbstractReadOnlyCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void createFunction(ObjectPath functionPath, CatalogFunction function, boolean ignoreIfExists) throws FunctionAlreadyExistException, DatabaseNotExistException, CatalogException {
    throw UNSUPPORTED_ERR;
}
 
Example 17
Source Project: bahir-flink   Source File: AbstractReadOnlyCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
    throw UNSUPPORTED_ERR;
}
 
Example 18
Source Project: bahir-flink   Source File: KuduCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
    throw new FunctionNotExistException(getName(), functionPath);
}
 
Example 19
Source Project: flink   Source File: AbstractJdbcCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example 20
Source Project: flink   Source File: AbstractJdbcCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void createFunction(ObjectPath functionPath, CatalogFunction function, boolean ignoreIfExists) throws FunctionAlreadyExistException, DatabaseNotExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example 21
Source Project: flink   Source File: AbstractJdbcCatalog.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example 22
Source Project: flink   Source File: HiveCatalogMetadataTestBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createFunction() {
	return new CatalogFunctionImpl(HiveSimpleUDF.class.getCanonicalName());
}
 
Example 23
Source Project: flink   Source File: HiveCatalogMetadataTestBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createAnotherFunction() {
	return new CatalogFunctionImpl(HiveGenericUDF.class.getCanonicalName());
}
 
Example 24
Source Project: flink   Source File: HiveCatalogMetadataTestBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createPythonFunction() {
	return new CatalogFunctionImpl("test.func1", FunctionLanguage.PYTHON);
}
 
Example 25
Source Project: flink   Source File: AlterCatalogFunctionOperation.java    License: Apache License 2.0 4 votes vote down vote up
public CatalogFunction getCatalogFunction() {
	return this.catalogFunction;
}
 
Example 26
Source Project: flink   Source File: CreateCatalogFunctionOperation.java    License: Apache License 2.0 4 votes vote down vote up
public CatalogFunction getCatalogFunction() {
	return this.catalogFunction;
}
 
Example 27
Source Project: flink   Source File: FunctionDefinitionFactory.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link FunctionDefinition} from given {@link CatalogFunction}.
 *
 * @param name name of the {@link CatalogFunction}
 * @param catalogFunction the catalog function
 * @return a {@link FunctionDefinition}
 */
FunctionDefinition createFunctionDefinition(String name, CatalogFunction catalogFunction);
 
Example 28
Source Project: flink   Source File: FunctionDefinitionFactory.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link FunctionDefinition} from given {@link CatalogFunction}.
 *
 * @param name name of the {@link CatalogFunction}
 * @param catalogFunction the catalog function
 * @return a {@link FunctionDefinition}
 */
FunctionDefinition createFunctionDefinition(String name, CatalogFunction catalogFunction);