Skip to content

Commit 2e0f745

Browse files
authored
postgresql: replace pg_query_go with sqlc-dev/oliphant (#4561)
1 parent 8a7cddf commit 2e0f745

9 files changed

Lines changed: 47 additions & 57 deletions

File tree

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ require (
1515
github.com/jinzhu/inflection v1.0.0
1616
github.com/lib/pq v1.12.3
1717
github.com/ncruces/go-sqlite3 v0.35.3
18-
github.com/pganalyze/pg_query_go/v6 v6.2.2
1918
github.com/riza-io/grpc-go v0.2.0
2019
github.com/spf13/cobra v1.10.2
2120
github.com/spf13/pflag v1.0.10
2221
github.com/sqlc-dev/doubleclick v1.0.0
2322
github.com/sqlc-dev/marino v0.1.0
2423
github.com/sqlc-dev/meyer v0.1.1
24+
github.com/sqlc-dev/oliphant v0.1.0
2525
github.com/sqlc-dev/zetajones v0.1.0
2626
github.com/tetratelabs/wazero v1.12.0
27-
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07
2827
github.com/xeipuuv/gojsonschema v1.2.0
2928
golang.org/x/sync v0.22.0
3029
google.golang.org/grpc v1.83.0
31-
google.golang.org/protobuf v1.36.11
30+
google.golang.org/protobuf v1.36.12
3231
gopkg.in/yaml.v3 v3.0.1
3332
)
3433

@@ -44,7 +43,6 @@ require (
4443
github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304 // indirect
4544
github.com/ncruces/julianday v1.0.0 // indirect
4645
github.com/rogpeppe/go-internal v1.10.0 // indirect
47-
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect
4846
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
4947
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
5048
go.yaml.in/yaml/v3 v3.0.4 // indirect

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304 h1:5NoQAewtgKNK3G4bjNPxVoGXu6F6
5353
github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304/go.mod h1:o8gr9w/50fXA5TDskg6bNUjvqmFfw4KaXth4q+yDSjg=
5454
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
5555
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
56-
github.com/pganalyze/pg_query_go/v6 v6.2.2 h1:O0L6zMC226R82RF3X5n0Ki6HjytDsoAzuzp4ATVAHNo=
57-
github.com/pganalyze/pg_query_go/v6 v6.2.2/go.mod h1:Cn6+j4870kJz3iYNsb0VsNG04vpSWgEvBwc590J4qD0=
5856
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5957
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6058
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
7371
github.com/sqlc-dev/marino v0.1.0/go.mod h1:mQxC2dgDE0DWHMb2B5jZNk7KToJuS6wnxnffBfYnq08=
7472
github.com/sqlc-dev/meyer v0.1.1 h1:BAeZcfgLyTnk9f90DyGEKXPrHxtgvVD/DTM6awq2kUY=
7573
github.com/sqlc-dev/meyer v0.1.1/go.mod h1:pS4USCRf/SLjWtaMcnTo4YrEEFKBj8CyyqlxcVUJQH8=
74+
github.com/sqlc-dev/oliphant v0.1.0 h1:RAsO6BMitIzB2+swx/qzUR5nf6w4cQ1abgHIu+Fgppo=
75+
github.com/sqlc-dev/oliphant v0.1.0/go.mod h1:fRM/t4FutRddTIq2YCuS4O9o+2rRwSwELRvLMqtPloo=
7676
github.com/sqlc-dev/zetajones v0.1.0 h1:VeG0atx6lNABr9V2bSI5vL9DvOKTHX0XjMqWUE/rv40=
7777
github.com/sqlc-dev/zetajones v0.1.0/go.mod h1:dU1DxwqC6Cahbpnw16KpH1J2waWRDMdwyDSvovMZR4I=
7878
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
8282
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
8383
github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU=
8484
github.com/tetratelabs/wazero v1.12.0/go.mod h1:LvKtzl2RqO4gyF27BiXU+nKAjcV8f38U+kP/q2vgxh0=
85-
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07 h1:mJdDDPblDfPe7z7go8Dvv1AJQDI3eQ/5xith3q2mFlo=
86-
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07/go.mod h1:Ak17IJ037caFp4jpCw/iQQ7/W74Sqpb1YuKJU6HTKfM=
87-
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 h1:OvLBa8SqJnZ6P+mjlzc2K7PM22rRUPE1x32G9DTPrC4=
88-
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52/go.mod h1:jMeV4Vpbi8osrE/pKUxRZkVaA0EX7NZN0A9/oRzgpgY=
8985
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
9086
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
9187
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:
124120
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
125121
google.golang.org/grpc v1.83.0 h1:JeNZEKJFbQxArAMl+hiytHauacDNqJUllNfmIMmpqnQ=
126122
google.golang.org/grpc v1.83.0/go.mod h1:kDyl6SKsiHKt0uylY5gtn5cEjkrIOhQOGDgIc4JGwzQ=
127-
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
128-
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
123+
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
124+
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
129125
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
130126
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
131127
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

internal/engine/postgresql/convert.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package postgresql
33
import (
44
"fmt"
55

6-
pg "github.com/pganalyze/pg_query_go/v6"
6+
pg "github.com/sqlc-dev/oliphant"
77

88
"github.com/sqlc-dev/sqlc/internal/sql/ast"
99
)
@@ -1424,7 +1424,7 @@ func convertDeleteStmt(n *pg.DeleteStmt) *ast.DeleteStmt {
14241424
},
14251425
UsingClause: convertSlice(n.UsingClause),
14261426
WhereClause: convertNode(n.WhereClause),
1427-
ReturningList: convertSlice(n.ReturningList),
1427+
ReturningList: convertSlice(n.ReturningClause.GetExprs()),
14281428
WithClause: convertWithClause(n.WithClause),
14291429
}
14301430
}
@@ -1806,7 +1806,7 @@ func convertInsertStmt(n *pg.InsertStmt) *ast.InsertStmt {
18061806
Cols: convertSlice(n.Cols),
18071807
SelectStmt: convertNode(n.SelectStmt),
18081808
OnConflictClause: convertOnConflictClause(n.OnConflictClause),
1809-
ReturningList: convertSlice(n.ReturningList),
1809+
ReturningList: convertSlice(n.ReturningClause.GetExprs()),
18101810
WithClause: convertWithClause(n.WithClause),
18111811
Override: ast.OverridingKind(n.Override),
18121812
}
@@ -1825,13 +1825,17 @@ func convertIntoClause(n *pg.IntoClause) *ast.IntoClause {
18251825
if n == nil {
18261826
return nil
18271827
}
1828+
var viewQuery ast.Node = &ast.TODO{}
1829+
if n.ViewQuery != nil {
1830+
viewQuery = convertQuery(n.ViewQuery)
1831+
}
18281832
return &ast.IntoClause{
18291833
Rel: convertRangeVar(n.Rel),
18301834
ColNames: convertSlice(n.ColNames),
18311835
Options: convertSlice(n.Options),
18321836
OnCommit: ast.OnCommitAction(n.OnCommit),
18331837
TableSpaceName: makeString(n.TableSpaceName),
1834-
ViewQuery: convertNode(n.ViewQuery),
1838+
ViewQuery: viewQuery,
18351839
SkipData: n.SkipData,
18361840
}
18371841
}
@@ -2434,7 +2438,7 @@ func convertRowCompareExpr(n *pg.RowCompareExpr) *ast.RowCompareExpr {
24342438
}
24352439
return &ast.RowCompareExpr{
24362440
Xpr: convertNode(n.Xpr),
2437-
Rctype: ast.RowCompareType(n.Rctype),
2441+
Rctype: ast.RowCompareType(n.Cmptype),
24382442
Opnos: convertSlice(n.Opnos),
24392443
Opfamilies: convertSlice(n.Opfamilies),
24402444
Inputcollids: convertSlice(n.Inputcollids),
@@ -2808,7 +2812,7 @@ func convertUpdateStmt(n *pg.UpdateStmt) *ast.UpdateStmt {
28082812
TargetList: convertSlice(n.TargetList),
28092813
WhereClause: convertNode(n.WhereClause),
28102814
FromClause: convertSlice(n.FromClause),
2811-
ReturningList: convertSlice(n.ReturningList),
2815+
ReturningList: convertSlice(n.ReturningClause.GetExprs()),
28122816
WithClause: convertWithClause(n.WithClause),
28132817
}
28142818
}

