-
Notifications
You must be signed in to change notification settings - Fork 740
Check types for pushdown on OLAP level. #1144
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
Tony-Romanov
merged 3 commits into
ydb-platform:main
from
Tony-Romanov:fix_comparators_in_olap
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ struct TPushdownSettings : public NPushdown::TSettings { | |
| Enable(EFlag::LikeOperator, NSsa::RuntimeVersion >= 2U); | ||
| Enable(EFlag::LikeOperatorOnlyForUtf8, NSsa::RuntimeVersion < 3U); | ||
| Enable(EFlag::JsonQueryOperators | EFlag::JsonExistsOperator, NSsa::RuntimeVersion >= 3U); | ||
| Enable(EFlag::ArithmeticalExpressions | EFlag::UnaryOperators, NSsa::RuntimeVersion >= 4U); | ||
| Enable(EFlag::ArithmeticalExpressions | EFlag::UnaryOperators | EFlag::DoNotCheckCompareArgumentsTypes, NSsa::RuntimeVersion >= 4U); | ||
| Enable(EFlag::LogicalXorOperator | ||
| | EFlag::ParameterExpression | ||
| | EFlag::CastExpression | ||
|
|
@@ -61,7 +61,7 @@ struct TFilterOpsLevels { | |
| } | ||
| } | ||
|
|
||
| bool IsValid() { | ||
| bool IsValid() const { | ||
| return FirstLevelOps.IsValid() || SecondLevelOps.IsValid(); | ||
| } | ||
|
|
||
|
|
@@ -152,10 +152,6 @@ TMaybeNode<TExprBase> YqlCoalescePushdown(const TCoCoalesce& coalesce, TExprCont | |
| return NullNode; | ||
| } | ||
|
|
||
| bool IsGoodTypeForPushdown(const TTypeAnnotationNode& type) { | ||
| return NUdf::EDataTypeFeatures::IntegralType & NUdf::GetDataTypeInfo(RemoveOptionality(type).Cast<TDataExprType>()->GetSlot()).Features; | ||
| } | ||
|
|
||
| std::vector<TExprBase> ConvertComparisonNode(const TExprBase& nodeIn, TExprContext& ctx, TPositionHandle pos) | ||
| { | ||
| std::vector<TExprBase> out; | ||
|
|
@@ -203,27 +199,25 @@ std::vector<TExprBase> ConvertComparisonNode(const TExprBase& nodeIn, TExprConte | |
|
|
||
| if constexpr (NKikimr::NSsa::RuntimeVersion >= 4U) { | ||
| if (const auto maybeArithmetic = node.Maybe<TCoBinaryArithmetic>()) { | ||
| if (const auto arithmetic = maybeArithmetic.Cast(); IsGoodTypeForPushdown(*arithmetic.Ref().GetTypeAnn()) && !arithmetic.Maybe<TCoAggrAdd>()) { | ||
| if (const auto params = ExtractBinaryFunctionParameters(arithmetic, ctx, pos)) { | ||
| return Build<TKqpOlapFilterBinaryOp>(ctx, pos) | ||
| .Operator().Value(arithmetic.Ref().Content(), TNodeFlags::Default).Build() | ||
| .Left(params->first) | ||
| .Right(params->second) | ||
| .Done(); | ||
| } | ||
| const auto arithmetic = maybeArithmetic.Cast(); | ||
| if (const auto params = ExtractBinaryFunctionParameters(arithmetic, ctx, pos)) { | ||
| return Build<TKqpOlapFilterBinaryOp>(ctx, pos) | ||
| .Operator().Value(arithmetic.Ref().Content(), TNodeFlags::Default).Build() | ||
| .Left(params->first) | ||
| .Right(params->second) | ||
| .Done(); | ||
| } | ||
| } | ||
|
|
||
| if (const auto maybeArithmetic = node.Maybe<TCoUnaryArithmetic>()) { | ||
| if (const auto arithmetic = maybeArithmetic.Cast(); IsGoodTypeForPushdown(*arithmetic.Ref().GetTypeAnn())) { | ||
| if (const auto params = ConvertComparisonNode(arithmetic.Arg(), ctx, pos); 1U == params.size()) { | ||
| TString oper(arithmetic.Ref().Content()); | ||
| YQL_ENSURE(oper.to_lower()); | ||
| return Build<TKqpOlapFilterUnaryOp>(ctx, pos) | ||
| .Operator().Value(oper, TNodeFlags::Default).Build() | ||
| .Arg(params.front()) | ||
| .Done(); | ||
| } | ||
| const auto arithmetic = maybeArithmetic.Cast(); | ||
| if (const auto params = ConvertComparisonNode(arithmetic.Arg(), ctx, pos); 1U == params.size()) { | ||
| TString oper(arithmetic.Ref().Content()); | ||
| YQL_ENSURE(oper.to_lower()); | ||
| return Build<TKqpOlapFilterUnaryOp>(ctx, pos) | ||
| .Operator().Value(oper, TNodeFlags::Default).Build() | ||
| .Arg(params.front()) | ||
| .Done(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -653,6 +647,80 @@ void SplitForPartialPushdown(const NPushdown::TPredicateNode& predicateTree, NPu | |
| remainingPredicates.SetPredicates(remaining, ctx, pos); | ||
| } | ||
|
|
||
| bool IsGoodTypeForPushdown(const TTypeAnnotationNode& type) { | ||
| return NUdf::EDataTypeFeatures::IntegralType & NUdf::GetDataTypeInfo(RemoveOptionality(type).Cast<TDataExprType>()->GetSlot()).Features; | ||
| } | ||
|
|
||
| bool IsGoodTypesForPushdownCompare(const TTypeAnnotationNode& typeOne, const TTypeAnnotationNode& typeTwo) { | ||
| const auto& rawOne = RemoveOptionality(typeOne); | ||
| const auto& rawTwo = RemoveOptionality(typeTwo); | ||
| if (IsSameAnnotation(rawOne, rawTwo)) | ||
| return true; | ||
|
|
||
| const auto kindOne = rawOne.GetKind(); | ||
| const auto kindTwo = rawTwo.GetKind(); | ||
| if (ETypeAnnotationKind::Null == kindOne || ETypeAnnotationKind::Null == kindTwo) | ||
| return true; | ||
|
|
||
| if (kindTwo != kindOne) | ||
| return false; | ||
|
|
||
| switch (kindOne) { | ||
| case ETypeAnnotationKind::Tuple: { | ||
| const auto& itemsOne = rawOne.Cast<TTupleExprType>()->GetItems(); | ||
| const auto& itemsTwo = rawTwo.Cast<TTupleExprType>()->GetItems(); | ||
| const auto size = itemsOne.size(); | ||
| if (size != itemsTwo.size()) | ||
| return false; | ||
| for (auto i = 0U; i < size; ++i) { | ||
| if (!IsGoodTypesForPushdownCompare(*itemsOne[i], *itemsTwo[i])) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| case ETypeAnnotationKind::Data: { | ||
| const auto fOne = NUdf::GetDataTypeInfo(rawOne.Cast<TDataExprType>()->GetSlot()).Features; | ||
| const auto fTwo = NUdf::GetDataTypeInfo(rawTwo.Cast<TDataExprType>()->GetSlot()).Features; | ||
| return ((NUdf::EDataTypeFeatures::NumericType | NUdf::EDataTypeFeatures::StringType) & fOne) && (NUdf::EDataTypeFeatures::CanCompare & fOne) | ||
| && ((NUdf::EDataTypeFeatures::NumericType | NUdf::EDataTypeFeatures::StringType) & fTwo) && (NUdf::EDataTypeFeatures::CanCompare & fTwo); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are two spaces before |
||
| } | ||
| default: break; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool IsGoodNodeForPushdown(const TExprBase& node) { | ||
| if (const auto maybeCompare = node.Maybe<TCoCompare>()) { | ||
| const auto compare = maybeCompare.Cast(); | ||
| return IsGoodTypesForPushdownCompare(*compare.Left().Ref().GetTypeAnn(), *compare.Right().Ref().GetTypeAnn()) | ||
| && IsGoodNodeForPushdown(compare.Left()) && IsGoodNodeForPushdown(compare.Right()); | ||
| } else if (const auto maybeUnaryOp = node.Maybe<TCoUnaryArithmetic>()) { | ||
| return IsGoodTypeForPushdown(*node.Ref().GetTypeAnn()) && IsGoodNodeForPushdown(maybeUnaryOp.Cast().Arg()); | ||
| } else if (const auto maybeBinaryOp = node.Maybe<TCoBinaryArithmetic>()) { | ||
| const auto binaryOp = maybeBinaryOp.Cast(); | ||
| return IsGoodTypeForPushdown(*binaryOp.Ref().GetTypeAnn()) && !binaryOp.Maybe<TCoAggrAdd>() | ||
| && IsGoodNodeForPushdown(binaryOp.Left()) && IsGoodNodeForPushdown(binaryOp.Right()); | ||
| } else if (const auto maybeCoalesce = node.Maybe<TCoCoalesce>()) { | ||
| const auto coalesce = maybeCoalesce.Cast(); | ||
| return IsGoodNodeForPushdown(coalesce.Predicate()) && IsGoodNodeForPushdown(coalesce.Value()); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void UpdatePushableFlagWithOlapSpecific(NPushdown::TPredicateNode& tree) { | ||
| if constexpr (NSsa::RuntimeVersion < 4U) | ||
| return; | ||
|
|
||
| std::for_each(tree.Children.begin(), tree.Children.end(), std::bind(&UpdatePushableFlagWithOlapSpecific, std::placeholders::_1)); | ||
| tree.CanBePushed = tree.CanBePushed && std::all_of(tree.Children.cbegin(), tree.Children.cend(), [](const NPushdown::TPredicateNode& node) { return node.CanBePushed; }); | ||
|
|
||
| if (tree.CanBePushed && NPushdown::EBoolOp::Undefined == tree.Op) { | ||
| tree.CanBePushed = IsGoodNodeForPushdown(tree.ExprNode.Cast()); | ||
| } | ||
| } | ||
|
|
||
| } // anonymous namespace end | ||
|
|
||
| TExprBase KqpPushOlapFilter(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx, | ||
|
|
@@ -685,10 +753,11 @@ TExprBase KqpPushOlapFilter(TExprBase node, TExprContext& ctx, const TKqpOptimiz | |
| return node; | ||
| } | ||
|
|
||
| auto optionalIf = maybeOptionalIf.Cast(); | ||
| const auto optionalIf = maybeOptionalIf.Cast(); | ||
| NPushdown::TPredicateNode predicateTree(optionalIf.Predicate()); | ||
| CollectPredicates(optionalIf.Predicate(), predicateTree, lambdaArg, read.Process().Body(), TPushdownSettings()); | ||
| YQL_ENSURE(predicateTree.IsValid(), "Collected OLAP predicates are invalid"); | ||
| UpdatePushableFlagWithOlapSpecific(predicateTree); | ||
|
|
||
| NPushdown::TPredicateNode predicatesToPush; | ||
| NPushdown::TPredicateNode remainingPredicates; | ||
|
|
@@ -700,7 +769,7 @@ TExprBase KqpPushOlapFilter(TExprBase node, TExprContext& ctx, const TKqpOptimiz | |
| YQL_ENSURE(predicatesToPush.IsValid(), "Predicates to push is invalid"); | ||
| YQL_ENSURE(remainingPredicates.IsValid(), "Remaining predicates is invalid"); | ||
|
|
||
| auto pushedFilters = PredicatePushdown(predicatesToPush.ExprNode.Cast(), ctx, node.Pos()); | ||
| const auto pushedFilters = PredicatePushdown(predicatesToPush.ExprNode.Cast(), ctx, node.Pos()); | ||
| YQL_ENSURE(pushedFilters.IsValid(), "Pushed predicate should be always valid!"); | ||
|
|
||
| TMaybeNode<TExprBase> olapFilter; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are two spaces before
&