Skip to content

Commit cbf2222

Browse files
authored
add asof join in table model (#771)
1 parent 52cc4c5 commit cbf2222

4 files changed

Lines changed: 372 additions & 4 deletions

File tree

src/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ relation
3434
joinType
3535
: INNER?
3636
| FULL OUTER?
37+
| CROSS?
38+
| ASOF?
3739
;
3840

3941
joinCriteria
@@ -73,6 +75,7 @@ In the current version of IoTDB, the following joins are supported:
7375
1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. The join condition must be an equality condition on the `time` column.
7476
2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. The join condition can be any equality expression.
7577
3. **Cross Join**: Represents the Cartesian product of two tables.
78+
4.**​ASOF JOIN​​** (AS OF a specific point in time) is a specialized join operation based on temporal or approximate matching conditions, designed for scenarios where timestamps between two datasets are not perfectly aligned. It matches each row from the left table with the closest corresponding row in the right table that meets the specified conditions (typically the nearest preceding or succeeding timestamp). This operation is widely used for time-series data analysis (e.g., sensor data, financial market feeds).
7679

7780
### 3.1 Inner Join
7881

@@ -132,6 +135,43 @@ joinCriteria
132135
### 3.3 Cross Join
133136
A cross join represents the Cartesian product of two tables, returning all possible combinations of the N rows from the left table and the M rows from the right table, resulting in N*M rows. This type of join is the least commonly used in practice.
134137

138+
### 3.4 ​Asof Join
139+
140+
IoTDB ASOF JOIN is an approximate point join method that allows users to perform matching based on the closest timestamp according to specified rules. ​​The current version only supports ASOF INNER JOIN for Time columns.​​
141+
142+
The SQL syntax is as follows:
143+
144+
```SQL
145+
SELECT selectExpr [, selectExpr] ... FROM
146+
<TABLE_NAME1> ASOF[(tolerance theta)] [INNER] JOIN <TABLE_NAME2> joinCriteria
147+
[WHERE whereCondition]
148+
WHERE a.time = tolerance(b.time, 1s)
149+
150+
joinCriteria
151+
: ON <TABLE_NAME1>.time comparisonOperator <TABLE_NAME2>.time
152+
;
153+
154+
comparisonOperator
155+
: < | <= | > | >=
156+
;
157+
```
158+
159+
**​Notes:​​**
160+
161+
* ASOF JOIN defaults to ASOF INNER JOIN implementation.
162+
* When using the ON keyword for joining, the join condition must include an inequality join condition for the Time column. Only four operators are supported: `">", ">=", "<", "<="`. The corresponding join matching rules are as follows (where lt represents the left table and rt represents the right table):
163+
164+
| Operator | Join Method |
165+
| -------------------------- | ---------------------------------------------- |
166+
| `lt.time >= rt.time` | The closest timestamp in the left table that is greater than or equal to the right table's timestamp. |
167+
| `lt.time > rt.time` | The closest timestamp in the left table that is greater than the right table's timestamp. |
168+
| `lt.time <= rt.time` | The closest timestamp in the left table that is less than or equal to the right table's timestamp. |
169+
| `lt.time < rt.time` | The closest timestamp in the left table that is less than the right table's timestamp. |
170+
171+
* `Tolerance parameter​`​: The maximum allowed time difference for searching data in the right table (expressed as a TimeDuration, e.g., 1d for one day). If the Tolerance parameter is not specified, the search time range defaults to ​​infinity​​. ​​Note​​: Currently, this parameter is only supported in ASOF ​​INNER​​ JOIN.
172+
173+
174+
135175
## 4. Example Queries
136176

137177
The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results.
@@ -499,4 +539,57 @@ Query Results:
499539
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
500540
Total line number = 8
501541
It costs 0.047s
502-
```
542+
```
543+
544+
#### 4.2.4 Asof join
545+
546+
​​Example 1​​: Without specifying the tolerance parameter, where the timestamp in table1 is greater than or equal to and closest to the timestamp in table2.
547+
548+
```SQL
549+
SELECT t1.time as time1, t1.device_id as device1, t1.temperature as temperature1, t2.time as time2, t2.device_id as device2, t2.temperature as temperature2 FROM table1 t1 ASOF JOIN table2 t2 ON t1.time>=t2.time;
550+
```
551+
552+
Query Results:
553+
554+
```SQL
555+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
556+
| time1|device1|temperature1| time2|device2|temperature2|
557+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
558+
|2024-11-30T14:30:00.000+08:00| 101| 90.0|2024-11-30T00:00:00.000+08:00| 101| 90.0|
559+
|2024-11-30T09:30:00.000+08:00| 101| 90.0|2024-11-30T00:00:00.000+08:00| 101| 90.0|
560+
|2024-11-29T18:30:00.000+08:00| 100| 90.0|2024-11-29T11:00:00.000+08:00| 100| null|
561+
|2024-11-29T11:00:00.000+08:00| 100| null|2024-11-29T11:00:00.000+08:00| 100| null|
562+
|2024-11-29T10:00:00.000+08:00| 101| 85.0|2024-11-29T00:00:00.000+08:00| 101| 85.0|
563+
|2024-11-28T11:00:00.000+08:00| 100| 88.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
564+
|2024-11-28T10:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
565+
|2024-11-28T09:00:00.000+08:00| 100| null|2024-11-28T08:00:00.000+08:00| 100| 85.0|
566+
|2024-11-28T08:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
567+
|2024-11-27T16:44:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
568+
|2024-11-27T16:43:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
569+
|2024-11-27T16:42:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
570+
|2024-11-27T16:41:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
571+
|2024-11-27T16:40:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
572+
|2024-11-27T16:39:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
573+
|2024-11-27T16:38:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
574+
|2024-11-26T13:38:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
575+
|2024-11-26T13:37:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
576+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
577+
```
578+
579+
Example 2​​: With the tolerance parameter specified, where the timestamp in table1 is greater than or equal to and closest to the timestamp in table2.
580+
581+
```SQL
582+
SELECT t1.time as time1, t1.device_id as device1, t1.temperature as temperature1, t2.time as time2, t2.device_id as device2, t2.temperature as temperature2 FROM table1 t1 ASOF(tolerance 2s) JOIN table2 t2 ON t1.time>=t2.time;
583+
```
584+
585+
Query Results:
586+
587+
```SQL
588+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
589+
| time1|device1|temperature1| time2|device2|temperature2|
590+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
591+
|2024-11-29T11:00:00.000+08:00| 100| null|2024-11-29T11:00:00.000+08:00| 100| null|
592+
|2024-11-28T08:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
593+
|2024-11-26T13:37:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
594+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
595+
```

src/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ relation
3434
joinType
3535
: INNER?
3636
| FULL OUTER?
37+
| CROSS?
38+
| ASOF?
3739
;
3840

3941
joinCriteria
@@ -73,6 +75,7 @@ In the current version of IoTDB, the following joins are supported:
7375
1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. The join condition must be an equality condition on the `time` column.
7476
2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. The join condition can be any equality expression.
7577
3. **Cross Join**: Represents the Cartesian product of two tables.
78+
4.**​ASOF JOIN​​** (AS OF a specific point in time) is a specialized join operation based on temporal or approximate matching conditions, designed for scenarios where timestamps between two datasets are not perfectly aligned. It matches each row from the left table with the closest corresponding row in the right table that meets the specified conditions (typically the nearest preceding or succeeding timestamp). This operation is widely used for time-series data analysis (e.g., sensor data, financial market feeds).
7679

7780
### 3.1 Inner Join
7881

@@ -132,6 +135,43 @@ joinCriteria
132135
### 3.3 Cross Join
133136
A cross join represents the Cartesian product of two tables, returning all possible combinations of the N rows from the left table and the M rows from the right table, resulting in N*M rows. This type of join is the least commonly used in practice.
134137

138+
### 3.4 ​Asof Join
139+
140+
IoTDB ASOF JOIN is an approximate point join method that allows users to perform matching based on the closest timestamp according to specified rules. ​​The current version only supports ASOF INNER JOIN for Time columns.​​
141+
142+
The SQL syntax is as follows:
143+
144+
```SQL
145+
SELECT selectExpr [, selectExpr] ... FROM
146+
<TABLE_NAME1> ASOF[(tolerance theta)] [INNER] JOIN <TABLE_NAME2> joinCriteria
147+
[WHERE whereCondition]
148+
WHERE a.time = tolerance(b.time, 1s)
149+
150+
joinCriteria
151+
: ON <TABLE_NAME1>.time comparisonOperator <TABLE_NAME2>.time
152+
;
153+
154+
comparisonOperator
155+
: < | <= | > | >=
156+
;
157+
```
158+
159+
**​Notes:​​**
160+
161+
* ASOF JOIN defaults to ASOF INNER JOIN implementation.
162+
* When using the ON keyword for joining, the join condition must include an inequality join condition for the Time column. Only four operators are supported: `">", ">=", "<", "<="`. The corresponding join matching rules are as follows (where lt represents the left table and rt represents the right table):
163+
164+
| Operator | Join Method |
165+
| -------------------------- | ---------------------------------------------- |
166+
| `lt.time >= rt.time` | The closest timestamp in the left table that is greater than or equal to the right table's timestamp. |
167+
| `lt.time > rt.time` | The closest timestamp in the left table that is greater than the right table's timestamp. |
168+
| `lt.time <= rt.time` | The closest timestamp in the left table that is less than or equal to the right table's timestamp. |
169+
| `lt.time < rt.time` | The closest timestamp in the left table that is less than the right table's timestamp. |
170+
171+
* `Tolerance parameter​`​: The maximum allowed time difference for searching data in the right table (expressed as a TimeDuration, e.g., 1d for one day). If the Tolerance parameter is not specified, the search time range defaults to ​​infinity​​. ​​Note​​: Currently, this parameter is only supported in ASOF ​​INNER​​ JOIN.
172+
173+
174+
135175
## 4. Example Queries
136176

137177
The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results.
@@ -499,4 +539,57 @@ Query Results:
499539
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
500540
Total line number = 8
501541
It costs 0.047s
502-
```
542+
```
543+
544+
#### 4.2.4 Asof join
545+
546+
​​Example 1​​: Without specifying the tolerance parameter, where the timestamp in table1 is greater than or equal to and closest to the timestamp in table2.
547+
548+
```SQL
549+
SELECT t1.time as time1, t1.device_id as device1, t1.temperature as temperature1, t2.time as time2, t2.device_id as device2, t2.temperature as temperature2 FROM table1 t1 ASOF JOIN table2 t2 ON t1.time>=t2.time;
550+
```
551+
552+
Query Results:
553+
554+
```SQL
555+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
556+
| time1|device1|temperature1| time2|device2|temperature2|
557+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
558+
|2024-11-30T14:30:00.000+08:00| 101| 90.0|2024-11-30T00:00:00.000+08:00| 101| 90.0|
559+
|2024-11-30T09:30:00.000+08:00| 101| 90.0|2024-11-30T00:00:00.000+08:00| 101| 90.0|
560+
|2024-11-29T18:30:00.000+08:00| 100| 90.0|2024-11-29T11:00:00.000+08:00| 100| null|
561+
|2024-11-29T11:00:00.000+08:00| 100| null|2024-11-29T11:00:00.000+08:00| 100| null|
562+
|2024-11-29T10:00:00.000+08:00| 101| 85.0|2024-11-29T00:00:00.000+08:00| 101| 85.0|
563+
|2024-11-28T11:00:00.000+08:00| 100| 88.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
564+
|2024-11-28T10:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
565+
|2024-11-28T09:00:00.000+08:00| 100| null|2024-11-28T08:00:00.000+08:00| 100| 85.0|
566+
|2024-11-28T08:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
567+
|2024-11-27T16:44:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
568+
|2024-11-27T16:43:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
569+
|2024-11-27T16:42:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
570+
|2024-11-27T16:41:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
571+
|2024-11-27T16:40:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
572+
|2024-11-27T16:39:00.000+08:00| 101| 85.0|2024-11-27T00:00:00.000+08:00| 101| 85.0|
573+
|2024-11-27T16:38:00.000+08:00| 101| null|2024-11-27T00:00:00.000+08:00| 101| 85.0|
574+
|2024-11-26T13:38:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
575+
|2024-11-26T13:37:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
576+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
577+
```
578+
579+
Example 2​​: With the tolerance parameter specified, where the timestamp in table1 is greater than or equal to and closest to the timestamp in table2.
580+
581+
```SQL
582+
SELECT t1.time as time1, t1.device_id as device1, t1.temperature as temperature1, t2.time as time2, t2.device_id as device2, t2.temperature as temperature2 FROM table1 t1 ASOF(tolerance 2s) JOIN table2 t2 ON t1.time>=t2.time;
583+
```
584+
585+
Query Results:
586+
587+
```SQL
588+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
589+
| time1|device1|temperature1| time2|device2|temperature2|
590+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
591+
|2024-11-29T11:00:00.000+08:00| 100| null|2024-11-29T11:00:00.000+08:00| 100| null|
592+
|2024-11-28T08:00:00.000+08:00| 100| 85.0|2024-11-28T08:00:00.000+08:00| 100| 85.0|
593+
|2024-11-26T13:37:00.000+08:00| 100| 90.0|2024-11-26T13:37:00.000+08:00| 100| 90.0|
594+
+-----------------------------+-------+------------+-----------------------------+-------+------------+
595+
```

0 commit comments

Comments
 (0)