From 2e97351d81cef8a379943abf69fc95938aa4d743 Mon Sep 17 00:00:00 2001 From: Sharad Gaur Date: Thu, 12 Nov 2026 18:19:04 -0500 Subject: [PATCH] Parse a tuple element index after a qualified column t.col.1 reads element 1 of the tuple column col of table t. The path parser consumed the dot and then expected another name, so a qualified column could not be followed by an element index. An unqualified col.1 already worked. --- parser/parser_table.go | 4 +- parser/parser_test.go | 2 + ...lect_tuple_element_of_qualified_column.sql | 14 ++ ...lect_tuple_element_of_qualified_column.sql | 6 + ...lement_of_qualified_column.sql.golden.json | 214 ++++++++++++++++++ ...lect_tuple_element_of_qualified_column.sql | 1 + 6 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 parser/testdata/query/format/beautify/select_tuple_element_of_qualified_column.sql create mode 100644 parser/testdata/query/format/select_tuple_element_of_qualified_column.sql create mode 100644 parser/testdata/query/output/select_tuple_element_of_qualified_column.sql.golden.json create mode 100644 parser/testdata/query/select_tuple_element_of_qualified_column.sql diff --git a/parser/parser_table.go b/parser/parser_table.go index 1da62a2..75196df 100644 --- a/parser/parser_table.go +++ b/parser/parser_table.go @@ -504,7 +504,9 @@ func (p *Parser) parseIdentOrFunction(_ Pos) (Expr, error) { return nil, err } fields = append(fields, child) - if !p.matchTokenKind(TokenKindDot) || p.peekTokenKind(TokenKindColon) { + // A number after the dot is a tuple element index, as in + // t.col.1, which the caller reads as an index operation. + if !p.matchTokenKind(TokenKindDot) || p.peekTokenKind(TokenKindColon) || p.peekTokenKind(TokenKindInt) { break } _ = p.lexer.consumeToken() diff --git a/parser/parser_test.go b/parser/parser_test.go index a69f480..2adb9fe 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -290,6 +290,8 @@ func TestParser_InvalidSyntax(t *testing.T) { "(SELECT 1) SETTINGS max_threads=1 UNION ALL SELECT 2", // AS at the very end of an ORDER BY item leaves no token to check for SELECT "SELECT * FROM t ORDER BY a AS", + // a tuple element index ends a qualified column + "SELECT t.a.1.:Int64 FROM t", } for _, sql := range invalidSQLs { parser := NewParser(sql) diff --git a/parser/testdata/query/format/beautify/select_tuple_element_of_qualified_column.sql b/parser/testdata/query/format/beautify/select_tuple_element_of_qualified_column.sql new file mode 100644 index 0000000..483fc17 --- /dev/null +++ b/parser/testdata/query/format/beautify/select_tuple_element_of_qualified_column.sql @@ -0,0 +1,14 @@ +-- Origin SQL: +SELECT t.a.1, db.t.a.2 + 1 FROM db.t AS t WHERE t.a.1 = 1 ORDER BY t.a.2 + + +-- Beautify SQL: +SELECT + t.a.1, + db.t.a.2 + 1 +FROM + db.t AS t +WHERE + t.a.1 = 1 +ORDER BY + t.a.2; diff --git a/parser/testdata/query/format/select_tuple_element_of_qualified_column.sql b/parser/testdata/query/format/select_tuple_element_of_qualified_column.sql new file mode 100644 index 0000000..3252399 --- /dev/null +++ b/parser/testdata/query/format/select_tuple_element_of_qualified_column.sql @@ -0,0 +1,6 @@ +-- Origin SQL: +SELECT t.a.1, db.t.a.2 + 1 FROM db.t AS t WHERE t.a.1 = 1 ORDER BY t.a.2 + + +-- Format SQL: +SELECT t.a.1, db.t.a.2 + 1 FROM db.t AS t WHERE t.a.1 = 1 ORDER BY t.a.2; diff --git a/parser/testdata/query/output/select_tuple_element_of_qualified_column.sql.golden.json b/parser/testdata/query/output/select_tuple_element_of_qualified_column.sql.golden.json new file mode 100644 index 0000000..8a01e8b --- /dev/null +++ b/parser/testdata/query/output/select_tuple_element_of_qualified_column.sql.golden.json @@ -0,0 +1,214 @@ +[ + { + "SelectPos": 0, + "StatementEnd": 72, + "With": null, + "Top": null, + "HasDistinct": false, + "DistinctOn": null, + "SelectItems": [ + { + "Expr": { + "Object": { + "Fields": [ + { + "Name": "t", + "QuoteType": 1, + "NamePos": 7, + "NameEnd": 8 + }, + { + "Name": "a", + "QuoteType": 1, + "NamePos": 9, + "NameEnd": 10 + } + ] + }, + "Operation": ".", + "Index": { + "NumPos": 11, + "NumEnd": 12, + "Literal": "1", + "Base": 10 + } + }, + "Modifiers": [], + "Alias": null + }, + { + "Expr": { + "LeftExpr": { + "Object": { + "Fields": [ + { + "Name": "db", + "QuoteType": 1, + "NamePos": 14, + "NameEnd": 16 + }, + { + "Name": "t", + "QuoteType": 1, + "NamePos": 17, + "NameEnd": 18 + }, + { + "Name": "a", + "QuoteType": 1, + "NamePos": 19, + "NameEnd": 20 + } + ] + }, + "Operation": ".", + "Index": { + "NumPos": 21, + "NumEnd": 22, + "Literal": "2", + "Base": 10 + } + }, + "Operation": "+", + "RightExpr": { + "NumPos": 25, + "NumEnd": 26, + "Literal": "1", + "Base": 10 + }, + "HasGlobal": false, + "HasNot": false + }, + "Modifiers": [], + "Alias": null + } + ], + "From": { + "FromPos": 27, + "Expr": { + "Table": { + "TablePos": 32, + "TableEnd": 41, + "Alias": null, + "Expr": { + "Expr": { + "Database": { + "Name": "db", + "QuoteType": 1, + "NamePos": 32, + "NameEnd": 34 + }, + "Table": { + "Name": "t", + "QuoteType": 1, + "NamePos": 35, + "NameEnd": 36 + } + }, + "AliasPos": 40, + "Alias": { + "Name": "t", + "QuoteType": 1, + "NamePos": 40, + "NameEnd": 41 + } + }, + "HasFinal": false + }, + "StatementEnd": 41, + "SampleRatio": null, + "HasFinal": false + } + }, + "Window": null, + "Prewhere": null, + "Where": { + "WherePos": 42, + "Expr": { + "LeftExpr": { + "Object": { + "Fields": [ + { + "Name": "t", + "QuoteType": 1, + "NamePos": 48, + "NameEnd": 49 + }, + { + "Name": "a", + "QuoteType": 1, + "NamePos": 50, + "NameEnd": 51 + } + ] + }, + "Operation": ".", + "Index": { + "NumPos": 52, + "NumEnd": 53, + "Literal": "1", + "Base": 10 + } + }, + "Operation": "=", + "RightExpr": { + "NumPos": 56, + "NumEnd": 57, + "Literal": "1", + "Base": 10 + }, + "HasGlobal": false, + "HasNot": false + } + }, + "GroupBy": null, + "WithTotal": false, + "Having": null, + "OrderBy": { + "OrderPos": 58, + "ListEnd": 72, + "Items": [ + { + "OrderPos": 58, + "Expr": { + "Object": { + "Fields": [ + { + "Name": "t", + "QuoteType": 1, + "NamePos": 67, + "NameEnd": 68 + }, + { + "Name": "a", + "QuoteType": 1, + "NamePos": 69, + "NameEnd": 70 + } + ] + }, + "Operation": ".", + "Index": { + "NumPos": 71, + "NumEnd": 72, + "Literal": "2", + "Base": 10 + } + }, + "Alias": null, + "Direction": "", + "Fill": null + } + ], + "Interpolate": null + }, + "LimitBy": null, + "Limit": null, + "Settings": null, + "Format": null, + "UnionAll": null, + "UnionDistinct": null, + "Except": null, + "Intersect": null + } +] \ No newline at end of file diff --git a/parser/testdata/query/select_tuple_element_of_qualified_column.sql b/parser/testdata/query/select_tuple_element_of_qualified_column.sql new file mode 100644 index 0000000..2370e3e --- /dev/null +++ b/parser/testdata/query/select_tuple_element_of_qualified_column.sql @@ -0,0 +1 @@ +SELECT t.a.1, db.t.a.2 + 1 FROM db.t AS t WHERE t.a.1 = 1 ORDER BY t.a.2