Skip to content

Commit ab479a1

Browse files
committed
Merge remote-tracking branch 'origin/main' into docs/census-policy
2 parents c1abe4d + 37e3767 commit ab479a1

28 files changed

Lines changed: 1290 additions & 42 deletions
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# The names Snowflake gives select items without an alias: the first thing a reader types, as in
2+
# SELECT COUNT(*) FROM orders. The recorded column names are the receipts for naming them.
3+
category = "relational"
4+
construct = "node:alias"
5+
session_profile = "utc-v1"
6+
7+
[[case]]
8+
id = "unaliased_aggregates"
9+
description = "Unaliased aggregates, written in upper, lower and mixed case and with inner spaces, over rows with NULLs and duplicates."
10+
columns = [{ name = "g", type = "VARCHAR" }, { name = "n", type = "NUMBER(10,2)" }]
11+
rows = [
12+
["'a'", "1.50"],
13+
["'a'", "1.50"],
14+
["'b'", "NULL"],
15+
["NULL", "-2.25"],
16+
]
17+
query = "SELECT COUNT(*), count(n), Count( DISTINCT g ), SUM(n), max(g), avg( n ) FROM input"
18+
order = "ordered"
19+
tags = ["null", "duplicates"]
20+
21+
[[case]]
22+
id = "unaliased_group_count"
23+
description = "A grouped column beside an unaliased COUNT(*), as an exploratory query writes it."
24+
columns = [{ name = "g", type = "VARCHAR" }]
25+
rows = [
26+
["'a'"],
27+
["'a'"],
28+
["NULL"],
29+
]
30+
query = "SELECT g, COUNT(*) FROM input GROUP BY g ORDER BY g"
31+
order = "ordered"
32+
tags = ["null", "duplicates"]
33+
34+
[[case]]
35+
id = "unaliased_over_no_rows"
36+
description = "Unaliased aggregates over no rows still name their columns."
37+
columns = [{ name = "n", type = "NUMBER(10,2)" }]
38+
rows = [
39+
["1.00"],
40+
]
41+
query = "SELECT COUNT(*), SUM(n) FROM input WHERE n > 5"
42+
order = "ordered"
43+
tags = ["empty-input"]
44+
45+
[[case]]
46+
id = "unaliased_literals"
47+
description = "Unaliased literals: integers, a negative, a decimal, an exponent, text in mixed case, NULL and booleans."
48+
columns = [{ name = "k", type = "NUMBER(38,0)" }]
49+
rows = [
50+
["1"],
51+
]
52+
query = "SELECT 1, -2, 1.50, 1e3, 'MiXeD text', NULL, TRUE, false FROM input"
53+
order = "ordered"
54+
tags = ["null"]
55+
56+
[[case]]
57+
id = "unaliased_expressions"
58+
description = "Unaliased arithmetic with and without spaces, function calls, casts, a qualified and a quoted column, and an expression across two lines."
59+
columns = [{ name = "g", type = "VARCHAR" }, { name = "n", type = "NUMBER(10,2)" }]
60+
rows = [
61+
["'Ab'", "2.00"],
62+
["NULL", "NULL"],
63+
]
64+
query = "SELECT n + 1, n*2, lower(g), UPPER( g ), g || 'x', CAST(n AS INTEGER), n::INTEGER, COALESCE(n, 0), input.n - 1, \"N\" / 2, n\n + 3 FROM input ORDER BY ordinal"
65+
order = "ordered"
66+
tags = ["null"]

‎coverage/translation/results-baseline.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6713,6 +6713,11 @@
67136713
"relational/subqueries.toml#scalar_subquery_empty": "matching",
67146714
"relational/subqueries.toml#select_distinct": "matching",
67156715
"relational/subqueries.toml#where_filters": "matching",
6716+
"relational/unaliased_output_names.toml#unaliased_aggregates": "matching",
6717+
"relational/unaliased_output_names.toml#unaliased_expressions": "matching",
6718+
"relational/unaliased_output_names.toml#unaliased_group_count": "matching",
6719+
"relational/unaliased_output_names.toml#unaliased_literals": "matching",
6720+
"relational/unaliased_output_names.toml#unaliased_over_no_rows": "matching",
67166721
"relational/union_by_name.toml#union_all_by_name_reordered": "matching",
67176722
"relational/union_by_name.toml#union_by_name_case_insensitive": "matching",
67186723
"relational/union_by_name.toml#union_by_name_chain": "matching",
@@ -11698,7 +11703,7 @@
1169811703
"translation/query/with.select.syntax.toml#t001_recursive": "refused",
1169911704
"translation/query/with.select.syntax.toml#t002_hierarchy": "refused",
1170011705
"translation/query/with.select.syntax.toml#t003_null_stop": "refused",
11701-
"translation/query/with.select.toml#t001_chained": "refused",
11706+
"translation/query/with.select.toml#t001_chained": "matching",
1170211707
"translation/query/with.select.toml#t002_null_group": "matching",
1170311708
"typing/array_agg.toml#array_agg_empty": "shape_only",
1170411709
"typing/array_agg.toml#array_agg_ordered": "shape_only",

