123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- #include "match_recognize.h"
- #include "source.h"
- #include "context.h"
- #include <util/generic/overloaded.h>
- namespace NSQLTranslationV1 {
- namespace {
- constexpr auto VarDataName = "data";
- constexpr auto VarMatchedVarsName = "vars";
- constexpr auto VarLastRowIndexName = "lri";
- class TMatchRecognizeColumnAccessNode final : public TAstListNode {
- public:
- TMatchRecognizeColumnAccessNode(TPosition pos, TString var, TString column)
- : TAstListNode(pos)
- , Var(std::move(var))
- , Column(std::move(column)) {
- }
- const TString* GetColumnName() const override {
- return std::addressof(Column);
- }
- bool DoInit(TContext& ctx, ISource* /* src */) override {
- switch (ctx.GetColumnReferenceState()) {
- case EColumnRefState::MatchRecognizeMeasures:
- if (!ctx.SetMatchRecognizeAggrVar(Var)) {
- return false;
- }
- Add(
- "Member",
- BuildAtom(Pos, "row"),
- Q(Column)
- );
- break;
- case EColumnRefState::MatchRecognizeDefine:
- if (ctx.GetMatchRecognizeDefineVar() != Var) {
- ctx.Error() << "Row pattern navigation function is required";
- return false;
- }
- BuildLookup(VarLastRowIndexName);
- break;
- case EColumnRefState::MatchRecognizeDefineAggregate:
- if (!ctx.SetMatchRecognizeAggrVar(Var)) {
- return false;
- }
- BuildLookup("index");
- break;
- default:
- ctx.Error(Pos) << "Unexpected column reference state";
- return false;
- }
- return true;
- }
- TNodePtr DoClone() const override {
- return MakeIntrusive<TMatchRecognizeColumnAccessNode>(Pos, Var, Column);
- }
- private:
- void BuildLookup(TString varKeyName) {
- Add(
- "Member",
- Y(
- "Lookup",
- Y(
- "ToIndexDict",
- BuildAtom(Pos, VarDataName)
- ),
- BuildAtom(Pos, std::move(varKeyName))
- ),
- Q(Column)
- );
- }
- private:
- TString Var;
- TString Column;
- };
- class TMatchRecognizeDefineAggregate final : public TAstListNode {
- public:
- TMatchRecognizeDefineAggregate(TPosition pos, TString name, TVector<TNodePtr> args)
- : TAstListNode(pos)
- , Name(std::move(name))
- , Args(std::move(args)) {
- }
- bool DoInit(TContext& ctx, ISource* src) override {
- if (EColumnRefState::MatchRecognizeDefine != ctx.GetColumnReferenceState()) {
- ctx.Error(Pos) << "Unexpected column reference state";
- return false;
- }
- TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeDefineAggregate, false, ctx.GetMatchRecognizeDefineVar());
- if (Args.size() != 1) {
- ctx.Error() << "Exactly one argument is required in MATCH_RECOGNIZE navigation function";
- return false;
- }
- const auto arg = Args[0];
- if (!arg || !arg->Init(ctx, src)) {
- return false;
- }
- const auto body = [&]() -> TNodePtr {
- if ("first" == Name) {
- return Y("Member", Y("Head", "item"), Q("From"));
- } else if ("last" == Name) {
- return Y("Member", Y("Last", "item"), Q("To"));
- } else {
- ctx.Error() << "Unknown row pattern navigation function: " << Name;
- return {};
- }
- }();
- if (!body) {
- return false;
- }
- Add("Apply", BuildLambda(Pos, Y("index"), arg), body);
- return true;
- }
- TNodePtr DoClone() const override {
- return MakeIntrusive<TMatchRecognizeDefineAggregate>(Pos, Name, Args);
- }
- private:
- TString Name;
- TVector<TNodePtr> Args;
- };
- class TMatchRecognizeVarAccessNode final : public INode {
- public:
- TMatchRecognizeVarAccessNode(TPosition pos, TNodePtr aggr)
- : INode(pos)
- , Aggr(std::move(aggr)) {
- }
- bool DoInit(TContext& ctx, ISource* src) override {
- if (!Aggr || !Aggr->Init(ctx, src)) {
- return false;
- }
- auto var = ctx.ExtractMatchRecognizeAggrVar();
- Expr = [&]() -> TNodePtr {
- switch (ctx.GetColumnReferenceState()) {
- case EColumnRefState::MatchRecognizeMeasures: {
- ctx.GetMatchRecognizeAggregations().emplace_back(std::move(var), Aggr->GetAggregation());
- return Aggr;
- }
- case EColumnRefState::MatchRecognizeDefine:
- return Y(
- "Apply",
- BuildLambda(Pos, Y("item"), Aggr),
- Y(
- "Member",
- BuildAtom(ctx.Pos(), VarMatchedVarsName),
- Q(std::move(var))
- )
- );
- default:
- ctx.Error(Pos) << "Unexpected column reference state";
- return {};
- }
- }();
- return Expr && Expr->Init(ctx, src);
- }
- TNodePtr DoClone() const override {
- return MakeIntrusive<TMatchRecognizeVarAccessNode>(Pos, Aggr);
- }
- TAstNode* Translate(TContext& ctx) const override {
- return Expr->Translate(ctx);
- }
- private:
- TNodePtr Aggr;
- TNodePtr Expr;
- };
- class TMatchRecognize final : public TAstListNode {
- public:
- TMatchRecognize(
- TPosition pos,
- TString label,
- TNodePtr partitionKeySelector,
- TNodePtr partitionColumns,
- TVector<TSortSpecificationPtr> sortSpecs,
- TVector<TNamedFunction> measures,
- TNodePtr rowsPerMatch,
- TNodePtr skipTo,
- TNodePtr pattern,
- TNodePtr patternVars,
- TNodePtr subset,
- TVector<TNamedFunction> definitions)
- : TAstListNode(pos)
- , Label(std::move(label))
- , PartitionKeySelector(std::move(partitionKeySelector))
- , PartitionColumns(std::move(partitionColumns))
- , SortSpecs(std::move(sortSpecs))
- , Measures(std::move(measures))
- , RowsPerMatch(std::move(rowsPerMatch))
- , SkipTo(std::move(skipTo))
- , Pattern(std::move(pattern))
- , PatternVars(std::move(patternVars))
- , Subset(std::move(subset))
- , Definitions(std::move(definitions)) {
- }
- private:
- bool DoInit(TContext& ctx, ISource* src) override {
- auto inputRowType = Y("ListItemType", Y("TypeOf", Label));
- if (!PartitionKeySelector || !PartitionKeySelector->Init(ctx, src)) {
- return false;
- }
- if (!PartitionColumns || !PartitionColumns->Init(ctx, src)) {
- return false;
- }
- const auto sortTraits = SortSpecs.empty() ? Y("Void") : src->BuildSortSpec(SortSpecs, Label, true, false);
- if (!sortTraits || !sortTraits->Init(ctx, src)) {
- return false;
- }
- auto measureNames = Y();
- auto measuresCallables = Y();
- for (auto& m: Measures) {
- TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeMeasures);
- if (!m.Callable || !m.Callable->Init(ctx, src)) {
- return false;
- }
- const auto pos = m.Callable->GetPos();
- measureNames = L(measureNames, BuildQuotedAtom(m.Callable->GetPos(), std::move(m.Name)));
- auto measuresVars = Y();
- auto measuresAggregates = Y();
- for (auto& [var, aggr] : ctx.GetMatchRecognizeAggregations()) {
- if (!aggr) {
- return false;
- }
- auto [traits, result] = aggr->AggregationTraits(Y("TypeOf", Label), false, false, false, ctx);
- if (!result) {
- return false;
- }
- measuresVars = L(measuresVars, BuildQuotedAtom(pos, std::move(var)));
- measuresAggregates = L(measuresAggregates, std::move(traits));
- }
- ctx.GetMatchRecognizeAggregations().clear();
- measuresCallables = L(
- measuresCallables,
- Y(
- "MatchRecognizeMeasuresCallable",
- BuildLambda(pos, Y("row"), std::move(m.Callable)),
- Q(measuresVars),
- Q(measuresAggregates)
- )
- );
- }
- auto measuresNode = Y("MatchRecognizeMeasuresCallables", inputRowType, Q(PatternVars), Q(measureNames), Q(measuresCallables));
- if (!RowsPerMatch || !RowsPerMatch->Init(ctx, src)) {
- return false;
- }
- if (!SkipTo || !SkipTo->Init(ctx, src)) {
- return false;
- }
- if (!Pattern || !Pattern->Init(ctx, src)) {
- return false;
- }
- if (!PatternVars || !PatternVars->Init(ctx, src)) {
- return false;
- }
- auto defineNames = Y();
- for (auto& d: Definitions) {
- defineNames = L(defineNames, BuildQuotedAtom(d.Callable->GetPos(), d.Name));
- }
- auto defineNode = Y("MatchRecognizeDefines", inputRowType, Q(PatternVars), Q(defineNames));
- for (auto& d: Definitions) {
- TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeDefine, true, d.Name);
- if (!d.Callable || !d.Callable->Init(ctx, src)) {
- return false;
- }
- const auto pos = d.Callable->GetPos();
- defineNode = L(defineNode, BuildLambda(pos, Y(VarDataName, VarMatchedVarsName, VarLastRowIndexName), std::move(d.Callable)));
- }
- Add(
- "block",
- Q(Y(
- Y("let", "input", Label),
- Y("let", "partitionKeySelector", PartitionKeySelector),
- Y("let", "partitionColumns", PartitionColumns),
- Y("let", "sortTraits", sortTraits),
- Y("let", "measures", measuresNode),
- Y("let", "rowsPerMatch", RowsPerMatch),
- Y("let", "skipTo", SkipTo),
- Y("let", "pattern", Pattern),
- Y("let", "subset", Subset ? Subset : Q("")),
- Y("let", "define", defineNode),
- Y("let", "res", Y("MatchRecognize",
- "input",
- "partitionKeySelector",
- "partitionColumns",
- "sortTraits",
- Y("MatchRecognizeParams",
- "measures",
- "rowsPerMatch",
- "skipTo",
- "pattern",
- "define"
- )
- )),
- Y("return", "res")
- ))
- );
- return true;
- }
- TNodePtr DoClone() const override {
- return MakeIntrusive<TMatchRecognize>(
- Pos,
- Label,
- PartitionKeySelector,
- PartitionColumns,
- SortSpecs,
- Measures,
- RowsPerMatch,
- SkipTo,
- Pattern,
- PatternVars,
- Subset,
- Definitions
- );
- }
- private:
- TString Label;
- TNodePtr PartitionKeySelector;
- TNodePtr PartitionColumns;
- TVector<TSortSpecificationPtr> SortSpecs;
- TVector<TNamedFunction> Measures;
- TNodePtr RowsPerMatch;
- TNodePtr SkipTo;
- TNodePtr Pattern;
- TNodePtr PatternVars;
- TNodePtr Subset;
- TVector<TNamedFunction> Definitions;
- };
- } // anonymous namespace
- TNodePtr TMatchRecognizeBuilder::Build(TContext& ctx, TString label, ISource* src) {
- const auto node = MakeIntrusive<TMatchRecognize>(
- Pos,
- std::move(label),
- std::move(PartitionKeySelector),
- std::move(PartitionColumns),
- std::move(SortSpecs),
- std::move(Measures),
- std::move(RowsPerMatch),
- std::move(SkipTo),
- std::move(Pattern),
- std::move(PatternVars),
- std::move(Subset),
- std::move(Definitions)
- );
- if (!node->Init(ctx, src)) {
- return {};
- }
- return node;
- }
- TNodePtr BuildMatchRecognizeColumnAccess(TPosition pos, TString var, TString column) {
- return MakeIntrusive<TMatchRecognizeColumnAccessNode>(pos, std::move(var), std::move(column));
- }
- TNodePtr BuildMatchRecognizeDefineAggregate(TPosition pos, TString name, TVector<TNodePtr> args) {
- const auto result = MakeIntrusive<TMatchRecognizeDefineAggregate>(pos, std::move(name), std::move(args));
- return BuildMatchRecognizeVarAccess(pos, std::move(result));
- }
- TNodePtr BuildMatchRecognizeVarAccess(TPosition pos, TNodePtr aggr) {
- return MakeIntrusive<TMatchRecognizeVarAccessNode>(pos, std::move(aggr));
- }
- } // namespace NSQLTranslationV1
|