fix(duckdb): return rows for shorthand result queries

This commit is contained in:
t8y2 committed 2026-06-03 11:13:14 +08:00
1 parent 0660194465
commit 6e82fe1e3f
4 files changed
+70 -4

No files matched your search

+29 -3
View File
@@ -9,7 +9,7 @@ use tokio_util::sync::CancellationToken;
use crate::connection::{AppState, PoolKind}; use crate::connection::{AppState, PoolKind};
use crate::db; use crate::db;
use crate::models::connection::DatabaseType; use crate::models::connection::DatabaseType;
use crate::sql::{split_sql_batches, split_sql_statements, starts_with_executable_sql_keyword}; use crate::sql::{split_sql_batches, split_sql_statements, starts_with_duckdb_result_sql_keyword};
pub const QUERY_TIMEOUT: Duration = Duration::from_secs(30); pub const QUERY_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_ROWS: usize = 10000; pub const MAX_ROWS: usize = 10000;
@@ -191,7 +191,7 @@ pub fn duckdb_execute_with_max_rows(
let start = std::time::Instant::now(); let start = std::time::Instant::now();
let row_limit = query_result_row_limit(max_rows); let row_limit = query_result_row_limit(max_rows);
if starts_with_executable_sql_keyword(sql, &["SELECT", "SHOW", "DESCRIBE", "EXPLAIN", "WITH", "PRAGMA"]) { if starts_with_duckdb_result_sql_keyword(sql) {
let mut stmt = con.prepare(sql).map_err(|e| e.to_string())?; let mut stmt = con.prepare(sql).map_err(|e| e.to_string())?;
let mut rows = stmt.query([]).map_err(|e| e.to_string())?; let mut rows = stmt.query([]).map_err(|e| e.to_string())?;
let stmt_ref = rows.as_ref().ok_or("DuckDB statement unavailable")?; let stmt_ref = rows.as_ref().ok_or("DuckDB statement unavailable")?;
@@ -607,7 +607,7 @@ pub async fn do_execute(
.map(|result| normalize_query_result_for_js(truncate_result_with_max_rows(result, max_rows))) .map(|result| normalize_query_result_for_js(truncate_result_with_max_rows(result, max_rows)))
} }
PoolKind::ExternalTabular(ext_pool) => { PoolKind::ExternalTabular(ext_pool) => {
if !starts_with_executable_sql_keyword(sql, &["SELECT", "WITH", "SHOW", "DESCRIBE", "EXPLAIN", "PRAGMA"]) { if !starts_with_duckdb_result_sql_keyword(sql) {
return Err("External data sources are read-only. Only SELECT queries are supported.".to_string()); return Err("External data sources are read-only. Only SELECT queries are supported.".to_string());
} }
let con = ext_pool.cache.clone(); let con = ext_pool.cache.clone();
@@ -1465,6 +1465,32 @@ mod tests {
assert_eq!(result.rows[2][0], serde_json::json!(99.999)); assert_eq!(result.rows[2][0], serde_json::json!(99.999));
} }
#[test]
fn duckdb_execute_returns_rows_for_from_first_query() {
let con = duckdb::Connection::open_in_memory().expect("connect in-memory DuckDB");
con.execute_batch("CREATE TABLE users (id INTEGER, name VARCHAR)").expect("create table");
con.execute_batch("INSERT INTO users VALUES (2, 'Grace'), (1, 'Ada')").expect("insert");
let result = duckdb_execute(&con, "FROM users ORDER BY id").expect("execute from-first query");
assert_eq!(result.columns, vec!["id", "name"]);
assert_eq!(result.rows.len(), 2);
assert_eq!(result.rows[0], vec![serde_json::json!(1), serde_json::json!("Ada")]);
assert_eq!(result.rows[1], vec![serde_json::json!(2), serde_json::json!("Grace")]);
}
#[test]
fn duckdb_execute_returns_rows_for_summarize_query() {
let con = duckdb::Connection::open_in_memory().expect("connect in-memory DuckDB");
con.execute_batch("CREATE TABLE metrics (value INTEGER)").expect("create table");
con.execute_batch("INSERT INTO metrics VALUES (1), (2), (NULL)").expect("insert");
let result = duckdb_execute(&con, "SUMMARIZE metrics").expect("execute summarize query");
assert!(!result.columns.is_empty());
assert!(!result.rows.is_empty());
}
#[test] #[test]
fn duckdb_execute_handles_various_types() { fn duckdb_execute_handles_various_types() {
let con = duckdb::Connection::open_in_memory().expect("connect in-memory DuckDB"); let con = duckdb::Connection::open_in_memory().expect("connect in-memory DuckDB");
+19
View File
@@ -1044,6 +1044,25 @@ pub fn starts_with_executable_sql_keyword_for_database(sql: &str, keywords: &[&s
starts_with_executable_sql_keyword_with_options(sql, keywords, SqlParsingOptions::for_database_type(db_type)) starts_with_executable_sql_keyword_with_options(sql, keywords, SqlParsingOptions::for_database_type(db_type))
} }
pub fn starts_with_duckdb_result_sql_keyword(sql: &str) -> bool {
starts_with_executable_sql_keyword(
sql,
&[
"SELECT",
"SHOW",
"DESCRIBE",
"EXPLAIN",
"WITH",
"PRAGMA",
"FROM",
"SUMMARIZE",
"SUMMARISE",
"PIVOT",
"UNPIVOT",
],
)
}
pub fn starts_with_executable_sql_keyword_with_options( pub fn starts_with_executable_sql_keyword_with_options(
sql: &str, sql: &str,
keywords: &[&str], keywords: &[&str],
+13 -1
View File
@@ -10,6 +10,8 @@ use sqlparser::dialect::{
use sqlparser::parser::Parser; use sqlparser::parser::Parser;
use sqlparser::tokenizer::Span; use sqlparser::tokenizer::Span;
use crate::sql::{starts_with_duckdb_result_sql_keyword, starts_with_executable_sql_keyword};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SqlReferenceAnalysis { pub struct SqlReferenceAnalysis {
pub tables: Vec<SqlTableReference>, pub tables: Vec<SqlTableReference>,
@@ -57,7 +59,12 @@ struct Analyzer {
} }
pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlReferenceAnalysis, String> { pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlReferenceAnalysis, String> {
let statements = match normalize_dialect(dialect).as_str() { let normalized_dialect = normalize_dialect(dialect);
if normalized_dialect == "duckdb" && starts_with_duckdb_parser_gap_sql(sql) {
return Ok(SqlReferenceAnalysis { tables: vec![], columns: vec![] });
}
let statements = match normalized_dialect.as_str() {
"postgres" => Parser::parse_sql(&PostgreSqlDialect {}, sql), "postgres" => Parser::parse_sql(&PostgreSqlDialect {}, sql),
"mysql" => Parser::parse_sql(&MySqlDialect {}, sql), "mysql" => Parser::parse_sql(&MySqlDialect {}, sql),
"sqlite" => Parser::parse_sql(&SQLiteDialect {}, sql), "sqlite" => Parser::parse_sql(&SQLiteDialect {}, sql),
@@ -76,6 +83,11 @@ pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlRef
Ok(SqlReferenceAnalysis { tables: analyzer.tables, columns: analyzer.columns }) Ok(SqlReferenceAnalysis { tables: analyzer.tables, columns: analyzer.columns })
} }
fn starts_with_duckdb_parser_gap_sql(sql: &str) -> bool {
starts_with_duckdb_result_sql_keyword(sql)
&& starts_with_executable_sql_keyword(sql, &["FROM", "SUMMARIZE", "SUMMARISE", "PIVOT", "UNPIVOT"])
}
fn normalize_dialect(dialect: Option<&str>) -> String { fn normalize_dialect(dialect: Option<&str>) -> String {
match dialect.unwrap_or("generic").to_ascii_lowercase().as_str() { match dialect.unwrap_or("generic").to_ascii_lowercase().as_str() {
"postgres" | "postgresql" | "redshift" | "opengauss" | "gaussdb" | "highgo" => "postgres".to_string(), "postgres" | "postgresql" | "redshift" | "opengauss" | "gaussdb" | "highgo" => "postgres".to_string(),
+9
View File
@@ -35,3 +35,12 @@ fn extracts_unqualified_order_by_columns_for_sqlserver_queries() {
analysis.columns.iter().map(|column| (column.qualifier.as_deref(), column.name.as_str())).collect(); analysis.columns.iter().map(|column| (column.qualifier.as_deref(), column.name.as_str())).collect();
assert_eq!(columns, vec![(None, "PDReceiveDatePartInfo")]); assert_eq!(columns, vec![(None, "PDReceiveDatePartInfo")]);
} }
#[test]
fn duckdb_parser_gap_queries_do_not_raise_syntax_errors() {
for sql in ["FROM users;", "SUMMARIZE users;", "SUMMARISE users;"] {
let analysis = analyze_sql_references(sql, Some("duckdb")).expect("duckdb parser gap query should analyze");
assert!(analysis.tables.is_empty());
assert!(analysis.columns.is_empty());
}
}