Skip to content

Commit efc83d3

Browse files
committed
clickhouse: casts, typed placeholders, aggregate-function idents, canonical forms and Nested columns
A cast and a {name:Type} placeholder hand the core their whole spelling, so CAST(x AS Nullable(String)) reports a nullable string and a placeholder reports the type it declares, named as the query named it. A per-dialect canonicalizer stores what ClickHouse stores: Decimal32(4) as Decimal(9, 4), Enum('a', 'b') as Enum8('a' = 1, 'b' = 2), a Variant's members sorted, and the function an AggregateFunction names as a word. A result-type rule types toDecimal64(x, 4) as Decimal(18, 4) from the literal. A Nested column becomes the array columns ClickHouse stores it as, reachable as n.a, and goldeneye's rewriter binds {name:Type} like any placeholder. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent 14c2a58 commit efc83d3

13 files changed

Lines changed: 636 additions & 24 deletions

File tree

‎internal/core/analyzer/expr.go‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ func (a *analyzer) typeColumnRef(c *ast.ColumnRef) (exprType, error) {
161161
if err != nil {
162162
return exprType{}, err
163163
}
164+
// A dotted name may be a column's own, as ClickHouse names the columns
165+
// a Nested column stores as n.a.
166+
if !ok && relation != "" {
167+
rel, col, ok, err = a.resolveColumn("", relation+"."+column)
168+
if err != nil {
169+
return exprType{}, err
170+
}
171+
}
164172
if !ok {
165173
if relation != "" {
166174
return exprType{}, fmt.Errorf("unknown column %q.%q", relation, column)
@@ -195,10 +203,10 @@ func flattenFields(fields *ast.List) []string {
195203
func (a *analyzer) typeParamRef(p *ast.ParamRef) (exprType, error) {
196204
cur, ok := a.params[p.Number]
197205
if !ok {
198-
cur = core.Parameter{Number: p.Number}
206+
cur = core.Parameter{Number: p.Number, Name: p.Name}
199207
a.params[p.Number] = cur
200208
}
201-
return exprType{typeOID: cur.TypeOID, nullable: !cur.NotNull}, nil
209+
return exprType{typeOID: cur.TypeOID, expr: cur.Type.WithNullable(false), nullable: !cur.NotNull}, nil
202210
}
203211

204212
func (a *analyzer) inferParam(number int, t exprType) {
@@ -296,11 +304,17 @@ func (a *analyzer) typeAExpr(e *ast.A_Expr) (exprType, error) {
296304
a.inferParam(pr.Number, rightT)
297305
a.nameParamAfter(pr.Number, e.Rexpr)
298306
leftT = rightT
307+
} else if pr := castParamRef(e.Lexpr); pr != nil {
308+
a.inferParam(pr.Number, rightT)
309+
a.nameParamAfter(pr.Number, e.Rexpr)
299310
}
300311
if pr, ok := e.Rexpr.(*ast.ParamRef); ok && leftT.typeOID != 0 {
301312
a.inferParam(pr.Number, leftT)
302313
a.nameParamAfter(pr.Number, e.Lexpr)
303314
rightT = leftT
315+
} else if pr := castParamRef(e.Rexpr); pr != nil {
316+
a.inferParam(pr.Number, leftT)
317+
a.nameParamAfter(pr.Number, e.Lexpr)
304318
}
305319

306320
overload, err := a.resolveOperator(opName, leftT, rightT)
@@ -316,6 +330,18 @@ func (a *analyzer) typeAExpr(e *ast.A_Expr) (exprType, error) {
316330
}, nil
317331
}
318332

333+
// castParamRef is the placeholder a cast wraps, as ClickHouse's {p:UInt64}
334+
// is written, or nil. The cast has typed it already; what it is compared
335+
// with still names it and says which column it stands in for.
336+
func castParamRef(n ast.Node) *ast.ParamRef {
337+
tc, ok := n.(*ast.TypeCast)
338+
if !ok {
339+
return nil
340+
}
341+
pr, _ := tc.Arg.(*ast.ParamRef)
342+
return pr
343+
}
344+
319345
// isNullTest reports whether an operator compares with NULL as a value
320346
// rather than propagating it, so that its result is never NULL.
321347
func isNullTest(opName string) bool {
@@ -633,6 +659,11 @@ func (a *analyzer) typeNullIf(e *ast.A_Expr) (exprType, error) {
633659
// placeholder that type.
634660
func (a *analyzer) typeOperands(n ast.Node, other exprType) error {
635661
if pr, ok := n.(*ast.ParamRef); ok {
662+
// Registering the placeholder first keeps the name its syntax gave
663+
// it, as ClickHouse's {name:Type} does.
664+
if _, err := a.typeParamRef(pr); err != nil {
665+
return err
666+
}
636667
if other.typeOID != 0 || other.expr != nil {
637668
a.inferParam(pr.Number, other)
638669
}
@@ -798,13 +829,34 @@ func (a *analyzer) typeFuncCall(f *ast.FuncCall) (exprType, error) {
798829
}
799830
}
800831
ret := a.returnType(p, argTypes)
832+
// A dialect may know the result better than the catalog does, from the
833+
// arguments' types and literal values.
834+
if computed := a.resultTypeHook(name, args, argTypes); computed != nil {
835+
ret = a.lookupType(computed)
836+
}
801837
ret.nullable = p.ReturnNullable
802838
if !p.NeverNull && anyNullable && a.cat.PropagatesNullable() {
803839
ret.nullable = true
804840
}
805841
return ret, nil
806842
}
807843

844+
// resultTypeHook asks the dialect's result-type rule about a call, handing
845+
// it each argument's type and, for an integer literal, its value.
846+
func (a *analyzer) resultTypeHook(name string, args []ast.Node, argTypes []exprType) *core.TypeExpr {
847+
ras := make([]core.ResultArg, len(args))
848+
for i, arg := range args {
849+
ras[i].Type = a.exprOf(argTypes[i])
850+
if c, ok := arg.(*ast.A_Const); ok {
851+
if n, ok := c.Val.(*ast.Integer); ok {
852+
v := n.Ival
853+
ras[i].Int = &v
854+
}
855+
}
856+
}
857+
return a.cat.ResultTypeOf(name, ras)
858+
}
859+
808860
// returnType resolves a polymorphic return type — max(anyelement), or a
809861
// seed's "$2" for the type of the second argument — to the type the call was
810862
// made with.
@@ -933,8 +985,10 @@ func (a *analyzer) typeTypeCast(c *ast.TypeCast) (exprType, error) {
933985
}
934986
t := a.lookupType(target)
935987
// A cast is how a query says what an otherwise untyped placeholder
936-
// holds, and a placeholder so typed is not null. Anything else cast
937-
// is NULL exactly when it was NULL before.
988+
// holds, and a placeholder so typed is not null unless the type says
989+
// otherwise, as ClickHouse's Nullable(String) does. Anything else
990+
// cast is NULL when it was NULL before, or when the type says so.
991+
t.nullable = target.Nullable
938992
if pr, ok := c.Arg.(*ast.ParamRef); ok {
939993
if err := a.typeOperands(pr, t); err != nil {
940994
return exprType{}, err
@@ -945,6 +999,6 @@ func (a *analyzer) typeTypeCast(c *ast.TypeCast) (exprType, error) {
945999
if err != nil {
9461000
return exprType{}, err
9471001
}
948-
t.nullable = arg.nullable
1002+
t.nullable = t.nullable || arg.nullable
9491003
return t, nil
9501004
}

‎internal/core/analyzer/projection.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"slices"
5+
"strings"
56

67
"github.com/sqlc-dev/sqlc/internal/core"
78
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -49,6 +50,11 @@ func (a *analyzer) projectTarget(rt *ast.ResTarget) error {
4950
col.Type = a.typeExprOf(t)
5051
a.decorateSource(&col, t.sourceAttributeOID, t.sourceTableAlias)
5152
if rt.Name == nil || *rt.Name == "" {
53+
// A column whose own name is dotted, as ClickHouse's n.a is, is
54+
// reported under that name rather than its last part.
55+
if col.Source != nil && strings.Contains(col.Source.Column, ".") {
56+
col.Name = col.Source.Column
57+
}
5258
a.qualifyDuplicate(&col, t.sourceTableAlias)
5359
}
5460
a.columns = append(a.columns, col)

‎internal/core/hooks.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,50 @@ type Canonicalizer func(*TypeExpr) *TypeExpr
1818
// takes, or an empty name for a type that stands on nothing.
1919
type UserTypeBase func(name string) (base, category string)
2020

21+
// A ResultArg is one argument of a function call as a result-type rule
22+
// sees it: its type, when known, and its value when it is an integer
23+
// literal, which is what the scale of toDecimal64(x, 4) is.
24+
type ResultArg struct {
25+
Type *TypeExpr
26+
Int *int64
27+
}
28+
29+
// A ResultType says what a function returns when that depends on its
30+
// arguments in a way no seed can spell: ClickHouse's toDecimal64(x, s) is
31+
// Decimal(18, s). It returns nil to leave the answer to the catalog.
32+
type ResultType func(name string, args []ResultArg) *TypeExpr
33+
2134
var (
2235
hooksMu sync.RWMutex
2336
canonicalizers = map[string]Canonicalizer{}
2437
userTypeBases = map[string]UserTypeBase{}
38+
resultTypes = map[string]ResultType{}
2539
)
2640

41+
// RegisterResultType installs a dialect's result-type rule, under the
42+
// dialect's name.
43+
func RegisterResultType(dialect string, fn ResultType) {
44+
hooksMu.Lock()
45+
defer hooksMu.Unlock()
46+
resultTypes[dialect] = fn
47+
}
48+
49+
// ResultTypeOf applies the catalog's dialect's result-type rule to a call,
50+
// or returns nil when there is none or it has nothing to say.
51+
func (c *Catalog) ResultTypeOf(name string, args []ResultArg) *TypeExpr {
52+
dialect := c.dialectName()
53+
if dialect == "" {
54+
return nil
55+
}
56+
hooksMu.RLock()
57+
fn := resultTypes[dialect]
58+
hooksMu.RUnlock()
59+
if fn == nil {
60+
return nil
61+
}
62+
return fn(name, args)
63+
}
64+
2765
// RegisterUserTypeBase installs the rule a dialect resolves an unseeded
2866
// type family by, under the dialect's name.
2967
func RegisterUserTypeBase(dialect string, fn UserTypeBase) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
INSERT INTO things VALUES (1, 'a', NULL, 1.5, ['x'], [NULL, 'y'], [[1, 2]], 'k', '2024-01-01 00:00:00', '2024-01-01 00:00:00.123', 1.25, 'active', {'k': 1}, (1.0, 2.0), (51.5, -0.1), {'a': NULL}, '127.0.0.1', '00000000-0000-0000-0000-000000000000', 'abcd', true);
1+
INSERT INTO things VALUES (1, 'a', NULL, 1.5, ['x'], [NULL, 'y'], [[1, 2]], 'k', '2024-01-01 00:00:00', '2024-01-01 00:00:00.123', 1.25, 'active', {'k': 1}, (1.0, 2.0), (51.5, -0.1), {'a': NULL}, '127.0.0.1', '00000000-0000-0000-0000-000000000000', 'abcd', true, 1.5, 'x', 'v', [1], ['s'], 3, 7);

‎internal/endtoend/testdata/analyze_types/clickhouse/query.sql‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@
22
SELECT * FROM things;
33

44
-- name: StarColumns :many
5-
SELECT id, name, tag, amount, tags, labels, matrix, kind, created, updated, price, status, attrs, pos, geo, scores, ip, uid, fixed, flag FROM things;
5+
SELECT id, name, tag, amount, tags, labels, matrix, kind, created, updated, price, status, attrs, pos, geo, scores, ip, uid, fixed, flag, small, plain, either, n.a, n.b, total, whole FROM things;
6+
7+
-- name: Casts :one
8+
SELECT
9+
CAST(id AS String) AS a,
10+
toDecimal64(id, 4) AS b,
11+
CAST(name AS Nullable(String)) AS c,
12+
toDateTime64(created, 3) AS d,
13+
CAST(tags AS Array(String)) AS e,
14+
CAST(tag AS Nullable(String)) AS f
15+
FROM things;
16+
17+
-- name: Placeholders :many
18+
SELECT id FROM things
19+
WHERE id = {p1:UInt64} AND name = {p2:String} AND amount > {p3:Float64} AND tag = {p4:Nullable(String)} AND price = {p5:Decimal(10, 2)};

‎internal/endtoend/testdata/analyze_types/clickhouse/schema.sql‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ CREATE TABLE things (
1818
ip IPv4,
1919
uid UUID,
2020
fixed FixedString(4),
21-
flag Bool
21+
flag Bool,
22+
small Decimal32(4),
23+
plain Enum('x', 'y'),
24+
either Variant(String, Int64),
25+
n Nested(a UInt8, b String),
26+
total SimpleAggregateFunction(sum, UInt64),
27+
whole INT
2228
) ENGINE = MergeTree ORDER BY id;

0 commit comments

Comments
 (0)