You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2023/12/28 10:45:45 UTC
(incubator-paimon) branch master updated: [core] Introduce collect agg (#2593)
This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 05aa84443 [core] Introduce collect agg (#2593)
05aa84443 is described below
commit 05aa84443f984b222e536d03302a74fa877d4cda
Author: yuzelin <33...@users.noreply.github.com>
AuthorDate: Thu Dec 28 18:45:41 2023 +0800
[core] Introduce collect agg (#2593)
---
docs/content/concepts/primary-key-table.md | 4 +
.../main/java/org/apache/paimon/CoreOptions.java | 9 +++
.../compact/aggregate/FieldAggregator.java | 25 +++++-
.../compact/aggregate/FieldCollectAgg.java | 78 +++++++++++++++++++
.../compact/aggregate/FieldAggregatorTest.java | 79 +++++++++++++++----
.../apache/paimon/flink/PreAggregationITCase.java | 89 ++++++++++++++++++++++
6 files changed, 269 insertions(+), 15 deletions(-)
diff --git a/docs/content/concepts/primary-key-table.md b/docs/content/concepts/primary-key-table.md
index 288c7a865..b80beee5d 100644
--- a/docs/content/concepts/primary-key-table.md
+++ b/docs/content/concepts/primary-key-table.md
@@ -395,6 +395,10 @@ Current supported aggregate functions and data types are:
{{< /tabs >}}
+* `collect`:
+ The collect function collects elements into an Array. By default, it will deduplicate elements. You can set
+ `fields.<field-name>.distinct=false` to allow collect duplicate elements.
+
Only `sum` and `product` supports retraction (`UPDATE_BEFORE` and `DELETE`), others aggregate functions do not support retraction.
If you allow some functions to ignore retraction messages, you can configure:
`'fields.${field_name}.ignore-retract'='true'`.
diff --git a/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java b/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
index d09cd3207..0d2b55ec4 100644
--- a/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
+++ b/paimon-common/src/main/java/org/apache/paimon/CoreOptions.java
@@ -66,6 +66,8 @@ public class CoreOptions implements Serializable {
public static final String NESTED_KEY = "nested-key";
+ public static final String DISTINCT = "distinct";
+
public static final ConfigOption<Integer> BUCKET =
key("bucket")
.intType()
@@ -1123,6 +1125,13 @@ public class CoreOptions implements Serializable {
return Arrays.asList(keyString.split(","));
}
+ public boolean fieldCollectAggDistinct(String fieldName) {
+ return options.get(
+ key(FIELDS_PREFIX + "." + fieldName + "." + DISTINCT)
+ .booleanType()
+ .defaultValue(true));
+ }
+
public String fileCompression() {
return options.get(FILE_COMPRESSION);
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregator.java b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregator.java
index 6012733e1..28c0622d4 100644
--- a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregator.java
+++ b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregator.java
@@ -21,6 +21,7 @@ package org.apache.paimon.mergetree.compact.aggregate;
import org.apache.paimon.CoreOptions;
import org.apache.paimon.types.ArrayType;
import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypeFamily;
import org.apache.paimon.types.RowType;
import javax.annotation.Nullable;
@@ -97,6 +98,11 @@ public abstract class FieldAggregator implements Serializable {
createFieldNestedUpdateAgg(
fieldType, options.fieldNestedUpdateAggNestedKey(field));
break;
+ case FieldCollectAgg.NAME:
+ fieldAggregator =
+ createFieldCollectAgg(
+ fieldType, options.fieldCollectAggDistinct(field));
+ break;
default:
throw new RuntimeException(
String.format(
@@ -113,7 +119,7 @@ public abstract class FieldAggregator implements Serializable {
return fieldAggregator;
}
- public static FieldAggregator createFieldNestedUpdateAgg(
+ private static FieldAggregator createFieldNestedUpdateAgg(
DataType fieldType, List<String> nestedKey) {
if (nestedKey == null) {
nestedKey = Collections.emptyList();
@@ -127,6 +133,23 @@ public abstract class FieldAggregator implements Serializable {
return new FieldNestedUpdateAgg(arrayType, nestedKey);
}
+ private static FieldAggregator createFieldCollectAgg(DataType fieldType, boolean distinct) {
+ checkArgument(
+ fieldType instanceof ArrayType,
+ "Data type for collect column must be 'Array' but was '%s'.",
+ fieldType);
+ ArrayType arrayType = (ArrayType) fieldType;
+ checkArgument(
+ !arrayType
+ .getElementType()
+ .getTypeRoot()
+ .getFamilies()
+ .contains(DataTypeFamily.CONSTRUCTED),
+ "Element type of collect column cannot be ARRAY, MULTISET, MAP and ROW.");
+
+ return new FieldCollectAgg(arrayType, distinct);
+ }
+
abstract String name();
public abstract Object agg(Object accumulator, Object inputField);
diff --git a/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldCollectAgg.java b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldCollectAgg.java
new file mode 100644
index 000000000..d8ba13fe7
--- /dev/null
+++ b/paimon-core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldCollectAgg.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.mergetree.compact.aggregate;
+
+import org.apache.paimon.data.GenericArray;
+import org.apache.paimon.data.InternalArray;
+import org.apache.paimon.types.ArrayType;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+
+/** Collect elements into an ARRAY. */
+public class FieldCollectAgg extends FieldAggregator {
+
+ public static final String NAME = "collect";
+
+ private final boolean distinct;
+ private final InternalArray.ElementGetter elementGetter;
+
+ public FieldCollectAgg(ArrayType dataType, boolean distinct) {
+ super(dataType);
+ this.distinct = distinct;
+ this.elementGetter = InternalArray.createElementGetter(dataType.getElementType());
+ }
+
+ @Override
+ String name() {
+ return NAME;
+ }
+
+ @Override
+ public Object agg(Object accumulator, Object inputField) {
+ if (accumulator == null && inputField == null) {
+ return null;
+ }
+
+ if ((accumulator == null || inputField == null) && !distinct) {
+ return accumulator == null ? inputField : accumulator;
+ }
+
+ Collection<Object> collection = distinct ? new HashSet<>() : new ArrayList<>();
+
+ collect(collection, accumulator);
+ collect(collection, inputField);
+
+ return new GenericArray(collection.toArray());
+ }
+
+ private void collect(Collection<Object> collection, @Nullable Object data) {
+ if (data == null) {
+ return;
+ }
+
+ InternalArray array = (InternalArray) data;
+ for (int i = 0; i < array.size(); i++) {
+ collection.add(elementGetter.getElementOrNull(array, i));
+ }
+ }
+}
diff --git a/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java b/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
index a98c3b3e6..0c733eb60 100644
--- a/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/mergetree/compact/aggregate/FieldAggregatorTest.java
@@ -26,6 +26,7 @@ import org.apache.paimon.data.InternalArray;
import org.apache.paimon.data.InternalRow;
import org.apache.paimon.types.BigIntType;
import org.apache.paimon.types.BooleanType;
+import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DataTypes;
import org.apache.paimon.types.DecimalType;
import org.apache.paimon.types.DoubleType;
@@ -287,6 +288,11 @@ public class FieldAggregatorTest {
@Test
public void testFieldNestedUpdateAgg() {
+ DataType elementRowType =
+ DataTypes.ROW(
+ DataTypes.FIELD(0, "k0", DataTypes.INT()),
+ DataTypes.FIELD(1, "k1", DataTypes.INT()),
+ DataTypes.FIELD(2, "v", DataTypes.STRING()));
FieldNestedUpdateAgg agg =
new FieldNestedUpdateAgg(
DataTypes.ARRAY(
@@ -297,50 +303,53 @@ public class FieldAggregatorTest {
Arrays.asList("k0", "k1"));
InternalArray accumulator;
+ InternalArray.ElementGetter elementGetter =
+ InternalArray.createElementGetter(elementRowType);
InternalRow current = row(0, 0, "A");
accumulator = (InternalArray) agg.agg(null, singletonArray(current));
- assertThat(unnest(accumulator))
+ assertThat(unnest(accumulator, elementGetter))
.containsExactlyInAnyOrderElementsOf(Collections.singletonList(current));
current = row(0, 1, "B");
accumulator = (InternalArray) agg.agg(accumulator, singletonArray(current));
- assertThat(unnest(accumulator))
+ assertThat(unnest(accumulator, elementGetter))
.containsExactlyInAnyOrderElementsOf(Arrays.asList(row(0, 0, "A"), row(0, 1, "B")));
current = row(0, 1, "b");
accumulator = (InternalArray) agg.agg(accumulator, singletonArray(current));
- assertThat(unnest(accumulator))
+ assertThat(unnest(accumulator, elementGetter))
.containsExactlyInAnyOrderElementsOf(Arrays.asList(row(0, 0, "A"), row(0, 1, "b")));
}
@Test
public void testFieldNestedAppendAgg() {
+ DataType elementRowType =
+ DataTypes.ROW(
+ DataTypes.FIELD(0, "k0", DataTypes.INT()),
+ DataTypes.FIELD(1, "k1", DataTypes.INT()),
+ DataTypes.FIELD(2, "v", DataTypes.STRING()));
FieldNestedUpdateAgg agg =
- new FieldNestedUpdateAgg(
- DataTypes.ARRAY(
- DataTypes.ROW(
- DataTypes.FIELD(0, "k0", DataTypes.INT()),
- DataTypes.FIELD(1, "k1", DataTypes.INT()),
- DataTypes.FIELD(2, "v", DataTypes.STRING()))),
- Collections.emptyList());
+ new FieldNestedUpdateAgg(DataTypes.ARRAY(elementRowType), Collections.emptyList());
InternalArray accumulator = null;
+ InternalArray.ElementGetter elementGetter =
+ InternalArray.createElementGetter(elementRowType);
InternalRow current = row(0, 1, "B");
accumulator = (InternalArray) agg.agg(accumulator, singletonArray(current));
- assertThat(unnest(accumulator))
+ assertThat(unnest(accumulator, elementGetter))
.containsExactlyInAnyOrderElementsOf(Collections.singletonList(row(0, 1, "B")));
current = row(0, 1, "b");
accumulator = (InternalArray) agg.agg(accumulator, singletonArray(current));
- assertThat(unnest(accumulator))
+ assertThat(unnest(accumulator, elementGetter))
.containsExactlyInAnyOrderElementsOf(Arrays.asList(row(0, 1, "B"), row(0, 1, "b")));
}
- private List<InternalRow> unnest(InternalArray array) {
+ private List<Object> unnest(InternalArray array, InternalArray.ElementGetter elementGetter) {
return IntStream.range(0, array.size())
- .mapToObj(i -> array.getRow(i, 3))
+ .mapToObj(i -> elementGetter.getElementOrNull(array, i))
.collect(Collectors.toList());
}
@@ -351,4 +360,46 @@ public class FieldAggregatorTest {
private InternalRow row(int k0, int k1, String v) {
return GenericRow.of(k0, k1, BinaryString.fromString(v));
}
+
+ @Test
+ public void testFieldCollectAggWithDistinct() {
+ FieldCollectAgg agg = new FieldCollectAgg(DataTypes.ARRAY(DataTypes.INT()), true);
+
+ InternalArray result;
+ InternalArray.ElementGetter elementGetter =
+ InternalArray.createElementGetter(DataTypes.INT());
+
+ assertThat(agg.agg(null, null)).isNull();
+
+ result = (InternalArray) agg.agg(null, new GenericArray(new int[] {1, 1, 2}));
+ assertThat(unnest(result, elementGetter)).containsExactlyInAnyOrder(1, 2);
+
+ result =
+ (InternalArray)
+ agg.agg(
+ new GenericArray(new int[] {1, 1, 2}),
+ new GenericArray(new int[] {2, 3}));
+ assertThat(unnest(result, elementGetter)).containsExactlyInAnyOrder(1, 2, 3);
+ }
+
+ @Test
+ public void testFieldCollectAggWithoutDistinct() {
+ FieldCollectAgg agg = new FieldCollectAgg(DataTypes.ARRAY(DataTypes.INT()), false);
+
+ InternalArray result;
+ InternalArray.ElementGetter elementGetter =
+ InternalArray.createElementGetter(DataTypes.INT());
+
+ assertThat(agg.agg(null, null)).isNull();
+
+ result = (InternalArray) agg.agg(null, new GenericArray(new int[] {1, 1, 2}));
+ assertThat(unnest(result, elementGetter)).containsExactlyInAnyOrder(1, 1, 2);
+
+ result =
+ (InternalArray)
+ agg.agg(
+ new GenericArray(new int[] {1, 1, 2}),
+ new GenericArray(new int[] {2, 3}));
+ assertThat(unnest(result, elementGetter)).containsExactlyInAnyOrder(1, 1, 2, 2, 3);
+ }
}
diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PreAggregationITCase.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PreAggregationITCase.java
index 63670eb7c..4a7dfd3ee 100644
--- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PreAggregationITCase.java
+++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PreAggregationITCase.java
@@ -18,6 +18,7 @@
package org.apache.paimon.flink;
+import org.apache.paimon.mergetree.compact.aggregate.FieldCollectAgg;
import org.apache.paimon.mergetree.compact.aggregate.FieldNestedUpdateAgg;
import org.apache.paimon.utils.BlockingIterator;
@@ -1243,4 +1244,92 @@ public class PreAggregationITCase {
return true;
}
}
+
+ /** ITCase for {@link FieldCollectAgg}. */
+ public static class CollectAggregationITCase extends CatalogITCaseBase {
+
+ @Test
+ public void testAggWithDistinct() {
+ sql(
+ "CREATE TABLE test_collect("
+ + " id INT PRIMARY KEY NOT ENFORCED,"
+ + " f0 ARRAY<STRING>"
+ + ") WITH ("
+ + " 'merge-engine' = 'aggregation',"
+ + " 'fields.f0.aggregate-function' = 'collect'"
+ + ")");
+
+ sql(
+ "INSERT INTO test_collect VALUES "
+ + "(1, CAST (NULL AS ARRAY<STRING>)), "
+ + "(2, ARRAY['A', 'B']), "
+ + "(3, ARRAY['car', 'watch'])");
+
+ List<Row> result = queryAndSort("SELECT * FROM test_collect");
+ checkOneRecord(result.get(0), 1);
+ checkOneRecord(result.get(1), 2, "A", "B");
+ checkOneRecord(result.get(2), 3, "car", "watch");
+
+ sql(
+ "INSERT INTO test_collect VALUES "
+ + "(1, ARRAY['paimon', 'paimon']), "
+ + "(2, ARRAY['A', 'B', 'C']), "
+ + "(3, CAST (NULL AS ARRAY<STRING>))");
+
+ result = queryAndSort("SELECT * FROM test_collect");
+ checkOneRecord(result.get(0), 1, "paimon");
+ checkOneRecord(result.get(1), 2, "A", "B", "C");
+ checkOneRecord(result.get(2), 3, "car", "watch");
+ }
+
+ @Test
+ public void testAggWithoutDistinct() {
+ sql(
+ "CREATE TABLE test_collect("
+ + " id INT PRIMARY KEY NOT ENFORCED,"
+ + " f0 ARRAY<STRING>"
+ + ") WITH ("
+ + " 'merge-engine' = 'aggregation',"
+ + " 'fields.f0.aggregate-function' = 'collect',"
+ + " 'fields.f0.distinct' = 'false'"
+ + ")");
+
+ sql(
+ "INSERT INTO test_collect VALUES "
+ + "(1, CAST (NULL AS ARRAY<STRING>)), "
+ + "(2, ARRAY['A', 'B', 'B']), "
+ + "(3, ARRAY['car', 'watch'])");
+
+ List<Row> result = queryAndSort("SELECT * FROM test_collect");
+ checkOneRecord(result.get(0), 1);
+ checkOneRecord(result.get(1), 2, "A", "B", "B");
+ checkOneRecord(result.get(2), 3, "car", "watch");
+
+ sql(
+ "INSERT INTO test_collect VALUES "
+ + "(1, ARRAY['paimon', 'paimon']), "
+ + "(2, ARRAY['A', 'B', 'C']), "
+ + "(3, CAST (NULL AS ARRAY<STRING>))");
+
+ result = queryAndSort("SELECT * FROM test_collect");
+ checkOneRecord(result.get(0), 1, "paimon", "paimon");
+ checkOneRecord(result.get(1), 2, "A", "A", "B", "B", "B", "C");
+ checkOneRecord(result.get(2), 3, "car", "watch");
+ }
+
+ private List<Row> queryAndSort(String sql) {
+ return sql(sql).stream()
+ .sorted(Comparator.comparingInt(r -> r.getFieldAs(0)))
+ .collect(Collectors.toList());
+ }
+
+ private void checkOneRecord(Row row, int id, String... elements) {
+ assertThat(row.getField(0)).isEqualTo(id);
+ if (elements == null || elements.length == 0) {
+ assertThat(row.getField(1)).isNull();
+ } else {
+ assertThat((String[]) row.getField(1)).containsExactlyInAnyOrder(elements);
+ }
+ }
+ }
}