‎crates/sqlquack-core/src/diagnostics/constants.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ pub const CASE_ONLY_MATCH: &str = "only without regard to case";
1010
pub const CASE_ONLY_MATCH_HELP: &str = "Snowflake folds unquoted names to upper case, so declare \
1111
the relation or column in the schema as the SQL resolves it (`ORDERS` for `FROM orders`), or \
1212
quote the name in the SQL to keep its case";
13+
/// The words of an unresolved name's reason when two result columns share a name.
14+
pub const REPEATED_RESULT_NAME: &str = "repeats, possibly differing only in case";
15+
/// The help for such a name, replacing SQ0004's own.
16+
pub const REPEATED_RESULT_NAME_HELP: &str =
17+
"give each result column its own alias, such as `SELECT 1 AS first, 1 AS second`";
1318
/// The words of a refusal reading a TIMESTAMP_TZ's local time, which needs the offset DuckDB drops.
1419
pub const ZONED_LOCAL_TIME: &str =
1520
"which reads the local time of an offset DuckDB's TIMESTAMPTZ drops";
@@ -168,11 +173,14 @@ pub const CODES: &[CodeEntry] = &[
168173
help: Some("add an alias, e.g. `AS one`"),
169174
explanation: "Snowflake names an unaliased select item after the expression's text, and \
170175
a converted query must return the same column names. SQLQuack emits Snowflake's name \
171-
only for the expression shapes whose names are verified against recorded results; \
172-
the expression at the span is not one of them, so the statement is refused rather \
173-
than converted with a different column name. An alias fixes the name in both \
176+
only for the shapes recorded results verify, named by their text upper-cased as \
177+
written: literals, columns and COUNT, SUM, MAX, AVG, LOWER, UPPER and COALESCE of \
178+
columns, a column plus, minus, times, divided by or concatenated with a literal, and \
179+
a column cast to INTEGER, written with single spaces and at most one line break. \
180+
Other items, such as paths, CASE, nested calls or other functions, are refused \
181+
rather than converted with a different column name. An alias fixes the name in both \
174182
engines.",
175-
example: r#"echo "SELECT 'a' || 'b'" | sqlquack convert --builtin-policy"#,
183+
example: r#"echo "SELECT 'café' || 'b'" | sqlquack convert --builtin-policy"#,
176184
},
177185
CodeEntry {
178186
code: "SQ0101",

‎crates/sqlquack-core/src/diagnostics/main/diagnose.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use crate::diagnostics::_helpers::coded::coded;
66
use crate::diagnostics::_helpers::line_index::LineIndex;
77
use crate::diagnostics::constants::{
88
CASE_ONLY_MATCH, CASE_ONLY_MATCH_HELP, CONVERSION_BUDGET, DIVERGENCE_REFUSED, GUARD_PREFIX,
9-
MISSING_RULE, NOT_EXPRESSIBLE, PARSE_FIDELITY, TYPE_CONDITION, UNPARSED_PREFIX,
10-
UNRESOLVED_NAME, ZONED_LOCAL_TIME, ZONED_LOCAL_TIME_HELP, ZONED_LOCAL_TIME_NOTE,
9+
MISSING_RULE, NOT_EXPRESSIBLE, PARSE_FIDELITY, REPEATED_RESULT_NAME, REPEATED_RESULT_NAME_HELP,
10+
TYPE_CONDITION, UNPARSED_PREFIX, UNRESOLVED_NAME, ZONED_LOCAL_TIME, ZONED_LOCAL_TIME_HELP,
11+
ZONED_LOCAL_TIME_NOTE,
1112
};
1213
use crate::diagnostics::models::Diagnostic;
1314
use crate::refusals::models::{Refusal, RefusalCategory};
@@ -25,6 +26,11 @@ pub fn diagnose(refusals: &[Refusal], source: &str) -> Vec<Diagnostic> {
2526
{
2627
diagnostic.help = Some(CASE_ONLY_MATCH_HELP.to_owned());
2728
}
29+
if refusal.category == RefusalCategory::UnresolvedName
30+
&& refusal.reason.contains(REPEATED_RESULT_NAME)
31+
{
32+
diagnostic.help = Some(REPEATED_RESULT_NAME_HELP.to_owned());
33+
}
2834
if refusal.category == RefusalCategory::TypeCondition
2935
&& refusal.reason.contains(ZONED_LOCAL_TIME)
3036
{

‎crates/sqlquack-core/src/parsing/models.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct SourceLines {
5353
line_starts: Vec<usize>,
5454
length: usize,
5555
shift: usize,
56+
source: String,
5657
}
5758

5859
impl SourceLines {
@@ -75,9 +76,19 @@ impl SourceLines {
7576
line_starts,
7677
length,
7778
shift,
79+
source: source.to_owned(),
7880
}
7981
}
8082

83+
/// The source text at `location`, a location these lines gave.
84+
pub fn text(&self, location: &SourceLocation) -> String {
85+
self.source
86+
.chars()
87+
.skip(location.start)
88+
.take(location.end.saturating_sub(location.start))
89+
.collect()
90+
}
91+
8192
/// The location of the characters `start..end` of the parsed text.
8293
pub fn location(&self, start: usize, end: usize) -> SourceLocation {
8394
let start = start.saturating_sub(self.shift).min(self.length);

‎crates/sqlquack-rules/src/catalogue/constants.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub const RULES: &[RuleSpec] = &[
194194
RuleSpec {
195195
id: ALIAS,
196196
constructs: &["node:alias"],
197-
conditions: "A single name.",
197+
conditions: "A single name. An unaliased select item that is not a column takes Snowflake's name for it: its text upper-cased as written, spacing, line breaks and quotes kept, as recorded; text outside ASCII or holding a comment is refused with SQ0009.",
198198
failures: &[],
199199
},
200200
RuleSpec {
@@ -384,7 +384,7 @@ pub const RULES: &[RuleSpec] = &[
384384
"fn:DATE_PART",
385385
"node:extract",
386386
],
387-
conditions: "Every branch an untyped NULL, in a conditional, in every branch of a set operation, or as the operand of DATE_PART, EXTRACT and the single-part functions: the result is NULL of Snowflake's untyped NULL type, described as VARCHAR(0), held as a DuckDB VARCHAR, as recorded.",
387+
conditions: "Every branch an untyped NULL, in a conditional, in every branch of a set operation, as the operand of DATE_PART, EXTRACT and the single-part functions, or as a result column of the statement: the result is NULL of Snowflake's untyped NULL type, described as VARCHAR(0), held as a DuckDB VARCHAR, as recorded.",
388388
failures: &[],
389389
},
390390
RuleSpec {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(crate) mod flatten;
33
pub(crate) mod function_body;
44
pub(crate) mod group_all;
55
pub(crate) mod hierarchy;
6+
pub(crate) mod output_names;
67
pub(crate) mod pivot;
78
pub(crate) mod query;
89
pub(crate) mod set_operations;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//! The unaliased select items whose column names Snowflake receipts record.
2+
3+
use serde_json::Value;
4+
5+
use crate::conversion::_helpers::support::tree::{
6+
LITERAL_NUMBER, LITERAL_STRING, list, node_kind, unexpected_fields,
7+
};
8+
use crate::conversion::constants::{
9+
OUTPUT_NAMED_AGGREGATES, OUTPUT_NAMED_ARITHMETIC, OUTPUT_NAMED_CAST_ENDINGS,
10+
OUTPUT_NAMED_FUNCTIONS,
11+
};
12+
13+
/// Whether an unaliased item written as `text` has a shape whose name receipts record.
14+
pub(crate) fn recorded_name_shape(item: &Value, text: &str) -> bool {
15+
let Some((kind, payload)) = node_kind(item) else {
16+
return false;
17+
};
18+
let Some(fields) = payload.as_object() else {
19+
return matches!(kind, "null");
20+
};
21+
let only = |understood: &[&str]| unexpected_fields(fields, understood).is_empty();
22+
let named = |names: &[&str]| {
23+
payload
24+
.get("name")
25+
.and_then(Value::as_str)
26+
.is_some_and(|name| names.contains(&name.to_ascii_uppercase().as_str()))
27+
};
28+
let arguments = list(fields, "args");
29+
match kind {
30+
"aggregate_function" => {
31+
only(&["name", "args", "distinct"])
32+
&& named(OUTPUT_NAMED_AGGREGATES)
33+
&& arguments.len() == 1
34+
&& arguments.iter().all(|argument| {
35+
plain_column(argument)
36+
|| (named(&["COUNT"]) && matches!(node_kind(argument), Some(("star", _))))
37+
})
38+
}
39+
"function" => {
40+
only(&["name", "args"])
41+
&& named(OUTPUT_NAMED_FUNCTIONS)
42+
&& !arguments.is_empty()
43+
&& arguments
44+
.iter()
45+
.all(|argument| plain_column(argument) || integer_literal(argument))
46+
}
47+
"concat" => {
48+
only(&["left", "right"])
49+
&& payload.get("left").is_some_and(plain_column)
50+
&& payload.get("right").is_some_and(string_literal)
51+
}
52+
"cast" => {
53+
let upper = text.to_ascii_uppercase();
54+
only(&["this", "to", "double_colon_syntax"])
55+
&& payload.get("this").is_some_and(plain_column)
56+
&& OUTPUT_NAMED_CAST_ENDINGS
57+
.iter()
58+
.any(|ending| upper.ends_with(ending))
59+
}
60+
"neg" => {
61+
only(&["this"])
62+
&& payload.get("this").is_some_and(integer_literal)
63+
&& text.strip_prefix('-').is_some_and(|rest| {
64+
!rest.is_empty() && rest.bytes().all(|byte| byte.is_ascii_digit())
65+
})
66+
}
67+
"literal" => recorded_literal(item),
68+
"null" | "boolean" => true,
69+
arithmetic if OUTPUT_NAMED_ARITHMETIC.contains(&arithmetic) => {
70+
only(&["left", "right"])
71+
&& payload.get("left").is_some_and(plain_column)
72+
&& payload.get("right").is_some_and(integer_literal)
73+
}
74+
_ => false,
75+
}
76+
}
77+
78+
/// A column, qualified or not, each part unquoted or quoted and already upper case.
79+
fn plain_column(node: &Value) -> bool {
80+
let Some(("column", payload)) = node_kind(node) else {
81+
return false;
82+
};
83+
let Some(fields) = payload.as_object() else {
84+
return false;
85+
};
86+
let upper_or_unquoted = |part: &Value| {
87+
let quoted = part.get("quoted").and_then(Value::as_bool) == Some(true);
88+
let name = part.get("name").and_then(Value::as_str).unwrap_or_default();
89+
!name.is_empty() && (!quoted || name == name.to_ascii_uppercase())
90+
};
91+
unexpected_fields(fields, &["name", "table"]).is_empty()
92+
&& payload.get("name").is_some_and(upper_or_unquoted)
93+
&& payload
94+
.get("table")
95+
.filter(|table| !table.is_null())
96+
.is_none_or(upper_or_unquoted)
97+
}
98+
99+
/// A literal spelled as a receipt records: an integer, a decimal, an exponent or plain text.
100+
fn recorded_literal(node: &Value) -> bool {
101+
let Some(("literal", payload)) = node_kind(node) else {
102+
return false;
103+
};
104+
let value = payload
105+
.get("value")
106+
.and_then(Value::as_str)
107+
.unwrap_or_default();
108+
match payload.get("literal_type").and_then(Value::as_str) {
109+
Some(LITERAL_NUMBER) => {
110+
let (mantissa, exponent) = match value.split_once('e') {
111+
Some((mantissa, exponent)) => (mantissa, Some(exponent)),
112+
None => (value, None),
113+
};
114+
let (whole, fraction) = match mantissa.split_once('.') {
115+
Some((whole, fraction)) => (whole, Some(fraction)),
116+
None => (mantissa, None),
117+
};
118+
let digits = |text: &str| !text.is_empty() && text.bytes().all(|b| b.is_ascii_digit());
119+
let leading_zero = whole.starts_with('0');
120+
digits(whole)
121+
&& (whole.len() == 1 || !leading_zero)
122+
&& fraction.is_none_or(digits)
123+
&& exponent
124+
.is_none_or(|exponent| fraction.is_none() && !leading_zero && digits(exponent))
125+
}
126+
Some(LITERAL_STRING) => string_literal(node),
127+
_ => false,
128+
}
129+
}
130+
131+
fn integer_literal(node: &Value) -> bool {
132+
recorded_literal(node)
133+
&& node_kind(node)
134+
.and_then(|(_, payload)| payload.get("value"))
135+
.and_then(Value::as_str)
136+
.is_some_and(|value| value.bytes().all(|byte| byte.is_ascii_digit()))
137+
}
138+
139+
/// Non-empty printable ASCII text without a quote or backslash, which has one spelling.
140+
fn string_literal(node: &Value) -> bool {
141+
let Some(("literal", payload)) = node_kind(node) else {
142+
return false;
143+
};
144+
payload.get("literal_type").and_then(Value::as_str) == Some(LITERAL_STRING)
145+
&& payload
146+
.get("value")
147+
.and_then(Value::as_str)
148+
.is_some_and(|value| {
149+
!value.is_empty()
150+
&& value
151+
.bytes()
152+
.all(|byte| (b' '..=b'~').contains(&byte) && byte != b'\'' && byte != b'\\')
153+
})
154+
}

0 commit comments

Comments
 (0)