diff --git a/agents/drivers/hive-go/config.go b/agents/drivers/hive-go/config.go index 6940c70f70..debc0d2e08 100644 --- a/agents/drivers/hive-go/config.go +++ b/agents/drivers/hive-go/config.go @@ -22,6 +22,9 @@ import ( const ( defaultHivePort = 10000 defaultHiveDatabase = "default" + defaultHiveIdentifierQuote = "`" + trinoIdentifierQuote = "\"" + kyuubiEngineTypeSetting = "kyuubi.engine.type" defaultHiveHTTPPath = "cliservice" defaultHiveService = "hive" defaultImpalaService = "impala" @@ -735,6 +738,31 @@ func hiveAssignmentValue(values map[string]string, key string) string { return "" } +func (config connectionConfig) identifierQuote() string { + if !strings.EqualFold(config.DatabaseType, "kyuubi") { + return defaultHiveIdentifierQuote + } + engineType := openSessionConfigurationValue(config.HiveConfiguration, kyuubiEngineTypeSetting) + if strings.EqualFold(strings.TrimSpace(engineType), "TRINO") { + return trinoIdentifierQuote + } + return defaultHiveIdentifierQuote +} + +func openSessionConfigurationValue(values map[string]string, key string) string { + // Hive JDBC-style URL fragments are sent as hive variables, while values + // after '?' are sent as hive confs. Kyuubi strips either prefix before it + // resolves session-scoped settings such as kyuubi.engine.type. + for _, wanted := range []string{"set:hivevar:" + key, "set:hiveconf:" + key, key} { + for candidate, value := range values { + if strings.EqualFold(strings.TrimSpace(candidate), wanted) { + return value + } + } + } + return "" +} + func applyDelegationToken(config *connectionConfig, values map[string]string) error { if !strings.EqualFold(config.Auth, "DELEGATIONTOKEN") && !strings.EqualFold(config.Auth, "DELEGATION_TOKEN") { return nil diff --git a/agents/drivers/hive-go/config_test.go b/agents/drivers/hive-go/config_test.go index 2575ff8ffa..868600ffc5 100644 --- a/agents/drivers/hive-go/config_test.go +++ b/agents/drivers/hive-go/config_test.go @@ -406,6 +406,70 @@ func TestParseStandardJDBCURLSectionsAndCredentials(t *testing.T) { } } +func TestKyuubiIdentifierQuoteFollowsPreservedEngineType(t *testing.T) { + tests := []struct { + name string + params connectParams + configuration string + identifierQuote string + }{ + { + name: "URL fragment selects Trino", + params: connectParams{ + Host: "kyuubi.example.com", + DatabaseType: "kyuubi", + URLParams: "#kyuubi.engine.type=TRINO", + }, + configuration: "set:hivevar:kyuubi.engine.type", + identifierQuote: trinoIdentifierQuote, + }, + { + name: "JDBC hive conf selects Trino", + params: connectParams{ + DatabaseType: "kyuubi", + ConnectionString: "jdbc:hive2://kyuubi.example.com:10009/default?kyuubi.engine.type=trino", + }, + configuration: "set:hiveconf:kyuubi.engine.type", + identifierQuote: trinoIdentifierQuote, + }, + { + name: "Kyuubi Spark keeps Hive quote", + params: connectParams{ + Host: "kyuubi.example.com", + DatabaseType: "kyuubi", + URLParams: "#kyuubi.engine.type=SPARK_SQL", + }, + configuration: "set:hivevar:kyuubi.engine.type", + identifierQuote: defaultHiveIdentifierQuote, + }, + { + name: "Hive ignores Kyuubi engine setting", + params: connectParams{ + Host: "hive.example.com", + DatabaseType: "hive", + URLParams: "#kyuubi.engine.type=TRINO", + }, + configuration: "set:hivevar:kyuubi.engine.type", + identifierQuote: defaultHiveIdentifierQuote, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + config, err := parseConnectionConfig(test.params) + if err != nil { + t.Fatal(err) + } + if got := config.HiveConfiguration[test.configuration]; got == "" { + t.Fatalf("engine type was not preserved in %q: %#v", test.configuration, config.HiveConfiguration) + } + if got := config.identifierQuote(); got != test.identifierQuote { + t.Fatalf("identifier quote = %q, want %q", got, test.identifierQuote) + } + }) + } +} + func TestOpenSessionCompatibilityVariablesFromSessionParams(t *testing.T) { config, err := parseConnectionConfig(connectParams{ Host: "hs2.example.com", diff --git a/agents/drivers/hive-go/metadata.go b/agents/drivers/hive-go/metadata.go index 6956d18be5..7827a313ff 100644 --- a/agents/drivers/hive-go/metadata.go +++ b/agents/drivers/hive-go/metadata.go @@ -223,7 +223,7 @@ func (server *server) connectionInfo() (map[string]any, error) { "username": username, "version": version, "sqlDialect": "HIVE", - "identifierQuote": "`", + "identifierQuote": server.config.identifierQuote(), "compatibilityMode": compatibilityMode, "databaseInfo": map[string]string{ "productName": productName, @@ -334,14 +334,14 @@ func (server *server) listTables(schema string, constraints metadataListConstrai if containsString(requestedTypes, "TABLE") { fallbackQueries = append(fallbackQueries, fallbackQuery{ operation: "SHOW TABLES", - statement: "SHOW TABLES IN " + quoteHiveIdentifier(schema), + statement: "SHOW TABLES IN " + server.quoteIdentifier(schema), objectType: "TABLE", }) } if containsString(requestedTypes, "VIEW") || containsString(requestedTypes, "MATERIALIZED VIEW") { fallbackQueries = append(fallbackQueries, fallbackQuery{ operation: "SHOW VIEWS", - statement: "SHOW VIEWS IN " + quoteHiveIdentifier(schema), + statement: "SHOW VIEWS IN " + server.quoteIdentifier(schema), objectType: "VIEW", }) } @@ -634,7 +634,7 @@ func (server *server) getColumns(schema, table string) ([]columnInfo, error) { } return values, nil } - qualified := qualifiedHiveName(schema, table) + qualified := server.qualifiedName(schema, table) result, err := server.executeQuery(queryOptions{SQL: "DESCRIBE " + qualified, MaxRows: metadataQueryLimit}) if err != nil { return nil, fmt.Errorf("HiveServer2 metadata failed (%v); DESCRIBE fallback failed: %w", metadataErr, err) @@ -716,7 +716,7 @@ func (server *server) getTableDDL(schema, table string) (string, error) { return "", errors.New("table is required") } result, err := server.executeQuery(queryOptions{ - SQL: "SHOW CREATE TABLE " + qualifiedHiveName(schema, table), + SQL: "SHOW CREATE TABLE " + server.qualifiedName(schema, table), MaxRows: metadataQueryLimit, }) if err != nil { @@ -871,11 +871,11 @@ func metadataListConstraintsFromParams(params map[string]json.RawMessage) metada } } -func qualifiedHiveName(schema, table string) string { +func (server *server) qualifiedName(schema, table string) string { if strings.TrimSpace(schema) == "" { - return quoteHiveIdentifier(table) + return server.quoteIdentifier(table) } - return quoteHiveIdentifier(schema) + "." + quoteHiveIdentifier(table) + return server.quoteIdentifier(schema) + "." + server.quoteIdentifier(table) } func metadataNameMatches(name, filter string) bool { diff --git a/agents/drivers/hive-go/metadata_test.go b/agents/drivers/hive-go/metadata_test.go index c8d7f569fc..c7f5450286 100644 --- a/agents/drivers/hive-go/metadata_test.go +++ b/agents/drivers/hive-go/metadata_test.go @@ -36,6 +36,7 @@ func TestKyuubiConnectionInfoReportsNativeIdentity(t *testing.T) { } server := newScriptedServer(t, behavior) server.params.DatabaseType = "kyuubi" + server.config.DatabaseType = "kyuubi" server.config.Username = "fallback" info, err := server.connectionInfo() @@ -49,6 +50,9 @@ func TestKyuubiConnectionInfoReportsNativeIdentity(t *testing.T) { if !ok || databaseInfo["productName"] != "Apache Kyuubi" || databaseInfo["driverName"] != "DBX Kyuubi Go Agent" { t.Fatalf("unexpected Kyuubi database identity: %#v", info["databaseInfo"]) } + if info["identifierQuote"] != defaultHiveIdentifierQuote { + t.Fatalf("non-Trino Kyuubi identifier quote changed: %#v", info["identifierQuote"]) + } } func TestGetObjectSourceReturnsProtocolObject(t *testing.T) { @@ -92,6 +96,66 @@ func TestGetObjectSourceReturnsProtocolObject(t *testing.T) { } } +func TestKyuubiTrinoUsesDoubleQuotesForMetadataFallbacksAndDDL(t *testing.T) { + behavior := &scriptedBehavior{ + getTables: func(context.Context, string, string, []string) (gohive.MetadataResult, error) { + return gohive.MetadataResult{}, errors.New("metadata unsupported") + }, + getColumns: func(context.Context, string, string, string) (gohive.MetadataResult, error) { + return gohive.MetadataResult{}, errors.New("metadata unsupported") + }, + query: func(ctx context.Context, query string) (driver.Rows, error) { + switch query { + case "SELECT VERSION()": + return newScriptedRows(ctx, []string{"version"}, []string{"STRING"}, [][]driver.Value{{"1.10.0"}}), nil + case "SELECT CURRENT_USER()": + return newScriptedRows(ctx, []string{"current_user"}, []string{"STRING"}, [][]driver.Value{{"dbx"}}), nil + case `SHOW TABLES IN "analytics"`: + return newScriptedRows(ctx, []string{"table_name"}, []string{"STRING"}, [][]driver.Value{{`event"log`}}), nil + case `DESCRIBE "analytics"."event""log"`: + return newScriptedRows(ctx, []string{"col_name", "data_type", "comment"}, []string{"STRING", "STRING", "STRING"}, [][]driver.Value{{"id", "bigint", ""}}), nil + case `SHOW CREATE TABLE "analytics"."event""log"`: + return newScriptedRows(ctx, []string{"create_table"}, []string{"STRING"}, [][]driver.Value{{`CREATE TABLE "analytics"."event""log" ("id" bigint)`}}), nil + default: + return nil, errors.New("unexpected query: " + query) + } + }, + } + server := newScriptedServer(t, behavior) + server.params.DatabaseType = "kyuubi" + server.config.DatabaseType = "kyuubi" + server.config.HiveConfiguration = map[string]string{"set:hivevar:kyuubi.engine.type": "TRINO"} + + info, err := server.connectionInfo() + if err != nil { + t.Fatal(err) + } + if info["identifierQuote"] != trinoIdentifierQuote { + t.Fatalf("unexpected Trino identifier quote: %#v", info["identifierQuote"]) + } + tables, err := server.listTables("analytics", metadataListConstraints{ObjectTypes: []string{"TABLE"}}) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(tables, []tableInfo{{Name: `event"log`, TableType: "TABLE"}}) { + t.Fatalf("unexpected fallback tables: %#v", tables) + } + columns, err := server.getColumns("analytics", `event"log`) + if err != nil { + t.Fatal(err) + } + if len(columns) != 1 || columns[0].Name != "id" { + t.Fatalf("unexpected fallback columns: %#v", columns) + } + ddl, err := server.getTableDDL("analytics", `event"log`) + if err != nil { + t.Fatal(err) + } + if ddl != "CREATE TABLE \"analytics\".\"event\"\"log\" (\"id\" bigint)\n" { + t.Fatalf("unexpected Trino DDL: %q", ddl) + } +} + func TestListDatabasesUsesShowDatabasesBeforeHiveServerMetadata(t *testing.T) { behavior := &scriptedBehavior{ query: func(ctx context.Context, sql string) (driver.Rows, error) { diff --git a/agents/drivers/hive-go/query.go b/agents/drivers/hive-go/query.go index 088a803976..c999889283 100644 --- a/agents/drivers/hive-go/query.go +++ b/agents/drivers/hive-go/query.go @@ -386,7 +386,7 @@ func (server *server) applySchemaContext(ctx context.Context, connection *sql.Co if schema == "" || strings.EqualFold(schema, server.config.Database) { return nil } - _, err := connection.ExecContext(ctx, "USE "+quoteHiveIdentifier(schema)) + _, err := connection.ExecContext(ctx, "USE "+server.quoteIdentifier(schema)) return err } @@ -527,6 +527,7 @@ func trimStatementSQL(sqlText string) string { return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(sqlText), ";")) } -func quoteHiveIdentifier(value string) string { - return "`" + strings.ReplaceAll(value, "`", "``") + "`" +func (server *server) quoteIdentifier(value string) string { + quote := server.config.identifierQuote() + return quote + strings.ReplaceAll(value, quote, quote+quote) + quote } diff --git a/agents/drivers/hive-go/query_test.go b/agents/drivers/hive-go/query_test.go index 2af52a96bf..705e148a88 100644 --- a/agents/drivers/hive-go/query_test.go +++ b/agents/drivers/hive-go/query_test.go @@ -265,6 +265,47 @@ func rawParams(values map[string]any) map[string]json.RawMessage { return result } +func TestApplySchemaContextUsesEngineIdentifierQuote(t *testing.T) { + tests := []struct { + name string + databaseType string + configuration map[string]string + expected string + }{ + { + name: "Kyuubi Trino", + databaseType: "kyuubi", + configuration: map[string]string{"set:hivevar:kyuubi.engine.type": "TRINO"}, + expected: `USE "sales""daily"`, + }, + { + name: "Hive", + databaseType: "hive", + configuration: map[string]string{"set:hivevar:kyuubi.engine.type": "TRINO"}, + expected: "USE `sales\"daily`", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + behavior := &scriptedBehavior{exec: func(context.Context, string) (driver.Result, error) { + return driver.RowsAffected(0), nil + }} + server := newScriptedServer(t, behavior) + server.config.DatabaseType = test.databaseType + server.config.HiveConfiguration = test.configuration + + if err := server.applySchemaContext(context.Background(), server.connection, `sales"daily`); err != nil { + t.Fatal(err) + } + _, executions, _, _ := behavior.snapshot() + if !reflect.DeepEqual(executions, []string{test.expected}) { + t.Fatalf("unexpected schema switch SQL: %#v", executions) + } + }) + } +} + func TestExecuteQueryUsesHiveServerResultSetSignal(t *testing.T) { behavior := &scriptedBehavior{} behavior.query = func(ctx context.Context, query string) (driver.Rows, error) { diff --git a/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts b/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts index 1358caa2d8..9a91b07c3f 100644 --- a/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts +++ b/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts @@ -157,6 +157,7 @@ describe("jdbc dialect inference", () => { expect(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.opengauss.Driver" })).toBe(true); expect(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.postgresql.Driver" })).toBe(true); expect(connectionShouldLoadIdentifierQuote({ db_type: "kingbase" })).toBe(true); + expect(connectionShouldLoadIdentifierQuote({ db_type: "kyuubi" })).toBe(true); expect(connectionShouldLoadIdentifierQuote({ db_type: "gaussdb" })).toBe(true); expect(connectionShouldLoadIdentifierQuote({ db_type: "gbase", driver_profile: "gbase8s" })).toBe(true); expect(connectionShouldLoadIdentifierQuote({ db_type: "gbase", driver_profile: "gbase8a" })).toBe(false); diff --git a/apps/desktop/src/lib/database/jdbcDialect.ts b/apps/desktop/src/lib/database/jdbcDialect.ts index a8d890903d..86689fc39c 100644 --- a/apps/desktop/src/lib/database/jdbcDialect.ts +++ b/apps/desktop/src/lib/database/jdbcDialect.ts @@ -133,6 +133,7 @@ export function connectionShouldLoadIdentifierQuote(connection: JdbcDialectConne if (!connection) return false; if (connection.db_type === "gbase" && isGbase8sProfile(connection.driver_profile)) return true; if (connection.db_type === "kingbase") return true; + if (connection.db_type === "kyuubi") return true; // Cloud Spanner is dual-dialect: the agent reports a backtick for GoogleSQL and // a double quote for PostgreSQL-dialect databases. The backend counts Spanner // unconditionally in `uses_connection_identifier_quote`, so the UI must fetch diff --git a/crates/dbx-sql-dialect/src/sql_dialect/table_select.rs b/crates/dbx-sql-dialect/src/sql_dialect/table_select.rs index 21d10a1593..aecc852437 100644 --- a/crates/dbx-sql-dialect/src/sql_dialect/table_select.rs +++ b/crates/dbx-sql-dialect/src/sql_dialect/table_select.rs @@ -302,6 +302,7 @@ pub fn build_table_data_select_sql_with_database( database_type, &options.columns, tdengine_should_include_tbname(database_type, options.table_type.as_deref()), + options.identifier_quote.as_deref(), ) }; let rownum_select_columns = quoted_table_columns_or_star(database_type, &options.columns); @@ -556,6 +557,9 @@ pub fn uses_connection_identifier_quote(database_type: Option, ide // Kingbase — when no quote was reported the callers fall back to // `quote_table_identifier`, whose static mapping is GoogleSQL-correct. || database_type == Some(DatabaseType::Spanner) + // Kyuubi normally uses Hive-family backticks, but a Trino-backed + // session reports the ANSI double quote through connection info. + || (database_type == Some(DatabaseType::Kyuubi) && identifier_quote.is_some()) || (database_type == Some(DatabaseType::Informix) && identifier_quote.is_some()) || (matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres)) && identifier_quote.is_some()) @@ -734,6 +738,7 @@ pub(super) fn build_select_columns( database_type: Option, columns: &[String], include_tdengine_tbname: bool, + identifier_quote: Option<&str>, ) -> String { if columns.is_empty() { if database_type == Some(DatabaseType::Tdengine) && include_tdengine_tbname { @@ -784,7 +789,11 @@ pub(super) fn build_select_columns( columns .iter() .map(|column| { - let ident = quote_table_identifier(database_type, column); + let ident = if database_type == Some(DatabaseType::Kyuubi) { + quote_table_data_identifier(database_type, column, identifier_quote) + } else { + quote_table_identifier(database_type, column) + }; if database_type == Some(DatabaseType::Hive) { format!("{ident} AS {ident}") } else { diff --git a/crates/dbx-sql-dialect/src/sql_dialect/tests.rs b/crates/dbx-sql-dialect/src/sql_dialect/tests.rs index 5633382341..1f6493d318 100644 --- a/crates/dbx-sql-dialect/src/sql_dialect/tests.rs +++ b/crates/dbx-sql-dialect/src/sql_dialect/tests.rs @@ -79,6 +79,42 @@ fn quotes_spanner_identifiers_by_connection_dialect() { ); } +#[test] +fn kyuubi_table_data_uses_connection_identifier_quote() { + let columns = vec!["id".to_string(), "order\"value".to_string()]; + let base = TableDataSelectSqlOptions { + database_type: Some(DatabaseType::Kyuubi), + schema: Some("sales\"daily".to_string()), + table_name: "event\"log".to_string(), + columns, + limit: Some(25), + ..Default::default() + }; + + assert_eq!( + build_table_data_select_sql(TableDataSelectSqlOptions { + identifier_quote: Some("\"".to_string()), + ..base.clone() + }), + "SELECT \"id\", \"order\"\"value\" FROM \"sales\"\"daily\".\"event\"\"log\" LIMIT 25;" + ); + assert_eq!( + build_table_data_select_sql(TableDataSelectSqlOptions { + identifier_quote: Some("`".to_string()), + ..base.clone() + }), + "SELECT `id`, `order\"value` FROM `sales\"daily`.`event\"log` LIMIT 25;" + ); + assert_eq!( + build_table_data_select_sql(TableDataSelectSqlOptions { + database_type: Some(DatabaseType::Hive), + identifier_quote: Some("\"".to_string()), + ..base + }), + "SELECT `id` AS `id`, `order\"value` AS `order\"value` FROM `sales\"daily`.`event\"log` LIMIT 25;" + ); +} + #[test] fn quotes_gaussdb_jdbc_identifiers_selectively() { for (name, expected) in [ diff --git a/packages/app-tests/jdbcDialect.test.ts b/packages/app-tests/jdbcDialect.test.ts index 6b114ee62e..dfd6b220e3 100644 --- a/packages/app-tests/jdbcDialect.test.ts +++ b/packages/app-tests/jdbcDialect.test.ts @@ -85,6 +85,7 @@ test("loads driver-reported identifier quotes for compatible JDBC connections", assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.opengauss.Driver" }), true); assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.postgresql.Driver" }), true); assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "kingbase" }), true); + assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "kyuubi" }), true); assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "gaussdb" }), true); });