Skip to content

Commit 75dcf8f

Browse files
Merge pull request #1711 from taozhi8833998/fix-except-bigquery
fix: table prefix star with except in bigquery
2 parents b0e5426 + 997e5ac commit 75dcf8f

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

pegjs/bigquery.pegjs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,29 +1641,7 @@ expr_alias
16411641
}
16421642

16431643
column_clause
1644-
= STAR __ k:('EXCEPT'i / 'REPLACE'i) __ LPAREN __ c:columns_list __ RPAREN {
1645-
columnList.add('select::null::(.*)')
1646-
return {
1647-
expr_list: c,
1648-
parentheses: true,
1649-
star: '*',
1650-
type: k.toLowerCase(),
1651-
}
1652-
}
1653-
/ head: (KW_ALL / (STAR !ident_start) / STAR) tail:(__ COMMA __ column_list_item)* __ COMMA? {
1654-
columnList.add('select::null::(.*)')
1655-
const item = {
1656-
expr: {
1657-
type: 'column_ref',
1658-
table: null,
1659-
column: '*'
1660-
},
1661-
as: null
1662-
}
1663-
if (tail && tail.length > 0) return createList(item, tail)
1664-
return [item]
1665-
}
1666-
/ c:columns_list __ COMMA? {
1644+
= c:columns_list __ COMMA? {
16671645
return c
16681646
}
16691647

@@ -1687,17 +1665,32 @@ column_offset_expr
16871665
}
16881666

16891667
column_list_item
1690-
= tbl:STAR {
1691-
columnList.add('select::null::(.*)');
1692-
return {
1668+
= p:(column_without_kw __ DOT)? STAR __ k:('EXCEPT'i / 'REPLACE'i) __ LPAREN __ c:columns_list __ RPAREN {
1669+
const tbl = p && p[0]
1670+
columnList.add(`select::${tbl}::(.*)`)
1671+
return {
1672+
expr_list: c,
1673+
parentheses: true,
1674+
expr: {
1675+
type: 'column_ref',
1676+
table: tbl,
1677+
column: '*'
1678+
},
1679+
type: k.toLowerCase(),
1680+
}
1681+
}
1682+
/ head: (KW_ALL / (STAR !ident_start) / STAR) {
1683+
columnList.add('select::null::(.*)')
1684+
const item = {
16931685
expr: {
16941686
type: 'column_ref',
16951687
table: null,
16961688
column: '*'
16971689
},
16981690
as: null
1699-
};
1700-
}
1691+
}
1692+
return item
1693+
}
17011694
/ tbl:column_without_kw __ DOT pro:((column_offset_expr / column_without_kw) __ DOT)? __ STAR {
17021695
columnList.add(`select::${tbl}::(.*)`)
17031696
let column = '*'

src/column.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ function columnToSQL(column, isDual) {
148148
if (type === 'cast') return castToSQL(column)
149149
if (isDual) expr.isDual = isDual
150150
let str = exprToSQL(expr)
151+
const { expr_list: exprList } = column
152+
if (exprList) {
153+
const result = [str]
154+
const columnsStr = exprList.map(col => columnToSQL(col, isDual)).join(', ')
155+
result.push([toUpper(type), type && '(', columnsStr, type && ')'].filter(hasVal).join(''))
156+
return result.filter(hasVal).join(' ')
157+
}
151158
if (expr.parentheses && Reflect.has(expr, 'array_index')) str = `(${str})`
152159
if (expr.array_index && expr.type !== 'column_ref') str = `${str}[${literalToSQL(expr.array_index.index)}]`
153160
return [str, asToSQL(column.as)].filter(hasVal).join(' ')
@@ -167,13 +174,7 @@ function getDual(tables) {
167174
function columnsToSQL(columns, tables) {
168175
if (!columns || columns === '*') return columns
169176
const isDual = getDual(tables)
170-
const result = []
171-
const { expr_list: exprList, star, type } = columns
172-
result.push(star, toUpper(type))
173-
const exprListArr = exprList || columns
174-
const columnsStr = exprListArr.map(col => columnToSQL(col, isDual)).join(', ')
175-
result.push([type && '(', columnsStr, type && ')'].filter(hasVal).join(''))
176-
return result.filter(hasVal).join(' ')
177+
return columns.map(col => columnToSQL(col, isDual)).join(', ')
177178
}
178179

179180
export {

test/bigquery.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ describe('BigQuery', () => {
8888
]
8989
},
9090
{
91-
title: 'select * expect',
91+
title: 'select * except',
9292
sql: [
9393
`WITH orders AS
9494
(SELECT 5 as order_id,
9595
"sprocket" as item_name,
9696
200 as quantity)
97-
SELECT * EXCEPT (order_id)
97+
SELECT * EXCEPT (order_id), orders.* EXCEPT(order_time)
9898
FROM orders;`,
99-
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * EXCEPT (order_id) FROM orders"
99+
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * EXCEPT(order_id), orders.* EXCEPT(order_time) FROM orders"
100100
]
101101
},
102102
{
@@ -108,7 +108,7 @@ describe('BigQuery', () => {
108108
200 as quantity)
109109
SELECT * REPLACE ("widget" AS item_name)
110110
FROM orders;`,
111-
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * REPLACE ('widget' AS item_name) FROM orders"
111+
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * REPLACE('widget' AS item_name) FROM orders"
112112
]
113113
},
114114
{
@@ -120,7 +120,7 @@ describe('BigQuery', () => {
120120
200 as quantity)
121121
SELECT * REPLACE (quantity/2 AS quantity)
122122
FROM orders;`,
123-
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * REPLACE (quantity / 2 AS quantity) FROM orders"
123+
"WITH orders AS (SELECT 5 AS order_id, 'sprocket' AS item_name, 200 AS quantity) SELECT * REPLACE(quantity / 2 AS quantity) FROM orders"
124124
]
125125
},
126126
{

0 commit comments

Comments
 (0)