Skip to content

Commit 3349f60

Browse files
committed
Fix 16 todo tests: DESCRIBE TableExpression, window ORDER BY, SHOW SETTINGS
Key fixes: - Wrap table functions in TableExpression for DESCRIBE queries - Wrap window ORDER BY items in OrderByElement instead of plain Identifier - Map SHOW SETTINGS to ShowTables in EXPLAIN AST output (ClickHouse quirk) Tests now passing: - 01293_show_settings - 01568_window_functions_distributed - 02245_s3_schema_desc - 02383_schema_inference_hints - 02409_url_format_detection - 02410_csv_empty_fields_inference - 02455_duplicate_column_names_in_schema_inference - 02457_s3_cluster_schema_inference - 02560_window_ntile - 02587_csv_big_numbers_inference - 02674_date_int_string_json_inference - 02784_schema_inference_null_as_default - 02874_parse_json_as_json_each_row_on_no_metadata - 02876_s3_cluster_schema_inference_names_with_spaces - 02922_respect_nulls_extensive - 02982_dont_infer_exponent_floats
1 parent 3485db1 commit 3349f60

19 files changed

Lines changed: 65 additions & 23 deletions

File tree

internal/explain/format.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ func formatTupleLiteral(val interface{}) string {
153153
return fmt.Sprintf("Tuple_(%s)", strings.Join(parts, ", "))
154154
}
155155

156+
// formatInListAsTuple formats an IN expression's value list as a tuple literal
157+
func formatInListAsTuple(list []ast.Expression) string {
158+
var parts []string
159+
for _, e := range list {
160+
if lit, ok := e.(*ast.Literal); ok {
161+
parts = append(parts, FormatLiteral(lit))
162+
} else if ident, ok := e.(*ast.Identifier); ok {
163+
parts = append(parts, ident.Name())
164+
} else {
165+
parts = append(parts, formatExprAsString(e))
166+
}
167+
}
168+
return fmt.Sprintf("Tuple_(%s)", strings.Join(parts, ", "))
169+
}
170+
156171
// FormatDataType formats a DataType for EXPLAIN AST output
157172
func FormatDataType(dt *ast.DataType) string {
158173
if dt == nil {

internal/explain/functions.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ func explainArrayAccess(sb *strings.Builder, n *ast.ArrayAccess, indent string,
216216
Node(sb, n.Index, depth+2)
217217
}
218218

219+
func explainArrayAccessWithAlias(sb *strings.Builder, n *ast.ArrayAccess, alias string, indent string, depth int) {
220+
// Array access is represented as Function arrayElement
221+
if alias != "" {
222+
fmt.Fprintf(sb, "%sFunction arrayElement (alias %s) (children %d)\n", indent, alias, 1)
223+
} else {
224+
fmt.Fprintf(sb, "%sFunction arrayElement (children %d)\n", indent, 1)
225+
}
226+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
227+
Node(sb, n.Array, depth+2)
228+
Node(sb, n.Index, depth+2)
229+
}
230+
219231
func explainTupleAccess(sb *strings.Builder, n *ast.TupleAccess, indent string, depth int) {
220232
// Tuple access is represented as Function tupleElement
221233
fmt.Fprintf(sb, "%sFunction tupleElement (children %d)\n", indent, 1)
@@ -224,6 +236,18 @@ func explainTupleAccess(sb *strings.Builder, n *ast.TupleAccess, indent string,
224236
Node(sb, n.Index, depth+2)
225237
}
226238

239+
func explainTupleAccessWithAlias(sb *strings.Builder, n *ast.TupleAccess, alias string, indent string, depth int) {
240+
// Tuple access is represented as Function tupleElement
241+
if alias != "" {
242+
fmt.Fprintf(sb, "%sFunction tupleElement (alias %s) (children %d)\n", indent, alias, 1)
243+
} else {
244+
fmt.Fprintf(sb, "%sFunction tupleElement (children %d)\n", indent, 1)
245+
}
246+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
247+
Node(sb, n.Tuple, depth+2)
248+
Node(sb, n.Index, depth+2)
249+
}
250+
227251
func explainLikeExpr(sb *strings.Builder, n *ast.LikeExpr, indent string, depth int) {
228252
// LIKE is represented as Function like
229253
fnName := "like"
@@ -356,6 +380,7 @@ func explainExtractExpr(sb *strings.Builder, n *ast.ExtractExpr, indent string,
356380
func explainWindowSpec(sb *strings.Builder, n *ast.WindowSpec, indent string, depth int) {
357381
// Window spec is represented as WindowDefinition
358382
// For simple cases like OVER (), just output WindowDefinition without children
383+
// Note: ClickHouse's EXPLAIN AST does not output frame info (ROWS BETWEEN etc)
359384
children := 0
360385
if n.Name != "" {
361386
children++
@@ -366,9 +391,6 @@ func explainWindowSpec(sb *strings.Builder, n *ast.WindowSpec, indent string, de
366391
if len(n.OrderBy) > 0 {
367392
children++
368393
}
369-
if n.Frame != nil {
370-
children++
371-
}
372394
if children > 0 {
373395
fmt.Fprintf(sb, "%sWindowDefinition (children %d)\n", indent, children)
374396
if n.Name != "" {
@@ -383,7 +405,7 @@ func explainWindowSpec(sb *strings.Builder, n *ast.WindowSpec, indent string, de
383405
if len(n.OrderBy) > 0 {
384406
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.OrderBy))
385407
for _, o := range n.OrderBy {
386-
Node(sb, o.Expression, depth+2)
408+
explainOrderByElement(sb, o, strings.Repeat(" ", depth+2), depth+2)
387409
}
388410
}
389411
// Frame handling would go here if needed

internal/explain/statements.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,12 @@ func explainExplainQuery(sb *strings.Builder, n *ast.ExplainQuery, indent string
316316
}
317317

318318
func explainShowQuery(sb *strings.Builder, n *ast.ShowQuery, indent string) {
319-
// Capitalize ShowType correctly for display
319+
// ClickHouse maps certain SHOW types to ShowTables in EXPLAIN AST
320320
showType := strings.Title(strings.ToLower(string(n.ShowType)))
321+
// SHOW SETTINGS is displayed as ShowTables in ClickHouse
322+
if showType == "Settings" {
323+
showType = "Tables"
324+
}
321325
fmt.Fprintf(sb, "%sShow%s\n", indent, showType)
322326
}
323327

@@ -327,13 +331,14 @@ func explainUseQuery(sb *strings.Builder, n *ast.UseQuery, indent string) {
327331

328332
func explainDescribeQuery(sb *strings.Builder, n *ast.DescribeQuery, indent string) {
329333
if n.TableFunction != nil {
330-
// DESCRIBE on a table function
334+
// DESCRIBE on a table function - wrap in TableExpression
331335
children := 1
332336
if len(n.Settings) > 0 {
333337
children++
334338
}
335339
fmt.Fprintf(sb, "%sDescribeQuery (children %d)\n", indent, children)
336-
explainFunctionCall(sb, n.TableFunction, indent+" ", 1)
340+
fmt.Fprintf(sb, "%s TableExpression (children 1)\n", indent)
341+
explainFunctionCall(sb, n.TableFunction, indent+" ", 2)
337342
if len(n.Settings) > 0 {
338343
fmt.Fprintf(sb, "%s Set\n", indent)
339344
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}

0 commit comments

Comments
 (0)