Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower threshold for MapJoin for Scale #9664

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
82 changes: 47 additions & 35 deletions ydb/core/kqp/opt/kqp_statistics_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,42 +360,14 @@ class TKqpOlapPredicateSelectivityComputer: public TPredicateSelectivityComputer
resSelectivity = tmpSelectivity;
} else if (auto notNode = input.Maybe<TKqpOlapNot>()) {
resSelectivity = 1 - Compute(notNode.Cast().Value());
} else if (input.Maybe<TCoAtomList>() && input.Ptr()->ChildrenSize() >= 1) {
auto listPtr = input.Maybe<TCoAtomList>().Cast().Ptr()->Child(1);
size_t listSize = listPtr->ChildrenSize();

if (listSize == 3) {
TString compSign = TString(listPtr->Child(0)->Content());
TString attr = TString(listPtr->Child(1)->Content());

TExprContext dummyCtx;
TPositionHandle dummyPos;

auto rowArg =
Build<TCoArgument>(dummyCtx, dummyPos)
.Name("row")
.Done();

auto member =
Build<TCoMember>(dummyCtx, dummyPos)
.Struct(rowArg)
.Name().Build(attr)
.Done();

auto value = TExprBase(listPtr->ChildPtr(2));
if (listPtr->ChildPtr(2)->ChildrenSize() >= 2 && listPtr->ChildPtr(2)->ChildPtr(0)->Content() == "just") {
value = TExprBase(listPtr->ChildPtr(2)->ChildPtr(1));
}
if (OlapCompSigns.contains(compSign)) {
resSelectivity = this->ComputeComparisonSelectivity(member, value);
} else if (compSign == "eq") {
resSelectivity = this->ComputeEqualitySelectivity(member, value);
} else if (compSign == "neq") {
resSelectivity = 1 - this->ComputeEqualitySelectivity(member, value);
} else if (RegexpSigns.contains(compSign)) {
return 0.5;
}
} else if (input.Maybe<TCoAtomList>()) {
auto list = input.Maybe<TCoAtomList>().Cast().Ptr();
resSelectivity = ComputeListSelectivity(list);

if (!resSelectivity.has_value() && list->ChildrenSize() >= 1) {
resSelectivity = ComputeListSelectivity(list->Child(1));
}

}

if (!resSelectivity.has_value()) {
Expand All @@ -408,6 +380,46 @@ class TKqpOlapPredicateSelectivityComputer: public TPredicateSelectivityComputer
}

private:
std::optional<double> ComputeListSelectivity(const TExprNode::TPtr& listPtr) {
std::optional<double> resSelectivity;

size_t listSize = listPtr->ChildrenSize();
if (listSize == 3) {
TString compSign = TString(listPtr->Child(0)->Content());
TString attr = TString(listPtr->Child(1)->Content());

TExprContext dummyCtx;
TPositionHandle dummyPos;

auto rowArg =
Build<TCoArgument>(dummyCtx, dummyPos)
.Name("row")
.Done();

auto member =
Build<TCoMember>(dummyCtx, dummyPos)
.Struct(rowArg)
.Name().Build(attr)
.Done();

auto value = TExprBase(listPtr->ChildPtr(2));
if (listPtr->ChildPtr(2)->ChildrenSize() >= 2 && listPtr->ChildPtr(2)->ChildPtr(0)->Content() == "just") {
value = TExprBase(listPtr->ChildPtr(2)->ChildPtr(1));
}
if (OlapCompSigns.contains(compSign)) {
resSelectivity = this->ComputeComparisonSelectivity(member, value);
} else if (compSign == "eq") {
resSelectivity = this->ComputeEqualitySelectivity(member, value);
} else if (compSign == "neq") {
resSelectivity = 1 - this->ComputeEqualitySelectivity(member, value);
} else if (RegexpSigns.contains(compSign)) {
return 0.5;
}
}

return resSelectivity;
}

THashSet<TString> OlapCompSigns = {
{"lt"},
{"lte"},
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/kqp/opt/logical/kqp_opt_cbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool TKqpProviderContext::IsJoinApplicable(const std::shared_ptr<IBaseOptimizerN
return IsLookupJoinApplicable(right, left, joinConditions, rightJoinKeys, leftJoinKeys, *this);

case EJoinAlgoType::MapJoin:
return joinKind != EJoinKind::OuterJoin && joinKind != EJoinKind::Exclusion && right->Stats->ByteSize < 1e8;
return joinKind != EJoinKind::OuterJoin && joinKind != EJoinKind::Exclusion && right->Stats->ByteSize < 1e5;
case EJoinAlgoType::GraceJoin:
return true;
default:
Expand Down
46 changes: 46 additions & 0 deletions ydb/core/kqp/ut/join/data/queries/tpch20.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
PRAGMA TablePathPrefix="/Root";

-- TPC-H/TPC-R Potential Part Promotion Query (Q20)
-- TPC TPC-H Parameter Substitution (Version 2.17.2 build 0)
-- using 1680793381 as a seed to the RNG


select
s_name,
s_address
from
supplier
cross join nation
cross join (
select
ps_suppkey
from
partsupp
cross join part
cross join (
select
l_partkey,
l_suppkey,
0.5 * sum(l_quantity) as q_threshold
from
lineitem
where
l_shipdate >= cast(date('1993-01-01') as Datetime)
and l_shipdate < cast(date('1993-01-01') as Datetime) + interval('P365D')
group by
l_partkey,
l_suppkey
) as threshold
where
ps_partkey = p_partkey
and ps_partkey = l_partkey
and ps_suppkey = l_suppkey
and p_name like 'maroon%'
and ps_availqty > threshold.q_threshold
) as partsupp
where
s_suppkey = ps_suppkey
and s_nationkey = n_nationkey
and n_name = 'VIETNAM'
order by
s_name;
2 changes: 1 addition & 1 deletion ydb/core/kqp/ut/join/data/schema/tpch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CREATE TABLE `/Root/lineitem` (

CREATE TABLE `/Root/nation` (
n_comment String ,
n_name String ,
n_name String NOT NULL,
n_nationkey Int32 NOT NULL, -- Identifier
n_regionkey Int32 , -- FK to R_REGIONKEY
PRIMARY KEY(n_nationkey)
Expand Down
68 changes: 68 additions & 0 deletions ydb/core/kqp/ut/join/data/stats/tpch100s_no_countmin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"/Root/customer": {
"byte_size": 1578400481,
"n_rows": 15000000,
"n_attrs": 8,
"key_columns": [
"c_custkey"
]
},
"/Root/lineitem": {
"byte_size": 41682657607,
"n_rows": 600037902,
"n_attrs": 16,
"key_columns": [
"l_linenumber",
"l_orderkey"
]
},
"/Root/nation": {
"byte_size": 3758,
"n_rows": 25,
"n_attrs": 4,
"key_columns": [
"n_nationkey"
]
},
"/Root/orders": {
"byte_size": 9159017758,
"n_rows": 150000000,
"n_attrs": 9,
"key_columns": [
"o_orderkey"
]
},
"/Root/part": {
"byte_size": 1460288880,
"n_rows": 20000000,
"n_attrs": 9,
"key_columns": [
"p_partkey"
]
},
"/Root/partsupp": {
"byte_size": 5068061409,
"n_rows": 80000000,
"n_attrs": 5,
"key_columns": [
"ps_partkey",
"ps_suppkey"
]
},
"/Root/region": {
"byte_size": 1280,
"n_rows": 5,
"n_attrs": 3,
"key_columns": [
"r_regionkey"
]
},
"/Root/supplier": {
"byte_size": 95577702,
"n_rows": 1000000,
"n_attrs": 7,
"key_columns": [
"s_suppkey"
]
}
}
4 changes: 4 additions & 0 deletions ydb/core/kqp/ut/join/kqp_join_order_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ Y_UNIT_TEST_SUITE(KqpJoinOrder) {
ExecuteJoinOrderTestDataQueryWithStats("queries/tpch11.sql", "stats/tpch1000s.json", StreamLookupJoin, ColumnStore);
}

Y_UNIT_TEST_XOR_OR_BOTH_FALSE(TPCH20, StreamLookupJoin, ColumnStore) {
ExecuteJoinOrderTestDataQueryWithStats("queries/tpch20.sql", "stats/tpch100s_no_countmin.json", StreamLookupJoin, ColumnStore);
}

Y_UNIT_TEST_XOR_OR_BOTH_FALSE(TPCH21, StreamLookupJoin, ColumnStore) {
ExecuteJoinOrderTestDataQueryWithStats("queries/tpch21.sql", "stats/tpch1000s.json", StreamLookupJoin, ColumnStore);
}
Expand Down
Loading