fix(kyuubi): honor Trino identifier quotes

Closes #8684
This commit is contained in:
t8y2 committed 2026-09-27 08:14:01 +00:00
1 parent 7e2b74fb9d
commit bee32d46b5
11 files changed
+258 -12

No files matched your search

+28
View File
@@ -22,6 +22,9 @@ import (
const ( const (
defaultHivePort = 10000 defaultHivePort = 10000
defaultHiveDatabase = "default" defaultHiveDatabase = "default"
defaultHiveIdentifierQuote = "`"
trinoIdentifierQuote = "\""
kyuubiEngineTypeSetting = "kyuubi.engine.type"
defaultHiveHTTPPath = "cliservice" defaultHiveHTTPPath = "cliservice"
defaultHiveService = "hive" defaultHiveService = "hive"
defaultImpalaService = "impala" defaultImpalaService = "impala"
@@ -735,6 +738,31 @@ func hiveAssignmentValue(values map[string]string, key string) string {
return "" 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 { func applyDelegationToken(config *connectionConfig, values map[string]string) error {
if !strings.EqualFold(config.Auth, "DELEGATIONTOKEN") && !strings.EqualFold(config.Auth, "DELEGATION_TOKEN") { if !strings.EqualFold(config.Auth, "DELEGATIONTOKEN") && !strings.EqualFold(config.Auth, "DELEGATION_TOKEN") {
return nil return nil
+64
View File
@@ -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) { func TestOpenSessionCompatibilityVariablesFromSessionParams(t *testing.T) {
config, err := parseConnectionConfig(connectParams{ config, err := parseConnectionConfig(connectParams{
Host: "hs2.example.com", Host: "hs2.example.com",
+8 -8
View File
@@ -223,7 +223,7 @@ func (server *server) connectionInfo() (map[string]any, error) {
"username": username, "username": username,
"version": version, "version": version,
"sqlDialect": "HIVE", "sqlDialect": "HIVE",
"identifierQuote": "`", "identifierQuote": server.config.identifierQuote(),
"compatibilityMode": compatibilityMode, "compatibilityMode": compatibilityMode,
"databaseInfo": map[string]string{ "databaseInfo": map[string]string{
"productName": productName, "productName": productName,
@@ -334,14 +334,14 @@ func (server *server) listTables(schema string, constraints metadataListConstrai
if containsString(requestedTypes, "TABLE") { if containsString(requestedTypes, "TABLE") {
fallbackQueries = append(fallbackQueries, fallbackQuery{ fallbackQueries = append(fallbackQueries, fallbackQuery{
operation: "SHOW TABLES", operation: "SHOW TABLES",
statement: "SHOW TABLES IN " + quoteHiveIdentifier(schema), statement: "SHOW TABLES IN " + server.quoteIdentifier(schema),
objectType: "TABLE", objectType: "TABLE",
}) })
} }
if containsString(requestedTypes, "VIEW") || containsString(requestedTypes, "MATERIALIZED VIEW") { if containsString(requestedTypes, "VIEW") || containsString(requestedTypes, "MATERIALIZED VIEW") {
fallbackQueries = append(fallbackQueries, fallbackQuery{ fallbackQueries = append(fallbackQueries, fallbackQuery{
operation: "SHOW VIEWS", operation: "SHOW VIEWS",
statement: "SHOW VIEWS IN " + quoteHiveIdentifier(schema), statement: "SHOW VIEWS IN " + server.quoteIdentifier(schema),
objectType: "VIEW", objectType: "VIEW",
}) })
} }
@@ -634,7 +634,7 @@ func (server *server) getColumns(schema, table string) ([]columnInfo, error) {
} }
return values, nil return values, nil
} }
qualified := qualifiedHiveName(schema, table) qualified := server.qualifiedName(schema, table)
result, err := server.executeQuery(queryOptions{SQL: "DESCRIBE " + qualified, MaxRows: metadataQueryLimit}) result, err := server.executeQuery(queryOptions{SQL: "DESCRIBE " + qualified, MaxRows: metadataQueryLimit})
if err != nil { if err != nil {
return nil, fmt.Errorf("HiveServer2 metadata failed (%v); DESCRIBE fallback failed: %w", metadataErr, err) 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") return "", errors.New("table is required")
} }
result, err := server.executeQuery(queryOptions{ result, err := server.executeQuery(queryOptions{
SQL: "SHOW CREATE TABLE " + qualifiedHiveName(schema, table), SQL: "SHOW CREATE TABLE " + server.qualifiedName(schema, table),
MaxRows: metadataQueryLimit, MaxRows: metadataQueryLimit,
}) })
if err != nil { 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) == "" { 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 { func metadataNameMatches(name, filter string) bool {
+64
View File
@@ -36,6 +36,7 @@ func TestKyuubiConnectionInfoReportsNativeIdentity(t *testing.T) {
} }
server := newScriptedServer(t, behavior) server := newScriptedServer(t, behavior)
server.params.DatabaseType = "kyuubi" server.params.DatabaseType = "kyuubi"
server.config.DatabaseType = "kyuubi"
server.config.Username = "fallback" server.config.Username = "fallback"
info, err := server.connectionInfo() 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" { if !ok || databaseInfo["productName"] != "Apache Kyuubi" || databaseInfo["driverName"] != "DBX Kyuubi Go Agent" {
t.Fatalf("unexpected Kyuubi database identity: %#v", info["databaseInfo"]) 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) { 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) { func TestListDatabasesUsesShowDatabasesBeforeHiveServerMetadata(t *testing.T) {
behavior := &scriptedBehavior{ behavior := &scriptedBehavior{
query: func(ctx context.Context, sql string) (driver.Rows, error) { query: func(ctx context.Context, sql string) (driver.Rows, error) {
+4 -3
View File
@@ -386,7 +386,7 @@ func (server *server) applySchemaContext(ctx context.Context, connection *sql.Co
if schema == "" || strings.EqualFold(schema, server.config.Database) { if schema == "" || strings.EqualFold(schema, server.config.Database) {
return nil return nil
} }
_, err := connection.ExecContext(ctx, "USE "+quoteHiveIdentifier(schema)) _, err := connection.ExecContext(ctx, "USE "+server.quoteIdentifier(schema))
return err return err
} }
@@ -527,6 +527,7 @@ func trimStatementSQL(sqlText string) string {
return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(sqlText), ";")) return strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(sqlText), ";"))
} }
func quoteHiveIdentifier(value string) string { func (server *server) quoteIdentifier(value string) string {
return "`" + strings.ReplaceAll(value, "`", "``") + "`" quote := server.config.identifierQuote()
return quote + strings.ReplaceAll(value, quote, quote+quote) + quote
} }
+41
View File
@@ -265,6 +265,47 @@ func rawParams(values map[string]any) map[string]json.RawMessage {
return result 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) { func TestExecuteQueryUsesHiveServerResultSetSignal(t *testing.T) {
behavior := &scriptedBehavior{} behavior := &scriptedBehavior{}
behavior.query = func(ctx context.Context, query string) (driver.Rows, error) { behavior.query = func(ctx context.Context, query string) (driver.Rows, error) {
@@ -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.opengauss.Driver" })).toBe(true);
expect(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.postgresql.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: "kingbase" })).toBe(true);
expect(connectionShouldLoadIdentifierQuote({ db_type: "kyuubi" })).toBe(true);
expect(connectionShouldLoadIdentifierQuote({ db_type: "gaussdb" })).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: "gbase8s" })).toBe(true);
expect(connectionShouldLoadIdentifierQuote({ db_type: "gbase", driver_profile: "gbase8a" })).toBe(false); expect(connectionShouldLoadIdentifierQuote({ db_type: "gbase", driver_profile: "gbase8a" })).toBe(false);
@@ -133,6 +133,7 @@ export function connectionShouldLoadIdentifierQuote(connection: JdbcDialectConne
if (!connection) return false; if (!connection) return false;
if (connection.db_type === "gbase" && isGbase8sProfile(connection.driver_profile)) return true; if (connection.db_type === "gbase" && isGbase8sProfile(connection.driver_profile)) return true;
if (connection.db_type === "kingbase") 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 // Cloud Spanner is dual-dialect: the agent reports a backtick for GoogleSQL and
// a double quote for PostgreSQL-dialect databases. The backend counts Spanner // a double quote for PostgreSQL-dialect databases. The backend counts Spanner
// unconditionally in `uses_connection_identifier_quote`, so the UI must fetch // unconditionally in `uses_connection_identifier_quote`, so the UI must fetch
@@ -302,6 +302,7 @@ pub fn build_table_data_select_sql_with_database(
database_type, database_type,
&options.columns, &options.columns,
tdengine_should_include_tbname(database_type, options.table_type.as_deref()), 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); 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<DatabaseType>, ide
// Kingbase — when no quote was reported the callers fall back to // Kingbase — when no quote was reported the callers fall back to
// `quote_table_identifier`, whose static mapping is GoogleSQL-correct. // `quote_table_identifier`, whose static mapping is GoogleSQL-correct.
|| database_type == Some(DatabaseType::Spanner) || 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()) || (database_type == Some(DatabaseType::Informix) && identifier_quote.is_some())
|| (matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres)) || (matches!(database_type, Some(DatabaseType::Gaussdb | DatabaseType::OpenGauss | DatabaseType::Postgres))
&& identifier_quote.is_some()) && identifier_quote.is_some())
@@ -734,6 +738,7 @@ pub(super) fn build_select_columns(
database_type: Option<DatabaseType>, database_type: Option<DatabaseType>,
columns: &[String], columns: &[String],
include_tdengine_tbname: bool, include_tdengine_tbname: bool,
identifier_quote: Option<&str>,
) -> String { ) -> String {
if columns.is_empty() { if columns.is_empty() {
if database_type == Some(DatabaseType::Tdengine) && include_tdengine_tbname { if database_type == Some(DatabaseType::Tdengine) && include_tdengine_tbname {
@@ -784,7 +789,11 @@ pub(super) fn build_select_columns(
columns columns
.iter() .iter()
.map(|column| { .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) { if database_type == Some(DatabaseType::Hive) {
format!("{ident} AS {ident}") format!("{ident} AS {ident}")
} else { } else {
@@ -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] #[test]
fn quotes_gaussdb_jdbc_identifiers_selectively() { fn quotes_gaussdb_jdbc_identifiers_selectively() {
for (name, expected) in [ for (name, expected) in [
+1
View File
@@ -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.opengauss.Driver" }), true);
assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "jdbc", jdbc_driver_class: "org.postgresql.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: "kingbase" }), true);
assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "kyuubi" }), true);
assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "gaussdb" }), true); assert.equal(connectionShouldLoadIdentifierQuote({ db_type: "gaussdb" }), true);
}); });