Skip to content

Commit 2d5ffd3

Browse files
committed
fix(duckdb): report an annotated statement that cannot be converted
The DuckDB parser skipped every statement that converted to ast.TODO. The span of a statement includes the comments before it, so a "-- name:" annotation was lost with the statement. sqlc generate exited 0, printed nothing and generated no method for it. Keep skipping a statement that has no query annotation (INSTALL, LOAD and SET in a schema file). Return an error for a statement that has one, and point it at the annotation. Adds analyze_unsupported_statement and a control case, analyze_skipped_schema_statements.
1 parent 3ad8b20 commit 2d5ffd3

9 files changed

Lines changed: 78 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: ListItems :many
2+
SELECT id, name FROM items;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INSTALL spatial;
2+
LOAD spatial;
3+
SET threads = 2;
4+
5+
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"name": "ListItems",
4+
"cmd": ":many",
5+
"columns": [
6+
{
7+
"name": "id",
8+
"type": {
9+
"name": "integer"
10+
},
11+
"table": "items"
12+
},
13+
{
14+
"name": "name",
15+
"type": {
16+
"name": "varchar"
17+
},
18+
"table": "items"
19+
}
20+
],
21+
"params": []
22+
}
23+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: ListItems :many
2+
SELECT id, name FROM items;
3+
4+
-- name: ExportItems :exec
5+
COPY (SELECT id, name FROM items) TO 'items.parquet' (FORMAT PARQUET);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error: error parsing queries: query.sql:5:1: query ExportItems: statement is not supported by the duckdb engine

‎internal/engine/duckdb/parse.go‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package duckdb
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
7+
"fmt"
68
"io"
9+
"strings"
710

811
"github.com/sqlc-dev/darkwing/parser"
912

13+
"github.com/sqlc-dev/sqlc/internal/metadata"
1014
"github.com/sqlc-dev/sqlc/internal/source"
1115
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1216
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
@@ -23,7 +27,11 @@ type Parser struct{}
2327
// starts where the previous one ended — so a statement's span covers the
2428
// comments before it, which is where the sqlc query annotation lives.
2529
func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
26-
stmts, err := parser.Parse(context.Background(), r)
30+
src, err := io.ReadAll(r)
31+
if err != nil {
32+
return nil, err
33+
}
34+
stmts, err := parser.Parse(context.Background(), bytes.NewReader(src))
2735
if err != nil {
2836
var perr *parser.Error
2937
if errors.As(err, &perr) {
@@ -45,6 +53,21 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
4553
converter := &cc{}
4654
node := converter.convert(stmt)
4755
if _, ok := node.(*ast.TODO); ok {
56+
// A statement with no query annotation (INSTALL, LOAD, SET in a
57+
// schema file) is skipped. A statement with one is a query that
58+
// the user expects sqlc to generate: dropping it would hide the
59+
// missing method, so report it.
60+
span := string(src[stmt.Pos():stmt.End()])
61+
name, _, merr := metadata.ParseQueryNameAndType(span, metadata.CommentSyntax(p.CommentSyntax()))
62+
if merr != nil {
63+
return nil, &sqlerr.Error{Message: merr.Error(), Location: stmt.Pos() + leadingSpace(span)}
64+
}
65+
if name != "" {
66+
return nil, &sqlerr.Error{
67+
Message: fmt.Sprintf("query %s: statement is not supported by the duckdb engine", name),
68+
Location: stmt.Pos() + leadingSpace(span),
69+
}
70+
}
4871
continue
4972
}
5073
out = append(out, ast.Statement{
@@ -58,6 +81,13 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
5881
return out, nil
5982
}
6083

84+
// leadingSpace returns the length of the whitespace before the first
85+
// character of s, so an error points at the annotation and not at the blank
86+
// lines that follow the previous statement.
87+
func leadingSpace(s string) int {
88+
return len(s) - len(strings.TrimLeft(s, " \t\r\n"))
89+
}
90+
6191
// https://duckdb.org/docs/stable/sql/introduction#comments
6292
func (p *Parser) CommentSyntax() source.CommentSyntax {
6393
return source.CommentSyntax{

0 commit comments

Comments
 (0)