diff --git a/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts b/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts index 80ff82b254..e78361d523 100644 --- a/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts +++ b/apps/desktop/src/lib/__tests__/database/jdbcDialect.spec.ts @@ -64,6 +64,16 @@ describe("jdbc dialect inference", () => { expect(inferJdbcDialect(opengaussConnection)).toBe("opengauss"); expect(connectionUsesDatabaseObjectTreeMode(opengaussConnection)).toBe(false); }); + + it("detects Dameng JDBC connections", () => { + const damengConnection = { + db_type: "jdbc" as const, + connection_string: "jdbc:dm://localhost:5236/DAMENG", + jdbc_driver_class: "dm.jdbc.driver.DmDriver", + }; + + expect(inferJdbcDialect(damengConnection)).toBe("dameng"); + }); }); describe("query execution schema", () => { diff --git a/apps/desktop/src/lib/database/jdbcDialect.ts b/apps/desktop/src/lib/database/jdbcDialect.ts index 30b4d3206b..27a3af6122 100644 --- a/apps/desktop/src/lib/database/jdbcDialect.ts +++ b/apps/desktop/src/lib/database/jdbcDialect.ts @@ -13,6 +13,7 @@ const JDBC_DIALECT_MATCHERS: Array<{ type: DatabaseType; patterns: RegExp[] }> = { type: "hive", patterns: [/org\.apache\.hive\.jdbc\.HiveDriver/i, /hive-jdbc/i] }, { type: "mysql", patterns: [/jdbc:mysql:/i, /mysql/i, /mariadb/i, /kyuubi/i, /hive2/i] }, { type: "gaussdb", patterns: [/jdbc:gaussdb:/i, /com\.huawei\.gaussdb/i, /gaussdb/i] }, + { type: "dameng", patterns: [/jdbc:dm:/i, /dm\.jdbc\.driver/i, /dameng/i] }, { type: "opengauss", patterns: [/jdbc:opengauss:/i, /org\.opengauss/i, /opengauss/i] }, { type: "postgres", patterns: [/jdbc:postgresql:/i, /postgres/i] }, { type: "sqlserver", patterns: [/jdbc:sqlserver:/i, /sqlserver/i, /mssql/i] }, diff --git a/crates/dbx-core/src/query_result_sql.rs b/crates/dbx-core/src/query_result_sql.rs index 78e7021d45..a7416698f9 100644 --- a/crates/dbx-core/src/query_result_sql.rs +++ b/crates/dbx-core/src/query_result_sql.rs @@ -139,7 +139,7 @@ pub fn build_query_pagination_execution_plan( let can_use_first_page_cursor = options.use_agent_cursor && options.pagination.offset == 0; let prefer_server_pagination = options.database_type == Some(DatabaseType::Kingbase); if can_use_first_page_cursor && !prefer_server_pagination { - if !options.first_page_uses_actual_sql { + if !options.first_page_uses_actual_sql && options.sql == options.query_base_sql { plan.sql_to_execute = options.query_base_sql; } plan.page_limit = Some(options.pagination.limit); @@ -149,7 +149,7 @@ pub fn build_query_pagination_execution_plan( } let paginated = build_paginated_query_sql(PaginatedQuerySqlOptions { - original_sql: options.sql, + original_sql: options.sql.clone(), database_type: options.database_type, limit: options.pagination.limit, offset: options.pagination.offset, @@ -164,7 +164,7 @@ pub fn build_query_pagination_execution_plan( // LIMIT/OFFSET whenever the statement can be rewritten safely. Keep the // Agent cursor as a bounded fallback for multi-statement or dialect- // specific SQL that the pagination parser cannot transform. - if !options.first_page_uses_actual_sql { + if !options.first_page_uses_actual_sql && options.sql == options.query_base_sql { plan.sql_to_execute = options.query_base_sql; } plan.page_limit = Some(options.pagination.limit); @@ -268,7 +268,10 @@ pub fn build_sorted_query_sql(options: SortedQuerySqlOptions) -> QuerySqlBuildRe // Doris accepts the derived-table alias but not its column-name list. && options.database_type != Some(DatabaseType::Doris) && options.database_type != Some(DatabaseType::Sqlite) - && options.database_type != Some(DatabaseType::DuckDb); + && options.database_type != Some(DatabaseType::DuckDb) + && options.database_type != Some(DatabaseType::Dameng) + && options.database_type != Some(DatabaseType::Oracle) + && options.database_type != Some(DatabaseType::OceanbaseOracle); let sort_alias = if use_derived_column_aliases { aliases .get(options.column_index) @@ -284,7 +287,22 @@ pub fn build_sorted_query_sql(options: SortedQuerySqlOptions) -> QuerySqlBuildRe } else { options.result_columns.get(options.column_index).cloned().unwrap_or_else(|| options.column.clone()) }; - let quoted_column = quote_table_identifier(options.database_type, &sort_alias); + // Oracle-compatible derived tables do not accept a PostgreSQL-style + // column alias list. Use the selected column position when duplicate + // labels would otherwise make ORDER BY ambiguous. + let use_sort_ordinal = !use_derived_column_aliases + && matches!( + options.database_type, + Some(DatabaseType::Dameng | DatabaseType::Oracle | DatabaseType::OceanbaseOracle) + ) + && options.result_columns.get(options.column_index).is_some_and(|column| { + options.result_columns.iter().filter(|candidate| candidate.eq_ignore_ascii_case(column)).count() > 1 + }); + let sort_reference = if use_sort_ordinal { + (options.column_index + 1).to_string() + } else { + quote_table_identifier(options.database_type, &sort_alias) + }; let wrapped_statement = if options.database_type == Some(DatabaseType::SqlServer) { sql_server_statement_for_derived_table(&statement) } else { @@ -298,11 +316,11 @@ pub fn build_sorted_query_sql(options: SortedQuerySqlOptions) -> QuerySqlBuildRe .collect::>() .join(", "); ok(format!( - "SELECT * FROM ({wrapped_statement}) t({alias_list}) ORDER BY {quoted_column} {};", + "SELECT * FROM ({wrapped_statement}) t({alias_list}) ORDER BY {sort_reference} {};", options.direction.as_sql() )) } else { - ok(format!("SELECT * FROM ({wrapped_statement}) t ORDER BY {quoted_column} {};", options.direction.as_sql())) + ok(format!("SELECT * FROM ({wrapped_statement}) t ORDER BY {sort_reference} {};", options.direction.as_sql())) } } @@ -2534,6 +2552,68 @@ WHERE u.id = picked.id; ); } + #[test] + fn builds_dameng_sorted_query_without_alias_list() { + let result = build_sorted_query_sql(SortedQuerySqlOptions { + original_sql: "SELECT id, name FROM users".to_string(), + database_type: Some(DatabaseType::Dameng), + result_columns: vec!["id".to_string(), "name".to_string()], + column_index: 0, + column: "id".to_string(), + direction: QuerySortDirection::Asc, + }); + + assert_eq!(result.sql.unwrap(), "SELECT * FROM (SELECT id, name FROM users) t ORDER BY \"id\" ASC;"); + } + + #[test] + fn builds_dameng_sorted_query_by_ordinal_for_duplicate_columns() { + let result = build_sorted_query_sql(SortedQuerySqlOptions { + original_sql: "SELECT a.id, b.id FROM a JOIN b ON b.a_id = a.id".to_string(), + database_type: Some(DatabaseType::Dameng), + result_columns: vec!["ID".to_string(), "id".to_string()], + column_index: 1, + column: "id".to_string(), + direction: QuerySortDirection::Desc, + }); + + assert_eq!( + result.sql.unwrap(), + "SELECT * FROM (SELECT a.id, b.id FROM a JOIN b ON b.a_id = a.id) t ORDER BY 2 DESC;" + ); + } + + #[test] + fn builds_oracle_sorted_query_by_ordinal_for_duplicate_columns() { + let result = build_sorted_query_sql(SortedQuerySqlOptions { + original_sql: "SELECT a.id, b.id FROM a JOIN b ON b.a_id = a.id".to_string(), + database_type: Some(DatabaseType::Oracle), + result_columns: vec!["ID".to_string(), "ID".to_string()], + column_index: 0, + column: "ID".to_string(), + direction: QuerySortDirection::Asc, + }); + + assert_eq!( + result.sql.unwrap(), + "SELECT * FROM (SELECT a.id, b.id FROM a JOIN b ON b.a_id = a.id) t ORDER BY 1 ASC;" + ); + } + + #[test] + fn preserves_derived_column_aliases_for_generic_jdbc() { + let result = build_sorted_query_sql(SortedQuerySqlOptions { + original_sql: "SELECT id, name FROM users".to_string(), + database_type: Some(DatabaseType::Jdbc), + result_columns: vec!["id".to_string(), "name".to_string()], + column_index: 0, + column: "id".to_string(), + direction: QuerySortDirection::Asc, + }); + + assert_eq!(result.sql.unwrap(), "SELECT * FROM (SELECT id, name FROM users) t(id, name) ORDER BY id ASC;"); + } + #[test] fn rejects_with_query_sorting() { let result = build_sorted_query_sql(SortedQuerySqlOptions { diff --git a/packages/app-tests/jdbcDialect.test.ts b/packages/app-tests/jdbcDialect.test.ts index be9b1026ee..672a830fd9 100644 --- a/packages/app-tests/jdbcDialect.test.ts +++ b/packages/app-tests/jdbcDialect.test.ts @@ -47,6 +47,16 @@ test("infers GaussDB-compatible JDBC connections as schema-aware", () => { assert.equal(connectionUsesDatabaseObjectTreeMode(opengaussConnection), false); }); +test("infers Dameng JDBC connections", () => { + const damengConnection = { + db_type: "jdbc" as const, + connection_string: "jdbc:dm://127.0.0.1:5236/DAMENG", + jdbc_driver_class: "dm.jdbc.driver.DmDriver", + }; + + assert.equal(inferJdbcDialect(damengConnection), "dameng"); +}); + test("discovers schemas only for unknown generic JDBC connections", () => { assert.equal(connectionShouldDiscoverJdbcSchemas({ db_type: "jdbc", driver_profile: "jdbc" }), true); assert.equal(connectionShouldDiscoverJdbcSchemas({ db_type: "jdbc", connection_string: "jdbc:mysql://127.0.0.1:3306/app" }), false);