|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + |
| 5 | +package parser_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ajitpratap0/GoSQLX/pkg/gosqlx" |
| 11 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 12 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 13 | +) |
| 14 | + |
| 15 | +// TestClickHouseParametricAggregates verifies that ClickHouse parametric |
| 16 | +// aggregates of the form `funcName(params)(args)` parse. Regression for #482. |
| 17 | +func TestClickHouseParametricAggregates(t *testing.T) { |
| 18 | + queries := map[string]string{ |
| 19 | + "quantile_tdigest": `SELECT quantileTDigest(0.95)(value) FROM events`, |
| 20 | + "top_k": `SELECT topK(10)(name) FROM users`, |
| 21 | + "quantiles": `SELECT quantiles(0.5, 0.9, 0.99)(latency_ms) FROM requests`, |
| 22 | + "with_group_by": `SELECT category, quantileTDigest(0.99)(price) FROM products GROUP BY category`, |
| 23 | + } |
| 24 | + for name, q := range queries { |
| 25 | + q := q |
| 26 | + t.Run(name, func(t *testing.T) { |
| 27 | + if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil { |
| 28 | + t.Fatalf("parse failed: %v", err) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// TestClickHouseParametricAggregates_ASTShape verifies that the Parameters |
| 35 | +// field is populated and reachable via the visitor pattern. |
| 36 | +func TestClickHouseParametricAggregates_ASTShape(t *testing.T) { |
| 37 | + q := `SELECT quantileTDigest(0.95)(value) FROM events` |
| 38 | + tree, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("parse failed: %v", err) |
| 41 | + } |
| 42 | + // Walk the tree until we find a FunctionCall node and verify both |
| 43 | + // Parameters and Arguments are populated, and Children() exposes both. |
| 44 | + var found bool |
| 45 | + var visit func(n ast.Node) |
| 46 | + visit = func(n ast.Node) { |
| 47 | + if n == nil || found { |
| 48 | + return |
| 49 | + } |
| 50 | + if fc, ok := n.(*ast.FunctionCall); ok && fc.Name == "quantileTDigest" { |
| 51 | + if len(fc.Parameters) != 1 { |
| 52 | + t.Fatalf("Parameters: want 1, got %d", len(fc.Parameters)) |
| 53 | + } |
| 54 | + if len(fc.Arguments) != 1 { |
| 55 | + t.Fatalf("Arguments: want 1, got %d", len(fc.Arguments)) |
| 56 | + } |
| 57 | + // Children() must include both the argument and the parameter. |
| 58 | + if len(fc.Children()) < 2 { |
| 59 | + t.Fatalf("Children(): want >=2 (args + params), got %d", len(fc.Children())) |
| 60 | + } |
| 61 | + found = true |
| 62 | + return |
| 63 | + } |
| 64 | + for _, c := range n.Children() { |
| 65 | + visit(c) |
| 66 | + } |
| 67 | + } |
| 68 | + for _, stmt := range tree.Statements { |
| 69 | + visit(stmt) |
| 70 | + } |
| 71 | + if !found { |
| 72 | + t.Fatal("did not find quantileTDigest FunctionCall in AST") |
| 73 | + } |
| 74 | +} |
0 commit comments