@@ -462,40 +462,38 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::TopSort(TExprBase node,
462
462
}
463
463
464
464
465
- TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted (TExprBase node, TExprContext& ctx, const TGetParents& getParents) const {
465
+ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeConstraints (TExprBase node, TExprContext& ctx, const TGetParents& getParents) const {
466
466
if (State_->Types ->EvaluationInProgress || State_->PassiveExecution ) {
467
467
return node;
468
468
}
469
469
470
- auto assume = node.Cast <TCoAssumeSorted >();
470
+ auto assume = node.Cast <TCoInputBase >();
471
471
auto input = assume.Input ();
472
472
if (!IsYtProviderInput (input)) {
473
473
return node;
474
474
}
475
475
476
476
auto sorted = node.Ref ().GetConstraint <TSortedConstraintNode>();
477
- if (!sorted) {
478
- // Drop AssumeSorted with unsupported sort modes
479
- return input;
480
- }
481
477
482
478
auto maybeOp = input.Maybe <TYtOutput>().Operation ();
483
479
bool needSeparateOp = !maybeOp
484
480
|| maybeOp.Raw ()->StartsExecution ()
485
481
|| (maybeOp.Raw ()->HasResult () && maybeOp.Raw ()->GetResult ().Type () == TExprNode::World)
486
482
|| IsOutputUsedMultipleTimes (maybeOp.Ref (), *getParents ())
487
- || maybeOp.Maybe <TYtMapReduce>()
488
- || maybeOp.Maybe <TYtEquiJoin>();
483
+ || (sorted && maybeOp.Maybe <TYtMapReduce>() )
484
+ || (sorted && maybeOp.Maybe <TYtEquiJoin>() );
489
485
490
- bool canMerge = false ;
486
+ bool canMerge = !sorted ;
491
487
bool equalSort = false ;
492
- if (auto inputSort = input.Ref ().GetConstraint <TSortedConstraintNode>()) {
493
- if (sorted->IsPrefixOf (*inputSort)) {
494
- canMerge = true ;
495
- equalSort = sorted->Equals (*inputSort);
488
+ if (sorted) {
489
+ if (auto inputSort = input.Ref ().GetConstraint <TSortedConstraintNode>()) {
490
+ if (sorted->IsPrefixOf (*inputSort)) {
491
+ canMerge = true ;
492
+ equalSort = sorted->Equals (*inputSort);
493
+ }
496
494
}
497
495
}
498
- if (equalSort && maybeOp.Maybe <TYtSort>()) {
496
+ if (equalSort && maybeOp.Maybe <TYtSort>() && node. Ref (). GetAllConstraints (). size () == 1 /* only sort constraint */ ) {
499
497
return input;
500
498
}
501
499
@@ -508,9 +506,12 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
508
506
509
507
const bool useNativeDescSort = State_->Configuration ->UseNativeDescSort .Get ().GetOrElse (DEFAULT_USE_NATIVE_DESC_SORT);
510
508
511
- TKeySelectorBuilder builder (assume.Pos (), ctx, useNativeDescSort, outItemType);
512
- builder.ProcessConstraint (*sorted);
513
- needSeparateOp = needSeparateOp || (builder.NeedMap () && !equalSort);
509
+ THolder<TKeySelectorBuilder> builder;
510
+ if (sorted) {
511
+ builder = MakeHolder<TKeySelectorBuilder>(assume.Pos (), ctx, useNativeDescSort, outItemType);
512
+ builder->ProcessConstraint (*sorted);
513
+ needSeparateOp = needSeparateOp || (builder->NeedMap () && !equalSort && !maybeOp.Maybe <TYtDqProcessWrite>());
514
+ }
514
515
515
516
if (needSeparateOp) {
516
517
TYtOutTableInfo outTable (outItemType, State_->Configuration ->UseNativeYtTypes .Get ().GetOrElse (DEFAULT_USE_NATIVE_YT_TYPES) ? NTCF_ALL : NTCF_NONE);
@@ -533,14 +534,17 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
533
534
&& firstNativeType == path->GetNativeYtType ();
534
535
});
535
536
if (canMerge) {
536
- outTable.RowSpec ->CopySortness (ctx, *inputPaths.front ()->Table ->RowSpec , TYqlRowSpecInfo::ECopySort::WithDesc);
537
- outTable.RowSpec ->ClearSortness (ctx, sorted->GetContent ().size ());
537
+ if (sorted) {
538
+ outTable.RowSpec ->CopySortness (ctx, *inputPaths.front ()->Table ->RowSpec , TYqlRowSpecInfo::ECopySort::WithDesc);
539
+ outTable.RowSpec ->ClearSortness (ctx, sorted->GetContent ().size ());
540
+ }
538
541
outTable.SetUnique (assume.Ref ().GetConstraint <TDistinctConstraintNode>(), assume.Pos (), ctx);
542
+
539
543
if (firstNativeType) {
540
544
outTable.RowSpec ->CopyTypeOrders (*firstNativeType);
541
545
}
542
546
543
- YQL_ENSURE (sorted->GetContent ().size () == outTable.RowSpec ->SortMembers .size ());
547
+ YQL_ENSURE (!sorted || sorted->GetContent ().size () == outTable.RowSpec ->SortMembers .size ());
544
548
const bool useExplicitColumns = AnyOf (inputPaths, [] (const TYtPathInfo::TPtr& path) {
545
549
return !path->Table ->IsTemp || (path->Table ->RowSpec && path->Table ->RowSpec ->HasAuxColumns ());
546
550
});
@@ -570,14 +574,16 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
570
574
.Done ();
571
575
}
572
576
else {
573
- builder.FillRowSpecSort (*outTable.RowSpec );
577
+ if (builder) {
578
+ builder->FillRowSpecSort (*outTable.RowSpec );
579
+ }
574
580
outTable.SetUnique (assume.Ref ().GetConstraint <TDistinctConstraintNode>(), assume.Pos (), ctx);
575
581
576
- TCoLambda mapper = builder. NeedMap ()
582
+ TCoLambda mapper = builder && builder-> NeedMap ()
577
583
? Build<TCoLambda>(ctx, assume.Pos ())
578
584
.Args ({" stream" })
579
585
.Body <TExprApplier>()
580
- .Apply (TCoLambda (builder. MakeRemapLambda (true )))
586
+ .Apply (TCoLambda (builder-> MakeRemapLambda (true )))
581
587
.With (0 , " stream" )
582
588
.Build ()
583
589
.Done ()
@@ -587,17 +593,19 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
587
593
.Done ();
588
594
589
595
auto settingsBuilder = Build<TCoNameValueTupleList>(ctx, assume.Pos ());
590
- settingsBuilder
591
- .Add ()
592
- .Name ()
593
- .Value (ToString (EYtSettingType::KeepSorted))
594
- .Build ()
595
- .Build ()
596
- .Add ()
597
- .Name ()
598
- .Value (ToString (EYtSettingType::Ordered))
596
+ if (sorted) {
597
+ settingsBuilder
598
+ .Add ()
599
+ .Name ()
600
+ .Value (ToString (EYtSettingType::KeepSorted))
601
+ .Build ()
599
602
.Build ()
600
- .Build ();
603
+ .Add ()
604
+ .Name ()
605
+ .Value (ToString (EYtSettingType::Ordered))
606
+ .Build ()
607
+ .Build ();
608
+ }
601
609
if (State_->Configuration ->UseFlow .Get ().GetOrElse (DEFAULT_USE_FLOW)) {
602
610
settingsBuilder
603
611
.Add ()
@@ -625,21 +633,43 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
625
633
626
634
auto op = GetOutputOp (input.Cast <TYtOutput>());
627
635
TExprNode::TPtr newOp = op.Ptr ();
628
- if (!op.Maybe <TYtSort>()) {
636
+
637
+ if (builder && builder->NeedMap () && maybeOp.Maybe <TYtDqProcessWrite>()) {
638
+ TNodeOnNodeOwnedMap remaps;
639
+ VisitExpr (maybeOp.Cast <TYtDqProcessWrite>().Input ().Ptr (), [&builder, &remaps, &ctx](const TExprNode::TPtr& n) {
640
+ if (TYtOutput::Match (n.Get ())) {
641
+ // Stop traversing dependent operations
642
+ return false ;
643
+ }
644
+ if (TYtDqWrite::Match (n.Get ())) {
645
+ auto newInput = Build<TExprApplier>(ctx, n->Pos ())
646
+ .Apply (TCoLambda (builder->MakeRemapLambda (true )))
647
+ .With (0 , TExprBase (n->ChildPtr (TYtDqWrite::idx_Input)))
648
+ .Done ();
649
+ remaps[n.Get ()] = ctx.ChangeChild (*n, TYtDqWrite::idx_Input, newInput.Ptr ());
650
+ }
651
+ return true ;
652
+ });
653
+ newOp = ctx.ChangeChild (*newOp, TYtDqProcessWrite::idx_Input, ctx.ReplaceNodes (newOp->ChildPtr (TYtDqProcessWrite::idx_Input), remaps));
654
+ }
655
+
656
+ if (!op.Maybe <TYtSort>() && sorted) {
629
657
if (auto settings = op.Maybe <TYtTransientOpBase>().Settings ()) {
630
658
if (!NYql::HasSetting (settings.Ref (), EYtSettingType::KeepSorted)) {
631
- newOp = ctx.ChangeChild (op. Ref () , TYtTransientOpBase::idx_Settings, NYql::AddSetting (settings.Ref (), EYtSettingType::KeepSorted, {}, ctx));
659
+ newOp = ctx.ChangeChild (*newOp , TYtTransientOpBase::idx_Settings, NYql::AddSetting (settings.Ref (), EYtSettingType::KeepSorted, {}, ctx));
632
660
}
633
661
} else if (auto settings = op.Maybe <TYtFill>().Settings ()) {
634
662
if (!NYql::HasSetting (settings.Ref (), EYtSettingType::KeepSorted)) {
635
- newOp = ctx.ChangeChild (op. Ref () , TYtFill::idx_Settings, NYql::AddSetting (settings.Ref (), EYtSettingType::KeepSorted, {}, ctx));
663
+ newOp = ctx.ChangeChild (*newOp , TYtFill::idx_Settings, NYql::AddSetting (settings.Ref (), EYtSettingType::KeepSorted, {}, ctx));
636
664
}
637
665
}
638
666
}
639
667
if (!equalSort) {
640
668
const size_t index = FromString (input.Cast <TYtOutput>().OutIndex ().Value ());
641
669
TYtOutTableInfo outTable (op.Output ().Item (index));
642
- builder.FillRowSpecSort (*outTable.RowSpec );
670
+ if (builder) {
671
+ builder->FillRowSpecSort (*outTable.RowSpec );
672
+ }
643
673
outTable.RowSpec ->SetConstraints (assume.Ref ().GetConstraintSet ());
644
674
outTable.SetUnique (assume.Ref ().GetConstraint <TDistinctConstraintNode>(), assume.Pos (), ctx);
645
675
@@ -658,6 +688,7 @@ TMaybeNode<TExprBase> TYtPhysicalOptProposalTransformer::AssumeSorted(TExprBase
658
688
return Build<TYtOutput>(ctx, assume.Pos ())
659
689
.Operation (newOp)
660
690
.OutIndex (input.Cast <TYtOutput>().OutIndex ())
691
+ .Mode (sorted ? TMaybeNode<TCoAtom>() : input.Cast <TYtOutput>().Mode ())
661
692
.Done ();
662
693
}
663
694
0 commit comments