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

fix(engine/sqlite): added json_tree and json_each definitions #2570

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

orisano
Copy link
Contributor

@orisano orisano commented Aug 3, 2023

fix #1830

@orisano
Copy link
Contributor Author

orisano commented Aug 3, 2023

@kyleconroy I would like to introduce the concept of hidden table for tables like json_tree and sqlite_schema, which are included in the catalog but for which I do not want to generate code.
Do you have any ideas?

@orisano orisano marked this pull request as ready for review August 3, 2023 18:00
@josharian
Copy link
Contributor

I gave this a try locally, as the underlying bug is blocking us. Unfortunately, it still doesn't work for me on this sqlite query:

-- name: LookUpVendors :many
SELECT
	"vendor".*
FROM
	vendor AS "vendor",
	json_each("vendor".benefits) AS "benefit",
	json_each("vendor".states) AS "state"
WHERE
	"state".value IN (@state, "National Coverage")
	AND "benefit".value = @benefit;

Schema (extract):

CREATE TABLE vendor(
	id text PRIMARY KEY,
	name text NOT NULL,
	benefits text NOT NULL,
	states text NOT NULL
);

Error message:

query.sql:10:2: 396: column "value" does not exist

When I try the query in sqlite3 directly, it works.

Thanks so much for working on this!

@josharian
Copy link
Contributor

Also, FYI, this rewrite works in v1.19 but fails with this PR:

-- name: LookUpVendors :many
SELECT
	vendor.*
FROM
	vendor,
	json_each(vendor.benefits) AS b,
	json_each(vendor.states) AS s
WHERE
	CAST(s.value AS text) IN (@state, "US")
	AND CAST(b.value AS text) = @benefit;

@josharian
Copy link
Contributor

...although that query in v1.19 generates a miscompile, with a method signature like:

func (q *Queries) LookUpVendors(ctx context.Context, benefit interface{}) ([]*Vendor, error) {

(note that it only takes a single benefit argument, not a benefit and a state arg.)

@orisano
Copy link
Contributor Author

orisano commented Aug 4, 2023

@josharian thanks!

workaround:

-- name: LookUpVendors :many
SELECT
	vendor.*
FROM
	vendor,
	json_each(vendor.benefits) AS benefit,
	json_each(vendor.states) AS state
WHERE
	state.value IN (CAST(@state AS TEXT), 'National Coverage')
	AND benefit.value = CAST(@benefit AS TEXT);

You can avoid the problem by doing these two things.

@jamietanna
Copy link

This has resolved the query noted here 👏

@jamietanna
Copy link

Is there anything outstanding on this PR aside from conflicts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sqlite: json_each's value parameter isn't supported
3 participants