Skip to content

Commit 72c08a4

Browse files
authored
fix(analyzer): Error on unexpanded star expression (#2882)
* fix(analyzer): Error on unexpanded star expression If a query includes a star reference, error if we're unable to expand that reference. The new analyzer will happily anaylyze that code. If executed at runtime, these queries could change values. * Update config
1 parent 4f875c1 commit 72c08a4

File tree

10 files changed

+128
-0
lines changed

10 files changed

+128
-0
lines changed

internal/compiler/parse.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
8484
return nil, err
8585
}
8686

87+
// If the query uses star expansion, verify that it was edited. If not,
88+
// return an error.
89+
stars := astutils.Search(raw, func(node ast.Node) bool {
90+
_, ok := node.(*ast.A_Star)
91+
return ok
92+
})
93+
hasStars := len(stars.Items) > 0
94+
unchanged := inference.Query == rawSQL
95+
if unchanged && hasStars {
96+
return nil, fmt.Errorf("star expansion failed for query")
97+
}
98+
8799
// FOOTGUN: combineAnalysis mutates inference
88100
anlys = combineAnalysis(inference, result)
89101
} else {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["managed-db"]
3+
}

internal/endtoend/testdata/star_expansion_failed/postgresql/pgx/go/db.go

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

internal/endtoend/testdata/star_expansion_failed/postgresql/pgx/go/models.go

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

internal/endtoend/testdata/star_expansion_failed/postgresql/pgx/go/query.sql.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- name: GetLatestVersionWithSubquery :one
2+
SELECT *
3+
FROM versions
4+
WHERE versions.id IN (
5+
WITH RECURSIVE search_tree(id) AS (
6+
SELECT id, 0 as chain_id, 0 as chain_counter
7+
FROM versions
8+
)
9+
SELECT DISTINCT ON (search_tree.chain_id)
10+
search_tree.id
11+
FROM search_tree
12+
ORDER BY search_tree.chain_id, chain_counter DESC
13+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE versions (
3+
id BIGSERIAL PRIMARY KEY,
4+
name TEXT,
5+
previous_version_id bigint NOT NULL
6+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package querytest
2+
query.sql:1:1: star expansion failed for query

0 commit comments

Comments
 (0)