Airblader commented on a change in pull request #17549:
URL: https://github.com/apache/flink/pull/17549#discussion_r735345872



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/WrapJsonAggFunctionArgumentsRule.java
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.plan.rules.logical;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction;
+import org.apache.flink.table.planner.hint.FlinkHints;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.fun.SqlJsonObjectAggAggFunction;
+import org.apache.calcite.tools.RelBuilder;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Predicate;
+
+import static 
org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_STRING;
+import static 
org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction.of;
+
+/**
+ * Transforms JSON aggregation functions by wrapping operands into {@link
+ * BuiltInFunctionDefinitions#JSON_STRING}.
+ *
+ * <p>Essentially, a call like {@code JSON_OBJECTAGG(f0 VALUE f1)} will be 
transformed into {@code
+ * JSON_OBJECTAGG(f0 VALUE JSON_STRING(f1))}. By placing a marker {@link 
RelHint} on the aggregation
+ * afterwards we ensure that this transformation occurs just once.
+ *
+ * <p>{@link BuiltInFunctionDefinitions#JSON_STRING} will take care of 
serializing the values into
+ * their correct representation, and the actual aggregation function's 
implementation can simply
+ * insert the values as raw nodes instead. This avoids having to re-implement 
the logic for all
+ * supported types in the aggregation function again.
+ */
+@Internal
+public class WrapJsonAggFunctionArgumentsRule
+        extends RelRule<WrapJsonAggFunctionArgumentsRule.Config> {
+
+    public static final RelOptRule INSTANCE =
+            Config.EMPTY.as(Config.class).onJsonFunctions().toRule();
+
+    /** Marker hint that a call has already been transformed. */
+    private static final RelHint MARKER_HINT =
+            
RelHint.builder(FlinkHints.HINT_NAME_JSON_AGGREGATE_WRAPPED).build();
+
+    public WrapJsonAggFunctionArgumentsRule(Config config) {
+        super(config);
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {

Review comment:
       I need to re-work this rule, because it makes an incorrect assumption. 
We cannot just wrap fields as needed in a projection, because those fields 
might also be used elsewhere afterwards, e.g.
   
   ```
   SELECT f0, JSON_OBJECTAGG(f1 VALUE f0) FROM T GROUP BY f0
   ```
   
   will produce an error. Instead, we need to preserve all fields and add the 
projected field _additionally_.




-- 
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-unsubscr...@flink.apache.org

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


Reply via email to