Skip to content

[#9533] feat(trino-connector): Add UDF adaptation for Trino connector#10494

Merged
diqiu50 merged 2 commits intoapache:mainfrom
laserninja:trino-connector-udf-adaptation
Apr 1, 2026
Merged

[#9533] feat(trino-connector): Add UDF adaptation for Trino connector#10494
diqiu50 merged 2 commits intoapache:mainfrom
laserninja:trino-connector-udf-adaptation

Conversation

@laserninja
Copy link
Copy Markdown
Collaborator

@laserninja laserninja commented Mar 21, 2026

What changes were proposed in this pull request?

Add support for consuming Gravitino function metadata in the Trino connector by mapping SQL functions with RuntimeType.TRINO to Trino's built-in LanguageFunction API.

Why are the changes needed?

  • CatalogConnectorMetadata: Add FunctionCatalog support with graceful fallback when catalog does not support functions. Add methods to list and retrieve functions from Gravitino server.
  • GravitinoMetadata: Implement listLanguageFunctions() and getLanguageFunctions() to convert Gravitino SQL functions with TRINO runtime to Trino LanguageFunction instances.
  • TestGravitinoMetadataFunction: 8 unit tests covering listing, filtering, multiple definitions, signature tokens, and edge cases.
  • TestCatalogConnectorMetadataFunction: 7 unit tests covering function catalog support detection, listing, retrieval, and error handling.

Fix #9533

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Added test cases.

@roryqi roryqi requested a review from diqiu50 March 22, 2026 09:51
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 23, 2026

Code Coverage Report

Overall Project 65.03% -0.32% 🟢
Files changed 13.46% 🔴

Module Coverage
aliyun 1.73% 🔴
api 47.14% 🟢
authorization-common 85.96% 🟢
aws 1.1% 🔴
azure 2.6% 🔴
catalog-common 10.0% 🔴
catalog-fileset 80.02% 🟢
catalog-hive 80.98% 🟢
catalog-jdbc-clickhouse 79.06% 🟢
catalog-jdbc-common 42.89% 🟢
catalog-jdbc-doris 80.28% 🟢
catalog-jdbc-hologres 54.03% 🟢
catalog-jdbc-mysql 79.23% 🟢
catalog-jdbc-oceanbase 78.38% 🟢
catalog-jdbc-postgresql 82.05% 🟢
catalog-jdbc-starrocks 78.27% 🟢
catalog-kafka 77.01% 🟢
catalog-lakehouse-generic 45.07% 🟢
catalog-lakehouse-hudi 79.1% 🟢
catalog-lakehouse-iceberg 87.15% 🟢
catalog-lakehouse-paimon 77.71% 🟢
catalog-model 77.72% 🟢
cli 44.51% 🟢
client-java 77.83% 🟢
common 49.42% 🟢
core 81.13% 🟢
filesystem-hadoop3 76.97% 🟢
flink 38.86% 🔴
flink-runtime 0.0% 🔴
gcp 14.2% 🔴
hadoop-common 10.39% 🔴
hive-metastore-common 45.82% 🟢
iceberg-common 50.0% 🟢
iceberg-rest-server 66.56% 🟢
integration-test-common 0.0% 🔴
jobs 66.17% 🟢
lance-common 23.88% 🔴
lance-rest-server 57.84% 🟢
lineage 53.02% 🟢
optimizer 82.95% 🟢
optimizer-api 21.95% 🔴
server 85.62% 🟢
server-common 70.14% 🟢
spark 32.79% 🔴
spark-common 39.09% 🔴
trino-connector 32.6% -3.11% 🔴
Files
Module File Coverage
trino-connector CatalogConnectorMetadata.java 15.67% 🔴
GravitinoMetadata.java 12.57% 🔴

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Trino connector support for consuming Gravitino function metadata by adapting Gravitino SQL UDFs (runtime = TRINO) into Trino LanguageFunction objects, with unit tests validating the behavior.

Changes:

  • Extend CatalogConnectorMetadata with optional FunctionCatalog support, including graceful fallback when functions aren’t supported.
  • Implement listLanguageFunctions() and getLanguageFunctions() in GravitinoMetadata, converting Gravitino function definitions/impls into Trino LanguageFunction instances.
  • Add unit tests for function-catalog detection/listing/retrieval and for language-function conversion/filtering/signature-token behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/CatalogConnectorMetadata.java Adds function-catalog discovery plus list/get function helpers with fallback when unsupported.
trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java Adds Trino LanguageFunction listing/getting by converting Gravitino TRINO SQL impls.
trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/catalog/TestCatalogConnectorMetadataFunction.java Tests catalog function support detection, listing, retrieval, and error handling.
trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/TestGravitinoMetadataFunction.java Tests conversion/filtering of Gravitino functions to Trino LanguageFunction, including signature token cases.

…nector

Add support for consuming Gravitino function metadata in the Trino
connector by mapping SQL functions with RuntimeType.TRINO to Trino's
built-in LanguageFunction API.

Changes:
- CatalogConnectorMetadata: Add FunctionCatalog support with graceful
  fallback when catalog does not support functions. Add methods to list
  and retrieve functions from Gravitino server.
- GravitinoMetadata: Implement listLanguageFunctions() and
  getLanguageFunctions() to convert Gravitino SQL functions with TRINO
  runtime to Trino LanguageFunction instances.
- TestGravitinoMetadataFunction: 8 unit tests covering listing,
  filtering, multiple definitions, signature tokens, and edge cases.
- TestCatalogConnectorMetadataFunction: 7 unit tests covering function
  catalog support detection, listing, retrieval, and error handling.

Fixes apache#9533
@laserninja laserninja force-pushed the trino-connector-udf-adaptation branch from 1974402 to 3d88499 Compare March 23, 2026 21:53
@laserninja
Copy link
Copy Markdown
Collaborator Author

@diqiu50 addressed the bot's comments/

@laserninja laserninja force-pushed the trino-connector-udf-adaptation branch from aeebb2e to 5f4239f Compare March 24, 2026 05:32
@diqiu50
Copy link
Copy Markdown
Contributor

diqiu50 commented Mar 24, 2026

Please update the PR description to match our format.

@diqiu50 diqiu50 added 1.3.0 Release v1.3.0 and removed 1.3.0 Release v1.3.0 labels Mar 24, 2026
@laserninja
Copy link
Copy Markdown
Collaborator Author

Done, I used some closed PR as a reference.

Copy link
Copy Markdown
Contributor

@diqiu50 diqiu50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some integration test for UDF
You can refer to the current Trino integration tests.

@laserninja laserninja force-pushed the trino-connector-udf-adaptation branch from 5f4239f to aed4474 Compare March 25, 2026 16:09
@laserninja
Copy link
Copy Markdown
Collaborator Author

Created TrinoUDFIT.java with 4 integration tests tagged @Tag("gravitino-docker-test"):

  • testListLanguageFunctionsShowsRegisteredUDF which registers a TRINO SQL function via Gravitino client, verifies it appears in Trino via SHOW FUNCTIONS
  • testListLanguageFunctionsFiltersNonTrinoRuntime which registers a SPARK function, verifies it does NOT appear in Trino
  • testMultipleUDFsInSameSchema that registers two TRINO functions, verifies both appear
  • testNoFunctionsWhenSchemaIsEmpty that verifies empty schema has no UDFs

Copy link
Copy Markdown
Contributor

@diqiu50 diqiu50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed you added integration tests—why not include test scripts as well, like those in trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets?
Can Trino create functions in Gravitino via SQL?

}
return Arrays.stream(catalogConnectorMetadata.listFunctionInfos(schemaName))
.flatMap(function -> toLanguageFunctions(function).stream())
.collect(Collectors.toList());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flatMap(XX).toList() is OK

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Types.IntegerType.get(),
FunctionImpls.of(
FunctionImpls.ofSql(FunctionImpl.RuntimeType.TRINO, "RETURN x + 1")))));
Assertions.assertNotNull(function);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments for this function definition

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@laserninja laserninja force-pushed the trino-connector-udf-adaptation branch from aed4474 to f9d5505 Compare March 28, 2026 04:59
@laserninja
Copy link
Copy Markdown
Collaborator Author

