Skip to content

Commit e0638cd

Browse files
committed
refactor(rules): read the shared-parser dialect from one lookup
1 parent e9221bc commit e0638cd

6 files changed

Lines changed: 32 additions & 40 deletions

File tree

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
//! `rules_tree_parser`: the parser that reads a dialect's SQL for rules.
22
3-
use sqlquack_syntax::Dialect;
4-
5-
use crate::sql_tree::constants::{
6-
DUCKDB_DIALECTS, POSTGRES_DIALECTS, SNOWFLAKE_DIALECT, SQLITE_DIALECT,
7-
};
3+
use crate::sql_tree::constants::POSTGRES_DIALECTS;
4+
use crate::sql_tree::main::shared_dialect::shared_dialect;
85
use crate::sql_tree::models::TreeParser;
96

107
/// The parser for `dialect`, or `None`; add a dialect (one line) only once its parser passes the depth probe with an empty allowlist.
118
pub(crate) fn rules_tree_parser(dialect: &str) -> Option<TreeParser> {
129
if POSTGRES_DIALECTS.contains(&dialect) {
1310
return Some(TreeParser::Postgres);
1411
}
15-
if DUCKDB_DIALECTS.contains(&dialect) {
16-
return Some(TreeParser::Shared(Dialect::DuckDB));
17-
}
18-
match dialect {
19-
SNOWFLAKE_DIALECT => Some(TreeParser::Shared(Dialect::Snowflake)),
20-
SQLITE_DIALECT => Some(TreeParser::Shared(Dialect::SQLite)),
21-
_ => None,
22-
}
12+
shared_dialect(dialect).map(TreeParser::Shared)
2313
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//! Entry points.
22
3+
pub mod shared_dialect;
34
pub mod sql_tree_nodes;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! `shared_dialect`: the grammar of SQLQuack's shared parser that reads a dialect's SQL.
2+
3+
use sqlquack_syntax::Dialect;
4+
5+
use crate::sql_tree::constants::{
6+
DUCKDB_DIALECTS, POSTGRES_DIALECTS, SNOWFLAKE_DIALECT, SQLITE_DIALECT,
7+
};
8+
9+
/// The shared-parser grammar for `dialect`, or `None` when the shared parser reads none of it.
10+
pub fn shared_dialect(dialect: &str) -> Option<Dialect> {
11+
if DUCKDB_DIALECTS.contains(&dialect) {
12+
return Some(Dialect::DuckDB);
13+
}
14+
if POSTGRES_DIALECTS.contains(&dialect) {
15+
return Some(Dialect::PostgreSQL);
16+
}
17+
match dialect {
18+
SNOWFLAKE_DIALECT => Some(Dialect::Snowflake),
19+
SQLITE_DIALECT => Some(Dialect::SQLite),
20+
_ => None,
21+
}
22+
}

‎crates/sqlbuild-rules/src/query_facts/_helpers/facts.rs‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::query_facts::_helpers::shape::stub_call;
22
use crate::query_facts::_helpers::tokens::{ComparisonOperator, QueryTokens, SelectTokens};
33
use crate::query_facts::constants::{
4-
DUCKDB_DIALECTS, MAX_AST_DEPTH, MAX_FUNCTION_CALL_DEPTH, POSTGRES_DIALECTS,
5-
SNOWFLAKE_DATE_PART_ARGUMENTS, SNOWFLAKE_DIALECT, SQLITE_DIALECT, TABLE_FUNCTION_STUB,
4+
MAX_AST_DEPTH, MAX_FUNCTION_CALL_DEPTH, SNOWFLAKE_DATE_PART_ARGUMENTS, TABLE_FUNCTION_STUB,
65
TRIPLE_QUOTE_LENGTH,
76
};
87
use crate::query_facts::models::{
98
CallKind, ColumnFact, ComparisonFacts, ComparisonSpan, CteFacts, ExpressionItem, ItemFacts,
109
ModelShape, ParsedFixture, QueryFacts, RelationFacts, SelectFacts, SetFacts, SourceText,
1110
};
11+
use sqlbuild_analysis::sql_tree::main::shared_dialect::shared_dialect;
1212
use sqlquack_syntax::expressions::names::models::{Identifier, Literal, TableRef};
1313
use sqlquack_syntax::expressions::queries::models::{Select, With};
1414
use sqlquack_syntax::{
@@ -18,23 +18,8 @@ use std::cell::{Cell, RefCell};
1818
use std::collections::{HashMap, HashSet};
1919
use std::ops::Range;
2020

21-
/// The SQLQuack grammar that reads `dialect_name` for built-in rules, or `None` when none does.
22-
pub(crate) fn facts_dialect(dialect_name: &str) -> Option<Dialect> {
23-
if DUCKDB_DIALECTS.contains(&dialect_name) {
24-
return Some(Dialect::DuckDB);
25-
}
26-
if POSTGRES_DIALECTS.contains(&dialect_name) {
27-
return Some(Dialect::PostgreSQL);
28-
}
29-
match dialect_name {
30-
SNOWFLAKE_DIALECT => Some(Dialect::Snowflake),
31-
SQLITE_DIALECT => Some(Dialect::SQLite),
32-
_ => None,
33-
}
34-
}
35-
3621
fn parse_query(sql: &str, dialect_name: &str) -> Result<(Expression, Dialect), String> {
37-
let dialect = facts_dialect(dialect_name)
22+
let dialect = shared_dialect(dialect_name)
3823
.ok_or_else(|| format!("no SQL parser for dialect {dialect_name}"))?;
3924
let options = ParseOptions {
4025
complexity_guard: Some(ComplexityGuardOptions {

‎crates/sqlbuild-rules/src/query_facts/constants.rs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ pub(crate) const UDF_CALL: &str = "__udf";
1212
pub(crate) const TABLE_FUNCTION_CALL: &str = "__table_fn";
1313
pub(crate) const MIN_CAST_TOKENS: usize = 2;
1414
pub(crate) const TRIPLE_QUOTE_LENGTH: usize = 3;
15-
/// Dialect names read with the shared DuckDB grammar; `generic` matches the fingerprint tokenizer.
16-
pub(crate) const DUCKDB_DIALECTS: [&str; 3] = ["duckdb", "motherduck", "generic"];
17-
/// Dialect names read with the shared PostgreSQL grammar.
18-
pub(crate) const POSTGRES_DIALECTS: [&str; 2] = ["postgres", "postgresql"];
19-
/// The Snowflake dialect name.
20-
pub(crate) const SNOWFLAKE_DIALECT: &str = "snowflake";
21-
/// The SQLite dialect name.
22-
pub(crate) const SQLITE_DIALECT: &str = "sqlite";
2315
/// Snowflake calls with a date-part argument, as (name, argument count, date-part index).
2416
pub(crate) const SNOWFLAKE_DATE_PART_ARGUMENTS: [(&str, usize, usize); 5] = [
2517
("DATEDIFF", 3, 0),
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! `facts_available`: whether SQLBuild parses a dialect's SQL for built-in rules.
22
3+
use sqlbuild_analysis::sql_tree::main::shared_dialect::shared_dialect;
4+
35
/// Whether SQLBuild parses `dialect`'s SQL for the built-in rules that read query facts.
46
pub(crate) fn facts_available(dialect: &str) -> bool {
5-
crate::query_facts::_helpers::facts::facts_dialect(dialect).is_some()
7+
shared_dialect(dialect).is_some()
68
}

0 commit comments

Comments
 (0)