From 233bc93fa0f4e5b8270bd941766ea2dbc2978c19 Mon Sep 17 00:00:00 2001 From: octo-patch Date: Thu, 23 Apr 2026 11:33:01 +0800 Subject: [PATCH] fix: use schemaName as fallback for databaseName in Hive DDL/columns/view queries (fixes #1745) In Hive, schema and database represent the same concept. When a client passes schemaName without databaseName (e.g. from the DDL export endpoint), the SQL was generated as `SHOW CREATE TABLE null.table_name`, causing a SemanticException. Apply the same schemaName fallback in tableDDL(), columns(), and view() so all three operations work correctly regardless of which field the caller populates. --- .../java/ai/chat2db/plugin/hive/HiveMetaData.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/HiveMetaData.java b/chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/HiveMetaData.java index 0579d4876..682d27ca6 100644 --- a/chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/HiveMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/HiveMetaData.java @@ -48,7 +48,9 @@ public List schemas(Connection connection, String databaseName) { @Override public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName, @NotEmpty String tableName) { - String sql = "SHOW CREATE TABLE " + format(databaseName) + "." + // In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent. + String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName; + String sql = "SHOW CREATE TABLE " + format(effectiveDb) + "." + format(tableName); return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { StringBuilder sb = new StringBuilder(); @@ -97,7 +99,9 @@ public SqlBuilder getSqlBuilder() { // TODO 待完善 @Override public List columns(Connection connection, String databaseName, String schemaName, String tableName) { - String sql = String.format(SELECT_TAB_COLS, databaseName, tableName); + // In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent. + String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName; + String sql = String.format(SELECT_TAB_COLS, effectiveDb, tableName); return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { List tableColumns = new ArrayList<>(); Map detailTableInfo = new HashMap<>(); @@ -268,7 +272,9 @@ public static String format(String name) { @Override public Table view(Connection connection, String databaseName, String schemaName, String viewName) { - String sql = String.format(VIEW_SQL, databaseName, viewName); + // In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent. + String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName; + String sql = String.format(VIEW_SQL, effectiveDb, viewName); return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { Table table = new Table(); table.setDatabaseName(databaseName);