Skip to content

Commit 4c5cc40

Browse files
committed
sqlite: leave the legacy compiler out of the "$1" return type
The legacy compiler takes a dialect function's return type as it is spelled, so it is not taught to follow "$1" to an argument; sum, abs, ceil, floor and trunc generate an untyped value on that path, the way max and min already do, and the analysis core resolves them. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdKaVjSmdkmXPeUkxetyDY
1 parent 012548f commit 4c5cc40

5 files changed

Lines changed: 16 additions & 54 deletions

File tree

‎internal/compiler/output_columns.go‎

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

97
"github.com/sqlc-dev/sqlc/internal/config"
108
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -326,7 +324,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
326324
if err == nil {
327325
cols = append(cols, &Column{
328326
Name: name,
329-
DataType: returnDataType(fun, n, tables, rtables),
327+
DataType: dataType(fun.ReturnType),
330328
NotNull: !fun.ReturnTypeNullable,
331329
IsFuncCall: true,
332330
})
@@ -660,41 +658,6 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
660658
return tables, nil
661659
}
662660

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-
698661
func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef) ([]*Column, error) {
699662
parts := stringSlice(node.Fields)
700663
var schema, name, alias string

‎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/mathfunc.sql.go‎

Lines changed: 8 additions & 8 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 & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)