Skip to content

Cannot cast using JSON.JSONPathType #8216

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

Closed
CaselIT opened this issue Jul 6, 2022 · 12 comments
Closed

Cannot cast using JSON.JSONPathType #8216

CaselIT opened this issue Jul 6, 2022 · 12 comments
Assignees
Labels
datatypes things to do with database types, like VARCHAR and others postgresql use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Milestone

Comments

@CaselIT
Copy link
Member

CaselIT commented Jul 6, 2022

In some dialects that use server side binding, like psycopg and asyncpg, queries that use jsonb_path_query_array and similar function that take jsonpath arguments fail without cast, and casting using JSON.JSONPathType errors with
AttributeError: 'JSONPathType' object has no attribute '_compiler_dispatch'

Example:

import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB

m = sa.MetaData()
t = sa.Table("t", m, sa.Column("addresses", JSONB))

e = sa.create_engine('postgresql+psycopg://scott:[email protected]:5432/test',echo=True)
m.create_all(e)

q = sa.select(
    sa.func.jsonb_path_query_array(
        t.c.addresses.op("->")("addresses"), "$.address.id"
    )
)

with e.connect() as c:
    c.execute(q)

casting instead to jsonpath with a literal columns works:

q = sa.select(
    sa.func.jsonb_path_query_array(
        t.c.addresses.op("->")("addresses"), sa.literal_column("'$.address.id'::jsonpath")
    )
)
@CaselIT CaselIT added bug Something isn't working postgresql labels Jul 6, 2022
@zzzeek
Copy link
Member

zzzeek commented Jul 6, 2022

OK if jsonpath is really a type we can cast to that using sqltypes.JSON.JSONPathType, just needs a visit set up in postgresql/base.py -> PGTypeCompiler

@zzzeek zzzeek added datatypes things to do with database types, like VARCHAR and others use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated and removed bug Something isn't working labels Jul 6, 2022
@zzzeek zzzeek added this to the 1.4.x milestone Jul 6, 2022
@zzzeek
Copy link
Member

zzzeek commented Jul 6, 2022

i dont this can be automatic however, we just document it

@CaselIT
Copy link
Member Author

CaselIT commented Jul 6, 2022

i dont this can be automatic however, we just document it

not unless we implement a function for jsonb_path_query_array with the proper types I guess. probably not worth it since it's safe to say that's not very common

@zzzeek
Copy link
Member

zzzeek commented Jul 6, 2022

right we could implement it but then it's like there's this one JSON function that's randomly implemented and the rest aren't, which is just not easy to explain

@CaselIT CaselIT self-assigned this Jul 8, 2022
@CaselIT
Copy link
Member Author

CaselIT commented Jul 8, 2022

Do you want to keep in inside the JSON class or should it have its own type outside of it (I'm guessing called JSONPATH)

@zzzeek
Copy link
Member

zzzeek commented Jul 8, 2022

I think we can make an alias for it JsonPath and it should be a genric type, that is, wont fail if used on other backends

@zzzeek
Copy link
Member

zzzeek commented Jul 8, 2022

within PG dialect, sure we can add JSONPATH directly as well, that will be the type map for JsonPath

@CaselIT
Copy link
Member Author

CaselIT commented Jul 8, 2022

ah ok, JsonPath is generic, I though it was in pg only.

I'll take a look

@sqla-tester
Copy link
Collaborator

Federico Caselli has proposed a fix for this issue in the main branch:

JSONPathType can be used in casts in PostgreSQL https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4019

@CaselIT
Copy link
Member Author

CaselIT commented Aug 2, 2022

I think we can make an alias for it JsonPath and it should be a genric type, that is, wont fail if used on other backends

I'm not sure what to render in the other backends. At the moment in the patch it fails with unsupported compilation

@Lawouach
Copy link

Lawouach commented Oct 7, 2022

Hello gentlemen,

Any chance this would have been backported to 1.4.x? Or do you advise I upgarde to 2.0?

Thanks,

@Lawouach
Copy link

Lawouach commented Oct 7, 2022

Well, I went and used literal_column with 1.4 and that works so that should be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datatypes things to do with database types, like VARCHAR and others postgresql use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Projects
None yet
Development

No branches or pull requests

4 participants