With Lateral Joins you can combine a data source with any array expression. As a result, you will get a new data source, where every record of the original one will be joined with the values of the lateral array expression.
Assume you have a source where elements is an array field:
source
elements
Then a Lateral Join of source with elements is a new table, where every record of source will be joined with all the single items of the value of elements for that record:
In this way the single elements of the array becomes available and can be used as a normal field in the query.
A query using lateral joins looks like a regular query apart from the definition of its source:
SELECT (STREAM|TABLE) <projection> FROM <source> LATERAL <lateralArrayExpression> AS <lateralAlias> WHERE <filterExpression>;
<source>
<lateralAlias>
Assume you have a topic batched_readings populated with the following records:
batched_readings
As you can see, readings is a field containing arrays of integers.
readings
We define a processor like this:
INSERT INTO readings SELECT STREAM meter_id, reading FROM batched_readings LATERAL readings AS reading WHERE reading > 90
The processor will emil the following records:
Things to notice:
reading
WHERE
_key
expression AS _key
a
b
90
d
It is possible to use multiple LATERAL joins in the same FROM clause.
LATERAL
FROM
Assume you have a topic batched_nested_readings populated with the following records:
Notice how nested_readings contains arrays of arrays of integers.
nested_readings
To get the same results of the previous example, we use a first lateral join to unpack the first level of nested_readings into an array that we call readings. We then define a second lateral join on readings to extract the single values:
INSERT INTO readings SELECT STREAM meter_id, reading FROM batched_readings LATERAL nested_readings AS readings LATERAL readings as reading WHERE reading > 90
In the previous example we used a simple field as the <lateralArrayExpression>. In the section we will see how any array expression can be used for it.
<lateralArrayExpression>
Assume you have a topic day_night_readings populated with the following records:
day_night_readings
We can make use of Array Functions to lateral join day_night_readings on the concatenation of the two readings fields:
INSERT INTO readings SELECT STREAM meter_id, reading FROM batched_readings LATERAL flatten([readings_day, readings_night]) AS reading WHERE reading > 90
The processor such defined will emit the records
On this page