diff options
| author | Vitaly Stoyan <[email protected]> | 2022-06-09 17:14:17 +0300 |
|---|---|---|
| committer | Vitaly Stoyan <[email protected]> | 2022-06-09 17:14:17 +0300 |
| commit | d5e8eb3e8399704deab119214d49e85fc5ab73fe (patch) | |
| tree | 533e395db3783118e1ce18cade34d36cc22cdadc | |
| parent | a3afc43b7bf8d377bc924e7cedd2fed01e293938 (diff) | |
YQL-14973 implemented common compare operators for pg types
ref:53311e2090eaf07ca13c7d3b8ed7e0dbeaaf9bc0
| -rw-r--r-- | ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp | 57 | ||||
| -rw-r--r-- | ydb/library/yql/core/yql_expr_type_annotation.cpp | 4 |
2 files changed, 59 insertions, 2 deletions
diff --git a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp index df1f73e336a..98554df9863 100644 --- a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp +++ b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp @@ -5154,6 +5154,20 @@ TExprNode::TPtr AggrEqualLists(const TExprNode& node, TExprContext& ctx) { } template <bool Asc, bool Equals> +TExprNode::TPtr SqlComparePg(const TExprNode& node, TExprContext& ctx) { + YQL_CLOG(DEBUG, CorePeepHole) << "Expand '" << node.Content() << "' over Pg."; + return ctx.Builder(node.Pos()) + .Callable("FromPg") + .Callable(0, "PgOp") + .Atom(0, Asc ? (Equals ? "<=" : "<") : (Equals ? ">=" : ">")) + .Add(1, node.ChildPtr(0)) + .Add(2, node.ChildPtr(1)) + .Seal() + .Seal() + .Build(); +} + +template <bool Asc, bool Equals> TExprNode::TPtr SqlCompareLists(const TExprNode& node, TExprContext& ctx) { YQL_CLOG(DEBUG, CorePeepHole) << "Expand '" << node.Content() << "' over Lists."; auto lenCompare = JustIf(ETypeAnnotationKind::Optional == node.GetTypeAnn()->GetKind(), @@ -5258,6 +5272,45 @@ TExprNode::TPtr CheckHasItems(const TExprNode::TPtr& node, TExprContext& ctx) { } template <bool Equals, bool IsDistinct> +TExprNode::TPtr SqlEqualPg(const TExprNode& node, TExprContext& ctx) { + YQL_CLOG(DEBUG, CorePeepHole) << "Expand '" << node.Content() << "' over Pg."; + if constexpr (IsDistinct) { + return ctx.Builder(node.Pos()) + .Callable("IfPresent") + .Callable(0, "FromPg") + .Callable(0, "PgOp") + .Atom(0, Equals ? "=" : "<>") + .Add(1, node.ChildPtr(0)) + .Add(2, node.ChildPtr(1)) + .Seal() + .Seal() + .Lambda(1) + .Param("unpacked") + .Arg("unpacked") + .Seal() + .Callable(2, Equals ? "==" : "!=") + .Callable(0, "Exists") + .Add(0, node.ChildPtr(0)) + .Seal() + .Callable(1, "Exists") + .Add(0, node.ChildPtr(1)) + .Seal() + .Seal() + .Build(); + } else { + return ctx.Builder(node.Pos()) + .Callable("FromPg") + .Callable(0, "PgOp") + .Atom(0, Equals ? "=" : "<>") + .Add(1, node.ChildPtr(0)) + .Add(2, node.ChildPtr(1)) + .Seal() + .Seal() + .Build(); + } +} + +template <bool Equals, bool IsDistinct> TExprNode::TPtr SqlEqualDicts(const TExprNode& node, TExprContext& ctx) { YQL_CLOG(DEBUG, CorePeepHole) << "Expand '" << node.Content() << "' over Dicts."; @@ -5624,6 +5677,8 @@ TExprNode::TPtr ExpandSqlEqual(const TExprNode::TPtr& node, TExprContext& ctx) { return SqlEqualLists<Equals>(*node, ctx); case ETypeAnnotationKind::Dict: return SqlEqualDicts<Equals, IsDistinct>(*node, ctx); + case ETypeAnnotationKind::Pg: + return SqlEqualPg<Equals, IsDistinct>(*node, ctx); case ETypeAnnotationKind::Variant: return SqlCompareVariants<true, !Equals, IsDistinct>(*node, ctx); case ETypeAnnotationKind::Tagged: @@ -5673,6 +5728,8 @@ TExprNode::TPtr ExpandSqlCompare(const TExprNode::TPtr& node, TExprContext& ctx) return CompareTagged(*node, ctx); case ETypeAnnotationKind::Optional: return ReduceBothArgs<false>(*node, ctx); + case ETypeAnnotationKind::Pg: + return SqlComparePg<Asc, Equals>(*node, ctx); default: break; } diff --git a/ydb/library/yql/core/yql_expr_type_annotation.cpp b/ydb/library/yql/core/yql_expr_type_annotation.cpp index cb42ede3118..19da3cdc1ce 100644 --- a/ydb/library/yql/core/yql_expr_type_annotation.cpp +++ b/ydb/library/yql/core/yql_expr_type_annotation.cpp @@ -1151,9 +1151,9 @@ ECompareOptions CanCompare(const TPgExprType* left, const TPgExprType* right) { } if (Equality) { - return left->IsEquatable() ? ECompareOptions::Comparable : ECompareOptions::Uncomparable; + return left->IsEquatable() ? ECompareOptions::Optional : ECompareOptions::Uncomparable; } else { - return left->IsComparable() ? ECompareOptions::Comparable : ECompareOptions::Uncomparable; + return left->IsComparable() ? ECompareOptions::Optional : ECompareOptions::Uncomparable; } } |
