Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite ALTER TABLE ADD COLUMN without type name panics #3375

Open
dvob opened this issue May 10, 2024 · 2 comments · May be fixed by #3378
Open

sqlite ALTER TABLE ADD COLUMN without type name panics #3375

dvob opened this issue May 10, 2024 · 2 comments · May be fixed by #3378
Labels
bug Something isn't working

Comments

@dvob
Copy link

dvob commented May 10, 2024

Version

1.26.0

What happened?

sqlc panics after i've added a migration which adds a generated column which uses a json function

Relevant log output

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xd0 pc=0x10a3189]

goroutine 6 [running]:
github.com/sqlc-dev/sqlc/internal/engine/sqlite.(*cc).convertAlter_table_stmtContext(0x1?, 0xc00012c280)
        github.com/sqlc-dev/[email protected]/internal/engine/sqlite/convert.go:77 +0x709
github.com/sqlc-dev/sqlc/internal/engine/sqlite.(*cc).convert(0x2bc0ae0?, {0x1d8a320?, 0xc00012c280?})
        github.com/sqlc-dev/[email protected]/internal/engine/sqlite/convert.go:1124 +0x251
github.com/sqlc-dev/sqlc/internal/engine/sqlite.(*cc).convertSql_stmtContext(0xc000a68b18, 0xc000196e80)
        github.com/sqlc-dev/[email protected]/internal/engine/sqlite/convert.go:657 +0x3a28
github.com/sqlc-dev/sqlc/internal/engine/sqlite.(*cc).convert(0x2bc27a0?, {0x1d8a620?, 0xc000196e80?})
        github.com/sqlc-dev/[email protected]/internal/engine/sqlite/convert.go:1197 +0x315
github.com/sqlc-dev/sqlc/internal/engine/sqlite.(*Parser).Parse(0x0?, {0x1d84640?, 0xc00037c1a0?})
        github.com/sqlc-dev/[email protected]/internal/engine/sqlite/parse.go:70 +0x579
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseCatalog(0xc0003bf688, {0xc000066490?, 0xc00061a01a?, 0x6?})
        github.com/sqlc-dev/[email protected]/internal/compiler/compile.go:42 +0x242
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseCatalog(...)
        github.com/sqlc-dev/[email protected]/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0xc00061a01a, 0x6}, ...}, ...)
        github.com/sqlc-dev/[email protected]/internal/cmd/generate.go:308 +0x1ce
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
        github.com/sqlc-dev/[email protected]/internal/cmd/process.go:107 +0x877
golang.org/x/sync/errgroup.(*Group).Go.func1()
        golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
        golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x96

Database schema

CREATE TABLE my_data (
	id        INTEGER NOT NULL PRIMARY KEY,
	data      BLOB
);


ALTER TABLE my_data ADD COLUMN object_id AS ( data->>'id' );

-- the following doesn't work either
-- ALTER TABLE my_data ADD COLUMN object_id AS ( json_extract(data, '$.id') );

SQL queries

-- name: GetIDs :many
SELECT object_id FROM my_data;

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "tutorial"
        out: "tutorial"

Playground URL

https://play.sqlc.dev/p/f925d1924e8a01e5ebb440040403c046229225dfd1e42b5f0fb6d839b0d9d3e2

What operating system are you using?

Linux

What database engines are you using?

SQLite

What type of code are you generating?

Go

@dvob dvob added bug Something isn't working triage New issues that hasn't been reviewed labels May 10, 2024
@dvob
Copy link
Author

dvob commented May 12, 2024

It works if you set the type explicitly:

ALTER TABLE my_data ADD COLUMN object_id TEXT AS ( data->>'id' );
-- instead of
ALTER TABLE my_data ADD COLUMN object_id AS ( data->>'id' );

@dvob dvob changed the title sqlite ALTER TABLE ADD COLUMN with generated columns and json functions panics sqlite ALTER TABLE ADD COLUMN without type name panics May 12, 2024
@dvob
Copy link
Author

dvob commented May 12, 2024

It's not related to generated columns or json. In general ALTER TABLE t1 ADD COLUMN c1; without type fails.

dvob added a commit to dvob/sqlc that referenced this issue May 12, 2024
Use type name 'any' for ALTER TABLE t1 ADD COLUMN c1 where no type
name for c1 is provided. This is the same logic as for CREATE TABLE.

Fixes sqlc-dev#3375
@dvob dvob linked a pull request May 12, 2024 that will close this issue
@kyleconroy kyleconroy removed the triage New issues that hasn't been reviewed label Aug 5, 2024
kyleconroy pushed a commit to dvob/sqlc that referenced this issue Nov 25, 2024
Use type name 'any' for ALTER TABLE t1 ADD COLUMN c1 where no type
name for c1 is provided. This is the same logic as for CREATE TABLE.

Fixes sqlc-dev#3375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants