Skip to content

Commit

Permalink
[CBO] Hint syntax fix (#9875)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashandor789 authored Sep 30, 2024
1 parent ea6dfcc commit 6e2ebb2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
14 changes: 7 additions & 7 deletions ydb/core/kqp/ut/join/data/queries/join_order_hints_complex.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ PRAGMA TablePathPrefix='/Root';

PRAGMA ydb.OptimizerHints =
'
Card(Unused # 10e8)
Rows(Unused # 10e8)
JoinOrder( (Unused1 Unused2) (Unused3 Unused4) )
Card(R # 10e8)
Card(T # 1)
Card(R T # 1)
Card(R S # 10e8)
Card(T U # 10e8)
Card(V # 1)
Rows(R # 10e8)
Rows(T # 1)
Rows(R T # 1)
Rows(R S # 10e8)
Rows(T U # 10e8)
Rows(V # 1)
JoinOrder( (R S) (T U) )
';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ PRAGMA TablePathPrefix='/Root';

PRAGMA ydb.OptimizerHints =
'
Card(R # 10e8)
Card(T # 1)
Card(R T # 1)
Card(R S # 10e8)
Card(T U # 10e8)
Card(V # 1)
Rows(R # 10e8)
Rows(T # 1)
Rows(R T # 1)
Rows(R S # 10e8)
Rows(T U # 10e8)
Rows(V # 1)
JoinOrder(T U)
JoinOrder(R S)
';
Expand Down
10 changes: 5 additions & 5 deletions ydb/core/kqp/ut/join/data/queries/join_order_hints_simple.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ PRAGMA TablePathPrefix='/Root';

PRAGMA ydb.OptimizerHints =
'
Card(R # 10e8)
Card(T # 1)
Card(S # 10e8)
Card(R T # 1)
Card(R S # 10e8)
Rows(R # 10e8)
Rows(T # 1)
Rows(S # 10e8)
Rows(R T # 1)
Rows(R S # 10e8)
JoinOrder(T (R S))
';

Expand Down
6 changes: 3 additions & 3 deletions ydb/core/kqp/ut/join/data/queries/test_join_hint.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PRAGMA ydb.OptimizerHints =
'
JoinAlgo(R S Grace)
Card(R # 1)
Card(R S # 10e6)
JoinType(R S Shuffle)
Rows(R # 1)
Rows(R S # 10e6)
';

SELECT *
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/kqp/ut/join/data/queries/test_join_hint2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRAGMA ydb.OptimizerHints = 'Card(R S # 1)';
PRAGMA ydb.OptimizerHints = 'Rows(R S # 1)';


SELECT *
Expand Down
37 changes: 20 additions & 17 deletions ydb/library/yql/core/cbo/cbo_hints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class TOptimizerHintsParser {
private:
void Start() {
while (Pos < Size) {
auto hintType = Keyword({"JoinOrder", "JoinAlgo", "Card"});
if (hintType == "JoinOrder") {
JoinOrder();
} else if (hintType == "JoinAlgo") {
JoinAlgo();
} else if (hintType == "Card"){
Card();
auto hintType = Keyword({"JoinOrder", "Leading", "JoinType", "Rows"});
if (hintType == "JoinOrder" || hintType == "Leading") {
JoinOrder(hintType == "Leading");
} else if (hintType == "JoinType") {
JoinType();
} else if (hintType == "Rows"){
Rows();
} else {
ParseError(Sprintf("Undefined hints type: %s", hintType.c_str()), Pos - hintType.Size());
}
Expand All @@ -45,43 +45,46 @@ class TOptimizerHintsParser {
return labels;
}

void JoinAlgo() {
void JoinType() {
i32 beginPos = Pos + 1;

Keyword({"("});

i32 labelsBeginPos = Pos + 1;
TVector<TString> labels = CollectLabels();
if (labels.size() <= 1) {
ParseError(Sprintf("Bad labels for JoinAlgo hint: %s, example of the format: JoinAlgo(t1 t2 Grace)", JoinSeq(", ", labels).c_str()), labelsBeginPos);
ParseError(Sprintf("Bad labels for JoinType hint: %s, example of the format: JoinType(t1 t2 Shuffle)", JoinSeq(", ", labels).c_str()), labelsBeginPos);
}
TString reqJoinAlgoStr = std::move(labels.back());
labels.pop_back();

Keyword({")"});

TVector<EJoinAlgoType> joinAlgos = {EJoinAlgoType::GraceJoin, EJoinAlgoType::LookupJoin, EJoinAlgoType::MapJoin};
TVector<TString> joinAlgosStr = {"Grace", "Lookup", "Map"};
TVector<TString> joinAlgosStr = {"Shuffle", "Lookup", "Broadcast"};

for (const auto& [joinAlgo, joinAlgoStr]: Zip(joinAlgos, joinAlgosStr)) {
for (const auto& [JoinType, joinAlgoStr]: Zip(joinAlgos, joinAlgosStr)) {
if (reqJoinAlgoStr == joinAlgoStr) {
Hints.JoinAlgoHints->PushBack(std::move(labels), joinAlgo, "JoinAlgo" + Text.substr(beginPos, Pos - beginPos + 1));
Hints.JoinAlgoHints->PushBack(std::move(labels), JoinType, "JoinType" + Text.substr(beginPos, Pos - beginPos + 1));
return;
}
}

ParseError(Sprintf("Unknown JoinAlgo: '%s', supported algos: [%s]", reqJoinAlgoStr.c_str(), JoinSeq(", ", joinAlgosStr).c_str()), Pos - reqJoinAlgoStr.Size());
ParseError(Sprintf("Unknown JoinType: '%s', supported algos: [%s]", reqJoinAlgoStr.c_str(), JoinSeq(", ", joinAlgosStr).c_str()), Pos - reqJoinAlgoStr.Size());
Y_UNREACHABLE();
}

void JoinOrder() {
void JoinOrder(bool leading /* is keyword "Leading" or "JoinOrder" */) {
i32 beginPos = Pos + 1;

Keyword({"("});
auto joinOrderHintTree = JoinOrderLabels();
Keyword({")"});

Hints.JoinOrderHints->PushBack(std::move(joinOrderHintTree), "JoinOrder" + Text.substr(beginPos, Pos - beginPos + 1));
Hints.JoinOrderHints->PushBack(
std::move(joinOrderHintTree),
leading? "Leading" : "JoinOrder" + Text.substr(beginPos, Pos - beginPos + 1)
);
}

std::shared_ptr<TJoinOrderHints::ITreeNode> JoinOrderLabels() {
Expand All @@ -103,7 +106,7 @@ class TOptimizerHintsParser {
Y_UNREACHABLE();
}

void Card() {
void Rows() {
i32 beginPos = Pos + 1;

Keyword({"("});
Expand All @@ -124,7 +127,7 @@ class TOptimizerHintsParser {
default: {ParseError(Sprintf("Unknown operation: '%c'", sign), Pos - 1); Y_UNREACHABLE();}
}

Hints.CardinalityHints->PushBack(std::move(labels), op, value, "Card" + Text.substr(beginPos, Pos - beginPos + 1));
Hints.CardinalityHints->PushBack(std::move(labels), op, value, "Rows" + Text.substr(beginPos, Pos - beginPos + 1));
}

private:
Expand Down

0 comments on commit 6e2ebb2

Please sign in to comment.