Skip to main content

Lateral View

Use LATERAL VIEW in a VeloDB Cloud warehouse to expand a row into multiple rows. Combined with generator functions (such as EXPLODE), it expands a collection-typed field in a row into multiple rows and joins the expanded result, treated as a virtual table, with the original row.

Applicable scenarios​

When a row of data contains an enumerable collection such as an array or list, and you want to split each element of the collection into a separate row for analysis in a SQL query, you can use LATERAL VIEW. Typical scenarios include:

  • Expanding an array field in a row and outputting each element together with the other original columns.
  • Performing aggregation, filtering, or joining with other tables on each element of the collection separately.
  • Using the output of a generator function (such as EXPLODE, EXPLODE_SPLIT) as a virtual table in a query.

Syntax​

LATERAL VIEW generator_function ( expression [, ...] ) table_identifier AS column_identifier [, ...]

Parameters​

ParameterDescription
generator_functionA generator function, such as EXPLODE or EXPLODE_SPLIT.
table_identifierThe alias of the virtual table produced by generator_function.
column_identifierThe column alias used to name the output rows. The number of column aliases must match the number of columns returned by the generator function.

Example: Expand rows with LATERAL VIEW​

The following example shows how to use LATERAL VIEW to perform a column-to-row query.

1. Prepare the data​

In the SQL Editor or a connected SQL client, select a database where your warehouse SQL user can create tables and insert and query data. Create a person table and insert the sample rows:

CREATE TABLE `person` (
`id` int(11) NULL,
`name` text NULL,
`age` int(11) NULL,
`class` int(11) NULL,
`address` text NULL
) ENGINE=OLAP
UNIQUE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1;

INSERT INTO person VALUES
(100, 'John', 30, 1, 'Street 1'),
(200, 'Mary', NULL, 1, 'Street 2'),
(300, 'Mike', 80, 3, 'Street 3'),
(400, 'Dan', 50, 4, 'Street 4');

2. Run the LATERAL VIEW query​

Use LATERAL VIEW together with the EXPLODE function to perform a Cartesian expansion of each element in the array ARRAY(30, 60) with each row of the person table:

SELECT * FROM person
LATERAL VIEW EXPLODE(ARRAY(30, 60)) tableName AS c_age;

3. View the result​

The query returns eight rows: two generated values for each of the four people. Row display order can vary without an outer ORDER BY. The elapsed time below is an upstream sample, not a VeloDB Cloud performance measurement:

+------+------+------+-------+----------+-------+
| id | name | age | class | address | c_age |
+------+------+------+-------+----------+-------+
| 100 | John | 30 | 1 | Street 1 | 30 |
| 100 | John | 30 | 1 | Street 1 | 60 |
| 200 | Mary | NULL | 1 | Street 2 | 30 |
| 200 | Mary | NULL | 1 | Street 2 | 60 |
| 300 | Mike | 80 | 3 | Street 3 | 30 |
| 300 | Mike | 80 | 3 | Street 3 | 60 |
| 400 | Dan | 50 | 4 | Street 4 | 30 |
| 400 | Dan | 50 | 4 | Street 4 | 60 |
+------+------+------+-------+----------+-------+
8 rows in set (0.12 sec)

After you finish, remove the sample table if you no longer need it:

DROP TABLE person;