-
Notifications
You must be signed in to change notification settings - Fork 978
SQLite: Coerce jsonb columns to json before returning to Go code #3968
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
Changes from 1 commit
3695ee3
967f653
0555230
c62c6e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This one follows up the discussion in #3953 to try and make the `jsonb` data type in SQLite usable (see discussion there, but I believe that it's currently not). According the SQLite docs on JSONB [1], it's considered a format that's internal to the database itself, and no attempt should be made to parse it elsewhere: > JSONB is not intended as an external format to be used by > applications. JSONB is designed for internal use by SQLite only. > Programmers do not need to understand the JSONB format in order to use > it effectively. Applications should access JSONB only through the JSON > SQL functions, not by looking at individual bytes of the BLOB. Currently, when trying to use a `jsonb` column in SQLite, sqlc ends up returning the internal binary data, which ends up being unparsable in Go: riverdrivertest.go:3030: Error Trace: /Users/brandur/Documents/projects/river/internal/riverinternaltest/riverdrivertest/riverdrivertest.go:3030 Error: Not equal: expected: []byte{0x7b, 0x22, 0x66, 0x6f, 0x6f, 0x22, 0x3a, 0x20, 0x22, 0x62, 0x61, 0x72, 0x22, 0x7d} actual : []byte{0x8c, 0x37, 0x66, 0x6f, 0x6f, 0x37, 0x62, 0x61, 0x72} Diff: --- Expected +++ Actual @@ -1,3 +1,3 @@ -([]uint8) (len=14) { - 00000000 7b 22 66 6f 6f 22 3a 20 22 62 61 72 22 7d |{"foo": "bar"}| +([]uint8) (len=9) { + 00000000 8c 37 66 6f 6f 37 62 61 72 |.7foo7bar| } Test: TestDriverRiverSQLite/QueueCreateOrSetUpdatedAt/InsertsANewQueueWithDefaultUpdatedAt The fix is that we should make sure to coerce `jsonb` columns back to `json` before returning. That's what this pull request does, intercepting `SELECT *` and wrapping `jsonb` columns with a `json(...)` invocation. I also assign `json` and `jsonb` a `[]byte` data type by default so they don't end up as `any`, which isn't very useful. `[]byte` is consistent with the default for `pgx/v5`. [1] https://sqlite.org/jsonb.html
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "strings" | ||
|
|
||
| "github.com/sqlc-dev/sqlc/internal/config" | ||
| "github.com/sqlc-dev/sqlc/internal/engine/sqlite" | ||
| "github.com/sqlc-dev/sqlc/internal/source" | ||
| "github.com/sqlc-dev/sqlc/internal/sql/ast" | ||
| "github.com/sqlc-dev/sqlc/internal/sql/astutils" | ||
|
|
@@ -149,6 +150,17 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node) | |
| if counts[cname] > 1 { | ||
| cname = tableName + "." + cname | ||
| } | ||
| // Under SQLite, neither json nor jsonb are real data types, and | ||
| // rather just of type blob, so database drivers just return | ||
| // whatever raw binary is stored as values. This is a problem | ||
| // for jsonb, which is considered an internal format to SQLite | ||
| // and no attempt should be made to parse it outside of the | ||
| // database itself. For jsonb columns in SQLite, wrap returned | ||
| // columns in `json(col)` to coerce the internal binary format | ||
| // to JSON parsable by the user-space application. | ||
| if _, ok := c.parser.(*sqlite.Parser); ok && column.DataType == "jsonb" { | ||
|
||
| cname = "json(" + cname + ")" | ||
| } | ||
| cols = append(cols, cname) | ||
| } | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| -- name: InsertFoo :exec | ||
| INSERT INTO foo ( | ||
| a, | ||
| b, | ||
| c, | ||
| d | ||
| ) VALUES ( | ||
| @a, | ||
| @b, | ||
| @c, | ||
| @d | ||
| ) RETURNING *; | ||
|
|
||
| -- name: SelectFoo :exec | ||
| SELECT * FROM foo; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CREATE TABLE foo ( | ||
| a json not null, | ||
| b jsonb not null, | ||
| c json, | ||
| d jsonb | ||
| ); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "version": "1", | ||
| "packages": [ | ||
| { | ||
| "path": "go", | ||
| "engine": "postgresql", | ||
| "sql_package": "pgx/v5", | ||
| "name": "querytest", | ||
| "schema": "schema.sql", | ||
| "queries": "query.sql" | ||
| } | ||
| ] | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| -- name: InsertFoo :exec | ||
| INSERT INTO foo ( | ||
| a, | ||
| b, | ||
| c, | ||
| d | ||
| ) VALUES ( | ||
| @a, | ||
| @b, | ||
| @c, | ||
| @d | ||
| ) RETURNING *; | ||
|
|
||
| -- name: SelectFoo :exec | ||
| SELECT * FROM foo; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CREATE TABLE foo ( | ||
| a json not null, | ||
| b jsonb not null, | ||
| c json, | ||
| d jsonb | ||
| ); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "version": "1", | ||
| "packages": [ | ||
| { | ||
| "path": "go", | ||
| "engine": "sqlite", | ||
| "name": "querytest", | ||
| "schema": "schema.sql", | ||
| "queries": "query.sql" | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
json.RawMessagepreferred over[]byte?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's what we do for PostgreSQL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, that makes sense. Changed!