From e0afec8a7654334e3e9992433f5a0424d2ed51b8 Mon Sep 17 00:00:00 2001 From: JingsongLi Date: Tue, 21 Jan 2020 19:09:15 +0800 Subject: [PATCH] [FLINK-15706][table-planner-blink] Fix LastValueAggFunctionWithOrderTest compilation error due to incompatible types This partially reverts the commit dcc1330375826b779e4902176bb2473704dabb11 and uses the initial approach from #10158 of using Enclosed annotation. The problem with the TestSpec approach was that the top level classes shouldn't use generics as they are stripped when instantiated by JUnit runner which may lead to generic signature mismatch. This closes #10915 --- .../aggfunctions/AggFunctionTestBase.java | 23 - ...LastValueAggFunctionWithOrderTestBase.java | 17 - .../FirstValueAggFunctionWithOrderTest.java | 742 +++++++------- ...FirstValueAggFunctionWithoutOrderTest.java | 514 +++++----- ...ueWithRetractAggFunctionWithOrderTest.java | 748 ++++++++------- ...ithRetractAggFunctionWithoutOrderTest.java | 522 +++++----- .../LastValueAggFunctionWithOrderTest.java | 742 +++++++------- .../LastValueAggFunctionWithoutOrderTest.java | 517 +++++----- ...ueWithRetractAggFunctionWithOrderTest.java | 760 ++++++++------- ...ithRetractAggFunctionWithoutOrderTest.java | 523 +++++----- .../MaxWithRetractAggFunctionTest.java | 902 +++++++++++------- .../MinWithRetractAggFunctionTest.java | 896 ++++++++++------- 12 files changed, 3980 insertions(+), 2926 deletions(-) diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/AggFunctionTestBase.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/AggFunctionTestBase.java index 62023598c0a97..365e874ae64b0 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/AggFunctionTestBase.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/AggFunctionTestBase.java @@ -54,29 +54,6 @@ */ public abstract class AggFunctionTestBase { - /** - * Spec for parameterized aggregate function tests. - */ - protected static class AggFunctionTestSpec { - final AggregateFunction aggregator; - final List> inputValueSets; - final List expectedResults; - - public AggFunctionTestSpec( - AggregateFunction aggregator, - List> inputValueSets, - List expectedResults) { - this.aggregator = aggregator; - this.inputValueSets = inputValueSets; - this.expectedResults = expectedResults; - } - - @Override - public String toString() { - return aggregator.getClass().getSimpleName(); - } - } - protected abstract List> getInputValueSets(); protected abstract List getExpectedResults(); diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstLastValueAggFunctionWithOrderTestBase.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstLastValueAggFunctionWithOrderTestBase.java index 06f84a5fadd5d..96eb0e25644ac 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstLastValueAggFunctionWithOrderTestBase.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstLastValueAggFunctionWithOrderTestBase.java @@ -36,23 +36,6 @@ */ public abstract class FirstLastValueAggFunctionWithOrderTestBase extends AggFunctionTestBase { - /** - * An AggFunctionTestSpec with input order. - */ - protected static class AggFunctionWithOrderTestSpec extends AggFunctionTestSpec { - - final List> inputOrderSets; - - public AggFunctionWithOrderTestSpec( - AggregateFunction aggregator, - List> inputOrderSets, - List> inputValueSets, - List expectedResults) { - super(aggregator, inputValueSets, expectedResults); - this.inputOrderSets = inputOrderSets; - } - } - protected Method getAccumulateFunc() throws NoSuchMethodException { return getAggregator().getClass().getMethod("accumulate", getAccClass(), Object.class, Long.class); } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithOrderTest.java index dbd9b8391cce4..658541ca09d2b 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithOrderTest.java @@ -33,357 +33,447 @@ import org.apache.flink.table.planner.functions.aggfunctions.FirstValueAggFunction.StringFirstValueAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in FirstValue aggregate function. * This class tests `accumulate` method with order argument. */ -@RunWith(Parameterized.class) -public class FirstValueAggFunctionWithOrderTest extends FirstLastValueAggFunctionWithOrderTestBase { +@RunWith(Enclosed.class) +public class FirstValueAggFunctionWithOrderTest { - @Parameterized.Parameter - public AggFunctionWithOrderTestSpec aggFunctionTestSpec; + /** + * The base test class for FirstValueAggFunction with order. + */ + public abstract static class FirstValueAggFunctionWithOrderTestBase + extends FirstLastValueAggFunctionWithOrderTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + } + + /** + * Test FirstValueAggFunction for number type. + */ + public abstract static class NumberFirstValueAggFunctionWithOrderTestBase + extends FirstValueAggFunctionWithOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null, + getValue("3"), + getValue("2"), + getValue("-99") + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("5") + ) + ); + } - @Override - protected List> getInputOrderSets() { - return aggFunctionTestSpec.inputOrderSets; + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + 6L, + 11L, + 3L, + 7L, + 5L + ), + Arrays.asList( + 8L, + 6L, + 9L, + 5L + ), + Arrays.asList( + null, + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("3"), + null, + getValue("5") + ); + } } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + /** + * Test for ByteFirstValueAggFunction. + */ + public static class ByteFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteFirstValueAggFunction(); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ShortFirstValueAggFunction. + */ + public static class ShortFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortFirstValueAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for IntFirstValueAggFunction. + */ + public static class IntFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntFirstValueAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ByteFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ShortFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new IntFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new LongFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new FloatFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DoubleFirstValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new BooleanFirstValueAggFunction(), - Arrays.asList( - Arrays.asList( - 6L, - 2L, - 3L - ), - Arrays.asList( - 1L, - 2L, - 3L - ), - Arrays.asList( - 10L, - 2L, - 5L, - 11L, - 3L, - 7L, - 5L - ), - Arrays.asList( - 6L, - 9L, - 5L - ), - Arrays.asList( - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - false, - null, - true - ) - ), - /** - * Test for DecimalFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DecimalFirstValueAggFunction(DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 1L, - 5L, - null, - 3L, - 1L, - 5L, - 2L - ), - Arrays.asList( - 6L, - 5L, - null, - 8L, - null - ), - Arrays.asList( - 8L, - 6L - ) - ), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringFirstValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new StringFirstValueAggFunction(), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - null, - 3L, - 1L, - 5L - ), - Arrays.asList( - 6L, - 5L - ), - Arrays.asList( - 8L, - 6L - ), - Arrays.asList( - 6L, - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("def"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("e") - ) - ) - ); + /** + * Test for LongFirstValueAggFunction. + */ + public static class LongFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongFirstValueAggFunction(); + } } - private static List> numberInputOrderSets() { - return Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - 6L, - 11L, - 3L, - 7L, - 5L - ), - Arrays.asList( - 8L, - 6L, - 9L, - 5L - ), - Arrays.asList( - null, - 6L, - 4L, - 3L - ) - ); + /** + * Test for FloatFirstValueAggFunction. + */ + public static class FloatFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatFirstValueAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null, - strToValueFun.apply("3"), - strToValueFun.apply("2"), - strToValueFun.apply("-99") - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("5") - ) - ); + /** + * Test for DoubleFirstValueAggFunction. + */ + public static class DoubleFirstValueAggFunctionWithOrderTest + extends NumberFirstValueAggFunctionWithOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleFirstValueAggFunction(); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("3"), - null, - strToValueFun.apply("5") - ); + /** + * Test for BooleanFirstValueAggFunction. + */ + public static class BooleanFirstValueAggFunctionWithOrderTest + extends FirstValueAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 6L, + 2L, + 3L + ), + Arrays.asList( + 1L, + 2L, + 3L + ), + Arrays.asList( + 10L, + 2L, + 5L, + 11L, + 3L, + 7L, + 5L + ), + Arrays.asList( + 6L, + 9L, + 5L + ), + Arrays.asList( + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + false, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanFirstValueAggFunction(); + } + } + + /** + * Test for DecimalFirstValueAggFunction. + */ + public static class DecimalFirstValueAggFunctionWithOrderTest + extends FirstValueAggFunctionWithOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 1L, + 5L, + null, + 3L, + 1L, + 5L, + 2L + ), + Arrays.asList( + 6L, + 5L, + null, + 8L, + null + ), + Arrays.asList( + 8L, + 6L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("-1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalFirstValueAggFunction(DecimalTypeInfo.of(precision, scale)); + } + } + + /** + * Test for StringFirstValueAggFunction. + */ + public static class StringFirstValueAggFunctionWithOrderTest + extends FirstValueAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + null, + 3L, + 1L, + 5L + ), + Arrays.asList( + 6L, + 5L + ), + Arrays.asList( + 8L, + 6L + ), + Arrays.asList( + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("def"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("e") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringFirstValueAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithoutOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithoutOrderTest.java index 3bfe52a300993..62be99cfdc38c 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithoutOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueAggFunctionWithoutOrderTest.java @@ -33,243 +33,329 @@ import org.apache.flink.table.planner.functions.aggfunctions.FirstValueAggFunction.StringFirstValueAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in FirstValue aggregate function. * This class tests `accumulate` method without order argument. */ -@RunWith(Parameterized.class) -public class FirstValueAggFunctionWithoutOrderTest extends AggFunctionTestBase { +@RunWith(Enclosed.class) +public class FirstValueAggFunctionWithoutOrderTest { - @Parameterized.Parameter - public AggFunctionTestSpec aggFunctionTestSpec; + /** + * The base test class for FirstValueAggFunction without order. + */ + public abstract static class FirstValueAggFunctionWithoutOrderTestBase + extends AggFunctionTestBase { + @Override + protected Class getAccClass() { + return GenericRow.class; + } + } + + /** + * Test FirstValueAggFunction for number type. + */ + public abstract static class NumberFirstValueAggFunctionWithoutOrderTest + extends FirstValueAggFunctionWithoutOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("3") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("1"), + null, + getValue("10") + ); + } + } + + /** + * Test for ByteFirstValueAggFunction. + */ + public static class ByteFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + @Override + protected AggregateFunction getAggregator() { + return new ByteFirstValueAggFunction(); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ShortFirstValueAggFunction. + */ + public static class ShortFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortFirstValueAggFunction(); + } + } + + /** + * Test for IntFirstValueAggFunction. + */ + public static class IntFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntFirstValueAggFunction(); + } + } + + /** + * Test for LongFirstValueAggFunction. + */ + public static class LongFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongFirstValueAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for FloatFirstValueAggFunction. + */ + public static class FloatFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatFirstValueAggFunction(); + } } - @Override - protected Class getAccClass() { - return GenericRow.class; + /** + * Test for DoubleFirstValueAggFunction. + */ + public static class DoubleFirstValueAggFunctionWithoutOrderTest + extends NumberFirstValueAggFunctionWithoutOrderTest { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleFirstValueAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new ByteFirstValueAggFunction(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new ShortFirstValueAggFunction(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new IntFirstValueAggFunction(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new LongFirstValueAggFunction(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new FloatFirstValueAggFunction(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new DoubleFirstValueAggFunction(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new BooleanFirstValueAggFunction(), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new DecimalFirstValueAggFunction(DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringFirstValueAggFunction. - */ - new AggFunctionTestSpec<>( - new StringFirstValueAggFunction(), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("abc"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("x") - ) - ) - ); + /** + * Test for BooleanFirstValueAggFunction. + */ + public static class BooleanFirstValueAggFunctionWithoutOrderTest extends + FirstValueAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanFirstValueAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("3") - ) - ); + /** + * Test for DecimalFirstValueAggFunction. + */ + public static class DecimalFirstValueAggFunctionWithoutOrderTest extends + FirstValueAggFunctionWithoutOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalFirstValueAggFunction(DecimalTypeInfo.of(precision, scale)); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("10") - ); + /** + * Test for StringFirstValueAggFunction. + */ + public static class StringFirstValueAggFunctionWithoutOrderTest extends + FirstValueAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("abc"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("x") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringFirstValueAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithOrderTest.java index caf1485790179..770f75adaf6fe 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithOrderTest.java @@ -33,364 +33,452 @@ import org.apache.flink.table.planner.functions.aggfunctions.FirstValueWithRetractAggFunction.StringFirstValueWithRetractAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in FirstValue with retract aggregate function. * This class tests `accumulate` method with order argument. */ -@RunWith(Parameterized.class) -public class FirstValueWithRetractAggFunctionWithOrderTest extends FirstLastValueAggFunctionWithOrderTestBase { +@RunWith(Enclosed.class) +public class FirstValueWithRetractAggFunctionWithOrderTest { - @Parameterized.Parameter - public AggFunctionWithOrderTestSpec aggFunctionTestSpec; - - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + /** + * The base test class for FirstValueWithRetractAggFunction with order. + */ + public abstract static class FirstValueWithRetractAggFunctionWithOrderTestBase + extends FirstLastValueAggFunctionWithOrderTestBase { @Override - protected List> getInputOrderSets() { - return aggFunctionTestSpec.inputOrderSets; + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class, Long.class); + } } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + /** + * Test FirstValueWithRetractAggFunction for number type. + */ + public abstract static class NumberFirstValueWithRetractAggFunctionWithOrderTestBase + extends FirstValueWithRetractAggFunctionWithOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null, + getValue("3"), + getValue("2"), + getValue("-99") + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("5") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + 6L, + 11L, + 3L, + 7L, + 5L + ), + Arrays.asList( + 8L, + 6L, + 9L, + 5L + ), + Arrays.asList( + null, + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("3"), + null, + getValue("5") + ); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ByteFirstValueWithRetractAggFunction. + */ + public static class ByteFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteFirstValueWithRetractAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for ShortFirstValueWithRetractAggFunction. + */ + public static class ShortFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortFirstValueWithRetractAggFunction(); + } } - @Override - protected Method getRetractFunc() throws NoSuchMethodException { - return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class, Long.class); + /** + * Test for IntFirstValueWithRetractAggFunction. + */ + public static class IntFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntFirstValueWithRetractAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ByteFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ShortFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new IntFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new LongFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new FloatFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DoubleFirstValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new BooleanFirstValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - 6L, - 2L, - 3L - ), - Arrays.asList( - 1L, - 2L, - 3L - ), - Arrays.asList( - 10L, - 2L, - 5L, - 11L, - 3L, - 7L, - 5L - ), - Arrays.asList( - 6L, - 9L, - 5L - ), - Arrays.asList( - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - false, - null, - true - ) - ), - /** - * Test for DecimalFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DecimalFirstValueWithRetractAggFunction( - DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 1L, - 5L, - null, - 3L, - 1L, - 5L, - 2L - ), - Arrays.asList( - 6L, - 5L, - null, - 8L, - null - ), - Arrays.asList( - 8L, - 6L - ) - ), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringFirstValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new StringFirstValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - null, - 3L, - 1L, - 5L - ), - Arrays.asList( - 6L, - 5L - ), - Arrays.asList( - 8L, - 6L - ), - Arrays.asList( - 6L, - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("def"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("e") - ) - ) - ); + /** + * Test for LongFirstValueWithRetractAggFunction. + */ + public static class LongFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongFirstValueWithRetractAggFunction(); + } } - private static List> numberInputOrderSets() { - return Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - 6L, - 11L, - 3L, - 7L, - 5L - ), - Arrays.asList( - 8L, - 6L, - 9L, - 5L - ), - Arrays.asList( - null, - 6L, - 4L, - 3L - ) - ); + /** + * Test for FloatFirstValueWithRetractAggFunction. + */ + public static class FloatFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatFirstValueWithRetractAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null, - strToValueFun.apply("3"), - strToValueFun.apply("2"), - strToValueFun.apply("-99") - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("5") - ) - ); + /** + * Test for DoubleFirstValueWithRetractAggFunction. + */ + public static class DoubleFirstValueWithRetractAggFunctionWithOrderTest + extends NumberFirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleFirstValueWithRetractAggFunction(); + } + } + + /** + * Test for BooleanFirstValueWithRetractAggFunction. + */ + public static class BooleanFirstValueWithRetractAggFunctionWithOrderTest + extends FirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 6L, + 2L, + 3L + ), + Arrays.asList( + 1L, + 2L, + 3L + ), + Arrays.asList( + 10L, + 2L, + 5L, + 11L, + 3L, + 7L, + 5L + ), + Arrays.asList( + 6L, + 9L, + 5L + ), + Arrays.asList( + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + false, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanFirstValueWithRetractAggFunction(); + } + } + + /** + * Test for DecimalFirstValueWithRetractAggFunction. + */ + public static class DecimalFirstValueWithRetractAggFunctionWithOrderTest + extends FirstValueWithRetractAggFunctionWithOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 1L, + 5L, + null, + 3L, + 1L, + 5L, + 2L + ), + Arrays.asList( + 6L, + 5L, + null, + 8L, + null + ), + Arrays.asList( + 8L, + 6L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("-1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalFirstValueWithRetractAggFunction(DecimalTypeInfo.of(precision, scale)); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("3"), - null, - strToValueFun.apply("5") - ); + /** + * Test for StringFirstValueWithRetractAggFunction. + */ + public static class StringFirstValueWithRetractAggFunctionWithOrderTest + extends FirstValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + null, + 3L, + 1L, + 5L + ), + Arrays.asList( + 6L, + 5L + ), + Arrays.asList( + 8L, + 6L + ), + Arrays.asList( + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("def"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("e") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringFirstValueWithRetractAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithoutOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithoutOrderTest.java index 386f80302e67b..2125885a8448a 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithoutOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/FirstValueWithRetractAggFunctionWithoutOrderTest.java @@ -33,250 +33,336 @@ import org.apache.flink.table.planner.functions.aggfunctions.FirstValueWithRetractAggFunction.StringFirstValueWithRetractAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in FirstValue with retract aggregate function. * This class tests `accumulate` method without order argument. */ -@RunWith(Parameterized.class) -public class FirstValueWithRetractAggFunctionWithoutOrderTest extends AggFunctionTestBase { +@RunWith(Enclosed.class) +public class FirstValueWithRetractAggFunctionWithoutOrderTest { - @Parameterized.Parameter - public AggFunctionTestSpec aggFunctionTestSpec; + /** + * The base test class for FirstValueWithRetractAggFunction without order. + */ + public abstract static class FirstValueWithRetractAggFunctionWithoutOrderTestBase + extends AggFunctionTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Class getAccClass() { + return GenericRow.class; + } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + @Override + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test FirstValueWithRetractAggFunction for number type. + */ + public abstract static class NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase + extends FirstValueWithRetractAggFunctionWithoutOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("3") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("1"), + null, + getValue("10") + ); + } + } + + /** + * Test for ByteFirstValueWithRetractAggFunction. + */ + public static class ByteFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteFirstValueWithRetractAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for ShortFirstValueWithRetractAggFunction. + */ + public static class ShortFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortFirstValueWithRetractAggFunction(); + } + } + + /** + * Test for IntFirstValueWithRetractAggFunction. + */ + public static class IntFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntFirstValueWithRetractAggFunction(); + } + } + + /** + * Test for LongFirstValueWithRetractAggFunction. + */ + public static class LongFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongFirstValueWithRetractAggFunction(); + } } - @Override - protected Class getAccClass() { - return GenericRow.class; + /** + * Test for FloatFirstValueWithRetractAggFunction. + */ + public static class FloatFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatFirstValueWithRetractAggFunction(); + } } - @Override - protected Method getRetractFunc() throws NoSuchMethodException { - return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + /** + * Test for DoubleFirstValueWithRetractAggFunction. + */ + public static class DoubleFirstValueWithRetractAggFunctionWithoutOrderTest + extends NumberFirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleFirstValueWithRetractAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ByteFirstValueWithRetractAggFunction(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ShortFirstValueWithRetractAggFunction(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new IntFirstValueWithRetractAggFunction(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new LongFirstValueWithRetractAggFunction(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new FloatFirstValueWithRetractAggFunction(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DoubleFirstValueWithRetractAggFunction(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new BooleanFirstValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - )), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalFirstValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DecimalFirstValueWithRetractAggFunction( - DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new StringFirstValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("abc"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("x") - ) - ) - - ); + /** + * Test for BooleanFirstValueWithRetractAggFunction. + */ + public static class BooleanFirstValueWithRetractAggFunctionWithoutOrderTest extends + FirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanFirstValueWithRetractAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("3") - ) - ); + /** + * Test for DecimalFirstValueWithRetractAggFunction. + */ + public static class DecimalFirstValueWithRetractAggFunctionWithoutOrderTest extends + FirstValueWithRetractAggFunctionWithoutOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalFirstValueWithRetractAggFunction(DecimalTypeInfo.of(precision, scale)); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("10") - ); + /** + * Test for StringFirstValueWithRetractAggFunction. + */ + public static class StringFirstValueWithRetractAggFunctionWithoutOrderTest extends + FirstValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("abc"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("x") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringFirstValueWithRetractAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithOrderTest.java index 1ffad8a62851a..478c3e5da696c 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithOrderTest.java @@ -33,357 +33,447 @@ import org.apache.flink.table.planner.functions.aggfunctions.LastValueAggFunction.StringLastValueAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in LastValue aggregate function. * This class tests `accumulate` method with order argument. */ -@RunWith(Parameterized.class) -public class LastValueAggFunctionWithOrderTest extends FirstLastValueAggFunctionWithOrderTestBase { +@RunWith(Enclosed.class) +public class LastValueAggFunctionWithOrderTest { - @Parameterized.Parameter - public AggFunctionWithOrderTestSpec aggFunctionTestSpec; + /** + * The base test class for LastValueAggFunction with order. + */ + public abstract static class LastValueAggFunctionWithOrderTestBase + extends FirstLastValueAggFunctionWithOrderTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + } + + /** + * Test LastValueAggFunction for number type. + */ + public abstract static class NumberLastValueAggFunctionWithOrderTestBase + extends LastValueAggFunctionWithOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null, + getValue("3"), + getValue("2"), + getValue("-99") + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("5") + ) + ); + } - @Override - protected List> getInputOrderSets() { - return aggFunctionTestSpec.inputOrderSets; + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + 6L, + 11L, + 3L, + 17L, + 5L + ), + Arrays.asList( + 8L, + 6L, + 9L, + 5L + ), + Arrays.asList( + null, + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("2"), + null, + getValue("10") + ); + } } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + /** + * Test for ByteLastValueAggFunction. + */ + public static class ByteLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteLastValueAggFunction(); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ShortLastValueAggFunction. + */ + public static class ShortLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortLastValueAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for IntLastValueAggFunction. + */ + public static class IntLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntLastValueAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ByteLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ShortLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new IntLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new LongLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new FloatLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DoubleLastValueAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new BooleanLastValueAggFunction(), - Arrays.asList( - Arrays.asList( - 6L, - 2L, - 3L - ), - Arrays.asList( - 1L, - 2L, - 3L - ), - Arrays.asList( - 10L, - 2L, - 5L, - 3L, - 11L, - 7L, - 5L - ), - Arrays.asList( - 6L, - 9L, - 5L - ), - Arrays.asList( - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - false, - null, - true - ) - ), - /** - * Test for DecimalLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DecimalLastValueAggFunction(DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 1L, - 5L, - null, - 3L, - 1L, - 5L, - 2L - ), - Arrays.asList( - 6L, - 5L, - null, - 8L, - null - ), - Arrays.asList( - 8L, - 6L - ) - ), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringLastValueAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new StringLastValueAggFunction(), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - null, - 3L, - 1L, - 15L - ), - Arrays.asList( - 6L, - 5L - ), - Arrays.asList( - 8L, - 6L - ), - Arrays.asList( - 6L, - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("zzz"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("x") - ) - ) - ); + /** + * Test for LongLastValueAggFunction. + */ + public static class LongLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongLastValueAggFunction(); + } } - private static List> numberInputOrderSets() { - return Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - 6L, - 11L, - 3L, - 17L, - 5L - ), - Arrays.asList( - 8L, - 6L, - 9L, - 5L - ), - Arrays.asList( - null, - 6L, - 4L, - 3L - ) - ); + /** + * Test for FloatLastValueAggFunction. + */ + public static class FloatLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatLastValueAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null, - strToValueFun.apply("3"), - strToValueFun.apply("2"), - strToValueFun.apply("-99") - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("5") - ) - ); + /** + * Test for DoubleLastValueAggFunction. + */ + public static class DoubleLastValueAggFunctionWithOrderTest + extends NumberLastValueAggFunctionWithOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleLastValueAggFunction(); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("2"), - null, - strToValueFun.apply("10") - ); + /** + * Test for BooleanLastValueAggFunction. + */ + public static class BooleanLastValueAggFunctionWithOrderTest + extends LastValueAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 6L, + 2L, + 3L + ), + Arrays.asList( + 1L, + 2L, + 3L + ), + Arrays.asList( + 10L, + 2L, + 5L, + 3L, + 11L, + 7L, + 5L + ), + Arrays.asList( + 6L, + 9L, + 5L + ), + Arrays.asList( + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + false, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanLastValueAggFunction(); + } + } + + /** + * Test for DecimalLastValueAggFunction. + */ + public static class DecimalLastValueAggFunctionWithOrderTest + extends LastValueAggFunctionWithOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 1L, + 5L, + null, + 3L, + 1L, + 5L, + 2L + ), + Arrays.asList( + 6L, + 5L, + null, + 8L, + null + ), + Arrays.asList( + 8L, + 6L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalLastValueAggFunction(DecimalTypeInfo.of(precision, scale)); + } + } + + /** + * Test for StringLastValueAggFunction. + */ + public static class StringLastValueAggFunctionWithOrderTest + extends LastValueAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + null, + 3L, + 1L, + 15L + ), + Arrays.asList( + 6L, + 5L + ), + Arrays.asList( + 8L, + 6L + ), + Arrays.asList( + 6L, + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("zzz"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("x") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringLastValueAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithoutOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithoutOrderTest.java index 96d199e959ef2..4af3f874b441c 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithoutOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueAggFunctionWithoutOrderTest.java @@ -33,244 +33,331 @@ import org.apache.flink.table.planner.functions.aggfunctions.LastValueAggFunction.StringLastValueAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in LastValue aggregate function. * This class tests `accumulate` method without order argument. */ -@RunWith(Parameterized.class) -public class LastValueAggFunctionWithoutOrderTest extends AggFunctionTestBase { +@RunWith(Enclosed.class) +public class LastValueAggFunctionWithoutOrderTest { - @Parameterized.Parameter - public AggFunctionTestSpec aggFunctionTestSpec; + /** + * The base test class for LastValueAggFunction without order. + */ + public abstract static class LastValueAggFunctionWithoutOrderTestBase + extends AggFunctionTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Class getAccClass() { + return GenericRow.class; + } + } + + /** + * Test LastValueAggFunction for number type. + */ + public abstract static class NumberLastValueAggFunctionWithoutOrderTestBase + extends LastValueAggFunctionWithoutOrderTestBase { + protected abstract T getValue(String v); - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("3") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("3"), + null, + getValue("3") + ); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ByteLastValueAggFunction. + */ + public static class ByteLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteLastValueAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for ShortLastValueAggFunction. + */ + public static class ShortLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortLastValueAggFunction(); + } } - @Override - protected Class getAccClass() { - return GenericRow.class; + /** + * Test for IntLastValueAggFunction. + */ + public static class IntLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntLastValueAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new ByteLastValueAggFunction(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new ShortLastValueAggFunction(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new IntLastValueAggFunction(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new LongLastValueAggFunction(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new FloatLastValueAggFunction(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new DoubleLastValueAggFunction(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new BooleanLastValueAggFunction(), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new DecimalLastValueAggFunction(DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringLastValueAggFunction. - */ - new AggFunctionTestSpec<>( - new StringLastValueAggFunction(), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a"), - null - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("zzz"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("e") - ) - ) - ); + /** + * Test for LongLastValueAggFunction. + */ + public static class LongLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongLastValueAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("3") - ) - ); + /** + * Test for FloatLastValueAggFunction. + */ + public static class FloatLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatLastValueAggFunction(); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("3"), - null, - strToValueFun.apply("3") - ); + /** + * Test for DoubleLastValueAggFunction. + */ + public static class DoubleLastValueAggFunctionWithoutOrderTest + extends NumberLastValueAggFunctionWithoutOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleLastValueAggFunction(); + } + } + + /** + * Test for BooleanLastValueAggFunction. + */ + public static class BooleanLastValueAggFunctionWithoutOrderTest extends + LastValueAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanLastValueAggFunction(); + } + } + + /** + * Test for DecimalLastValueAggFunction. + */ + public static class DecimalLastValueAggFunctionWithoutOrderTest extends + LastValueAggFunctionWithoutOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("999.999", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalLastValueAggFunction(DecimalTypeInfo.of(precision, scale)); + } + } + + /** + * Test for StringLastValueAggFunction. + */ + public static class StringLastValueAggFunctionWithoutOrderTest extends + LastValueAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a"), + null + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("zzz"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("e") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringLastValueAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithOrderTest.java index 861953e3a52fc..8ef1cf442d414 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithOrderTest.java @@ -33,372 +33,458 @@ import org.apache.flink.table.planner.functions.aggfunctions.LastValueWithRetractAggFunction.StringLastValueWithRetractAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in LastValue with retract aggregate function. * This class tests `accumulate` method with order argument. */ -@RunWith(Parameterized.class) -public class LastValueWithRetractAggFunctionWithOrderTest extends FirstLastValueAggFunctionWithOrderTestBase { +@RunWith(Enclosed.class) +public class LastValueWithRetractAggFunctionWithOrderTest { - @Parameterized.Parameter - public AggFunctionWithOrderTestSpec aggFunctionTestSpec; + /** + * The base test class for LastValueWithRetractAggFunction with order. + */ + public abstract static class LastValueWithRetractAggFunctionWithOrderTestBase + extends FirstLastValueAggFunctionWithOrderTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class, Long.class); + } + } + + /** + * Test LastValueWithRetractAggFunction for number type. + */ + public abstract static class NumberLastValueWithRetractAggFunctionWithOrderTestBase + extends LastValueWithRetractAggFunctionWithOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null, + getValue("3"), + getValue("2"), + getValue("-99") + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("5") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + 6L, + 11L, + 13L, + 7L, + 5L + ), + Arrays.asList( + 8L, + 6L, + 9L, + 5L + ), + Arrays.asList( + null, + 6L, + 4L, + 3L + ) + ); + } - @Override - protected List> getInputOrderSets() { - return aggFunctionTestSpec.inputOrderSets; + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("3"), + null, + getValue("10") + ); + } } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + /** + * Test for ByteLastValueWithRetractAggFunction. + */ + public static class ByteLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteLastValueWithRetractAggFunction(); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test for ShortLastValueWithRetractAggFunction. + */ + public static class ShortLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortLastValueWithRetractAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for IntLastValueWithRetractAggFunction. + */ + public static class IntLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntLastValueWithRetractAggFunction(); + } } - @Override - protected Method getRetractFunc() throws NoSuchMethodException { - return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class, Long.class); + /** + * Test for LongLastValueWithRetractAggFunction. + */ + public static class LongLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongLastValueWithRetractAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ByteLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new ShortLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new IntLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new LongLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new FloatLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DoubleLastValueWithRetractAggFunction(), - numberInputOrderSets(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new BooleanLastValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - 6L, - 2L, - 3L - ), - Arrays.asList( - 1L, - 2L, - 3L - ), - Arrays.asList( - 10L, - 2L, - 5L, - 11L, - 3L, - 7L, - 5L - ), - Arrays.asList( - 6L, - 9L, - 5L - ), - Arrays.asList( - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new DecimalLastValueWithRetractAggFunction( - DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 1L, - 5L, - null, - 3L, - 1L, - 5L, - 2L - ), - Arrays.asList( - 6L, - 5L, - null, - 8L, - null - ), - Arrays.asList( - 8L, - 6L - ) - ), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringLastValueWithRetractAggFunction. - */ - new AggFunctionWithOrderTestSpec<>( - new StringLastValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - null, - 3L, - 1L, - 5L, - 10L, - 15L, - 11L - ), - Arrays.asList( - 6L, - 5L - ), - Arrays.asList( - 8L, - 6L - ), - Arrays.asList( - 6L, - 4L, - 3L - ) - ), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz"), - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("abc") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("def"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("x") - ) - ) - ); + /** + * Test for FloatLastValueWithRetractAggFunction. + */ + public static class FloatLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatLastValueWithRetractAggFunction(); + } } - private static List> numberInputOrderSets() { - return Arrays.asList( - Arrays.asList( - 10L, - 2L, - 5L, - 6L, - 11L, - 13L, - 7L, - 5L - ), - Arrays.asList( - 8L, - 6L, - 9L, - 5L - ), - Arrays.asList( - null, - 6L, - 4L, - 3L - ) - ); + /** + * Test for DoubleLastValueWithRetractAggFunction. + */ + public static class DoubleLastValueWithRetractAggFunctionWithOrderTest + extends NumberLastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleLastValueWithRetractAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null, - strToValueFun.apply("3"), - strToValueFun.apply("2"), - strToValueFun.apply("-99") - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("5") - ) - ); + /** + * Test for BooleanLastValueWithRetractAggFunction. + */ + public static class BooleanLastValueWithRetractAggFunctionWithOrderTest + extends LastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 6L, + 2L, + 3L + ), + Arrays.asList( + 1L, + 2L, + 3L + ), + Arrays.asList( + 10L, + 2L, + 5L, + 11L, + 3L, + 7L, + 5L + ), + Arrays.asList( + 6L, + 9L, + 5L + ), + Arrays.asList( + 4L, + 3L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanLastValueWithRetractAggFunction(); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("3"), - null, - strToValueFun.apply("10") - ); + /** + * Test for DecimalLastValueWithRetractAggFunction. + */ + public static class DecimalLastValueWithRetractAggFunctionWithOrderTest + extends LastValueWithRetractAggFunctionWithOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 1L, + 5L, + null, + 3L, + 1L, + 5L, + 2L + ), + Arrays.asList( + 6L, + 5L, + null, + 8L, + null + ), + Arrays.asList( + 8L, + 6L + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("1", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalLastValueWithRetractAggFunction(DecimalTypeInfo.of(precision, scale)); + } } -} + /** + * Test for StringLastValueWithRetractAggFunction. + */ + public static class StringLastValueWithRetractAggFunctionWithOrderTest + extends LastValueWithRetractAggFunctionWithOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz"), + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("abc") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List> getInputOrderSets() { + return Arrays.asList( + Arrays.asList( + 10L, + 2L, + 5L, + null, + 3L, + 1L, + 5L, + 10L, + 15L, + 11L + ), + Arrays.asList( + 6L, + 5L + ), + Arrays.asList( + 8L, + 6L + ), + Arrays.asList( + 6L, + 4L, + 3L + ) + ); + } + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("def"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("x") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringLastValueWithRetractAggFunction(); + } + } +} diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithoutOrderTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithoutOrderTest.java index 774f82e005144..801a4ed04b60b 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithoutOrderTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/LastValueWithRetractAggFunctionWithoutOrderTest.java @@ -33,251 +33,336 @@ import org.apache.flink.table.planner.functions.aggfunctions.LastValueWithRetractAggFunction.StringLastValueWithRetractAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in LastValue with retract aggregate function. * This class tests `accumulate` method without order argument. */ -@RunWith(Parameterized.class) -public class LastValueWithRetractAggFunctionWithoutOrderTest extends AggFunctionTestBase { +@RunWith(Enclosed.class) +public class LastValueWithRetractAggFunctionWithoutOrderTest { - @Parameterized.Parameter - public AggFunctionTestSpec aggFunctionTestSpec; + /** + * The base test class for LastValueWithRetractAggFunction without order. + */ + public abstract static class LastValueWithRetractAggFunctionWithoutOrderTestBase + extends AggFunctionTestBase { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Class getAccClass() { + return GenericRow.class; + } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + @Override + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test LastValueWithRetractAggFunction for number type. + */ + public abstract static class NumberLastValueWithRetractAggFunctionWithoutOrderTestBase + extends LastValueWithRetractAggFunctionWithoutOrderTestBase { + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getValue("-99"), + getValue("3"), + null + ), + Arrays.asList( + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10"), + null, + getValue("3") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getValue("3"), + null, + getValue("3") + ); + } + } + + /** + * Test for ByteLastValueWithRetractAggFunction. + */ + public static class ByteLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ByteLastValueWithRetractAggFunction(); + } } - @Override - protected AggregateFunction getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for ShortLastValueWithRetractAggFunction. + */ + public static class ShortLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new ShortLastValueWithRetractAggFunction(); + } + } + + /** + * Test for IntLastValueWithRetractAggFunction. + */ + public static class IntLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new IntLastValueWithRetractAggFunction(); + } + } + + /** + * Test for LongLastValueWithRetractAggFunction. + */ + public static class LongLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new LongLastValueWithRetractAggFunction(); + } } - @Override - protected Class getAccClass() { - return GenericRow.class; + /** + * Test for FloatLastValueWithRetractAggFunction. + */ + public static class FloatLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new FloatLastValueWithRetractAggFunction(); + } } - @Override - protected Method getRetractFunc() throws NoSuchMethodException { - return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + /** + * Test for DoubleLastValueWithRetractAggFunction. + */ + public static class DoubleLastValueWithRetractAggFunctionWithoutOrderTest + extends NumberLastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction getAggregator() { + return new DoubleLastValueWithRetractAggFunction(); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ByteLastValueWithRetractAggFunction(), - numberInputValueSets(Byte::valueOf), - numberExpectedResults(Byte::valueOf) - ), - /** - * Test for ShortLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ShortLastValueWithRetractAggFunction(), - numberInputValueSets(Short::valueOf), - numberExpectedResults(Short::valueOf) - ), - /** - * Test for IntLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new IntLastValueWithRetractAggFunction(), - numberInputValueSets(Integer::valueOf), - numberExpectedResults(Integer::valueOf) - ), - /** - * Test for LongLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new LongLastValueWithRetractAggFunction(), - numberInputValueSets(Long::valueOf), - numberExpectedResults(Long::valueOf) - ), - /** - * Test for FloatLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new FloatLastValueWithRetractAggFunction(), - numberInputValueSets(Float::valueOf), - numberExpectedResults(Float::valueOf) - ), - /** - * Test for DoubleLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DoubleLastValueWithRetractAggFunction(), - numberInputValueSets(Double::valueOf), - numberExpectedResults(Double::valueOf) - ), - /** - * Test for BooleanLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new BooleanLastValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DecimalLastValueWithRetractAggFunction( - DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringLastValueWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new StringLastValueWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("zzz"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("e") - ) - ) - - ); + /** + * Test for BooleanLastValueWithRetractAggFunction. + */ + public static class BooleanLastValueWithRetractAggFunctionWithoutOrderTest extends + LastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new BooleanLastValueWithRetractAggFunction(); + } } - private static List> numberInputValueSets(Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - null - ), - Arrays.asList( - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10"), - null, - strToValueFun.apply("3") - ) - ); + /** + * Test for DecimalLastValueWithRetractAggFunction. + */ + public static class DecimalLastValueWithRetractAggFunctionWithoutOrderTest extends + LastValueWithRetractAggFunctionWithoutOrderTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("999.999", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new DecimalLastValueWithRetractAggFunction(DecimalTypeInfo.of(precision, scale)); + } } - private static List numberExpectedResults(Function strToValueFun) { - return Arrays.asList( - strToValueFun.apply("3"), - null, - strToValueFun.apply("3") - ); + /** + * Test for StringLastValueWithRetractAggFunction. + */ + public static class StringLastValueWithRetractAggFunctionWithoutOrderTest extends + LastValueWithRetractAggFunctionWithoutOrderTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("zzz"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("e") + ); + } + + @Override + protected AggregateFunction getAggregator() { + return new StringLastValueWithRetractAggFunction(); + } } } diff --git a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/MaxWithRetractAggFunctionTest.java b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/MaxWithRetractAggFunctionTest.java index aa02763164481..0ee6899b886bf 100644 --- a/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/MaxWithRetractAggFunctionTest.java +++ b/flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/functions/aggfunctions/MaxWithRetractAggFunctionTest.java @@ -37,383 +37,585 @@ import org.apache.flink.table.planner.functions.aggfunctions.MaxWithRetractAggFunction.TimestampMaxWithRetractAggFunction; import org.apache.flink.table.runtime.typeutils.DecimalTypeInfo; +import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.lang.reflect.Method; import java.sql.Date; import java.sql.Time; import java.util.Arrays; import java.util.List; -import java.util.function.Function; /** * Test case for built-in Max with retraction aggregate function. */ -@RunWith(Parameterized.class) -public class MaxWithRetractAggFunctionTest extends AggFunctionTestBase> { +@RunWith(Enclosed.class) +public class MaxWithRetractAggFunctionTest { - @Parameterized.Parameter - public AggFunctionTestSpec> aggFunctionTestSpec; + /** + * The base test class for MaxWithRetractAggFunction. + */ + public abstract static class MaxWithRetractAggFunctionTestBase + extends AggFunctionTestBase> { - private static final int DECIMAL_PRECISION = 20; - private static final int DECIMAL_SCALE = 6; + @Override + protected Class getAccClass() { + return MaxWithRetractAccumulator.class; + } - @Override - protected List> getInputValueSets() { - return aggFunctionTestSpec.inputValueSets; + @Override + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + } } - @Override - protected List getExpectedResults() { - return aggFunctionTestSpec.expectedResults; + /** + * Test MaxWithRetractAggFunction for number type. + */ + public abstract static class NumberMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + protected abstract T getMinValue(); + + protected abstract T getMaxValue(); + + protected abstract T getValue(String v); + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + getValue("1"), + null, + getMaxValue(), + getValue("-99"), + getValue("3"), + getValue("56"), + getValue("0"), + getMinValue(), + getValue("-20"), + getValue("17"), + null + ), + Arrays.asList( + null, + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + getValue("10") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + getMaxValue(), + null, + getValue("10") + ); + } + } + + /** + * Test for ByteMaxWithRetractAggFunction. + */ + public static class ByteMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Byte getMinValue() { + return Byte.MIN_VALUE + 1; + } + + @Override + protected Byte getMaxValue() { + return Byte.MAX_VALUE - 1; + } + + @Override + protected Byte getValue(String v) { + return Byte.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new ByteMaxWithRetractAggFunction(); + } + } + + /** + * Test for ShortMaxWithRetractAggFunction. + */ + public static class ShortMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Short getMinValue() { + return Short.MIN_VALUE + 1; + } + + @Override + protected Short getMaxValue() { + return Short.MAX_VALUE - 1; + } + + @Override + protected Short getValue(String v) { + return Short.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new ShortMaxWithRetractAggFunction(); + } + } + + /** + * Test for IntMaxWithRetractAggFunction. + */ + public static class IntMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Integer getMinValue() { + return Integer.MIN_VALUE + 1; + } + + @Override + protected Integer getMaxValue() { + return Integer.MAX_VALUE - 1; + } + + @Override + protected Integer getValue(String v) { + return Integer.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new IntMaxWithRetractAggFunction(); + } + } + + /** + * Test for LongMaxWithRetractAggFunction. + */ + public static class LongMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Long getMinValue() { + return Long.MIN_VALUE + 1; + } + + @Override + protected Long getMaxValue() { + return Long.MAX_VALUE - 1; + } + + @Override + protected Long getValue(String v) { + return Long.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new LongMaxWithRetractAggFunction(); + } + } + + /** + * Test for FloatMaxWithRetractAggFunction. + */ + public static class FloatMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Float getMinValue() { + return -Float.MAX_VALUE / 2; + } + + @Override + protected Float getMaxValue() { + return Float.MAX_VALUE / 2; + } + + @Override + protected Float getValue(String v) { + return Float.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new FloatMaxWithRetractAggFunction(); + } } - @Override - protected AggregateFunction> getAggregator() { - return aggFunctionTestSpec.aggregator; + /** + * Test for DoubleMaxWithRetractAggFunction. + */ + public static class DoubleMaxWithRetractAggFunctionTest extends NumberMaxWithRetractAggFunctionTest { + + @Override + protected Double getMinValue() { + return -Double.MAX_VALUE / 2; + } + + @Override + protected Double getMaxValue() { + return Double.MAX_VALUE / 2; + } + + @Override + protected Double getValue(String v) { + return Double.valueOf(v); + } + + @Override + protected AggregateFunction> getAggregator() { + return new DoubleMaxWithRetractAggFunction(); + } } - @Override - protected Class getAccClass() { - return MaxWithRetractAccumulator.class; + /** + * Test for BooleanMaxWithRetractAggFunction. + */ + public static class BooleanMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + false, + false, + false + ), + Arrays.asList( + true, + true, + true + ), + Arrays.asList( + true, + false, + null, + true, + false, + true, + null + ), + Arrays.asList( + null, + null, + null + ), + Arrays.asList( + null, + true + )); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + false, + true, + true, + null, + true + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new BooleanMaxWithRetractAggFunction(); + } + + @Override + protected Class getAccClass() { + return MaxWithRetractAccumulator.class; + } + + @Override + protected Method getRetractFunc() throws NoSuchMethodException { + return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + } } - @Override - protected Method getRetractFunc() throws NoSuchMethodException { - return getAggregator().getClass().getMethod("retract", getAccClass(), Object.class); + /** + * Test for DecimalMaxWithRetractAggFunction. + */ + public static class DecimalMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + private int precision = 20; + private int scale = 6; + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + Decimal.castFrom("1", precision, scale), + Decimal.castFrom("1000.000001", precision, scale), + Decimal.castFrom("-1", precision, scale), + Decimal.castFrom("-999.998999", precision, scale), + null, + Decimal.castFrom("0", precision, scale), + Decimal.castFrom("-999.999", precision, scale), + null, + Decimal.castFrom("999.999", precision, scale) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + Decimal.castFrom("0", precision, scale) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + Decimal.castFrom("1000.000001", precision, scale), + null, + Decimal.castFrom("0", precision, scale) + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new DecimalMaxWithRetractAggFunction(DecimalTypeInfo.of(precision, scale)); + } } - @Parameterized.Parameters(name = "{index}: {0}") - public static List testData() { - return Arrays.asList( - /** - * Test for ByteMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ByteMaxWithRetractAggFunction(), - numberInputValueSets((byte) (Byte.MIN_VALUE + 1), (byte) (Byte.MAX_VALUE - 1), Byte::valueOf), - numberExpectedResults((byte) (Byte.MAX_VALUE - 1), Byte::valueOf) - ), - /** - * Test for ShortMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new ShortMaxWithRetractAggFunction(), - numberInputValueSets( - (short) (Short.MIN_VALUE + 1), (short) (Short.MAX_VALUE - 1), Short::valueOf), - numberExpectedResults((short) (Short.MAX_VALUE - 1), Short::valueOf) - ), - /** - * Test for IntMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new IntMaxWithRetractAggFunction(), - numberInputValueSets(Integer.MIN_VALUE + 1, Integer.MAX_VALUE - 1, Integer::valueOf), - numberExpectedResults(Integer.MAX_VALUE - 1, Integer::valueOf) - ), - /** - * Test for LongMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new LongMaxWithRetractAggFunction(), - numberInputValueSets(Long.MIN_VALUE + 1L, Long.MAX_VALUE - 1L, Long::valueOf), - numberExpectedResults(Long.MAX_VALUE - 1L, Long::valueOf) - ), - /** - * Test for FloatMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new FloatMaxWithRetractAggFunction(), - numberInputValueSets((-Float.MAX_VALUE / 2), (Float.MAX_VALUE / 2), Float::valueOf), - numberExpectedResults((Float.MAX_VALUE / 2), Float::valueOf) - ), - /** - * Test for DoubleMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DoubleMaxWithRetractAggFunction(), - numberInputValueSets((-Double.MAX_VALUE / 2), (Double.MAX_VALUE / 2), Double::valueOf), - numberExpectedResults((Double.MAX_VALUE / 2), Double::valueOf) - ), - /** - * Test for BooleanMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new BooleanMaxWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - false, - false, - false - ), - Arrays.asList( - true, - true, - true - ), - Arrays.asList( - true, - false, - null, - true, - false, - true, - null - ), - Arrays.asList( - null, - null, - null - ), - Arrays.asList( - null, - true - ) - ), - Arrays.asList( - false, - true, - true, - null, - true - ) - ), - /** - * Test for DecimalMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DecimalMaxWithRetractAggFunction(DecimalTypeInfo.of(DECIMAL_PRECISION, DECIMAL_SCALE)), - Arrays.asList( - Arrays.asList( - Decimal.castFrom("1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-1", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.998999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE), - Decimal.castFrom("-999.999", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("999.999", DECIMAL_PRECISION, DECIMAL_SCALE) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - Arrays.asList( - Decimal.castFrom("1000.000001", DECIMAL_PRECISION, DECIMAL_SCALE), - null, - Decimal.castFrom("0", DECIMAL_PRECISION, DECIMAL_SCALE) - ) - ), - /** - * Test for StringMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new StringMaxWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - BinaryString.fromString("abc"), - BinaryString.fromString("def"), - BinaryString.fromString("ghi"), - null, - BinaryString.fromString("jkl"), - null, - BinaryString.fromString("zzz") - ), - Arrays.asList( - null, - null - ), - Arrays.asList( - null, - BinaryString.fromString("a") - ), - Arrays.asList( - BinaryString.fromString("x"), - null, - BinaryString.fromString("e") - ) - ), - Arrays.asList( - BinaryString.fromString("zzz"), - null, - BinaryString.fromString("a"), - BinaryString.fromString("x") - ) - ), - /** - * Test for TimestampMaxWithRetractAggFunction with millisecond's precision. - */ - new AggFunctionTestSpec<>( - new TimestampMaxWithRetractAggFunction(3), - Arrays.asList( - Arrays.asList( - SqlTimestamp.fromEpochMillis(0), - SqlTimestamp.fromEpochMillis(1000), - SqlTimestamp.fromEpochMillis(100), - null, - SqlTimestamp.fromEpochMillis(10) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - SqlTimestamp.fromEpochMillis(1) - ) - ), - Arrays.asList( - SqlTimestamp.fromEpochMillis(1000), - null, - SqlTimestamp.fromEpochMillis(1) - ) - ), - /** - * Test for TimestampMaxWithRetractAggFunction with nanosecond's precision. - */ - new AggFunctionTestSpec<>( - new TimestampMaxWithRetractAggFunction(9), - Arrays.asList( - Arrays.asList( - SqlTimestamp.fromEpochMillis(0, 0), - SqlTimestamp.fromEpochMillis(1000, 0), - SqlTimestamp.fromEpochMillis(1000, 1), - SqlTimestamp.fromEpochMillis(100, 0), - null, - SqlTimestamp.fromEpochMillis(10, 0) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - SqlTimestamp.fromEpochMillis(1, 0), - SqlTimestamp.fromEpochMillis(1, 1) - ) - ), - Arrays.asList( - SqlTimestamp.fromEpochMillis(1000, 1), - null, - SqlTimestamp.fromEpochMillis(1, 1) - ) - ), - /** - * Test for DateMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new DateMaxWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - new Date(0), - new Date(1000), - new Date(100), - null, - new Date(10) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - new Date(1) - ) - ), - Arrays.asList( - new Date(1000), - null, - new Date(1) - ) - ), - /** - * Test for TimeMaxWithRetractAggFunction. - */ - new AggFunctionTestSpec<>( - new TimeMaxWithRetractAggFunction(), - Arrays.asList( - Arrays.asList( - new Time(0), - new Time(1000), - new Time(100), - null, - new Time(10) - ), - Arrays.asList( - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - new Time(1) - ) - ), - Arrays.asList( - new Time(1000), - null, - new Time(1) - ) - ) - ); + /** + * Test for StringMaxWithRetractAggFunction. + */ + public static class StringMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + BinaryString.fromString("abc"), + BinaryString.fromString("def"), + BinaryString.fromString("ghi"), + null, + BinaryString.fromString("jkl"), + null, + BinaryString.fromString("zzz") + ), + Arrays.asList( + null, + null + ), + Arrays.asList( + null, + BinaryString.fromString("a") + ), + Arrays.asList( + BinaryString.fromString("x"), + null, + BinaryString.fromString("e") + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + BinaryString.fromString("zzz"), + null, + BinaryString.fromString("a"), + BinaryString.fromString("x") + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new StringMaxWithRetractAggFunction(); + } } - private static List> numberInputValueSets(N minValue, N maxValue, Function strToValueFun) { - return Arrays.asList( - Arrays.asList( - strToValueFun.apply("1"), - null, - maxValue, - strToValueFun.apply("-99"), - strToValueFun.apply("3"), - strToValueFun.apply("56"), - strToValueFun.apply("0"), - minValue, - strToValueFun.apply("-20"), - strToValueFun.apply("17"), - null - ), - Arrays.asList( - null, - null, - null, - null, - null, - null - ), - Arrays.asList( - null, - strToValueFun.apply("10") - ) - ); + /** + * Test for TimestampMaxWithRetractAggFunction. + */ + public static class TimestampMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + SqlTimestamp.fromEpochMillis(0), + SqlTimestamp.fromEpochMillis(1000), + SqlTimestamp.fromEpochMillis(100), + null, + SqlTimestamp.fromEpochMillis(10) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + SqlTimestamp.fromEpochMillis(1) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + SqlTimestamp.fromEpochMillis(1000), + null, + SqlTimestamp.fromEpochMillis(1) + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new TimestampMaxWithRetractAggFunction(3); + } + } + + /** + * Test for TimestampMaxWithRetractAggFunction, precision is 9. + */ + public static class Timestamp9MaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + SqlTimestamp.fromEpochMillis(0, 0), + SqlTimestamp.fromEpochMillis(1000, 0), + SqlTimestamp.fromEpochMillis(1000, 1), + SqlTimestamp.fromEpochMillis(100, 0), + null, + SqlTimestamp.fromEpochMillis(10, 0) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + SqlTimestamp.fromEpochMillis(1, 0), + SqlTimestamp.fromEpochMillis(1, 1) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + SqlTimestamp.fromEpochMillis(1000, 1), + null, + SqlTimestamp.fromEpochMillis(1, 1) + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new TimestampMaxWithRetractAggFunction(9); + } } - private static List numberExpectedResults(N maxValue, Function strToValueFun) { - return Arrays.asList( - maxValue, - null, - strToValueFun.apply("10") - ); + /** + * Test for DateMaxWithRetractAggFunction. + */ + public static class DateMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase { + + @Override + protected List> getInputValueSets() { + return Arrays.asList( + Arrays.asList( + new Date(0), + new Date(1000), + new Date(100), + null, + new Date(10) + ), + Arrays.asList( + null, + null, + null, + null, + null + ), + Arrays.asList( + null, + new Date(1) + ) + ); + } + + @Override + protected List getExpectedResults() { + return Arrays.asList( + new Date(1000), + null, + new Date(1) + ); + } + + @Override + protected AggregateFunction> getAggregator() { + return new DateMaxWithRetractAggFunction(); + } + } + + /** + * Test for TimeMaxWithRetractAggFunction. + */ + public static class TimeMaxWithRetractAggFunctionTest extends MaxWithRetractAggFunctionTestBase