question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Querying Semistructured Data with Column Elements

See original GitHub issue

Please answer these questions before submitting your issue. Thanks!

  1. What version of Python are you using (python --version)? 3.6.5

  2. What operating system and processor architecture are you using (python -c 'import platform; print(platform.platform())')? Darwin-16.7.0-x86_64-i386-64bit

  3. Question: My question is how do I use semistructured data in my sqlalchemy orm queries. I am interested in referencing the semistructured data in the SELECT, JOINs and the WHERE clause.

I would like to generate a query like this with snowflake-sqlalchemy, however I’m not sure how to generate the snowflake semistructured data syntax table.column_name:key:

SELECT activity.blob:document_id
FROM event
JOIN event ON activity.blob:event_id == event.id
WHERE activity.blob:document_id > 5

Thank you for your help, Alana

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
ztanecommented, May 8, 2019

The JSON column type as it works in PostgreSQL would allow writing the query as

    Query(Activity)
        .join(Event, Activity.blob['event_id'] == Event.id)
        .filter(Activity.blob.blob['document_id'] > 5)

which is a significant improvement

1reaction
alanapradhancommented, Aug 21, 2018

Here is a possible solution, but it seems a little hacky and verbose .op(':')(text('key')):

from sqlalchemy.orm import Query
from sqlalchemy import text
(
    Query(Activity)
        .join(Event, Activity.blob.op(':')(text('event_id')) == Event.id)
        .filter(Activity.blob.op(':')(text('document_id')) > 5)
)
SELECT 
    activity.id,
    activity.blob
FROM activity
JOIN event ON event.id = (activity.blob : event_id)
WHERE (activity.blob : document_id) > 5
Read more comments on GitHub >

github_iconTop Results From Across the Web

Querying Semi-structured Data - Snowflake Documentation
This topic explains how to use special operators and functions to query complex hierarchical data stored in a VARIANT.
Read more >
Querying Snowflake Semi-Structured Data: 2 Critical Aspects
This page details how to query Snowflake Semi-structured data. It also details how you can cast data types and then use the LATERAL...
Read more >
Querying semistructured data - Amazon Redshift
In Amazon Redshift, you can work with the PartiQL language for SQL-compatible access to semistructured data.
Read more >
Query semi-structured data in Databricks
Create a table with highly nested data; Extract a top-level column; Extract nested fields; Extract values from arrays; Cast values; NULL ...
Read more >
Query semi-structured data in Azure Databricks - Microsoft Learn
You specify nested fields through dot notation or using brackets. When you use brackets, columns are matched case sensitively. SQL Copy.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found