Repository: AfterShip/clickhouse-sql-parser (go)

Issue:
cannot parse `SELECT` as argument to a function
It's possible to use SELECT as argument to function:

```
select 1, (select 70) as `power`, number
from numbers(plus(ifNull(
        (SELECT 1 AS bin_count), 1), 1))
```

Here's a query I've tried to parse to test the parser:
https://github.com/PostHog/posthog/blob/40f632057891ffc614489e3a7942445e14db6a85/posthog/api/test/__snapshots__/test_insight_funnels.ambr

Pull request description:
Add support of using the SELECT query as table function argument
This closes #126.

Patch:
diff --git a/parser/parser_table.go b/parser/parser_table.go
index f6bcba4..a0ebe62 100644
--- a/parser/parser_table.go
+++ b/parser/parser_table.go
@@ -521,7 +521,9 @@ func (p *Parser) parseTableArgExpr(pos Pos) (Expr, error) {
 		default:
 			return ident, nil
 		}
-	case p.matchTokenKind(TokenKindInt), p.matchTokenKind(TokenKindString), p.matchKeyword("NULL"):
+	case p.matchTokenKind(TokenKindLParen):
+		return p.parseSubQuery(p.Pos())
+	case p.matchTokenKind(TokenKindInt), p.matchTokenKind(TokenKindString), p.matchKeyword(KeywordNull):
 		return p.parseLiteral(p.Pos())
 	default:
 		return nil, fmt.Errorf("unexpected token: %q, expected <Name>, <literal>", p.last().String)


