Skip to content

Commit ce9e964

Browse files
authored
The experimental parser passes all tests (#515)
* test: Add first passing test for new parser * Most tests pass * Fix walk panic * Fix TestReplay/invalid_params/experimental * Fix two more tests * one more failing test * Fix the last failing test
1 parent 8ac5803 commit ce9e964

29 files changed

+490
-86
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
- name: Test sqlc
3333
run: go test --tags=examples -v ./...
3434
env:
35+
SQLC_EXPERIMENTAL_PARSER: on
3536
PG_USER: postgres
3637
PG_HOST: localhost
3738
PG_DATABASE: postgres

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ build:
44
test:
55
go test ./...
66

7-
exp:
8-
SQLC_EXPERIMENTAL_PARSER=on go test ./...
9-
107
sqlc-dev:
118
go build -o ~/bin/sqlc-dev ./cmd/sqlc/
129

internal/compiler/compat.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func sameTableName(n *ast.TableName, f core.FQN) bool {
1414
if n == nil {
1515
return false
1616
}
17-
return n.Catalog == n.Catalog && n.Schema == f.Schema && n.Name == f.Rel
17+
schema := n.Schema
18+
if n.Schema == "" {
19+
schema = "public"
20+
}
21+
return n.Catalog == n.Catalog && schema == f.Schema && n.Name == f.Rel
1822
}
1923

2024
// This is mainly copy-pasted from internal/postgresql/parse.go

internal/compiler/compile.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"io/ioutil"
@@ -18,6 +19,7 @@ import (
1819
"github.com/kyleconroy/sqlc/internal/postgresql"
1920
"github.com/kyleconroy/sqlc/internal/sql/ast"
2021
"github.com/kyleconroy/sqlc/internal/sql/catalog"
22+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
2123
"github.com/kyleconroy/sqlc/internal/sql/sqlpath"
2224
"github.com/kyleconroy/sqlc/internal/sqlite"
2325
)
@@ -111,12 +113,17 @@ func parseQueries(p Parser, c *catalog.Catalog, queries []string) (*Result, erro
111113
continue
112114
}
113115
if err != nil {
114-
merr.Add(filename, src, stmt.Raw.Pos(), err)
116+
var e *sqlerr.Error
117+
loc := stmt.Raw.Pos()
118+
if errors.As(err, &e) && e.Location != 0 {
119+
loc = e.Location
120+
}
121+
merr.Add(filename, src, loc, err)
115122
continue
116123
}
117124
if query.Name != "" {
118125
if _, exists := set[query.Name]; exists {
119-
merr.Add(filename, src, 0, fmt.Errorf("duplicate query name: %s", query.Name))
126+
merr.Add(filename, src, stmt.Raw.Pos(), fmt.Errorf("duplicate query name: %s", query.Name))
120127
continue
121128
}
122129
set[query.Name] = struct{}{}

internal/compiler/find_params.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
6969
*p.refs = append(*p.refs, paramRef{parent: n.Cols.Items[i], ref: ref, rv: n.Relation})
7070
p.seen[ref.Location] = struct{}{}
7171
}
72-
for _, vl := range s.ValuesLists {
73-
for i, v := range vl {
72+
for _, item := range s.ValuesLists.Items {
73+
vl, ok := item.(*ast.List)
74+
if !ok {
75+
continue
76+
}
77+
for i, v := range vl.Items {
7478
ref, ok := v.(*pg.ParamRef)
7579
if !ok {
7680
continue

internal/compiler/output_columns.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
161161
name = *res.Name
162162
}
163163
switch n.SubLinkType {
164-
// TODO: case nodes.EXISTS_SUBLINK:
165-
// cols = append(cols, core.Column{Name: name, DataType: "bool", NotNull: true})
164+
case pg.EXISTS_SUBLINK:
165+
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
166166
default:
167167
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
168168
}

internal/compiler/parse.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"sort"
77
"strings"
88

9-
"github.com/davecgh/go-spew/spew"
10-
119
"github.com/kyleconroy/sqlc/internal/metadata"
1210
"github.com/kyleconroy/sqlc/internal/source"
1311
"github.com/kyleconroy/sqlc/internal/sql/ast"
@@ -77,7 +75,6 @@ func parseQuery(p Parser, c *catalog.Catalog, stmt ast.Node, src string, rewrite
7775
}
7876
params, err := resolveCatalogRefs(c, rvs, refs, namedParams)
7977
if err != nil {
80-
spew.Dump(raw)
8178
return nil, err
8279
}
8380

internal/compiler/query_catalog.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ func buildQueryCatalog(c *catalog.Catalog, node ast.Node) (*QueryCatalog, error)
3131
if err != nil {
3232
return nil, err
3333
}
34+
rel := &ast.TableName{Name: *cte.Ctename}
35+
for i := range cols {
36+
cols[i].Table = rel
37+
}
3438
qc.ctes[*cte.Ctename] = &Table{
35-
Rel: &ast.TableName{
36-
Name: *cte.Ctename,
37-
},
39+
Rel: rel,
3840
Columns: cols,
3941
}
4042
}

internal/endtoend/endtoend_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,10 @@ func TestExamples(t *testing.T) {
4747

4848
func TestReplay(t *testing.T) {
4949
t.Parallel()
50-
51-
experimental := map[string]bool{
52-
"ondeck": true,
53-
}
54-
env := cmd.ParseEnv()
55-
5650
files, err := ioutil.ReadDir("testdata")
5751
if err != nil {
5852
t.Fatal(err)
5953
}
60-
6154
for _, replay := range files {
6255
if !replay.IsDir() {
6356
continue
@@ -76,15 +69,19 @@ func TestReplay(t *testing.T) {
7669
if diff := cmp.Diff(expected, stderr.String()); diff != "" {
7770
t.Errorf("stderr differed (-want +got):\n%s", diff)
7871
}
79-
if env.ExperimentalParser && experimental[tc] {
80-
output, err := cmd.Generate(env, path, &stderr)
81-
if len(expected) == 0 && err != nil {
82-
t.Fatalf("EXPERIMENTAL sqlc generate failed: %s", stderr.String())
83-
}
84-
cmpDirectory(t, path, output)
85-
if diff := cmp.Diff(expected, stderr.String()); diff != "" {
86-
t.Errorf("EXPERIMENTAL stderr differed (-want +got):\n%s", diff)
87-
}
72+
})
73+
t.Run(tc+"/experimental", func(t *testing.T) {
74+
t.Parallel()
75+
path, _ := filepath.Abs(filepath.Join("testdata", tc))
76+
var stderr bytes.Buffer
77+
expected := expectedStderr(t, path)
78+
output, err := cmd.Generate(cmd.Env{ExperimentalParser: true}, path, &stderr)
79+
if len(expected) == 0 && err != nil {
80+
t.Fatalf("sqlc generate failed: %s", stderr.String())
81+
}
82+
cmpDirectory(t, path, output)
83+
if diff := cmp.Diff(expected, stderr.String()); diff != "" {
84+
t.Errorf("stderr differed (-want +got):\n%s", diff)
8885
}
8986
})
9087
}

internal/endtoend/testdata/pg_advisory_xact_lock/go/query.sql.go

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)