1919import org .apache .gluten .rexnode .AggregateCallConverter ;
2020import org .apache .gluten .rexnode .Utils ;
2121import org .apache .gluten .rexnode .WindowUtils ;
22- import org .apache .gluten .table .runtime .operators .GlutenOneInputOperator ;
2322import org .apache .gluten .util .LogicalTypeConverter ;
2423import org .apache .gluten .util .PlanNodeIdGenerator ;
2524
3736
3837import org .apache .flink .FlinkVersion ;
3938import org .apache .flink .api .dag .Transformation ;
39+ import org .apache .flink .api .java .tuple .Tuple2 ;
4040import org .apache .flink .api .java .tuple .Tuple5 ;
4141import org .apache .flink .configuration .ReadableConfig ;
4242import org .apache .flink .streaming .api .operators .OneInputStreamOperator ;
4343import org .apache .flink .streaming .api .operators .SimpleOperatorFactory ;
4444import org .apache .flink .streaming .api .transformations .OneInputTransformation ;
4545import org .apache .flink .table .data .RowData ;
46- import org .apache .flink .table .planner .codegen .CodeGeneratorContext ;
47- import org .apache .flink .table .planner .codegen .agg .AggsHandlerCodeGenerator ;
4846import org .apache .flink .table .planner .delegation .PlannerBase ;
4947import org .apache .flink .table .planner .plan .logical .WindowingStrategy ;
5048import org .apache .flink .table .planner .plan .nodes .exec .ExecEdge ;
5553import org .apache .flink .table .planner .plan .nodes .exec .InputProperty ;
5654import org .apache .flink .table .planner .plan .nodes .exec .utils .ExecNodeUtil ;
5755import org .apache .flink .table .planner .plan .utils .AggregateInfoList ;
56+ import org .apache .flink .table .planner .plan .utils .AggregateUtil ;
5857import org .apache .flink .table .planner .plan .utils .KeySelectorUtil ;
5958import org .apache .flink .table .planner .utils .JavaScalaConversionUtil ;
6059import org .apache .flink .table .planner .utils .TableConfigUtils ;
61- import org .apache .flink .table .runtime .generated .GeneratedNamespaceAggsHandleFunction ;
6260import org .apache .flink .table .runtime .groupwindow .NamedWindowProperty ;
63- import org .apache .flink .table .runtime .groupwindow .WindowProperty ;
6461import org .apache .flink .table .runtime .keyselector .RowDataKeySelector ;
65- import org .apache .flink .table .runtime .operators .window .tvf .slicing .SliceAssigner ;
6662import org .apache .flink .table .runtime .typeutils .InternalTypeInfo ;
6763import org .apache .flink .table .runtime .util .TimeWindowUtil ;
68- import org .apache .flink .table .types .DataType ;
64+ import org .apache .flink .table .types .logical . LogicalType ;
6965import org .apache .flink .table .types .logical .RowType ;
7066
7167import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .annotation .JsonCreator ;
7268import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .annotation .JsonProperty ;
7369
7470import org .apache .calcite .rel .core .AggregateCall ;
75- import org .apache .calcite .tools .RelBuilder ;
7671import org .apache .commons .math3 .util .ArithmeticUtils ;
7772
7873import javax .annotation .Nullable ;
@@ -184,6 +179,14 @@ protected Transformation<RowData> translateToPlanInternal(
184179 final ZoneId shiftTimeZone =
185180 TimeWindowUtil .getShiftTimeZone (
186181 windowing .getTimeAttributeType (), TableConfigUtils .getLocalTimeZone (config ));
182+ final AggregateInfoList globalAggInfoList =
183+ AggregateUtil .deriveStreamWindowAggregateInfoList (
184+ planner .getTypeFactory (),
185+ localAggInputRowType , // should use original input here
186+ JavaScalaConversionUtil .toScala (Arrays .asList (aggCalls )),
187+ needRetraction ,
188+ windowing .getWindow (),
189+ true );
187190
188191 // --- Begin Gluten-specific code changes ---
189192 // TODO: velox window not equal to flink window.
@@ -205,11 +208,15 @@ protected Transformation<RowData> translateToPlanInternal(
205208 // TODO: support more window types.
206209 Tuple5 <Long , Long , Long , Integer , Integer > windowSpecParams =
207210 WindowUtils .extractWindowParameters (windowing );
211+ Tuple2 <Integer , Integer > windowStartAndEndIndexes =
212+ WindowUtils .getWindowStartAndEndIndexes (namedWindowProperties , (RowType ) getOutputType ());
208213 long size = windowSpecParams .f0 ;
209214 long slide = windowSpecParams .f1 ;
210215 long offset = windowSpecParams .f2 ;
211216 int rowtimeIndex = windowSpecParams .f3 ;
212217 int windowType = windowSpecParams .f4 ;
218+ int windowStartIndex = windowStartAndEndIndexes .f0 ;
219+ int windowEndIndex = windowStartAndEndIndexes .f1 ;
213220 PartitionFunctionSpec sliceAssignerSpec =
214221 new StreamWindowPartitionFunctionSpec (
215222 inputType , rowtimeIndex , size , slide , offset , windowType );
@@ -252,24 +259,30 @@ protected Transformation<RowData> translateToPlanInternal(
252259 offset ,
253260 windowType ,
254261 outputType ,
255- rowtimeIndex );
256- final OneInputStreamOperator windowOperator =
257- new GlutenOneInputOperator (
262+ true ,
263+ rowtimeIndex ,
264+ windowStartIndex ,
265+ windowEndIndex );
266+ final LogicalType [] accTypes = convertToLogicalTypes (globalAggInfoList .getAccTypes ());
267+ final RowDataKeySelector selector =
268+ KeySelectorUtil .getRowDataSelector (
269+ planner .getFlinkContext ().getClassLoader (),
270+ grouping ,
271+ InternalTypeInfo .of (inputRowType ));
272+ final OneInputStreamOperator <RowData , RowData > windowOperator =
273+ new org .apache .gluten .table .runtime .operators .WindowAggOperator <RowData , RowData , Long >(
258274 new StatefulPlanNode (windowAgg .getId (), windowAgg ),
259275 PlanNodeIdGenerator .newId (),
260276 inputType ,
261277 Map .of (windowAgg .getId (), outputType ),
262278 RowData .class ,
263279 RowData .class ,
264- "StreamExecGlobalWindowAggregate" );
280+ "StreamExecWindowAggregate" ,
281+ selector .getProducedType (),
282+ globalAggInfoList .getAggNames (),
283+ accTypes );
265284 // --- End Gluten-specific code changes ---
266285
267- final RowDataKeySelector selector =
268- KeySelectorUtil .getRowDataSelector (
269- planner .getFlinkContext ().getClassLoader (),
270- grouping ,
271- InternalTypeInfo .of (inputRowType ));
272-
273286 final OneInputTransformation <RowData , RowData > transform =
274287 ExecNodeUtil .createOneInputTransformation (
275288 inputTransform ,
@@ -285,40 +298,4 @@ protected Transformation<RowData> translateToPlanInternal(
285298 transform .setStateKeyType (selector .getProducedType ());
286299 return transform ;
287300 }
288-
289- private GeneratedNamespaceAggsHandleFunction <Long > createAggsHandler (
290- String name ,
291- SliceAssigner sliceAssigner ,
292- AggregateInfoList aggInfoList ,
293- int mergedAccOffset ,
294- boolean mergedAccIsOnHeap ,
295- DataType [] mergedAccExternalTypes ,
296- ExecNodeConfig config ,
297- ClassLoader classLoader ,
298- RelBuilder relBuilder ,
299- ZoneId shifTimeZone ) {
300- final AggsHandlerCodeGenerator generator =
301- new AggsHandlerCodeGenerator (
302- new CodeGeneratorContext (config , classLoader ),
303- relBuilder ,
304- JavaScalaConversionUtil .toScala (localAggInputRowType .getChildren ()),
305- true ) // copyInputField
306- .needAccumulate ()
307- .needMerge (mergedAccOffset , mergedAccIsOnHeap , mergedAccExternalTypes );
308-
309- final List <WindowProperty > windowProperties =
310- Arrays .asList (
311- Arrays .stream (namedWindowProperties )
312- .map (NamedWindowProperty ::getProperty )
313- .toArray (WindowProperty []::new ));
314-
315- return generator .generateNamespaceAggsHandler (
316- name ,
317- aggInfoList ,
318- JavaScalaConversionUtil .toScala (windowProperties ),
319- sliceAssigner ,
320- // we use window end timestamp to indicate a slicing window, see SliceAssigner
321- Long .class ,
322- shifTimeZone );
323- }
324301}
0 commit comments