Skip to content

Commit f6cbbff

Browse files
authored
fix: escape BigQuery reserved keywords (#5166) (#5173)
1 parent ebfd6c6 commit f6cbbff

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

prqlc/prqlc/src/sql/keywords.rs

+102
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn sql_keywords() -> &'static HashSet<&'static str> {
2323
m.extend(SQLITE_KEYWORDS);
2424
m.extend(POSTGRES_KEYWORDS);
2525
m.extend(DUCKDB_KEYWORDS);
26+
m.extend(BIGQUERY_KEYWORDS);
2627

2728
let reverse_index: HashMap<&Keyword, usize> = ALL_KEYWORDS_INDEX
2829
.iter()
@@ -390,6 +391,107 @@ const DUCKDB_KEYWORDS: &[&str] = &[
390391
"WITH",
391392
];
392393

394+
// source reserved keywords GoogleSQL
395+
// <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#reserved_keywords>
396+
const BIGQUERY_KEYWORDS: &[&str] = &[
397+
"ALL",
398+
"AND",
399+
"ANY",
400+
"ARRAY",
401+
"AS",
402+
"ASC",
403+
"ASSERT_ROWS_MODIFIED",
404+
"AT",
405+
"BETWEEN",
406+
"BY",
407+
"CASE",
408+
"CAST",
409+
"COLLATE",
410+
"CONTAINS",
411+
"CREATE",
412+
"CROSS",
413+
"CUBE",
414+
"CURRENT",
415+
"DEFAULT",
416+
"DEFINE",
417+
"DESC",
418+
"DISTINCT",
419+
"ELSE",
420+
"END",
421+
"ENUM",
422+
"ESCAPE",
423+
"EXCEPT",
424+
"EXCLUDE",
425+
"EXISTS",
426+
"EXTRACT",
427+
"FALSE",
428+
"FETCH",
429+
"FOLLOWING",
430+
"FOR",
431+
"FROM",
432+
"FULL",
433+
"GROUP",
434+
"GROUPING",
435+
"GROUPS",
436+
"HASH",
437+
"HAVING",
438+
"IF",
439+
"IGNORE",
440+
"IN",
441+
"INNER",
442+
"INTERSECT",
443+
"INTERVAL",
444+
"INTO",
445+
"IS",
446+
"JOIN",
447+
"LATERAL",
448+
"LEFT",
449+
"LIKE",
450+
"LIMIT",
451+
"LOOKUP",
452+
"MERGE",
453+
"NATURAL",
454+
"NEW",
455+
"NO",
456+
"NOT",
457+
"NULL",
458+
"NULLS",
459+
"OF",
460+
"ON",
461+
"OR",
462+
"ORDER",
463+
"OUTER",
464+
"OVER",
465+
"PARTITION",
466+
"PRECEDING",
467+
"PROTO",
468+
"QUALIFY",
469+
"RANGE",
470+
"RECURSIVE",
471+
"RESPECT",
472+
"RIGHT",
473+
"ROLLUP",
474+
"ROWS",
475+
"SELECT",
476+
"SET",
477+
"SOME",
478+
"STRUCT",
479+
"TABLESAMPLE",
480+
"THEN",
481+
"TO",
482+
"TREAT",
483+
"TRUE",
484+
"UNBOUNDED",
485+
"UNION",
486+
"UNNEST",
487+
"USING",
488+
"WHEN",
489+
"WHERE",
490+
"WINDOW",
491+
"WITH",
492+
"WITHIN",
493+
];
494+
393495
#[test]
394496
fn test_sql_keywords() {
395497
assert!(is_keyword("from"));

prqlc/prqlc/tests/integration/sql.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,23 @@ fn test_quoting_05() {
10921092
"#);
10931093
}
10941094

1095+
#[test]
1096+
fn test_quoting_06() {
1097+
let prql = "
1098+
prql target:sql.bigquery
1099+
1100+
from `some_dataset.demo`
1101+
select {`hash`}
1102+
";
1103+
1104+
assert_snapshot!(compile(prql).unwrap(), @r#"
1105+
SELECT
1106+
`hash`
1107+
FROM
1108+
`some_dataset.demo`
1109+
"#);
1110+
}
1111+
10951112
#[test]
10961113
fn test_sorts_01() {
10971114
assert_snapshot!((compile(r###"

0 commit comments

Comments
 (0)