Here is snippet from aws site:
WITH dataset AS (
SELECT ARRAY[
CAST(
ROW('aws.amazon.com', ROW(true)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
),
CAST(
ROW('news.cnn.com', ROW(false)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
),
CAST(
ROW('netflix.com', ROW(false)) AS ROW(hostname VARCHAR, flaggedActivity ROW(isNew BOOLEAN))
)
] as items
)
SELECT sites.hostname, sites.flaggedActivity.isNew
FROM dataset, UNNEST(items) t(sites)
WHERE sites.flaggedActivity.isNew = true;
And it works! But what t(sites) means? When I try to use real table instead dataset of I got error Table 'site' not found.
It looks very strange - like function which is invoked with UNNEST keyword. Can somebody explain what is this?
FROM dataset, UNNEST(items) t(sites)
UNNEST() is a set returning-function: it produces a series of rows, with one column holding the values from the original array.
This set of rows is also called a derived table. Here t is an alias for the derived table, and site is the name of the (only) column in this derived table.
It might be easier to understand if we add the AS keyword:
FROM dataset, UNNEST(items) AS t(sites)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With