Skip to content

Use wazero-based libpgquery wrapper in currently disabled environments #3027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ jobs:
with:
go-version-file: go.mod
check-latest: true

# Currently some tests rely on line endings so we only build
# without running.
- run: go build ./...

darwin-build:
darwin-test:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized these may have intentionally been only building and not testing before since there doesn't seem to be a reason for darwin to otherwise fail them. Let me know if I should revert these, but I thought it could be good to verify these work in CI

anuraaga@315cb5a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I have also filed pganalyze/pg_query_go#103

if: ${{ github.ref == 'refs/heads/main' }}
runs-on: macos-latest
steps:
Expand All @@ -25,7 +28,34 @@ jobs:
with:
go-version-file: go.mod
check-latest: true
- run: go build ./...

- name: install sqlc-gen-test
run: go install github.com/sqlc-dev/sqlc-gen-test@v0.1.0

- name: install ./...
run: go install ./...

- run: go test ./...

linux-purego-test:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
check-latest: true

- name: install sqlc-gen-test
run: go install github.com/sqlc-dev/sqlc-gen-test@v0.1.0

- name: install ./...
run: go install ./...

- run: go test ./...
env:
CGO_ENABLED: '0'

build:
name: test
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ require (
github.com/riza-io/grpc-go v0.2.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/wasilibs/go-pgquery v0.0.0-20231201074715-bbab8b1ab4a9
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/sync v0.5.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
)

require github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6 // indirect

require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/wasilibs/go-pgquery v0.0.0-20231201074715-bbab8b1ab4a9 h1:JrDBgtPAsn6LC0EMn9jBaSFiZqVpKMa9hea47/nkaeA=
github.com/wasilibs/go-pgquery v0.0.0-20231201074715-bbab8b1ab4a9/go.mod h1:kalxVxSjXzPXnzVrIsejhYyPa81QLKHbNnNSB8bL6q0=
github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6 h1:jwbU8u5TuXModzdEG4wI0g4FyuD7ROSttU86go5sPdU=
github.com/wasilibs/wazerox v0.0.0-20231117065139-b3503f4aeff6/go.mod h1:IQNVyA4d1hWIe23mlMMuqXjyWMdndgSlNx6FqBkwPsM=
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=
Expand Down
8 changes: 5 additions & 3 deletions internal/codegen/sdk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ func Title(s string) string {
// a backtick, replace it the following way:
//
// input:
// SELECT `group` FROM foo
//
// SELECT `group` FROM foo
//
// output:
// SELECT ` + "`" + `group` + "`" + ` FROM foo
//
// The escaped string must be rendered inside an existing string literal
// SELECT ` + "`" + `group` + "`" + ` FROM foo
//
// # The escaped string must be rendered inside an existing string literal
//
// A string cannot be escaped twice
func EscapeBacktick(s string) string {
Expand Down
4 changes: 4 additions & 0 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Currently requires cgo for wasmtime.
//go:build cgo
// +build cgo

package main

import (
Expand Down
7 changes: 3 additions & 4 deletions internal/endtoend/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"testing"

pg_query "github.com/pganalyze/pg_query_go/v4"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
Expand Down Expand Up @@ -40,7 +39,7 @@ func TestFormat(t *testing.T) {
}
query := query
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
expected, err := pg_query.Fingerprint(string(query))
expected, err := postgresql.Fingerprint(string(query))
if err != nil {
t.Fatal(err)
}
Expand All @@ -52,12 +51,12 @@ func TestFormat(t *testing.T) {
t.Fatal("expected one statement")
}
if false {
r, err := pg_query.Parse(string(query))
r, err := postgresql.Parse(string(query))
debug.Dump(r, err)
}

out := ast.Format(stmts[0].Raw)
actual, err := pg_query.Fingerprint(out)
actual, err := postgresql.Fingerprint(out)
if err != nil {
t.Error(err)
}
Expand Down
5 changes: 1 addition & 4 deletions internal/engine/postgresql/convert.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
//go:build !windows && cgo
// +build !windows,cgo

package postgresql

import (
"fmt"

pg "github.com/pganalyze/pg_query_go/v4"
pg "github.com/wasilibs/go-pgquery"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
Expand Down
9 changes: 3 additions & 6 deletions internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !windows && cgo
// +build !windows,cgo

package postgresql

import (
Expand All @@ -9,9 +6,9 @@ import (
"io"
"strings"

nodes "github.com/pganalyze/pg_query_go/v4"
"github.com/pganalyze/pg_query_go/v4/parser"
nodes "github.com/wasilibs/go-pgquery"

"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"
Expand Down Expand Up @@ -154,7 +151,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
if err != nil {
return nil, err
}
tree, err := nodes.Parse(string(contents))
tree, err := parseNodes(string(contents))
if err != nil {
pErr := normalizeErr(err)
return nil, pErr
Expand Down
38 changes: 38 additions & 0 deletions internal/engine/postgresql/parse_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build !windows && cgo
// +build !windows,cgo

package postgresql

import (
cgonodes "github.com/pganalyze/pg_query_go/v4"
wasinodes "github.com/wasilibs/go-pgquery"
"google.golang.org/protobuf/proto"
)

func parseNodes(input string) (*wasinodes.ParseResult, error) {
cgoRes, err := cgonodes.Parse(input)
if err != nil {
return nil, err
}

// It would be too tedious to maintain conversion logic in a way that
// can target the two types from different packages despite being identical.
// We go ahead and take a small performance hit by marshaling through
// protobuf to unify the types. We must use the wasilibs version because
// the upstream version requires cgo even when only accessing the proto.

resBytes, err := proto.Marshal(cgoRes)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the performance regression, protobuf marshaling is very fast and it's actually relatively common to do this sort of conversion in real code I've seen. But I will try to get go-pgquery to use the same parsetree type as upstream to be able to avoid it, hopefully that goes smoothly

if err != nil {
return nil, err
}

var wasiRes wasinodes.ParseResult
if err := proto.Unmarshal(resBytes, &wasiRes); err != nil {
return nil, err
}

return &wasiRes, nil
}

var Parse = parseNodes
var Fingerprint = cgonodes.Fingerprint
35 changes: 0 additions & 35 deletions internal/engine/postgresql/parse_disabled.go

This file was deleted.

13 changes: 13 additions & 0 deletions internal/engine/postgresql/parse_wasi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build windows || !cgo
// +build windows !cgo

package postgresql

import (
nodes "github.com/wasilibs/go-pgquery"
)

var parseNodes = nodes.Parse

var Parse = parseNodes
var Fingerprint = nodes.Fingerprint
8 changes: 8 additions & 0 deletions internal/engine/postgresql/parser/parser_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows && cgo
// +build !windows,cgo

package parser

import "github.com/pganalyze/pg_query_go/v4/parser"

type Error = parser.Error
8 changes: 8 additions & 0 deletions internal/engine/postgresql/parser/parser_wasi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows || !cgo
// +build windows !cgo

package parser

import "github.com/wasilibs/go-pgquery/parser"

type Error = parser.Error
5 changes: 1 addition & 4 deletions internal/engine/postgresql/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//go:build !windows && cgo
// +build !windows,cgo

package postgresql

import (
nodes "github.com/pganalyze/pg_query_go/v4"
nodes "github.com/wasilibs/go-pgquery"
)

func isArray(n *nodes.TypeName) bool {
Expand Down