Skip to content

Commit 4a05999

Browse files
authored
Use wazero-based libpgquery wrapper in currently disabled environments (#3027)
* Use wazero-based wrapper of libpgquery when cgo is disabled or on windows
1 parent 7370598 commit 4a05999

File tree

19 files changed

+115
-137
lines changed

19 files changed

+115
-137
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,24 @@ on:
55
- main
66
pull_request:
77
jobs:
8-
windows-build:
9-
if: ${{ github.ref == 'refs/heads/main' }}
10-
runs-on: windows-latest
11-
steps:
12-
- uses: actions/checkout@v4
13-
- uses: actions/setup-go@v4
14-
with:
15-
go-version-file: go.mod
16-
check-latest: true
17-
- run: go build ./...
18-
19-
darwin-build:
20-
if: ${{ github.ref == 'refs/heads/main' }}
21-
runs-on: macos-latest
22-
steps:
23-
- uses: actions/checkout@v4
24-
- uses: actions/setup-go@v4
25-
with:
26-
go-version-file: go.mod
27-
check-latest: true
28-
- run: go build ./...
29-
30-
build:
31-
name: test
32-
runs-on: ubuntu-latest
8+
test:
9+
strategy:
10+
matrix:
11+
os: [ubuntu-latest, macos-latest, windows-latest]
12+
cgo: ['1', '0']
13+
# Workaround no native support for conditional matrix items
14+
# https://github.com/orgs/community/discussions/26253#discussioncomment-6745038
15+
isMain:
16+
- ${{ github.ref == 'refs/heads/main' }}
17+
exclude:
18+
- isMain: false
19+
include:
20+
- os: ubuntu-latest
21+
cgo: '1'
22+
- os: ubuntu-latest
23+
cgo: '0'
24+
name: test ${{ matrix.os }} cgo=${{ matrix.cgo }}
25+
runs-on: ${{ matrix.os }}
3326

3427
steps:
3528
- uses: actions/checkout@v4
@@ -46,17 +39,22 @@ jobs:
4639

4740
- name: install ./...
4841
run: go install ./...
42+
env:
43+
CGO_ENABLED: ${{ matrix.cgo }}
4944

5045
- name: build internal/endtoend
5146
run: go build ./...
5247
working-directory: internal/endtoend/testdata
48+
env:
49+
CGO_ENABLED: ${{ matrix.cgo }}
5350

5451
- name: test ./...
5552
run: gotestsum --junitfile junit.xml -- --tags=examples -timeout 20m ./...
5653
env:
5754
CI_SQLC_PROJECT_ID: ${{ secrets.CI_SQLC_PROJECT_ID }}
5855
CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
5956
SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
57+
CGO_ENABLED: ${{ matrix.cgo }}
6058

6159
vuln_check:
6260
runs-on: ubuntu-latest

examples/authors/sqlite/db_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build examples
2-
// +build examples
1+
//go:build examples && cgo
2+
// +build examples,cgo
33

44
package authors
55

examples/booktest/sqlite/db_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build examples
2-
// +build examples
1+
//go:build examples && cgo
2+
// +build examples,cgo
33

44
package booktest
55

examples/ondeck/sqlite/db_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build examples
2-
// +build examples
1+
//go:build examples && cgo
2+
// +build examples,cgo
33

44
package ondeck
55

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ require (
1515
github.com/jinzhu/inflection v1.0.0
1616
github.com/lib/pq v1.10.9
1717
github.com/mattn/go-sqlite3 v1.14.18
18-
github.com/pganalyze/pg_query_go/v4 v4.2.3
18+
github.com/pganalyze/pg_query_go/v4 v4.2.4-0.20231205012101-7463430c7b73
1919
github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1
2020
github.com/riza-io/grpc-go v0.2.0
2121
github.com/spf13/cobra v1.8.0
2222
github.com/spf13/pflag v1.0.5
23+
github.com/wasilibs/go-pgquery v0.0.0-20231205013331-96e794bb074e
2324
github.com/xeipuuv/gojsonschema v1.2.0
2425
golang.org/x/sync v0.5.0
2526
google.golang.org/grpc v1.59.0
2627
google.golang.org/protobuf v1.31.0
2728
gopkg.in/yaml.v3 v3.0.1
2829
)
2930

31+
require github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6 // indirect
32+
3033
require (
3134
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
3235
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect

go.sum

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,11 @@ github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
2929
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
3030
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
3131
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
32-
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
33-
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
34-
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
35-
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
36-
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
37-
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
3832
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
3933
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
4034
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
4135
github.com/google/cel-go v0.18.2 h1:L0B6sNBSVmt0OyECi8v6VOS74KOc9W/tLiWKfZABvf4=
4236
github.com/google/cel-go v0.18.2/go.mod h1:kWcIzTsPX0zmQ+H3TirHstLLf9ep5QTsZBN9u4dOYLg=
43-
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
44-
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
45-
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
46-
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4737
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4838
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4939
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -129,8 +119,8 @@ github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
129119
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
130120
github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
131121
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
132-
github.com/pganalyze/pg_query_go/v4 v4.2.3 h1:cNLqyiVMasV7YGWyYV+fkXyHp32gDfXVNCqoHztEGNk=
133-
github.com/pganalyze/pg_query_go/v4 v4.2.3/go.mod h1:aEkDNOXNM5j0YGzaAapwJ7LB3dLNj+bvbWcLv1hOVqA=
122+
github.com/pganalyze/pg_query_go/v4 v4.2.4-0.20231205012101-7463430c7b73 h1:vZEujakYrBzd7CWMwqTsDRU7bdRm4Aiu76X9K3P+qtI=
123+
github.com/pganalyze/pg_query_go/v4 v4.2.4-0.20231205012101-7463430c7b73/go.mod h1:pRJ9rCcWIhDLAzymhH2iY5kEVEfU6UGrL1wBWuO7gmM=
134124
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
135125
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
136126
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 h1:+FZIDR/D97YOPik4N4lPDaUcLDF/EQPogxtlHB2ZZRM=
@@ -185,6 +175,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
185175
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
186176
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
187177
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
178+
github.com/wasilibs/go-pgquery v0.0.0-20231205013331-96e794bb074e h1:sGIC6/D0KqpA+qBSDSVDQswU/IJVYkbnUXnipgTLQWk=
179+
github.com/wasilibs/go-pgquery v0.0.0-20231205013331-96e794bb074e/go.mod h1:KW0azBSWqkPZ71r+3O4qt8h6A/NisFLp0rbjZ3py4OE=
180+
github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6 h1:jwbU8u5TuXModzdEG4wI0g4FyuD7ROSttU86go5sPdU=
181+
github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6/go.mod h1:IQNVyA4d1hWIe23mlMMuqXjyWMdndgSlNx6FqBkwPsM=
188182
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
189183
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
190184
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
@@ -303,12 +297,6 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:
303297
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
304298
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
305299
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
306-
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
307-
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
308-
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
309-
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
310-
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
311-
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
312300
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
313301
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
314302
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=

internal/codegen/sdk/utils.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ func Title(s string) string {
2323
// a backtick, replace it the following way:
2424
//
2525
// input:
26-
// SELECT `group` FROM foo
26+
//
27+
// SELECT `group` FROM foo
2728
//
2829
// output:
29-
// SELECT ` + "`" + `group` + "`" + ` FROM foo
3030
//
31-
// The escaped string must be rendered inside an existing string literal
31+
// SELECT ` + "`" + `group` + "`" + ` FROM foo
32+
//
33+
// # The escaped string must be rendered inside an existing string literal
3234
//
3335
// A string cannot be escaped twice
3436
func EscapeBacktick(s string) string {

internal/endtoend/endtoend_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Currently requires cgo for wasmtime and has line-ending issues on windows.
2+
//go:build cgo && !windows
3+
// +build cgo,!windows
4+
15
package main
26

37
import (

internal/endtoend/fmt_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99
"testing"
1010

11-
pg_query "github.com/pganalyze/pg_query_go/v4"
1211
"github.com/sqlc-dev/sqlc/internal/debug"
1312
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
1413
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -40,7 +39,7 @@ func TestFormat(t *testing.T) {
4039
}
4140
query := query
4241
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
43-
expected, err := pg_query.Fingerprint(string(query))
42+
expected, err := postgresql.Fingerprint(string(query))
4443
if err != nil {
4544
t.Fatal(err)
4645
}
@@ -52,12 +51,12 @@ func TestFormat(t *testing.T) {
5251
t.Fatal("expected one statement")
5352
}
5453
if false {
55-
r, err := pg_query.Parse(string(query))
54+
r, err := postgresql.Parse(string(query))
5655
debug.Dump(r, err)
5756
}
5857

5958
out := ast.Format(stmts[0].Raw)
60-
actual, err := pg_query.Fingerprint(out)
59+
actual, err := postgresql.Fingerprint(out)
6160
if err != nil {
6261
t.Error(err)
6362
}

internal/engine/postgresql/convert.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build !windows && cgo
2-
// +build !windows,cgo
3-
41
package postgresql
52

63
import (

internal/engine/postgresql/parse.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build !windows && cgo
2-
// +build !windows,cgo
3-
41
package postgresql
52

63
import (
@@ -10,8 +7,8 @@ import (
107
"strings"
118

129
nodes "github.com/pganalyze/pg_query_go/v4"
13-
"github.com/pganalyze/pg_query_go/v4/parser"
1410

11+
"github.com/sqlc-dev/sqlc/internal/engine/postgresql/parser"
1512
"github.com/sqlc-dev/sqlc/internal/source"
1613
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1714
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
@@ -154,7 +151,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
154151
if err != nil {
155152
return nil, err
156153
}
157-
tree, err := nodes.Parse(string(contents))
154+
tree, err := Parse(string(contents))
158155
if err != nil {
159156
pErr := normalizeErr(err)
160157
return nil, pErr
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !windows && cgo
2+
// +build !windows,cgo
3+
4+
package postgresql
5+
6+
import (
7+
nodes "github.com/pganalyze/pg_query_go/v4"
8+
)
9+
10+
var Parse = nodes.Parse
11+
var Fingerprint = nodes.Fingerprint

internal/engine/postgresql/parse_disabled.go

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build windows || !cgo
2+
// +build windows !cgo
3+
4+
package postgresql
5+
6+
import (
7+
nodes "github.com/wasilibs/go-pgquery"
8+
)
9+
10+
var Parse = nodes.Parse
11+
var Fingerprint = nodes.Fingerprint
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !windows && cgo
2+
// +build !windows,cgo
3+
4+
package parser
5+
6+
import "github.com/pganalyze/pg_query_go/v4/parser"
7+
8+
type Error = parser.Error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build windows || !cgo
2+
// +build windows !cgo
3+
4+
package parser
5+
6+
import "github.com/wasilibs/go-pgquery/parser"
7+
8+
type Error = parser.Error

internal/engine/postgresql/utils.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build !windows && cgo
2-
// +build !windows,cgo
3-
41
package postgresql
52

63
import (

internal/sql/sqlpath/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Glob(patterns []string) ([]string, error) {
4646
files = append(files, filepath.Join(path, f.Name()))
4747
}
4848
} else {
49-
files = append(files, path)
49+
files = append(files, filepath.Clean(path))
5050
}
5151
}
5252
var sqlFiles []string

0 commit comments

Comments
 (0)