snowflake-sqlalchemy icon indicating copy to clipboard operation
snowflake-sqlalchemy copied to clipboard

SNOW-372459: Missing the way to to do `array_contains`?

Open petercho42 opened this issue 3 years ago • 2 comments

How do I write a SQLAlchemy ORM that will do ARRAY_CONTAINS for an array field in Snowflake? I googled and searched in this repo but couldn't find anything.

  • Example: query = session.query(TestUser).filter(TestUser.numbers.contains([some_int])).all()

  • Issue: Above results in LIKE, not ARRAY_CONTAINS

petercho42 avatar Jun 25 '21 16:06 petercho42

@petercho42 This is not currently supported. We will look at this as part of our broader initiatives around ORM support.

sfc-gh-hkapre avatar Jul 15 '21 20:07 sfc-gh-hkapre

To use a function like ARRAY_CONTAINS you'll need to define the function in SQLAlchemy and apply them into ORM filters. Here's an example:

Goal: Check if array [1, 2] exists as an independent element in a 2D array A 2D array input that will match could be [ [1, 0], [2, 3], [1, 2], [3, 0] ] And an input that will not match could be: [ [1, 0], [2, 3], [1, 3], [3, 0] ]

from sqlalchemy import func

# Construct the literal array:
check_arr = func.ARRAY_CONSTRUCT(1, 2)

# Apply ARRAY_CONTAINS
contains_arr = func.ARRAY_CONTAINS(check_arr, TestUser.numbers)

# Filter for positive matches by checking the return of ARRAY_CONTAINS
query = session.query(TestUser).filter(contains_arr == True).all()

sfc-gh-hachouraria avatar Aug 31 '22 08:08 sfc-gh-hachouraria

To clean up and re-prioritize bugs and feature requests we are closing all issues older than 6 months as of Apr 1, 2023. If there are any issues or feature requests that you would like us to address, please re-create them. For urgent issues, opening a support case with this link Snowflake Community is the fastest way to get a response

github-actions[bot] avatar Apr 05 '23 01:04 github-actions[bot]