Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -840,17 +840,16 @@ if_not_exists_stmt

create_table_stmt
= a:KW_CREATE __
tp:KW_TEMPORARY? __
or:(KW_OR __ KW_REPLACE)? __
tp:(KW_TEMP / KW_TEMPORARY)? __
KW_TABLE __
ife:if_not_exists_stmt? __
t:table_ref_list __
c:create_table_definition __
con:(create_constraint_definition)* __
t:table_name __
c:create_table_definition? __
to:table_options? __
ir: (KW_IGNORE / KW_REPLACE)? __
as: KW_AS? __
qe: union_stmt? {
if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`));
as:KW_AS? __
qe:union_stmt? {
if(t) tableList.add(`create::${t.db}::${t.table}`)
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
Expand All @@ -859,12 +858,11 @@ create_table_stmt
keyword: 'table',
temporary: tp && tp[0].toLowerCase(),
if_not_exists:ife,
table: t,
ignore_replace: ir && ir[0].toLowerCase(),
table: [t],
or_replace: or && 'or replace',
as: as && as[0].toLowerCase(),
query_expr: qe && qe.ast,
create_definitions: c,
constraint : con,
table_options: to
}
}
Expand Down Expand Up @@ -1194,6 +1192,18 @@ reference_definition
}
}

table_option_list_item
= k:('expiration_timestamp'i / 'partition_expiration_days'i / 'require_partition_filter'i / 'kms_key_name'i / 'friendly_name'i / 'description'i / 'labels'i / 'default_rounding_mode'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:expr {
return {
keyword: k,
symbol: '=',
value: v
}
}
table_option_list
= head:table_option_list_item tail:(__ COMMA __ table_option_list_item)* {
return createList(head, tail);
}
table_option
= kw:('AUTO_INCREMENT'i / 'AVG_ROW_LENGTH'i / 'KEY_BLOCK_SIZE'i / 'MAX_ROWS'i / 'MIN_ROWS'i / 'STATS_SAMPLE_PAGES'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:literal_numeric {
return {
Expand Down Expand Up @@ -1224,6 +1234,25 @@ table_option
value: c.toUpperCase()
}
}
/ KW_PARTITION __ KW_BY __ v:expr {
return {
keyword: 'partition by',
value: v
}
}
/ 'CLUSTER'i __ 'BY'i __ c:column_list {
return {
keyword: 'cluster by',
value: c
}
}
/ 'OPTIONS'i __ LPAREN __ v:table_option_list __ RPAREN {
return {
keyword: 'options',
parentheses: true,
value: v
}
}

create_like_table_simple
= KW_LIKE __ t: table_ref_list {
Expand All @@ -1234,7 +1263,7 @@ create_like_table_simple
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:literal_string {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down Expand Up @@ -1480,6 +1509,7 @@ KW_VAR__PRE_AT_AT = '@@'
KW_VAR_PRE_DOLLAR = '$'
KW_VAR_PRE = KW_VAR__PRE_AT_AT / KW_VAR__PRE_AT / KW_VAR_PRE_DOLLAR
KW_TEMPORARY = "TEMPORARY"i !ident_start
KW_TEMP = "TEMP"i !ident_start
KW_SCHEMA = "SCHEMA"i !ident_start
KW_ALTER = "ALTER"i !ident_start
KW_SPATIAL = "SPATIAL"i !ident_start
Expand Down
3 changes: 2 additions & 1 deletion src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ function createTableToSQL(stmt) {
create_definitions: createDefinition,
table_options: tableOptions,
ignore_replace: ignoreReplace,
or_replace: orReplace,
query_expr: queryExpr,
} = stmt
const sql = [toUpper(type), toUpper(temporary), toUpper(keyword), toUpper(ifNotExists), tablesToSQL(table)]
const sql = [toUpper(type), toUpper(orReplace), toUpper(temporary), toUpper(keyword), toUpper(ifNotExists), tablesToSQL(table)]
if (like) {
const { type: likeType, table: likeTable } = like
const likeTableName = tablesToSQL(likeTable)
Expand Down
15 changes: 14 additions & 1 deletion src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,20 @@ function tableOptionToSQL(tableOption) {
const { keyword, symbol, value } = tableOption
const sql = [keyword.toUpperCase()]
if (symbol) sql.push(symbol)
sql.push(value)
let val = value
switch (keyword) {
case 'partition by':
case 'default collate':
val = exprToSQL(value)
break
case 'options':
val = `(${value.map(tableOptionItem => [tableOptionItem.keyword, tableOptionItem.symbol, exprToSQL(tableOptionItem.value)].join(' ')).join(', ')})`
break
case 'cluster by':
val = value.map(exprToSQL).join(', ')
break
}
sql.push(val)
return sql.join(' ')
}

Expand Down
23 changes: 23 additions & 0 deletions test/bigquery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,29 @@ describe('BigQuery', () => {
'CREATE TABLE mydataset.newtable (x INT64)'
]
},
{
title: 'create table with multiple options',
sql: [
`CREATE OR REPLACE TEMP TABLE
table1
DEFAULT COLLATE 'und:ci'
PARTITION BY
DATE(event_time)
CLUSTER BY
id
OPTIONS (
require_partition_filter = TRUE
)
AS
SELECT
table2.id,
table2.value,
table2.event_time
FROM
table2;`,
"CREATE OR REPLACE TEMP TABLE table1 DEFAULT COLLATE 'und:ci' PARTITION BY DATE(event_time) CLUSTER BY id OPTIONS (require_partition_filter = TRUE) AS SELECT table2.id, table2.value, table2.event_time FROM table2"
]
},
]

SQL_LIST.forEach(sqlInfo => {
Expand Down