Table of Contents |
---|
Lateral View Syntax
Code Block |
---|
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
fromClause: FROM baseTable (lateralView)*
|
...
Lateral view is used in conjunction with user-defined table generating functions such as explode()
. As mentioned in Built-in Table-Generating Functions, a UDTF generates one zero or more output rows for each input row. A lateral view first applies the UDTF to each row of base table and then joins resulting output rows to the input rows to form a virtual table having the supplied table alias.
...
An example table with two rows:
pageid | adid_list | |
---|---|---|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3b1d8a78-0833-4af4-89a2-d681c7d058f0"><ac:plain-text-body><![CDATA[ | front_page | [1, 2, 3] ]]></ac:plain-text-body></ac:structured-macro><ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="79c8f565-45a7-4702-9113-31a86bcedb5a"><ac:plain-text-body><![CDATA[ |
contact_page | [3, 4, 5] | ]]></ac:plain-text-body></ac:structured-macro> |
and the user would like to count the total number of times an ad appears across all pages.
A lateral view with explode() can be used to convert adid_list
into separate rows using the query:
Code Block | ||||
---|---|---|---|---|
| ||||
SELECT pageid, adid
FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;
|
...
Then in order to count the number of times a particular ad appears, count/group by can be used:
Code Block | ||||
---|---|---|---|---|
| ||||
SELECT adid, count(1)
FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid
GROUP BY adid;
|
...
For example, the following could be a valid query:
Code Block | ||||
---|---|---|---|---|
| ||||
SELECT * FROM exampleTable
LATERAL VIEW explode(col1) myTable1 AS myCol1
LATERAL VIEW explode(myCol1) myTable2 AS myCol2;
|
...
Array<int> col1 | Array<string> col2 | ||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="546e13a9-f415-48f9-9ad2-c6f7a0021dec"><ac:plain-text-body><![CDATA[ [1, 2] | [a", "b", "c"] | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7db2e0ec-3050-414a-a3b3-31dbfcb5f574"><ac:plain-text-body><![CDATA[ |
[3, 4] | [d", "e", "f"] | ]]></ac:plain-text-body></ac:structured-macro> |
The query:
Code Block | ||||
---|---|---|---|---|
| ||||
SELECT myCol1, col2 FROM baseTable
LATERAL VIEW explode(col1) myTable1 AS myCol1;
|
...
int mycol1 | Array<string> col2<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6009794b-cdca-41c4-90fe-bd83e4afb318"><ac:plain-text-body><![CDATA[ | |||
1 | [a", "b", "c"] ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b60232ee-28f6-4ba2-aeef-48be125072c5"><ac:plain-text-body><![CDATA[ | ||
2 | [a", "b", "c"]]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="09291875-6fc4-4c2d-ac98-3f58a3b8c27b"><ac:plain-text-body><![CDATA[ | ||
3 | [d", "e", "f"] | |||
]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="40964699-744f-467a-9607-63ad23ab8d8a"><ac:plain-text-body><![CDATA[ | 4 | [d", "e", "f"] | ]]></ac:plain-text-body></ac:structured-macro> |
A query that adds an additional LATERAL VIEW:
Code Block | ||||
---|---|---|---|---|
| ||||
SELECT myCol1, myCol2 FROM baseTable
LATERAL VIEW explode(col1) myTable1 AS myCol1
LATERAL VIEW explode(col2) myTable2 AS myCol2;
|
...
Info | ||
---|---|---|
| ||
Introduced in Hive version 0.12.0 |
User The user can specify the optional OUTER
keyword to make a row with NULL(s) for UDTF side in case the UDTF does not make any rows keyword to generate rows even when a LATERAL VIEW
usually would not generate a row. This happens when the UDTF used does not generate any rows which happens easily with explode
when the column to explode is empty. In this case the source row would never appear in the results. OUTER
can be used to prevent that and rows will be generated with NULL
values in the columns coming from the UDTF.
For example, the following query returns an empty result:
Code Block | ||
---|---|---|
| ||
SELEC select * fromFROM src LATERAL VIEW explode(array()) C AS a limit 10; |
But with the OUTER
keyword, keyword
Code Block | ||
---|---|---|
| ||
SELECT select * fromFROM src LATERAL VIEW OUTER explode(array()) C AS a limit 10; |
Will it will produce:
238 val_238 NULL
86 val_86 NULL
311 val_311 NULL
27 val_27 NULL
165 val_165 NULL
409 val_409 NULL
255 val_255 NULL
278 val_278 NULL
98 val_98 NULL
...