You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "Jiabao-Sun (via GitHub)" <gi...@apache.org> on 2023/09/13 12:58:36 UTC

[GitHub] [flink] Jiabao-Sun opened a new pull request, #23411: [FLINK-21949][table] Support ARRAY_AGG aggregate function

Jiabao-Sun opened a new pull request, #23411:
URL: https://github.com/apache/flink/pull/23411

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   [FLINK-21949][table] Support ARRAY_AGG aggregate function
   
   Some nosql databases like mongodb and elasticsearch support nested data types.
   Aggregating multiple rows into ARRAY<ROW> is a common requirement.
   
   ## Brief change log
   
   Introduce built in function `ARRAY_AGG([ ALL | DISTINCT ] expression)` to return an array that concatenates the input rows
   and returns NULL if there are no input rows. NULL values will be ignored. Use DISTINCT for one unique instance of each value.
   
   ```sql
   SELECT ARRAY_AGG(f1)
     FROM tmp
    GROUP BY f0
   ```
   
   ![image](https://github.com/apache/flink/assets/27403841/4ba953d0-92bf-485f-afc2-3fc292fc81ce)
   
   Note that we have made some simplifications based on Calcite's `SqlLibraryOperators.ARRAY_AGG`.
   ```sql
   -- calcite
   ARRAY_AGG([ ALL | DISTINCT ] value [ RESPECT NULLS | IGNORE NULLS ] [ ORDER BY orderItem [, orderItem ]* ] )
   -- flink
   ARRAY_AGG([ ALL | DISTINCT ] expression)
   ```
   
   **The differences from Calcite are as follows:**
     1. **Null values are ignored.**
     2. **The order by expression within the function is not supported because the complete row record cannot be accessed within the function implementation.**
     3. **The function returns null when there's no input rows, but calcite definition returns an empty array. The behavior was referenced from BigQuery and Postgres.**
   
   - https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions#array_agg
   - https://www.postgresql.org/docs/8.4/functions-aggregate.html
   
   
   ## Verifying this change
   ITCase and UnitCase are added.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes)
     - The serializers: (no )
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes)
     - If yes, how is the feature documented? (docs)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1898595368

   @flinkbot run azure


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1897966163

   @flinkbot run azure


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1754523455

   Hi @wuchong, could you help review this when you have time?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1897393850

   Thanks for addressing comments 
   in general it looks ok from my side
   i guess there is one little thing: since it is based on Calcite parser it allows to have `ORDER BY` inside...
   At the same time it is currently not supported on Flink level, not sure whether we can redefine this behavior however at least it would make sense to mention it in doc that it is not supported 
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [flink] flinkbot commented on pull request #23411: [FLINK-21949][table] Support ARRAY_AGG aggregate function

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1717597767

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "10081ad9bdba84b3dac22fb7a6137994cc79622b",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "10081ad9bdba84b3dac22fb7a6137994cc79622b",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 10081ad9bdba84b3dac22fb7a6137994cc79622b UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453223169


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   Thanks @snuyanzin, I'm trying to support `RESPECT NULLS | IGNORE NULLS` syntax.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1892868178

   thanks a lot for the explanation @dawidwys you're right


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452891261


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlAggFunction.java:
##########
@@ -63,6 +64,7 @@ public final class BridgingSqlAggFunction extends SqlAggFunction {
     private final List<RelDataType> paramTypes;
 
     private BridgingSqlAggFunction(
+            FlinkContext context,

Review Comment:
   Yes, I'll revert that changes.
   Thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1892398690

   https://github.com/apache/flink/pull/23411#pullrequestreview-1770150649
   
   It took me a while to understand that myself, but I think it's actually ok to depend on the `Object#equals/hashcode` in `AggregateFunctions` @snuyanzin what you said is correct for `ArrayDistinctFunction`, because:
   1. it is a scalar function, so it can be chained with other functions without an exchange before it
   2. It gets data from two input(s) (previous operator and e.g. a literal) haystack and needle may come from different operators which may produce data in different formats e.g. `GenericRowData` and `BinaryRowData`
   
   In `AggregateFunctions` we will always have all records as `BinaryRowData` (and alike) and those `equals/hashcode` should work just fine. (It may be a different story when we support `STRUCTURED_TYPE#equals/hashcode`, but we will need to revisit most of the operators then, because all `MapView(s)` will work incorrectly). This is what we do in other aggregate functions already. Take a look at e.g. `JsonArrayAggFunction` or `FirstValueWithRetractAggFunction `
   
   I haven't seen the previous version @Jiabao-Sun , but I believe we can remove the `equalityHandler` and make the `ArrayAggFunction` look similar to `JsonArrayAggFunction`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1924010094

   Sorry, it takes such a long time from my side. I had a vacation in the meantime. I'll try to check it Monday. Nevertheless if you're comfortable with the PR @snuyanzin feel free to merge it without waiting for my review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1477969162


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   The downside of the solution is that the array must fit into memory at all times. The difference with the `LagAggFunction` is that `LAG` keeps at most `n` elements where `n` is controlled by the user.
   
   Still I am reasonably good with the `LinkedList` approach because it anyhow needs to fit into memory when we emit it at the end as a single record. Writing this down for awareness.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/ArrayAggFunction.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.flink.table.runtime.functions.aggregate;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.runtime.typeutils.InternalSerializers;
+import org.apache.flink.table.runtime.typeutils.LinkedListSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in ARRAY_AGG aggregate function. */
+@Internal
+public final class ArrayAggFunction<T>
+        extends BuiltInAggregateFunction<ArrayData, ArrayAggFunction.ArrayAggAccumulator<T>> {
+
+    private static final long serialVersionUID = -5860934997657147836L;
+
+    private final transient DataType elementDataType;
+
+    private final boolean ignoreNulls;
+
+    public ArrayAggFunction(LogicalType elementType, boolean ignoreNulls) {
+        this.elementDataType = toInternalDataType(elementType);
+        this.ignoreNulls = ignoreNulls;
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Planning
+    // --------------------------------------------------------------------------------------------
+
+    @Override
+    public List<DataType> getArgumentDataTypes() {
+        return Collections.singletonList(elementDataType);
+    }
+
+    @Override
+    public DataType getAccumulatorDataType() {
+        DataType linkedListType = getLinkedListType();
+        return DataTypes.STRUCTURED(
+                ArrayAggAccumulator.class,
+                DataTypes.FIELD("list", linkedListType),
+                DataTypes.FIELD("retractList", linkedListType));
+    }
+
+    @Override
+    public DataType getOutputDataType() {
+        return DataTypes.ARRAY(elementDataType).bridgedTo(ArrayData.class);
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private DataType getLinkedListType() {
+        TypeSerializer<T> serializer = InternalSerializers.create(elementDataType.getLogicalType());
+        return DataTypes.RAW(
+                LinkedList.class, (TypeSerializer) new LinkedListSerializer<>(serializer));
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Runtime
+    // --------------------------------------------------------------------------------------------
+
+    /** Accumulator for ARRAY_AGG with retraction. */
+    public static class ArrayAggAccumulator<T> {
+        public LinkedList<T> list;
+        public LinkedList<T> retractList;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            ArrayAggAccumulator<?> that = (ArrayAggAccumulator<?>) o;
+            return Objects.equals(list, that.list) && Objects.equals(retractList, that.retractList);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(list, retractList);
+        }
+    }
+
+    @Override
+    public ArrayAggAccumulator<T> createAccumulator() {
+        final ArrayAggAccumulator<T> acc = new ArrayAggAccumulator<>();
+        acc.list = new LinkedList<>();
+        acc.retractList = new LinkedList<>();
+        return acc;
+    }
+
+    public void accumulate(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value == null) {
+            if (!ignoreNulls) {
+                acc.list.add(null);
+            }
+        } else {
+            acc.list.add(value);
+        }
+    }
+
+    public void retract(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value != null) {

Review Comment:
   would be super nice to have a test case for that



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/ArrayAggFunction.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.flink.table.runtime.functions.aggregate;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.runtime.typeutils.InternalSerializers;
+import org.apache.flink.table.runtime.typeutils.LinkedListSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in ARRAY_AGG aggregate function. */
+@Internal
+public final class ArrayAggFunction<T>
+        extends BuiltInAggregateFunction<ArrayData, ArrayAggFunction.ArrayAggAccumulator<T>> {
+
+    private static final long serialVersionUID = -5860934997657147836L;
+
+    private final transient DataType elementDataType;
+
+    private final boolean ignoreNulls;
+
+    public ArrayAggFunction(LogicalType elementType, boolean ignoreNulls) {
+        this.elementDataType = toInternalDataType(elementType);
+        this.ignoreNulls = ignoreNulls;
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Planning
+    // --------------------------------------------------------------------------------------------
+
+    @Override
+    public List<DataType> getArgumentDataTypes() {
+        return Collections.singletonList(elementDataType);
+    }
+
+    @Override
+    public DataType getAccumulatorDataType() {
+        DataType linkedListType = getLinkedListType();
+        return DataTypes.STRUCTURED(
+                ArrayAggAccumulator.class,
+                DataTypes.FIELD("list", linkedListType),
+                DataTypes.FIELD("retractList", linkedListType));
+    }
+
+    @Override
+    public DataType getOutputDataType() {
+        return DataTypes.ARRAY(elementDataType).bridgedTo(ArrayData.class);
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private DataType getLinkedListType() {
+        TypeSerializer<T> serializer = InternalSerializers.create(elementDataType.getLogicalType());
+        return DataTypes.RAW(
+                LinkedList.class, (TypeSerializer) new LinkedListSerializer<>(serializer));
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Runtime
+    // --------------------------------------------------------------------------------------------
+
+    /** Accumulator for ARRAY_AGG with retraction. */
+    public static class ArrayAggAccumulator<T> {
+        public LinkedList<T> list;
+        public LinkedList<T> retractList;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            ArrayAggAccumulator<?> that = (ArrayAggAccumulator<?>) o;
+            return Objects.equals(list, that.list) && Objects.equals(retractList, that.retractList);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(list, retractList);
+        }
+    }
+
+    @Override
+    public ArrayAggAccumulator<T> createAccumulator() {
+        final ArrayAggAccumulator<T> acc = new ArrayAggAccumulator<>();
+        acc.list = new LinkedList<>();
+        acc.retractList = new LinkedList<>();
+        return acc;
+    }
+
+    public void accumulate(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value == null) {
+            if (!ignoreNulls) {
+                acc.list.add(null);
+            }
+        } else {
+            acc.list.add(value);
+        }
+    }
+
+    public void retract(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value != null) {
+            if (!acc.list.remove(value)) {
+                acc.retractList.add(value);
+            }
+        }
+    }
+
+    public void merge(ArrayAggAccumulator<T> acc, Iterable<ArrayAggAccumulator<T>> its)
+            throws Exception {
+        for (ArrayAggAccumulator<T> otherAcc : its) {
+            // merge list of acc and other
+            List<T> buffer = new ArrayList<>();
+            for (T element : acc.list) {
+                buffer.add(element);
+            }
+            for (T element : otherAcc.list) {
+                buffer.add(element);
+            }
+            // merge retract list of acc and other
+            List<T> retractBuffer = new ArrayList<>();
+            for (T element : acc.retractList) {
+                retractBuffer.add(element);
+            }
+            for (T element : otherAcc.retractList) {
+                retractBuffer.add(element);
+            }
+
+            // merge list & retract list
+            List<T> newRetractBuffer = new ArrayList<>();
+            for (T element : retractBuffer) {
+                if (!buffer.remove(element)) {
+                    newRetractBuffer.add(element);
+                }
+            }
+
+            // update to acc
+            acc.list.clear();
+            acc.list.addAll(buffer);

Review Comment:
   can't we populate `acc.list` in a single go? The current approach does make sense with a `ListView` but it does not with a `LinkedList` kept in memory.
   
   I believe we don't need the intermediate `buffer` and `newRetractBuffer`



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/ArrayAggFunction.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.flink.table.runtime.functions.aggregate;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.runtime.typeutils.InternalSerializers;
+import org.apache.flink.table.runtime.typeutils.LinkedListSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in ARRAY_AGG aggregate function. */
+@Internal
+public final class ArrayAggFunction<T>
+        extends BuiltInAggregateFunction<ArrayData, ArrayAggFunction.ArrayAggAccumulator<T>> {
+
+    private static final long serialVersionUID = -5860934997657147836L;
+
+    private final transient DataType elementDataType;
+
+    private final boolean ignoreNulls;
+
+    public ArrayAggFunction(LogicalType elementType, boolean ignoreNulls) {
+        this.elementDataType = toInternalDataType(elementType);
+        this.ignoreNulls = ignoreNulls;
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Planning
+    // --------------------------------------------------------------------------------------------
+
+    @Override
+    public List<DataType> getArgumentDataTypes() {
+        return Collections.singletonList(elementDataType);
+    }
+
+    @Override
+    public DataType getAccumulatorDataType() {
+        DataType linkedListType = getLinkedListType();
+        return DataTypes.STRUCTURED(
+                ArrayAggAccumulator.class,
+                DataTypes.FIELD("list", linkedListType),
+                DataTypes.FIELD("retractList", linkedListType));
+    }
+
+    @Override
+    public DataType getOutputDataType() {
+        return DataTypes.ARRAY(elementDataType).bridgedTo(ArrayData.class);
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private DataType getLinkedListType() {
+        TypeSerializer<T> serializer = InternalSerializers.create(elementDataType.getLogicalType());
+        return DataTypes.RAW(
+                LinkedList.class, (TypeSerializer) new LinkedListSerializer<>(serializer));
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Runtime
+    // --------------------------------------------------------------------------------------------
+
+    /** Accumulator for ARRAY_AGG with retraction. */
+    public static class ArrayAggAccumulator<T> {
+        public LinkedList<T> list;
+        public LinkedList<T> retractList;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            ArrayAggAccumulator<?> that = (ArrayAggAccumulator<?>) o;
+            return Objects.equals(list, that.list) && Objects.equals(retractList, that.retractList);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(list, retractList);
+        }
+    }
+
+    @Override
+    public ArrayAggAccumulator<T> createAccumulator() {
+        final ArrayAggAccumulator<T> acc = new ArrayAggAccumulator<>();
+        acc.list = new LinkedList<>();
+        acc.retractList = new LinkedList<>();
+        return acc;
+    }
+
+    public void accumulate(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value == null) {
+            if (!ignoreNulls) {
+                acc.list.add(null);
+            }
+        } else {
+            acc.list.add(value);
+        }
+    }
+
+    public void retract(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value != null) {

Review Comment:
   what about retracting nulls?



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java:
##########
@@ -1139,6 +1141,22 @@ public List<SqlGroupedWindowFunction> getAuxiliaryFunctions() {
     public static final SqlAggFunction APPROX_COUNT_DISTINCT =
             SqlStdOperatorTable.APPROX_COUNT_DISTINCT;
 
+    /**
+     * Use the definitions in Flink instead of {@link SqlLibraryOperators#ARRAY_AGG}, because we
+     * ignore nulls and returns nullable ARRAY type. Order by clause like <code>

Review Comment:
   is the comment still correct after the last changes?



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/ArrayAggFunction.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.flink.table.runtime.functions.aggregate;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.runtime.typeutils.InternalSerializers;
+import org.apache.flink.table.runtime.typeutils.LinkedListSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.apache.flink.table.types.utils.DataTypeUtils.toInternalDataType;
+
+/** Built-in ARRAY_AGG aggregate function. */
+@Internal
+public final class ArrayAggFunction<T>
+        extends BuiltInAggregateFunction<ArrayData, ArrayAggFunction.ArrayAggAccumulator<T>> {
+
+    private static final long serialVersionUID = -5860934997657147836L;
+
+    private final transient DataType elementDataType;
+
+    private final boolean ignoreNulls;
+
+    public ArrayAggFunction(LogicalType elementType, boolean ignoreNulls) {
+        this.elementDataType = toInternalDataType(elementType);
+        this.ignoreNulls = ignoreNulls;
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Planning
+    // --------------------------------------------------------------------------------------------
+
+    @Override
+    public List<DataType> getArgumentDataTypes() {
+        return Collections.singletonList(elementDataType);
+    }
+
+    @Override
+    public DataType getAccumulatorDataType() {
+        DataType linkedListType = getLinkedListType();
+        return DataTypes.STRUCTURED(
+                ArrayAggAccumulator.class,
+                DataTypes.FIELD("list", linkedListType),
+                DataTypes.FIELD("retractList", linkedListType));
+    }
+
+    @Override
+    public DataType getOutputDataType() {
+        return DataTypes.ARRAY(elementDataType).bridgedTo(ArrayData.class);
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private DataType getLinkedListType() {
+        TypeSerializer<T> serializer = InternalSerializers.create(elementDataType.getLogicalType());
+        return DataTypes.RAW(
+                LinkedList.class, (TypeSerializer) new LinkedListSerializer<>(serializer));
+    }
+
+    // --------------------------------------------------------------------------------------------
+    // Runtime
+    // --------------------------------------------------------------------------------------------
+
+    /** Accumulator for ARRAY_AGG with retraction. */
+    public static class ArrayAggAccumulator<T> {
+        public LinkedList<T> list;
+        public LinkedList<T> retractList;
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            ArrayAggAccumulator<?> that = (ArrayAggAccumulator<?>) o;
+            return Objects.equals(list, that.list) && Objects.equals(retractList, that.retractList);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(list, retractList);
+        }
+    }
+
+    @Override
+    public ArrayAggAccumulator<T> createAccumulator() {
+        final ArrayAggAccumulator<T> acc = new ArrayAggAccumulator<>();
+        acc.list = new LinkedList<>();
+        acc.retractList = new LinkedList<>();
+        return acc;
+    }
+
+    public void accumulate(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value == null) {
+            if (!ignoreNulls) {
+                acc.list.add(null);
+            }
+        } else {
+            acc.list.add(value);
+        }
+    }
+
+    public void retract(ArrayAggAccumulator<T> acc, T value) throws Exception {
+        if (value != null) {
+            if (!acc.list.remove(value)) {
+                acc.retractList.add(value);
+            }
+        }
+    }
+
+    public void merge(ArrayAggAccumulator<T> acc, Iterable<ArrayAggAccumulator<T>> its)
+            throws Exception {
+        for (ArrayAggAccumulator<T> otherAcc : its) {
+            // merge list of acc and other
+            List<T> buffer = new ArrayList<>();
+            for (T element : acc.list) {
+                buffer.add(element);
+            }
+            for (T element : otherAcc.list) {
+                buffer.add(element);
+            }
+            // merge retract list of acc and other
+            List<T> retractBuffer = new ArrayList<>();
+            for (T element : acc.retractList) {
+                retractBuffer.add(element);
+            }
+            for (T element : otherAcc.retractList) {
+                retractBuffer.add(element);
+            }

Review Comment:
   do we need the `retractBuffer`? Can't we just iterate over both the `retractList` and create only the final `newRetractBuffer`? It seems we create quite some unnecessary objects and potentially list resizing here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1455187450


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   Hi @dawidwys, @snuyanzin,
   
   I referenced `LagAggFunction` to use `LinkedList` to hold null values and it works. Now `RESPECT NULLS` and `IGNORE NULLS` are supported. 
   https://github.com/apache/flink/blob/1ffb481111f658b699702357921a48e914d13caf/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/LagAggFunction.java#L147-L150
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452991614


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   It's ok to support it only partially however the main concern here: 
   ~~I would expect more consistent result
   since there is also input `Row.ofKind(INSERT, "D", null),` and expected value `Row.of("D", null),`
   So I would expect for ths either containing nulls or both not containing null however not mixed~~
   
   UPD: to be more clear:
   after playing with Postgres and BigQuery I noticed that both respect nulls by default and for BigQuery to make it ignoring nulls it should be specified explicitely. Is there a reason why for Flink it is done differently? Probably it's better to have similar behaviour
   
   I don't tell that we need to support `RESPECT NULLS | IGNORE NULLS` syntax, however need to make `RESPECT NULLS` default behaviour to be on same page with vendors



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452991614


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   It's ok to support it only partially however the main concern here: 
   ~~I would expect more consistent result
   since there is also input `Row.ofKind(INSERT, "D", null),` and expected value `Row.of("D", null),`
   So I would expect for ths either containing nulls or both not containing null however not mixed~~
   
   UPD: to be more clear:
   after playing with Postgres and BigQuery I noticed that both respect nulls by default and for BigQuery to make it ignoring nulls it should be specified explicitely. Is there a reason why for Flink it is done differently? Probably it's better to have similar behaviour



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453252729


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   @snuyanzin 
   `ListView<T>` cannot hold null values, and since it is a generic `List,` it cannot be represented by a specific object for null values. Is there any good solution for this?
   
   https://github.com/apache/flink/blob/d92ab390ff0d91459daccfda7d6cb0acf6ca92eb/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/JsonArrayAggFunction.java#L65



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1859780440

   @flinkbot run azure


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453313913


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),

Review Comment:
   One more thing
   currently it checks only ability to work with `INT` input for `ARRAY_AGG`
   it would be great to have tests for other types
   especially `ROW`, `ARRAY`, `MAP` where expected output should be `ARRAY<ROW>`, `ARRAY<ARRAY>`. ARRAY<MAP>`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys merged PR #23411:
URL: https://github.com/apache/flink/pull/23411


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1457306043


##########
docs/data/sql_functions.yml:
##########
@@ -1059,6 +1059,13 @@ aggregate:
       Divides the rows for each window partition into `n` buckets ranging from 1 to at most `n`.
       If the number of rows in the window partition doesn't divide evenly into the number of buckets, then the remainder values are distributed one per bucket, starting with the first bucket.
       For example, with 6 rows and 4 buckets, the bucket values would be as follows: 1 1 2 2 3 4
+  - sql: ARRAY_AGG([ ALL | DISTINCT ] expression [ RESPECT NULLS | IGNORE NULLS ])
+    table: FIELD.arrayAgg
+    description: |
+      By default or with keyword `ALL` and, return an array that concatenates the input rows
+      and returns `NULL` if there are no input rows. Use `DISTINCT` for one unique instance of each value.
+      By default null values are respected, use `IGNORE NULLS` to skip null values.

Review Comment:
   ```suggestion
         By default `NULL` values are respected, use `IGNORE NULLS` to skip `NULL` values.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452975551


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   ```sql
   -- calcite
   ARRAY_AGG([ ALL | DISTINCT ] value [ RESPECT NULLS | IGNORE NULLS ] [ ORDER BY orderItem [, orderItem ]* ] )
   -- flink
   ARRAY_AGG([ ALL | DISTINCT ] expression)
   ```
   
   This function simplifies some aspects compared to calcite. 
   1. Currently, the Flink parser does not support parsing RESPECT NULLS | IGNORE NULLS, but it is still possible to make changes to support it.
   2. AggregateUtil#extractDistinctInformation 848~858 ignores the ignoreNulls fields. https://github.com/apache/flink/blob/6bdb4f752adb2b43dbadd8ad4fffcb4c00568dd3/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/AggregateUtil.scala#L848-L858
   3. ListView does not support null values. https://github.com/apache/flink/blob/40cbf231e3dc15129b69136941adc15beebe8ef6/flink-table/flink-table-common/src/main/java/org/apache/flink/table/api/dataview/ListView.java#L45-L47
   
   For that reasons I made some simplification. If we need it, I can make some attempts.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452991614


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   It's ok to support it only partially however the main concern here: 
   ~~I would expect more consistent result
   since there is also input `Row.ofKind(INSERT, "D", null),` and expected value `Row.of("D", null),`
   So I would expect for ths either containing nulls or both not containing null however not mixed~~
   
   UPD: to be more clear:
   after playing with Postgres and BigQuery I noticed that both respect nulls by default and for BigQuery to make it ignoring nulls it should be specified explicitely. Is there a reason why for Flink it is done differently?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452991614


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   It's ok to support it only partially however the main concern here: I would expect more consistent result
   since there is also input `Row.ofKind(INSERT, "D", null),` and expected value `Row.of("D", null),`
   So I would expect for ths either containing nulls or both not containing null however not mixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1455190307


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),

Review Comment:
   Full types test is in `ArrayAggFunctionTest`. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1457306043


##########
docs/data/sql_functions.yml:
##########
@@ -1059,6 +1059,13 @@ aggregate:
       Divides the rows for each window partition into `n` buckets ranging from 1 to at most `n`.
       If the number of rows in the window partition doesn't divide evenly into the number of buckets, then the remainder values are distributed one per bucket, starting with the first bucket.
       For example, with 6 rows and 4 buckets, the bucket values would be as follows: 1 1 2 2 3 4
+  - sql: ARRAY_AGG([ ALL | DISTINCT ] expression [ RESPECT NULLS | IGNORE NULLS ])
+    table: FIELD.arrayAgg
+    description: |
+      By default or with keyword `ALL` and, return an array that concatenates the input rows
+      and returns `NULL` if there are no input rows. Use `DISTINCT` for one unique instance of each value.
+      By default null values are respected, use `IGNORE NULLS` to skip null values.

Review Comment:
   ```suggestion
         By default `NULL` values are respected, use `IGNORE NULLS` to skip null values.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1455187450


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   Hi @dawidwys, @snuyanzin,
   
   I refers to `LagAggFunction` use `LinkedList` to hold null values and it works. Now `RESPECT NULLS` and `IGNORE NULLS` are supported. 
   https://github.com/apache/flink/blob/1ffb481111f658b699702357921a48e914d13caf/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/aggregate/LagAggFunction.java#L147-L150
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1897661962

   > Thanks for addressing comments in general it looks ok from my side i guess there is one little thing: since it is based on Calcite parser it allows to have `ORDER BY` inside... At the same time it is currently not supported on Flink level, not sure whether we can redefine this behavior however at least it would make sense to mention it in doc that it is not supported
   
   Yes, ORDER BY allows sorting of any field in the input rows, but currently it is difficult to obtain the complete input rows for sorting in the function implementation. Therefore, the `ORDER BY` clause is not supported yet.
   I have added an explanation in the documentation.
   
   @snuyanzin, please help take a look again when you have time.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1845527082

   yes, sorry was not clear enough
   >to compare when merging and retracting lists?
   
   yes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452526246


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlAggFunction.java:
##########
@@ -63,6 +64,7 @@ public final class BridgingSqlAggFunction extends SqlAggFunction {
     private final List<RelDataType> paramTypes;
 
     private BridgingSqlAggFunction(
+            FlinkContext context,

Review Comment:
   What is the reason for those changes? Is it only to be able to create the `equalityHandler`? If so, let's keep it simple and remove those.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453268979


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   I think it's a very good point. It would be nice to be compatible with SQL and other vendors. 
   
   One idea is we could keep the null indices and set those while retrieving the result. Happy to hear better solutions though.
   
   Another is to wrap all values in a `GenericRowData`, but we'd need to iterate over the data to unwrap it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453268979


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   I think it's a very good point. It would be nice to be compatible with SQL and other vendors. 
   
   One idea is we could keep the null indices and set those while retrieving the result. Happy to hear better solutions though.
   
   Another is to wrap all values in a `GenericRowData`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453313913


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),

Review Comment:
   One more thing
   currently it checks only ability to work with `INT` input for `ARRAY_AGG`
   it would be great to have tests for other types
   epecially `ROW`, `ARRAY`, `MAP` where expected output should be `ARRAY<ROW>`, `ARRAY<ARRAY>`. ARRAY<MAP>`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452798689


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),
+                                        Row.of("D", null),
+                                        Row.of("E", new Integer[] {6})))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(DISTINCT f1) FROM "
+                                                + source
+                                                + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg().distinct()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   same here 
   based on input I would expect 
   ```java
   Row.of("C", new Integer[] {3, null}),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452798388


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   I wonder why here is just 
   ```java
   Row.of("C", new Integer[] {3}),
   ```
   based on input I would expect
   ```java
   Row.of("C", new Integer[] {3, null}),
   ```
   or did I miss anything?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453313913


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),

Review Comment:
   One more thing
   currently it checks only ability to work with `INT` input for `ARRAY_AGG`
   it would be great to have tests for other types
   especially `ROW`, `ARRAY`, `MAP` where expected output should be `ARRAY<ROW>`, `ARRAY<ARRAY>`,   ` ARRAY<MAP>`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1893141857

   Thanks @snuyanzin @dawidwys for the review.
   Could you help review it again?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1845388338

   Thanks @snuyanzin for the review.
   Sorry, I was a little puzzled because I did not use `HashMap` in `Arrayaggfunction`.
   Could you help pinpoint exactly where it is?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1845473679

   Do you mean using equalityEvaluator to compare when merging and retracting lists?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1862451170

   Hi @snuyanzin, could you help take a look again when you have time?
   Thanks.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1903588731

   Hi @dawidwys, please help review it again when you have time.
   Thanks a lot.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1924052949

   I think we could wait until Monday or even more since right now a feature freeze and need to wait for cutting release branch


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453268979


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   I think it's a very good point. It would be nice to be compatible with SQL and other vendors. 
   
   One idea is we could keep the null indices and set those while retrieving the result. Happy to hear better solutions though.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1453283389


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   yes, I was also thinking about storing null indexes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #23411:
URL: https://github.com/apache/flink/pull/23411#discussion_r1452799002


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/ArrayAggFunctionITCase.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.flink.table.planner.functions;
+
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.ROW;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.types.RowKind.DELETE;
+import static org.apache.flink.types.RowKind.INSERT;
+import static org.apache.flink.types.RowKind.UPDATE_AFTER;
+import static org.apache.flink.types.RowKind.UPDATE_BEFORE;
+
+/** Tests for built-in ARRAY_AGG aggregation functions. */
+class ArrayAggFunctionITCase extends BuiltInAggregateFunctionTestBase {
+
+    @Override
+    Stream<TestSpec> getTestCaseSpecs() {
+        return Stream.of(
+                TestSpec.forFunction(BuiltInFunctionDefinitions.ARRAY_AGG)
+                        .withDescription("ARRAY changelog stream aggregation")
+                        .withSource(
+                                ROW(STRING(), INT()),
+                                Arrays.asList(
+                                        Row.ofKind(INSERT, "A", 1),
+                                        Row.ofKind(INSERT, "A", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 2),
+                                        Row.ofKind(INSERT, "B", 3),
+                                        Row.ofKind(INSERT, "C", 3),
+                                        Row.ofKind(INSERT, "C", null),
+                                        Row.ofKind(INSERT, "D", null),
+                                        Row.ofKind(INSERT, "E", 4),
+                                        Row.ofKind(INSERT, "E", 5),
+                                        Row.ofKind(DELETE, "E", 5),
+                                        Row.ofKind(UPDATE_BEFORE, "E", 4),
+                                        Row.ofKind(UPDATE_AFTER, "E", 6)))
+                        .testResult(
+                                source ->
+                                        "SELECT f0, array_agg(f1) FROM " + source + " GROUP BY f0",
+                                TableApiAggSpec.groupBySelect(
+                                        Collections.singletonList($("f0")),
+                                        $("f0"),
+                                        $("f1").arrayAgg()),
+                                ROW(STRING(), ARRAY(INT())),
+                                ROW(STRING(), ARRAY(INT())),
+                                Arrays.asList(
+                                        Row.of("A", new Integer[] {1, 2}),
+                                        Row.of("B", new Integer[] {2, 2, 3}),
+                                        Row.of("C", new Integer[] {3}),

Review Comment:
   Also similar query for postgres
   ```sql
   with input(a, c) as (
   select 'a', 1
   union all 
   select 'a', 2
   union all 
   select 'c', 3
   union all 
   select 'c', null
   )
   select a, array_agg(distinct c) from input group by a
   ```
   gives
   ```
   a|array_agg|
   -+---------+
   a|{1,2}    |
   c|{3,NULL} |
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-21949][table] Support ARRAY_AGG aggregate function [flink]

Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on PR #23411:
URL: https://github.com/apache/flink/pull/23411#issuecomment-1930284532

   Thanks for the update @Jiabao-Sun The implementation looks good now. I want to go through the tests again, but I need a bit more time. I hope this is fine, cause anyway we need to wait for a branch cut.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org