@@ -97,6 +97,9 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
9797 if n .AsSelect != nil {
9898 children ++
9999 }
100+ if n .AsTableFunction != nil {
101+ children ++
102+ }
100103 // ClickHouse adds an extra space before (children N) for CREATE DATABASE
101104 if n .CreateDatabase {
102105 fmt .Fprintf (sb , "%sCreateQuery %s (children %d)\n " , indent , name , children )
@@ -112,6 +115,16 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
112115 if len (n .Indexes ) > 0 {
113116 childrenCount ++
114117 }
118+ // Check for PRIMARY KEY constraints in column declarations
119+ var primaryKeyColumns []string
120+ for _ , col := range n .Columns {
121+ if col .PrimaryKey {
122+ primaryKeyColumns = append (primaryKeyColumns , col .Name )
123+ }
124+ }
125+ if len (primaryKeyColumns ) > 0 {
126+ childrenCount ++ // Add for Function tuple containing PRIMARY KEY columns
127+ }
115128 fmt .Fprintf (sb , "%s Columns definition (children %d)\n " , indent , childrenCount )
116129 if len (n .Columns ) > 0 {
117130 fmt .Fprintf (sb , "%s ExpressionList (children %d)\n " , indent , len (n .Columns ))
@@ -125,6 +138,14 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
125138 Index (sb , idx , depth + 3 )
126139 }
127140 }
141+ // Output PRIMARY KEY columns as Function tuple
142+ if len (primaryKeyColumns ) > 0 {
143+ fmt .Fprintf (sb , "%s Function tuple (children %d)\n " , indent , 1 )
144+ fmt .Fprintf (sb , "%s ExpressionList (children %d)\n " , indent , len (primaryKeyColumns ))
145+ for _ , colName := range primaryKeyColumns {
146+ fmt .Fprintf (sb , "%s Identifier %s\n " , indent , colName )
147+ }
148+ }
128149 }
129150 if n .Engine != nil || len (n .OrderBy ) > 0 || len (n .PrimaryKey ) > 0 || n .PartitionBy != nil || len (n .Settings ) > 0 {
130151 storageChildren := 0
@@ -228,6 +249,10 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
228249 // AS SELECT is output directly without Subquery wrapper
229250 Node (sb , n .AsSelect , depth + 1 )
230251 }
252+ if n .AsTableFunction != nil {
253+ // AS table_function(...) is output directly
254+ Node (sb , n .AsTableFunction , depth + 1 )
255+ }
231256}
232257
233258func explainDropQuery (sb * strings.Builder , n * ast.DropQuery , indent string , depth int ) {
@@ -337,6 +362,27 @@ func explainShowQuery(sb *strings.Builder, n *ast.ShowQuery, indent string) {
337362 if showType == "Settings" || showType == "Databases" {
338363 showType = "Tables"
339364 }
365+
366+ // SHOW CREATE TABLE has special output format with database and table identifiers
367+ if n .ShowType == ast .ShowCreate && (n .Database != "" || n .From != "" ) {
368+ // Format: ShowCreateTableQuery database table (children 2)
369+ name := n .From
370+ if n .Database != "" && n .From != "" {
371+ fmt .Fprintf (sb , "%sShowCreateTableQuery %s %s (children 2)\n " , indent , n .Database , n .From )
372+ fmt .Fprintf (sb , "%s Identifier %s\n " , indent , n .Database )
373+ fmt .Fprintf (sb , "%s Identifier %s\n " , indent , n .From )
374+ } else if n .From != "" {
375+ fmt .Fprintf (sb , "%sShowCreateTableQuery %s (children 1)\n " , indent , name )
376+ fmt .Fprintf (sb , "%s Identifier %s\n " , indent , name )
377+ } else if n .Database != "" {
378+ fmt .Fprintf (sb , "%sShowCreateTableQuery %s (children 1)\n " , indent , n .Database )
379+ fmt .Fprintf (sb , "%s Identifier %s\n " , indent , n .Database )
380+ } else {
381+ fmt .Fprintf (sb , "%sShow%s\n " , indent , showType )
382+ }
383+ return
384+ }
385+
340386 fmt .Fprintf (sb , "%sShow%s\n " , indent , showType )
341387}
342388
0 commit comments