Skip to content

sqlite: Missing parameters in ORDER BY and OFFSET clauses inside DELETE/UPDATE statements  #4706

Backlog

Description

@manuelschoene

Version

Other (main, v1.31.1-105-g3ad8b20fb)

What happened?

On SQLite, parameters in the ORDER BY and OFFSET of an UPDATE or DELETE are silently dropped from the generated code. With the core analyzer (SQLCEXPERIMENT=coreanalyzer) the LIMIT parameter is dropped as well. The generated methods therefore bind fewer arguments than the query has placeholders and fail at runtime (sql: expected N arguments, got M / missing argument with index N, as in #4211).

SQLite only accepts this syntax when built with SQLITE_ENABLE_UPDATE_DELETE_LIMIT, but sqlc parses it deliberately (internal/engine/sqlite/parse.go), so the generated code should be complete.

Generated today (default path):

type DeleteLimitOffsetParams struct {
	Name  string
	Limit int64
	// Offset missing
}

type DeleteOrderByParamParams struct {
	Name  string
	Limit int64
	// parameter in ORDER BY abs(score - ?) missing
}

type UpdateOrderByParamParams struct {
	Score int64
	Name  string
	// parameter in ORDER BY abs(score - ?) missing
}

Core path: DeleteLimitOffset(ctx, name string) and DeleteOrderByParam(ctx, name string) take only name, and UpdateLimitOffsetParams is missing both Limit and Offset.

Expected: every ? becomes a parameter, named and typed the same way as for the equivalent SELECT. For example, DeleteLimitOffsetParams{Name, Limit, Offset}, DeleteOrderByParamParams{Name, Score, Limit} and UpdateOrderByParamParams{Score, Name, Score_2}.

Cause: the meyer parser fills OrderBy and Limit.Offset on meyer.DeleteStmt / meyer.UpdateStmt, but the conversion into sqlc's AST drops them:

  • limitCount (internal/engine/sqlite/convert.go:444-451) keeps only Limit.Count. Its comment says that the OFFSET and ORDER BY are dropped.
  • ast.DeleteStmt and ast.UpdateStmt only have LimitCount, with no field for a sort clause or an offset, so internal/compiler/find_params.go never sees those parameters.
  • In the core analyzer, analyzeUpdate / analyzeDelete (internal/core/analyzer/dml.go) do not visit LimitCount at all.

Relevant log output

No error: sqlc generate succeeds, but the generated code binds too few arguments.

Database schema

CREATE TABLE t (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    score INTEGER NOT NULL
);

SQL queries

-- name: DeleteLimitOffset :exec
DELETE FROM t WHERE name = ? ORDER BY score DESC LIMIT ? OFFSET ?;

-- name: DeleteOrderByParam :exec
DELETE FROM t WHERE name = ? ORDER BY abs(score - ?) LIMIT ?;

-- name: UpdateLimitOffset :exec
UPDATE t SET score = ? WHERE name = ? ORDER BY id LIMIT ? OFFSET ?;

-- name: UpdateOrderByParam :exec
UPDATE t SET score = ? WHERE name = ? ORDER BY abs(score - ?) LIMIT 1;

Configuration

{
  "version": "1",
  "packages": [
    {
      "engine": "sqlite",
      "path": "go",
      "name": "querytest",
      "schema": "schema.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

SQLite

What type of code are you generating?

Go

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions