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 @@ -17,7 +17,6 @@

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.catalog.constraint.TableIdentifier;
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
Expand Down Expand Up @@ -577,14 +576,14 @@ boolean isNotSupported(Plan plan) {
}

boolean comparePlan(Plan plan1, Plan plan2) {
if (plan1.getOutput().size() != plan2.getOutput().size()) {
return false;
}
boolean isEqual = true;
if (plan1 instanceof LogicalCatalogRelation && plan2 instanceof LogicalCatalogRelation) {
isEqual = new TableIdentifier(((LogicalCatalogRelation) plan1).getTable())
.equals(new TableIdentifier(((LogicalCatalogRelation) plan2).getTable()));
isEqual = ((LogicalCatalogRelation) plan1)
.hasSameScanSemantics((LogicalCatalogRelation) plan2);
} else if (plan1 instanceof LogicalProject && plan2 instanceof LogicalProject) {
if (plan1.getOutput().size() != plan2.getOutput().size()) {
isEqual = false;
}
for (int i = 0; isEqual && i < plan2.getOutput().size(); i++) {
Expression expr1 = ((LogicalProject<?>) plan1).getProjects().get(i);
Expression expr2 = ((LogicalProject<?>) plan2).getProjects().get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.constraint.PrimaryKeyConstraint;
import org.apache.doris.catalog.constraint.TableIdentifier;
import org.apache.doris.catalog.constraint.UniqueConstraint;
import org.apache.doris.common.IdGenerator;
import org.apache.doris.common.util.Util;
Expand Down Expand Up @@ -250,6 +251,47 @@ public LogicalCatalogRelation withVirtualColumns(List<NamedExpression> virtualCo
return this;
}

/** Compare whether two catalog relations read the same data with the same output semantics. */
public final boolean hasSameScanSemantics(LogicalCatalogRelation other) {
if (other == null || getClass() != other.getClass()) {
return false;
}
if (!hasSameTableIdentity(other)) {
return false;
}
if (getOutput().size() != other.getOutput().size()) {
return false;
}
for (int i = 0; i < getOutput().size(); i++) {
if (!hasSameOutputSlotSemantics(getOutput().get(i), other.getOutput().get(i))) {
return false;
}
}
return hasSameScanState(other);
}

protected boolean hasSameTableIdentity(LogicalCatalogRelation other) {
if (!Utils.isSameClass(this, other)) {
return false;
}
return new TableIdentifier(table).equals(new TableIdentifier(other.table));
}

protected boolean hasSameScanState(LogicalCatalogRelation other) {
return false;
}

private boolean hasSameOutputSlotSemantics(Slot left, Slot right) {
if (!(left instanceof SlotReference) || !(right instanceof SlotReference)) {
return false;
}
SlotReference leftSlot = (SlotReference) left;
SlotReference rightSlot = (SlotReference) right;
return Objects.equals(left.getClass(), right.getClass())
&& Objects.equals(leftSlot.getName(), rightSlot.getName())
&& Objects.equals(leftSlot.getSubPath(), rightSlot.getSubPath());
}

public abstract LogicalCatalogRelation withRelationId(RelationId relationId);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ public boolean equals(Object o) {
return super.equals(o) && Objects.equals(selectedPartitions, ((LogicalFileScan) o).selectedPartitions);
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
if (!Utils.isSameClass(this, other)) {
return false;
}
LogicalFileScan that = (LogicalFileScan) other;
return Objects.equals(selectedPartitions, that.selectedPartitions)
&& Objects.equals(tableSample, that.tableSample)
&& hasSameSnapshot(tableSnapshot, that.tableSnapshot)
&& hasSameScanParams(scanParams, that.scanParams);
}

@Override
public List<Slot> computeOutput() {
if (cachedOutputs.isPresent()) {
Expand Down Expand Up @@ -209,6 +221,23 @@ public boolean supportPruneNestedColumn() {
return false;
}

private boolean hasSameSnapshot(Optional<TableSnapshot> left, Optional<TableSnapshot> right) {
if (!left.isPresent() || !right.isPresent()) {
return left.isPresent() == right.isPresent();
}
return left.get().getType() == right.get().getType()
&& Objects.equals(left.get().getValue(), right.get().getValue());
}

private boolean hasSameScanParams(Optional<TableScanParams> left, Optional<TableScanParams> right) {
if (!left.isPresent() || !right.isPresent()) {
return left.isPresent() == right.isPresent();
}
return Objects.equals(left.get().getParamType(), right.get().getParamType())
&& Objects.equals(left.get().getMapParams(), right.get().getMapParams())
&& Objects.equals(left.get().getListParams(), right.get().getListParams());
}

/**
* SelectedPartitions contains the selected partitions and the total partition number.
* Mainly for hive table partition pruning.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public Optional<IncrementalRelation> getIncrementalRelation() {
return incrementalRelation;
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
if (!Utils.isSameClass(this, other)) {
return false;
}
LogicalHudiScan that = (LogicalHudiScan) other;
// IncrementalRelation contains the resolved Hudi timeline and split state and has no value equality.
return super.hasSameScanState(other) && Objects.equals(incrementalRelation, that.incrementalRelation);
}

/**
* replace incremental params as AND expression
* incr('beginTime'='20240308110257169', 'endTime'='20240308110677278') =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public LogicalOdbcScan withRelationId(RelationId relationId) {
return new LogicalOdbcScan(relationId, table, qualifier, Optional.empty(), Optional.empty());
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
return Utils.isSameClass(this, other);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitLogicalOdbcScan(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,21 @@ public LogicalOlapScan withCachedOutput(List<Slot> outputSlots) {
scoreRangeInfo, annOrderKeys, annLimit, partitionPrunablePredicates);
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
if (!Utils.isSameClass(this, other)) {
return false;
}
LogicalOlapScan that = (LogicalOlapScan) other;
return selectedIndexId == that.selectedIndexId
&& indexSelected == that.indexSelected
&& Objects.equals(selectedPartitionIds, that.selectedPartitionIds)
&& Objects.equals(manuallySpecifiedPartitions, that.manuallySpecifiedPartitions)
&& Objects.equals(selectedTabletIds, that.selectedTabletIds)
&& Objects.equals(manuallySpecifiedTabletIds, that.manuallySpecifiedTabletIds)
&& Objects.equals(tableSample, that.tableSample);
}

@Override
public boolean supportPruneNestedColumn() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public List<Expression> getFrontendConjuncts() {
return frontendConjuncts;
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
if (!Utils.isSameClass(this, other)) {
return false;
}
LogicalSchemaScan that = (LogicalSchemaScan) other;
return filterPushed == that.filterPushed
&& Objects.equals(schemaCatalog, that.schemaCatalog)
&& Objects.equals(schemaDatabase, that.schemaDatabase)
&& Objects.equals(schemaTable, that.schemaTable)
&& Objects.equals(frontendConjuncts, that.frontendConjuncts);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitLogicalSchemaScan(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public Plan withGroupExprLogicalPropChildren(Optional<GroupExpression> groupExpr
return new LogicalTestScan(relationId, table, qualifier, groupExpression, logicalProperties);
}

@Override
protected boolean hasSameScanState(LogicalCatalogRelation other) {
return Utils.isSameClass(this, other);
}

@Override
public LogicalTestScan withRelationId(RelationId relationId) {
throw new RuntimeException("should not call LogicalTestScan's withRelationId method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
public class Utils {
public static final boolean enableAssert;

/**
* Check whether two objects are non-null and have the same concrete class.
*/
public static boolean isSameClass(Object left, Object right) {
return left != null && right != null && left.getClass() == right.getClass();
}

static {
boolean enabled = false;
// if run jvm with -ea or -enableassertions, the assert statement will be executed
Expand Down
Loading
Loading