26
26
import org .apache .doris .nereids .trees .expressions .Slot ;
27
27
import org .apache .doris .nereids .trees .expressions .SlotReference ;
28
28
import org .apache .doris .nereids .trees .expressions .functions .agg .AggregateFunction ;
29
+ import org .apache .doris .nereids .trees .expressions .literal .TinyIntLiteral ;
29
30
import org .apache .doris .nereids .trees .plans .Plan ;
30
31
import org .apache .doris .nereids .trees .plans .algebra .Aggregate ;
31
32
import org .apache .doris .nereids .trees .plans .algebra .SetOperation .Qualifier ;
42
43
import org .apache .doris .nereids .trees .plans .logical .OutputPrunable ;
43
44
import org .apache .doris .nereids .trees .plans .visitor .CustomRewriter ;
44
45
import org .apache .doris .nereids .trees .plans .visitor .DefaultPlanRewriter ;
46
+ import org .apache .doris .nereids .types .TinyIntType ;
45
47
import org .apache .doris .nereids .util .ExpressionUtils ;
46
48
import org .apache .doris .nereids .util .Utils ;
47
49
import org .apache .doris .qe .ConnectContext ;
@@ -345,6 +347,8 @@ private LogicalUnion pruneUnionOutput(LogicalUnion union, PruneContext context)
345
347
}
346
348
List <NamedExpression > prunedOutputs = Lists .newArrayList ();
347
349
List <List <NamedExpression >> constantExprsList = union .getConstantExprsList ();
350
+ List <List <SlotReference >> regularChildrenOutputs = union .getRegularChildrenOutputs ();
351
+ List <Plan > children = union .children ();
348
352
List <Integer > extractColumnIndex = Lists .newArrayList ();
349
353
for (int i = 0 ; i < originOutput .size (); i ++) {
350
354
NamedExpression output = originOutput .get (i );
@@ -353,31 +357,41 @@ private LogicalUnion pruneUnionOutput(LogicalUnion union, PruneContext context)
353
357
extractColumnIndex .add (i );
354
358
}
355
359
}
356
- if (prunedOutputs .isEmpty ()) {
357
- List <NamedExpression > candidates = Lists .newArrayList (originOutput );
358
- candidates .retainAll (keys );
359
- if (candidates .isEmpty ()) {
360
- candidates = originOutput ;
361
- }
362
- NamedExpression minimumColumn = ExpressionUtils .selectMinimumColumn (candidates );
363
- prunedOutputs = ImmutableList .of (minimumColumn );
364
- extractColumnIndex .add (originOutput .indexOf (minimumColumn ));
365
- }
366
360
367
- int len = extractColumnIndex .size ();
368
361
ImmutableList .Builder <List <NamedExpression >> prunedConstantExprsList
369
362
= ImmutableList .builderWithExpectedSize (constantExprsList .size ());
370
- for (List <NamedExpression > row : constantExprsList ) {
371
- ImmutableList .Builder <NamedExpression > newRow = ImmutableList .builderWithExpectedSize (len );
372
- for (int idx : extractColumnIndex ) {
373
- newRow .add (row .get (idx ));
363
+ if (prunedOutputs .isEmpty ()) {
364
+ // process prune all columns
365
+ NamedExpression originSlot = originOutput .get (0 );
366
+ prunedOutputs = ImmutableList .of (new SlotReference (originSlot .getExprId (), originSlot .getName (),
367
+ TinyIntType .INSTANCE , false , originSlot .getQualifier ()));
368
+ regularChildrenOutputs = Lists .newArrayListWithCapacity (regularChildrenOutputs .size ());
369
+ children = Lists .newArrayListWithCapacity (children .size ());
370
+ for (int i = 0 ; i < union .getArity (); i ++) {
371
+ LogicalProject <?> project = new LogicalProject <>(
372
+ ImmutableList .of (new Alias (new TinyIntLiteral ((byte ) 1 ))), union .child (i ));
373
+ regularChildrenOutputs .add ((List ) project .getOutput ());
374
+ children .add (project );
375
+ }
376
+ for (int i = 0 ; i < constantExprsList .size (); i ++) {
377
+ prunedConstantExprsList .add (ImmutableList .of (new Alias (new TinyIntLiteral ((byte ) 1 ))));
378
+ }
379
+ } else {
380
+ int len = extractColumnIndex .size ();
381
+ for (List <NamedExpression > row : constantExprsList ) {
382
+ ImmutableList .Builder <NamedExpression > newRow = ImmutableList .builderWithExpectedSize (len );
383
+ for (int idx : extractColumnIndex ) {
384
+ newRow .add (row .get (idx ));
385
+ }
386
+ prunedConstantExprsList .add (newRow .build ());
374
387
}
375
- prunedConstantExprsList .add (newRow .build ());
376
388
}
377
- if (prunedOutputs .equals (originOutput )) {
389
+
390
+ if (prunedOutputs .equals (originOutput ) && !context .requiredSlots .isEmpty ()) {
378
391
return union ;
379
392
} else {
380
- return union .withNewOutputsAndConstExprsList (prunedOutputs , prunedConstantExprsList .build ());
393
+ return union .withNewOutputsChildrenAndConstExprsList (prunedOutputs , children ,
394
+ regularChildrenOutputs , prunedConstantExprsList .build ());
381
395
}
382
396
}
383
397
0 commit comments