fix(query): preserve JDBC aliases and duplicate-column sorting

This commit is contained in:
GIWTO authored and GitHub committed 2026-07-27 00:27:12 +08:00
1 parent 5ad08e633b
commit d9a6655fd9
4 files changed
+108 -7

No files matched your search

+87 -7
View File
@@ -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::<Vec<_>>()
.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 {