Skip to content

Commit 441253b

Browse files
rebeliceclaude
andcommitted
fix(parser): handle DISTINCT clause in SELECT statements
The grammar rule for SELECT with distinct_clause parsed the DISTINCT keyword but never assigned the result to SelectStmt.DistinctClause, causing all DISTINCT information to be silently lost. Translate the PostgreSQL grammar faithfully: - distinct_clause DISTINCT returns list_make1(NIL) equivalent - distinct_clause DISTINCT ON returns the expression list - Assign $2 to DistinctClause in the SELECT action Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1ceee52 commit 441253b

File tree

4 files changed

+7612
-7480
lines changed

4 files changed

+7612
-7480
lines changed

parser/gram.y

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ import (
147147
%type <ival> opt_asc_desc
148148
%type <ival> Iconst
149149
%type <str> Sconst
150-
%type <boolean> opt_all_clause opt_distinct_clause set_quantifier
150+
%type <boolean> opt_all_clause set_quantifier
151+
%type <list> distinct_clause opt_distinct_clause
151152
%type <node> with_clause opt_with_clause common_table_expr
152153
%type <ival> opt_materialized
153154
%type <list> cte_list
@@ -7725,7 +7726,8 @@ simple_select:
77257726
| SELECT distinct_clause target_list into_clause from_clause where_clause group_clause having_clause window_clause
77267727
{
77277728
n := &nodes.SelectStmt{
7728-
TargetList: $3,
7729+
DistinctClause: $2,
7730+
TargetList: $3,
77297731
}
77307732
if $4 != nil {
77317733
n.IntoClause = $4.(*nodes.IntoClause)
@@ -7745,7 +7747,6 @@ simple_select:
77457747
if $9 != nil {
77467748
n.WindowClause = $9
77477749
}
7748-
// TODO: handle DISTINCT clause
77497750
$$ = n
77507751
}
77517752
| select_clause UNION set_quantifier select_clause
@@ -7803,13 +7804,22 @@ opt_all_clause:
78037804
;
78047805

78057806
opt_distinct_clause:
7806-
distinct_clause { $$ = true }
7807-
| /* EMPTY */ { $$ = false }
7807+
distinct_clause { $$ = $1 }
7808+
| opt_all_clause { $$ = nil }
78087809
;
78097810

78107811
distinct_clause:
78117812
DISTINCT
7813+
{
7814+
/* We use (NIL) as a placeholder to indicate that all target expressions
7815+
* should be placed in the DISTINCT list during parsetree analysis.
7816+
*/
7817+
$$ = &nodes.List{Items: []nodes.Node{nil}}
7818+
}
78127819
| DISTINCT ON '(' expr_list ')'
7820+
{
7821+
$$ = $4
7822+
}
78137823
;
78147824

78157825
set_quantifier:

0 commit comments

Comments
 (0)