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

Add support for JSONField #54

Merged
merged 2 commits into from
Apr 10, 2023
Merged

Add support for JSONField #54

merged 2 commits into from
Apr 10, 2023

Conversation

timgraham
Copy link
Collaborator

Fixes #23

# Could possibly use "OR" to check both cases but the number of ORs
# need increases exponentially for nested lookups.
'model_fields.test_jsonfield.TestQuerying.test_has_key_number',
# null issue:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfc-gh-mkeller I'm wondering if have you a sense of whether or not this issue and the below issues mentioning "needs to operate as" (where PARSE_JSON() is needed on the right-hand side of the query) will be solved by snowflakedb/snowflake-connector-python#244? I don't know if it's possible to work around here until then.

Also, feel free to comment on any of the other failures here if you have any suggestions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd make sense if you did "MODEL_FIELDS_NULLABLEJSONMODEL"."VALUE"[123] if the key is an integer and do the "MODEL_FIELDS_NULLABLEJSONMODEL"."VALUE":"123" if the key is a string.
The JSON standard only allows string keys.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your comment refers to test_has_key_number' which I solved last night in the latest commit. I was actually asking about cases like this:

WHERE TO_JSON("MODEL_FIELDS_NULLABLEJSONMODEL"."VALUE":k) = '{"l": "m"}'

where the right-hand side parameter is treated as a string literal rather than as JSON.

A working version of this query is:

WHERE TO_JSON("MODEL_FIELDS_NULLABLEJSONMODEL"."VALUE":k) = PARSE_JSON('{"l": "m"}')

but I'm not sure this is feasible to implement here. I thought maybe when/if the Python connector gains awareness of VARIANT type, the first query might work as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've documented this as a known limitation: #58.

# Not yet implemented in this backend.
supports_json_field = False
# TODO: Not yet implemented in this backend.
supports_json_field_contains = False
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other databases implement JSON_CONTAINS() (@> and <@ on PostgreSQL). Is it unsupported by Snowflake or did I miss an equivalent?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can treat JSON columns as regular VARIANT columns, like:

select *
from (
  select parse_json(column1) as user
  from (
    select * from values
    ('{"name": "Luke", "age": 25}'),
    ('{"name": "Mark", "age": 35}')
  )
) where user ilike '%luke%'
;

We don't have any special JSON variants of this as far as I know

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can (easily?) use regular expressions since the example code you gave will also match JSON keys. I'll call this unsupported, at least for now.

@timgraham timgraham force-pushed the jsonfield branch 2 times, most recently from 6d26ec1 to 7458602 Compare April 8, 2023 20:29
@timgraham timgraham marked this pull request as ready for review April 8, 2023 20:34
SQLInsertCompiler.as_sql() is copied from Django, to be customized in the
next commit adding support for inserting JSONField values.
@timgraham timgraham merged commit d50f0a6 into master Apr 10, 2023
@timgraham timgraham deleted the jsonfield branch April 10, 2023 00:43
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.

Add support for JSONField
3 participants