Skip to content

support direct mult-dimensional ARRAY access based on explicit dimensions #3487

Closed

Description

@sqlalchemy-bot

Migrated issue, originally created by Andrey Semenov

Here's just an example:

    weights = Column(ARRAY(DECIMAL(precision=2, asdecimal=False), dimensions=2), nullable=False, default=[])

then in query:

CallServiceCampaign.weights[
    func.idx(CallServiceCampaign.goods_ids, literal(goods_id))
][1] != None,

produces:

NotImplementedError: Operator 'getitem' is not supported on this expression

I think this is because https://bitbucket.org/zzzeek/sqlalchemy/src/447ee0af1d2fbb95f2f1244de301f2fe4a87a72f/lib/sqlalchemy/dialects/postgresql/base.py?at=rel_1_0_6#cl-911 doesn't handle dimensions parameter and always returns the underlying array's class on the first non-sliced reference.

Activity

  1. sqlalchemy-bot commented on Jul 15, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Michael Bayer (@zzzeek) wrote:

    Note that a pg ARRAY with no explicit dimensions is N-dimensional, and I'm not sure if that can be handled without explicit casts.

    Below, please use the ARRAY_D workaround for now.

    from sqlalchemy.dialects.postgresql import ARRAY, dialect
    from sqlalchemy import Integer, column, type_coerce, func
    
    c = column('x', ARRAY(Integer(), dimensions=2))
    
    print c[5].type  # produces INTEGER, should be ARRAY(INTEGER, dimensions=1)
    
    
    # fails because we aren't an array
    
    try:
        print(c[5][2].type)
    except NotImplementedError as e:
        print e
    
    # workaround
    print type_coerce(c[5], ARRAY(Integer))[2].type
    
    # better workaround
    
    
    class ARRAY_D(ARRAY):
        class Comparator(ARRAY.Comparator):
            def __getitem__(self, index):
                super_ = super(ARRAY_D.Comparator, self).__getitem__(index)
                if not isinstance(index, slice) and self.type.dimensions > 1:
                    super_ = type_coerce(
                        super_,
                        ARRAY_D(
                            self.type.item_type,
                            dimensions=self.type.dimensions - 1)
                    )
                return super_
        comparator_factory = Comparator
    
    c2 = column('x', ARRAY_D(Integer(), dimensions=2))
    print c2[5][2].type
    
    print(
        c2[func.idx(column('q'))][2].compile(dialect=dialect())
    )
    
  2. sqlalchemy-bot commented on Jul 15, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • removed labels: bug
    • added labels: feature
  3. sqlalchemy-bot commented on Jul 15, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • changed title from "Multi-dimensional ARRAY in Postgres aren't correct" to "support direct mult-dimensional ARRAY access based"
  4. sqlalchemy-bot commented on Jul 15, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • set milestone to "1.1"
  5. sqlalchemy-bot commented on Jul 15, 2015

    @sqlalchemy-bot
    CollaboratorAuthor
  6. sqlalchemy-bot commented on Jul 24, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Michael Bayer (@zzzeek) wrote:

    see also #3499, where we have similar issues with JSON. We need to determine for indexable types a clear picture for what type the return values should be. For ARRAY it is fairly simple. For a JSON, perhaps some option to specify a schema, though that seems quite ominous.

  7. sqlalchemy-bot commented on Aug 11, 2015

    @sqlalchemy-bot
    CollaboratorAuthor
  8. sqlalchemy-bot commented on Aug 17, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Michael Bayer (@zzzeek) wrote:

    → ceeb033

  9. sqlalchemy-bot commented on Aug 17, 2015

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • changed status to closed
  10. added this to the 1.1 milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions