Skip to content

Commit 89807c6

Browse files
authored
Milestones 6 and 7: DDL and utility statements — parse corpus at 100% (#6)
1 parent 528d8b4 commit 89807c6

291 files changed

Lines changed: 8655 additions & 20423 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.

‎PLAN.md‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,18 @@ gate; the corpus harness exists before the first line of the lexer.
392392
5. **DML.** ✅ *Landed 2026-08-16.* `INSERT` (`ON CONFLICT`, `OVERRIDING`),
393393
`UPDATE`, `DELETE`, `MERGE`, `RETURNING`, `COPY`, `PREPARE`/`EXECUTE`,
394394
cursors. See "As-built notes (milestone 5)".
395-
6. **DDL, part 1.** `CREATE`/`ALTER TABLE` (the second-biggest grammar
396-
region: constraints, partitioning, identity, storage options),
397-
`CREATE INDEX`, views, sequences, schemas.
398-
7. **DDL, part 2 + utility.** The long tail of ~150 statement types:
399-
functions/procedures, triggers, policies, roles/grants, types/domains,
400-
FDWs, publications/subscriptions, `EXPLAIN`/`VACUUM`/`SET`/`SHOW`, event
401-
triggers, … `SplitWithParser` and `IsUtilityStmt` fall out here.
395+
6. **DDL, part 1.** ✅ *Landed 2026-08-16.* `CREATE`/`ALTER TABLE` (the
396+
second-biggest grammar region: constraints, partitioning, identity,
397+
storage options), `CREATE INDEX`, views, sequences, schemas. See
398+
"As-built notes (milestones 6–7)".
399+
7. **DDL, part 2 + utility.** ✅ *Landed 2026-08-16.* The long tail of ~150
400+
statement types: functions/procedures, triggers, policies, roles/grants,
401+
types/domains, FDWs, publications/subscriptions,
402+
`EXPLAIN`/`VACUUM`/`SET`/`SHOW`, event triggers, … `SplitWithParser` and
403+
`IsUtilityStmt` ship here.
402404
**Gate for 4–7:** 100% of the regress corpus — trees, JSON bytes, error
403-
messages, cursor positions.
405+
messages, cursor positions — met: the parse suite's todo list is empty
406+
(42,970 cases across every tier), as are scan and both split suites.
404407
8. **Normalize + Fingerprint.** Port `pg_query_normalize.c` (constant
405408
locations → `$n`, `NormalizeUtility`); generated fingerprint walk + the
406409
hand-written special cases. **Gate:** all normalize/fingerprint goldens,
@@ -555,6 +558,46 @@ Measured at the pin, where the plan's estimates differ:
555558
and the `-summary` classifier shows no tree or error mismatches against
556559
any implemented statement.
557560

561+
### As-built notes (milestones 6–7)
562+
563+
- Statement dispatch is a family of lookahead routers
564+
(`internal/parse/ddl_dispatch.go`, `ddl_alter_generic.go`): each
565+
CREATE/ALTER/DROP object-kind arm parses the object reference once, then
566+
the following token picks the production, mirroring how the LALR tables
567+
keep RenameStmt/AlterObjectSchemaStmt/AlterOwnerStmt/
568+
AlterObjectDependsStmt alive alongside each object's own ALTER statement.
569+
A shared `parseAlterGenericTail` implements the four generic tails with a
570+
per-kind support mask, so `ALTER LANGUAGE x SET SCHEMA` still fails at
571+
the token bison would reject.
572+
- The CreateStmt/CreateAsStmt ambiguity (`CREATE TABLE t (…)` element list
573+
vs. `create_as_target` column list) is the one new bounded-backtracking
574+
site: the CTAS reading is tried first and commits only when its `AS`
575+
arrives, else the position resets and the element list parses.
576+
- C's zero-valued enums surface as their proto-shifted spellings, which the
577+
goldens made explicit: every `AlterTableCmd`/`RenameStmt` carries
578+
`behavior: DROP_RESTRICT`, `RenameStmt.relationType` defaults to
579+
`OBJECT_ACCESS_METHOD`, and `AlterPublicationStmt.action` to
580+
`AP_AddObjects`. Two NIL-vs-empty-list shapes matter: the zero-argument
581+
`(*)` aggregate stores a null list element in `DefineStmt.args`, and an
582+
empty `BEGIN ATOMIC` body stores `[NIL]`, not `[[]]`.
583+
- Support-function ereports carry their C call site's funcname
584+
(`processCASbits`, `SplitColQualList`, `makeOrderedSetArgs`,
585+
`preprocess_pubobj_list`, …), and `ConstraintAttributeSpec`'s own
586+
location is always -1 (PG's `YYLLOC_DEFAULT` gives empty productions -1
587+
and the left recursion propagates it), so `processCASbits` errors have no
588+
cursor position.
589+
- The `base_yylex`-merged `*_LA` tokens keep their first word's keyword
590+
classification in the lexer but are never identifiers in the grammar; the
591+
identifier-class helpers reject them (found via `nulls first` in
592+
`index_elem`, where `NULLS_LA` must not parse as an opclass name).
593+
- `makeRangeVarFromAnyName` builds via `makeNode`, so `inh` stays false —
594+
unlike `makeRangeVar`'s true — which the CompositeTypeStmt goldens pin.
595+
- `cmd/difftodo` (debug aid) prints input/want/got for a file's remaining
596+
parse todos; it drove the long-tail mismatch hunt to zero.
597+
- Corpus effect: all 19,787 remaining parse todos graduated, plus the 8
598+
`split_parser` cases. The remaining todo suites (deparse, fingerprint,
599+
normalize, normalize_utility) belong to milestones 8–9.
600+
558601
## Regeneration (the PostgreSQL-upgrade story)
559602

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

‎cmd/difftodo/main.go‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// difftodo prints input, expected, and actual output for the still-failing
2+
// todo cases of parse-suite .test files. Debug aid for the corpus loop; not
3+
// part of the public tooling.
4+
package main
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"os"
10+
"strconv"
11+
12+
pg_query "github.com/sqlc-dev/oliphant"
13+
"github.com/sqlc-dev/oliphant/internal/testfile"
14+
"github.com/sqlc-dev/oliphant/parser"
15+
)
16+
17+
func main() {
18+
path := os.Args[1]
19+
limit := 5
20+
if len(os.Args) > 2 {
21+
limit, _ = strconv.Atoi(os.Args[2])
22+
}
23+
cases, err := testfile.Read(path)
24+
if err != nil {
25+
panic(err)
26+
}
27+
meta, err := testfile.ReadMetadata(path)
28+
if err != nil {
29+
panic(err)
30+
}
31+
todo := map[string]bool{}
32+
for _, name := range meta.Todo {
33+
todo[name] = true
34+
}
35+
shown := 0
36+
for _, c := range cases {
37+
if !todo[c.Name] {
38+
continue
39+
}
40+
got, perr := pg_query.ParseToJSON(c.Input)
41+
if perr != nil {
42+
var pe *parser.Error
43+
if errors.As(perr, &pe) {
44+
got = testfile.RenderError(testfile.ErrorExpectation{
45+
Message: pe.Message,
46+
Cursorpos: pe.Cursorpos,
47+
Filename: pe.Filename,
48+
Funcname: pe.Funcname,
49+
Context: pe.Context,
50+
})
51+
} else {
52+
got = "UNIMPLEMENTED: " + perr.Error()
53+
}
54+
}
55+
if got == c.Expected {
56+
continue
57+
}
58+
fmt.Printf("== %s\nINPUT: %s\nWANT: %s\nGOT: %s\n\n", c.Name, c.Input, c.Expected, got)
59+
shown++
60+
if shown >= limit {
61+
break
62+
}
63+
}
64+
}

