Giter VIP home page Giter VIP logo

Comments (2)

JamesRTaylor avatar JamesRTaylor commented on May 10, 2024

This functionality is supported in BigQuery as described here.

One use case is to simplify queries that process arrays. Instead of doing a cross join, you can use the ARRAY function on a subquery. For example, you can do this:

select d.id,
       ARRAY(SELECT CAST(ROW(
          date_trunc('day',s.timestamp),
          s.user_id,
          s.path_id,
          s.accept_source
         ) as ROW(occurred_at TIMESTAMP, 
                  id varchar, 
                  path_id varchar, 
                  accept_source varchar))
         FROM UNNEST(ARRAY_CONCAT(
               previous_states, 
               current_state) s
       ) AS all_states
FROM my_data d

instead of this:

WITH arrayed_data AS (
    SELECT d.created_at, d.id,
           ARRAY_CONCAT(d.previous_states,
               current_state) as accepted
    FROM my_data d
)
select all_a.id,
    ARRAY_AGG(CAST(ROW(
          date_trunc('day',a.timestamp),
          a.user_id,
          a.path_id,
          a.accept_source
         ) as ROW(occurred_at TIMESTAMP, 
                  id varchar, 
                  path_id varchar, 
                  accept_source varchar)))
    FROM arrayed_data as all_a
    CROSS JOIN UNNEST(all_a.accepted) AS a
    GROUP BY all_a.id

from trino.

JamesRTaylor avatar JamesRTaylor commented on May 10, 2024

Below are a few standalone tests that show the current and desired behavior.

Example of query we'd like to support:

SELECT a,
    ARRAY(SELECT cast(row(d.x, d.y) as row(e VARCHAR, f VARCHAR)) 
                 FROM UNNEST(c) as d)
FROM (VALUES(1,0,
         ARRAY[cast(row('a','1') as row(x varchar, y varchar)),
               cast(row('b','2') as row(x varchar, y varchar)),
               cast(row('c','3') as row(x varchar, y varchar))])) t(a,b,c)

Desired output:

Row # a d
1 1 a, 1
b, 2
c, 3

Example of query that produces the same output today, but requires an aggregation:

SELECT a,array_agg(cast(row(d.x, d.y) as row(e VARCHAR, f VARCHAR))) 
FROM (VALUES(1,0,
         ARRAY[cast(row('a','1') as row(x varchar, y varchar)),
               cast(row('b','2') as row(x varchar, y varchar)),
               cast(row('c','3') as row(x varchar, y varchar))])) t(a,b,c) 
CROSS JOIN UNNEST(c) as d
GROUP BY a

Example of query that produces one row per array element:

SELECT a,cast(row(d.x, d.y) as row(e VARCHAR, f VARCHAR)) 
FROM (VALUES(1,0,
         ARRAY[cast(row('a','1') as row(x varchar, y varchar)),
               cast(row('b','2') as row(x varchar, y varchar)),
               cast(row('c','3') as row(x varchar, y varchar))])) t(a,b,c) 
CROSS JOIN UNNEST(c) as d

Output:

Row # a d
1 1 a, 1
2 1 b, 2
3 1 c, 3

from trino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.