Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
import org.apache.doris.thrift.TResultSinkType;
import org.apache.doris.thrift.TRuntimeFilterType;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand All @@ -233,10 +234,12 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -2887,8 +2890,8 @@ private void updateScanSlotsMaterialization(ScanNode scanNode,
.collect(Collectors.toSet());
requiredWithVirtualColumns.addAll(virtualColumnInputSlotIds);
}
// TODO: use smallest slot if do not need any slot in upper node
SlotDescriptor smallest = scanNode.getTupleDesc().getSlots().get(0);
// Find the smallest column, for count(*) or other situation that slot is empty after prune
SlotDescriptor smallest = getSmallestSlot(scanNode.getTupleDesc().getSlots());
scanNode.getTupleDesc().getSlots().removeIf(s -> !requiredWithVirtualColumns.contains(s.getId()));
if (scanNode.getTupleDesc().getSlots().isEmpty()) {
scanNode.getTupleDesc().getSlots().add(smallest);
Expand All @@ -2906,6 +2909,56 @@ private void updateScanSlotsMaterialization(ScanNode scanNode,
}
}

/**
* Get the smallest slot in the slot list.
*
* @param slots slot list
* @return the smallest slot
*/
@VisibleForTesting
@Nullable
public static SlotDescriptor getSmallestSlot(List<SlotDescriptor> slots) {
if (slots == null || slots.isEmpty()) {
return null;
}
return Collections.min(slots, new SlotSizeComparator());
}

/**
* Comparator to compare the size of two slots.
*/
public static class SlotSizeComparator implements Comparator<SlotDescriptor> {
@Override
public int compare(SlotDescriptor s1, SlotDescriptor s2) {
int p1 = typePriority(s1);
int p2 = typePriority(s2);

if (p1 != p2) {
return Integer.compare(p1, p2);
}

int res = Integer.compare(s1.getType().getSlotSize(), s2.getType().getSlotSize());
if (res != 0) {
return res;
} else {
return Integer.compare(s1.getType().getLength(), s2.getType().getLength());
}
}

private int typePriority(SlotDescriptor s) {
if (s.getType().isNumericType() || s.getType().isDateType() || s.getType().isBoolean()
|| s.getType().isTimeType() || s.getType().isIP()) {
return 1;
} else if (s.getType().isStringType()) {
return 2;
} else if (s.getType().isComplexType()) {
return 3;
} else {
return Integer.MAX_VALUE;
}
}
}

private void addConjunctsToPlanNode(PhysicalFilter<? extends Plan> filter,
PlanNode planNode,
PlanTranslatorContext context) {
Expand Down
Loading
Loading