‎compat_test.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ func TestNotImplementedErrors(t *testing.T) {
4343
check("Fingerprint", err)
4444
_, err = pg_query.FingerprintToUInt64("SELECT 1")
4545
check("FingerprintToUInt64", err)
46-
_, err = pg_query.SplitWithParser("SELECT 1", false)
47-
check("SplitWithParser", err)
48-
_, err = pg_query.IsUtilityStmt("SELECT 1")
49-
check("IsUtilityStmt", err)
5046
_, err = pg_query.ParsePlPgSqlToJSON("SELECT 1")
5147
check("ParsePlPgSqlToJSON", err)
5248
_, err = pg_query.Summary("SELECT 1", 0)
@@ -90,6 +86,35 @@ func TestScanTokens(t *testing.T) {
9086
}
9187
}
9288

89+
// TestSplitWithParser mirrors pg_query_go's parser-based split tests.
90+
func TestSplitWithParser(t *testing.T) {
91+
stmts, err := pg_query.SplitWithParser("CREATE RULE x AS ON SELECT TO tbl DO (SELECT 1; SELECT 2); SELECT 3", true)
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
want := []string{"CREATE RULE x AS ON SELECT TO tbl DO (SELECT 1; SELECT 2)", "SELECT 3"}
96+
if len(stmts) != len(want) || stmts[0] != want[0] || stmts[1] != want[1] {
97+
t.Fatalf("SplitWithParser = %q, want %q", stmts, want)
98+
}
99+
}
100+
101+
// TestIsUtilityStmt pins the statement classification split.
102+
func TestIsUtilityStmt(t *testing.T) {
103+
got, err := pg_query.IsUtilityStmt("SELECT 1; VACUUM; INSERT INTO t VALUES (1); CREATE TABLE t (a int)")
104+
if err != nil {
105+
t.Fatal(err)
106+
}
107+
want := []bool{false, true, false, true}
108+
if len(got) != len(want) {
109+
t.Fatalf("IsUtilityStmt returned %d results, want %d", len(got), len(want))
110+
}
111+
for i := range want {
112+
if got[i] != want[i] {
113+
t.Errorf("IsUtilityStmt[%d] = %v, want %v", i, got[i], want[i])
114+
}
115+
}
116+
}
117+
93118
// TestSplitWithScanner mirrors pg_query_go's scanner-based split tests.
94119
func TestSplitWithScanner(t *testing.T) {
95120
stmts, err := pg_query.SplitWithScanner("SELECT /* comment with ; */ 1; SELECT 2", true)

0 commit comments

Comments
 (0)