Skip to content

[Feature] Support Lateral view syntax #6746

@EmmyMiao87

Description

@EmmyMiao87

Search before asking

  • I had searched in the issues and found no similar issues.

Description

The functions in SQL can be divided into the following categories according to the number of input and output rows:

  1. Scalar function
  2. Aggregate function
  3. Table Function: One line of input corresponds to multiple lines of output.

Doris currently supports the first two functions, but does not support the third.

Table Function is usually used in operations such as column to row. For example, the following table data:

id names
1 a, b, c
2 d, e, f

If you want to output multiple comma-separated data in the names column in rows as follows:

id name
1 a
1 b
1 c
2 d
2 e
2 f

You need to use the Table Function, which converts one row of data into multiple rows.

Common Table Function, such as explode in hive, or unnest in presto, can convert array or list types into multi-line output. like:

select explode(array_col1) from tbl;

Design

Syntax

lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)
fromClause: FROM baseTable (lateralView)

Multiple Lateral Views

A FROM clause can have multiple LATERAL VIEW clauses. Subsequent LATERAL VIEWS can reference columns from any of the tables appearing to the left of the LATERAL VIEW.

For example, the following could be a valid query:

SELECT * FROM exampleTable
LATERAL VIEW explode_split(col1, ",") myTable1 AS myCol1
LATERAL VIEW explode(col2, ",") myTable2 AS myCol2;

TableFunctionNode

The result of Table Function is multiple rows, which can also be regarded as a relational table. So we need to implement a new TableFunctionNode. It contains the TableFunction that needs to be processed. For each row of TableFunctionNode input, execute the corresponding TableFunction, and then perform the correlation operation and output.

MySQL [test]> explain select k1, e1, e2 from test_explode lateral view explode_split (col1, ",") tmp as e1  lateral view explode_split (col2, ",") tmp as e2;
+---------------------------------------------------------------------------+
| Explain String                                                            |
+---------------------------------------------------------------------------+
| PLAN FRAGMENT 0                                                           |
|  OUTPUT EXPRS:`k1` | `e1` | 'e2'                                                 |
|                                                                           |
|   RESULT SINK                                                             |
|                                                                           |
|   1:TABLE FUNCTION NODE                                                   |
|   |  table function: explode_split(`col1`, ','), explode_split('col2', ',')                             |
|   |                                                                       |
|   0:OlapScanNode                                                          |
|      TABLE: test_explode                                                  |
+---------------------------------------------------------------------------+

Use case

No response

Related issues

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureCategorizes issue or PR as related to a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions