Repository: AfterShip/clickhouse-sql-parser
Pull request #49 (2024-02-27 06:50:58)

Fix the wrong parsing of creating materialized view statements
The correct syntax for create materialized view is:
```sql
CREATE MATERIALIZED VIEW [IF NOT EXISTS] [db.]table_name [ON CLUSTER] [TO[db.]name] [ENGINE = engine] [POPULATE] AS SELECT ...
```
Our original parsing implement from antlr, process was wrong, now it is fixed.

example:
```sql
CREATE
MATERIALIZED VIEW infra_bm.view_name 
    ON CLUSTER 'default_cluster' TO infra_bm.table_name
(
  `f1` DateTime64(3), 
  `f2` String, 
  `f3` String, 
  `f4` String, 
  `f5` String, 
  `f6` Int64
) AS
SELECT f1,
       f2,
       visitParamExtractString(properties, 'f3')   AS f3,
       visitParamExtractString(properties, 'f4')      AS f4,
       visitParamExtractString(properties, 'f5')    AS f5,
    visitParamExtractInt(properties, 'f6') AS f6
FROM
    infra_bm.table_name1
WHERE
    infra_bm.table_name1.event = 'test-event';

```

coveralls: ## Pull Request Test Coverage Report for [Build 8060774617](https://coveralls.io/builds/65933832)


### Details

