-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add COUNT_DISTINCT and allow generics in UDAFs
- Loading branch information
Showing
6 changed files
with
247 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
ksql-engine/src/main/java/io/confluent/ksql/function/udaf/count/CountDistinct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udaf.count; | ||
|
||
import com.clearspring.analytics.stream.cardinality.HyperLogLog; | ||
import com.clearspring.analytics.stream.cardinality.RegisterSet; | ||
import com.google.common.primitives.Ints; | ||
import io.confluent.ksql.function.udaf.Udaf; | ||
import io.confluent.ksql.function.udaf.UdafDescription; | ||
import io.confluent.ksql.function.udaf.UdafFactory; | ||
import java.util.List; | ||
|
||
@UdafDescription( | ||
name = "COUNT_DISTINCT", | ||
description = CountDistinct.DESCRIPTION | ||
) | ||
public class CountDistinct { | ||
|
||
static final String DESCRIPTION = "This function returns the number of items found in a group. " | ||
+ "The implementation is probabilistic with a typical accuracy (standard error) of less " | ||
+ "than 1%."; | ||
|
||
// magic number causes accuracy < .01 - we can consider making | ||
// this configurable if the need arises | ||
private static final int M = 1 << 14; | ||
private static final int LOG_2_M = 14; | ||
|
||
private CountDistinct() { | ||
} | ||
|
||
// NOTE: since our UDAF framework requires the aggregate values to | ||
// be serializable, and we don't support serialization of native int[], | ||
// this implementation can be optimized by avoiding conversions between | ||
// int[] and List<Integer> - since RegisterSet requires an int[], we would | ||
// need to duplicate a lot of code to get this to be zero-copy | ||
private static <T> Udaf<T, List<Integer>, Long> countDistinct() { | ||
return new Udaf<T, List<Integer>, Long>() { | ||
|
||
@Override | ||
public List<Integer> initialize() { | ||
return Ints.asList(new int[RegisterSet.getSizeForCount(M)]); | ||
} | ||
|
||
@Override | ||
public List<Integer> aggregate(T current, List<Integer> aggregate) { | ||
if (current == null) { | ||
return aggregate; | ||
} | ||
|
||
// this operation updates the underlying bytes | ||
final int[] ints = Ints.toArray(aggregate); | ||
final RegisterSet set = new RegisterSet(M, ints); | ||
|
||
// this modifies the underlying ints | ||
toHyperLogLog(set).offer(current); | ||
|
||
return Ints.asList(ints); | ||
} | ||
|
||
@Override | ||
public List<Integer> merge(List<Integer> aggOne, List<Integer> aggTwo) { | ||
final RegisterSet registerSet = new RegisterSet(M, Ints.toArray(aggOne)); | ||
registerSet.merge(new RegisterSet(M, Ints.toArray(aggTwo))); | ||
|
||
return Ints.asList(registerSet.bits()); | ||
} | ||
|
||
@Override | ||
public Long map(List<Integer> agg) { | ||
return toHyperLogLog(new RegisterSet(M, Ints.toArray(agg))).cardinality(); | ||
} | ||
}; | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
private static HyperLogLog toHyperLogLog(final RegisterSet set) { | ||
return new HyperLogLog(LOG_2_M, set); | ||
} | ||
|
||
@UdafFactory(description = "Count distinct") | ||
public static <T> Udaf<T, List<Integer>, Long> distinct() { | ||
return countDistinct(); | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
ksql-engine/src/test/java/io/confluent/ksql/function/udaf/count/CountDistinctKudafTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udaf.count; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import com.google.common.primitives.Ints; | ||
import io.confluent.ksql.function.udaf.Udaf; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import org.junit.Test; | ||
|
||
public class CountDistinctKudafTest { | ||
|
||
@Test | ||
public void shouldCountStrings() { | ||
// Given: | ||
final Udaf<String, List<Integer>, Long> udaf = CountDistinct.distinct(); | ||
final String[] values = IntStream | ||
.range(0, 100) | ||
.mapToObj(i -> String.valueOf(i % 4)) | ||
.toArray(String[]::new); | ||
|
||
List<Integer> agg = udaf.initialize(); | ||
|
||
// When: | ||
for (String value : values) { | ||
agg = udaf.aggregate(value, agg); | ||
} | ||
|
||
// Then: | ||
assertThat(udaf.map(agg), is(4L)); | ||
} | ||
|
||
@Test | ||
public void shouldCountList() { | ||
// Given: | ||
final Udaf<List<Integer>, List<Integer>, Long> udaf = CountDistinct.distinct(); | ||
final List<List<Integer>> values = IntStream | ||
.range(0, 100) | ||
.mapToObj(i -> Ints.asList(i % 4)) | ||
.collect(Collectors.toList()); | ||
|
||
List<Integer> agg = udaf.initialize(); | ||
|
||
// When: | ||
for (List<Integer> value : values) { | ||
agg = udaf.aggregate(value, agg); | ||
} | ||
|
||
// Then: | ||
assertThat(udaf.map(agg), is(4L)); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreNulls() { | ||
// Given: | ||
final Udaf<String, List<Integer>, Long> udaf = CountDistinct.distinct(); | ||
List<Integer> agg = udaf.initialize(); | ||
|
||
// When: | ||
agg = udaf.aggregate(null, agg); | ||
|
||
// Then: | ||
assertThat(udaf.map(agg), is(0L)); | ||
} | ||
|
||
@Test | ||
public void shouldMerge() { | ||
// Given: | ||
final Udaf<String, List<Integer>, Long> udaf = CountDistinct.distinct(); | ||
final String[] values1 = IntStream | ||
.range(0, 100) | ||
.mapToObj(i -> String.valueOf(i % 4)) | ||
.toArray(String[]::new); | ||
|
||
List<Integer> agg1 = udaf.initialize(); | ||
List<Integer> agg2 = udaf.initialize(); | ||
|
||
// When: | ||
for (String value : values1) { | ||
agg1 = udaf.aggregate(value, agg1); | ||
} | ||
|
||
for (String value : new String[]{"5"}) { | ||
agg2 = udaf.aggregate(value, agg2); | ||
} | ||
|
||
// Then: | ||
assertThat(udaf.map(udaf.merge(agg1, agg2)), is(5L)); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
ksql-functional-tests/src/test/resources/query-validation-tests/count-distinct.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"tests": [ | ||
{ | ||
"name": "count distinct", | ||
"statements": [ | ||
"CREATE STREAM TEST (ID varchar, NAME varchar) WITH (kafka_topic='test_topic', value_format='JSON');", | ||
"CREATE TABLE S2 as SELECT id, count_distinct(name) as count FROM test group by id;" | ||
], | ||
"inputs": [ | ||
{"topic": "test_topic", "key": "0", "value": {"id": "foo", "name": "one"}}, | ||
{"topic": "test_topic", "key": "0", "value": {"id": "foo", "name": "two"}}, | ||
{"topic": "test_topic", "key": "0", "value": {"id": "foo", "name": "one"}}, | ||
{"topic": "test_topic", "key": "0", "value": {"id": "foo", "name": "two"}}, | ||
{"topic": "test_topic", "key": "0", "value": {"id": "bar", "name": "one"}}, | ||
{"topic": "test_topic", "key": "0", "value": {"id": "foo", "name": null}} | ||
], | ||
"outputs": [ | ||
{"topic": "S2", "key": "foo" ,"value": {"ID": "foo", "COUNT": 1}}, | ||
{"topic": "S2", "key": "foo" ,"value": {"ID": "foo", "COUNT": 2}}, | ||
{"topic": "S2", "key": "foo" ,"value": {"ID": "foo", "COUNT": 2}}, | ||
{"topic": "S2", "key": "foo" ,"value": {"ID": "foo", "COUNT": 2}}, | ||
{"topic": "S2", "key": "bar" ,"value": {"ID": "bar", "COUNT": 1}}, | ||
{"topic": "S2", "key": "foo" ,"value": {"ID": "foo", "COUNT": 2}} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters