Skip to content

Commit 637e627

Browse files
authored
Merge branch 'main' into draftwindows
2 parents 7d2db37 + d580805 commit 637e627

19 files changed

+3461
-3341
lines changed

axiom/optimizer/DerivedTable.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ PlanObjectCP singleTable(PlanObjectCP object) {
3030
return nullptr;
3131
}
3232

33-
} // namespace
34-
35-
void DerivedTable::addJoinEquality(ExprCP left, ExprCP right) {
33+
// Adds an equijoin edge between 'left' and 'right'.
34+
void addJoinEquality(ExprCP left, ExprCP right, JoinEdgeVector& joins) {
3635
auto leftTable = singleTable(left);
3736
auto rightTable = singleTable(right);
37+
38+
VELOX_CHECK_NOT_NULL(leftTable);
39+
VELOX_CHECK_NOT_NULL(rightTable);
40+
VELOX_CHECK(leftTable != rightTable);
41+
3842
for (auto& join : joins) {
3943
if (join->leftTable() == leftTable && join->rightTable() == rightTable) {
4044
join->addEquality(left, right);
@@ -52,26 +56,20 @@ void DerivedTable::addJoinEquality(ExprCP left, ExprCP right) {
5256
joins.push_back(join);
5357
}
5458

55-
namespace {
56-
59+
// Set of pairs of column IDs. Each pair represents a join equality condition.
60+
// Pairs are canonicalized so that first ID is < second ID.
5761
using EdgeSet = folly::F14FastSet<std::pair<int32_t, int32_t>>;
5862

59-
void addEdge(EdgeSet& edges, int32_t id1, int32_t id2) {
60-
if (id1 > id2) {
61-
edges.insert(std::pair<int32_t, int32_t>(id2, id1));
62-
} else {
63-
edges.insert(std::pair<int32_t, int32_t>(id1, id2));
63+
bool addEdge(EdgeSet& edges, PlanObjectCP left, PlanObjectCP right) {
64+
if (left->id() == right->id()) {
65+
return false;
6466
}
65-
}
6667

67-
bool hasEdge(const EdgeSet& edges, int32_t id1, int32_t id2) {
68-
if (id1 == id2) {
69-
return true;
68+
if (left->id() < right->id()) {
69+
return edges.emplace(left->id(), right->id()).second;
70+
} else {
71+
return edges.emplace(right->id(), left->id()).second;
7072
}
71-
auto it = edges.find(
72-
id1 > id2 ? std::pair<int32_t, int32_t>(id2, id1)
73-
: std::pair<int32_t, int32_t>(id1, id2));
74-
return it != edges.end();
7573
}
7674

7775
void fillJoins(
@@ -80,9 +78,8 @@ void fillJoins(
8078
EdgeSet& edges,
8179
DerivedTableP dt) {
8280
for (auto& other : equivalence.columns) {
83-
if (!hasEdge(edges, column->id(), other->id())) {
84-
addEdge(edges, column->id(), other->id());
85-
dt->addJoinEquality(column->as<Column>(), other->as<Column>());
81+
if (addEdge(edges, column, other)) {
82+
addJoinEquality(column->as<Column>(), other->as<Column>(), dt->joins);
8683
}
8784
}
8885
}
@@ -96,7 +93,7 @@ void DerivedTable::addImpliedJoins() {
9693
const auto* leftKey = join->leftKeys()[i];
9794
const auto* rightKey = join->rightKeys()[i];
9895
if (leftKey->isColumn() && rightKey->isColumn()) {
99-
addEdge(edges, leftKey->id(), rightKey->id());
96+
addEdge(edges, leftKey, rightKey);
10097
}
10198
}
10299
}

axiom/optimizer/DerivedTable.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,18 @@ struct DerivedTable : public PlanObject {
156156
// Write.
157157
WritePlanCP write{nullptr};
158158

159-
/// Adds an equijoin edge between 'left' and 'right'.
160-
void addJoinEquality(ExprCP left, ExprCP right);
159+
/// Moves suitable elements of 'conjuncts' into join edges or single
160+
/// table filters. May be called repeatedly if enclosing dt's add
161+
/// more conjuncts. May call itself recursively on component dts.
162+
void distributeConjuncts();
163+
164+
/// Completes 'joins' with edges implied by column equivalences.
165+
void addImpliedJoins();
161166

162167
/// After 'joins' is filled in, links tables to their direct and
163168
/// equivalence-implied joins.
164169
void linkTablesToJoins();
165170

166-
/// Completes 'joins' with edges implied by column equivalences.
167-
void addImpliedJoins();
168-
169171
/// Extracts implied conjuncts and removes duplicates from
170172
/// 'conjuncts' and updates 'conjuncts'. Extracted conjuncts may
171173
/// allow extra pushdown or allow create join edges. May be called
@@ -229,11 +231,6 @@ struct DerivedTable : public PlanObject {
229231

230232
void addJoinedBy(JoinEdgeP join);
231233

232-
/// Moves suitable elements of 'conjuncts' into join edges or single
233-
/// table filters. May be called repeatedly if enclosing dt's add
234-
/// more conjuncts. May call itself recursively on component dts.
235-
void distributeConjuncts();
236-
237234
/// Memoizes plans for 'this' and fills in 'distribution_'. Needed
238235
/// before adding 'this' as a join side because join sides must have
239236
/// a cardinality guess.

axiom/optimizer/JoinSample.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ std::shared_ptr<runner::Runner> prepareSampleRunner(
130130

131131
auto columns = sampleColumns.toObjects<Column>();
132132
auto index = base->chooseLeafIndex()[0];
133-
auto* scan = make<TableScan>(
134-
nullptr,
135-
TableScan::outputDistribution(base, index, columns),
136-
base,
137-
index,
138-
index->table->cardinality,
139-
columns);
133+
auto* scan = make<TableScan>(base, index, columns);
140134

141135
ExprVector hashes;
142136
hashes.reserve(keys.size());

axiom/optimizer/Optimization.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,10 @@ void Optimization::joinByIndex(
982982
// The number of keys is the prefix that matches index order.
983983
lookupKeys.resize(info.lookupKeys.size());
984984
state.columns.unionSet(availableColumns(rightTable, index));
985+
985986
auto c = state.downstreamColumns();
986987
c.intersect(state.columns);
987-
for (auto& filter : rightTable->filter) {
988-
c.unionSet(filter->columns());
989-
}
988+
c.unionColumns(rightTable->filter);
990989

991990
auto* scan = make<TableScan>(
992991
newPartition,
@@ -1102,8 +1101,7 @@ void Optimization::joinByHash(
11021101
auto buildKeys = precomputeBuild.toColumns(build.keys);
11031102
buildInput = std::move(precomputeBuild).maybeProject();
11041103

1105-
auto* buildOp =
1106-
make<HashBuild>(buildInput, ++buildCounter_, build.keys, buildPlan);
1104+
auto* buildOp = make<HashBuild>(buildInput, build.keys, buildPlan);
11071105
buildState.addCost(*buildOp);
11081106

11091107
const auto joinType = build.leftJoinType();
@@ -1222,8 +1220,7 @@ void Optimization::joinByHashRight(
12221220
auto buildKeys = precomputeBuild.toColumns(build.keys);
12231221
buildInput = std::move(precomputeBuild).maybeProject();
12241222

1225-
auto* buildOp =
1226-
make<HashBuild>(buildInput, ++buildCounter_, build.keys, nullptr);
1223+
auto* buildOp = make<HashBuild>(buildInput, build.keys, nullptr);
12271224
state.addCost(*buildOp);
12281225

12291226
PlanObjectSet buildColumns;
@@ -1756,15 +1753,7 @@ void Optimization::makeJoins(PlanState& state) {
17561753
state.placed.add(table);
17571754
state.columns.unionObjects(columns);
17581755

1759-
auto distribution =
1760-
TableScan::outputDistribution(table, index, columns);
1761-
auto* scan = make<TableScan>(
1762-
nullptr,
1763-
std::move(distribution),
1764-
table,
1765-
index,
1766-
index->table->cardinality * table->filterSelectivity,
1767-
std::move(columns));
1756+
auto* scan = make<TableScan>(table, index, columns);
17681757
state.addCost(*scan);
17691758
makeJoins(scan, state);
17701759
}

axiom/optimizer/Optimization.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,6 @@ class Optimization {
340340
// Controls tracing.
341341
int32_t traceFlags_{0};
342342

343-
// Generates unique ids for build sides.
344-
int32_t buildCounter_{0};
345-
346343
bool cnamesInExpr_{true};
347344

348345
ToGraph toGraph_;

axiom/optimizer/RelationOp.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ const auto& relTypeNames() {
5151

5252
AXIOM_DEFINE_ENUM_NAME(RelType, relTypeNames)
5353

54-
const Value& RelationOp::value(ExprCP expr) const {
55-
// Compute new Value by applying restrictions from operators
56-
// between the place Expr is first defined and the output of
57-
// 'this'. Memoize the result in 'this'.
58-
return expr->value();
59-
}
60-
6154
namespace {
6255
template <typename T>
6356
std::string itemsToString(const T* items, size_t n) {
@@ -106,10 +99,25 @@ float orderPrefixDistance(
10699

107100
} // namespace
108101

102+
TableScan::TableScan(
103+
BaseTableCP table,
104+
ColumnGroupCP index,
105+
const ColumnVector& columns)
106+
: TableScan(
107+
/*input=*/nullptr,
108+
TableScan::outputDistribution(table, index, columns),
109+
table,
110+
index,
111+
/*fanout=*/index->table->cardinality * table->filterSelectivity,
112+
columns,
113+
/*lookupKeys=*/{},
114+
velox::core::JoinType::kInner,
115+
/*joinFilter=*/{}) {}
116+
109117
TableScan::TableScan(
110118
RelationOpPtr input,
111119
Distribution distribution,
112-
const BaseTable* table,
120+
BaseTableCP table,
113121
ColumnGroupCP index,
114122
float fanout,
115123
ColumnVector columns,
@@ -676,13 +684,8 @@ void Aggregation::accept(
676684
visitor.visit(*this, context);
677685
}
678686

679-
HashBuild::HashBuild(
680-
RelationOpPtr input,
681-
int32_t id,
682-
ExprVector keysVector,
683-
PlanP plan)
687+
HashBuild::HashBuild(RelationOpPtr input, ExprVector keysVector, PlanP plan)
684688
: RelationOp{RelType::kHashBuild, std::move(input)},
685-
buildId{id},
686689
keys{std::move(keysVector)},
687690
plan{plan} {
688691
cost_.inputCardinality = inputCardinality();

0 commit comments

Comments
 (0)