Skip to content

Commit 1c4ae55

Browse files
Ajit Pratap SinghAjit Pratap Singh
authored andcommitted
fix(ast): include Parameters in FunctionCall.Children() (#482 review)
1 parent f161feb commit 1c4ae55

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

pkg/sql/ast/ast.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ func (f *FunctionCall) expressionNode() {}
705705
func (f FunctionCall) TokenLiteral() string { return f.Name }
706706
func (f FunctionCall) Children() []Node {
707707
children := nodifyExpressions(f.Arguments)
708+
if len(f.Parameters) > 0 {
709+
children = append(children, nodifyExpressions(f.Parameters)...)
710+
}
708711
if f.Over != nil {
709712
children = append(children, f.Over)
710713
}

pkg/sql/parser/clickhouse_parametric_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/ajitpratap0/GoSQLX/pkg/gosqlx"
11+
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
1112
"github.com/ajitpratap0/GoSQLX/pkg/sql/keywords"
1213
)
1314

@@ -29,3 +30,45 @@ func TestClickHouseParametricAggregates(t *testing.T) {
2930
})
3031
}
3132
}
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

Comments
 (0)