Skip to content

Commit a624f15

Browse files
committed
preprocess: do not rewrite sqlc syntax for GoogleSQL or ClickHouse
Both engines are getting their own parameter system, so they should not pick up sqlc.arg()/narg()/slice()/embed() by default just because the preprocessor happens to be dialect-driven. Drop their entries from the dialect table. An engine with no entry is now passed through untouched: File returns the source unchanged with an empty side table, so sqlc.arg() reaches the parser as the function call it looks like and fails there. Leaving an engine out of the table is the switch, and dialect.go says so. GoogleSQL still converts "@name" to a ParamRef. That is native GoogleSQL syntax rather than sqlc syntax, and the AST rewriter that used to turn it into one is gone, so the converter has to. Parameter names now come from the column the parameter is compared against, which is what every GoogleSQL golden already expected. The ClickHouse operator work stays, retargeted off sqlc syntax: A_Expr_Kind_OP on binary and unary operators plus the seeded comparison and arithmetic operators are what let the analysis core type a WHERE clause at all, and analyze_params/clickhouse now covers them with native "?" placeholders. Preprocessor testdata drops to 69 cases across the three engines that are actually preprocessed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015BSM9xGoosq58r1Mz6B7YP
1 parent dc51324 commit a624f15

33 files changed

Lines changed: 50 additions & 213 deletions

File tree

‎internal/compiler/compile.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
107107

108108
// sqlc syntax is not SQL. Rewrite it to native placeholders before the
109109
// engine parser ever sees the query, so parsers only handle SQL.
110-
pp, err := preprocess.File(c.conf.Engine, src)
111-
if err != nil {
112-
merr.Add(filename, src, 0, err)
113-
continue
114-
}
110+
pp := preprocess.File(c.conf.Engine, src)
115111

116112
stmts, err := c.parser.Parse(strings.NewReader(pp.Text))
117113
if err != nil {

‎internal/core/analyzer/bench_test.go‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"strings"
55
"testing"
66

7-
"github.com/sqlc-dev/sqlc/internal/config"
87
"github.com/sqlc-dev/sqlc/internal/core"
98
"github.com/sqlc-dev/sqlc/internal/core/analyzer"
109
coreschema "github.com/sqlc-dev/sqlc/internal/core/schema"
1110
"github.com/sqlc-dev/sqlc/internal/engine/googlesql"
1211
"github.com/sqlc-dev/sqlc/internal/sql/ast"
13-
"github.com/sqlc-dev/sqlc/internal/sql/preprocess"
1412
)
1513

1614
const benchSchema = `
@@ -49,14 +47,11 @@ func parseAll(t testing.TB, src string) []ast.Node {
4947
return out
5048
}
5149

52-
// parseQueries mirrors the compiler pipeline: rewrite sqlc syntax to native SQL,
53-
// then parse.
50+
// parseQueries mirrors the compiler pipeline. GoogleSQL is not preprocessed —
51+
// "@name" is native syntax the converter turns into a ParamRef — so parsing is
52+
// all it takes.
5453
func parseQueries(t testing.TB, src string) []ast.Node {
55-
res, err := preprocess.File(config.EngineGoogleSQL, src)
56-
if err != nil {
57-
t.Fatal(err)
58-
}
59-
return parseAll(t, res.Text)
54+
return parseAll(t, src)
6055
}
6156

6257
func newBenchCatalog(t testing.TB) (*core.Catalog, []ast.Node) {

internal/endtoend/testdata/analyze_sqlc_args/clickhouse/exec.json renamed to internal/endtoend/testdata/analyze_params/clickhouse/exec.json

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: GetEvent :one
2+
SELECT id, name FROM events WHERE id = ?;
3+
4+
-- name: FilterEvents :many
5+
SELECT id, name FROM events WHERE name = ? AND amount > ?;

internal/endtoend/testdata/analyze_sqlc_args/clickhouse/schema.sql renamed to internal/endtoend/testdata/analyze_params/clickhouse/schema.sql

File renamed without changes.

internal/endtoend/testdata/analyze_sqlc_args/clickhouse/stdout.txt renamed to internal/endtoend/testdata/analyze_params/clickhouse/stdout.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{
2323
"number": 1,
2424
"column": {
25-
"name": "event_id",
25+
"name": "id",
2626
"data_type": "uint64",
2727
"not_null": true,
2828
"is_array": false,
@@ -64,9 +64,9 @@
6464
{
6565
"number": 2,
6666
"column": {
67-
"name": "min_amount",
67+
"name": "amount",
6868
"data_type": "float64",
69-
"not_null": false,
69+
"not_null": true,
7070
"is_array": false,
7171
"table": "events"
7272
}

‎internal/endtoend/testdata/analyze_sqlc_args/clickhouse/query.sql‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎internal/engine/googlesql/convert.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,11 @@ func (c *cc) convertIntLiteral(n *zjast.IntLiteral) ast.Node {
514514

515515
// convertParameterExpr converts "@name" and "?" query parameters.
516516
//
517-
// Both become ParamRef nodes. GoogleSQL names its parameters, so repeated uses
518-
// of a name share a number; positional parameters are numbered by their 1-based
519-
// position in the statement. The compiler replaces these numbers with the ones
520-
// the preprocessor assigned in source order, so they only matter when a
521-
// statement is converted on its own.
517+
// Both become ParamRef nodes. "@name" is native GoogleSQL syntax rather than
518+
// sqlc syntax, so nothing rewrites it before the parser runs and the numbering
519+
// here is what the rest of the pipeline sees: repeated uses of a name share a
520+
// number, and positional parameters are numbered by their 1-based position in
521+
// the statement.
522522
func (c *cc) convertParameterExpr(n *zjast.ParameterExpr) ast.Node {
523523
if n.Name != nil {
524524
number, ok := c.namedParams[n.Name.Name]

‎internal/sql/preprocess/CLAUDE.md‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ Native placeholders by dialect:
2020
| postgresql | `$1` | sqlc syntax |
2121
| mysql | `?` | user variable, left alone |
2222
| sqlite | `?1` | sqlc syntax |
23-
| googlesql | `@name` | native, left in place |
24-
| clickhouse | `?` | not a parameter |
23+
24+
GoogleSQL and ClickHouse are deliberately absent. They handle their own
25+
parameter syntax, so `File` returns their source unchanged and sqlc syntax is
26+
not available to them — `sqlc.arg()` reaches the parser as the function call it
27+
looks like.
2528

2629
## How it works
2730

@@ -31,8 +34,9 @@ comments (`--`, `/* */`, `#`), string literals, quoted identifiers, backticks
3134
and PostgreSQL dollar-quoted strings. `Dialect` (dialect.go) is the single
3235
place those lexical rules live.
3336

