diff --git a/crates/dbx-core/src/query.rs b/crates/dbx-core/src/query.rs index 8ea251f9fe..35ebfca3fb 100644 --- a/crates/dbx-core/src/query.rs +++ b/crates/dbx-core/src/query.rs @@ -9,7 +9,7 @@ use tokio_util::sync::CancellationToken; use crate::connection::{AppState, PoolKind}; use crate::db; 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 MAX_ROWS: usize = 10000; @@ -191,7 +191,7 @@ pub fn duckdb_execute_with_max_rows( let start = std::time::Instant::now(); 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 rows = stmt.query([]).map_err(|e| e.to_string())?; 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))) } 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()); } let con = ext_pool.cache.clone(); @@ -1465,6 +1465,32 @@ mod tests { 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] fn duckdb_execute_handles_various_types() { let con = duckdb::Connection::open_in_memory().expect("connect in-memory DuckDB"); diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rs index b615fb4e13..0d1dc122c0 100644 --- a/crates/dbx-core/src/sql.rs +++ b/crates/dbx-core/src/sql.rs @@ -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)) } +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( sql: &str, keywords: &[&str], diff --git a/crates/dbx-core/src/sql_analysis.rs b/crates/dbx-core/src/sql_analysis.rs index 88075ac21c..642b04fcb7 100644 --- a/crates/dbx-core/src/sql_analysis.rs +++ b/crates/dbx-core/src/sql_analysis.rs @@ -10,6 +10,8 @@ use sqlparser::dialect::{ use sqlparser::parser::Parser; 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)] pub struct SqlReferenceAnalysis { pub tables: Vec, @@ -57,7 +59,12 @@ struct Analyzer { } pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result { - 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), "mysql" => Parser::parse_sql(&MySqlDialect {}, sql), "sqlite" => Parser::parse_sql(&SQLiteDialect {}, sql), @@ -76,6 +83,11 @@ pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result 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 { match dialect.unwrap_or("generic").to_ascii_lowercase().as_str() { "postgres" | "postgresql" | "redshift" | "opengauss" | "gaussdb" | "highgo" => "postgres".to_string(), diff --git a/crates/dbx-core/tests/sql_analysis.rs b/crates/dbx-core/tests/sql_analysis.rs index 6adaee276f..8f78500bad 100644 --- a/crates/dbx-core/tests/sql_analysis.rs +++ b/crates/dbx-core/tests/sql_analysis.rs @@ -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(); 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()); + } +}