Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e949b19
Initial YDB support: SELECT logic, some convert tests, and YDB engine…
1NepuNep1 Apr 22, 2025
4e46daa
Added almost full INSERT support and basic DELETE (without ON) support
1NepuNep1 Apr 26, 2025
3a4f818
Added UPDATE and DELETE full logic. Updated examples in example/authors
1NepuNep1 Apr 28, 2025
e980d19
Added some examples and README for demo
1NepuNep1 Apr 28, 2025
3013c84
Supported some more YQL constructions:
1NepuNep1 May 17, 2025
e687cf6
ADDED DROP/ALTER USER/GROUP
1NepuNep1 May 19, 2025
0293f6a
Added Funcs support!
1NepuNep1 May 20, 2025
fc8c932
Added examples for funcs codegen
1NepuNep1 May 21, 2025
d0e1550
Upgraded jwt to 4.5.2
1NepuNep1 May 27, 2025
0b1752a
First ydb-go-sdk generation version (See #4) (#6)
1NepuNep1 Sep 2, 2025
4eb8ff5
Bump sqlc version in examples/authors/ydb to v1.30.0
Sep 3, 2025
14b60c5
Rewrited comments and testdata to eng
Sep 3, 2025
f2d7aea
Expanding YQL Syntax support in sqlc engine (Refactored SELECT (Suppo…
1NepuNep1 Sep 8, 2025
296cca7
Made new params building logic & got rid off .Query handle (#11)
1NepuNep1 Sep 9, 2025
1379e43
Rewrited convert to Visitor Interface from ANTLR4 + fixed some types …
1NepuNep1 Sep 23, 2025
a680b1b
Massive Optional + Functions update (#13)
1NepuNep1 Sep 26, 2025
a69429c
Added simple types in param builder and ydbType (#14)
1NepuNep1 Oct 8, 2025
816eda8
Ydb-go-sdk codegen: Containers support in ydb.ParamsBuilder() (#15)
1NepuNep1 Oct 9, 2025
2eaa5d0
Tests: Added a lot of ydb e2e tests + some engine and types fixes
1NepuNep1 Oct 27, 2025
bde9d5e
Merge branch 'main' into ydb
1NepuNep1 Oct 27, 2025
08424e0
Resolved go.mod && go.sum
1NepuNep1 Oct 27, 2025
be13a37
Fixed builtins test
1NepuNep1 Oct 27, 2025
683926c
Added docs and rewrited examples for ydb (#17)
1NepuNep1 Oct 27, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Prev Previous commit
Next Next commit
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
  • Loading branch information
1NepuNep1 authored Oct 27, 2025
commit 2eaa5d08dc6744940bc02f3b93c6dde8e2220e6b
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