internal/engine/postgresql/parse.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"io"
77
"strings"
88

9-
nodes "github.com/pganalyze/pg_query_go/v6"
9+
nodes "github.com/sqlc-dev/oliphant"
10+
"github.com/sqlc-dev/oliphant/parser"
1011

11-
"github.com/sqlc-dev/sqlc/internal/engine/postgresql/parser"
1212
"github.com/sqlc-dev/sqlc/internal/source"
1313
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1414
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
1515
)
1616

17+
var Parse = nodes.Parse
18+
var Fingerprint = nodes.Fingerprint
19+
1720
func stringSlice(list *nodes.List) []string {
1821
items := []string{}
1922
for _, item := range list.Items {
@@ -158,7 +161,30 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
158161
}
159162

160163
var stmts []ast.Statement
164+
// PostgreSQL 18 changed stmt_location to point at the statement's first
165+
// token. The rest of the compiler expects the earlier convention, where a
166+
// statement begins where the previous one ended and so carries its leading
167+
// comments (that's where `-- name:` lives).
168+
loc := 0
161169
for _, raw := range tree.Stmts {
170+
start := loc
171+
end := int(raw.StmtLocation) + int(raw.StmtLen)
172+
next := end
173+
for next < len(contents) && contents[next] != ';' {
174+
next++
175+
}
176+
terminated := next < len(contents)
177+
if terminated {
178+
next++
179+
}
180+
loc = next
181+
length := end - start
182+
if !terminated {
183+
// A zero length marked an unterminated final statement before
184+
// PostgreSQL 18, and the compiler still reports it that way.
185+
length = 0
186+
}
187+
162188
n, err := translate(raw.Stmt)
163189
if err == errSkip {
164190
continue
@@ -172,8 +198,8 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
172198
stmts = append(stmts, ast.Statement{
173199
Raw: &ast.RawStmt{
174200
Stmt: n,
175-
StmtLocation: int(raw.StmtLocation),
176-
StmtLen: int(raw.StmtLen),
201+
StmtLocation: start,
202+
StmtLen: length,
177203
},
178204
})
179205
}

internal/engine/postgresql/parse_default.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/engine/postgresql/parse_wasi.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/engine/postgresql/parser/parser_default.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

internal/engine/postgresql/parser/parser_wasi.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

internal/engine/postgresql/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package postgresql
22

33
import (
4-
nodes "github.com/pganalyze/pg_query_go/v6"
4+
nodes "github.com/sqlc-dev/oliphant"
55
)
66

77
func isArray(n *nodes.TypeName) bool {

0 commit comments

Comments
 (0)