Skip to content

Commit b34aa37

Browse files
kyleconroyclaude
andauthored
SQLite: Fix parsing of INSERT DEFAULT VALUES syntax (#4010)
This commit fixes issue #3998 where SQLite's parser couldn't handle the valid SQL syntax "INSERT INTO table DEFAULT VALUES". The fix involves: 1. Updating the ANTLR grammar to properly support DEFAULT VALUES as part of the INSERT statement structure 2. Adding handling in convert.go for the DEFAULT VALUES case 3. Regenerating the parser from the updated grammar Fixes #3998 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3da0b82 commit b34aa37

File tree

10 files changed

+2027
-1955
lines changed

10 files changed

+2027
-1955
lines changed

internal/endtoend/testdata/insert_default_values/sqlite/go/db.go

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

internal/endtoend/testdata/insert_default_values/sqlite/go/models.go

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

internal/endtoend/testdata/insert_default_values/sqlite/go/query.sql.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: InsertWorkspace :exec
2+
INSERT INTO workspace DEFAULT VALUES;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE workspace (
2+
id INTEGER PRIMARY KEY AUTOINCREMENT
3+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "sqlite",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,25 @@ func (c *cc) convertInsert_stmtContext(n *parser.Insert_stmtContext) ast.Node {
875875
ReturningList: c.convertReturning_caluseContext(n.Returning_clause()),
876876
}
877877

878-
if n.Select_stmt() != nil {
878+
// Check if this is a DEFAULT VALUES insert
879+
hasDefaultValues := false
880+
for _, child := range n.GetChildren() {
881+
if term, ok := child.(antlr.TerminalNode); ok {
882+
if term.GetSymbol().GetTokenType() == parser.SQLiteParserDEFAULT_ {
883+
hasDefaultValues = true
884+
break
885+
}
886+
}
887+
}
888+
889+
if hasDefaultValues {
890+
// For DEFAULT VALUES, create an empty select statement
891+
insert.SelectStmt = &ast.SelectStmt{
892+
FromClause: &ast.List{},
893+
TargetList: &ast.List{},
894+
ValuesLists: &ast.List{Items: []ast.Node{&ast.List{}}}, // Single empty values list
895+
}
896+
} else if n.Select_stmt() != nil {
879897
if ss, ok := c.convert(n.Select_stmt()).(*ast.SelectStmt); ok {
880898
ss.ValuesLists = &ast.List{}
881899
insert.SelectStmt = ss

internal/engine/sqlite/parser/SQLiteParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ insert_stmt:
359359
COMMA OPEN_PAR expr ( COMMA expr)* CLOSE_PAR
360360
)*
361361
| select_stmt
362+
| DEFAULT_ VALUES_
362363
) upsert_clause? returning_clause?
363364
)
364-
| DEFAULT_ VALUES_
365365
;
366366

367367
upsert_clause:

internal/engine/sqlite/parser/SQLiteParser.interp

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

internal/engine/sqlite/parser/sqlite_parser.go

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

0 commit comments

Comments
 (0)