Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ SELECT
SUM(amount * rate) AS amount
FROM
orders,
LATERAL TABLE (rates(order_time))
LATERAL rates(order_time)
WHERE
rates.currency = orders.currency
```
Expand Down
16 changes: 8 additions & 8 deletions docs/content.zh/docs/dev/table/functions/udfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ env.sqlQuery("SELECT GetBeverageName(beverageId) FROM Beverages");

在 Table API 中,表值函数是通过 `.joinLateral(...)` 或者 `.leftOuterJoinLateral(...)` 来使用的。`joinLateral` 算子会把外表(算子左侧的表)的每一行跟跟表值函数返回的所有行(位于算子右侧)进行 (cross)join。`leftOuterJoinLateral` 算子也是把外表(算子左侧的表)的每一行跟表值函数返回的所有行(位于算子右侧)进行(cross)join,并且如果表值函数返回 0 行也会保留外表的这一行。

在 SQL 里面用 `JOIN` 或者 以 `ON TRUE` 为条件的 `LEFT JOIN` 来配合 `LATERAL TABLE(<TableFunction>)` 的使用。
在 SQL 里面用 `JOIN` 或者 以 `ON TRUE` 为条件的 `LEFT JOIN` 来配合 `LATERAL <TableFunction>(...)` 的使用(等价的写法为 `LATERAL TABLE(<TableFunction>(...))`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAK we only add the english text as place holder and let folks translate it to Chinese


下面的例子展示了如何实现一个分隔函数并在查询里调用它,详情可参考[开发指南](#开发指南):

Expand Down Expand Up @@ -1147,17 +1147,17 @@ env
// 在 SQL 里调用注册好的函数
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable, LATERAL TABLE(SplitFunction(myField))");
"FROM MyTable, LATERAL SplitFunction(myField)");
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) ON TRUE");
"LEFT JOIN LATERAL SplitFunction(myField) ON TRUE");

// 在 SQL 里重命名函数字段
env.sqlQuery(
"SELECT myField, newWord, newLength " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) AS T(newWord, newLength) ON TRUE");
"LEFT JOIN LATERAL SplitFunction(myField) AS T(newWord, newLength) ON TRUE");

```
{{< /tab >}}
Expand Down Expand Up @@ -1212,17 +1212,17 @@ env
// 在 SQL 里调用注册好的函数
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable, LATERAL TABLE(SplitFunction(myField))");
"FROM MyTable, LATERAL SplitFunction(myField)");
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) ON TRUE")
"LEFT JOIN LATERAL SplitFunction(myField) ON TRUE")

// 在 SQL 里重命名函数字段
env.sqlQuery(
"SELECT myField, newWord, newLength " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) AS T(newWord, newLength) ON TRUE")
"LEFT JOIN LATERAL SplitFunction(myField) AS T(newWord, newLength) ON TRUE")

```
{{< /tab >}}
Expand Down Expand Up @@ -1312,7 +1312,7 @@ env.from("MyTable")
.select($("*"))

// call registered function in SQL
env.sqlQuery("SELECT * FROM MyTable, LATERAL TABLE(BackgroundFunction(myField))");
env.sqlQuery("SELECT * FROM MyTable, LATERAL BackgroundFunction(myField)");

```

Expand Down
8 changes: 4 additions & 4 deletions docs/content.zh/docs/sql/reference/queries/joins.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ SELECT
o_amount, r_rate
FROM
Orders,
LATERAL TABLE (Rates(o_proctime))
LATERAL Rates(o_proctime)
WHERE
r_currency = o_currency
```
Expand All @@ -285,7 +285,7 @@ SELECT
o_amount, r_rate
FROM
Orders,
LATERAL TABLE (Rates(o_proctime))
LATERAL Rates(o_proctime)
WHERE
r_currency = o_currency
```
Expand Down Expand Up @@ -348,7 +348,7 @@ Table Function
```sql
SELECT order_id, res
FROM Orders,
LATERAL TABLE(table_func(order_id)) t(res)
LATERAL table_func(order_id) t(res)
```

### LEFT OUTER JOIN
Expand All @@ -358,7 +358,7 @@ LATERAL TABLE(table_func(order_id)) t(res)
```sql
SELECT order_id, res
FROM Orders
LEFT OUTER JOIN LATERAL TABLE(table_func(order_id)) t(res)
LEFT OUTER JOIN LATERAL table_func(order_id) t(res)
ON TRUE
```

Expand Down
3 changes: 2 additions & 1 deletion docs/content.zh/docs/sql/reference/queries/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ tableReference:

