Skip to content

Commit bd43faf

Browse files
authored
Feature/issue 6875 largest triangle three buckets (#6877) (#885)
* Add aggregation function for Largest-Triangle-Three-Buckets (#53145) * Added a simple lttb aggregate function * Added support for multiple datatypes * Added support for Date and Date32, updated LTTBData struct * Updated code to handle bucket size 0 and 1 * Added sort for LTTBData * Added tests and documentation * Added some code style fixes * Added function to new func ref file * Removed function from new func ref file * Apply suggestions from code review * Updated unit tests * updated LTTB data code * Minor style fixes * Updated code with std sort * updated tests * Renamed lttb to largestTriangleThreeBuckets * Added alias lttb --------- Co-authored-by: Alexey Milovidov <milovidov@clickhouse.com> * ClickHouse/ClickHouse#56350 (partial) * fix: split lttb bucket strategy, first bucket and last bucket should only contain single point (#57003) * fix: split lttb bucket policy, first bucket and last bucket should only contain single point * add comments and modify the corresponding query test * style: update code format * style: remove useless comments * feat: add lttb bucket size test * fix: typo, duplicate sql * Merge pull request #62646 from Algunenano/i_like_triangles Fix crash in largestTriangleThreeBuckets * ClickHouse/ClickHouse#60469 (partial) * ClickHouse/ClickHouse#68135 (partial) * Merge pull request #73172 from ucasfl/fix-ubsan Fix UBSAN in largestTriangleThreeBuckets * fixes
1 parent 3195dda commit bd43faf

12 files changed

+626
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
slug: /en/sql-reference/aggregate-functions/reference/largestTriangleThreeBuckets
3+
sidebar_position: 312
4+
sidebar_label: largestTriangleThreeBuckets
5+
---
6+
7+
# largestTriangleThreeBuckets
8+
9+
Applies the [Largest-Triangle-Three-Buckets](https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf) algorithm to the input data.
10+
The algorithm is used for downsampling time series data for visualization. It is designed to operate on series sorted by x coordinate.
11+
It works by dividing the sorted series into buckets and then finding the largest triangle in each bucket. The number of buckets is equal to the number of points in the resulting series.
12+
the function will sort data by `x` and then apply the downsampling algorithm to the sorted data.
13+
14+
**Syntax**
15+
16+
``` sql
17+
largestTriangleThreeBuckets(n)(x, y)
18+
```
19+
20+
Alias: `lttb`.
21+
22+
**Arguments**
23+
24+
- `x` — x coordinate. [Integer](../../../sql-reference/data-types/int-uint.md) , [Float](../../../sql-reference/data-types/float.md) , [Decimal](../../../sql-reference/data-types/decimal.md) , [Date](../../../sql-reference/data-types/date.md), [Date32](../../../sql-reference/data-types/date32.md), [DateTime](../../../sql-reference/data-types/datetime.md), [DateTime64](../../../sql-reference/data-types/datetime64.md).
25+
- `y` — y coordinate. [Integer](../../../sql-reference/data-types/int-uint.md) , [Float](../../../sql-reference/data-types/float.md) , [Decimal](../../../sql-reference/data-types/decimal.md) , [Date](../../../sql-reference/data-types/date.md), [Date32](../../../sql-reference/data-types/date32.md), [DateTime](../../../sql-reference/data-types/datetime.md), [DateTime64](../../../sql-reference/data-types/datetime64.md).
26+
27+
**Parameters**
28+
29+
- `n` — number of points in the resulting series. [UInt64](../../../sql-reference/data-types/int-uint.md).
30+
31+
**Returned values**
32+
33+
[Array](../../../sql-reference/data-types/array.md) of [Tuple](../../../sql-reference/data-types/tuple.md) with two elements:
34+
35+
**Example**
36+
37+
Input table:
38+
39+
``` text
40+
┌─────x───────┬───────y──────┐
41+
│ 1.000000000 │ 10.000000000 │
42+
│ 2.000000000 │ 20.000000000 │
43+
│ 3.000000000 │ 15.000000000 │
44+
│ 8.000000000 │ 60.000000000 │
45+
│ 9.000000000 │ 55.000000000 │
46+
│ 10.00000000 │ 70.000000000 │
47+
│ 4.000000000 │ 30.000000000 │
48+
│ 5.000000000 │ 40.000000000 │
49+
│ 6.000000000 │ 35.000000000 │
50+
│ 7.000000000 │ 50.000000000 │
51+
└─────────────┴──────────────┘
52+
```
53+
54+
Query:
55+
56+
``` sql
57+
SELECT largestTriangleThreeBuckets(4)(x, y) FROM largestTriangleThreeBuckets_test;
58+
```
59+
60+
Result:
61+
62+
``` text
63+
┌────────largestTriangleThreeBuckets(3)(x, y)───────────┐
64+
│ [(1,10),(3,15),(5,40),(10,70)] │
65+
└───────────────────────────────────────────────────────┘
66+
```
67+

0 commit comments

Comments
 (0)