ClickHouse/docs/en/sql-reference/aggregate-functions/reference/largestTriangleThreeBuckets.md
Sinan c7f78ac73a
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>
2023-10-18 14:47:18 +02:00

2.9 KiB

slug sidebar_position sidebar_label
/en/sql-reference/aggregate-functions/reference/largestTriangleThreeBuckets 312 largestTriangleThreeBuckets

largestTriangleThreeBuckets

Applies the Largest-Triangle-Three-Buckets 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

largestTriangleThreeBuckets(n)(x, y)

Alias: lttb.

Arguments

Parameters

  • n — number of points in the resulting series. UInt64.

Returned values

Array of Tuple with two elements:

Example

Input table:

┌─────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:

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

Result:

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