Skip to content

Commit 2eaa5d0

Browse files
authored
Tests: Added a lot of ydb e2e tests + some engine and types fixes
* Fixed range result.Rows(ctx) issue (go<1.23) * Rewrited strings to BYTES ([]byte) * Fixed count(*) issue * Fixed datatype test * fixed uuid issue * rewrited ydb-go-sdk codegen to ranges back (+ upped testada go.mod version to 1.23) * rewrited parseStringLiteral func to be more understandable * resolved limit issue * Rewrited slices logic not replace them
1 parent 816eda8 commit 2eaa5d0

File tree

1,138 files changed

+27691
-548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,138 files changed

+27691
-548
lines changed

internal/codegen/golang/gen.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type tmplCtx struct {
4242
OmitSqlcVersion bool
4343
BuildTags string
4444
WrapErrors bool
45+
EmitSliceExpansion bool
4546
}
4647

4748
func (t *tmplCtx) OutputQuery(sourceName string) bool {
@@ -55,6 +56,10 @@ func (t *tmplCtx) codegenDbarg() string {
5556
return ""
5657
}
5758

59+
func (t *tmplCtx) shouldExpandSlices(q Query) bool {
60+
return t.EmitSliceExpansion && q.Arg.HasSqlcSlices()
61+
}
62+
5863
// Called as a global method since subtemplate queryCodeStdExec does not have
5964
// access to the toplevel tmplCtx
6065
func (t *tmplCtx) codegenEmitPreparedQueries() bool {
@@ -181,6 +186,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
181186
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
182187
EmitEnumValidMethod: options.EmitEnumValidMethod,
183188
EmitAllEnumValues: options.EmitAllEnumValues,
189+
EmitSliceExpansion: options.Engine != "ydb",
184190
UsesCopyFrom: usesCopyFrom(queries),
185191
UsesBatch: usesBatch(queries),
186192
SQLDriver: parseDriver(options.SqlPackage),
@@ -232,6 +238,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
232238
"emitPreparedQueries": tctx.codegenEmitPreparedQueries,
233239
"queryMethod": tctx.codegenQueryMethod,
234240
"queryRetval": tctx.codegenQueryRetval,
241+
"shouldExpandSlices": tctx.shouldExpandSlices,
235242
}
236243

237244
tmpl := template.Must(

internal/codegen/golang/imports.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (i *importer) dbImports() fileImports {
149149
var stdlibTypes = map[string]string{
150150
"json.RawMessage": "encoding/json",
151151
"time.Time": "time",
152+
"time.Duration": "time",
152153
"net.IP": "net",
153154
"net.HardwareAddr": "net",
154155
"netip.Addr": "net/netip",
@@ -232,6 +233,10 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
232233
if uses("pgvector.Vector") && !overrideVector {
233234
pkg[ImportSpec{Path: "github.com/pgvector/pgvector-go"}] = struct{}{}
234235
}
236+
_, overrideDecimal := overrideTypes["types.Decimal"]
237+
if uses("types.Decimal") && !overrideDecimal {
238+
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/table/types"}] = struct{}{}
239+
}
235240

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

273278
std["context"] = struct{}{}
274-
279+
275280
sqlpkg := parseDriver(i.Options.SqlPackage)
276281
if sqlpkg.IsYDBGoSDK() {
277282
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/query"}] = struct{}{}
@@ -404,7 +409,7 @@ func (i *importer) queryImports(filename string) fileImports {
404409
}
405410

406411
sqlpkg := parseDriver(i.Options.SqlPackage)
407-
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {
412+
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() && i.Options.Engine != "ydb" {
408413
std["strings"] = struct{}{}
409414
}
410415
if sliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {

internal/codegen/golang/opts/options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Options struct {
4747
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`
4848

4949
InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
50+
Engine string `json:"-" yaml:"-"`
5051
}
5152

5253
type GlobalOptions struct {
@@ -72,6 +73,11 @@ func Parse(req *plugin.GenerateRequest) (*Options, error) {
7273
}
7374
maps.Copy(options.Rename, global.Rename)
7475
}
76+
77+
if req.Settings != nil {
78+
options.Engine = req.Settings.Engine
79+
}
80+
7581
return options, nil
7682
}
7783

internal/codegen/golang/query.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func ydbBuilderMethodForColumnType(dbType string) string {
307307
return "Int32"
308308
case "uint16":
309309
return "Uint16"
310-
case "int16", "smallserial","serial2":
310+
case "int16", "smallserial", "serial2":
311311
return "Int16"
312312
case "uint8":
313313
return "Uint8"
@@ -321,8 +321,10 @@ func ydbBuilderMethodForColumnType(dbType string) string {
321321
return "JSON"
322322
case "jsondocument":
323323
return "JSONDocument"
324-
case "utf8", "text", "string":
324+
case "utf8", "text":
325325
return "Text"
326+
case "string":
327+
return "Bytes"
326328
case "date":
327329
return "Date"
328330
case "date32":
@@ -338,12 +340,19 @@ func ydbBuilderMethodForColumnType(dbType string) string {
338340
case "tztimestamp":
339341
return "TzTimestamp"
340342
case "uuid":
341-
return "UUID"
343+
return "Uuid"
342344
case "yson":
343345
return "YSON"
344346

347+
case "integer": // LIMIT/OFFSET parameters support
348+
return "Uint64"
349+
345350
//TODO: support other types
346351
default:
352+
// Check for decimal types
353+
if strings.HasPrefix(baseType, "decimal") {
354+
return "Decimal"
355+
}
347356
return ""
348357
}
349358
}
@@ -405,6 +414,21 @@ func (v QueryValue) YDBParamsBuilder() string {
405414
isPtr := strings.HasPrefix(goType, "*")
406415
isArray := field.Column.IsArray || field.Column.IsSqlcSlice
407416

417+
if method == "Decimal" {
418+
if isArray {
419+
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
420+
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))
421+
lines = append(lines, "\t\tlist = list.Add().Decimal(param.Bytes, param.Precision, param.Scale)")
422+
lines = append(lines, "\t}")
423+
lines = append(lines, "\tparameters = list.EndList()")
424+
} else if isPtr {
425+
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).BeginOptional().Decimal(&%s.Bytes, %s.Precision, %s.Scale).EndOptional()", paramName, variable, variable, variable))
426+
} else {
427+
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).Decimal(%s.Bytes, %s.Precision, %s.Scale)", paramName, variable, variable, variable))
428+
}
429+
return true
430+
}
431+
408432
if isArray {
409433
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
410434
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
125125
{{end}}
126126

127127
{{define "queryCodeStdExec"}}
128-
{{- if .Arg.HasSqlcSlices }}
128+
{{- if shouldExpandSlices . }}
129129
query := {{.ConstantName}}
130130
var queryParams []interface{}
131131
{{- if .Arg.Struct }}

internal/codegen/golang/templates/ydb-go-sdk/interfaceCode.tmpl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
{{- $dbtxParam := .EmitMethodsWithDBArgument -}}
44
{{- range .GoQueries}}
55
{{- if and (eq .Cmd ":one") ($dbtxParam) }}
6-
{{range .Comments}}//{{.}}
7-
{{end -}}
6+
{{- range .Comments}}//{{.}}
7+
{{- end}}
88
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ({{.Ret.DefineType}}, error)
99
{{- else if eq .Cmd ":one"}}
10-
{{range .Comments}}//{{.}}
11-
{{end -}}
10+
{{- range .Comments}}//{{.}}
11+
{{- end}}
1212
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ({{.Ret.DefineType}}, error)
1313
{{- end}}
1414
{{- if and (eq .Cmd ":many") ($dbtxParam) }}
15-
{{range .Comments}}//{{.}}
16-
{{end -}}
15+
{{- range .Comments}}//{{.}}
16+
{{- end}}
1717
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ([]{{.Ret.DefineType}}, error)
1818
{{- else if eq .Cmd ":many"}}
1919
{{range .Comments}}//{{.}}
2020
{{end -}}
2121
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) ([]{{.Ret.DefineType}}, error)
2222
{{- end}}
2323
{{- if and (eq .Cmd ":exec") ($dbtxParam) }}
24-
{{range .Comments}}//{{.}}
25-
{{end -}}
24+
{{- range .Comments}}//{{.}}
25+
{{- end}}
2626
{{.MethodName}}(ctx context.Context, db DBTX, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) error
2727
{{- else if eq .Cmd ":exec"}}
28-
{{range .Comments}}//{{.}}
29-
{{end -}}
28+
{{- range .Comments}}//{{.}}
29+
{{- end}}
3030
{{.MethodName}}(ctx context.Context, {{if not .Arg.IsEmpty}}{{.Arg.Pair}}, {{end}}opts ...query.ExecuteOption) error
3131
{{- end}}
3232
{{- end}}

internal/codegen/golang/ydb_type.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
4343
// // use the smallest type they have which is NullInt16
4444
// return "sql.NullInt16"
4545
return "*int8"
46-
case "int16":
46+
case "int16", "smallint":
4747
if notNull {
4848
return "int16"
4949
}
@@ -52,7 +52,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
5252
}
5353
// return "sql.NullInt16"
5454
return "*int16"
55-
case "int", "int32": //ydb doesn't have int type, but we need it to support untyped constants
55+
case "int", "int32", "integer":
5656
if notNull {
5757
return "int32"
5858
}
@@ -61,7 +61,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
6161
}
6262
// return "sql.NullInt32"
6363
return "*int32"
64-
case "int64":
64+
case "int64", "bigint":
6565
if notNull {
6666
return "int64"
6767
}
@@ -72,25 +72,37 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
7272
return "*int64"
7373

7474
case "uint8":
75+
if notNull {
76+
return "uint8"
77+
}
7578
if emitPointersForNull {
7679
return "*uint8"
7780
}
78-
return "uint8"
81+
return "*uint8"
7982
case "uint16":
83+
if notNull {
84+
return "uint16"
85+
}
8086
if emitPointersForNull {
8187
return "*uint16"
8288
}
83-
return "uint16"
89+
return "*uint16"
8490
case "uint32":
91+
if notNull {
92+
return "uint32"
93+
}
8594
if emitPointersForNull {
8695
return "*uint32"
8796
}
88-
return "uint32"
97+
return "*uint32"
8998
case "uint64":
99+
if notNull {
100+
return "uint64"
101+
}
90102
if emitPointersForNull {
91103
return "*uint64"
92104
}
93-
return "uint64"
105+
return "*uint64"
94106

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

116128
// string types
117-
case "string", "utf8", "text":
129+
case "utf8", "text":
118130
if notNull {
119131
return "string"
120132
}
@@ -163,7 +175,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
163175
}
164176
return "*string"
165177

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

187+
case "interval", "interval64":
188+
if notNull {
189+
return "time.Duration"
190+
}
191+
if emitPointersForNull {
192+
return "*time.Duration"
193+
}
194+
return "*time.Duration"
195+
175196
case "uuid":
176197
if notNull {
177198
return "uuid.UUID"
@@ -181,7 +202,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
181202
}
182203
return "*uuid.UUID"
183204

184-
case "yson":
205+
case "yson", "string":
185206
if notNull {
186207
return "[]byte"
187208
}
@@ -198,6 +219,17 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
198219
return "interface{}"
199220

200221
default:
222+
if strings.HasPrefix(columnType, "decimal") {
223+
if notNull {
224+
return "types.Decimal"
225+
}
226+
227+
if emitPointersForNull {
228+
return "*types.Decimal"
229+
}
230+
return "*types.Decimal"
231+
}
232+
201233
if debug.Active {
202234
log.Printf("unknown YDB type: %s\n", columnType)
203235
}

internal/compiler/resolve.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log/slog"
66
"strconv"
77

8+
"github.com/sqlc-dev/sqlc/internal/config"
89
"github.com/sqlc-dev/sqlc/internal/sql/ast"
910
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
1011
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
@@ -118,11 +119,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
118119
case *limitOffset:
119120
defaultP := named.NewInferredParam("offset", true)
120121
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
122+
123+
dataType := "integer"
124+
if comp.conf.Engine == config.EngineYDB {
125+
dataType = "uint64"
126+
}
127+
121128
a = append(a, Parameter{
122129
Number: ref.ref.Number,
123130
Column: &Column{
124131
Name: p.Name(),
125-
DataType: "integer",
132+
DataType: dataType,
126133
NotNull: p.NotNull(),
127134
IsNamedParam: isNamed,
128135
},
@@ -131,11 +138,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
131138
case *limitCount:
132139
defaultP := named.NewInferredParam("limit", true)
133140
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
141+
142+
dataType := "integer"
143+
if comp.conf.Engine == config.EngineYDB {
144+
dataType = "uint64"
145+
}
146+
134147
a = append(a, Parameter{
135148
Number: ref.ref.Number,
136149
Column: &Column{
137150
Name: p.Name(),
138-
DataType: "integer",
151+
DataType: dataType,
139152
NotNull: p.NotNull(),
140153
IsNamedParam: isNamed,
141154
},

0 commit comments

Comments
 (0)