This PR only implements the read path 'listLanguageFunctions'/'getLanguageFunctions'. Trino cannot create functions in Gravitino via SQL yet; the write path (CREATE FUNCTION) is not implemented in this PR. Because of that, the SQL testset approach doesn't work here since we need to register functions via the Gravitino Java client first, then verify them from the Trino side. SQL testsets would be a great fit once the CREATE FUNCTION write path is added in a follow-up.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

import org.apache.gravitino.SupportsSchemas;
import org.apache.gravitino.client.GravitinoMetalake;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchFunctionException;
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import for NoSuchFunctionException is unused (it’s only referenced in JavaDoc). Spotless is configured with removeUnusedImports(), so this will fail formatting checks unless removed or referenced in code/JavaDoc using FQN without importing.

Suggested change
import org.apache.gravitino.exceptions.NoSuchFunctionException;

Copilot uses AI. Check for mistakes.
Comment on lines +425 to +434
/**
* Lists all functions with details in the specified schema.
*
* @param schemaName the name of the schema
* @return an array of functions, or an empty array if functions are not supported
*/
public Function[] listFunctionInfos(String schemaName) {
if (!supportsFunctions()) {
throw new UnsupportedOperationException("Catalog does not support functions");
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaDoc for listFunctionInfos says it returns an empty array when functions are not supported, but the method currently throws UnsupportedOperationException in that case. Please align the JavaDoc with the actual behavior (or change the method to match the documented contract).

Copilot uses AI. Check for mistakes.
Comment on lines +746 to +750
} catch (Exception e) {
LOG.warn(
"Failed to build signature token for function {}: {}",
function.name(),
e.getMessage());
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toLanguageFunctions catches a generic Exception and logs only e.getMessage(), which can hide the root cause and stack trace (and the repo guidelines discourage catch (Exception e)). Catch the specific expected exceptions (e.g., unsupported type conversion) and include the exception in the log call so failures are diagnosable.

Suggested change
} catch (Exception e) {
LOG.warn(
"Failed to build signature token for function {}: {}",
function.name(),
e.getMessage());
} catch (IllegalArgumentException | UnsupportedOperationException e) {
LOG.warn(
"Failed to build signature token for function {}",
function.name(),
e);

Copilot uses AI. Check for mistakes.
LOG.warn(
"Failed to build signature token for function {}: {}",
function.name(),
e.getMessage());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing stack

* Lists all functions with details in the specified schema.
*
* @param schemaName the name of the schema
* @return an array of functions, or an empty array if functions are not supported
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update docs

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

LOG.info("SHOW FUNCTIONS for empty schema: {}", result);
// For an empty schema, the result should not contain any custom function names
Assertions.assertFalse(
result.contains("udf_"), "Expected no UDFs in empty schema. Got: " + result);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify the criteria here? udf_ is too vague.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


LOG.info("SELECT result: {}", result);
Assertions.assertTrue(
result.contains("10"), "Expected SELECT test_add_five(5) to return 10. Got: " + result);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same problem

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

metadata.getLanguageFunctions(
session, new SchemaFunctionName("test_schema", "no_such_func"));
assertTrue(functions.isEmpty());
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some tests for supportsFunctions=false

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@diqiu50
Copy link
Copy Markdown
Contributor

diqiu50 commented Mar 30, 2026

Can you add some docs for the UDF

@laserninja laserninja force-pushed the trino-connector-udf-adaptation branch from f9d5505 to 5d6d107 Compare March 31, 2026 03:32
@laserninja
Copy link
Copy Markdown
Collaborator Author

Done, created 'udf-support.md'

Copy link
Copy Markdown
Contributor

@diqiu50 diqiu50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@diqiu50
Copy link
Copy Markdown
Contributor

diqiu50 commented Mar 31, 2026

@laserninja Please leave a comment on the issue #9533—I can’t assign it to you.

@laserninja
Copy link
Copy Markdown
Collaborator Author

Commented, thanks.

@diqiu50 diqiu50 merged commit 9809eed into apache:main Apr 1, 2026
21 checks passed
babumahesh pushed a commit to babumahesh/gravitino that referenced this pull request Apr 9, 2026
…nector (apache#10494)

### What changes were proposed in this pull request?
Add support for consuming Gravitino function metadata in the Trino
connector by mapping SQL functions with RuntimeType.TRINO to Trino's
built-in LanguageFunction API.

### Why are the changes needed?
- CatalogConnectorMetadata: Add FunctionCatalog support with graceful
fallback when catalog does not support functions. Add methods to list
and retrieve functions from Gravitino server.
- GravitinoMetadata: Implement listLanguageFunctions() and
getLanguageFunctions() to convert Gravitino SQL functions with TRINO
runtime to Trino LanguageFunction instances.
- TestGravitinoMetadataFunction: 8 unit tests covering listing,
filtering, multiple definitions, signature tokens, and edge cases.
- TestCatalogConnectorMetadataFunction: 7 unit tests covering function
catalog support detection, listing, retrieval, and error handling.

Fix apache#9533

### Does this PR introduce any user-facing change?
No.

### How was this patch tested?

Added test cases.
babumahesh added a commit to babumahesh/gravitino that referenced this pull request Apr 9, 2026
babumahesh added a commit to babumahesh/gravitino that referenced this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Subtask] Trino connector: UDF adaptation

3 participants