Skip to content

Commit 233bc93

Browse files
author
octo-patch
committed
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.
1 parent 4068ed2 commit 233bc93

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

  • chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive

chat2db-server/chat2db-plugins/chat2db-hive/src/main/java/ai/chat2db/plugin/hive/HiveMetaData.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public List<Schema> schemas(Connection connection, String databaseName) {
4848
@Override
4949
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
5050
@NotEmpty String tableName) {
51-
String sql = "SHOW CREATE TABLE " + format(databaseName) + "."
51+
// In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent.
52+
String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName;
53+
String sql = "SHOW CREATE TABLE " + format(effectiveDb) + "."
5254
+ format(tableName);
5355
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
5456
StringBuilder sb = new StringBuilder();
@@ -97,7 +99,9 @@ public SqlBuilder getSqlBuilder() {
9799
// TODO 待完善
98100
@Override
99101
public List<TableColumn> columns(Connection connection, String databaseName, String schemaName, String tableName) {
100-
String sql = String.format(SELECT_TAB_COLS, databaseName, tableName);
102+
// In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent.
103+
String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName;
104+
String sql = String.format(SELECT_TAB_COLS, effectiveDb, tableName);
101105
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
102106
List<TableColumn> tableColumns = new ArrayList<>();
103107
Map<String, String> detailTableInfo = new HashMap<>();
@@ -268,7 +272,9 @@ public static String format(String name) {
268272

269273
@Override
270274
public Table view(Connection connection, String databaseName, String schemaName, String viewName) {
271-
String sql = String.format(VIEW_SQL, databaseName, viewName);
275+
// In Hive, schema and database are the same concept. Fall back to schemaName when databaseName is absent.
276+
String effectiveDb = StringUtils.isNotBlank(databaseName) ? databaseName : schemaName;
277+
String sql = String.format(VIEW_SQL, effectiveDb, viewName);
272278
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
273279
Table table = new Table();
274280
table.setDatabaseName(databaseName);

0 commit comments

Comments
 (0)