@@ -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 {
195203func (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
204212func (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.
321347func isNullTest (opName string ) bool {
@@ -633,6 +659,11 @@ func (a *analyzer) typeNullIf(e *ast.A_Expr) (exprType, error) {
633659// placeholder that type.
634660func (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}
0 commit comments