Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(trino): keep the WITH clause in table DDL

HARRISON63416a3cdea9
diff --git a/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java b/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
index 8f6dbeee5d..c93d52ef42 100644
--- a/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
+++ b/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
@@ -4,6 +4,7 @@ import com.dbx.agent.AbstractJdbcAgent;
import com.dbx.agent.ColumnInfo;
import com.dbx.agent.ConnectParams;
import com.dbx.agent.DatabaseInfo;
+import com.dbx.agent.DdlBuilder;
import com.dbx.agent.ForeignKeyInfo;
import com.dbx.agent.IndexInfo;
import com.dbx.agent.JdbcIdentifiers;
@@ -158,6 +159,53 @@ public final class TrinoAgent extends AbstractJdbcAgent {
return Collections.emptyList();
}
+ /**
+ * Trino's {@code SHOW CREATE TABLE} returns the authoritative DDL, including
+ * the {@code WITH (...)} clause that carries a table's connector properties
+ * (format, bucket_count, partitioned_by, ...). The metadata-based fallback
+ * only knows columns, so it silently dropped those properties and the DDL no
+ * longer round-tripped (#11977). A server or connector that rejects the
+ * statement, or returns nothing, keeps the previous metadata-based DDL.
+ */
+ @Override
+ public String getTableDdl(String schema, String table) {
+ try {
+ String tableReference = tableReference(schema, table);
+ try (java.sql.Statement stmt = requireConnected().createStatement();
+ ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + tableReference)) {
+ if (rs.next()) {
+ String ddl = trimToNull(rs.getString(1));
+ if (ddl != null) {
+ return ddl;
+ }
+ }
+ }
+ } catch (Exception ignored) {
+ // A connector may not implement SHOW CREATE TABLE; the metadata-based
+ // DDL below is a valid (if less complete) fallback.
+ }
+ return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), Collections.emptyList(), Collections.emptyList());
+ }
+
+ /**
+ * Qualifies {@code schema.table} for {@code SHOW CREATE TABLE}. Trino resolves
+ * an unqualified schema against the session catalog, so the catalog is only
+ * included when it is known.
+ */
+ private String tableReference(String schema, String table) {
+ String catalog;
+ try {
+ catalog = blankToNull(requireConnected().getCatalog());
+ } catch (java.sql.SQLException error) {
+ catalog = null;
+ }
+ StringBuilder reference = new StringBuilder();
+ if (catalog != null) {
+ reference.append(quoteIdentifier(catalog)).append('.');
+ }
+ return reference.append(quoteIdentifier(schema)).append('.').append(quoteIdentifier(table)).toString();
+ }
+
@Override
public String setSchemaSQL(String schema) {
return "USE " + JdbcIdentifiers.INSTANCE.doubleQuote(schema);
@@ -262,6 +310,14 @@ public final class TrinoAgent extends AbstractJdbcAgent {
return "\"" + identifier.replace("\"", "\"\"") + "\"";
}
+ private static String trimToNull(String value) {
+ if (value == null) {
+ return null;
+ }
+ String trimmed = value.trim();
+ return trimmed.isEmpty() ? null : trimmed;
+ }
+
private static boolean isNumericType(int sqlType) {
switch (sqlType) {
case Types.BIGINT:
diff --git a/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentDdlTest.java b/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentDdlTest.java
new file mode 100644
index 0000000000..3cb4d900cb
--- /dev/null
+++ b/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentDdlTest.java
@@ -0,0 +1,190 @@
+package com.dbx.agent.trino;
+
+import com.dbx.agent.test.TestSupport;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * #11977: Trino DDL must come from {@code SHOW CREATE TABLE} so the
+ * {@code WITH (...)} connector-property clause survives. Building the DDL from
+ * column metadata alone produced a statement that no longer round-tripped.
+ */
+class TrinoAgentDdlTest {
+ private static final String TABLE_WITH_PROPERTIES = "CREATE TABLE hive.native_map.fact_pid_limited (\n" +
+ " pid varchar,\n" +
+ " record_id bigint,\n" +
+ " amount varchar,\n" +
+ " payload varchar\n" +
+ ")\n" +
+ "WITH (\n" +
+ " bucket_count = 32,\n" +
+ " bucketed_by = ARRAY['pid'],\n" +
+ " bucketing_version = 2,\n" +
+ " format = 'ORC',\n" +
+ " sorted_by = ARRAY[]\n" +
+ ")";
+
+ @Test
+ void getTableDdlKeepsTheWithClauseFromShowCreateTable() {
+ List<String> sql = new ArrayList<>();
+ TrinoAgent agent = new TrinoAgent();
+ TestSupport.setPrivateConnection(agent, connection(sql, TABLE_WITH_PROPERTIES, null, false));
+
+ String ddl = agent.getTableDdl("native_map", "fact_pid_limited");
+
+ Assertions.assertEquals(TABLE_WITH_PROPERTIES, ddl);
+ Assertions.assertTrue(ddl.contains("WITH ("), ddl);
+ Assertions.assertTrue(ddl.contains("bucket_count = 32"), ddl);
+ Assertions.assertEquals(List.of("SHOW CREATE TABLE \"native_map\".\"fact_pid_limited\""), sql);
+ }
+
+ @Test
+ void getTableDdlQualifiesTheSessionCatalogWhenKnown() {
+ List<String> sql = new ArrayList<>();
+ TrinoAgent agent = new TrinoAgent();
+ TestSupport.setPrivateConnection(agent, connection(sql, "CREATE TABLE native_ddl", "hive", false));
+
+ String ddl = agent.getTableDdl("native_map", "orders");
+
+ Assertions.assertEquals("CREATE TABLE native_ddl", ddl);
+ Assertions.assertEquals(List.of("SHOW CREATE TABLE \"hive\".\"native_map\".\"orders\""), sql);
+ }
+
+ @Test
+ void getTableDdlEscapesEmbeddedQuotesInIdentifiers() {
+ List<String> sql = new ArrayList<>();
+ TrinoAgent agent = new TrinoAgent();
+ TestSupport.setPrivateConnection(agent, connection(sql, "CREATE TABLE native_ddl", null, false));
+
+ agent.getTableDdl("we\"ird", "or\"ders");
+
+ Assertions.assertEquals(List.of("SHOW CREATE TABLE \"we\"\"ird\".\"or\"\"ders\""), sql);
+ }
+
+ @Test
+ void getTableDdlFallsBackToColumnMetadataWhenShowCreateTableFails() {
+ List<String> sql = new ArrayList<>();
+ TrinoAgent agent = new TrinoAgent();
+ TestSupport.setPrivateConnection(agent, connection(sql, null, null, true));
+
+ String ddl = agent.getTableDdl("native_map", "fact_pid_limited");
+
+ // The fallback still produces usable DDL, just without connector properties.
+ Assertions.assertTrue(ddl.startsWith("CREATE TABLE"), ddl);
+ Assertions.assertFalse(ddl.contains("WITH ("), ddl);
+ Assertions.assertEquals("SHOW CREATE TABLE \"native_map\".\"fact_pid_limited\"", sql.get(0));
+ }
+
+ private static Connection connection(List<String> sql, String nativeDdl, String catalog, boolean failShowCreate) {
+ Statement statement = proxy(Statement.class, (method, args) -> {
+ if ("execute".equals(method.getName())) {
+ sql.add(String.valueOf(args[0]));
+ return false;
+ }
+ if ("executeQuery".equals(method.getName())) {
+ String query = String.valueOf(args[0]);
+ sql.add(query);
+ if (query.startsWith("SHOW CREATE TABLE")) {
+ if (failShowCreate) {
+ throw new SQLException("SHOW CREATE TABLE is unavailable");
+ }
+ return resultSet(new String[]{"Create Table"}, new Object[][]{{nativeDdl}});
+ }
+ if (query.startsWith("DESCRIBE")) {
+ return resultSet(
+ new String[]{"Column", "Type", "Extra", "Comment"},
+ new Object[][]{
+ {"pid", "varchar", "", null},
+ {"record_id", "bigint", "", null}
+ }
+ );
+ }
+ }
+ if ("close".equals(method.getName())) {
+ return null;
+ }
+ return defaultValue(method.getReturnType());
+ });
+ return proxy(Connection.class, (method, args) -> {
+ if ("createStatement".equals(method.getName())) {
+ return statement;
+ }
+ if ("getCatalog".equals(method.getName())) {
+ return catalog;
+ }
+ if ("isClosed".equals(method.getName())) {
+ return false;
+ }
+ return defaultValue(method.getReturnType());
+ });
+ }
+
+ private static ResultSet resultSet(String[] columns, Object[][] rows) {
+ int[] index = {-1};
+ ResultSetMetaData metadata = proxy(ResultSetMetaData.class, (method, args) -> {
+ if ("getColumnCount".equals(method.getName())) {
+ return columns.length;
+ }
+ return defaultValue(method.getReturnType());
+ });
+ return proxy(ResultSet.class, (method, args) -> {
+ if ("next".equals(method.getName())) {
+ index[0] += 1;
+ return index[0] < rows.length;
+ }
+ if ("getString".equals(method.getName())) {
+ Object key = args[0];
+ int column = key instanceof Number ? ((Number) key).intValue() - 1 : findColumn(columns, String.valueOf(key));
+ Object value = rows[index[0]][column];
+ return value == null ? null : String.valueOf(value);
+ }
+ if ("getMetaData".equals(method.getName())) {
+ return metadata;
+ }
+ if ("close".equals(method.getName())) {
+ return null;
+ }
+ return defaultValue(method.getReturnType());
+ });
+ }
+
+ private static int findColumn(String[] columns, String name) {
+ for (int i = 0; i < columns.length; i++) {
+ if (columns[i].equalsIgnoreCase(name)) {
+ return i;
+ }
+ }
+ throw new IllegalArgumentException("Unknown column: " + name);
+ }
+
+ private static <T> T proxy(Class<T> type, MethodHandler handler) {
+ InvocationHandler invocationHandler = (proxy, method, args) -> handler.handle(method, args == null ? new Object[0] : args);
+ return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, invocationHandler));
+ }
+
+ private static Object defaultValue(Class<?> type) {
+ if (type == Boolean.TYPE) return false;
+ if (type == Byte.TYPE) return (byte) 0;
+ if (type == Short.TYPE) return (short) 0;
+ if (type == Integer.TYPE) return 0;
+ if (type == Long.TYPE) return 0L;
+ if (type == Float.TYPE) return 0f;
+ if (type == Double.TYPE) return 0d;
+ if (type == Character.TYPE) return (char) 0;
+ return null;
+ }
+
+ private interface MethodHandler {
+ Object handle(Method method, Object[] args) throws Throwable;
+ }
+}