Skip to content

Commit e8a4e76

Browse files
authored
Milestones 1 and 2: scaffolding, codegen, oracle, and corpus goldens (#2)
1 parent 3fd4bc3 commit e8a4e76

3,095 files changed

Lines changed: 1999835 additions & 12 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
env:
15+
# The whole point: no cgo, anywhere, ever.
16+
CGO_ENABLED: "0"
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version-file: go.mod
22+
- name: gofmt
23+
run: test -z "$(gofmt -l . | grep -v '^internal/reference/' | grep -v '\.pb\.go$')"
24+
- run: go vet ./...
25+
- run: go build ./...
26+
- name: go test -race
27+
env:
28+
CGO_ENABLED: "1" # -race requires cgo in the toolchain, not in our code
29+
run: go test -race ./...
30+
- name: go test (pure)
31+
run: go test ./...
32+
33+
generate-clean:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
- uses: actions/setup-go@v5
38+
with:
39+
go-version-file: go.mod
40+
- name: cmd/generate output is committed and current
41+
run: |
42+
go run ./cmd/generate -aliases -keywords
43+
git diff --exit-code

‎.github/workflows/regenerate.yml‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: regenerate
2+
3+
# Scheduled proof that the committed goldens are reproducible byte-for-byte
4+
# from the pinned oracle (libpg_query 17-6.2.2 via pg_query_go v6.2.2, cgo).
5+
# This is the only place cgo ever runs; the main module never links it.
6+
7+
on:
8+
schedule:
9+
- cron: "17 6 * * 1" # weekly, Monday 06:17 UTC
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
regenerate:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: actions/setup-go@v5
21+
with:
22+
go-version-file: go.mod
23+
- name: Fetch pinned corpora
24+
run: |
25+
git clone --depth 1 --branch 17-6.2.2 https://github.com/pganalyze/libpg_query /tmp/libpg_query
26+
git clone --depth 1 --branch v6.2.2 https://github.com/pganalyze/pg_query_go /tmp/pg_query_go
27+
- name: Regenerate goldens (builds the cgo oracle; several minutes)
28+
run: |
29+
go run ./cmd/regenerate -libpg-query /tmp/libpg_query -pg-query-go /tmp/pg_query_go
30+
- name: Goldens are reproducible
31+
run: git diff --exit-code -- parser/testdata

‎CLAUDE.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# oliphant development guide
2+
3+
Pure Go port of pg_query_go. Read `PLAN.md` first — it is the contract.
4+
Pin: libpg_query **17-6.2.2** (PostgreSQL 17.7, patched). No cgo anywhere in
5+
the main module; the only module allowed to link pg_query_go (cgo) is
6+
`oracle/`, and only `cmd/regenerate`/`cmd/difftest` ever run it.
7+
8+
## The development loop
9+
10+
1. `go run ./cmd/next-test` — picks the next `todo` case from
11+
`parser/testdata/*/`.
12+
2. Implement the production in `internal/parse/` (or lexer rule in
13+
`internal/lexer/`), with an attribution comment naming the `gram.y` /
14+
`scan.l` rule (`// gram.y: simple_select`).
15+
3. `go test ./parser -run TestCorpus -check-parse` — runs the `todo` cases;
16+
newly passing cases are removed from the `metadata.json` sidecars
17+
automatically. Commit the sidecar diff with the code.
18+
4. `go test ./...` must stay green; a case that regresses from passing to
19+
failing is a hard failure, never a new todo.
20+
21+
## Hard rules
22+
23+
- **Expected outputs are never edited by hand.** Goldens come from
24+
`cmd/regenerate` (the pinned oracle) or not at all.
25+
- Generated files (`ast/pg_query.pb.go`, `aliases.go`,
26+
`internal/lexer/keywords.go`) are only ever written by `cmd/generate`.
27+
- The public API mirrors pg_query_go exactly; consumers must migrate by
28+
changing one import path. Never rename, add, or remove exported symbols
29+
without checking pg_query_go v6.2.2.
30+
- Location fields must byte-match the reference; when in doubt, check which
31+
`@N` the `gram.y` action uses.
32+
33+
## Layout crib
34+
35+
- `internal/reference/` — pinned `gram.y`, `scan.l`, `parser.c`, `kwlist.h`
36+
(docs only). The `base_yylex` token-merge filter is in `parser.c`.
37+
- `srcdata/` — vendored libpg_query metadata JSON + `pg_query.proto`
38+
(generator inputs).
39+
- `parser/testdata/<suite>/*.test` — corpus: cases split by `==`, input and
40+
expectation split by `--`. `metadata.json` sidecar per file tracks `todo`.
41+
- `oracle/` — separate go module, cgo, never imported by the main module.
42+
43+
## Regenerating
44+
45+
- `go run ./cmd/generate -aliases -keywords` — pure Go, always safe.
46+
- `go run ./cmd/generate -proto` — requires `protoc` + `protoc-gen-go`.
47+
- `go run ./cmd/regenerate -libpg-query <checkout> -pg-query-go <checkout>` —
48+
builds the cgo oracle, rewrites `parser/testdata` from scratch. Output must
49+
be byte-identical run-to-run.

‎LICENSE‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
MIT License
2+
3+
Copyright (c) 2026 The sqlc Authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
---
24+
25+
This repository additionally contains:
26+
27+
- Logic ported from the PostgreSQL parser (grammar, scanner, keyword tables,
28+
deparser): see LICENSE.POSTGRESQL.
29+
- Logic ported from pganalyze/libpg_query and pganalyze/pg_query_go
30+
(API surface, fingerprinting, JSON output conventions, test corpora):
31+
see LICENSE.LIBPG_QUERY.

‎LICENSE.LIBPG_QUERY‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Copyright (c) 2015, Lukas Fittl <lukas@fittl.com>
2+
Copyright (c) 2016-2023, Duboce Labs, Inc. (pganalyze) <team@pganalyze.com>
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
* Neither the name of pg_query nor the names of its contributors may be used
16+
to endorse or promote products derived from this software without specific
17+
prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
POSSIBILITY OF SUCH DAMAGE.

‎LICENSE.POSTGRESQL‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
This project includes code derived from the PostgreSQL project (http://www.postgresql.org/),
2+
see the original full copyright notice below:
3+
4+
---
5+
6+
PostgreSQL Database Management System
7+
(formerly known as Postgres, then as Postgres95)
8+
9+
Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
10+
11+
Portions Copyright (c) 1994, The Regents of the University of California
12+
13+
Permission to use, copy, modify, and distribute this software and its
14+
documentation for any purpose, without fee, and without a written agreement
15+
is hereby granted, provided that the above copyright notice and this
16+
paragraph and the following two paragraphs appear in all copies.
17+
18+
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
19+
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
20+
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
21+
DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
22+
POSSIBILITY OF SUCH DAMAGE.
23+
24+
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26+
AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
27+
ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
28+
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

‎PLAN.md‎

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,20 @@ oliphant/
359359
Ordered so that every milestone lands with its own oracle-backed acceptance
360360
gate; the corpus harness exists before the first line of the lexer.
361361

362-
1. **Scaffolding + codegen.** Module, licenses, CI; vendor `srcdata/`,
363-
`pg_query.proto`, reference grammar files at the pin; `cmd/generate`
364-
producing `ast/pg_query.pb.go`, root aliases, keyword tables;
365-
`oliphant.go` API stubs returning not-implemented; `makefuncs.go` and the
366-
`parser.Error` type ported. **Gate:** pg_query_go's `parse_test.go`
367-
expected-tree literals compile unchanged against oliphant's types.
368-
2. **Corpus + oracle.** `oracle/` module wrapping pg_query_go v6.2.2;
369-
`cmd/regenerate` extracting all corpus tiers into `.test` goldens
370-
(trees, errors, scan tokens, normalize, fingerprint, deparse);
371-
`internal/testfile` reader; harness running everything as `todo`.
372-
**Gate:** goldens reproducible byte-for-byte across two regeneration runs.
362+
1. **Scaffolding + codegen.** ✅ *Landed 2026-08-15.* Module, licenses, CI;
363+
vendor `srcdata/`, `pg_query.proto`, reference grammar files at the pin;
364+
`cmd/generate` producing `ast/pg_query.pb.go`, root aliases, keyword
365+
tables; `oliphant.go` API stubs returning not-implemented; `makefuncs.go`
366+
and the `parser.Error` type ported. **Gate:** pg_query_go's
367+
`parse_test.go` expected-tree literals compile unchanged against
368+
oliphant's types — met (`parse_test.go`, import swap only).
369+
2. **Corpus + oracle.** ✅ *Landed 2026-08-15.* `oracle/` module wrapping
370+
pg_query_go v6.2.2; `cmd/regenerate` extracting the corpus tiers into
371+
`.test` goldens (trees, errors, scan tokens, normalize, fingerprint,
372+
deparse, splits); `internal/testfile` reader; harness running everything
373+
as `todo`. **Gate:** goldens reproducible byte-for-byte across two
374+
regeneration runs — met, and re-verified weekly by the `regenerate` CI
375+
workflow. See "As-built notes" below for measured counts and deferrals.
373376
3. **Lexer.** `scan.l` port + `base_yylex` filter layer; `Scan`,
374377
`SplitWithScanner`, `HashXXH3_64` (+ `internal/xxh3`) ship. **Gate:**
375378
token streams byte-identical to the oracle across the entire corpus,
@@ -417,6 +420,34 @@ gate; the corpus harness exists before the first line of the lexer.
417420
`tetratelabs/wazero`, and the cgo requirement; sqlc's endtoend suite is
418421
the acceptance gate. Windows and `CGO_ENABLED=0` become first-class.
419422

423+
### As-built notes (milestones 1–2)
424+
425+
Measured at the pin, where the plan's estimates differ:
426+
427+
- The corpus as regenerated: **1,525 `.test` files / 217,229 cases (~80 MB)**
428+
across eight suites (`parse`, `scan`, `normalize`, `normalize_utility`,
429+
`fingerprint`, `deparse`, `split_scanner`, `split_parser`).
430+
- `postgres_regress/` has **224** files at `17-6.2.2` (not 233); `kwlist.h`
431+
yields **491** keywords (not 494); the proto has **273** messages and
432+
**71** enums (not 276/73). The inline tables carry 36 parse, 12 scan,
433+
25 normalize, 25 normalize_utility, 78 fingerprint, 418 deparse, and
434+
8 split inputs; `deparse-depesz/` is 150 `.psql` files under `*.d/`
435+
subdirectories.
436+
- The `.test` format needed one addition over the family convention: input
437+
or expectation lines that collide with the `== ` / `--` markers are
438+
escaped with a leading `|` (SQL comment banners of exactly `--` are
439+
everywhere in the regress files). `internal/testfile` round-trips this
440+
bijectively.
441+
- `cmd/regenerate` preserves passing status across runs: a case identical in
442+
name, input, and expectation to one that had graduated out of `todo` stays
443+
passing, so a pin advance puts exactly the diff back on the todo list.
444+
- `cmd/generate -proto` requires `protoc` + `protoc-gen-go` on PATH; the
445+
pure-Go `-aliases`/`-keywords` modes are what CI verifies.
446+
- Deferred, deliberately: the **sqlc endtoend tier** (needs per-directory
447+
engine classification in the sqlc repo; import it alongside milestone 4),
448+
**`plpgsql_regress/`** (milestone 11), and **summary** golden extraction
449+
(milestone 10). The stretch tarball tier remains stretch.
450+
420451
## Regeneration (the PostgreSQL-upgrade story)
421452

422453
Everything derived is derived by committed tooling from the pin:

‎README.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
# oliphant
1+
# oliphant
2+
3+
A hand-written, pure Go (no cgo, no wasm) drop-in replacement for
4+
[pganalyze/pg_query_go](https://github.com/pganalyze/pg_query_go): same
5+
exported functions, same generated protobuf types, same JSON output, same
6+
error type, same fingerprints. Consumers change one import path:
7+
8+
```diff
9+
-import pg_query "github.com/pganalyze/pg_query_go/v6"
10+
+import pg_query "github.com/sqlc-dev/oliphant"
11+
```
12+
13+
Pinned to **libpg_query `17-6.2.2`** (PostgreSQL 17.7, patched) — the exact
14+
build pg_query_go v6.2.2 ships. Every golden in `parser/testdata` derives
15+
from that oracle, byte for byte, and is never edited by hand.
16+
17+
See [`PLAN.md`](PLAN.md) for the full design and [`CLAUDE.md`](CLAUDE.md)
18+
for the development loop.
19+
20+
## Status
21+
22+
| Milestone | State |
23+
|---|---|
24+
| 1. Scaffolding + codegen | ✅ module, licenses, CI, vendored pin, `ast/` protobuf types, aliases, keyword tables, API surface, `parser.Error`; pg_query_go's parse_test tree literals compile unchanged |
25+
| 2. Corpus + oracle | ✅ cgo oracle (`oracle/`), `cmd/regenerate`, 1,525 golden files / 217k cases across 8 suites, byte-reproducible; harness + `cmd/next-test` running everything as todo |
26+
| 3. Lexer | — |
27+
| 4. Expressions + SELECT | — |
28+
| 5–7. DML, DDL, utility | — |
29+
| 8. Normalize + fingerprint | — |
30+
| 9. Deparse | — |
31+
| 10–13. Summary, PL/pgSQL, hardening, sqlc integration | — |
32+
33+
Until the parser milestones land, every public entry point returns (or
34+
panics with, for `HashXXH3_64`) a clear not-implemented error.

0 commit comments

Comments
 (0)