Skip to content
Open
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 @@ -66,6 +66,7 @@
import org.apache.doris.nereids.trees.expressions.functions.agg.NullableAggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.generator.TableGeneratingFunction;
import org.apache.doris.nereids.trees.expressions.functions.generator.Unnest;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GroupingScalarFunction;
import org.apache.doris.nereids.trees.expressions.functions.table.FullTextSearch;
Expand Down Expand Up @@ -873,7 +874,7 @@ private LogicalJoin<Plan, Plan> bindJoin(MatchingContext<LogicalJoin<Plan, Plan>
}
return new LogicalJoin<>(join.getJoinType(),
hashConjuncts, otherConjuncts,
join.getDistributeHint(), join.getMarkJoinSlotReference(), join.getExceptAsteriskOutputs(),
join.getDistributeHint(), join.getMarkJoinSlotReference(),
join.children(), null);
}

Expand Down Expand Up @@ -928,6 +929,7 @@ private LogicalPlan bindUsingJoin(MatchingContext<LogicalUsingJoin<Plan, Plan>>
ExpressionRewriteContext rewriteContext = new ExpressionRewriteContext(using, cascadesContext);

Builder<Expression> hashEqExprs = ImmutableList.builderWithExpectedSize(unboundHashJoinConjunct.size());
List<Slot> leftConjunctsSlots = Lists.newArrayList();
List<Slot> rightConjunctsSlots = Lists.newArrayList();
for (Expression usingColumn : unboundHashJoinConjunct) {
ExpressionAnalyzer leftExprAnalyzer = new ExpressionAnalyzer(
Expand All @@ -937,15 +939,61 @@ private LogicalPlan bindUsingJoin(MatchingContext<LogicalUsingJoin<Plan, Plan>>
ExpressionAnalyzer rightExprAnalyzer = new ExpressionAnalyzer(
using, rightScope, cascadesContext, true, false);
Expression usingRightSlot = rightExprAnalyzer.analyze(usingColumn, rewriteContext);
leftConjunctsSlots.add((Slot) usingLeftSlot);
rightConjunctsSlots.add((Slot) usingRightSlot);
hashEqExprs.add(new EqualTo(usingLeftSlot, usingRightSlot));
}

return new LogicalJoin<>(
using.getJoinType() == JoinType.CROSS_JOIN ? JoinType.INNER_JOIN : using.getJoinType(),
hashEqExprs.build(), using.getMatchCondition().map(ImmutableList::of).orElse(ImmutableList.of()),
using.getDistributeHint(), Optional.empty(), rightConjunctsSlots,
using.children(), null);
JoinType joinType = using.getJoinType() == JoinType.CROSS_JOIN
? JoinType.INNER_JOIN : using.getJoinType();
LogicalJoin<Plan, Plan> join = new LogicalJoin<>(joinType,
hashEqExprs.build(), using.getMatchCondition().map(ImmutableList::of).orElse(ImmutableList.of()),
using.getDistributeHint(), Optional.empty(),
using.children(), null);

// Build Project on top of join with correct merge key semantics
List<Slot> joinOutput = join.getOutput();
Set<Slot> leftUsingSlotSet = ImmutableSet.copyOf(leftConjunctsSlots);
Set<Slot> rightUsingSlotSet = ImmutableSet.copyOf(rightConjunctsSlots);

// Build project expressions: merged key(s) + all join output columns
ImmutableList.Builder<NamedExpression> projectExprs = ImmutableList.builder();
ImmutableList.Builder<NamedExpression> asteriskOutputs = ImmutableList.builder();

// 1. Add merged key columns (without qualifier)
for (int i = 0; i < leftConjunctsSlots.size(); i++) {
Slot leftSlot = leftConjunctsSlots.get(i);
Slot rightSlot = rightConjunctsSlots.get(i);
String colName = leftSlot.getName();
NamedExpression mergeExpr;
if (joinType.isFullOuterJoin()) {
// FULL OUTER JOIN: COALESCE(left, right)
mergeExpr = new Alias(new Coalesce(leftSlot, rightSlot), colName);
} else if (joinType.isRightJoin() || joinType.isRightSemiOrAntiJoin()) {
// RIGHT OUTER / RIGHT SEMI / RIGHT ANTI: use right side (preserved side)
mergeExpr = new Alias(rightSlot, colName);
} else {
// LEFT OUTER / INNER / LEFT SEMI / LEFT ANTI / CROSS: use left side
mergeExpr = new Alias(leftSlot, colName);
}
projectExprs.add(mergeExpr);
asteriskOutputs.add(mergeExpr);
}

// 2. Pass through all join output columns (with original qualifiers)
for (Slot slot : joinOutput) {
projectExprs.add(slot);
}

// 3. Build asterisk output: merged USING keys + child asterisk output without current USING keys.
for (Slot slot : join.getAsteriskOutput()) {
if (!leftUsingSlotSet.contains(slot) && !rightUsingSlotSet.contains(slot)) {
asteriskOutputs.add(slot);
}
}

return new LogicalProject<>(projectExprs.build(), false,
asteriskOutputs.build(), ImmutableList.of(join));
}

private Plan bindProject(MatchingContext<LogicalProject<Plan>> ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public Expression visitUnboundStar(UnboundStar unboundStar, ExpressionRewriteCon
List<String> qualifier = unboundStar.getQualifier();
boolean showHidden = Util.showHiddenColumns();

List<Slot> scopeSlots = getScope().getAsteriskSlots();
List<Slot> scopeSlots = qualifier.isEmpty() ? getScope().getAsteriskSlots() : getScope().getSlots();
ImmutableList.Builder<Slot> showSlots = ImmutableList.builderWithExpectedSize(scopeSlots.size());
for (Slot slot : scopeSlots) {
if (!(slot instanceof SlotReference) || (((SlotReference) slot).isVisible()) || showHidden) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ public Plan visitLogicalProject(LogicalProject<? extends Plan> project, DeepCopi
List<NamedExpression> newProjects = project.getProjects().stream()
.map(p -> (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(p, context))
.collect(ImmutableList.toImmutableList());
return new LogicalProject<>(newProjects, project.isDistinct(), child);
List<NamedExpression> newAsteriskOutputs = project.getAsteriskOutputs().stream()
.map(p -> (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(p, context))
.collect(ImmutableList.toImmutableList());
return new LogicalProject<>(newProjects, project.isDistinct(), newAsteriskOutputs, child);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
public interface DiffOutputInAsterisk extends Plan {
@Override
default List<Slot> getAsteriskOutput() {
boolean outputMayDiff = false;
for (Plan child : children()) {
if (child instanceof DiffOutputInAsterisk) {
outputMayDiff = true;
break;
}
}
return outputMayDiff ? getLogicalProperties().getAsteriskOutput() : getLogicalProperties().getOutput();
return getLogicalProperties().getAsteriskOutput();
}
}
Loading
Loading