Skip to content

Fix pushdown logical ops with scalar argument. (#6142) #6221

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

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
30 changes: 16 additions & 14 deletions ydb/core/kqp/query_compiler/kqp_olap_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ TTypedColumn CompileYqlKernelBinaryOperation(const TKqpOlapFilterBinaryOp& opera
}

template<typename TFunc>
const TTypedColumn BuildLogicalProgram(const TExprNode::TChildrenType& args, const TFunc function, TKqpOlapCompileContext& ctx)
const TTypedColumn BuildLogicalProgram(TPositionHandle pos, const TExprNode::TChildrenType& args, const TFunc function, TKqpOlapCompileContext& ctx)
{
const auto childrenCount = args.size();
if (childrenCount == 1) {
Expand All @@ -700,22 +700,24 @@ const TTypedColumn BuildLogicalProgram(const TExprNode::TChildrenType& args, con

const bool twoArgs = 2U == childrenCount;
const auto half = childrenCount >> 1U;
const auto left = twoArgs ? GetOrCreateColumnIdAndType(TExprBase(args.front()), ctx) : BuildLogicalProgram(args.subspan(0U, half), function, ctx);
const auto right = twoArgs ? GetOrCreateColumnIdAndType(TExprBase(args.back()), ctx) : BuildLogicalProgram(args.subspan(half), function, ctx);
const auto left = twoArgs ? GetOrCreateColumnIdAndType(TExprBase(args.front()), ctx) : BuildLogicalProgram(pos, args.subspan(0U, half), function, ctx);
const auto right = twoArgs ? GetOrCreateColumnIdAndType(TExprBase(args.back()), ctx) : BuildLogicalProgram(pos, args.subspan(half), function, ctx);

auto *const logicalOp = ctx.CreateAssignCmd();
auto *const logicalFunc = logicalOp->MutableFunction();
logicalFunc->AddArguments()->SetId(left.Id);
logicalFunc->AddArguments()->SetId(right.Id);

const auto block = ctx.ExprCtx().MakeType<TBlockExprType>(ctx.ExprCtx().MakeType<TOptionalExprType>(ctx.ExprCtx().MakeType<TDataExprType>(EDataSlot::Bool)));
const TTypeAnnotationNode* block = nullptr;
if constexpr (std::is_same<TFunc, TKernelRequestBuilder::EBinaryOp>()) {
const auto idx = ctx.GetKernelRequestBuilder().AddBinaryOp(function, block, block, block);
logicalFunc->SetKernelIdx(idx);
const auto kernel = ctx.AddYqlKernelBinaryFunc(pos, function, *left.Type, *right.Type, nullptr);
block = kernel.second;
logicalFunc->SetKernelIdx(kernel.first);
logicalFunc->SetFunctionType(TProgram::YQL_KERNEL);
logicalFunc->SetYqlOperationId((ui32)function);
} else {
logicalFunc->SetId((ui32)function);
block = ctx.ExprCtx().MakeType<TBlockExprType>(ctx.ExprCtx().MakeType<TOptionalExprType>(ctx.ExprCtx().MakeType<TDataExprType>(EDataSlot::Bool)));
}

return {logicalOp->GetColumn().GetId(), block};
Expand Down Expand Up @@ -765,19 +767,19 @@ TTypedColumn GetOrCreateColumnIdAndType(const TExprBase& node, TKqpOlapCompileCo
return CompileYqlKernelUnaryOperation(maybeUnaryOp.Cast(), ctx);
} else if (const auto& maybeAnd = node.Maybe<TKqpOlapAnd>()) {
if constexpr (NSsa::RuntimeVersion >= 4U)
return BuildLogicalProgram(maybeAnd.Ref().Children(), TKernelRequestBuilder::EBinaryOp::And, ctx);
return BuildLogicalProgram(node.Pos(), maybeAnd.Ref().Children(), TKernelRequestBuilder::EBinaryOp::And, ctx);
else
return BuildLogicalProgram(maybeAnd.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_AND, ctx);
return BuildLogicalProgram(node.Pos(), maybeAnd.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_AND, ctx);
} else if (const auto& maybeOr = node.Maybe<TKqpOlapOr>()) {
if constexpr (NSsa::RuntimeVersion >= 4U)
return BuildLogicalProgram(maybeOr.Ref().Children(), TKernelRequestBuilder::EBinaryOp::Or, ctx);
return BuildLogicalProgram(node.Pos(), maybeOr.Ref().Children(), TKernelRequestBuilder::EBinaryOp::Or, ctx);
else
return BuildLogicalProgram(maybeOr.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_OR, ctx);
return BuildLogicalProgram(node.Pos(), maybeOr.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_OR, ctx);
} else if (const auto& maybeXor = node.Maybe<TKqpOlapXor>()) {
if constexpr (NSsa::RuntimeVersion >= 4U)
return BuildLogicalProgram(maybeXor.Ref().Children(), TKernelRequestBuilder::EBinaryOp::Xor, ctx);
return BuildLogicalProgram(node.Pos(), maybeXor.Ref().Children(), TKernelRequestBuilder::EBinaryOp::Xor, ctx);
else
return BuildLogicalProgram(maybeXor.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_XOR, ctx);
return BuildLogicalProgram(node.Pos(), maybeXor.Ref().Children(), TProgram::TAssignment::FUNC_BINARY_XOR, ctx);
} else if (const auto& maybeNot = node.Maybe<TKqpOlapNot>()) {
return BuildLogicalNot(maybeNot.Cast().Value(), ctx);
} else if (const auto& maybeJsonValue = node.Maybe<TKqpOlapJsonValue>()) {
Expand Down Expand Up @@ -847,9 +849,9 @@ ui64 CompileCondition(const TExprBase& condition, TKqpOlapCompileContext& ctx) {
}

if constexpr (NSsa::RuntimeVersion >= 4U)
return BuildLogicalProgram(condition.Ref().Children(), op, ctx).Id;
return BuildLogicalProgram(condition.Pos(), condition.Ref().Children(), op, ctx).Id;
else
return BuildLogicalProgram(condition.Ref().Children(), function, ctx).Id;
return BuildLogicalProgram(condition.Pos(), condition.Ref().Children(), function, ctx).Id;
}

void CompileFilter(const TKqpOlapFilter& filterNode, TKqpOlapCompileContext& ctx) {
Expand Down
2 changes: 2 additions & 0 deletions ydb/core/kqp/ut/olap/kqp_olap_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@ Y_UNIT_TEST_SUITE(KqpOlap) {
R"(`resource_id` != `uid`)",
R"(`resource_id` = "10001")",
R"(`resource_id` != "10001")",
R"("XXX" == "YYY" OR `resource_id` != "10001")",
R"(`resource_id` != "10001" XOR "XXX" == "YYY")",
R"(`level` = 1)",
R"(`level` = Int8("1"))",
R"(`level` = Int16("1"))",
Expand Down
Loading