25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
feat(sql): group-by violation check and where completion enter accept
大峰527b56ac414f
diff --git a/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletionKeys.spec.ts b/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletionKeys.spec.tsindex 1342b43cde..c1473e6c94 100644--- a/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletionKeys.spec.ts+++ b/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletionKeys.spec.ts@@ -149,6 +149,31 @@ describe("QueryEditor completion-key ownership", () => {expect(completion.suppressAutoStartUntil).toBeGreaterThan(Date.now());});+ it("accepts the first completion once a pending Enter retry becomes active", async () => {+ const { keys, view, status, runtime, accept, newline, settings } = createHarness();+ status.mockReturnValue("pending");+ settings.editorSettings.selectFirstCompletionOnOpen = true;+ expect(keys.handleEnter(view)).toBe(true);+ expect(newline).not.toHaveBeenCalled();+ status.mockReturnValue("active");+ runtime.codeMirrorSelectedCompletionIndex = () => 0;+ accept.mockReturnValue(true);+ await vi.advanceTimersByTimeAsync(5);+ expect(accept).toHaveBeenCalledOnce();+ expect(view.state.doc.toString()).toBe("abc");+ });++ it("inserts one newline when a pending Enter retry expires", async () => {+ const { keys, view, status, settings, newline, completion } = createHarness();+ status.mockReturnValue("pending");+ settings.editorSettings.selectFirstCompletionOnOpen = true;+ expect(keys.handleEnter(view)).toBe(true);+ await vi.advanceTimersByTimeAsync(25);+ expect(newline).toHaveBeenCalledOnce();+ expect(view.state.doc.toString()).toBe("abc\n");+ expect(completion.suppressAutoStartUntil).toBeGreaterThan(Date.now());+ });+it("cancels Enter retries without cancelling another editor's pending Tab", async () => {const first = createHarness();const second = createHarness();diff --git a/apps/desktop/src/lib/__tests__/sql/semantic/diagnostics.spec.ts b/apps/desktop/src/lib/__tests__/sql/semantic/diagnostics.spec.tsnew file mode 100644index 0000000000..7ba48f8734--- /dev/null+++ b/apps/desktop/src/lib/__tests__/sql/semantic/diagnostics.spec.ts@@ -0,0 +1,58 @@+import { describe, expect, it } from "vitest";+import { buildSqlSemanticDiagnostics } from "@/lib/sql/semantic/diagnostics";+import type { SqlReferenceAnalysis } from "@/types/database";++const span = (startColumn: number, endColumn: number) => ({+ start_line: 1,+ start_column: startColumn,+ end_line: 1,+ end_column: endColumn,+});++describe("buildSqlSemanticDiagnostics GROUP BY violations", () => {+ it("maps analyzer GROUP BY violations to error diagnostics", () => {+ const sql = "SELECT USER_ID, SUM(MONEY) FROM users GROUP BY USER_NAME";+ const analysis: SqlReferenceAnalysis = {+ tables: [],+ columns: [],+ group_by_violations: [{ span: span(8, 15), column: "USER_ID", qualifier: null }],+ };++ const diagnostics = buildSqlSemanticDiagnostics(analysis, { tables: [], columnsByTable: new Map(), sql });++ expect(diagnostics).toEqual([+ {+ span: span(8, 15),+ message: "Column USER_ID must appear in the GROUP BY clause or be used in an aggregate function",+ severity: "error",+ },+ ]);+ });++ it("downgrades the severity to warning on PostgreSQL", () => {+ const analysis: SqlReferenceAnalysis = {+ tables: [],+ columns: [],+ group_by_violations: [{ span: span(8, 15), column: "name", qualifier: "u" }],+ };++ const diagnostics = buildSqlSemanticDiagnostics(analysis, { tables: [], columnsByTable: new Map(), databaseType: "postgres" });++ expect(diagnostics).toHaveLength(1);+ expect(diagnostics[0]?.severity).toBe("warning");+ });++ it("includes the qualifier in the message and tolerates missing sql", () => {+ const analysis: SqlReferenceAnalysis = {+ tables: [],+ columns: [],+ group_by_violations: [{ span: span(1, 5), column: "name", qualifier: "u" }],+ };++ const diagnostics = buildSqlSemanticDiagnostics(analysis, { tables: [], columnsByTable: new Map() });++ expect(diagnostics).toHaveLength(1);+ expect(diagnostics[0]?.message).toBe("Column u.name must appear in the GROUP BY clause or be used in an aggregate function");+ expect(diagnostics[0]?.severity).toBe("error");+ });+});diff --git a/apps/desktop/src/lib/editor/queryEditorCompletionAcceptance.ts b/apps/desktop/src/lib/editor/queryEditorCompletionAcceptance.tsindex 54fd95068c..473770e8f6 100644--- a/apps/desktop/src/lib/editor/queryEditorCompletionAcceptance.ts+++ b/apps/desktop/src/lib/editor/queryEditorCompletionAcceptance.ts@@ -31,7 +31,11 @@ export function acceptSelectedOrFirstCompletion(view: EditorView, acceptCompleti}export function acceptSelectedCompletionWithRetry(view: EditorView, options: RetryCompletionAcceptanceOptions): CompletionAcceptanceAttempt {- if (options.completionStatus(view.state) !== "active") return { handled: false };+ // A "pending" status (results still computing, e.g. columns loading from the+ // database) must also enter the retry loop: bailing out immediately would turn+ // Enter into a newline while the suggestion popup is about to open. Only a+ // null status (no completion requested at all) is left to the caller.+ if (options.completionStatus(view.state) == null) return { handled: false };if (acceptSelectedOrFirstCompletion(view, options.acceptCompletion, options.selectedCompletionIndex, options.selectFirstCompletion)) return { handled: true };const initialDoc = view.state.doc;diff --git a/apps/desktop/src/lib/sql/semantic/diagnostics.ts b/apps/desktop/src/lib/sql/semantic/diagnostics.tsindex 11d8f41a6b..0053aa72e9 100644--- a/apps/desktop/src/lib/sql/semantic/diagnostics.ts+++ b/apps/desktop/src/lib/sql/semantic/diagnostics.ts@@ -2,7 +2,7 @@ import type { SqlCompletionColumn, SqlCompletionTable } from "@/lib/sql/sqlComplimport { getSqlCompletionContext, isOracleSystemValueName } from "@/lib/sql/sqlCompletion";import { executableStatementRanges, keepsOracleStyleBlockTogether, type SqlTextRange } from "@/lib/sql/sqlStatementRanges";import { DBX_TDENGINE_TBNAME_COLUMN, isTdengineStableTableType } from "@/lib/table/tableEditing";-import type { DatabaseType, SqlColumnReference, SqlReferenceAnalysis, SqlReferenceScope, SqlTableReference, SqlTextSpan } from "@/types/database";+import type { DatabaseType, SqlColumnReference, SqlGroupByViolation, SqlReferenceAnalysis, SqlReferenceScope, SqlTableReference, SqlTextSpan } from "@/types/database";export interface SqlSemanticDiagnostic {span: SqlTextSpan;@@ -318,9 +318,26 @@ export function buildSqlSemanticDiagnostics(analysis: SqlReferenceAnalysis, sche});}+ for (const violation of analysis.group_by_violations ?? []) {+ diagnostics.push(buildSqlGroupByViolationDiagnostic(violation, schema.sql, schema.databaseType));+ }+return diagnostics;}+function buildSqlGroupByViolationDiagnostic(violation: SqlGroupByViolation, sql?: string, databaseType?: DatabaseType): SqlSemanticDiagnostic {+ const displayName = violation.qualifier ? `${violation.qualifier}.${violation.column}` : violation.column;+ return {+ span: trimSqlTextSpanWhitespace(sql, violation.span),+ message: `Column ${displayName} must appear in the GROUP BY clause or be used in an aggregate function`,+ // PostgreSQL additionally accepts columns functionally dependent on the+ // GROUP BY key (e.g. projecting other columns of the primary-key table),+ // which the analyzer cannot see from column metadata alone; keep the hint+ // there, but as a warning instead of an error.+ severity: databaseType === "postgres" ? "warning" : "error",+ };+}+function tdengineStableTableKeys(tables: readonly SqlCompletionTable[]): Set<string> {const keys = new Set<string>();for (const table of tables) {diff --git a/apps/desktop/src/types/database.ts b/apps/desktop/src/types/database.tsindex d5be03e9a4..99c14694e3 100644--- a/apps/desktop/src/types/database.ts+++ b/apps/desktop/src/types/database.ts@@ -1576,6 +1576,12 @@ export interface SqlColumnReference {scope_id?: number;}+export interface SqlGroupByViolation {+ span: SqlTextSpan;+ column: string;+ qualifier?: string | null;+}+export interface SqlReferenceScope {id: number;parent_id?: number | null;@@ -1585,6 +1591,7 @@ export interface SqlReferenceAnalysis {tables: SqlTableReference[];columns: SqlColumnReference[];scopes?: SqlReferenceScope[];+ group_by_violations?: SqlGroupByViolation[];}export type TreeNodeType =diff --git a/crates/dbx-sql-core/src/sql_analysis.rs b/crates/dbx-sql-core/src/sql_analysis.rsindex c81837468c..325b947867 100644--- a/crates/dbx-sql-core/src/sql_analysis.rs+++ b/crates/dbx-sql-core/src/sql_analysis.rs@@ -1,5 +1,5 @@use serde::{Deserialize, Serialize};-use std::collections::HashSet;+use std::collections::{HashMap, HashSet};use std::sync::LazyLock;use regex::Regex;@@ -45,6 +45,9 @@ pub struct SqlReferenceAnalysis {pub tables: Vec<SqlTableReference>,pub columns: Vec<SqlColumnReference>,pub scopes: Vec<SqlReferenceScope>,+ /// SELECT projection columns that are neither aggregated nor listed in GROUP BY.+ #[serde(default)]+ pub group_by_violations: Vec<SqlGroupByViolation>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]@@ -64,6 +67,13 @@ pub struct SqlColumnReference {pub scope_id: usize,}+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct SqlGroupByViolation {+ pub span: SqlTextSpan,+ pub column: String,+ pub qualifier: Option<String>,+}+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct SqlReferenceScope {pub id: usize,@@ -89,6 +99,34 @@ impl From<Span> for SqlTextSpan {}}+/// One SELECT projection item: the columns it references plus its output alias.+/// Columns are flagged `aggregated` when they sit inside an aggregate call or a+/// window function, which exempts them from the GROUP BY membership check.+#[derive(Default)]+struct ProjectionItemAcc {+ alias: Option<String>,+ columns: Vec<ProjectionColumnRef>,+}++#[derive(Clone)]+struct ProjectionColumnRef {+ name: String,+ qualifier: Option<String>,+ span: SqlTextSpan,+ aggregated: bool,+}++/// GROUP BY context of the SELECT currently being analyzed. Plain identifier+/// groups are recorded for membership matching; any other shape (ordinals,+/// computed expressions, `GROUP BY ALL`, rollups) marks the whole statement+/// unresolved so the conservative check skips it entirely.+#[derive(Default)]+struct GroupByContext {+ bare: HashSet<String>,+ qualified: HashSet<String>,+ unresolved: bool,+}+#[derive(Default)]struct Analyzer {tables: Vec<SqlTableReference>,@@ -99,15 +137,31 @@ struct Analyzer {next_scope_id: usize,is_sqlserver: bool,is_spark: bool,+ group_by_violations: Vec<SqlGroupByViolation>,+ projection_items: Vec<ProjectionItemAcc>,+ current_projection_item: Option<ProjectionItemAcc>,+ aggregate_depth: usize,+ window_depth: usize,+ group_by: Option<GroupByContext>,}pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlReferenceAnalysis, String> {let normalized_dialect = normalize_dialect(dialect);if normalized_dialect == "duckdb" && starts_with_duckdb_parser_gap_sql(sql) {- return Ok(SqlReferenceAnalysis { tables: vec![], columns: vec![], scopes: vec![] });+ return Ok(SqlReferenceAnalysis {+ tables: vec![],+ columns: vec![],+ scopes: vec![],+ group_by_violations: vec![],+ });}if normalized_dialect == "postgres" && starts_with_postgres_parser_gap_sql(sql) {- return Ok(SqlReferenceAnalysis { tables: vec![], columns: vec![], scopes: vec![] });+ return Ok(SqlReferenceAnalysis {+ tables: vec![],+ columns: vec![],+ scopes: vec![],+ group_by_violations: vec![],+ });}let parser_sql = if normalized_dialect == "clickhouse" {normalize_clickhouse_join_order_for_parser(sql)@@ -135,7 +189,12 @@ pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlRefErr(error)if normalized_dialect == "postgres" && is_postgres_create_procedure_parser_gap(&parser_sql, &error) =>{- return Ok(SqlReferenceAnalysis { tables: vec![], columns: vec![], scopes: vec![] });+ return Ok(SqlReferenceAnalysis {+ tables: vec![],+ columns: vec![],+ scopes: vec![],+ group_by_violations: vec![],+ });}Err(error) => match oracle_admin_ddl_gap_statements(dialect, &parser_sql) {Some(statements) => statements,@@ -152,7 +211,21 @@ pub fn analyze_sql_references(sql: &str, dialect: Option<&str>) -> Result<SqlRefanalyzer.visit_statement(&statement);}- Ok(SqlReferenceAnalysis { tables: analyzer.tables, columns: analyzer.columns, scopes: analyzer.scopes })+ let mut analysis = SqlReferenceAnalysis {+ tables: analyzer.tables,+ columns: analyzer.columns,+ scopes: analyzer.scopes,+ group_by_violations: analyzer.group_by_violations,+ };+ // Engines that do not enforce strict GROUP BY semantics (MySQL, SQLite,+ // DuckDB, ClickHouse, Spark) accept ungrouped projection columns — MySQL+ // enforces ONLY_FULL_GROUP_BY by default since 5.7 but the frontend has no+ // access to the connection's sql_mode, so skipping is the conservative+ // choice — flagging them there would be noise.+ if matches!(normalized_dialect.as_str(), "mysql" | "sqlite" | "duckdb" | "clickhouse" | "spark") {+ analysis.group_by_violations.clear();+ }+ Ok(analysis)}fn parse_sqlserver(sql: &str) -> Result<Vec<Statement>, ParserError> {@@ -1183,15 +1256,24 @@ impl Analyzer {}fn visit_select(&mut self, select: &Select) {+ // Nested selects (subqueries, derived tables, CTE bodies) run their own+ // projection/group-by collection; save the outer context so their columns+ // and violations never mix with this statement's.+ let outer_projection_items = std::mem::take(&mut self.projection_items);+ let outer_projection_item = self.current_projection_item.take();+ let outer_group_by = self.group_by.take();+for table in &select.from {self.visit_table_with_joins(table);}for item in &select.projection {match item {- SelectItem::UnnamedExpr(expr)- | SelectItem::ExprWithAlias { expr, .. }- | SelectItem::ExprWithAliases { expr, .. } => self.visit_expr(expr),+ SelectItem::UnnamedExpr(expr) => self.visit_projection_item(expr, None),+ SelectItem::ExprWithAlias { expr, alias } => {+ self.visit_projection_item(expr, Some(alias.value.clone()))+ }+ SelectItem::ExprWithAliases { expr, .. } => self.visit_projection_item(expr, None),_ => {}}}@@ -1202,11 +1284,31 @@ impl Analyzer {if let Some(expr) = &select.selection {self.visit_expr(expr);}+ let mut group_by_context = GroupByContext::default();if let GroupByExpr::Expressions(exprs, _) = &select.group_by {for expr in exprs {+ match plain_identifier_parts(expr) {+ Some((qualifier, name)) => {+ if let Some(qualifier) = qualifier {+ group_by_context.qualified.insert(format!(+ "{}.{}",+ qualifier.to_ascii_lowercase(),+ name.to_ascii_lowercase()+ ));+ } else {+ group_by_context.bare.insert(name.to_ascii_lowercase());+ }+ }+ None => group_by_context.unresolved = true,+ }self.visit_expr(expr);}+ } else if matches!(select.group_by, GroupByExpr::All(_)) {+ // GROUP BY ALL / rollup-style grouping has no explicit membership to match.+ group_by_context.unresolved = true;}+ self.group_by = Some(group_by_context);+for expr in &select.cluster_by {self.visit_expr(expr);}@@ -1222,6 +1324,97 @@ impl Analyzer {if let Some(expr) = &select.qualify {self.visit_expr(expr);}++ self.finish_group_by_check();+ self.projection_items = outer_projection_items;+ self.current_projection_item = outer_projection_item;+ self.group_by = outer_group_by;+ }++ fn visit_projection_item(&mut self, expr: &Expr, alias: Option<String>) {+ self.current_projection_item = Some(ProjectionItemAcc { alias, ..ProjectionItemAcc::default() });+ self.visit_expr(expr);+ if let Some(item) = self.current_projection_item.take() {+ self.projection_items.push(item);+ }+ }++ /// Flag SELECT projection columns that are neither aggregated nor part of the+ /// statement's GROUP BY. Conservative by design: unresolved GROUP BY shapes+ /// skip the check entirely, alias-based coverage covers `GROUP BY <alias>`.+ fn finish_group_by_check(&mut self) {+ let Some(context) = self.group_by.as_ref() else {+ return;+ };+ if context.unresolved || (context.bare.is_empty() && context.qualified.is_empty()) {+ return;+ }+ // Columns of projection items whose alias is what GROUP BY references+ // (`SELECT a AS x ... GROUP BY x`) count as grouped for identical column+ // names elsewhere in the projection.+ let mut alias_grouped_columns: HashSet<String> = HashSet::new();+ for item in &self.projection_items {+ let Some(alias) = item.alias.as_ref() else { continue };+ if !context.bare.contains(&alias.to_ascii_lowercase()) {+ continue;+ }+ for column in &item.columns {+ alias_grouped_columns.insert(column.name.to_ascii_lowercase());+ }+ }+ // Qualifier asymmetry: `u.name` in the projection and `users.name` in+ // GROUP BY refer to the same table when one side is the other's alias,+ // so normalize every qualifier to the underlying table name before+ // comparing.+ let qualifier_table_names: HashMap<String, String> = self+ .tables+ .iter()+ .filter_map(|table| {+ table.alias.as_ref().map(|alias| (alias.to_ascii_lowercase(), table.name.to_ascii_lowercase()))+ })+ .collect();+ let normalize_qualifier = |qualifier: &str| -> String {+ qualifier_table_names+ .get(&qualifier.to_ascii_lowercase())+ .cloned()+ .unwrap_or_else(|| qualifier.to_ascii_lowercase())+ };+ let grouped_qualified: HashSet<String> = context+ .qualified+ .iter()+ .map(|entry| match entry.split_once('.') {+ Some((qualifier, name)) => format!("{}.{}", normalize_qualifier(qualifier), name),+ None => entry.clone(),+ })+ .collect();+ let mut violations: Vec<SqlGroupByViolation> = Vec::new();+ for item in &self.projection_items {+ let item_alias_grouped =+ item.alias.as_ref().map(|alias| context.bare.contains(&alias.to_ascii_lowercase())).unwrap_or(false);+ for column in &item.columns {+ if column.aggregated || item_alias_grouped {+ continue;+ }+ let name = column.name.to_ascii_lowercase();+ if context.bare.contains(&name) {+ continue;+ }+ if let Some(qualifier) = &column.qualifier {+ if grouped_qualified.contains(&format!("{}.{}", normalize_qualifier(qualifier), name)) {+ continue;+ }+ }+ if alias_grouped_columns.contains(&name) {+ continue;+ }+ violations.push(SqlGroupByViolation {+ span: column.span,+ column: column.name.clone(),+ qualifier: column.qualifier.clone(),+ });+ }+ }+ self.group_by_violations.extend(violations);}fn visit_table_with_joins(&mut self, table: &TableWithJoins) {@@ -1370,6 +1563,18 @@ impl Analyzer {self.visit_expr(r#in);}Expr::Function(function) => {+ // Columns wrapped in an aggregate call or a window function are+ // exempt from the GROUP BY membership check.+ let function_name =+ object_name_last_ident(&function.name).map(|ident| ident.value.to_ascii_lowercase());+ let is_aggregate = function_name.as_deref().map(is_aggregate_function_name).unwrap_or(false);+ let has_window = function.over.is_some();+ if is_aggregate {+ self.aggregate_depth += 1;+ }+ if has_window {+ self.window_depth += 1;+ }self.visit_function_args(&function.parameters);self.visit_function_call_args(&function.name, &function.args);if let Some(filter) = &function.filter {@@ -1378,6 +1583,12 @@ impl Analyzer {for order in &function.within_group {self.visit_expr(&order.expr);}+ if is_aggregate {+ self.aggregate_depth -= 1;+ }+ if has_window {+ self.window_depth -= 1;+ }}Expr::Case { operand, conditions, else_result, .. } => {if let Some(operand) = operand {@@ -1445,17 +1656,85 @@ impl Analyzer {}fn push_column(&mut self, qualifier: Option<String>, ident: &Ident) {+ let span: SqlTextSpan = ident.span.into();if let Some(scope_id) = self.current_scope_id() {self.columns.push(SqlColumnReference {name: ident.value.clone(),- qualifier,- span: ident.span.into(),+ qualifier: qualifier.clone(),+ span,scope_id,});}+ // Projection collection: the accumulator is Some exclusively while a+ // SELECT projection expression is being visited (nested selects swap it+ // in visit_select), so subquery columns never leak into outer items.+ if let Some(item) = self.current_projection_item.as_mut() {+ item.columns.push(ProjectionColumnRef {+ name: ident.value.clone(),+ qualifier,+ span,+ aggregated: self.aggregate_depth > 0 || self.window_depth > 0,+ });+ }}}+/// Split a GROUP BY expression into its `(qualifier, name)` parts when it is a+/// plain (possibly qualified) identifier — the only shape the conservative+/// membership check can match against.+fn plain_identifier_parts(expr: &Expr) -> Option<(Option<String>, String)> {+ match expr {+ Expr::Identifier(ident) => Some((None, ident.value.clone())),+ Expr::CompoundIdentifier(idents) => {+ let column = idents.last()?;+ let qualifier =+ if idents.len() >= 2 { idents.get(idents.len() - 2).map(|ident| ident.value.clone()) } else { None };+ Some((qualifier, column.value.clone()))+ }+ _ => None,+ }+}++fn is_aggregate_function_name(name: &str) -> bool {+ const AGGREGATE_FUNCTION_NAMES: &[&str] = &[+ "sum",+ "count",+ "count_big",+ "avg",+ "min",+ "max",+ "total",+ "group_concat",+ "string_agg",+ "array_agg",+ "listagg",+ "bit_and",+ "bit_or",+ "bit_xor",+ "bool_and",+ "bool_or",+ "every",+ "median",+ "stddev",+ "stddev_pop",+ "stddev_samp",+ "stdev",+ "stdevp",+ "variance",+ "var_pop",+ "var_samp",+ "varp",+ "json_agg",+ "jsonb_agg",+ "json_group_array",+ "xmlagg",+ "wm_concat",+ "approx_count_distinct",+ "checksum_agg",+ ];+ AGGREGATE_FUNCTION_NAMES.contains(&name)+}+fn table_reference_from_name(name: &ObjectName,alias: Option<String>,@@ -1545,3 +1824,101 @@ fn is_sqlserver_datepart_argument(function_name: &str, arg: &FunctionArg) -> boofn is_datepart(value: &str, valid_dateparts: &[&str]) -> bool {valid_dateparts.iter().any(|datepart| value.eq_ignore_ascii_case(datepart))}++#[cfg(test)]+mod group_by_violation_tests {+ use super::*;++ fn violations(sql: &str, dialect: Option<&str>) -> Vec<SqlGroupByViolation> {+ analyze_sql_references(sql, dialect)+ .unwrap_or_else(|error| panic!("parse failed for {sql:?}: {error}"))+ .group_by_violations+ }++ #[test]+ fn flags_ungrouped_projection_column() {+ let result = violations("SELECT USER_ID, SUM(MONEY) FROM users GROUP BY USER_NAME", None);+ assert_eq!(result.len(), 1);+ assert_eq!(result[0].column, "USER_ID");+ assert_eq!(result[0].qualifier, None);+ }++ #[test]+ fn grouped_column_passes() {+ assert!(violations("SELECT USER_ID, SUM(MONEY) FROM users GROUP BY USER_ID", None).is_empty());+ assert!(violations("SELECT dept_name, COUNT(*) FROM emp GROUP BY dept_name", None).is_empty());+ }++ #[test]+ fn aggregate_wrapped_columns_pass() {+ assert!(violations("SELECT COUNT(a) FROM t GROUP BY b", None).is_empty());+ assert!(violations("SELECT a, SUM(b) FROM t GROUP BY a", None).is_empty());+ }++ #[test]+ fn alias_grouped_query_passes() {+ assert!(violations("SELECT a AS x FROM t GROUP BY x", None).is_empty());+ // `SELECT a, a AS b ... GROUP BY b` groups the same source column twice.+ assert!(violations("SELECT a, a AS b FROM t GROUP BY b", None).is_empty());+ }++ #[test]+ fn unresolved_group_by_shapes_skip_the_check() {+ assert!(violations("SELECT a FROM t GROUP BY 1", None).is_empty());+ assert!(violations("SELECT a + b FROM t GROUP BY a + b", None).is_empty());+ }++ #[test]+ fn computed_projection_flags_ungrouped_member() {+ let result = violations("SELECT a + b FROM t GROUP BY a", None);+ assert_eq!(result.len(), 1);+ assert_eq!(result[0].column, "b");+ }++ #[test]+ fn window_function_columns_pass() {+ assert!(violations("SELECT dept, SUM(sal) OVER (PARTITION BY dept) FROM emp GROUP BY dept", None).is_empty());+ }++ #[test]+ fn subquery_columns_do_not_leak() {+ assert!(violations("SELECT (SELECT max(v) FROM t2) AS m, uid FROM t1 GROUP BY uid", None).is_empty());+ }++ #[test]+ fn qualified_references_match_by_qualifier() {+ let result = violations("SELECT u.id, u.name FROM users u GROUP BY u.id", None);+ assert_eq!(result.len(), 1);+ assert_eq!(result[0].column, "name");+ assert_eq!(result[0].qualifier.as_deref(), Some("u"));+ assert!(violations("SELECT u.id, u.name FROM users u GROUP BY u.id, u.name", None).is_empty());+ }++ #[test]+ fn qualifier_asymmetry_resolves_alias_and_table_name() {+ // Projection qualifier (alias) vs GROUP BY qualifier (table name) refers+ // to the same table, so the grouped column itself must pass...+ assert!(violations("SELECT u.id FROM users u GROUP BY users.id", None).is_empty());+ // ...and the reverse direction.+ assert!(violations("SELECT users.id FROM users u GROUP BY u.id", None).is_empty());+ // Columns that are genuinely ungrouped stay flagged under either spelling.+ let result = violations("SELECT u.id, u.name FROM users u GROUP BY users.id", None);+ assert_eq!(result.len(), 1);+ assert_eq!(result[0].column, "name");+ }++ #[test]+ fn missing_group_by_passes() {+ assert!(violations("SELECT a, SUM(b) FROM t", None).is_empty());+ }++ #[test]+ fn lenient_engines_skip_the_check() {+ for dialect in ["mysql", "sqlite", "duckdb", "clickhouse", "spark"] {+ assert!(+ violations("SELECT USER_ID, SUM(MONEY) FROM users GROUP BY USER_NAME", Some(dialect)).is_empty(),+ "dialect {dialect}"+ );+ }+ }+}