Test patch:
diff --git a/parser/testdata/query/format/select_table_function_with_query.sql b/parser/testdata/query/format/select_table_function_with_query.sql
new file mode 100644
index 0000000..c94c228
--- /dev/null
+++ b/parser/testdata/query/format/select_table_function_with_query.sql
@@ -0,0 +1,12 @@
+-- Origin SQL:
+SELECT 1, (SELECT 70) AS `power`, number
+FROM
+numbers(
+    plus(
+        ifNull((SELECT 1 AS bin_count, 1),
+        1)
+    )
+)
+
+-- Format SQL:
+SELECT 1, (SELECT 70) AS `power`, number FROM numbers(plus(ifNull((SELECT 1 AS bin_count, 1), 1)));
diff --git a/parser/testdata/query/output/select_table_function_with_query.sql.golden.json b/parser/testdata/query/output/select_table_function_with_query.sql.golden.json
new file mode 100644
index 0000000..ea48f92
--- /dev/null
+++ b/parser/testdata/query/output/select_table_function_with_query.sql.golden.json
@@ -0,0 +1,205 @@
+[
+  {
+    "SelectPos": 0,
+    "StatementEnd": 125,
+    "With": null,
+    "Top": null,
+    "SelectItems": [
+      {
+        "Expr": {
+          "NumPos": 7,
+          "NumEnd": 8,
+          "Literal": "1",
+          "Base": 10
+        },
+        "Modifiers": [],
+        "Alias": null
+      },
+      {
+        "Expr": {
+          "HasParen": true,
+          "Select": {
+            "SelectPos": 11,
+            "StatementEnd": 20,
+            "With": null,
+            "Top": null,
+            "SelectItems": [
+              {
+                "Expr": {
+                  "NumPos": 18,
+                  "NumEnd": 20,
+                  "Literal": "70",
+                  "Base": 10
+                },
+                "Modifiers": [],
+                "Alias": null
+              }
+            ],
+            "From": null,
+            "ArrayJoin": null,
+            "Window": null,
+            "Prewhere": null,
+            "Where": null,
+            "GroupBy": null,
+            "WithTotal": false,
+            "Having": null,
+            "OrderBy": null,
+            "LimitBy": null,
+            "Limit": null,
+            "Settings": null,
+            "Format": null,
+            "UnionAll": null,
+            "UnionDistinct": null,
+            "Except": null
+          }
+        },
+        "Modifiers": [],
+        "Alias": {
+          "Name": "power",
+          "QuoteType": 3,
+          "NamePos": 26,
+          "NameEnd": 31
+        }
+      },
+      {
+        "Expr": {
+          "Name": "number",
+          "QuoteType": 1,
+          "NamePos": 34,
+          "NameEnd": 40
+        },
+        "Modifiers": [],
+        "Alias": null
+      }
+    ],
+    "From": {
+      "FromPos": 41,
+      "Expr": {
+        "Table": {
+          "TablePos": 46,
+          "TableEnd": 125,
+          "Alias": null,
+          "Expr": {
+            "Name": {
+              "Name": "numbers",
+              "QuoteType": 1,
+              "NamePos": 46,
+              "NameEnd": 53
+            },
+            "Args": {
+              "LeftParenPos": 53,
+              "RightParenPos": 125,
+              "Args": [
+                {
+                  "Name": {
+                    "Name": "plus",
+                    "QuoteType": 1,
+                    "NamePos": 59,
+                    "NameEnd": 63
+                  },
+                  "Args": {
+                    "LeftParenPos": 59,
+                    "RightParenPos": 123,
+                    "Args": [
+                      {
+                        "Name": {
+                          "Name": "ifNull",
+                          "QuoteType": 1,
+                          "NamePos": 73,
+                          "NameEnd": 79
+                        },
+                        "Args": {
+                          "LeftParenPos": 73,
+                          "RightParenPos": 117,
+                          "Args": [
+                            {
+                              "HasParen": true,
+                              "Select": {
+                                "SelectPos": 81,
+                                "StatementEnd": 105,
+                                "With": null,
+                                "Top": null,
+                                "SelectItems": [
+                                  {
+                                    "Expr": {
+                                      "NumPos": 88,
+                                      "NumEnd": 89,
+                                      "Literal": "1",
+                                      "Base": 10
+                                    },
+                                    "Modifiers": [],
+                                    "Alias": {
+                                      "Name": "bin_count",
+                                      "QuoteType": 1,
+                                      "NamePos": 93,
+                                      "NameEnd": 102
+                                    }
+                                  },
+                                  {
+                                    "Expr": {
+                                      "NumPos": 104,
+                                      "NumEnd": 105,
+                                      "Literal": "1",
+                                      "Base": 10
+                                    },
+                                    "Modifiers": [],
+                                    "Alias": null
+                                  }
+                                ],
+                                "From": null,
+                                "ArrayJoin": null,
+                                "Window": null,
+                                "Prewhere": null,
+                                "Where": null,
+                                "GroupBy": null,
+                                "WithTotal": false,
+                                "Having": null,
+                                "OrderBy": null,
+                                "LimitBy": null,
+                                "Limit": null,
+                                "Settings": null,
+                                "Format": null,
+                                "UnionAll": null,
+                                "UnionDistinct": null,
+                                "Except": null
+                              }
+                            },
+                            {
+                              "NumPos": 116,
+                              "NumEnd": 117,
+                              "Literal": "1",
+                              "Base": 10
+                            }
+                          ]
+                        }
+                      }
+                    ]
+                  }
+                }
+              ]
+            }
+          },
+          "HasFinal": false
+        },
+        "StatementEnd": 125,
+        "SampleRatio": null,
+        "HasFinal": false
+      }
+    },
+    "ArrayJoin": null,
+    "Window": null,
+    "Prewhere": null,
+    "Where": null,
+    "GroupBy": null,
+    "WithTotal": false,
+    "Having": null,
+    "OrderBy": null,
+    "LimitBy": null,
+    "Limit": null,
+    "Settings": null,
+    "Format": null,
+    "UnionAll": null,
+    "UnionDistinct": null,
+    "Except": null
+  }
+]
\ No newline at end of file
diff --git a/parser/testdata/query/select_table_function_with_query.sql b/parser/testdata/query/select_table_function_with_query.sql
new file mode 100644
index 0000000..1fa6e5f
--- /dev/null
+++ b/parser/testdata/query/select_table_function_with_query.sql
@@ -0,0 +1,8 @@
+SELECT 1, (SELECT 70) AS `power`, number
+FROM
+numbers(
+    plus(
+        ifNull((SELECT 1 AS bin_count, 1),
+        1)
+    )
+)
\ No newline at end of file