Skip to content

Custom Codec doesn't work with DOMAIN types #457

Closed

Description

@kjmph
  • asyncpg version: 0.18.3
  • PostgreSQL version: 11.3
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : Local
  • Python version: 3.7.3
  • Platform: MacOS
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: PyPI
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : Don't Know

When creating a DOMAIN, and using a custom codec, it calls the native type's codec, as opposed to the custom code. Here is the test code that is supposed to test for this:

https://github.com/MagicStack/asyncpg/blob/master/tests/test_codecs.py#L1224-L1239

However, the lambda functions aren't called. It appears the data type is recognized as an int.

Activity

  1. elprans commented on Jul 8, 2019

    @elprans
    Member

    Postgres seems to always report the base type OID in the RowDescription message. Interestingly, ParameterDescription contains the correct domain OID, so we can set up a custom encoder, but not a custom decoder. Given this, I think we should declare custom codecs on domains to be unsupported altogether.

  2. kjmph commented on Jul 8, 2019

    @kjmph
    Author

    That’s unfortunate. I’m sure you’ve gotten this before, however, with some rules on the decoder function couldn’t we support composite types?

    If we removed this check:

    if not introspection.is_scalar_type(typeinfo):
    raise ValueError(
    'cannot use custom codec on non-scalar type {}.{}'.format(
    schema, typename))

    And required the decoder function to do something like x.strip('()').split(','), as well as making the decoder handle Python type conversion, couldn’t asyncpg support composites?

  3. elprans commented on Oct 3, 2019

    @elprans
    Member

    Text I/O for composites is gnarly and wouldn't work very well, since asyncpg would refuse to do text I/O for the component types. Theoretically, this might be possible using the 'tuple' format protocol, but there's quite a few changes to be made to make this work, and I don't see a compelling use case for it.

  4. kjmph commented on Oct 29, 2019

    @kjmph
    Author

    I hear your point around the text I/O processing. The use case was around having a composite type so that one can input the result into a dataclass. row_to_json works for now, as the json type can be registered and decoded to a dictionary. However, it is beneficial to have errors if what is returned doesn't fit into a specific structure. I can close this if you don't think there is value here.

  5. elprans commented on Oct 29, 2019

    @elprans
    Member

    This issue is originally about domains, and that still needs to be resolved properly, so let's keep this open.

    Regarding custom codecs for composite types -- feel free to open a separate issue (with the description of your use case). It's not impossible to implement and there's some value in that, so maybe someone will chime in as well.

  6. kjmph commented on Oct 29, 2019

    @kjmph
    Author

    Right! Sorry, my slow responses made me lose sight of the originating issue. I'll create a new ticket.

  7. added a commit that references this issue on Nov 26, 2020
    1389b7b
  8. added a commit that references this issue on Dec 2, 2020
    50f964f
  9. added a commit that references this issue on Feb 10, 2021
  10. added a commit that references this issue on Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions