-
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: Support BYTES and time types for TopKDistinct, Greatest, and Le…
…ast (#9202) * feat: enable TopKDistinct for time & bytes data types - Add support for bytes, time, timestamp, and date arguments to TopKDistinct - See #9125 * feat: enable Greatest/Least for time & bytes data types - Add support for bytes, time, timestamp, and date arguments to Greatest and Least - See #9125 * fix: test greatest/least w/ null arrays, not array containing null element - The cast to the type of array element makes the compiler implicitly create an array around the variable arguments. - So an array was being created with a null element. - shouldHandleNullArrays() is intended to test a null array, not an array with nulls. * style: add previously missing licenses for greatest & least test files
- Loading branch information
1 parent
2e1c457
commit 6824f23
Showing
48 changed files
with
5,871 additions
and
68 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
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
106 changes: 106 additions & 0 deletions
106
...rc/test/java/io/confluent/ksql/function/udaf/topkdistinct/BytesTopKDistinctKudafTest.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,106 @@ | ||
/* | ||
* Copyright 2022 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community 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.topkdistinct; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.confluent.ksql.function.udf.string.ToBytes; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import io.confluent.ksql.util.BytesUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class BytesTopKDistinctKudafTest { | ||
private final List<String> valuesArray = ImmutableList.of("A", "D", "F", "A", "G", "H", "B", "H", | ||
"I", "E", "C", "H", "I"); | ||
private final TopkDistinctKudaf<ByteBuffer> bytesTopkDistinctKudaf | ||
= TopKDistinctTestUtils.getTopKDistinctKudaf(3, SqlTypes.BYTES); | ||
private ToBytes toBytesUDF; | ||
|
||
@Before | ||
public void setUp() { | ||
toBytesUDF = new ToBytes(); | ||
} | ||
|
||
@Test | ||
public void shouldAggregateTopK() { | ||
List<ByteBuffer> currentVal = new ArrayList<>(); | ||
for (final String d : valuesArray) { | ||
currentVal = bytesTopkDistinctKudaf.aggregate(toBytes(d), currentVal); | ||
} | ||
|
||
List<ByteBuffer> expected = toBytes(ImmutableList.of("I", "H", "G")); | ||
assertThat("Invalid results.", currentVal, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldAggregateTopKWithLessThanKValues() { | ||
List<ByteBuffer> currentVal = new ArrayList<>(); | ||
currentVal = bytesTopkDistinctKudaf.aggregate(toBytes("I"), currentVal); | ||
|
||
assertThat("Invalid results.", currentVal, equalTo(toBytes(ImmutableList.of("I")))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopK() { | ||
final List<ByteBuffer> array1 = toBytes(ImmutableList.of("D", "B", "A")); | ||
final List<ByteBuffer> array2 = toBytes(ImmutableList.of("E", "D", "C")); | ||
|
||
assertThat("Invalid results.", bytesTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(toBytes(ImmutableList.of("E", "D", "C")))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithNulls() { | ||
final List<ByteBuffer> array1 = toBytes(ImmutableList.of("B", "A")); | ||
final List<ByteBuffer> array2 = toBytes(ImmutableList.of("C")); | ||
|
||
assertThat("Invalid results.", bytesTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(toBytes(ImmutableList.of("C", "B", "A")))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithNullsDuplicates() { | ||
final List<ByteBuffer> array1 = toBytes(ImmutableList.of("B", "A")); | ||
final List<ByteBuffer> array2 = toBytes(ImmutableList.of("C", "B")); | ||
|
||
assertThat("Invalid results.", bytesTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(toBytes(ImmutableList.of("C", "B", "A")))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithMoreNulls() { | ||
final List<ByteBuffer> array1 = toBytes(ImmutableList.of("A")); | ||
final List<ByteBuffer> array2 = toBytes(ImmutableList.of("A")); | ||
|
||
assertThat("Invalid results.", bytesTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(toBytes(ImmutableList.of("A")))); | ||
} | ||
|
||
private ByteBuffer toBytes(final String val) { | ||
return toBytesUDF.toBytes(val, BytesUtils.Encoding.ASCII.toString()); | ||
} | ||
|
||
private List<ByteBuffer> toBytes(final List<String> vals) { | ||
return vals.stream().map(this::toBytes).collect(Collectors.toList()); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...src/test/java/io/confluent/ksql/function/udaf/topkdistinct/DateTopKDistinctKudafTest.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,90 @@ | ||
/* | ||
* Copyright 2022 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community 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.topkdistinct; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import org.junit.Test; | ||
|
||
import java.sql.Date; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class DateTopKDistinctKudafTest { | ||
|
||
private final List<Date> valuesArray = ImmutableList.of(new Date(10), new Date(30), new Date(45), | ||
new Date(10), new Date(50), new Date(60), new Date(20), new Date(60), new Date(80), | ||
new Date(35), new Date(25), new Date(60), new Date(80)); | ||
private final TopkDistinctKudaf<Date> dateTopkDistinctKudaf | ||
= TopKDistinctTestUtils.getTopKDistinctKudaf(3, SqlTypes.DATE); | ||
|
||
@Test | ||
public void shouldAggregateTopK() { | ||
List<Date> currentVal = new ArrayList<>(); | ||
for (final Date d : valuesArray) { | ||
currentVal = dateTopkDistinctKudaf.aggregate(d, currentVal); | ||
} | ||
|
||
assertThat("Invalid results.", currentVal, | ||
equalTo(ImmutableList.of(new Date(80), new Date(60), new Date(50)))); | ||
} | ||
|
||
@Test | ||
public void shouldAggregateTopKWithLessThanKValues() { | ||
List<Date> currentVal = new ArrayList<>(); | ||
currentVal = dateTopkDistinctKudaf.aggregate(new Date(80), currentVal); | ||
|
||
assertThat("Invalid results.", currentVal, equalTo(ImmutableList.of(new Date(80)))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopK() { | ||
final List<Date> array1 = ImmutableList.of(new Date(50), new Date(45), new Date(25)); | ||
final List<Date> array2 = ImmutableList.of(new Date(60), new Date(50), new Date(48)); | ||
|
||
assertThat("Invalid results.", dateTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(ImmutableList.of(new Date(60), new Date(50), new Date(48)))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithNulls() { | ||
final List<Date> array1 = ImmutableList.of(new Date(50), new Date(45)); | ||
final List<Date> array2 = ImmutableList.of(new Date(60)); | ||
|
||
assertThat("Invalid results.", dateTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(ImmutableList.of(new Date(60), new Date(50), new Date(45)))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithNullsDuplicates() { | ||
final List<Date> array1 = ImmutableList.of(new Date(50), new Date(45)); | ||
final List<Date> array2 = ImmutableList.of(new Date(60), new Date(50)); | ||
|
||
assertThat("Invalid results.", dateTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(ImmutableList.of(new Date(60), new Date(50), new Date(45)))); | ||
} | ||
|
||
@Test | ||
public void shouldMergeTopKWithMoreNulls() { | ||
final List<Date> array1 = ImmutableList.of(new Date(60)); | ||
final List<Date> array2 = ImmutableList.of(new Date(60)); | ||
|
||
assertThat("Invalid results.", dateTopkDistinctKudaf.getMerger().apply(null, array1, array2), | ||
equalTo(ImmutableList.of(new Date(60)))); | ||
} | ||
} |
Oops, something went wrong.