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

Feature Implementation NullEq function push down #6057

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions dbms/src/Flash/Coprocessor/DAGExpressionAnalyzerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ String DAGExpressionAnalyzerHelper::buildIfNullFunction(
return analyzer->applyFunction(func_name, argument_names, actions, getCollatorFromExpr(expr));
}

String DAGExpressionAnalyzerHelper::buildNullEqFunction(
DAGExpressionAnalyzer * analyzer,
const tipb::Expr & expr,
const ExpressionActionsPtr & actions)
{
/// nullEq(col1, col2) == isNull(col1) AND isNull(col2) OR coalesce(Equal(col1, col2), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that isNull(col1) AND isNull(col2) OR coalesce(Equal(col1, col2), 0) can be reduced to Equal(col1, col2) when col1 and col2 is not null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use actions->getSampleBlock().getByName(col).type->isNullable() to determine if col is nullable


if (expr.children_size() != 2)
{
throw TiFlashException("Invalid arguments of nullEq function", Errors::Coprocessor::BadRequest);
}

String col1 = analyzer->getActions(expr.children(0), actions, false);
String col2 = analyzer->getActions(expr.children(1), actions, false);
String is_null_col1 = analyzer->applyFunction("isNull", {col1}, actions, getCollatorFromExpr(expr));
String is_null_col2 = analyzer->applyFunction("isNull", {col2}, actions, getCollatorFromExpr(expr));
String and_is_null = analyzer->applyFunction("and", {is_null_col1, is_null_col2}, actions, nullptr);

String equals = analyzer->applyFunction("equals", {col1, col2}, actions, getCollatorFromExpr(expr));
String name = analyzer->getActions(constructInt64LiteralTiExpr(0), actions);
String not_null_equals = analyzer->applyFunction("coalesce", {equals, name}, actions, nullptr);

return analyzer->applyFunction("or", {and_is_null, not_null_equals}, actions, nullptr);
}

String DAGExpressionAnalyzerHelper::buildInFunction(
DAGExpressionAnalyzer * analyzer,
const tipb::Expr & expr,
Expand Down Expand Up @@ -440,6 +465,7 @@ DAGExpressionAnalyzerHelper::FunctionBuilderMap DAGExpressionAnalyzerHelper::fun
{"tidbIn", DAGExpressionAnalyzerHelper::buildInFunction},
{"tidbNotIn", DAGExpressionAnalyzerHelper::buildInFunction},
{"ifNull", DAGExpressionAnalyzerHelper::buildIfNullFunction},
{"nullEq", DAGExpressionAnalyzerHelper::buildNullEqFunction},
{"multiIf", DAGExpressionAnalyzerHelper::buildMultiIfFunction},
{"tidb_cast", DAGExpressionAnalyzerHelper::buildCastFunction},
{"and", DAGExpressionAnalyzerHelper::buildLogicalFunction},
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Flash/Coprocessor/DAGExpressionAnalyzerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class DAGExpressionAnalyzerHelper
const tipb::Expr & expr,
const ExpressionActionsPtr & actions);

static String buildNullEqFunction(
DAGExpressionAnalyzer * analyzer,
const tipb::Expr & expr,
const ExpressionActionsPtr & actions);

static String buildLogicalFunction(
DAGExpressionAnalyzer * analyzer,
const tipb::Expr & expr,
Expand Down
16 changes: 8 additions & 8 deletions dbms/src/Flash/Coprocessor/DAGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ const std::unordered_map<tipb::ScalarFuncSig, String> scalar_func_map({
{tipb::ScalarFuncSig::NEDuration, "notEquals"},
{tipb::ScalarFuncSig::NEJson, "notEquals"},

//{tipb::ScalarFuncSig::NullEQInt, "cast"},
//{tipb::ScalarFuncSig::NullEQReal, "cast"},
//{tipb::ScalarFuncSig::NullEQString, "cast"},
//{tipb::ScalarFuncSig::NullEQDecimal, "cast"},
//{tipb::ScalarFuncSig::NullEQTime, "cast"},
//{tipb::ScalarFuncSig::NullEQDuration, "cast"},
//{tipb::ScalarFuncSig::NullEQJson, "cast"},
{tipb::ScalarFuncSig::NullEQInt, "nullEq"},
{tipb::ScalarFuncSig::NullEQReal, "nullEq"},
{tipb::ScalarFuncSig::NullEQString, "nullEq"},
{tipb::ScalarFuncSig::NullEQDecimal, "nullEq"},
{tipb::ScalarFuncSig::NullEQTime, "nullEq"},
{tipb::ScalarFuncSig::NullEQDuration, "nullEq"},
{tipb::ScalarFuncSig::NullEQJson, "nullEq"},

{tipb::ScalarFuncSig::PlusReal, "plus"},
{tipb::ScalarFuncSig::PlusDecimal, "plus"},
Expand Down Expand Up @@ -1094,7 +1094,7 @@ Field decodeLiteral(const tipb::Expr & expr)
case tipb::ExprType::Uint64:
return decodeDAGUInt64(expr.val());
case tipb::ExprType::Float32:
return Float64(decodeDAGFloat32(expr.val()));
return static_cast<Float64>(decodeDAGFloat32(expr.val()));
case tipb::ExprType::Float64:
return decodeDAGFloat64(expr.val());
case tipb::ExprType::String:
Expand Down
Loading