Skip to content

Commit 4ea7859

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/pivot-unaliased
2 parents a6e8689 + 9c008e0 commit 4ea7859

3 files changed

Lines changed: 93 additions & 2 deletions

File tree

‎crates/sqlquack-rules/src/conversion/_helpers/queries/query.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,19 @@ fn convert_projection(
607607
let _rule = site.context.log.rule(COMMENT);
608608
let annotated = payload_object(payload, &construct)?;
609609
require_understood(annotated, &["this", "trailing_comments"], &construct)?;
610-
convert_projection(field(annotated, "this", &construct)?, site, named)
610+
let this = field(annotated, "this", &construct)?;
611+
match node_kind(this) {
612+
Some((kind, _)) if named && !matches!(kind, "star" | "alias" | "column") => {
613+
let refusal = missing(
614+
Construct::new(ConstructFamily::Syntax, UNALIASED_OUTPUT_NAME_CONSTRUCT),
615+
format!(
616+
"Snowflake names this `{kind}` column after its text, comment included"
617+
),
618+
);
619+
Err(site.context.locator.placed(refusal, this))
620+
}
621+
_ => convert_projection(this, site, named),
622+
}
611623
}
612624
Some(("alias", payload)) => {
613625
let _rule = site.context.log.rule(ALIAS);

‎crates/sqlquack-rules/src/conversion/_helpers/support/tree.rs‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const COSMETIC_FIELDS: &[&str] = &[
1818
"comments",
1919
"leading_comments",
2020
"pre_alias_comments",
21+
"post_select_comments",
2122
"left_comments",
2223
"operator_comments",
2324
"right_comments",
@@ -194,13 +195,65 @@ pub(crate) fn integer_literal(node: &Value) -> Option<u64> {
194195

195196
/// The tree conversion reads, positions stripped and quoted identifiers folded if case is ignored.
196197
pub(crate) fn session_tree(positioned: &Value, ignore_quoted_case: bool) -> Result<Value, String> {
197-
let mut tree = strip_positions(positioned);
198+
let mut tree = without_comment_wrappers(&strip_positions(positioned));
198199
if ignore_quoted_case {
199200
fold_quoted(&mut tree)?;
200201
}
201202
Ok(tree)
202203
}
203204

205+
/// `value` with each node that only wrapped a comment replaced by the node it wrapped.
206+
fn without_comment_wrappers(value: &Value) -> Value {
207+
unwrap_comments(value, false)
208+
}
209+
210+
/// `value` with its comment wrappers unwrapped, its own kept if `keep_wrapper`.
211+
fn unwrap_comments(value: &Value, keep_wrapper: bool) -> Value {
212+
match value {
213+
Value::Object(fields) => match fields.get("annotated").and_then(Value::as_object) {
214+
Some(annotated) if !keep_wrapper && fields.len() == 1 && annotated.len() == 1 => {
215+
annotated
216+
.get("this")
217+
.map_or_else(|| value.clone(), without_comment_wrappers)
218+
}
219+
_ => Value::Object(
220+
fields
221+
.iter()
222+
.map(|(key, field)| (key.clone(), unwrap_field(key, field)))
223+
.collect(),
224+
),
225+
},
226+
Value::Array(items) => Value::Array(items.iter().map(without_comment_wrappers).collect()),
227+
other => other.clone(),
228+
}
229+
}
230+
231+
/// A node's field unwrapped; a SELECT keeps its select items' wrappers, which name their columns.
232+
fn unwrap_field(key: &str, field: &Value) -> Value {
233+
match (key, field) {
234+
("select", Value::Object(clauses)) => Value::Object(
235+
clauses
236+
.iter()
237+
.map(|(name, clause)| (name.clone(), unwrap_clause(name, clause)))
238+
.collect(),
239+
),
240+
_ => without_comment_wrappers(field),
241+
}
242+
}
243+
244+
/// A SELECT clause with comment wrappers unwrapped, except around the select items themselves.
245+
fn unwrap_clause(name: &str, clause: &Value) -> Value {
246+
match (name, clause) {
247+
("expressions", Value::Array(items)) => Value::Array(
248+
items
249+
.iter()
250+
.map(|item| unwrap_comments(item, true))
251+
.collect(),
252+
),
253+
_ => without_comment_wrappers(clause),
254+
}
255+
}
256+
204257
/// Upper-cases the name of each quoted identifier under `value`.
205258
fn fold_quoted(value: &mut Value) -> Result<(), String> {
206259
match value {

‎crates/sqlquack/src/analysis/tests/test_analyze.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ fn given_sql_and_a_schema_when_analyzing_then_it_converts_or_refuses_with_a_cate
9292
converted: true,
9393
},
9494
},
95+
AnalyzeTestCase {
96+
description: "a block comment straight after SELECT is dropped",
97+
sql: "SELECT /* c */ amount AS v FROM orders",
98+
expected_summary: converted(),
99+
},
100+
AnalyzeTestCase {
101+
description: "a line comment straight after SELECT is dropped",
102+
sql: "SELECT -- note\n amount AS v FROM orders",
103+
expected_summary: converted(),
104+
},
105+
AnalyzeTestCase {
106+
description: "a comment after SELECT inside a CTE and a subquery is dropped",
107+
sql: "WITH x AS (SELECT /* c */ amount FROM orders) \
108+
SELECT v FROM (SELECT /* d */ amount AS v FROM x) AS s",
109+
expected_summary: converted(),
110+
},
111+
AnalyzeTestCase {
112+
description: "a comment after a literal argument leaves the literal a literal",
113+
sql: "SELECT ROUND(amount, 1 /* c */) AS v FROM orders",
114+
expected_summary: converted(),
115+
},
116+
AnalyzeTestCase {
117+
description: "an unaliased literal carrying a comment has no recorded column name",
118+
sql: "SELECT 'x' /* c */ FROM orders",
119+
expected_summary: refused(RefusalCategory::MissingRule),
120+
},
95121
AnalyzeTestCase {
96122
description: "comments after select items and inside expressions are dropped",
97123
sql: "SELECT amount /* net */ + 1 AS gross, -- trailing\n customer_id -- last\n \

0 commit comments

Comments
 (0)