Skip to content

Commit fa533f1

Browse files
committed
LT JOIN supports TOLERANCE too
1 parent fc80914 commit fa533f1

File tree

4 files changed

+133
-97
lines changed

4 files changed

+133
-97
lines changed

documentation/reference/sql/asof-join.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Result:
332332

333333
### TOLERANCE clause
334334

335-
The `TOLERANCE` clause enhances ASOF JOIN by limiting how far back in time the join should look for a match in the right
335+
The `TOLERANCE` clause enhances ASOF and LT JOINs by limiting how far back in time the join should look for a match in the right
336336
table. The `TOLERANCE` parameter accepts a time interval value (e.g., 2s, 100ms, 1d).
337337

338338
When specified, a record from the left table t1 at t1.ts will only be joined with a record from the right table t2 at

documentation/reference/sql/join.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ High-level overview:
3434

3535
- `ASOF`, `LT`, and `SPLICE` `JOIN` has optional `ON` clause allowing only the
3636
`=` predicate.
37-
- `ASOF` additionally allows an optional `TOLERANCE` clause:
37+
- `ASOF` and `LT` join additionally allows an optional `TOLERANCE` clause:
3838

3939
![Flow chart showing the syntax of the ASOF, LT, and SPLICE JOIN keyword](/images/docs/diagrams/AsofLtSpliceJoin.svg)
4040

@@ -312,7 +312,7 @@ WHERE t.timestamp < t2.timestamp
312312

313313
## LT JOIN
314314

315-
Similar to `ASOF JOIN`, `LT JOIN` joins two different time-series measured. For
315+
Similar to [`ASOF JOIN`](/docs/reference/sql/asof-join/), `LT JOIN` joins two different time-series measured. For
316316
each row in the first time-series, the `LT JOIN` takes from the second
317317
time-series a timestamp that meets both of the following criteria:
318318

@@ -395,6 +395,42 @@ order to get preceding values for every row.
395395
The `ON` clause can also be used in combination with `LT JOIN` to join both by
396396
timestamp and column values.
397397

398+
### TOLERANCE clause
399+
The `TOLERANCE` clause enhances LT JOIN by limiting how far back in time the join should look for a match in the right
400+
table. The `TOLERANCE` parameter accepts a time interval value (e.g., 2s, 100ms, 1d).
401+
402+
When specified, a record from the left table t1 at t1.ts will only be joined with a record from the right table t2 at
403+
t2.ts if both conditions are met: `t2.ts < t1.ts` and `t1.ts - t2.ts <= tolerance_value`
404+
405+
This ensures that the matched record from the right table is not only the latest one on or before t1.ts, but also within
406+
the specified time window.
407+
408+
```questdb-sql title="LT JOIN with a TOLERANCE parameter"
409+
SELECT ...
410+
FROM table1
411+
LT JOIN table2 TOLERANCE 10s
412+
[WHERE ...]
413+
```
414+
415+
The interval_literal must be a valid QuestDB interval string, like '5s' (5 seconds), '100ms' (100 milliseconds),
416+
'2m' ( 2 minutes), '3h' (3 hours), or '1d' (1 day).
417+
418+
#### Supported Units for interval_literal
419+
The `TOLERANCE` interval literal supports the following time unit qualifiers:
420+
- U: Microseconds
421+
- T: Milliseconds
422+
- s: Seconds
423+
- m: Minutes
424+
- h: Hours
425+
- d: Days
426+
- w: Weeks
427+
428+
For example, '100U' is 100 microseconds, '50T' is 50 milliseconds, '2s' is 2 seconds, '30m' is 30 minutes,
429+
'1h' is 1 hour, '7d' is 7 days, and '2w' is 2 weeks. Please note that months (M) and years (Y) are not supported as
430+
units for the `TOLERANCE` clause.
431+
432+
See [`ASOF JOIN documentation`](/docs/reference/sql/asof-join#tolerance-clause) for more examples with the `TOLERANCE` clause.
433+
398434
## SPLICE JOIN
399435

400436
`SPLICE JOIN` is a full `ASOF JOIN`. It will return all the records from both

static/images/docs/diagrams/.railroad

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ innerLeftJoin
239239
::= ( 'INNER' | 'LEFT' )? 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( ( column operator anotherColumn ) ( 'AND' ( column operator anotherColumn ) )* | '(' column ( ',' column )* ')' ) )?
240240

241241
AsofLtSpliceJoin
242-
::= 'ASOF' 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( column '=' anotherColumn ( 'AND' column '=' anotherColumn )* | '(' column ( ',' column )* ')' ) )? ( 'TOLERANCE' interval_literal )?
243-
| ( 'LT' | 'SPLICE' ) 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( column '=' anotherColumn ( 'AND' column '=' anotherColumn )* | '(' column ( ',' column )* ')' ) )?
242+
::= ( 'ASOF' | 'LT' ) 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( column '=' anotherColumn ( 'AND' column '=' anotherColumn )* | '(' column ( ',' column )* ')' ) )? ( 'TOLERANCE' interval_literal )?
243+
| 'SPLICE' 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( column '=' anotherColumn ( 'AND' column '=' anotherColumn )* | '(' column ( ',' column )* ')' ) )?
244244

245245
AsofJoin
246246
::= 'ASOF' 'JOIN' ( table | '(' sub-query ')' ) ( 'ON' ( column '=' anotherColumn ( 'AND' column '=' anotherColumn )* | '(' column ( ',' column )* ')' ) )? ( 'TOLERANCE' interval_literal )?

0 commit comments

Comments
 (0)