JingsongLi commented on a change in pull request #8881: [FLINK-12660][hive] 
Integrate Flink with Hive UDAF
URL: https://github.com/apache/flink/pull/8881#discussion_r298029105
 
 

 ##########
 File path: 
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/functions/hive/HiveGenericUDAF.java
 ##########
 @@ -0,0 +1,215 @@
+/*
+ * 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.functions.hive;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.catalog.hive.util.HiveTypeUtil;
+import org.apache.flink.table.functions.AggregateFunction;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.hive.conversion.HiveInspectors;
+import org.apache.flink.table.functions.hive.conversion.HiveObjectConversion;
+import org.apache.flink.table.functions.hive.conversion.IdentityConversion;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.LegacyTypeInfoDataTypeConverter;
+
+import org.apache.hadoop.hive.ql.exec.UDAF;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFBridge;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFResolver2;
+import org.apache.hadoop.hive.ql.udf.generic.SimpleGenericUDAFParameterInfo;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+
+import java.util.Arrays;
+
+/**
+ * An {@link AggregateFunction} implementation that calls Hive's {@link UDAF} 
or {@link GenericUDAFEvaluator}.
+ */
+@Internal
+public class HiveGenericUDAF
+       extends AggregateFunction<Object, 
GenericUDAFEvaluator.AggregationBuffer> implements HiveFunction {
+
+       private final HiveFunctionWrapper hiveFunctionWrapper;
+       // Flag that indicates whether a bridge between GenericUDAF and UDAF is 
required.
+       // Old UDAF can be used with the GenericUDAF infrastructure through 
bridging.
+       private final boolean isUDAFBridgeRequired;
+
+       private Object[] constantArguments;
+       private DataType[] argTypes;
+
+       private transient GenericUDAFEvaluator partialEvaluator;
+       private transient GenericUDAFEvaluator finalEvaluator;
+       private transient ObjectInspector finalResult;
+       private transient HiveObjectConversion[] conversions;
+       private transient boolean allIdentityConverter;
+       private transient boolean initialized;
+
+       public HiveGenericUDAF(HiveFunctionWrapper funcWrapper) {
+               this(funcWrapper, false);
+       }
+
+       public HiveGenericUDAF(HiveFunctionWrapper funcWrapper, boolean 
isUDAFBridgeRequired) {
+               this.hiveFunctionWrapper = funcWrapper;
+               this.isUDAFBridgeRequired = isUDAFBridgeRequired;
+       }
+
+       @Override
+       public void open(FunctionContext context) throws Exception {
+               super.open(context);
+               init();
+       }
+
+       private void init() throws HiveException {
+               ObjectInspector[] inputInspectors = 
HiveInspectors.toInspectors(constantArguments, argTypes);
+
+               // Flink UDAF only supports Hive UDAF's PARTIAL_1 and FINAL mode
+
+               // PARTIAL1: from original data to partial aggregation data:
+               //              iterate() and terminatePartial() will be called.
+               this.partialEvaluator = createEvaluator(inputInspectors);
+               ObjectInspector partialResult = 
partialEvaluator.init(GenericUDAFEvaluator.Mode.PARTIAL1, inputInspectors);
+
+               // FINAL: from partial aggregation to full aggregation:
+               //              merge() and terminate() will be called.
+               this.finalEvaluator = createEvaluator(inputInspectors);
+               this.finalResult = finalEvaluator.init(
+                       GenericUDAFEvaluator.Mode.FINAL, new ObjectInspector[]{ 
partialResult });
+
+               conversions = new HiveObjectConversion[inputInspectors.length];
+               for (int i = 0; i < inputInspectors.length; i++) {
+                       conversions[i] = 
HiveInspectors.getConversion(inputInspectors[i], argTypes[i].getLogicalType());
+               }
+               allIdentityConverter = Arrays.stream(conversions)
+                       .allMatch(conv -> conv instanceof IdentityConversion);
+
+               initialized = true;
+       }
+
+       private GenericUDAFEvaluator createEvaluator(ObjectInspector[] 
inputInspectors) throws SemanticException {
+               GenericUDAFResolver2 resolver;
+
+               if (isUDAFBridgeRequired) {
+                       resolver = new GenericUDAFBridge((UDAF) 
hiveFunctionWrapper.createFunction());
+               } else {
+                       resolver = (GenericUDAFResolver2) 
hiveFunctionWrapper.createFunction();
+               }
+
+               return resolver.getEvaluator(
+                       new SimpleGenericUDAFParameterInfo(
+                               inputInspectors,
+                               // The flag to indicate if the UDAF invocation 
was from the windowing function call or not.
 
 Review comment:
   👍 Detailed comments.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to