Skip to content

Commit 93df74a

Browse files
committed
fix: sqlify to origin table name in bigquery
1 parent f28e60d commit 93df74a

5 files changed

Lines changed: 42 additions & 19 deletions

File tree

pegjs/bigquery.pegjs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@
195195
columnList.clear()
196196
columns.forEach(col => columnList.add(col))
197197
}
198+
199+
function getSurroundFromLiteralType(literal) {
200+
switch (literal.type) {
201+
case 'double_quote_string':
202+
return '"'
203+
case 'single_quote_string':
204+
return "'"
205+
case 'backticks_quote_string':
206+
return '`'
207+
default:
208+
return ''
209+
}
210+
}
198211

199212
const cmpPrefixMap = {
200213
'+': true,
@@ -1926,21 +1939,23 @@ join_op
19261939
/ k:KW_INNER? __ KW_JOIN { return k ? `${k[0].toUpperCase()} JOIN` : 'JOIN'; }
19271940

19281941
table_name
1929-
= db:ident_without_kw schema:(__ DOT __ ident_without_kw) tail:(__ DOT __ ident_without_kw) {
1930-
const obj = { db: null, table: db };
1942+
= db:ident_without_kw_type schema:(__ DOT __ ident_without_kw_type) tail:(__ DOT __ ident_without_kw_type) {
1943+
const obj = { db: null, table: db.value };
19311944
if (tail !== null) {
1932-
obj.db = db;
1933-
obj.catalog = db;
1934-
obj.schema = schema[3];
1935-
obj.table = tail[3];
1945+
obj.db = db.value;
1946+
obj.catalog = db.value;
1947+
obj.schema = schema[3].value;
1948+
obj.table = tail[3].value;
1949+
obj.surround = { table: getSurroundFromLiteralType(tail[3]), db: getSurroundFromLiteralType(db), schema: getSurroundFromLiteralType(schema[3]) };
19361950
}
19371951
return obj;
19381952
}
1939-
/ dt:ident_without_kw tail:(__ DOT __ ident_without_kw)? {
1940-
const obj = { db: null, table: dt };
1953+
/ dt:ident_without_kw_type tail:(__ DOT __ ident_without_kw_type)? {
1954+
const obj = { db: null, table: dt.value, surround: { table: getSurroundFromLiteralType(dt) } };
19411955
if (tail !== null) {
1942-
obj.db = dt;
1943-
obj.table = tail[3];
1956+
obj.db = dt.value;
1957+
obj.table = tail[3].value;
1958+
obj.surround = { table: getSurroundFromLiteralType(tail[3]), db: getSurroundFromLiteralType(dt) };
19441959
}
19451960
return obj;
19461961
}

src/insert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function partitionToSQL(partition) {
1919
if (!partition) return ''
2020
const partitionArr = ['PARTITION', '(']
2121
if (Array.isArray(partition)) {
22-
partitionArr.push(partition.map(identifierToSql).join(', '))
22+
partitionArr.push(partition.map(partitionItem => identifierToSql(partitionItem)).join(', '))
2323
} else {
2424
const { value } = partition
2525
partitionArr.push(value.map(exprToSQL).join(', '))

src/tables.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function tableHintToSQL(tableHintExpr) {
5656
result.push(toUpper(keyword), '=', exprToSQL(expr))
5757
break
5858
case 'index':
59-
result.push(toUpper(prefix), toUpper(keyword), parentheses ? `(${expr.map(identifierToSql).join(', ')})` : `= ${identifierToSql(expr)}`)
59+
result.push(toUpper(prefix), toUpper(keyword), parentheses ? `(${expr.map(indexItem => identifierToSql(indexItem)).join(', ')})` : `= ${identifierToSql(expr)}`)
6060
break
6161
default:
6262
result.push(exprToSQL(expr))
@@ -114,11 +114,11 @@ function generateVirtualTable(stmt) {
114114

115115
function tableToSQL(tableInfo) {
116116
if (toUpper(tableInfo.type) === 'UNNEST') return unnestToSQL(tableInfo)
117-
const { table, db, as, expr, operator, prefix: prefixStr, schema, server, suffix, tablesample, temporal_table, table_hint } = tableInfo
118-
const serverName = identifierToSql(server)
119-
const database = identifierToSql(db)
120-
const schemaStr = identifierToSql(schema)
121-
let tableName = table && identifierToSql(table)
117+
const { table, db, as, expr, operator, prefix: prefixStr, schema, server, suffix, tablesample, temporal_table, table_hint, surround = {} } = tableInfo
118+
const serverName = identifierToSql(server, false, surround.server)
119+
const database = identifierToSql(db, false, surround.db)
120+
const schemaStr = identifierToSql(schema, false, surround.schema)
121+
let tableName = table && identifierToSql(table, false, surround.table)
122122
if (expr) {
123123
const exprType = expr.type
124124
switch (exprType) {

src/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ function columnIdentifierToSql(ident) {
151151
}
152152
}
153153

154-
function identifierToSql(ident, isDual) {
155-
const { database } = getParserOpt()
154+
function identifierToSql(ident, isDual, surround) {
156155
if (isDual === true) return `'${ident}'`
157156
if (!ident) return
158157
if (ident === '*') return ident
158+
if (surround) return `${surround}${ident}${surround}`
159+
const { database } = getParserOpt()
159160
switch (database && database.toLowerCase()) {
160161
case 'mysql':
161162
case 'mariadb':

test/bigquery.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,13 @@ describe('BigQuery', () => {
891891
"SELECT SAFE_CAST(SPLIT(t1.CREATED_BY, 'a')[0] AS STRING) AS FIRST_SPLIT_ARG FROM table1 AS t1 WHERE CREATED_BY = 'node-sql-parser' AND FIRST_SPLIT_ARG = @FIRST_SPLIT_ARG LIMIT 1"
892892
]
893893
},
894+
{
895+
title: 'quoted table name',
896+
sql: [
897+
'SELECT COUNT(*) FROM `bigquery-public-data.blah.events_*`',
898+
'SELECT COUNT(*) FROM `bigquery-public-data.blah.events_*`'
899+
]
900+
}
894901
]
895902

896903
SQL_LIST.forEach(sqlInfo => {

0 commit comments

Comments
 (0)