tablePrimary:
[ TABLE ] tablePath [ dynamicTableOptions ] [systemTimePeriod] [[AS] correlationName]
| LATERAL TABLE '(' functionName '(' expression [, expression ]* ')' ')'
| [ LATERAL ] functionName '(' expression [, expression ]* ')'
| [ LATERAL ] TABLE '(' functionName '(' expression [, expression ]* ')' ')'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If table is optional

Suggested change
| [ LATERAL ] TABLE '(' functionName '(' expression [, expression ]* ')' ')'
| [ LATERAL ] [TABLE] '(' functionName '(' expression [, expression ]* ')' ')'

| [ LATERAL ] '(' query ')'
| UNNEST '(' expression ')'

Expand Down
16 changes: 8 additions & 8 deletions docs/content.zh/docs/sql/reference/queries/vector-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ Flink SQL 提供了 `VECTOR_SEARCH` 表值函数 (TVF) 来在 SQL 查询中执
### 语法

```sql
SELECT * FROM input_table, LATERAL TABLE(VECTOR_SEARCH(
SELECT * FROM input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
top_k,
[CONFIG => MAP['key', 'value']]
))
)
```

### 参数
Expand All @@ -64,32 +64,32 @@ SELECT * FROM input_table, LATERAL TABLE(VECTOR_SEARCH(
```sql
-- 基本用法
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
10
));
);

-- 带配置选项
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
10,
MAP['async', 'true', 'timeout', '100s']
));
);

-- 使用命名参数
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
SEARCH_TABLE => TABLE vector_table,
COLUMN_TO_QUERY => input_table.vector_column,
COLUMN_TO_SEARCH => DESCRIPTOR(index_column),
TOP_K => 10,
CONFIG => MAP['async', 'true', 'timeout', '100s']
));
);

-- 使用常量值搜索
SELECT * FROM TABLE(VECTOR_SEARCH(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ SELECT
SUM(amount * rate) AS amount
FROM
orders,
LATERAL TABLE (rates(order_time))
LATERAL rates(order_time)
WHERE
rates.currency = orders.currency
```
Expand Down
16 changes: 8 additions & 8 deletions docs/content/docs/dev/table/functions/udfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ In order to define a table function, one has to extend the base class `TableFunc

In the Table API, a table function is used with `.joinLateral(...)` or `.leftOuterJoinLateral(...)`. The `joinLateral` operator (cross) joins each row from the outer table (table on the left of the operator) with all rows produced by the table-valued function (which is on the right side of the operator). The `leftOuterJoinLateral` operator joins each row from the outer table (table on the left of the operator) with all rows produced by the table-valued function (which is on the right side of the operator) and preserves outer rows for which the table function returns an empty table.

In SQL, use `LATERAL TABLE(<TableFunction>)` with `JOIN` or `LEFT JOIN` with an `ON TRUE` join condition.
In SQL, use `LATERAL <TableFunction>(...)` (or the equivalent `LATERAL TABLE(<TableFunction>(...))`) with `JOIN` or `LEFT JOIN` with an `ON TRUE` join condition.

The following example shows how to define your own split function and call it in a query. See the [Implementation Guide](#implementation-guide) for more details.

Expand Down Expand Up @@ -1189,17 +1189,17 @@ env
// call registered function in SQL
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable, LATERAL TABLE(SplitFunction(myField))");
"FROM MyTable, LATERAL SplitFunction(myField)");
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) ON TRUE");
"LEFT JOIN LATERAL SplitFunction(myField) ON TRUE");

// rename fields of the function in SQL
env.sqlQuery(
"SELECT myField, newWord, newLength " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) AS T(newWord, newLength) ON TRUE");
"LEFT JOIN LATERAL SplitFunction(myField) AS T(newWord, newLength) ON TRUE");

```
{{< /tab >}}
Expand Down Expand Up @@ -1254,17 +1254,17 @@ env
// call registered function in SQL
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable, LATERAL TABLE(SplitFunction(myField))")
"FROM MyTable, LATERAL SplitFunction(myField)")
env.sqlQuery(
"SELECT myField, word, length " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) ON TRUE")
"LEFT JOIN LATERAL SplitFunction(myField) ON TRUE")

// rename fields of the function in SQL
env.sqlQuery(
"SELECT myField, newWord, newLength " +
"FROM MyTable " +
"LEFT JOIN LATERAL TABLE(SplitFunction(myField)) AS T(newWord, newLength) ON TRUE")
"LEFT JOIN LATERAL SplitFunction(myField) AS T(newWord, newLength) ON TRUE")

```
{{< /tab >}}
Expand Down Expand Up @@ -1354,7 +1354,7 @@ env.from("MyTable")
.select($("*"))

// call registered function in SQL
env.sqlQuery("SELECT * FROM MyTable, LATERAL TABLE(BackgroundFunction(myField))");
env.sqlQuery("SELECT * FROM MyTable, LATERAL BackgroundFunction(myField)");

```

