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

The following examples show how to use org.apache.flink.table.catalog.CatalogFunction. 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: FunctionITCase.java    From flink with 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 #2
Source File: SqlToOperationConverter.java    From flink with 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 File: HiveCatalog.java    From flink with 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 #4
Source File: FunctionITCase.java    From flink with 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 #5
Source File: SqlToOperationConverter.java    From flink with 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 #6
Source File: SqlToOperationConverter.java    From flink with 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 #7
Source File: CreateCatalogFunctionOperation.java    From flink with 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 #8
Source File: AlterCatalogFunctionOperation.java    From flink with 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 #9
Source File: SqlToOperationConverter.java    From flink with 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 #10
Source File: HiveCatalog.java    From flink with 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 #11
Source File: HiveFunctionDefinitionFactory.java    From flink with 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 #12
Source File: HiveCatalogMetadataTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createFunction() {
	return new CatalogFunctionImpl(HiveSimpleUDF.class.getCanonicalName());
}
 
Example #13
Source File: CreateCatalogFunctionOperation.java    From flink with Apache License 2.0 4 votes vote down vote up
public CatalogFunction getCatalogFunction() {
	return this.catalogFunction;
}
 
Example #14
Source File: AlterCatalogFunctionOperation.java    From flink with Apache License 2.0 4 votes vote down vote up
public CatalogFunction getCatalogFunction() {
	return this.catalogFunction;
}
 
Example #15
Source File: HiveCatalogMetadataTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createPythonFunction() {
	return new CatalogFunctionImpl("test.func1", FunctionLanguage.PYTHON);
}
 
Example #16
Source File: HiveCatalogMetadataTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CatalogFunction createAnotherFunction() {
	return new CatalogFunctionImpl(HiveGenericUDF.class.getCanonicalName());
}
 
Example #17
Source File: PulsarCatalog.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
    throw new UnsupportedOperationException();
}
 
Example #18
Source File: AbstractJdbcCatalog.java    From flink with 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 #19
Source File: AbstractJdbcCatalog.java    From flink with 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 #20
Source File: AbstractJdbcCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example #21
Source File: KuduCatalog.java    From bahir-flink with 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 #22
Source File: AbstractReadOnlyCatalog.java    From bahir-flink with 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 #23
Source File: AbstractReadOnlyCatalog.java    From bahir-flink with 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 #24
Source File: HiveTableFactory.java    From flink with 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 #25
Source File: PulsarCatalog.java    From pulsar-flink with 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 #26
Source File: PulsarCatalog.java    From pulsar-flink with 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 #27
Source File: FunctionDefinitionFactory.java    From flink with 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 File: FunctionDefinitionFactory.java    From flink with 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);