Skip to content

Commit

Permalink
Feature/issue 6875 largest triangle three buckets (#6877) (#885)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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
  • Loading branch information
yl-lisen authored Dec 21, 2024
1 parent 3195dda commit bd43faf
Show file tree
Hide file tree
Showing 12 changed files with 626 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
slug: /en/sql-reference/aggregate-functions/reference/largestTriangleThreeBuckets
sidebar_position: 312
sidebar_label: largestTriangleThreeBuckets
---

# largestTriangleThreeBuckets

Applies the [Largest-Triangle-Three-Buckets](https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf) algorithm to the input data.
The algorithm is used for downsampling time series data for visualization. It is designed to operate on series sorted by x coordinate.
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.
the function will sort data by `x` and then apply the downsampling algorithm to the sorted data.

**Syntax**

``` sql
largestTriangleThreeBuckets(n)(x, y)
```

Alias: `lttb`.

**Arguments**

- `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).
- `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).

**Parameters**

- `n` — number of points in the resulting series. [UInt64](../../../sql-reference/data-types/int-uint.md).

**Returned values**

[Array](../../../sql-reference/data-types/array.md) of [Tuple](../../../sql-reference/data-types/tuple.md) with two elements:

**Example**

Input table:

``` text
┌─────x───────┬───────y──────┐
│ 1.000000000 │ 10.000000000 │
│ 2.000000000 │ 20.000000000 │
│ 3.000000000 │ 15.000000000 │
│ 8.000000000 │ 60.000000000 │
│ 9.000000000 │ 55.000000000 │
│ 10.00000000 │ 70.000000000 │
│ 4.000000000 │ 30.000000000 │
│ 5.000000000 │ 40.000000000 │
│ 6.000000000 │ 35.000000000 │
│ 7.000000000 │ 50.000000000 │
└─────────────┴──────────────┘
```

Query:

``` sql
SELECT largestTriangleThreeBuckets(4)(x, y) FROM largestTriangleThreeBuckets_test;
```

Result:

``` text
┌────────largestTriangleThreeBuckets(3)(x, y)───────────┐
│ [(1,10),(3,15),(5,40),(10,70)] │
└───────────────────────────────────────────────────────┘
```

Loading

0 comments on commit bd43faf

Please sign in to comment.