Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
71de72f
Endtoend tests: bunch of ydb e2e tests + some engine and types fixes
1NepuNep1 Oct 15, 2025
0a5a8df
Fixed go.mod && go.sum
1NepuNep1 Oct 15, 2025
44a76e7
Umolyau zarabotai pg
1NepuNep1 Oct 15, 2025
1fc9aac
Fixed range result.Rows(ctx) issue (go<1.23)
1NepuNep1 Oct 15, 2025
673969c
Fixed catalog tests after rewriting some internal logic in convert.go
1NepuNep1 Oct 15, 2025
5434c17
fixed some new tests
1NepuNep1 Oct 15, 2025
1f324b4
Bunch of join tests
1NepuNep1 Oct 16, 2025
342854d
Bunch of tests + bunch of fixes
1NepuNep1 Oct 20, 2025
6fada2f
Added some more tests + rewrited strings to BYTES ([]byte)
1NepuNep1 Oct 20, 2025
90001cf
fixed string
1NepuNep1 Oct 20, 2025
dfabcaa
Fixed Strings
1NepuNep1 Oct 20, 2025
d668dde
MORE TESTS + fixed count(*) issue
1NepuNep1 Oct 20, 2025
548aadd
Bunch of tests + some convert fixes
1NepuNep1 Oct 22, 2025
94837ef
Fixed datatype && builtins test
1NepuNep1 Oct 22, 2025
45daae9
MORE TESTS
1NepuNep1 Oct 22, 2025
ee9b918
MORE TESTS
1NepuNep1 Oct 22, 2025
f8cb775
Final tests + convert fixes
1NepuNep1 Oct 23, 2025
9401511
fixed incorrect func name
1NepuNep1 Oct 23, 2025
b2dd615
fixed uuid issue
1NepuNep1 Oct 23, 2025
c745457
fixed uuid in types_uuid test
1NepuNep1 Oct 23, 2025
b0c7864
fixed test
1NepuNep1 Oct 23, 2025
2461778
rewrited ydb-go-sdk codegen to ranges back (+ upped testada go.mod ve…
1NepuNep1 Oct 27, 2025
52f6c4d
rewrited parseStringLiteral func to be more understandable
1NepuNep1 Oct 27, 2025
6783d19
fixed ydb-go-sdk test with newer generation
1NepuNep1 Oct 27, 2025
3b8d7ec
resolved limit issue
1NepuNep1 Oct 27, 2025
7b0a77e
Rewrited slices logic not replace them
1NepuNep1 Oct 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type tmplCtx struct {
OmitSqlcVersion bool
BuildTags string
WrapErrors bool
EmitSliceExpansion bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand All @@ -55,6 +56,10 @@ func (t *tmplCtx) codegenDbarg() string {
return ""
}

func (t *tmplCtx) shouldExpandSlices(q Query) bool {
return t.EmitSliceExpansion && q.Arg.HasSqlcSlices()
}

// Called as a global method since subtemplate queryCodeStdExec does not have
// access to the toplevel tmplCtx
func (t *tmplCtx) codegenEmitPreparedQueries() bool {
Expand Down Expand Up @@ -181,6 +186,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
EmitEnumValidMethod: options.EmitEnumValidMethod,
EmitAllEnumValues: options.EmitAllEnumValues,
EmitSliceExpansion: options.Engine != "ydb",
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(options.SqlPackage),
Expand Down Expand Up @@ -232,6 +238,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
"emitPreparedQueries": tctx.codegenEmitPreparedQueries,
"queryMethod": tctx.codegenQueryMethod,
"queryRetval": tctx.codegenQueryRetval,
"shouldExpandSlices": tctx.shouldExpandSlices,
}

tmpl := template.Must(
Expand Down
9 changes: 7 additions & 2 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (i *importer) dbImports() fileImports {
var stdlibTypes = map[string]string{
"json.RawMessage": "encoding/json",
"time.Time": "time",
"time.Duration": "time",
"net.IP": "net",
"net.HardwareAddr": "net",
"netip.Addr": "net/netip",
Expand Down Expand Up @@ -232,6 +233,10 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
if uses("pgvector.Vector") && !overrideVector {
pkg[ImportSpec{Path: "github.com/pgvector/pgvector-go"}] = struct{}{}
}
_, overrideDecimal := overrideTypes["types.Decimal"]
if uses("types.Decimal") && !overrideDecimal {
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/table/types"}] = struct{}{}
}

// Custom imports
for _, override := range options.Overrides {
Expand Down Expand Up @@ -271,7 +276,7 @@ func (i *importer) interfaceImports() fileImports {
})

std["context"] = struct{}{}

sqlpkg := parseDriver(i.Options.SqlPackage)
if sqlpkg.IsYDBGoSDK() {
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/query"}] = struct{}{}
Expand Down Expand Up @@ -404,7 +409,7 @@ func (i *importer) queryImports(filename string) fileImports {
}

sqlpkg := parseDriver(i.Options.SqlPackage)
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() && i.Options.Engine != "ydb" {
std["strings"] = struct{}{}
}
if sliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {
Expand Down
6 changes: 6 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Options struct {
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`

InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
Engine string `json:"-" yaml:"-"`
}

type GlobalOptions struct {
Expand All @@ -72,6 +73,11 @@ func Parse(req *plugin.GenerateRequest) (*Options, error) {
}
maps.Copy(options.Rename, global.Rename)
}

if req.Settings != nil {
options.Engine = req.Settings.Engine
}

return options, nil
}

Expand Down
30 changes: 27 additions & 3 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func ydbBuilderMethodForColumnType(dbType string) string {
return "Int32"
case "uint16":
return "Uint16"
case "int16", "smallserial","serial2":
case "int16", "smallserial", "serial2":
return "Int16"
case "uint8":
return "Uint8"
Expand All @@ -321,8 +321,10 @@ func ydbBuilderMethodForColumnType(dbType string) string {
return "JSON"
case "jsondocument":
return "JSONDocument"
case "utf8", "text", "string":
case "utf8", "text":
return "Text"
case "string":
return "Bytes"
case "date":
return "Date"
case "date32":
Expand All @@ -338,12 +340,19 @@ func ydbBuilderMethodForColumnType(dbType string) string {
case "tztimestamp":
return "TzTimestamp"
case "uuid":
return "UUID"
return "Uuid"
case "yson":
return "YSON"

case "integer": // LIMIT/OFFSET parameters support
return "Uint64"

//TODO: support other types
default:
// Check for decimal types
if strings.HasPrefix(baseType, "decimal") {
return "Decimal"
}
return ""
}
}
Expand Down Expand Up @@ -405,6 +414,21 @@ func (v QueryValue) YDBParamsBuilder() string {
isPtr := strings.HasPrefix(goType, "*")
isArray := field.Column.IsArray || field.Column.IsSqlcSlice

if method == "Decimal" {
if isArray {
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))
lines = append(lines, "\t\tlist = list.Add().Decimal(param.Bytes, param.Precision, param.Scale)")
lines = append(lines, "\t}")
lines = append(lines, "\tparameters = list.EndList()")
} else if isPtr {
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).BeginOptional().Decimal(&%s.Bytes, %s.Precision, %s.Scale).EndOptional()", paramName, variable, variable, variable))
} else {
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).Decimal(%s.Bytes, %s.Precision, %s.Scale)", paramName, variable, variable, variable))
}
return true
}

if isArray {
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{end}}

{{define "queryCodeStdExec"}}
{{- if .Arg.HasSqlcSlices }}
{{- if shouldExpandSlices . }}
query := {{.ConstantName}}
var queryParams []interface{}
{{- if .Arg.Struct }}
Expand Down
20 changes: 10 additions & 10 deletions internal/codegen/golang/templates/ydb-go-sdk/interfaceCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
{{- $dbtxParam := .EmitMethodsWithDBArgument -}}
{{- range .GoQueries}}
{{- if and (eq .Cmd ":one") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
{{- range .Comments}}//{{.}}
{{- end}}
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ({{.Ret.DefineType}}, error)
{{- else if eq .Cmd ":one"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- range .Comments}}//{{.}}
{{- end}}
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ({{.Ret.DefineType}}, error)
{{- end}}
{{- if and (eq .Cmd ":many") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
{{- range .Comments}}//{{.}}
{{- end}}
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ([]{{.Ret.DefineType}}, error)
{{- else if eq .Cmd ":many"}}
{{range .Comments}}//{{.}}
{{end -}}
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ([]{{.Ret.DefineType}}, error)
{{- end}}
{{- if and (eq .Cmd ":exec") ($dbtxParam) }}
{{range .Comments}}//{{.}}
{{end -}}
{{- range .Comments}}//{{.}}
{{- end}}
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) error
{{- else if eq .Cmd ":exec"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- range .Comments}}//{{.}}
{{- end}}
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) error
{{- end}}
{{- end}}
Expand Down
52 changes: 42 additions & 10 deletions internal/codegen/golang/ydb_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
// // use the smallest type they have which is NullInt16
// return "sql.NullInt16"
return "*int8"
case "int16":
case "int16", "smallint":
if notNull {
return "int16"
}
Expand All @@ -52,7 +52,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
// return "sql.NullInt16"
return "*int16"
case "int", "int32": //ydb doesn't have int type, but we need it to support untyped constants
case "int", "int32", "integer":
if notNull {
return "int32"
}
Expand All @@ -61,7 +61,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
// return "sql.NullInt32"
return "*int32"
case "int64":
case "int64", "bigint":
if notNull {
return "int64"
}
Expand All @@ -72,25 +72,37 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
return "*int64"

case "uint8":
if notNull {
return "uint8"
}
if emitPointersForNull {
return "*uint8"
}
return "uint8"
return "*uint8"
case "uint16":
if notNull {
return "uint16"
}
if emitPointersForNull {
return "*uint16"
}
return "uint16"
return "*uint16"
case "uint32":
if notNull {
return "uint32"
}
if emitPointersForNull {
return "*uint32"
}
return "uint32"
return "*uint32"
case "uint64":
if notNull {
return "uint64"
}
if emitPointersForNull {
return "*uint64"
}
return "uint64"
return "*uint64"

case "float":
if notNull {
Expand All @@ -114,7 +126,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
return "*float64"

// string types
case "string", "utf8", "text":
case "utf8", "text":
if notNull {
return "string"
}
Expand Down Expand Up @@ -163,7 +175,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
return "*string"

case "date", "date32", "datetime", "timestamp", "tzdate", "tztimestamp", "tzdatetime":
case "date", "date32", "datetime", "timestamp", "tzdate", "tztimestamp", "tzdatetime", "datetime64", "timestamp64", "tzdate32", "tzdatetime64", "tztimestamp64":
if notNull {
return "time.Time"
}
Expand All @@ -172,6 +184,15 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
return "*time.Time"

case "interval", "interval64":
if notNull {
return "time.Duration"
}
if emitPointersForNull {
return "*time.Duration"
}
return "*time.Duration"

case "uuid":
if notNull {
return "uuid.UUID"
Expand All @@ -181,7 +202,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
return "*uuid.UUID"

case "yson":
case "yson", "string":
if notNull {
return "[]byte"
}
Expand All @@ -198,6 +219,17 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
return "interface{}"

default:
if strings.HasPrefix(columnType, "decimal") {
if notNull {
return "types.Decimal"
}

if emitPointersForNull {
return "*types.Decimal"
}
return "*types.Decimal"
}

if debug.Active {
log.Printf("unknown YDB type: %s\n", columnType)
}
Expand Down
17 changes: 15 additions & 2 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"strconv"

"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
Expand Down Expand Up @@ -118,11 +119,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
case *limitOffset:
defaultP := named.NewInferredParam("offset", true)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)

dataType := "integer"
if comp.conf.Engine == config.EngineYDB {
dataType = "uint64"
}

a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
DataType: "integer",
DataType: dataType,
NotNull: p.NotNull(),
IsNamedParam: isNamed,
},
Expand All @@ -131,11 +138,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
case *limitCount:
defaultP := named.NewInferredParam("limit", true)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)

dataType := "integer"
if comp.conf.Engine == config.EngineYDB {
dataType = "uint64"
}

a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
DataType: "integer",
DataType: dataType,
NotNull: p.NotNull(),
IsNamedParam: isNamed,
},
Expand Down
Loading
Loading