Copilot commented on code in PR #3462:
URL: https://github.com/apache/doris-website/pull/3462#discussion_r2928982997
##########
docs/query-data/asof-join.md:
##########
@@ -0,0 +1,422 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "en",
+ "description": "ASOF JOIN matches each row of the left table to the
nearest qualifying row in the right table based on a date/time condition,
commonly used in time-series analysis."
+}
+---
+
+## Overview
+
+ASOF JOIN is a special type of JOIN designed for time-series lookups on
date/time columns. Unlike regular equality JOIN, ASOF JOIN does not require an
exact match. Instead, for each left-table row, it finds the nearest right-table
row that satisfies the directional comparison in `MATCH_CONDITION`.
+
+ASOF JOIN does **not** mean "the absolutely closest row by time difference".
The returned row is the nearest row in the direction specified by
`MATCH_CONDITION`.
+
+A typical use case: given a table of stock trades and a table of stock quotes,
for each trade, find the most recent quote that was available at the time of
the trade. With regular JOIN, this requires complex subqueries and window
functions, while ASOF JOIN accomplishes it in a single, clear statement.
+
+ASOF JOIN supports two sub-types:
+
+- **ASOF JOIN** (ASOF LEFT JOIN): For each row in the left table, find the
nearest qualifying match in the right table according to `MATCH_CONDITION`. If
no match is found, the right-side columns are filled with NULL.
+- **ASOF INNER JOIN**: Same matching logic, but rows from the left table that
have no match are excluded from the result.
+
+## Syntax
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**Where:**
+
+- `ASOF JOIN` or `ASOF LEFT JOIN`: Left outer ASOF JOIN. Left table rows
without a match produce NULL on the right side.
+- `ASOF INNER JOIN`: Inner ASOF JOIN. Left table rows without a match are
discarded.
+- `MATCH_CONDITION(...)`: Specifies the nearest-match condition. Must
reference columns from both the left and right tables and evaluate to supported
date/time values (`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`).
+- `<comparison_operator>`: One of `>=`, `>`, `<=`, `<`.
+- `ON ...` or `USING (...)`: Defines one or more equality keys that partition
the matching space (equivalent to grouping). With `ON`, each condition must be
in the form `left_expr = right_expr`. With `USING`, each listed column name
must exist on both sides.
+
+## Parameters
+
+| Parameter | Required | Description |
+|-----------|----------|-------------|
+| `left_table` | Yes | The left (probe) table. All rows from this table are
evaluated. |
+| `right_table` | Yes | The right (build) table. Used to find the closest
match. |
+| `MATCH_CONDITION` | Yes | Defines the nearest-match rule. Both sides must
reference columns from both tables and evaluate to supported date/time values
(`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`). Expressions are allowed. Supported
operators: `>=`, `>`, `<=`, `<`. |
+| `ON` / `USING` clause | Yes | Defines one or more equality keys. Acts as the
grouping key — matching is only performed within the same group. `ON` supports
one or more equality (`=`) conditions and expressions (e.g., `SUBSTRING(l.code,
1, 3) = r.prefix`). `USING` supports one or more shared column names. |
+
+## How ASOF JOIN Matching Works
+
+The matching rule depends on the comparison operator in `MATCH_CONDITION`:
+
+| Operator | Matching Behavior | Typical Use Case |
+|----------|------------------|-----------------|
+| `>=` | For each left row, find the right row with the **largest** value that
is **less than or equal to** the left value. | Find the most recent
snapshot/quote before or at the event time. |
+| `>` | For each left row, find the right row with the **largest** value that
is **strictly less than** the left value. | Find the most recent snapshot/quote
strictly before the event time. |
+| `<=` | For each left row, find the right row with the **smallest** value
that is **greater than or equal to** the left value. | Find the next
event/snapshot at or after the current time. |
+| `<` | For each left row, find the right row with the **smallest** value that
is **strictly greater than** the left value. | Find the next event/snapshot
strictly after the current time. |
+
+**Key rules:**
+
+1. `MATCH_CONDITION` columns must be of type `DATEV2`, `DATETIMEV2`, or
`TIMESTAMPTZ`.
+2. Expressions are allowed in `MATCH_CONDITION`, for example:
`MATCH_CONDITION(l.ts >= r.ts + INTERVAL 1 HOUR)` or `MATCH_CONDITION(l.ts >=
DATE_ADD(r.ts, INTERVAL 3 HOUR))`.
+3. The equality key clause can be written with either `ON` or `USING`. In
`ON`, only equality (`=`) conjuncts are allowed. Non-equality conditions (such
as `>`, `OR`) or literal comparisons (such as `l.grp = 1`) are not allowed in
the `ON` clause.
+4. NULL values in the match column or the equality column never produce a
match. If the left row's match column is NULL, or if no matching right row
exists within the group, the right side is filled with NULL (for LEFT JOIN) or
the row is discarded (for INNER JOIN).
+5. When multiple right-side rows in the same group have the same match value
and satisfy the match condition, one of them is returned (non-deterministic).
+
+## Examples
+
+### Preparation
+
+Create a trades table and a quotes table:
+
+```sql
+CREATE TABLE trades (
+ trade_id INT,
+ symbol VARCHAR(10),
+ trade_time DATETIME,
+ price DECIMAL(10, 2),
Review Comment:
The doc states `MATCH_CONDITION` only supports
`DATEV2`/`DATETIMEV2`/`TIMESTAMPTZ`, but the examples create/join on `DATETIME`
columns (e.g. `trade_time DATETIME`, `quote_time DATETIME`, and `CAST(... AS
DATETIME)`). Please align the examples with the stated supported types, or
expand the supported-type list if `DATETIME` is actually supported.
##########
docs/query-data/asof-join.md:
##########
@@ -0,0 +1,422 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "en",
+ "description": "ASOF JOIN matches each row of the left table to the
nearest qualifying row in the right table based on a date/time condition,
commonly used in time-series analysis."
+}
+---
+
+## Overview
+
+ASOF JOIN is a special type of JOIN designed for time-series lookups on
date/time columns. Unlike regular equality JOIN, ASOF JOIN does not require an
exact match. Instead, for each left-table row, it finds the nearest right-table
row that satisfies the directional comparison in `MATCH_CONDITION`.
+
+ASOF JOIN does **not** mean "the absolutely closest row by time difference".
The returned row is the nearest row in the direction specified by
`MATCH_CONDITION`.
+
+A typical use case: given a table of stock trades and a table of stock quotes,
for each trade, find the most recent quote that was available at the time of
the trade. With regular JOIN, this requires complex subqueries and window
functions, while ASOF JOIN accomplishes it in a single, clear statement.
+
+ASOF JOIN supports two sub-types:
+
+- **ASOF JOIN** (ASOF LEFT JOIN): For each row in the left table, find the
nearest qualifying match in the right table according to `MATCH_CONDITION`. If
no match is found, the right-side columns are filled with NULL.
+- **ASOF INNER JOIN**: Same matching logic, but rows from the left table that
have no match are excluded from the result.
+
+## Syntax
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**Where:**
+
+- `ASOF JOIN` or `ASOF LEFT JOIN`: Left outer ASOF JOIN. Left table rows
without a match produce NULL on the right side.
+- `ASOF INNER JOIN`: Inner ASOF JOIN. Left table rows without a match are
discarded.
+- `MATCH_CONDITION(...)`: Specifies the nearest-match condition. Must
reference columns from both the left and right tables and evaluate to supported
date/time values (`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`).
+- `<comparison_operator>`: One of `>=`, `>`, `<=`, `<`.
Review Comment:
`MATCH_CONDITION(...)` is described as needing to “evaluate to supported
date/time values”, but the expression inside `MATCH_CONDITION` (e.g. `l_ts >=
r_ts`) evaluates to a boolean. This wording is misleading—please clarify that
the *operands* must be supported date/time types (and that `MATCH_CONDITION`
itself is a boolean predicate).
##########
i18n/zh-CN/docusaurus-plugin-content-docs/current/query-data/asof-join.md:
##########
@@ -0,0 +1,422 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "zh-CN",
+ "description": "ASOF JOIN 基于日期时间条件,将左表的每一行与右表中满足条件的最近一行进行匹配,常用于时序数据分析场景。"
+}
+---
+
+## 概述
+
+ASOF JOIN 是一种专为基于日期时间列的时序查询设计的特殊 JOIN 类型。与常规的等值 JOIN 不同,ASOF JOIN 不要求精确匹配,而是根据
`MATCH_CONDITION` 指定的方向,为左表中的每一行选择右表中满足条件的最近一行。
+
+ASOF JOIN 的“最近”并不是指按时间差绝对值查找最近一行,而是指在 `MATCH_CONDITION` 指定方向上满足条件的最近一行。
+
+一个典型的应用场景:给定一张股票交易表和一张行情报价表,需要为每笔交易找到交易发生时刻最近的报价。如果使用普通
JOIN,需要借助复杂的子查询和窗口函数来实现,而 ASOF JOIN 可以用一条简洁的语句完成。
+
+ASOF JOIN 支持两种子类型:
+
+- **ASOF JOIN**(ASOF LEFT JOIN):对左表的每一行,根据 `MATCH_CONDITION`
的方向,在右表中查找满足条件的最近匹配行。如果未找到匹配,右侧列填充 NULL。
+- **ASOF INNER JOIN**:匹配逻辑相同,但左表中没有匹配的行会从结果中被排除。
+
+## 语法
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**说明:**
+
+- `ASOF JOIN` 或 `ASOF LEFT JOIN`:左外 ASOF JOIN。左表中没有匹配的行,右侧列填充 NULL。
+- `ASOF INNER JOIN`:内 ASOF JOIN。左表中没有匹配的行会被丢弃。
+-
`MATCH_CONDITION(...)`:指定最近匹配条件,必须同时引用左表和右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2`
或 `TIMESTAMPTZ`)。
+- `<comparison_operator>`:`>=`、`>`、`<=`、`<` 四种比较运算符之一。
+- `ON ...` 或 `USING (...)`:定义一个或多个等值键,用于划分匹配空间(相当于分组)。使用 `ON` 时,每个条件的形式必须为
`left_expr = right_expr`;使用 `USING` 时,列出的列名必须同时存在于左右两侧。
+
+## 参数说明
+
+| 参数 | 是否必须 | 说明 |
+|------|----------|------|
+| `left_table` | 是 | 左表(探测表)。该表的所有行都会被评估。 |
+| `right_table` | 是 | 右表(构建表)。用于查找最接近的匹配。 |
+| `MATCH_CONDITION` | 是 |
定义最近匹配规则。两侧必须引用左右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2` 或
`TIMESTAMPTZ`);允许使用表达式。支持的运算符:`>=`、`>`、`<=`、`<`。 |
+| `ON` / `USING` 子句 | 是 | 定义一个或多个等值键。作为分组键——匹配仅在同一组内进行。`ON`
支持一个或多个等值(`=`)条件以及表达式(例如 `SUBSTRING(l.code, 1, 3) = r.prefix`);`USING`
支持一个或多个同名列。 |
+
+## ASOF JOIN 匹配规则
+
+匹配规则取决于 `MATCH_CONDITION` 中的比较运算符:
+
+| 运算符 | 匹配行为 | 典型使用场景 |
+|--------|---------|-------------|
+| `>=` | 对左表的每一行,查找右表中**最大的**且**小于等于**左侧值的行。 | 查找事件发生时刻或之前的最新快照/报价。 |
+| `>` | 对左表的每一行,查找右表中**最大的**且**严格小于**左侧值的行。 | 查找严格早于事件时刻的最新快照/报价。 |
+| `<=` | 对左表的每一行,查找右表中**最小的**且**大于等于**左侧值的行。 | 查找当前时刻或之后的下一个事件/快照。 |
+| `<` | 对左表的每一行,查找右表中**最小的**且**严格大于**左侧值的行。 | 查找严格晚于当前时刻的下一个事件/快照。 |
+
+**关键规则:**
+
+1. `MATCH_CONDITION` 中的列必须为 `DATEV2`、`DATETIMEV2` 或 `TIMESTAMPTZ` 类型。
+2. `MATCH_CONDITION` 中允许使用表达式,例如:`MATCH_CONDITION(l.ts >= r.ts + INTERVAL 1
HOUR)` 或 `MATCH_CONDITION(l.ts >= DATE_ADD(r.ts, INTERVAL 3 HOUR))`。
+3. 等值键子句可以写成 `ON` 或 `USING`。使用 `ON` 时,只允许使用等值(`=`)条件并用 `AND` 连接;`ON`
子句中不允许使用不等式条件(如 `>`、`OR`)或字面量比较(如 `l.grp = 1`)。
+4. 匹配列或等值列中的 NULL 值不会产生匹配。如果左表行的匹配列为 NULL,或者在同组内没有符合条件的右表行,则右侧列填充 NULL(LEFT
JOIN)或该行被丢弃(INNER JOIN)。
+5. 当右表中多行具有相同的分组键且在匹配列上具有相同的值,并且都满足匹配条件时,返回其中一行(不确定性)。
+
+## 示例
+
+### 数据准备
+
+创建交易表和报价表:
+
+```sql
+CREATE TABLE trades (
+ trade_id INT,
+ symbol VARCHAR(10),
+ trade_time DATETIME,
+ price DECIMAL(10, 2),
Review Comment:
文档中声明 `MATCH_CONDITION` 仅支持 `DATEV2`/`DATETIMEV2`/`TIMESTAMPTZ`,但示例里建表与 CAST
使用的是 `DATETIME`(如 `trade_time DATETIME`、`quote_time DATETIME`、`CAST(... AS
DATETIME)`)。请统一:要么把示例改成对应类型,要么在支持类型说明中补充 `DATETIME`(如果确实支持)。
##########
versioned_docs/version-4.x/query-data/asof-join.md:
##########
@@ -0,0 +1,426 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "en",
+ "description": "ASOF JOIN matches each row of the left table to the
nearest qualifying row in the right table based on a date/time condition,
commonly used in time-series analysis."
+}
+---
+
+## Overview
+
+:::info
+This feature is supported since Apache Doris versions 4.0.5 and 4.1.0.
+:::
+
+ASOF JOIN is a special type of JOIN designed for time-series lookups on
date/time columns. Unlike regular equality JOIN, ASOF JOIN does not require an
exact match. Instead, for each left-table row, it finds the nearest right-table
row that satisfies the directional comparison in `MATCH_CONDITION`.
+
+ASOF JOIN does **not** mean "the absolutely closest row by time difference".
The returned row is the nearest row in the direction specified by
`MATCH_CONDITION`.
+
+A typical use case: given a table of stock trades and a table of stock quotes,
for each trade, find the most recent quote that was available at the time of
the trade. With regular JOIN, this requires complex subqueries and window
functions, while ASOF JOIN accomplishes it in a single, clear statement.
+
+ASOF JOIN supports two sub-types:
+
+- **ASOF JOIN** (ASOF LEFT JOIN): For each row in the left table, find the
nearest qualifying match in the right table according to `MATCH_CONDITION`. If
no match is found, the right-side columns are filled with NULL.
+- **ASOF INNER JOIN**: Same matching logic, but rows from the left table that
have no match are excluded from the result.
+
+## Syntax
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**Where:**
+
+- `ASOF JOIN` or `ASOF LEFT JOIN`: Left outer ASOF JOIN. Left table rows
without a match produce NULL on the right side.
+- `ASOF INNER JOIN`: Inner ASOF JOIN. Left table rows without a match are
discarded.
+- `MATCH_CONDITION(...)`: Specifies the nearest-match condition. Must
reference columns from both the left and right tables and evaluate to supported
date/time values (`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`).
+- `<comparison_operator>`: One of `>=`, `>`, `<=`, `<`.
+- `ON ...` or `USING (...)`: Defines one or more equality keys that partition
the matching space (equivalent to grouping). With `ON`, each condition must be
in the form `left_expr = right_expr`. With `USING`, each listed column name
must exist on both sides.
+
+## Parameters
+
+| Parameter | Required | Description |
+|-----------|----------|-------------|
+| `left_table` | Yes | The left (probe) table. All rows from this table are
evaluated. |
+| `right_table` | Yes | The right (build) table. Used to find the closest
match. |
+| `MATCH_CONDITION` | Yes | Defines the nearest-match rule. Both sides must
reference columns from both tables and evaluate to supported date/time values
(`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`). Expressions are allowed. Supported
operators: `>=`, `>`, `<=`, `<`. |
+| `ON` / `USING` clause | Yes | Defines one or more equality keys. Acts as the
grouping key — matching is only performed within the same group. `ON` supports
one or more equality (`=`) conditions and expressions (e.g., `SUBSTRING(l.code,
1, 3) = r.prefix`). `USING` supports one or more shared column names. |
+
+## How ASOF JOIN Matching Works
+
+The matching rule depends on the comparison operator in `MATCH_CONDITION`:
+
+| Operator | Matching Behavior | Typical Use Case |
+|----------|------------------|-----------------|
+| `>=` | For each left row, find the right row with the **largest** value that
is **less than or equal to** the left value. | Find the most recent
snapshot/quote before or at the event time. |
+| `>` | For each left row, find the right row with the **largest** value that
is **strictly less than** the left value. | Find the most recent snapshot/quote
strictly before the event time. |
+| `<=` | For each left row, find the right row with the **smallest** value
that is **greater than or equal to** the left value. | Find the next
event/snapshot at or after the current time. |
+| `<` | For each left row, find the right row with the **smallest** value that
is **strictly greater than** the left value. | Find the next event/snapshot
strictly after the current time. |
+
+**Key rules:**
+
+1. `MATCH_CONDITION` columns must be of type `DATEV2`, `DATETIMEV2`, or
`TIMESTAMPTZ`.
+2. Expressions are allowed in `MATCH_CONDITION`, for example:
`MATCH_CONDITION(l.ts >= r.ts + INTERVAL 1 HOUR)` or `MATCH_CONDITION(l.ts >=
DATE_ADD(r.ts, INTERVAL 3 HOUR))`.
+3. The equality key clause can be written with either `ON` or `USING`. In
`ON`, only equality (`=`) conjuncts are allowed. Non-equality conditions (such
as `>`, `OR`) or literal comparisons (such as `l.grp = 1`) are not allowed in
the `ON` clause.
+4. NULL values in the match column or the equality column never produce a
match. If the left row's match column is NULL, or if no matching right row
exists within the group, the right side is filled with NULL (for LEFT JOIN) or
the row is discarded (for INNER JOIN).
+5. When multiple right-side rows in the same group have the same match value
and satisfy the match condition, one of them is returned (non-deterministic).
+
+## Examples
+
+### Preparation
+
+Create a trades table and a quotes table:
+
+```sql
+CREATE TABLE trades (
+ trade_id INT,
+ symbol VARCHAR(10),
+ trade_time DATETIME,
+ price DECIMAL(10, 2),
Review Comment:
This page says `MATCH_CONDITION` only supports
`DATEV2`/`DATETIMEV2`/`TIMESTAMPTZ`, but the examples use `DATETIME` columns
and casts (`trade_time DATETIME`, `quote_time DATETIME`, `CAST(... AS
DATETIME)`). Please make the examples consistent with the stated supported
types (or adjust the statement if `DATETIME` is supported).
##########
i18n/zh-CN/docusaurus-plugin-content-docs/current/query-data/asof-join.md:
##########
@@ -0,0 +1,422 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "zh-CN",
+ "description": "ASOF JOIN 基于日期时间条件,将左表的每一行与右表中满足条件的最近一行进行匹配,常用于时序数据分析场景。"
+}
+---
+
+## 概述
+
+ASOF JOIN 是一种专为基于日期时间列的时序查询设计的特殊 JOIN 类型。与常规的等值 JOIN 不同,ASOF JOIN 不要求精确匹配,而是根据
`MATCH_CONDITION` 指定的方向,为左表中的每一行选择右表中满足条件的最近一行。
+
+ASOF JOIN 的“最近”并不是指按时间差绝对值查找最近一行,而是指在 `MATCH_CONDITION` 指定方向上满足条件的最近一行。
+
+一个典型的应用场景:给定一张股票交易表和一张行情报价表,需要为每笔交易找到交易发生时刻最近的报价。如果使用普通
JOIN,需要借助复杂的子查询和窗口函数来实现,而 ASOF JOIN 可以用一条简洁的语句完成。
+
+ASOF JOIN 支持两种子类型:
+
+- **ASOF JOIN**(ASOF LEFT JOIN):对左表的每一行,根据 `MATCH_CONDITION`
的方向,在右表中查找满足条件的最近匹配行。如果未找到匹配,右侧列填充 NULL。
+- **ASOF INNER JOIN**:匹配逻辑相同,但左表中没有匹配的行会从结果中被排除。
+
+## 语法
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**说明:**
+
+- `ASOF JOIN` 或 `ASOF LEFT JOIN`:左外 ASOF JOIN。左表中没有匹配的行,右侧列填充 NULL。
+- `ASOF INNER JOIN`:内 ASOF JOIN。左表中没有匹配的行会被丢弃。
+-
`MATCH_CONDITION(...)`:指定最近匹配条件,必须同时引用左表和右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2`
或 `TIMESTAMPTZ`)。
+- `<comparison_operator>`:`>=`、`>`、`<=`、`<` 四种比较运算符之一。
Review Comment:
这里将 `MATCH_CONDITION(...)` 描述为“结果必须为受支持的日期时间值”,但 `MATCH_CONDITION(left_expr
<op> right_expr)` 实际返回的是布尔值。建议改为说明:参与比较的两侧表达式必须是受支持的日期时间类型。
##########
i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/query-data/asof-join.md:
##########
@@ -0,0 +1,426 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "zh-CN",
+ "description": "ASOF JOIN 基于日期时间条件,将左表的每一行与右表中满足条件的最近一行进行匹配,常用于时序数据分析场景。"
+}
+---
+
+## 概述
+
+:::info
+此功能自 Apache Doris 4.0.5 和 4.1.0 版本起支持。
+:::
+
+ASOF JOIN 是一种专为基于日期时间列的时序查询设计的特殊 JOIN 类型。与常规的等值 JOIN 不同,ASOF JOIN 不要求精确匹配,而是根据
`MATCH_CONDITION` 指定的方向,为左表中的每一行选择右表中满足条件的最近一行。
+
+ASOF JOIN 的“最近”并不是指按时间差绝对值查找最近一行,而是指在 `MATCH_CONDITION` 指定方向上满足条件的最近一行。
+
+一个典型的应用场景:给定一张股票交易表和一张行情报价表,需要为每笔交易找到交易发生时刻最近的报价。如果使用普通
JOIN,需要借助复杂的子查询和窗口函数来实现,而 ASOF JOIN 可以用一条简洁的语句完成。
+
+ASOF JOIN 支持两种子类型:
+
+- **ASOF JOIN**(ASOF LEFT JOIN):对左表的每一行,根据 `MATCH_CONDITION`
的方向,在右表中查找满足条件的最近匹配行。如果未找到匹配,右侧列填充 NULL。
+- **ASOF INNER JOIN**:匹配逻辑相同,但左表中没有匹配的行会从结果中被排除。
+
+## 语法
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**说明:**
+
+- `ASOF JOIN` 或 `ASOF LEFT JOIN`:左外 ASOF JOIN。左表中没有匹配的行,右侧列填充 NULL。
+- `ASOF INNER JOIN`:内 ASOF JOIN。左表中没有匹配的行会被丢弃。
+-
`MATCH_CONDITION(...)`:指定最近匹配条件,必须同时引用左表和右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2`
或 `TIMESTAMPTZ`)。
+- `<comparison_operator>`:`>=`、`>`、`<=`、`<` 四种比较运算符之一。
+- `ON ...` 或 `USING (...)`:定义一个或多个等值键,用于划分匹配空间(相当于分组)。使用 `ON` 时,每个条件的形式必须为
`left_expr = right_expr`;使用 `USING` 时,列出的列名必须同时存在于左右两侧。
+
+## 参数说明
+
+| 参数 | 是否必须 | 说明 |
+|------|----------|------|
+| `left_table` | 是 | 左表(探测表)。该表的所有行都会被评估。 |
+| `right_table` | 是 | 右表(构建表)。用于查找最接近的匹配。 |
+| `MATCH_CONDITION` | 是 |
定义最近匹配规则。两侧必须引用左右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2` 或
`TIMESTAMPTZ`);允许使用表达式。支持的运算符:`>=`、`>`、`<=`、`<`。 |
+| `ON` / `USING` 子句 | 是 | 定义一个或多个等值键。作为分组键——匹配仅在同一组内进行。`ON`
支持一个或多个等值(`=`)条件以及表达式(例如 `SUBSTRING(l.code, 1, 3) = r.prefix`);`USING`
支持一个或多个同名列。 |
+
+## ASOF JOIN 匹配规则
+
+匹配规则取决于 `MATCH_CONDITION` 中的比较运算符:
+
+| 运算符 | 匹配行为 | 典型使用场景 |
+|--------|---------|-------------|
+| `>=` | 对左表的每一行,查找右表中**最大的**且**小于等于**左侧值的行。 | 查找事件发生时刻或之前的最新快照/报价。 |
+| `>` | 对左表的每一行,查找右表中**最大的**且**严格小于**左侧值的行。 | 查找严格早于事件时刻的最新快照/报价。 |
+| `<=` | 对左表的每一行,查找右表中**最小的**且**大于等于**左侧值的行。 | 查找当前时刻或之后的下一个事件/快照。 |
+| `<` | 对左表的每一行,查找右表中**最小的**且**严格大于**左侧值的行。 | 查找严格晚于当前时刻的下一个事件/快照。 |
+
+**关键规则:**
+
+1. `MATCH_CONDITION` 中的列必须为 `DATEV2`、`DATETIMEV2` 或 `TIMESTAMPTZ` 类型。
+2. `MATCH_CONDITION` 中允许使用表达式,例如:`MATCH_CONDITION(l.ts >= r.ts + INTERVAL 1
HOUR)` 或 `MATCH_CONDITION(l.ts >= DATE_ADD(r.ts, INTERVAL 3 HOUR))`。
+3. 等值键子句可以写成 `ON` 或 `USING`。使用 `ON` 时,只允许使用等值(`=`)条件并用 `AND` 连接;`ON`
子句中不允许使用不等式条件(如 `>`、`OR`)或字面量比较(如 `l.grp = 1`)。
+4. 匹配列或等值列中的 NULL 值不会产生匹配。如果左表行的匹配列为 NULL,或者在同组内没有符合条件的右表行,则右侧列填充 NULL(LEFT
JOIN)或该行被丢弃(INNER JOIN)。
+5. 当右表中多行具有相同的分组键且在匹配列上具有相同的值,并且都满足匹配条件时,返回其中一行(不确定性)。
+
+## 示例
+
+### 数据准备
+
+创建交易表和报价表:
+
+```sql
+CREATE TABLE trades (
+ trade_id INT,
+ symbol VARCHAR(10),
+ trade_time DATETIME,
+ price DECIMAL(10, 2),
Review Comment:
本页声明 `MATCH_CONDITION` 仅支持 `DATEV2`/`DATETIMEV2`/`TIMESTAMPTZ`,但示例使用了
`DATETIME` 列与 `CAST(... AS DATETIME)`。请让示例与支持类型说明保持一致(或如果支持
`DATETIME`,请更新支持类型列表)。
##########
i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/query-data/asof-join.md:
##########
@@ -0,0 +1,426 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "zh-CN",
+ "description": "ASOF JOIN 基于日期时间条件,将左表的每一行与右表中满足条件的最近一行进行匹配,常用于时序数据分析场景。"
+}
+---
+
+## 概述
+
+:::info
+此功能自 Apache Doris 4.0.5 和 4.1.0 版本起支持。
+:::
+
+ASOF JOIN 是一种专为基于日期时间列的时序查询设计的特殊 JOIN 类型。与常规的等值 JOIN 不同,ASOF JOIN 不要求精确匹配,而是根据
`MATCH_CONDITION` 指定的方向,为左表中的每一行选择右表中满足条件的最近一行。
+
+ASOF JOIN 的“最近”并不是指按时间差绝对值查找最近一行,而是指在 `MATCH_CONDITION` 指定方向上满足条件的最近一行。
+
+一个典型的应用场景:给定一张股票交易表和一张行情报价表,需要为每笔交易找到交易发生时刻最近的报价。如果使用普通
JOIN,需要借助复杂的子查询和窗口函数来实现,而 ASOF JOIN 可以用一条简洁的语句完成。
+
+ASOF JOIN 支持两种子类型:
+
+- **ASOF JOIN**(ASOF LEFT JOIN):对左表的每一行,根据 `MATCH_CONDITION`
的方向,在右表中查找满足条件的最近匹配行。如果未找到匹配,右侧列填充 NULL。
+- **ASOF INNER JOIN**:匹配逻辑相同,但左表中没有匹配的行会从结果中被排除。
+
+## 语法
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**说明:**
+
+- `ASOF JOIN` 或 `ASOF LEFT JOIN`:左外 ASOF JOIN。左表中没有匹配的行,右侧列填充 NULL。
+- `ASOF INNER JOIN`:内 ASOF JOIN。左表中没有匹配的行会被丢弃。
+-
`MATCH_CONDITION(...)`:指定最近匹配条件,必须同时引用左表和右表的列,且结果必须为受支持的日期时间值(`DATEV2`、`DATETIMEV2`
或 `TIMESTAMPTZ`)。
+- `<comparison_operator>`:`>=`、`>`、`<=`、`<` 四种比较运算符之一。
Review Comment:
这里将 `MATCH_CONDITION(...)`
描述为“结果必须为受支持的日期时间值”,但该条件表达式实际返回布尔值。建议改为:两侧参与比较的表达式必须是受支持的日期时间类型。
##########
versioned_docs/version-4.x/query-data/asof-join.md:
##########
@@ -0,0 +1,426 @@
+---
+{
+ "title": "ASOF JOIN",
+ "language": "en",
+ "description": "ASOF JOIN matches each row of the left table to the
nearest qualifying row in the right table based on a date/time condition,
commonly used in time-series analysis."
+}
+---
+
+## Overview
+
+:::info
+This feature is supported since Apache Doris versions 4.0.5 and 4.1.0.
+:::
+
+ASOF JOIN is a special type of JOIN designed for time-series lookups on
date/time columns. Unlike regular equality JOIN, ASOF JOIN does not require an
exact match. Instead, for each left-table row, it finds the nearest right-table
row that satisfies the directional comparison in `MATCH_CONDITION`.
+
+ASOF JOIN does **not** mean "the absolutely closest row by time difference".
The returned row is the nearest row in the direction specified by
`MATCH_CONDITION`.
+
+A typical use case: given a table of stock trades and a table of stock quotes,
for each trade, find the most recent quote that was available at the time of
the trade. With regular JOIN, this requires complex subqueries and window
functions, while ASOF JOIN accomplishes it in a single, clear statement.
+
+ASOF JOIN supports two sub-types:
+
+- **ASOF JOIN** (ASOF LEFT JOIN): For each row in the left table, find the
nearest qualifying match in the right table according to `MATCH_CONDITION`. If
no match is found, the right-side columns are filled with NULL.
+- **ASOF INNER JOIN**: Same matching logic, but rows from the left table that
have no match are excluded from the result.
+
+## Syntax
+
+```sql
+SELECT <select_list>
+FROM <left_table>
+ASOF [LEFT] JOIN <right_table>
+ MATCH_CONDITION(<left_datetime_expr> <comparison_operator>
<right_datetime_expr>)
+ { ON <left_table.col> = <right_table.col> [AND ...]
+ | USING (<column_name> [, ...]) }
+```
+
+**Where:**
+
+- `ASOF JOIN` or `ASOF LEFT JOIN`: Left outer ASOF JOIN. Left table rows
without a match produce NULL on the right side.
+- `ASOF INNER JOIN`: Inner ASOF JOIN. Left table rows without a match are
discarded.
+- `MATCH_CONDITION(...)`: Specifies the nearest-match condition. Must
reference columns from both the left and right tables and evaluate to supported
date/time values (`DATEV2`, `DATETIMEV2`, or `TIMESTAMPTZ`).
+- `<comparison_operator>`: One of `>=`, `>`, `<=`, `<`.
Review Comment:
`MATCH_CONDITION(...)` is described as needing to “evaluate to supported
date/time values”, but the `MATCH_CONDITION(left_expr <op> right_expr)` form
evaluates to a boolean. Please reword to say the compared expressions must be
supported date/time types.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]