* **-4** of **15**   **(73.33%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.009%**) to **56.407%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [parser/ast.go](https://coveralls.io/builds/65933832/source?filename=parser%2Fast.go#L1171) | 7 | 9 | 77.78%
| [parser/parser_view.go](https://coveralls.io/builds/65933832/source?filename=parser%2Fparser_view.go#L46) | 4 | 6 | 66.67%
<!-- | **Total:** | **11** | **15** | **73.33%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/65933832/badge)](https://coveralls.io/builds/65933832) |
| :-- | --: |
| Change from base [Build 7853328021](https://coveralls.io/builds/65620813): |  -0.009% |
| Covered Lines: | 5423 |
| Relevant Lines: | 9614 |

---
##### 💛  - [Coveralls](https://coveralls.io)

Lance726: Our original implementation is based on [antlr](https://github.com/ClickHouse/ClickHouse/blob/c8db5403d793545be6b34fb9b90897c5faa4a6de/utils/antlr/ClickHouseParser.g4#L110), which looks a little different from the actual syntax.

Patch:
diff --git a/parser/ast.go b/parser/ast.go
index 93493d0..8707717 100644
--- a/parser/ast.go
+++ b/parser/ast.go
@@ -1095,9 +1095,7 @@ type CreateMaterializedView struct {
 	StatementEnd Pos
 	Name         *TableIdentifier
 	IfNotExists  bool
-	UUID         *UUID
 	OnCluster    *OnClusterExpr
-	TableSchema  *TableSchemaExpr
 	Engine       *EngineExpr
 	Destination  *DestinationExpr
 	SubQuery     *SubQueryExpr
@@ -1127,17 +1125,17 @@ func (c *CreateMaterializedView) String(level int) string {
 		builder.WriteString(NewLine(level))
 		builder.WriteString(c.OnCluster.String(level))
 	}
-	if c.TableSchema != nil {
-		builder.WriteString(NewLine(level))
-		// level + 1 to add an indent for table schema
-		builder.WriteString(c.TableSchema.String(level + 1))
-	}
 	if c.Engine != nil {
 		builder.WriteString(c.Engine.String(level))
 	}
 	if c.Destination != nil {
 		builder.WriteString(NewLine(level))
 		builder.WriteString(c.Destination.String(level))
+		if c.Destination.TableSchema != nil {
+			builder.WriteString(NewLine(level))
+			// level + 1 to add an indent for table schema
+			builder.WriteString(c.Destination.TableSchema.String(level + 1))
+		}
 	}
 	if c.Populate {
 		builder.WriteString(" POPULATE ")
@@ -1154,21 +1152,11 @@ func (c *CreateMaterializedView) Accept(visitor ASTVisitor) error {
 	if err := c.Name.Accept(visitor); err != nil {
 		return err
 	}
-	if c.UUID != nil {
-		if err := c.UUID.Accept(visitor); err != nil {
-			return err
-		}
-	}
 	if c.OnCluster != nil {
 		if err := c.OnCluster.Accept(visitor); err != nil {
 			return err
 		}
 	}
-	if c.TableSchema != nil {
-		if err := c.TableSchema.Accept(visitor); err != nil {
-			return err
-		}
-	}
 	if c.Engine != nil {
 		if err := c.Engine.Accept(visitor); err != nil {
 			return err
@@ -1178,6 +1166,11 @@ func (c *CreateMaterializedView) Accept(visitor ASTVisitor) error {
 		if err := c.Destination.Accept(visitor); err != nil {
 			return err
 		}
+		if c.Destination.TableSchema != nil {
+			if err := c.Destination.TableSchema.Accept(visitor); err != nil {
+				return err
+			}
+		}
 	}
 	if c.SubQuery != nil {
 		if err := c.SubQuery.Accept(visitor); err != nil {
@@ -1642,6 +1635,7 @@ func (r *RoleRenamePair) Accept(visitor ASTVisitor) error {
 type DestinationExpr struct {
 	ToPos           Pos
 	TableIdentifier *TableIdentifier
+	TableSchema     *TableSchemaExpr
 }
 
 func (d *DestinationExpr) Pos() Pos {
diff --git a/parser/parser_view.go b/parser/parser_view.go
index d2e6ba3..907c6a4 100644
--- a/parser/parser_view.go
+++ b/parser/parser_view.go
@@ -25,12 +25,6 @@ func (p *Parser) parseCreateMaterializedView(pos Pos) (*CreateMaterializedView,
 	}
 	createMaterializedView.Name = tableIdentifier
 
-	// try parse UUID clause if exists
-	uuid, err := p.tryParseUUID()
-	if err != nil {
-		return nil, err
-	}
-	createMaterializedView.UUID = uuid
 	// parse ON CLUSTER clause if exists
 	onCluster, err := p.tryParseOnCluster(p.Pos())
 	if err != nil {
@@ -38,12 +32,6 @@ func (p *Parser) parseCreateMaterializedView(pos Pos) (*CreateMaterializedView,
 	}
 	createMaterializedView.OnCluster = onCluster
 
-	tableSchema, err := p.parseTableSchemaExpr(p.Pos())
-	if err != nil {
-		return nil, err
-	}
-	createMaterializedView.TableSchema = tableSchema
-
 	switch {
 	case p.matchKeyword(KeywordTo):
 		destinationExpr, err := p.parseDestinationExpr(p.Pos())
@@ -52,6 +40,13 @@ func (p *Parser) parseCreateMaterializedView(pos Pos) (*CreateMaterializedView,
 		}
 		createMaterializedView.Destination = destinationExpr
 		createMaterializedView.StatementEnd = destinationExpr.End()
+		if p.matchTokenKind("(") {
+			tableSchema, err := p.parseTableSchemaExpr(p.Pos())
+			if err != nil {
+				return nil, err
+			}
+			createMaterializedView.Destination.TableSchema = tableSchema
+		}
 	case p.matchKeyword(KeywordEngine):
 		engineExpr, err := p.parseEngineExpr(p.Pos())
 		if err != nil {


Test patch:
diff --git a/parser/testdata/ddl/create_materialized_view_basic.sql b/parser/testdata/ddl/create_materialized_view_basic.sql
index b94b344..4437911 100644
--- a/parser/testdata/ddl/create_materialized_view_basic.sql
+++ b/parser/testdata/ddl/create_materialized_view_basic.sql
@@ -1,12 +1,21 @@
-ATTACH MATERIALIZED VIEW test.events_local
-UUID '3493e374-e2bb-481b-b493-e374e2bb981b'
-(`f0` DateTime64(3),
-`f1` String,
-`f2` String,
-`f3` String,
-`f4` String,
-`f5` Int64)
-ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{layer}-{shard}}')
-PARTITION BY toDate(f1)
-ORDER BY (f1, f2, f3, f4)
-SETTINGS index_granularity = 8192;
\ No newline at end of file
+CREATE
+MATERIALIZED VIEW infra_bm.view_name 
+    ON CLUSTER 'default_cluster' TO infra_bm.table_name
+(
+  `f1` DateTime64(3), 
+  `f2` String, 
+  `f3` String, 
+  `f4` String, 
+  `f5` String, 
+  `f6` Int64
+) AS
+SELECT f1,
+       f2,
+       visitParamExtractString(properties, 'f3')   AS f3,
+       visitParamExtractString(properties, 'f4')      AS f4,
+       visitParamExtractString(properties, 'f5')    AS f5,
+    visitParamExtractInt(properties, 'f6') AS f6
+FROM
+    infra_bm.table_name1
+WHERE
+    infra_bm.table_name1.event = 'test-event';
\ No newline at end of file
diff --git a/parser/testdata/ddl/format/create_materialized_view_basic.sql b/parser/testdata/ddl/format/create_materialized_view_basic.sql
index ae9dc9f..39f9641 100644
--- a/parser/testdata/ddl/format/create_materialized_view_basic.sql
+++ b/parser/testdata/ddl/format/create_materialized_view_basic.sql
@@ -1,28 +1,47 @@
 -- Origin SQL:
-ATTACH MATERIALIZED VIEW test.events_local
-UUID '3493e374-e2bb-481b-b493-e374e2bb981b'
-(`f0` DateTime64(3),
-`f1` String,
-`f2` String,
-`f3` String,
-`f4` String,
-`f5` Int64)
-ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{layer}-{shard}}')
-PARTITION BY toDate(f1)
-ORDER BY (f1, f2, f3, f4)
-SETTINGS index_granularity = 8192;
+CREATE
+MATERIALIZED VIEW infra_bm.view_name 
+    ON CLUSTER 'default_cluster' TO infra_bm.table_name
+(
+  `f1` DateTime64(3), 
+  `f2` String, 
+  `f3` String, 
+  `f4` String, 
+  `f5` String, 
+  `f6` Int64
+) AS
+SELECT f1,
+       f2,
+       visitParamExtractString(properties, 'f3')   AS f3,
+       visitParamExtractString(properties, 'f4')      AS f4,
+       visitParamExtractString(properties, 'f5')    AS f5,
+    visitParamExtractInt(properties, 'f6') AS f6
+FROM
+    infra_bm.table_name1
+WHERE
+    infra_bm.table_name1.event = 'test-event';
 
 -- Format SQL:
-CREATE MATERIALIZED VIEW test.events_local
+CREATE MATERIALIZED VIEW infra_bm.view_name
+ON CLUSTER 'default_cluster'
+TO infra_bm.table_name
 (
-    `f0` DateTime64(3),
-    `f1` String,
+    `f1` DateTime64(3),
     `f2` String,
     `f3` String,
     `f4` String,
-    `f5` Int64
-)
-ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/{layer}-{shard}}')
-PARTITION BY toDate(f1)
-SETTINGS index_granularity=8192
-ORDER BY (f1, f2, f3, f4);
+    `f5` String,
+    `f6` Int64
+) AS (
+  SELECT 
+    f1,
+    f2,
+    visitParamExtractString(properties, 'f3') AS f3,
+    visitParamExtractString(properties, 'f4') AS f4,
+    visitParamExtractString(properties, 'f5') AS f5,
+    visitParamExtractInt(properties, 'f6') AS f6
+  FROM
+    infra_bm.table_name1
+  WHERE
+    infra_bm.table_name1.event = 'test-event'
+);
diff --git a/parser/testdata/ddl/output/bug_001.sql.golden.json b/parser/testdata/ddl/output/bug_001.sql.golden.json
index 92aeed6..9c4ba8d 100644
--- a/parser/testdata/ddl/output/bug_001.sql.golden.json
+++ b/parser/testdata/ddl/output/bug_001.sql.golden.json
@@ -17,7 +17,6 @@
       }
     },
     "IfNotExists": true,
-    "UUID": null,
     "OnCluster": {
       "OnPos": 60,
       "Expr": {
@@ -26,7 +25,6 @@
         "Literal": "default_cluster"
       }
     },
-    "TableSchema": null,
     "Engine": null,
     "Destination": {
       "ToPos": 89,
@@ -43,7 +41,8 @@
           "NamePos": 95,
           "NameEnd": 103
         }
-      }
+      },
+      "TableSchema": null
     },
     "SubQuery": {
       "AsPos": 104,
diff --git a/parser/testdata/ddl/output/create_live_view_basic.sql.golden.json b/parser/testdata/ddl/output/create_live_view_basic.sql.golden.json
index f57a6de..7c19559 100644
--- a/parser/testdata/ddl/output/create_live_view_basic.sql.golden.json
+++ b/parser/testdata/ddl/output/create_live_view_basic.sql.golden.json
@@ -24,7 +24,8 @@
           "NamePos": 49,
           "NameEnd": 63
         }
-      }
+      },
+      "TableSchema": null
     },
     "TableSchema": {
       "SchemaPos": 63,
diff --git a/parser/testdata/ddl/output/create_materialized_view_basic.sql.golden.json b/parser/testdata/ddl/output/create_materialized_view_basic.sql.golden.json
index aa41dc4..826603a 100644
--- a/parser/testdata/ddl/output/create_materialized_view_basic.sql.golden.json
+++ b/parser/testdata/ddl/output/create_materialized_view_basic.sql.golden.json
@@ -1,328 +1,473 @@
 [
   {
     "CreatePos": 0,
-    "StatementEnd": 334,
+    "StatementEnd": 537,
     "Name": {
       "Database": {
-        "Name": "test",
+        "Name": "infra_bm",
         "Unquoted": false,
         "NamePos": 25,
-        "NameEnd": 29
+        "NameEnd": 33
       },
       "Table": {
-        "Name": "events_local",
+        "Name": "view_name",
         "Unquoted": false,
-        "NamePos": 30,
-        "NameEnd": 42
+        "NamePos": 34,
+        "NameEnd": 43
       }
     },
     "IfNotExists": false,
-    "UUID": {
-      "Value": {
-        "LiteralPos": 49,
-        "LiteralEnd": 85,
-        "Literal": "3493e374-e2bb-481b-b493-e374e2bb981b"
+    "OnCluster": {
+      "OnPos": 49,
+      "Expr": {
+        "LiteralPos": 61,
+        "LiteralEnd": 76,
+        "Literal": "default_cluster"
       }
     },
-    "OnCluster": null,
-    "TableSchema": {
-      "SchemaPos": 87,
-      "SchemaEnd": 170,
-      "Columns": [
-        {
-          "NamePos": 89,
-          "ColumnEnd": 105,
-          "Name": {
-            "Name": "f0",
-            "Unquoted": true,
-            "NamePos": 89,
-            "NameEnd": 91
-          },
-          "Type": {
-            "LeftParenPos": 104,
-            "RightParenPos": 105,
+    "Engine": null,
+    "Destination": {
+      "ToPos": 78,
+      "TableIdentifier": {
+        "Database": {
+          "Name": "infra_bm",
+          "Unquoted": false,
+          "NamePos": 81,
+          "NameEnd": 89
+        },
+        "Table": {
+          "Name": "table_name",
+          "Unquoted": false,
+          "NamePos": 90,
+          "NameEnd": 100
+        }
+      },
+      "TableSchema": {
+        "SchemaPos": 101,
+        "SchemaEnd": 203,
+        "Columns": [
+          {
+            "NamePos": 106,
+            "ColumnEnd": 122,
             "Name": {
-              "Name": "DateTime64",
-              "Unquoted": false,
-              "NamePos": 93,
-              "NameEnd": 103
+              "Name": "f1",
+              "Unquoted": true,
+              "NamePos": 106,
+              "NameEnd": 108
             },
-            "Params": [
-              {
-                "NumPos": 104,
-                "NumEnd": 105,
-                "Literal": "3",
-                "Base": 10
-              }
-            ]
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        },
-        {
-          "NamePos": 109,
-          "ColumnEnd": 119,
-          "Name": {
-            "Name": "f1",
-            "Unquoted": true,
-            "NamePos": 109,
-            "NameEnd": 111
+            "Type": {
+              "LeftParenPos": 121,
+              "RightParenPos": 122,
+              "Name": {
+                "Name": "DateTime64",
+                "Unquoted": false,
+                "NamePos": 110,
+                "NameEnd": 120
+              },
+              "Params": [
+                {
+                  "NumPos": 121,
+                  "NumEnd": 122,
+                  "Literal": "3",
+                  "Base": 10
+                }
+              ]
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
           },
-          "Type": {
+          {
+            "NamePos": 129,
+            "ColumnEnd": 139,
             "Name": {
-              "Name": "String",
-              "Unquoted": false,
-              "NamePos": 113,
-              "NameEnd": 119
-            }
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        },
-        {
-          "NamePos": 122,
-          "ColumnEnd": 132,
-          "Name": {
-            "Name": "f2",
-            "Unquoted": true,
-            "NamePos": 122,
-            "NameEnd": 124
+              "Name": "f2",
+              "Unquoted": true,
+              "NamePos": 129,
+              "NameEnd": 131
+            },
+            "Type": {
+              "Name": {
+                "Name": "String",
+                "Unquoted": false,
+                "NamePos": 133,
+                "NameEnd": 139
+              }
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
           },
-          "Type": {
+          {
+            "NamePos": 145,
+            "ColumnEnd": 155,
             "Name": {
-              "Name": "String",
-              "Unquoted": false,
-              "NamePos": 126,
-              "NameEnd": 132
-            }
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        },
-        {
-          "NamePos": 135,
-          "ColumnEnd": 145,
-          "Name": {
-            "Name": "f3",
-            "Unquoted": true,
-            "NamePos": 135,
-            "NameEnd": 137
+              "Name": "f3",
+              "Unquoted": true,
+              "NamePos": 145,
+              "NameEnd": 147
+            },
+            "Type": {
+              "Name": {
+                "Name": "String",
+                "Unquoted": false,
+                "NamePos": 149,
+                "NameEnd": 155
+              }
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
           },
-          "Type": {
+          {
+            "NamePos": 161,
+            "ColumnEnd": 171,
             "Name": {
-              "Name": "String",
-              "Unquoted": false,
-              "NamePos": 139,
-              "NameEnd": 145
-            }
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        },
-        {
-          "NamePos": 148,
-          "ColumnEnd": 158,
-          "Name": {
-            "Name": "f4",
-            "Unquoted": true,
-            "NamePos": 148,
-            "NameEnd": 150
+              "Name": "f4",
+              "Unquoted": true,
+              "NamePos": 161,
+              "NameEnd": 163
+            },
+            "Type": {
+              "Name": {
+                "Name": "String",
+                "Unquoted": false,
+                "NamePos": 165,
+                "NameEnd": 171
+              }
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
           },
-          "Type": {
+          {
+            "NamePos": 177,
+            "ColumnEnd": 187,
             "Name": {
-              "Name": "String",
-              "Unquoted": false,
-              "NamePos": 152,
-              "NameEnd": 158
-            }
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        },
-        {
-          "NamePos": 161,
-          "ColumnEnd": 170,
-          "Name": {
-            "Name": "f5",
-            "Unquoted": true,
-            "NamePos": 161,
-            "NameEnd": 163
+              "Name": "f5",
+              "Unquoted": true,
+              "NamePos": 177,
+              "NameEnd": 179
+            },
+            "Type": {
+              "Name": {
+                "Name": "String",
+                "Unquoted": false,
+                "NamePos": 181,
+                "NameEnd": 187
+              }
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
           },
-          "Type": {
+          {
+            "NamePos": 193,
+            "ColumnEnd": 202,
             "Name": {
-              "Name": "Int64",
-              "Unquoted": false,
-              "NamePos": 165,
-              "NameEnd": 170
-            }
-          },
-          "NotNull": null,
-          "Nullable": null,
-          "Property": null,
-          "Codec": null,
-          "TTL": null,
-          "Comment": null,
-          "CompressionCodec": null
-        }
-      ],
-      "AliasTable": null,
-      "TableFunction": null
+              "Name": "f6",
+              "Unquoted": true,
+              "NamePos": 193,
+              "NameEnd": 195
+            },
+            "Type": {
+              "Name": {
+                "Name": "Int64",
+                "Unquoted": false,
+                "NamePos": 197,
+                "NameEnd": 202
+              }
+            },
+            "NotNull": null,
+            "Nullable": null,
+            "Property": null,
+            "Codec": null,
+            "TTL": null,
+            "Comment": null,
+            "CompressionCodec": null
+          }
+        ],
+        "AliasTable": null,
+        "TableFunction": null
+      }
     },
-    "Engine": {
-      "EnginePos": 172,
-      "EngineEnd": 334,
-      "Name": "ReplicatedAggregatingMergeTree",
-      "Params": {
-        "LeftParenPos": 211,
-        "RightParenPos": 249,
-        "Items": {
-          "ListPos": 213,
-          "ListEnd": 248,
+    "SubQuery": {
+      "AsPos": 205,
+      "Select": {
+        "SelectPos": 208,
+        "StatementEnd": 537,
+        "With": null,
+        "Top": null,
+        "SelectColumns": {
+          "ListPos": 215,
+          "ListEnd": 456,
           "HasDistinct": false,
           "Items": [
             {
-              "LiteralPos": 213,
-              "LiteralEnd": 248,
-              "Literal": "/clickhouse/tables/{layer}-{shard}}"
-            }
-          ]
-        },
-        "ColumnArgList": null
-      },
-      "PrimaryKey": null,
-      "PartitionBy": {
-        "PartitionPos": 251,
-        "Expr": {
-          "ListPos": 264,
-          "ListEnd": 273,
-          "HasDistinct": false,
-          "Items": [
+              "Name": "f1",
+              "Unquoted": false,
+              "NamePos": 215,
+              "NameEnd": 217
+            },
             {
-              "Name": {
-                "Name": "toDate",
+              "Name": "f2",
+              "Unquoted": false,
+              "NamePos": 226,
+              "NameEnd": 228
+            },
+            {
+              "Expr": {
+                "Name": {
+                  "Name": "visitParamExtractString",
+                  "Unquoted": false,
+                  "NamePos": 237,
+                  "NameEnd": 260
+                },
+                "Params": {
+                  "LeftParenPos": 260,
+                  "RightParenPos": 277,
+                  "Items": {
+                    "ListPos": 261,
+                    "ListEnd": 276,
+                    "HasDistinct": false,
+                    "Items": [
+                      {
+                        "Name": "properties",
+                        "Unquoted": false,
+                        "NamePos": 261,
+                        "NameEnd": 271
+                      },
+                      {
+                        "LiteralPos": 274,
+                        "LiteralEnd": 276,
+                        "Literal": "f3"
+                      }
+                    ]
+                  },
+                  "ColumnArgList": null
+                }
+              },
+              "AliasPos": 281,
+              "Alias": {
+                "Name": "f3",
+                "Unquoted": false,
+                "NamePos": 284,
+                "NameEnd": 286
+              }
+            },
+            {
+              "Expr": {
+                "Name": {
+                  "Name": "visitParamExtractString",
+                  "Unquoted": false,
+                  "NamePos": 295,
+                  "NameEnd": 318
+                },
+                "Params": {
+                  "LeftParenPos": 318,
+                  "RightParenPos": 335,
+                  "Items": {
+                    "ListPos": 319,
+                    "ListEnd": 334,
+                    "HasDistinct": false,
+                    "Items": [
+                      {
+                        "Name": "properties",
+                        "Unquoted": false,
+                        "NamePos": 319,
+                        "NameEnd": 329
+                      },
+                      {
+                        "LiteralPos": 332,
+                        "LiteralEnd": 334,
+                        "Literal": "f4"
+                      }
+                    ]
+                  },
+                  "ColumnArgList": null
+                }
+              },
+              "AliasPos": 342,
+              "Alias": {
+                "Name": "f4",
                 "Unquoted": false,
-                "NamePos": 264,
-                "NameEnd": 270
+                "NamePos": 345,
+                "NameEnd": 347
+              }
+            },
+            {
+              "Expr": {
+                "Name": {
+                  "Name": "visitParamExtractString",
+                  "Unquoted": false,
+                  "NamePos": 356,
+                  "NameEnd": 379
+                },
+                "Params": {
+                  "LeftParenPos": 379,
+                  "RightParenPos": 396,
+                  "Items": {
+                    "ListPos": 380,
+                    "ListEnd": 395,
+                    "HasDistinct": false,
+                    "Items": [
+                      {
+                        "Name": "properties",
+                        "Unquoted": false,
+                        "NamePos": 380,
+                        "NameEnd": 390
+                      },
+                      {
+                        "LiteralPos": 393,
+                        "LiteralEnd": 395,
+                        "Literal": "f5"
+                      }
+                    ]
+                  },
+                  "ColumnArgList": null
+                }
               },
-              "Params": {
-                "LeftParenPos": 270,
-                "RightParenPos": 273,
-                "Items": {
-                  "ListPos": 271,
-                  "ListEnd": 273,
-                  "HasDistinct": false,
-                  "Items": [
-                    {
-                      "Name": "f1",
-                      "Unquoted": false,
-                      "NamePos": 271,
-                      "NameEnd": 273
-                    }
-                  ]
+              "AliasPos": 401,
+              "Alias": {
+                "Name": "f5",
+                "Unquoted": false,
+                "NamePos": 404,
+                "NameEnd": 406
+              }
+            },
+            {
+              "Expr": {
+                "Name": {
+                  "Name": "visitParamExtractInt",
+                  "Unquoted": false,
+                  "NamePos": 412,
+                  "NameEnd": 432
                 },
-                "ColumnArgList": null
+                "Params": {
+                  "LeftParenPos": 432,
+                  "RightParenPos": 449,
+                  "Items": {
+                    "ListPos": 433,
+                    "ListEnd": 448,
+                    "HasDistinct": false,
+                    "Items": [
+                      {
+                        "Name": "properties",
+                        "Unquoted": false,
+                        "NamePos": 433,
+                        "NameEnd": 443
+                      },
+                      {
+                        "LiteralPos": 446,
+                        "LiteralEnd": 448,
+                        "Literal": "f6"
+                      }
+                    ]
+                  },
+                  "ColumnArgList": null
+                }
+              },
+              "AliasPos": 451,
+              "Alias": {
+                "Name": "f6",
+                "Unquoted": false,
+                "NamePos": 454,
+                "NameEnd": 456
               }
             }
           ]
-        }
-      },
-      "SampleBy": null,
-      "TTLExprList": null,
-      "SettingsExprList": {
-        "SettingsPos": 301,
-        "ListEnd": 334,
-        "Items": [
-          {
-            "SettingsPos": 310,
-            "Name": {
-              "Name": "index_granularity",
-              "Unquoted": false,
-              "NamePos": 310,
-              "NameEnd": 327
-            },
+        },
+        "From": {
+          "FromPos": 457,
+          "Expr": {
+            "TablePos": 466,
+            "TableEnd": 486,
+            "Alias": null,
             "Expr": {
-              "NumPos": 330,
-              "NumEnd": 334,
-              "Literal": "8192",
-              "Base": 10
-            }
+              "Database": {
+                "Name": "infra_bm",
+                "Unquoted": false,
+                "NamePos": 466,
+                "NameEnd": 474
+              },
+              "Table": {
+                "Name": "table_name1",
+                "Unquoted": false,
+                "NamePos": 475,
+                "NameEnd": 486
+              }
+            },
+            "HasFinal": false
           }
-        ]
-      },
-      "OrderByListExpr": {
-        "OrderPos": 275,
-        "ListEnd": 299,
-        "Items": [
-          {
-            "OrderPos": 275,
-            "Expr": {
-              "LeftParenPos": 284,
-              "RightParenPos": 299,
-              "Items": {
-                "ListPos": 285,
-                "ListEnd": 299,
-                "HasDistinct": false,
-                "Items": [
-                  {
-                    "Name": "f1",
-                    "Unquoted": false,
-                    "NamePos": 285,
-                    "NameEnd": 287
-                  },
-                  {
-                    "Name": "f2",
-                    "Unquoted": false,
-                    "NamePos": 289,
-                    "NameEnd": 291
-                  },
-                  {
-                    "Name": "f3",
-                    "Unquoted": false,
-                    "NamePos": 293,
-                    "NameEnd": 295
-                  },
-                  {
-                    "Name": "f4",
-                    "Unquoted": false,
-                    "NamePos": 297,
-                    "NameEnd": 299
-                  }
-                ]
+        },
+        "ArrayJoin": null,
+        "Window": null,
+        "Prewhere": null,
+        "Where": {
+          "WherePos": 487,
+          "Expr": {
+            "LeftExpr": {
+              "Database": {
+                "Name": "infra_bm",
+                "Unquoted": false,
+                "NamePos": 497,
+                "NameEnd": 505
+              },
+              "Table": {
+                "Name": "table_name1",
+                "Unquoted": false,
+                "NamePos": 506,
+                "NameEnd": 517
               },
-              "ColumnArgList": null
+              "Column": {
+                "Name": "event",
+                "Unquoted": false,
+                "NamePos": 518,
+                "NameEnd": 523
+              }
             },
-            "Direction": "None"
+            "Operation": "=",
+            "RightExpr": {
+              "LiteralPos": 527,
+              "LiteralEnd": 537,
+              "Literal": "test-event"
+            },
+            "HasGlobal": false,
+            "HasNot": false
           }
-        ]
+        },
+        "GroupBy": null,
+        "WithTotal": false,
+        "Having": null,
+        "OrderBy": null,
+        "LimitBy": null,
+        "Limit": null,
+        "Settings": null,
+        "UnionAll": null,
+        "UnionDistinct": null,
+        "Except": null
       }
     },
-    "Destination": null,
-    "SubQuery": null,
     "Populate": false
   }
 ]
\ No newline at end of file
diff --git a/parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json b/parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json
index 263ff5a..6723458 100644
--- a/parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json
+++ b/parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json
@@ -17,7 +17,6 @@
       }
     },
     "IfNotExists": false,
-    "UUID": null,
     "OnCluster": {
       "OnPos": 33,
       "Expr": {
@@ -27,7 +26,6 @@
         "NameEnd": 59
       }
     },
-    "TableSchema": null,
     "Engine": {
       "EnginePos": 60,
       "EngineEnd": 190,