-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Enable bytes and string data types for max/min udaf #9081
Merged
bvarghese1
merged 6 commits into
confluentinc:master
from
bvarghese1:feat_max_min_byte_string_type
May 2, 2022
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
030f214
feat: enable max/min udaf for string & bytes data types
bvarghese1 c142d77
test: Add unit tests to validate max/min udafs for string & bytes dat…
bvarghese1 4772166
test: Add qtt to validate max/min udafs with string & bytes data types
bvarghese1 93a9762
Merge remote-tracking branch 'upstream/master' into feat_max_min_byte…
bvarghese1 f59a8db
test: Add historical plans for max/min bytes & string data types
bvarghese1 be09c0a
refactor: Set supportedArgs with string & bytes for max/min agg funct…
bvarghese1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
130 changes: 130 additions & 0 deletions
130
ksqldb-engine/src/test/java/io/confluent/ksql/function/udaf/max/BytesMaxKudafTest.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,130 @@ | ||
/* | ||
* Copyright 2022 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.max; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import io.confluent.ksql.GenericKey; | ||
import io.confluent.ksql.function.AggregateFunctionInitArguments; | ||
import io.confluent.ksql.function.KsqlAggregateFunction; | ||
import io.confluent.ksql.function.udf.string.FromBytes; | ||
import io.confluent.ksql.function.udf.string.ToBytes; | ||
import io.confluent.ksql.schema.ksql.SqlArgument; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import io.confluent.ksql.util.BytesUtils.Encoding; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import org.apache.kafka.streams.kstream.Merger; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BytesMaxKudafTest { | ||
|
||
private ToBytes toBytesUDF; | ||
private FromBytes fromBytesUDF; | ||
|
||
@Before | ||
public void setUp() { | ||
toBytesUDF = new ToBytes(); | ||
fromBytesUDF = new FromBytes(); | ||
} | ||
|
||
@Test | ||
public void shouldFindCorrectMax() { | ||
final MaxKudaf<ByteBuffer> bytesMaxKudaf = getMaxComparableKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMax = "A"; | ||
for (final String val : values) { | ||
currentMax = fromBytesUDF.fromBytes( | ||
bytesMaxKudaf.aggregate( | ||
toBytesUDF.toBytes(val, Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes(currentMax, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
} | ||
assertThat("F", equalTo(currentMax)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleNull() { | ||
final MaxKudaf<ByteBuffer> bytesMaxKudaf = getMaxComparableKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMax = null; | ||
|
||
// null before any aggregation | ||
currentMax = fromBytesUDF.fromBytes( | ||
bytesMaxKudaf.aggregate( | ||
null, | ||
toBytesUDF.toBytes(currentMax, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(null, equalTo(currentMax)); | ||
|
||
// now send each value to aggregation and verify | ||
for (final String val : values) { | ||
currentMax = fromBytesUDF.fromBytes( | ||
bytesMaxKudaf.aggregate( | ||
toBytesUDF.toBytes(val, Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes(currentMax, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
} | ||
assertThat("F", equalTo(currentMax)); | ||
|
||
// null should not impact result | ||
currentMax = fromBytesUDF.fromBytes( | ||
bytesMaxKudaf.aggregate( | ||
null, | ||
toBytesUDF.toBytes(currentMax, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat("F", equalTo(currentMax)); | ||
} | ||
|
||
@Test | ||
public void shouldFindCorrectMaxForMerge() { | ||
final MaxKudaf bytesMaxKudaf = getMaxComparableKudaf(); | ||
final Merger<GenericKey, ByteBuffer> merger = bytesMaxKudaf.getMerger(); | ||
final String mergeResult1 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("B", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("D", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult1, equalTo("D")); | ||
final String mergeResult2 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("P", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("F", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult2, equalTo("P")); | ||
final String mergeResult3 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("A", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("K", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult3, equalTo("K")); | ||
} | ||
|
||
private MaxKudaf getMaxComparableKudaf() { | ||
final KsqlAggregateFunction aggregateFunction = new MaxAggFunctionFactory() | ||
.createAggregateFunction(Collections.singletonList(SqlArgument.of(SqlTypes.BYTES)), | ||
AggregateFunctionInitArguments.EMPTY_ARGS); | ||
assertThat(aggregateFunction, instanceOf(MaxKudaf.class)); | ||
return (MaxKudaf) aggregateFunction; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
ksqldb-engine/src/test/java/io/confluent/ksql/function/udaf/max/StringMaxKudafTest.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,85 @@ | ||
/* | ||
* Copyright 2022 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.max; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import io.confluent.ksql.GenericKey; | ||
import io.confluent.ksql.function.AggregateFunctionInitArguments; | ||
import io.confluent.ksql.function.KsqlAggregateFunction; | ||
import io.confluent.ksql.schema.ksql.SqlArgument; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import java.util.Collections; | ||
import org.apache.kafka.streams.kstream.Merger; | ||
import org.junit.Test; | ||
|
||
public class StringMaxKudafTest { | ||
|
||
@Test | ||
public void shouldFindCorrectMax() { | ||
final MaxKudaf<String> stringMaxKudaf = getMaxComparableKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMax = "A"; | ||
for (final String val : values) { | ||
currentMax = stringMaxKudaf.aggregate(val, currentMax); | ||
} | ||
assertThat("F", equalTo(currentMax)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleNull() { | ||
final MaxKudaf<String> stringMaxKudaf = getMaxComparableKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMax = null; | ||
|
||
// null before any aggregation | ||
currentMax = stringMaxKudaf.aggregate(null, currentMax); | ||
assertThat(null, equalTo(currentMax)); | ||
|
||
// now send each value to aggregation and verify | ||
for (final String val : values) { | ||
currentMax = stringMaxKudaf.aggregate(val, currentMax); | ||
} | ||
assertThat("F", equalTo(currentMax)); | ||
|
||
// null should not impact result | ||
currentMax = stringMaxKudaf.aggregate(null, currentMax); | ||
assertThat("F", equalTo(currentMax)); | ||
} | ||
|
||
@Test | ||
public void shouldFindCorrectMaxForMerge() { | ||
final MaxKudaf stringMaxKudaf = getMaxComparableKudaf(); | ||
final Merger<GenericKey, String> merger = stringMaxKudaf.getMerger(); | ||
final String mergeResult1 = merger.apply(null, "B", "D"); | ||
assertThat(mergeResult1, equalTo("D")); | ||
final String mergeResult2 = merger.apply(null, "P", "F"); | ||
assertThat(mergeResult2, equalTo("P")); | ||
final String mergeResult3 = merger.apply(null, "A", "K"); | ||
assertThat(mergeResult3, equalTo("K")); | ||
} | ||
|
||
private MaxKudaf getMaxComparableKudaf() { | ||
final KsqlAggregateFunction aggregateFunction = new MaxAggFunctionFactory() | ||
.createAggregateFunction(Collections.singletonList(SqlArgument.of(SqlTypes.STRING)), | ||
AggregateFunctionInitArguments.EMPTY_ARGS); | ||
assertThat(aggregateFunction, instanceOf(MaxKudaf.class)); | ||
return (MaxKudaf) aggregateFunction; | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
ksqldb-engine/src/test/java/io/confluent/ksql/function/udaf/min/BytesMinKudafTest.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,130 @@ | ||
/* | ||
* Copyright 2022 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.min; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import io.confluent.ksql.GenericKey; | ||
import io.confluent.ksql.function.AggregateFunctionInitArguments; | ||
import io.confluent.ksql.function.KsqlAggregateFunction; | ||
import io.confluent.ksql.function.udf.string.FromBytes; | ||
import io.confluent.ksql.function.udf.string.ToBytes; | ||
import io.confluent.ksql.schema.ksql.SqlArgument; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import io.confluent.ksql.util.BytesUtils.Encoding; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collections; | ||
import org.apache.kafka.streams.kstream.Merger; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BytesMinKudafTest { | ||
|
||
private ToBytes toBytesUDF; | ||
private FromBytes fromBytesUDF; | ||
|
||
@Before | ||
public void setUp() { | ||
toBytesUDF = new ToBytes(); | ||
fromBytesUDF = new FromBytes(); | ||
} | ||
|
||
@Test | ||
public void shouldFindCorrectMin() { | ||
final MinKudaf<ByteBuffer> bytesMinKudaf = getBytesMinKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMin = "D"; | ||
for (final String val : values) { | ||
currentMin = fromBytesUDF.fromBytes( | ||
bytesMinKudaf.aggregate( | ||
toBytesUDF.toBytes(val, Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes(currentMin, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
} | ||
assertThat("A", equalTo(currentMin)); | ||
} | ||
|
||
@Test | ||
public void shouldHandleNull() { | ||
final MinKudaf<ByteBuffer> bytesMinKudaf = getBytesMinKudaf(); | ||
final String[] values = new String[]{"C", "F", "B", "E", "A", "D", "B"}; | ||
String currentMin = null; | ||
|
||
// null before any aggregation | ||
currentMin = fromBytesUDF.fromBytes( | ||
bytesMinKudaf.aggregate( | ||
null, | ||
toBytesUDF.toBytes(currentMin, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(null, equalTo(currentMin)); | ||
|
||
// now send each value to aggregation and verify | ||
for (final String val : values) { | ||
currentMin = fromBytesUDF.fromBytes( | ||
bytesMinKudaf.aggregate( | ||
toBytesUDF.toBytes(val, Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes(currentMin, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
} | ||
assertThat("A", equalTo(currentMin)); | ||
|
||
// null should not impact result | ||
currentMin = fromBytesUDF.fromBytes( | ||
bytesMinKudaf.aggregate( | ||
null, | ||
toBytesUDF.toBytes(currentMin, Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat("A", equalTo(currentMin)); | ||
} | ||
|
||
@Test | ||
public void shouldFindCorrectMinForMerge() { | ||
final MinKudaf bytesMinKudaf = getBytesMinKudaf(); | ||
final Merger<GenericKey, ByteBuffer> merger = bytesMinKudaf.getMerger(); | ||
final String mergeResult1 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("B", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("D", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult1, equalTo("B")); | ||
final String mergeResult2 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("P", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("F", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult2, equalTo("F")); | ||
final String mergeResult3 = fromBytesUDF.fromBytes( | ||
merger.apply( | ||
null, | ||
toBytesUDF.toBytes("A", Encoding.ASCII.toString()), | ||
toBytesUDF.toBytes("K", Encoding.ASCII.toString())), | ||
Encoding.ASCII.toString()); | ||
assertThat(mergeResult3, equalTo("A")); | ||
} | ||
|
||
private MinKudaf getBytesMinKudaf() { | ||
final KsqlAggregateFunction aggregateFunction = new MinAggFunctionFactory() | ||
.createAggregateFunction(Collections.singletonList(SqlArgument.of(SqlTypes.BYTES)), | ||
AggregateFunctionInitArguments.EMPTY_ARGS); | ||
assertThat(aggregateFunction, instanceOf(MinKudaf.class)); | ||
return (MinKudaf) aggregateFunction; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice catch that STRINGs weren't supported or mentioned on the issue: #8913
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.
The code already had support for those types. @jzaralim also asked if we could support strings along with bytes.