34-
`File(engine, src)` walks the whole file, splits it at top-level semicolons and,
35-
for each statement:
37+
`File(engine, src)` looks up the dialect; an engine with no entry gets its
38+
source back untouched. Otherwise it walks the whole file, splits it at
39+
top-level semicolons and, for each statement:
3640

3741
1. `scan` collects every sqlc construct **and** every native placeholder, in
3842
source order.
@@ -96,13 +100,15 @@ go test ./internal/sql/preprocess -update
96100
```
97101

98102
The cases cover every shape of sqlc syntax that appears in
99-
`internal/endtoend`, per engine — each function against a bare reference, a
100-
string constant and a quoted identifier, the placeholder styles, and the
101-
constructs that must be left alone (comments, literals, MySQL user variables
102-
and `$1`, PostgreSQL's `@>` and `?` operators). When you add a shape to the
103-
corpus, add it here too.
103+
`internal/endtoend`, per preprocessed engine — each function against a bare
104+
reference, a string constant and a quoted identifier, the placeholder styles,
105+
and the constructs that must be left alone (comments, literals, MySQL user
106+
variables and `$1`, PostgreSQL's `@>` and `?` operators). When you add a shape
107+
to the corpus, add it here too.
104108

105109
## Adding a dialect
106110

107111
Add an entry to `dialects` in dialect.go. Nothing else in the codebase needs to
108-
change for `sqlc.arg`/`narg`/`slice`/`embed` to work for a new engine.
112+
change for `sqlc.arg`/`narg`/`slice`/`embed` to work for a new engine — but only
113+
add one if the engine should support sqlc syntax at all. Leaving an engine out
114+
is a deliberate choice, not an oversight.

‎internal/sql/preprocess/dialect.go‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ type Style int
1010
const (
1111
// StyleDollar numbers parameters as $1, $2, ... (PostgreSQL)
1212
StyleDollar Style = iota
13-
// StyleQuestion uses an unnumbered ? for every parameter (MySQL, ClickHouse)
13+
// StyleQuestion uses an unnumbered ? for every parameter (MySQL)
1414
StyleQuestion
1515
// StyleOrdinal numbers parameters as ?1, ?2, ... (SQLite)
1616
StyleOrdinal
17-
// StyleAtName keeps parameters named as @name (GoogleSQL)
18-
StyleAtName
1917
)
2018

2119
// Dialect describes the lexical rules the preprocessor needs to walk a query
@@ -90,31 +88,19 @@ var dialects = map[config.Engine]Dialect{
9088
Backtick: true,
9189
Backslash: true,
9290
},
93-
config.EngineGoogleSQL: {
94-
Style: StyleAtName,
95-
AtSign: true,
96-
Question: true,
97-
Backtick: true,
98-
Backslash: true,
99-
},
100-
config.EngineClickHouse: {
101-
Style: StyleQuestion,
102-
Question: true,
103-
Backtick: true,
104-
HashComment: true,
105-
Backslash: true,
106-
},
10791
}
10892

109-
// DialectFor returns the lexical rules for an engine.
93+
// DialectFor returns the lexical rules for an engine, and whether the engine is
94+
// preprocessed at all. GoogleSQL and ClickHouse are not: they handle their own
95+
// parameter syntax, so their queries reach the parser unchanged.
11096
func DialectFor(engine config.Engine) (Dialect, bool) {
11197
d, ok := dialects[engine]
11298
return d, ok
11399
}
114100

115101
// hasNamedSupport reports whether repeated uses of the same parameter name can
116-
// share a single placeholder. MySQL and ClickHouse send an argument per "?", so
117-
// every occurrence needs its own number.
102+
// share a single placeholder. MySQL sends an argument per "?", so every
103+
// occurrence needs its own number.
118104
func (d Dialect) hasNamedSupport() bool {
119105
return d.Style != StyleQuestion
120106
}

0 commit comments

Comments
 (0)