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

Support CompactNamedExprs mode #5048

Merged
merged 9 commits into from
Jun 5, 2024
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
34 changes: 17 additions & 17 deletions ydb/library/yql/sql/v1/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ class TYqlCallableType final : public TCallNode {
return false;
}

if (!dynamic_cast<TTupleNode*>(Args[0].Get())) {
if (!Args[0]->GetTupleNode()) {
ui32 numOptArgs;
if (!Parseui32(Args[0], numOptArgs)) {
ctx.Error(Args[0]->GetPos()) << "Expected either tuple or number of optional arguments";
Expand All @@ -1409,12 +1409,12 @@ class TYqlCallableType final : public TCallNode {
Args[0] = Q(Y(BuildQuotedAtom(Args[0]->GetPos(), ToString(numOptArgs))));
}

if (!dynamic_cast<TTupleNode*>(Args[1].Get())) {
if (!Args[1]->GetTupleNode()) {
Args[1] = Q(Y(Args[1]));
}

for (ui32 index = 2; index < Args.size(); ++index) {
if (!dynamic_cast<TTupleNode*>(Args[index].Get())) {
if (!Args[index]->GetTupleNode()) {
Args[index] = Q(Y(Args[index]));
}
}
Expand Down Expand Up @@ -1838,7 +1838,7 @@ class TTableRow final : public INode {

bool DoInit(TContext& ctx, ISource* src) override {
if (!src || src->IsFake()) {
ctx.Error(Pos) << "TableRow requires data source";
ctx.Error(Pos) << TStringBuilder() << (Join ? "Join" : "") << "TableRow requires data source";
return false;
}

Expand Down Expand Up @@ -2300,7 +2300,7 @@ TNodePtr BuildSqlCall(TContext& ctx, TPosition pos, const TString& module, const
TVector<TNodePtr> sqlCallArgs;
sqlCallArgs.push_back(BuildQuotedAtom(pos, fullName));
if (namedArgs) {
auto tupleNodePtr = dynamic_cast<const TTupleNode*>(positionalArgs.Get());
auto tupleNodePtr = positionalArgs->GetTupleNode();
YQL_ENSURE(tupleNodePtr);
TNodePtr positionalArgsNode = new TCallNodeImpl(pos, "PositionalArgs", tupleNodePtr->Elements());
sqlCallArgs.push_back(BuildTuple(pos, { positionalArgsNode, namedArgs }));
Expand Down Expand Up @@ -2484,7 +2484,7 @@ class TScriptUdf final: public INode {
return false;
}
auto scriptStrPtr = Args.back()->GetLiteral("String");
if (scriptStrPtr && scriptStrPtr->size() > SQL_MAX_INLINE_SCRIPT_LEN) {
if (!ctx.CompactNamedExprs && scriptStrPtr && scriptStrPtr->size() > SQL_MAX_INLINE_SCRIPT_LEN) {
scriptNode = ctx.UniversalAlias("scriptudf", std::move(scriptNode));
}

Expand Down Expand Up @@ -3444,9 +3444,9 @@ TNodePtr BuildBuiltinFunc(TContext& ctx, TPosition pos, TString name, const TVec
};
};

auto structNode = dynamic_cast<TStructNode*>(args[0].Get());
auto structNode = args[0]->GetStructNode();
if (!structNode) {
if (auto callNode = dynamic_cast<TCallNode*>(args[0].Get())) {
if (auto callNode = args[0]->GetCallNode()) {
if (callNode->GetOpName() == "AsStruct") {
return BuildUdf(ctx, pos, nameSpace, name, makeUdfArgs());
}
Expand All @@ -3458,7 +3458,7 @@ TNodePtr BuildBuiltinFunc(TContext& ctx, TPosition pos, TString name, const TVec
for (const auto& item : structNode->GetExprs()) {
const auto& label = item->GetLabel();
if (label == "Entities") {
auto callNode = dynamic_cast<TCallNode*>(item.Get());
auto callNode = item->GetCallNode();
if (!callNode || callNode->GetOpName() != "AsListMayWarn") {
return new TInvalidBuiltin(pos, TStringBuilder() << name << " entities must be list of strings");
}
Expand Down Expand Up @@ -3633,14 +3633,14 @@ TNodePtr BuildBuiltinFunc(TContext& ctx, TPosition pos, TString name, const TVec
if (mustUseNamed && *mustUseNamed) {
*mustUseNamed = false;
YQL_ENSURE(args.size() == 2);
Y_DEBUG_ABORT_UNLESS(dynamic_cast<TTupleNode*>(args[0].Get()));
auto posArgs = static_cast<TTupleNode*>(args[0].Get());
Y_DEBUG_ABORT_UNLESS(args[0]->GetTupleNode());
auto posArgs = args[0]->GetTupleNode();
if (posArgs->IsEmpty()) {
if (normalizedName == "asstruct") {
return args[1];
} else {
Y_DEBUG_ABORT_UNLESS(dynamic_cast<TStructNode*>(args[1].Get()));
auto namedArgs = static_cast<TStructNode*>(args[1].Get());
Y_DEBUG_ABORT_UNLESS(args[1]->GetStructNode());
auto namedArgs = args[1]->GetStructNode();
return new TStructTypeNode(pos, namedArgs->GetExprs());
}
}
Expand All @@ -3656,9 +3656,9 @@ TNodePtr BuildBuiltinFunc(TContext& ctx, TPosition pos, TString name, const TVec
*mustUseNamed = false;
}
YQL_ENSURE(args.size() == 2);
auto posArgs = static_cast<TTupleNode*>(args[0].Get());
Y_DEBUG_ABORT_UNLESS(dynamic_cast<TTupleNode*>(args[0].Get()));
Y_DEBUG_ABORT_UNLESS(dynamic_cast<TStructNode*>(args[1].Get()));
Y_DEBUG_ABORT_UNLESS(args[0]->GetTupleNode());
Y_DEBUG_ABORT_UNLESS(args[1]->GetStructNode());
auto posArgs = args[0]->GetTupleNode();
if (posArgs->GetTupleSize() != 1) {
return new TInvalidBuiltin(pos, TStringBuilder() << "ExpandStruct requires all arguments except first to be named");
}
Expand Down Expand Up @@ -3743,7 +3743,7 @@ TNodePtr BuildBuiltinFunc(TContext& ctx, TPosition pos, TString name, const TVec

if (ns == "datetime2" && name == "Update") {
if (namedArgs) {
TStructNode* castedNamedArgs = dynamic_cast<TStructNode*>(namedArgs.Get());
TStructNode* castedNamedArgs = namedArgs->GetStructNode();
Y_DEBUG_ABORT_UNLESS(castedNamedArgs);
auto exprs = castedNamedArgs->GetExprs();
for (auto& arg : exprs) {
Expand Down
5 changes: 5 additions & 0 deletions ydb/library/yql/sql/v1/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ THashMap<TStringBuf, TPragmaField> CTX_PRAGMA_FIELDS = {
{"BlockEngineEnable", &TContext::BlockEngineEnable},
{"BlockEngineForce", &TContext::BlockEngineForce},
{"UnorderedResult", &TContext::UnorderedResult},
{"CompactNamedExprs", &TContext::CompactNamedExprs},
{"ValidateUnusedExprs", &TContext::ValidateUnusedExprs},
};

typedef TMaybe<bool> TContext::*TPragmaMaybeField;
Expand Down Expand Up @@ -413,6 +415,9 @@ const TVector<std::pair<TString, TDeferredAtom>>& TScopedState::GetUsedClusters(
TNodePtr TScopedState::WrapCluster(const TDeferredAtom& cluster, TContext& ctx) {
auto node = cluster.Build();
if (!cluster.GetLiteral()) {
if (ctx.CompactNamedExprs) {
return node->Y("EvaluateAtom", node);
nepal marked this conversation as resolved.
Show resolved Hide resolved
}
AddExprCluster(node, ctx);
auto exprIt = Local.ExprClustersMap.find(node.Get());
YQL_ENSURE(exprIt != Local.ExprClustersMap.end());
Expand Down
2 changes: 2 additions & 0 deletions ydb/library/yql/sql/v1/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ namespace NSQLTranslationV1 {
bool BlockEngineForce = false;
bool UnorderedResult = false;
ui64 ParallelModeCount = 0;
bool CompactNamedExprs = false;
bool ValidateUnusedExprs = false;
};

class TColumnRefScope {
Expand Down
4 changes: 2 additions & 2 deletions ydb/library/yql/sql/v1/join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class TJoinBase: public IJoin {
return false;
}

op = dynamic_cast<const TCallNode*>(expr.Get());
op = expr->GetCallNode();
YQL_ENSURE(op, "Invalid JOIN equal operation node");
YQL_ENSURE(op->GetArgs().size() == 2, "Invalid JOIN equal operation arguments");
}
Expand Down Expand Up @@ -441,7 +441,7 @@ bool TJoinBase::DoInit(TContext& ctx, ISource* initSrc) {
TNodePtr cur = conjQueue.front();
conjQueue.pop_front();
if (cur->GetOpName() == "And") {
auto conj = dynamic_cast<const TCallNode*>(cur.Get());
auto conj = cur->GetCallNode();
YQL_ENSURE(conj, "Invalid And operation node");
conjQueue.insert(conjQueue.begin(), conj->GetArgs().begin(), conj->GetArgs().end());
} else if (!InitKeysOrFilters(ctx, idx, cur)) {
Expand Down
Loading
Loading