From 9b4e5733e16ca14203bd9f7e8f22b9346a294c6c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 17 Aug 2026 16:18:30 +0000 Subject: [PATCH 1/2] postgresql: replace pg_query_go with sqlc-dev/oliphant Swap github.com/pganalyze/pg_query_go/v6 for github.com/sqlc-dev/oliphant, a pure Go drop-in replacement with the same API. Since it needs neither cgo nor wasm, the cgo/wasi build-tag split collapses: parse_default.go, parse_wasi.go and the engine's parser alias package are gone, along with the wasilibs/go-pgquery fallback dependency. Oliphant tracks libpg_query 18, which moved RETURNING lists into a ReturningClause message, renamed RowCompareExpr.rctype to cmptype, typed IntoClause.view_query as Query, and changed stmt_location to point at a statement's first token instead of the end of the previous statement. convert.go adapts to the new shapes, and Parse restores the pre-18 location convention the compiler expects, so statements keep carrying their leading '-- name:' comments and an unterminated final statement is still reported via a zero length. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01F4xWkw3nC1NQdjfNm8Mgyt --- go.mod | 6 ++-- go.sum | 12 +++---- internal/engine/postgresql/convert.go | 16 +++++---- internal/engine/postgresql/parse.go | 34 ++++++++++++++++--- internal/engine/postgresql/parse_default.go | 10 ------ internal/engine/postgresql/parse_wasi.go | 10 ------ .../postgresql/parser/parser_default.go | 7 ---- .../engine/postgresql/parser/parser_wasi.go | 7 ---- internal/engine/postgresql/utils.go | 2 +- 9 files changed, 47 insertions(+), 57 deletions(-) delete mode 100644 internal/engine/postgresql/parse_default.go delete mode 100644 internal/engine/postgresql/parse_wasi.go delete mode 100644 internal/engine/postgresql/parser/parser_default.go delete mode 100644 internal/engine/postgresql/parser/parser_wasi.go diff --git a/go.mod b/go.mod index 1e1cee395b..d2cf90b4ee 100644 --- a/go.mod +++ b/go.mod @@ -15,20 +15,19 @@ require ( github.com/jinzhu/inflection v1.0.0 github.com/lib/pq v1.12.3 github.com/ncruces/go-sqlite3 v0.35.3 - github.com/pganalyze/pg_query_go/v6 v6.2.2 github.com/riza-io/grpc-go v0.2.0 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 github.com/sqlc-dev/doubleclick v1.0.0 github.com/sqlc-dev/marino v0.1.0 github.com/sqlc-dev/meyer v0.1.1 + github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2 github.com/sqlc-dev/zetajones v0.1.0 github.com/tetratelabs/wazero v1.12.0 - github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07 github.com/xeipuuv/gojsonschema v1.2.0 golang.org/x/sync v0.22.0 google.golang.org/grpc v1.83.0 - google.golang.org/protobuf v1.36.11 + google.golang.org/protobuf v1.36.12 gopkg.in/yaml.v3 v3.0.1 ) @@ -44,7 +43,6 @@ require ( github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304 // indirect github.com/ncruces/julianday v1.0.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect - github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect diff --git a/go.sum b/go.sum index 57e9e815fb..8528730c9d 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,6 @@ github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304 h1:5NoQAewtgKNK3G4bjNPxVoGXu6F6 github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304/go.mod h1:o8gr9w/50fXA5TDskg6bNUjvqmFfw4KaXth4q+yDSjg= github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M= github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= -github.com/pganalyze/pg_query_go/v6 v6.2.2 h1:O0L6zMC226R82RF3X5n0Ki6HjytDsoAzuzp4ATVAHNo= -github.com/pganalyze/pg_query_go/v6 v6.2.2/go.mod h1:Cn6+j4870kJz3iYNsb0VsNG04vpSWgEvBwc590J4qD0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/riza-io/grpc-go v0.2.0 h1:2HxQKFVE7VuYstcJ8zqpN84VnAoJ4dCL6YFhJewNcHQ= @@ -73,6 +71,8 @@ github.com/sqlc-dev/marino v0.1.0 h1:8Fn13vFhx7OUcmDFfRZdf3zARAbNl04Lcy74211ZpIw github.com/sqlc-dev/marino v0.1.0/go.mod h1:mQxC2dgDE0DWHMb2B5jZNk7KToJuS6wnxnffBfYnq08= github.com/sqlc-dev/meyer v0.1.1 h1:BAeZcfgLyTnk9f90DyGEKXPrHxtgvVD/DTM6awq2kUY= github.com/sqlc-dev/meyer v0.1.1/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8= +github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2 h1:2EL2CIeFA4/ch+fM115rR28oiDr9FeP8GADaCSs3Dtw= +github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2/go.mod h1:fRM/t4FutRddTIq2YCuS4O9o+2rRwSwELRvLMqtPloo= github.com/sqlc-dev/zetajones v0.1.0 h1:VeG0atx6lNABr9V2bSI5vL9DvOKTHX0XjMqWUE/rv40= github.com/sqlc-dev/zetajones v0.1.0/go.mod h1:dU1DxwqC6Cahbpnw16KpH1J2waWRDMdwyDSvovMZR4I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -82,10 +82,6 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU= github.com/tetratelabs/wazero v1.12.0/go.mod h1:LvKtzl2RqO4gyF27BiXU+nKAjcV8f38U+kP/q2vgxh0= -github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07 h1:mJdDDPblDfPe7z7go8Dvv1AJQDI3eQ/5xith3q2mFlo= -github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07/go.mod h1:Ak17IJ037caFp4jpCw/iQQ7/W74Sqpb1YuKJU6HTKfM= -github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 h1:OvLBa8SqJnZ6P+mjlzc2K7PM22rRUPE1x32G9DTPrC4= -github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52/go.mod h1:jMeV4Vpbi8osrE/pKUxRZkVaA0EX7NZN0A9/oRzgpgY= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= @@ -124,8 +120,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= google.golang.org/grpc v1.83.0 h1:JeNZEKJFbQxArAMl+hiytHauacDNqJUllNfmIMmpqnQ= google.golang.org/grpc v1.83.0/go.mod h1:kDyl6SKsiHKt0uylY5gtn5cEjkrIOhQOGDgIc4JGwzQ= -google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= -google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/internal/engine/postgresql/convert.go b/internal/engine/postgresql/convert.go index 321294c59e..6a71c609dc 100644 --- a/internal/engine/postgresql/convert.go +++ b/internal/engine/postgresql/convert.go @@ -3,7 +3,7 @@ package postgresql import ( "fmt" - pg "github.com/pganalyze/pg_query_go/v6" + pg "github.com/sqlc-dev/oliphant" "github.com/sqlc-dev/sqlc/internal/sql/ast" ) @@ -1424,7 +1424,7 @@ func convertDeleteStmt(n *pg.DeleteStmt) *ast.DeleteStmt { }, UsingClause: convertSlice(n.UsingClause), WhereClause: convertNode(n.WhereClause), - ReturningList: convertSlice(n.ReturningList), + ReturningList: convertSlice(n.ReturningClause.GetExprs()), WithClause: convertWithClause(n.WithClause), } } @@ -1806,7 +1806,7 @@ func convertInsertStmt(n *pg.InsertStmt) *ast.InsertStmt { Cols: convertSlice(n.Cols), SelectStmt: convertNode(n.SelectStmt), OnConflictClause: convertOnConflictClause(n.OnConflictClause), - ReturningList: convertSlice(n.ReturningList), + ReturningList: convertSlice(n.ReturningClause.GetExprs()), WithClause: convertWithClause(n.WithClause), Override: ast.OverridingKind(n.Override), } @@ -1825,13 +1825,17 @@ func convertIntoClause(n *pg.IntoClause) *ast.IntoClause { if n == nil { return nil } + var viewQuery ast.Node = &ast.TODO{} + if n.ViewQuery != nil { + viewQuery = convertQuery(n.ViewQuery) + } return &ast.IntoClause{ Rel: convertRangeVar(n.Rel), ColNames: convertSlice(n.ColNames), Options: convertSlice(n.Options), OnCommit: ast.OnCommitAction(n.OnCommit), TableSpaceName: makeString(n.TableSpaceName), - ViewQuery: convertNode(n.ViewQuery), + ViewQuery: viewQuery, SkipData: n.SkipData, } } @@ -2434,7 +2438,7 @@ func convertRowCompareExpr(n *pg.RowCompareExpr) *ast.RowCompareExpr { } return &ast.RowCompareExpr{ Xpr: convertNode(n.Xpr), - Rctype: ast.RowCompareType(n.Rctype), + Rctype: ast.RowCompareType(n.Cmptype), Opnos: convertSlice(n.Opnos), Opfamilies: convertSlice(n.Opfamilies), Inputcollids: convertSlice(n.Inputcollids), @@ -2808,7 +2812,7 @@ func convertUpdateStmt(n *pg.UpdateStmt) *ast.UpdateStmt { TargetList: convertSlice(n.TargetList), WhereClause: convertNode(n.WhereClause), FromClause: convertSlice(n.FromClause), - ReturningList: convertSlice(n.ReturningList), + ReturningList: convertSlice(n.ReturningClause.GetExprs()), WithClause: convertWithClause(n.WithClause), } } diff --git a/internal/engine/postgresql/parse.go b/internal/engine/postgresql/parse.go index 0c6b3a0fc2..bb4c0d2a0a 100644 --- a/internal/engine/postgresql/parse.go +++ b/internal/engine/postgresql/parse.go @@ -6,14 +6,17 @@ import ( "io" "strings" - nodes "github.com/pganalyze/pg_query_go/v6" + nodes "github.com/sqlc-dev/oliphant" + "github.com/sqlc-dev/oliphant/parser" - "github.com/sqlc-dev/sqlc/internal/engine/postgresql/parser" "github.com/sqlc-dev/sqlc/internal/source" "github.com/sqlc-dev/sqlc/internal/sql/ast" "github.com/sqlc-dev/sqlc/internal/sql/sqlerr" ) +var Parse = nodes.Parse +var Fingerprint = nodes.Fingerprint + func stringSlice(list *nodes.List) []string { items := []string{} for _, item := range list.Items { @@ -158,7 +161,30 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { } var stmts []ast.Statement + // PostgreSQL 18 changed stmt_location to point at the statement's first + // token. The rest of the compiler expects the earlier convention, where a + // statement begins where the previous one ended and so carries its leading + // comments (that's where `-- name:` lives). + loc := 0 for _, raw := range tree.Stmts { + start := loc + end := int(raw.StmtLocation) + int(raw.StmtLen) + next := end + for next < len(contents) && contents[next] != ';' { + next++ + } + terminated := next < len(contents) + if terminated { + next++ + } + loc = next + length := end - start + if !terminated { + // A zero length marked an unterminated final statement before + // PostgreSQL 18, and the compiler still reports it that way. + length = 0 + } + n, err := translate(raw.Stmt) if err == errSkip { continue @@ -172,8 +198,8 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { stmts = append(stmts, ast.Statement{ Raw: &ast.RawStmt{ Stmt: n, - StmtLocation: int(raw.StmtLocation), - StmtLen: int(raw.StmtLen), + StmtLocation: start, + StmtLen: length, }, }) } diff --git a/internal/engine/postgresql/parse_default.go b/internal/engine/postgresql/parse_default.go deleted file mode 100644 index 272f189649..0000000000 --- a/internal/engine/postgresql/parse_default.go +++ /dev/null @@ -1,10 +0,0 @@ -//go:build !windows && cgo - -package postgresql - -import ( - nodes "github.com/pganalyze/pg_query_go/v6" -) - -var Parse = nodes.Parse -var Fingerprint = nodes.Fingerprint diff --git a/internal/engine/postgresql/parse_wasi.go b/internal/engine/postgresql/parse_wasi.go deleted file mode 100644 index 377b812cdb..0000000000 --- a/internal/engine/postgresql/parse_wasi.go +++ /dev/null @@ -1,10 +0,0 @@ -//go:build windows || !cgo - -package postgresql - -import ( - nodes "github.com/wasilibs/go-pgquery" -) - -var Parse = nodes.Parse -var Fingerprint = nodes.Fingerprint diff --git a/internal/engine/postgresql/parser/parser_default.go b/internal/engine/postgresql/parser/parser_default.go deleted file mode 100644 index 706c3bb097..0000000000 --- a/internal/engine/postgresql/parser/parser_default.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !windows && cgo - -package parser - -import "github.com/pganalyze/pg_query_go/v6/parser" - -type Error = parser.Error diff --git a/internal/engine/postgresql/parser/parser_wasi.go b/internal/engine/postgresql/parser/parser_wasi.go deleted file mode 100644 index e172d36c72..0000000000 --- a/internal/engine/postgresql/parser/parser_wasi.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build windows || !cgo - -package parser - -import "github.com/wasilibs/go-pgquery/parser" - -type Error = parser.Error diff --git a/internal/engine/postgresql/utils.go b/internal/engine/postgresql/utils.go index 0f4be00634..3dbe6d8ba9 100644 --- a/internal/engine/postgresql/utils.go +++ b/internal/engine/postgresql/utils.go @@ -1,7 +1,7 @@ package postgresql import ( - nodes "github.com/pganalyze/pg_query_go/v6" + nodes "github.com/sqlc-dev/oliphant" ) func isArray(n *nodes.TypeName) bool { From e89b6cf90413f8d8feb1129a4d02bd2a66a2fe8e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 17 Aug 2026 17:03:14 +0000 Subject: [PATCH 2/2] build(deps): pin sqlc-dev/oliphant to v0.1.0 Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01F4xWkw3nC1NQdjfNm8Mgyt --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d2cf90b4ee..c9db1b3408 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/sqlc-dev/doubleclick v1.0.0 github.com/sqlc-dev/marino v0.1.0 github.com/sqlc-dev/meyer v0.1.1 - github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2 + github.com/sqlc-dev/oliphant v0.1.0 github.com/sqlc-dev/zetajones v0.1.0 github.com/tetratelabs/wazero v1.12.0 github.com/xeipuuv/gojsonschema v1.2.0 diff --git a/go.sum b/go.sum index 8528730c9d..e22b4e345b 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,8 @@ github.com/sqlc-dev/marino v0.1.0 h1:8Fn13vFhx7OUcmDFfRZdf3zARAbNl04Lcy74211ZpIw github.com/sqlc-dev/marino v0.1.0/go.mod h1:mQxC2dgDE0DWHMb2B5jZNk7KToJuS6wnxnffBfYnq08= github.com/sqlc-dev/meyer v0.1.1 h1:BAeZcfgLyTnk9f90DyGEKXPrHxtgvVD/DTM6awq2kUY= github.com/sqlc-dev/meyer v0.1.1/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8= -github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2 h1:2EL2CIeFA4/ch+fM115rR28oiDr9FeP8GADaCSs3Dtw= -github.com/sqlc-dev/oliphant v0.0.0-20260817154825-6aaeaa73d2c2/go.mod h1:fRM/t4FutRddTIq2YCuS4O9o+2rRwSwELRvLMqtPloo= +github.com/sqlc-dev/oliphant v0.1.0 h1:RAsO6BMitIzB2+swx/qzUR5nf6w4cQ1abgHIu+Fgppo= +github.com/sqlc-dev/oliphant v0.1.0/go.mod h1:fRM/t4FutRddTIq2YCuS4O9o+2rRwSwELRvLMqtPloo= github.com/sqlc-dev/zetajones v0.1.0 h1:VeG0atx6lNABr9V2bSI5vL9DvOKTHX0XjMqWUE/rv40= github.com/sqlc-dev/zetajones v0.1.0/go.mod h1:dU1DxwqC6Cahbpnw16KpH1J2waWRDMdwyDSvovMZR4I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=