Skip to content

Commit 012548f

Browse files
committed
sqlite: sum and abs return their argument's type, and IS never NULL
Three things the goldeneye check found sqlc's analysis saying that SQLite does not. sum, abs, ceil, floor and trunc hand back an integer for an integer and a real for a real, so the dialect now spells their return type "$1", the argument's type, rather than widening to real; unixepoch, which is a real only with 'subsec', keeps real. The legacy compiler learns to resolve "$n" to the named column's or literal's type, so sum(int_val) generates NullInt64 rather than NullFloat64. IS and IS NOT test for NULL rather than propagate it, so their result is never NULL, as are SQLite's postfix ISNULL and NOTNULL, which used to be typed as their operand. And LIKE, GLOB, REGEXP and MATCH are predicates, listed with the dialect's comparison operators under the names the parser gives them, in place of the ~~ spellings it never uses. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdKaVjSmdkmXPeUkxetyDY
1 parent d95f3da commit 012548f

13 files changed

Lines changed: 101 additions & 25 deletions

File tree

‎internal/compiler/output_columns.go‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package compiler
33
import (
44
"errors"
55
"fmt"
6+
"strconv"
7+
"strings"
68

79
"github.com/sqlc-dev/sqlc/internal/config"
810
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -324,7 +326,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
324326
if err == nil {
325327
cols = append(cols, &Column{
326328
Name: name,
327-
DataType: dataType(fun.ReturnType),
329+
DataType: returnDataType(fun, n, tables, rtables),
328330
NotNull: !fun.ReturnTypeNullable,
329331
IsFuncCall: true,
330332
})
@@ -658,6 +660,41 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
658660
return tables, nil
659661
}
660662

663+
// returnDataType is the type a function call produces. A dialect spells a
664+
// function that hands back one of its arguments, as abs and sum do, with a
665+
// return type of "$n", the type of the nth argument, which is known when
666+
// that argument names a column.
667+
func returnDataType(fun *catalog.Function, call *ast.FuncCall, tables, rtables []*Table) string {
668+
dt := dataType(fun.ReturnType)
669+
rest, ok := strings.CutPrefix(dt, "$")
670+
if !ok {
671+
return dt
672+
}
673+
n, err := strconv.Atoi(rest)
674+
if err != nil || n < 1 || call.Args == nil || n > len(call.Args.Items) {
675+
return "any"
676+
}
677+
switch arg := call.Args.Items[n-1].(type) {
678+
case *ast.ColumnRef:
679+
cols, err := outputColumnRefs(&ast.ResTarget{}, tablesForRef(arg, tables, rtables), arg)
680+
if err == nil && len(cols) == 1 {
681+
return cols[0].DataType
682+
}
683+
case *ast.A_Const:
684+
switch arg.Val.(type) {
685+
case *ast.String:
686+
return "text"
687+
case *ast.Integer:
688+
return "int"
689+
case *ast.Float:
690+
return "float"
691+
case *ast.Boolean:
692+
return "bool"
693+
}
694+
}
695+
return "any"
696+
}
697+
661698
func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef) ([]*Column, error) {
662699
parts := stringSlice(node.Fields)
663700
var schema, name, alias string

‎internal/core/analyzer/expr.go‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ func (a *analyzer) typeAExpr(e *ast.A_Expr) (exprType, error) {
285285
if err != nil {
286286
return exprType{}, err
287287
}
288+
// The postfix null tests SQLite has — x ISNULL, x NOTNULL, x NOT NULL —
289+
// arrive as operators with no right operand, and are predicates that
290+
// are never NULL.
291+
if e.Rexpr == nil && isNullTest(opName) {
292+
return a.boolType(false)
293+
}
288294
rightT, err := a.typeExpr(e.Rexpr)
289295
if err != nil {
290296
return exprType{}, err
@@ -305,12 +311,25 @@ func (a *analyzer) typeAExpr(e *ast.A_Expr) (exprType, error) {
305311
if err != nil {
306312
return exprType{}, err
307313
}
314+
// An operator's result is NULL when an operand is, except for IS and
315+
// IS NOT, which test for NULL rather than propagate it: x IS NULL and
316+
// x IS y are never NULL, whatever x and y are.
308317
return exprType{
309318
typeOID: overload.ResultTypeOID,
310-
nullable: leftT.nullable || rightT.nullable,
319+
nullable: (leftT.nullable || rightT.nullable) && !isNullTest(opName),
311320
}, nil
312321
}
313322

323+
// isNullTest reports whether an operator compares with NULL as a value
324+
// rather than propagating it, so that its result is never NULL.
325+
func isNullTest(opName string) bool {
326+
switch opName {
327+
case "IS", "IS NOT", "ISNULL", "NOTNULL", "NOT NULL":
328+
return true
329+
}
330+
return false
331+
}
332+
314333
// typeQuantifiedExpr types "x = ANY($1)" and "x > ALL(...)": the right side
315334
// holds values of the left side's type, and the result is a predicate.
316335
func (a *analyzer) typeQuantifiedExpr(e *ast.A_Expr) (exprType, error) {

‎internal/endtoend/testdata/analyze_expressions/sqlite/query.sql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SELECT count(*) AS total, max(id) AS latest, min(created) AS first
33
FROM posts WHERE user_id = ?;
44

55
-- name: UserScores :one
6-
SELECT avg(score) AS mean, sum(score) AS sum, group_concat(name) AS names
6+
SELECT avg(score) AS mean, sum(score) AS sum, sum(id) AS ids, group_concat(name) AS names
77
FROM users;
88

99
-- name: ListUsers :many

‎internal/endtoend/testdata/analyze_expressions/sqlite/stdout.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
"nullable": true
5656
}
5757
},
58+
{
59+
"name": "ids",
60+
"type": {
61+
"name": "integer",
62+
"nullable": true
63+
}
64+
},
5865
{
5966
"name": "names",
6067
"type": {

‎internal/endtoend/testdata/builtins/sqlite/go/aggfunc.sql.go‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/builtins/sqlite/go/scalarfunc.sql.go‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/func_match_types/sqlite/go/query.sql.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/engine/sqlite/dialect/dialect.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"bool": "boolean"
88
},
99
"bool": "boolean",
10-
"comparison": ["=", "==", "<>", "!=", "<", "<=", ">", ">=", "IS", "IS NOT"],
10+
"comparison": ["=", "==", "<>", "!=", "<", "<=", ">", ">=", "IS", "IS NOT", "LIKE", "NOT LIKE", "GLOB", "NOT GLOB", "REGEXP", "NOT REGEXP", "MATCH", "NOT MATCH"],
1111
"comparison_categories": "BNSDU",
1212
"arithmetic": ["+", "-", "*", "/", "%"],
1313
"arithmetic_categories": "N",

‎internal/engine/sqlite/dialect/functions.jsonl‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{"name":"-\u003e","args":[{"type":"any"},{"type":"any"}],"returns":"text","nullable":true}
22
{"name":"-\u003e\u003e","args":[{"type":"any"},{"type":"any"}],"returns":"any","nullable":true}
3-
{"name":"abs","args":[{"type":"any"}],"returns":"real"}
3+
{"name":"abs","args":[{"type":"any"}],"returns":"$1"}
44
{"name":"acos","args":[{"type":"real"}],"returns":"real"}
55
{"name":"acosh","args":[{"type":"real"}],"returns":"real"}
66
{"name":"asin","args":[{"type":"real"}],"returns":"real"}
@@ -9,8 +9,8 @@
99
{"name":"atan2","args":[{"type":"real"},{"type":"real"}],"returns":"real"}
1010
{"name":"atanh","args":[{"type":"real"}],"returns":"real"}
1111
{"name":"avg","kind":"a","args":[{"type":"any"}],"returns":"real","nullable":true}
12-
{"name":"ceil","args":[{"type":"any"}],"returns":"real"}
13-
{"name":"ceiling","args":[{"type":"any"}],"returns":"real"}
12+
{"name":"ceil","args":[{"type":"any"}],"returns":"$1"}
13+
{"name":"ceiling","args":[{"type":"any"}],"returns":"$1"}
1414
{"name":"changes","returns":"integer"}
1515
{"name":"char","args":[{"type":"integer","mode":"v"}],"returns":"text"}
1616
{"name":"coalesce","args":[{"type":"any"},{"type":"any"},{"type":"any","mode":"v"}],"returns":"any"}
@@ -30,7 +30,7 @@
3030
{"name":"dense_rank","kind":"w","returns":"integer"}
3131
{"name":"exp","args":[{"type":"real"}],"returns":"real"}
3232
{"name":"first_value","kind":"w","args":[{"type":"any"}],"returns":"any","nullable":true}
33-
{"name":"floor","args":[{"type":"any"}],"returns":"real"}
33+
{"name":"floor","args":[{"type":"any"}],"returns":"$1"}
3434
{"name":"format","args":[{"type":"text","has_default":true},{"type":"any","mode":"v"}],"returns":"text"}
3535
{"name":"glob","args":[{"type":"text"},{"type":"text"}],"returns":"integer"}
3636
{"name":"group_concat","kind":"a","args":[{"type":"text"}],"returns":"text","nullable":true}
@@ -136,7 +136,7 @@
136136
{"name":"substring","args":[{"type":"any"},{"type":"integer"}],"returns":"text"}
137137
{"name":"substring","args":[{"type":"any"},{"type":"integer"},{"type":"integer"}],"returns":"text"}
138138
{"name":"subtype","args":[{"type":"any"}],"returns":"integer"}
139-
{"name":"sum","kind":"a","args":[{"type":"any"}],"returns":"real","nullable":true}
139+
{"name":"sum","kind":"a","args":[{"type":"any"}],"returns":"$1","nullable":true}
140140
{"name":"tan","args":[{"type":"real"}],"returns":"real"}
141141
{"name":"tanh","args":[{"type":"real"}],"returns":"real"}
142142
{"name":"time","args":[{"type":"any","mode":"v"}],"returns":"text","nullable":true}
@@ -145,7 +145,7 @@
145145
{"name":"total_changes","returns":"integer"}
146146
{"name":"trim","args":[{"type":"text"}],"returns":"text"}
147147
{"name":"trim","args":[{"type":"text"},{"type":"text"}],"returns":"text"}
148-
{"name":"trunc","args":[{"type":"any"}],"returns":"real"}
148+
{"name":"trunc","args":[{"type":"any"}],"returns":"$1"}
149149
{"name":"typeof","args":[{"type":"any"}],"returns":"text"}
150150
{"name":"unhex","args":[{"type":"text"}],"returns":"blob","nullable":true}
151151
{"name":"unhex","args":[{"type":"text"},{"type":"text"}],"returns":"blob","nullable":true}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
{"name": "||", "left": "text", "right": "text", "result": "text"}
2-
{"name": "~~", "left": "text", "right": "text", "result": "boolean"}
3-
{"name": "!~~", "left": "text", "right": "text", "result": "boolean"}

0 commit comments

Comments
 (0)