Skip to content

Commit ef800f7

Browse files
Merge pull request taozhi8833998#2136 from taozhi8833998/fix-distinct-on-jsonb-pg
fix: distinct on jsonb expr in pg
2 parents c48216e + 7e9fc48 commit ef800f7

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

pegjs/postgresql.pegjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,8 +3146,8 @@ cte_column_definition
31463146
}
31473147

31483148
distinct_on
3149-
= d:KW_DISTINCT __ o:KW_ON __ LPAREN __ c:column_ref_list __ RPAREN {
3150-
// => {type: string; columns: column_ref_list;}
3149+
= d:KW_DISTINCT __ o:KW_ON __ LPAREN __ c:column_list_items __ RPAREN {
3150+
// => {type: string; columns: column_list_items;}
31513151
console.lo
31523152
return {
31533153
type: `${d} ON`,
@@ -3238,6 +3238,11 @@ query_option
32383238
return option;
32393239
}
32403240

3241+
column_list_items
3242+
= head:column_list_item tail:(__ COMMA __ column_list_item)* {
3243+
// => column_list_item[]
3244+
return createList(head, tail);
3245+
}
32413246
column_clause
32423247
= head: (KW_ALL / (STAR !ident_start) / STAR) tail:(__ COMMA __ column_list_item)* {
32433248
// => 'ALL' | '*' | column_list_item[]
@@ -3253,10 +3258,7 @@ column_clause
32533258
if (tail && tail.length > 0) return createList(item, tail)
32543259
return [item]
32553260
}
3256-
/ head:column_list_item tail:(__ COMMA __ column_list_item)* {
3257-
// => column_list_item[]
3258-
return createList(head, tail);
3259-
}
3261+
/ column_list_items
32603262

32613263
array_index
32623264
= LBRAKE __ n:(literal_numeric / literal_string) __ RBRAKE {

src/expr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function exprToSQL(exprOrigin) {
7171
}
7272
}
7373
const { type } = expr
74+
if (type === 'expr') return exprToSQL(expr.expr)
7475
return exprToSQLConvertFn[type] ? exprToSQLConvertFn[type](expr) : literalToSQL(expr)
7576
}
7677

src/select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exprToSQL, getExprListSQL, orderOrPartitionByToSQL, varToSQL } from './expr'
2-
import { columnRefToSQL, columnsToSQL } from './column'
2+
import { columnsToSQL } from './column'
33
import { limitToSQL } from './limit'
44
import { withToSQL } from './with'
55
import { tablesToSQL } from './tables'
@@ -11,7 +11,7 @@ function distinctToSQL(distinct) {
1111
if (typeof distinct === 'string') return distinct
1212
const { type, columns } = distinct
1313
const result = [toUpper(type)]
14-
if (columns) result.push(`(${columns.map(columnRefToSQL).join(', ')})`)
14+
if (columns) result.push(`(${columns.map(exprToSQL).join(', ')})`)
1515
return result.filter(hasVal).join(' ')
1616
}
1717

test/postgres.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,8 @@ describe('Postgres', () => {
521521
{
522522
title: 'distinct on',
523523
sql: [
524-
'SELECT DISTINCT ON (a, b) a, b, c FROM tbl',
525-
'SELECT DISTINCT ON (a, b) a, b, c FROM "tbl"'
524+
"SELECT DISTINCT ON (a->>'someJsonAttribute', b, c) a->>'someJsonAttribute', b, c FROM tbl",
525+
`SELECT DISTINCT ON (a ->> 'someJsonAttribute', b, c) a ->> 'someJsonAttribute', b, c FROM "tbl"`
526526
]
527527
},
528528
{

0 commit comments

Comments
 (0)