-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apache DataSketches plugin for Trino #6643
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
====================== | ||
DataSketches Functions | ||
====================== | ||
DataSketches is a high-performance library of stochastic streaming | ||
algorithms commonly called ”sketches” in the data sciences. Sketches are | ||
small, stateful programs that process massive data as a stream and can | ||
provide approximate answers, with mathematical guarantees, to | ||
computationally difficult queries orders-of-magnitude faster than | ||
traditional, exact methods. | ||
The DataSketches functions allows querying the fast and memory-efficient `Apache | ||
DataSkecthes <https://datasketches.apache.org/docs/Community/Research.html>`_ | ||
from Trino. Support for `Theta Sketch Framework <https://datasketches.apache.org/docs/Theta/ThetaSketchFramework.html>`_ | ||
is added, specifically :func:`theta_sketch_union` and :func:`theta_sketch_estimate` functions. | ||
These functions are used in the ``count distinct`` queries using sketches. | ||
Datasketches can be created using Hive or Pig using respective sketch APIs. | ||
|
||
DataSketches functions | ||
---------------------- | ||
|
||
.. function:: theta_sketch_union(sketches) -> sketch | ||
|
||
Returns a single sketch which is a merged collection of sketches. | ||
|
||
.. function:: theta_sketch_estimate(sketch) -> double | ||
|
||
Returns the estimated value of the sketch. | ||
|
||
Example in Trino for using DataSketches | ||
--------------------------------------- | ||
Query:: | ||
|
||
sql | ||
SELECT | ||
o_orderdate as date, | ||
theta_sketch_estimate(theta_sketch_union(o_custkey_sketch)) AS unique_user_count | ||
SUM(o_totalprice) AS user_spent, | ||
FROM tpch.sf100000.orders WHERE o_orderdate >= dateadd(day, -90, current_date) | ||
GROUP BY o_orderdate; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-root</artifactId> | ||
<version>362-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>trino-datasketches</artifactId> | ||
<name>trino-datasketches</name> | ||
<packaging>trino-plugin</packaging> | ||
<url>http://datasketches.apache.org/</url> | ||
|
||
<properties> | ||
<air.main.basedir>${project.parent.basedir}</air.main.basedir> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-array</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.datasketches</groupId> | ||
<artifactId>datasketches-java</artifactId> | ||
<version>2.0.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.datasketches</groupId> | ||
<artifactId>datasketches-memory</artifactId> | ||
<version>1.3.0</version> | ||
</dependency> | ||
|
||
<!-- Trino SPI --> | ||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-spi</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.airlift</groupId> | ||
<artifactId>slice</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.openjdk.jol</groupId> | ||
<artifactId>jol-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-hive</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-hive</artifactId> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-main</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.trino</groupId> | ||
<artifactId>trino-testing</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.trino.hadoop</groupId> | ||
<artifactId>hadoop-apache</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.datasketches.state; | ||
|
||
import io.airlift.slice.Slice; | ||
import io.trino.spi.function.AccumulatorState; | ||
import io.trino.spi.function.AccumulatorStateMetadata; | ||
|
||
/** | ||
* State object to keep track of sketch aggregations. | ||
*/ | ||
@AccumulatorStateMetadata(stateSerializerClass = SketchStateSerializer.class, stateFactoryClass = SketchStateFactory.class) | ||
public interface SketchState | ||
extends AccumulatorState | ||
{ | ||
Slice getSketch(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #14290 added a Theta sketch aggregation function: Anyway, i was able to use |
||
|
||
int getNominalEntries(); | ||
|
||
long getSeed(); | ||
|
||
void setSketch(Slice value); | ||
|
||
void setNominalEntries(int value); | ||
|
||
void setSeed(long value); | ||
|
||
void merge(SketchState state); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are two variants of
theta_sketch_union
, one having parameters. The parameters need to be documented.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, should the docs say this is an aggregation function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no "sketch" type. should this be "varbinary"?