Expand Down
8 changes: 4 additions & 4 deletions docs/content/docs/sql/reference/queries/joins.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ SELECT
o_amount, r_rate
FROM
Orders,
LATERAL TABLE (Rates(o_proctime))
LATERAL Rates(o_proctime)
WHERE
r_currency = o_currency
```
Expand All @@ -290,7 +290,7 @@ SELECT
o_amount, r_rate
FROM
Orders,
LATERAL TABLE (Rates(o_proctime))
LATERAL Rates(o_proctime)
WHERE
r_currency = o_currency
```
Expand Down Expand Up @@ -435,7 +435,7 @@ The row of the left (outer) table is dropped, if its table function call returns
```sql
SELECT order_id, res
FROM Orders,
LATERAL TABLE(table_func(order_id)) t(res)
LATERAL table_func(order_id) t(res)
```

### LEFT OUTER JOIN
Expand All @@ -445,7 +445,7 @@ If a table function call returns an empty result, the corresponding outer row is
```sql
SELECT order_id, res
FROM Orders
LEFT OUTER JOIN LATERAL TABLE(table_func(order_id)) t(res)
LEFT OUTER JOIN LATERAL table_func(order_id) t(res)
ON TRUE
```

Expand Down
3 changes: 2 additions & 1 deletion docs/content/docs/sql/reference/queries/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ tableReference:

tablePrimary:
[ TABLE ] tablePath [ dynamicTableOptions ] [systemTimePeriod] [[AS] correlationName]
| LATERAL TABLE '(' functionName '(' expression [, expression ]* ')' ')'
| [ LATERAL ] functionName '(' expression [, expression ]* ')'
| [ LATERAL ] TABLE '(' functionName '(' expression [, expression ]* ')' ')'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| [ LATERAL ] TABLE '(' functionName '(' expression [, expression ]* ')' ')'
| [ LATERAL ] [TABLE] '(' functionName '(' expression [, expression ]* ')' ')'

| [ LATERAL ] '(' query ')'
| UNNEST '(' expression ')'

Expand Down
16 changes: 8 additions & 8 deletions docs/content/docs/sql/reference/queries/vector-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ The `VECTOR_SEARCH` uses a processing-time attribute to correlate rows to the la

```sql
SELECT *
FROM input_table, LATERAL TABLE(VECTOR_SEARCH(
FROM input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
top_k,
[CONFIG => MAP['key', 'value']]
))
)
```

### Parameters
Expand All @@ -66,32 +66,32 @@ The following configuration options can be specified in the config map:
```sql
-- Basic usage
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
10
));
);

-- With configuration options
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
TABLE vector_table,
input_table.vector_column,
DESCRIPTOR(index_column),
10,
MAP['async', 'true', 'timeout', '100s']
));
);

-- Using named parameters
SELECT * FROM
input_table, LATERAL TABLE(VECTOR_SEARCH(
input_table, LATERAL VECTOR_SEARCH(
SEARCH_TABLE => TABLE vector_table,
COLUMN_TO_QUERY => input_table.vector_column,
COLUMN_TO_SEARCH => DESCRIPTOR(index_column),
TOP_K => 10,
CONFIG => MAP['async', 'true', 'timeout', '100s']
));
);

-- Searching with contant value
SELECT *
Expand Down
10 changes: 10 additions & 0 deletions flink-table/flink-sql-parser/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,16 @@ SqlNode TableRef3(ExprContext exprContext, boolean lateral) :
{
tableRef = unnestOp.createCall(s.end(this), (List<SqlNode>) args);
}
|
// LATERAL with implicit table function call syntax,
// e.g. "FROM t, LATERAL fn(...)" instead of "FROM t, LATERAL TABLE(fn(...))".
// The non-LATERAL implicit form is handled by the CompoundTableIdentifier
// branch above.
LOOKAHEAD(2)
<LATERAL> { lateral = true; }
tableName = CompoundTableIdentifier()
tableRef = ImplicitTableFunctionCallArgs(tableName)
tableRef = addLateral(tableRef, lateral)
Comment on lines +2239 to +2248
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the Calcite file from https://github.com/apache/calcite/blob/main/core/src/main/codegen/templates/Parser.jj

The only reason we have it here is a number of backports from Calcite.

In case we need to make changes here: ideally first need to make them in Calcite and then backport here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this file should be removed once we update Calcite to versions containing all the backports

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree this is danger zone ⚠️

|
[ <LATERAL> { lateral = true; } ]
tableRef = TableFunctionCall()
Expand Down
Loading