Skip to content

Commit 144dc45

Browse files
committed
fix: refuse COUNT over a qualified or modified star
COUNT(t.*), COUNT(* EXCLUDE ...), COUNT(* ILIKE ...), COUNT(* RENAME ...), COUNT(DISTINCT t.*) and their windowed forms converted to count(*), which counts every row. Snowflake counts other rows for them, so they refuse until recordings show what to convert them to.
1 parent 287f275 commit 144dc45

4 files changed

Lines changed: 70 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ pub const RULES: &[RuleSpec] = &[
12351235
RuleSpec {
12361236
id: COUNT,
12371237
constructs: &["fn:COUNT"],
1238-
conditions: "COUNT(*), COUNT(x) or COUNT(DISTINCT x); typed NUMBER(18,0). COUNT of a VARIANT skips JSON null as well as NULL; COUNT(DISTINCT) of a VARIANT is refused.",
1238+
conditions: "COUNT(*), COUNT(x) or COUNT(DISTINCT x); typed NUMBER(18,0). COUNT of a VARIANT skips JSON null as well as NULL; COUNT(DISTINCT) of a VARIANT is refused. COUNT over a qualified star (t.*) or a star with EXCLUDE, ILIKE, RENAME or REPLACE is refused, as it counts other rows than COUNT(*).",
12391239
failures: &[],
12401240
},
12411241
RuleSpec {

‎crates/sqlquack-rules/src/conversion/_helpers/expressions/calls.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ use crate::conversion::_helpers::support::divergence::diverge;
6363
use crate::conversion::_helpers::support::scope::{Context, Emitted};
6464
use crate::conversion::_helpers::support::site::{Placement, Site};
6565
use crate::conversion::_helpers::support::tree::{
66-
Payload, field, flag, function_construct, list, node_construct, payload_object,
66+
Payload, field, flag, function_construct, list, node_construct, node_kind, payload_object,
6767
require_understood,
6868
};
6969
use crate::conversion::_helpers::support::typed_leaves::compared_by_text;
7070
use crate::conversion::constants::MAX_KIND;
7171
use crate::conversion::constants::{
7272
AVERAGE, AVG_NAME, COUNT, GENERATOR_RULE, GENERATOR_SEQUENCE_COLUMN, LEAST_GREATEST,
7373
LOCAL_ZONE_SUFFIX, MIN_MAX, MIN_MAX_FLOAT, NOT_SNOWFLAKE_FUNCTIONS, NULLIF,
74-
PART_FUNCTIONS_PAIR, REGEXP_LIKE_NAME, RLIKE_PAIR, SORTED_ARRAY_FUNCTION, SUM, SUM_FLOAT,
75-
TIMESTAMPDIFF_PAIR, TO_DATE_NAME, TRIM, TRY_PREFIX, VARIANT_MAX,
74+
PART_FUNCTIONS_PAIR, REGEXP_LIKE_NAME, RLIKE_PAIR, SORTED_ARRAY_FUNCTION, STAR_KIND, SUM,
75+
SUM_FLOAT, TIMESTAMPDIFF_PAIR, TO_DATE_NAME, TRIM, TRY_PREFIX, VARIANT_MAX,
7676
};
7777
use sqlquack_policy::policy::constants::NUMERIC_RANGE;
7878

@@ -432,6 +432,17 @@ impl AggregateCall<'_> {
432432
fn count(&self) -> Result<Emitted, Refusal> {
433433
let log = self.site.context.log;
434434
let _rule = log.rule(COUNT);
435+
let starred = self
436+
.payload
437+
.get("this")
438+
.and_then(node_kind)
439+
.is_some_and(|(kind, _)| kind == STAR_KIND);
440+
if starred || (flag(self.payload, "star") && flag(self.payload, "distinct")) {
441+
return Err(outside_conditions(
442+
node_construct("count"),
443+
"COUNT over a qualified star or a star with EXCLUDE, ILIKE, RENAME, REPLACE or DISTINCT, which counts other rows than COUNT(*)".to_owned(),
444+
));
445+
}
435446
let inner = match flag(self.payload, "star") {
436447
true => "*".to_owned(),
437448
false => {

‎crates/sqlquack-rules/src/conversion/_helpers/expressions/spellings.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ fn shaped(spelling: &SpellingRule, arguments: &[Value], payload: &Payload) -> Op
100100
json!({ "this": end, "expression": start, "unit": unit_var(unit)? })
101101
}
102102
(CallShape::Count, [argument]) => {
103-
let star = argument.get("star").is_some();
103+
let star = argument.get("star").is_some_and(|star| {
104+
["table", "ilike", "except", "replace", "rename"]
105+
.iter()
106+
.all(|field| star.get(*field).is_none_or(Value::is_null))
107+
});
104108
json!({
105109
"this": if star { Value::Null } else { argument.clone() },
106110
"star": star,

‎crates/sqlquack/tests/cli/test_convert.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::test_types::{
1414
use sha2::{Digest, Sha256};
1515

1616
const OTHER_BODY_SHA256: &str = "0000000000000000000000000000000000000000000000000000000000000000";
17+
const COUNT_STAR_REFUSAL: &str = "COUNT over a qualified star or a star with";
1718
const COUNTING_HANDLER: &str = "import itertools\nTICKETS = itertools.count(1)\ndef handle(*arguments):\n return next(TICKETS)\n";
1819
const QUOTED_ALIAS: &str =
1920
"WITH totals AS (SELECT 1 AS \"orderTotal\") SELECT ordertotal FROM totals";
@@ -1151,6 +1152,55 @@ fn given_recorded_shapes_when_converting_then_they_convert_or_refuse_as_recorded
11511152
}
11521153
}
11531154

1155+
#[test]
1156+
fn given_count_over_a_qualified_or_modified_star_when_converting_then_it_refuses() {
1157+
let test_cases = [
1158+
PythonRefusalTestCase {
1159+
description: "a qualified star counts rows where some column is not NULL, not every row",
1160+
sql: "SELECT COUNT(t.*) AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1161+
expected_mention: COUNT_STAR_REFUSAL,
1162+
},
1163+
PythonRefusalTestCase {
1164+
description: "EXCLUDE narrows the columns a star counts over",
1165+
sql: "SELECT COUNT(* EXCLUDE b) AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1166+
expected_mention: COUNT_STAR_REFUSAL,
1167+
},
1168+
PythonRefusalTestCase {
1169+
description: "ILIKE narrows the columns a star counts over",
1170+
sql: "SELECT COUNT(* ILIKE 'a%') AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1171+
expected_mention: COUNT_STAR_REFUSAL,
1172+
},
1173+
PythonRefusalTestCase {
1174+
description: "RENAME on a counted star",
1175+
sql: "SELECT COUNT(* RENAME a AS z) AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1176+
expected_mention: COUNT_STAR_REFUSAL,
1177+
},
1178+
PythonRefusalTestCase {
1179+
description: "a qualified star with EXCLUDE",
1180+
sql: "SELECT COUNT(t.* EXCLUDE b) AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1181+
expected_mention: COUNT_STAR_REFUSAL,
1182+
},
1183+
PythonRefusalTestCase {
1184+
description: "DISTINCT over a qualified star",
1185+
sql: "SELECT COUNT(DISTINCT t.*) AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1186+
expected_mention: COUNT_STAR_REFUSAL,
1187+
},
1188+
PythonRefusalTestCase {
1189+
description: "a qualified star over a window",
1190+
sql: "SELECT COUNT(t.*) OVER () AS n FROM (SELECT 1 AS a, 'x' AS b UNION ALL SELECT NULL, NULL) AS t",
1191+
expected_mention: COUNT_STAR_REFUSAL,
1192+
},
1193+
];
1194+
for test_case in test_cases {
1195+
let refusals = python_report(test_case.sql, &[])["refusals"].to_string();
1196+
assert!(
1197+
refusals.contains(test_case.expected_mention),
1198+
"{}: {refusals}",
1199+
test_case.description
1200+
);
1201+
}
1202+
}
1203+
11541204
#[test]
11551205
fn given_a_subquery_operand_when_converting_and_running_then_it_is_bound_outside_lambdas() {
11561206
let test_cases = [

0 commit comments

Comments
 (0)