cloudspannerecosystem / memefish

memefish is the foundation to analyze Spanner SQL
https://cloudspannerecosystem.dev/memefish/
MIT License
71 stars 19 forks source link

Need to maintain keyword list #116

Open apstndb opened 2 weeks ago

apstndb commented 2 weeks ago

I think the most important characteristic of "parse-unparse" is that it never changes the semantics of the query, but "parse-unparse" can break the semantics of the query.

(*ast.Ident).SQL() quote identifiers when the identifier is the keyword using QuoteSQLIdent.

$ go run ./tools/parse --mode query 'SELECT 1 AS `SELECT`'
...
--- SQL
SELECT 1 AS `SELECT`

There are bad cases.

$ go run ./tools/parse --mode query 'SELECT 1 AS `IF`'
...
--- SQL
SELECT 1 AS IF

It is not a valid query.

spanner> SELECT 1 AS IF;
ERROR: spanner: code = "InvalidArgument", desc = "Syntax error: Unexpected keyword IF [at 1:13]\\nSELECT 1 AS IF\\n            ^"

So, memefish must maintain token.Keywords in keywords.go to match with the official reserved keyword list.

It seems that the current diff is GRAPH_TABLE and IF.

--- keywords-in-go.txt  2024-09-28 00:12:47
+++ keywords.txt        2024-09-28 00:01:00
@@ -34,11 +34,13 @@
 FOR
 FROM
 FULL
+GRAPH_TABLE
 GROUP
 GROUPING
 GROUPS
 HASH
 HAVING
+IF
 IGNORE
 IN
 INNER
apstndb commented 2 weeks ago

I think it it the reason to haveIfExpr. Related: https://github.com/cloudspannerecosystem/memefish/pull/47