prql icon indicating copy to clipboard operation
prql copied to clipboard

Arrays / Nesting

Open max-sixty opened this issue 3 years ago • 3 comments

Another big feature that we haven't thought about are arrays.

There's probably a small group of folks who use these, but they really like them. I've used them very successfully in the past. The newer DBs encourage using them in favor of more joins given the relative scanning vs joining performance.

They're frequently combined with structs so, for example, an Account can contain an array Addresses, each of which has a Street / City / Zip field, etc.

Generally DBs use something like ARRAY_AGG like a standard aggregation function, but instead of reducing to a number, it reduces to a row containing an array.

Malloy uses nest: extensively, I think it works quite well.

If anyone has a fuller proposal for the language design, that would be very welcome. We'd need to think about how both nesting (maybe a simple as a nest function...) and unnesting works.

max-sixty avatar Feb 13 '23 06:02 max-sixty

This would be super helpful for me! It's often right when I get to arrays that the SQL starts to get overwhelming complex, and I'd like to switch to a better thought out language.

nicot avatar Feb 21 '23 16:02 nicot

An example of what we'd want:

from tracks
group album_id (
    aggregate titles = (title | std.collect | std.join ", ") 
)

aljazerzen avatar May 20 '23 09:05 aljazerzen

I just want to be able to do this:

SELECT a.x FROM (SELECT {'x': 1, 'y': 2, 'z': 3} AS a);

Unfortunately it doesn't seem to work:

from table
select ( a.x )

Just gives Unknown name 'a.x'

Structs/arrays are extremely useful when importing JSON.

Timmmm avatar Feb 09 '24 11:02 Timmmm