@@ -16,43 +16,106 @@ TExprBase KqpDeleteOverLookup(const TExprBase& node, TExprContext& ctx, const TK
1616 auto deleteRows = node.Cast <TKqlDeleteRows>();
1717
1818 TMaybeNode<TKqlLookupTableBase> lookup;
19+ TMaybeNode<TKqlReadTable> read;
20+ TMaybeNode<TKqlReadTableRanges> rangeRead;
1921 TMaybeNode<TCoSkipNullMembers> skipNulMembers;
2022
2123 if (deleteRows.Input ().Maybe <TKqlLookupTableBase>()) {
2224 lookup = deleteRows.Input ().Cast <TKqlLookupTableBase>();
2325 } else if (deleteRows.Input ().Maybe <TCoSkipNullMembers>().Input ().Maybe <TKqlLookupTableBase>()) {
2426 skipNulMembers = deleteRows.Input ().Cast <TCoSkipNullMembers>();
2527 lookup = skipNulMembers.Input ().Cast <TKqlLookupTableBase>();
28+ } else if (deleteRows.Input ().Maybe <TKqlReadTable>()) {
29+ read = deleteRows.Input ().Cast <TKqlReadTable>();
30+ } else if (deleteRows.Input ().Maybe <TKqlReadTableRanges>()) {
31+ rangeRead = deleteRows.Input ().Cast <TKqlReadTableRanges>();
2632 } else {
2733 return node;
2834 }
2935
30- YQL_ENSURE (lookup);
31- if (deleteRows.Table ().Raw () != lookup.Cast ().Table ().Raw ()) {
32- return node;
33- }
34-
35- auto lookupKeysType = lookup.Cast ().LookupKeys ().Ref ().GetTypeAnn ();
36- auto lookupKeyType = lookupKeysType->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
37- YQL_ENSURE (lookupKeyType);
38-
39- // Only consider complete PK lookups
36+ TMaybeNode<TExprBase> deleteInput;
4037 const auto & tableDesc = GetTableData (*kqpCtx.Tables , kqpCtx.Cluster , deleteRows.Table ().Path ());
41- if (lookupKeyType->GetSize () != tableDesc.Metadata ->KeyColumnNames .size ()) {
42- return node;
43- }
44-
45- TExprBase deleteInput = lookup.Cast ().LookupKeys ();
46- if (skipNulMembers) {
47- deleteInput = Build<TCoSkipNullMembers>(ctx, skipNulMembers.Cast ().Pos ())
48- .Input (deleteInput)
49- .Members (skipNulMembers.Cast ().Members ())
38+ if (lookup) {
39+ if (deleteRows.Table ().Raw () != lookup.Cast ().Table ().Raw ()) {
40+ return node;
41+ }
42+
43+ auto lookupKeysType = lookup.Cast ().LookupKeys ().Ref ().GetTypeAnn ();
44+ auto lookupKeyType = lookupKeysType->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
45+ YQL_ENSURE (lookupKeyType);
46+
47+ // Only consider complete PK lookups
48+ if (lookupKeyType->GetSize () != tableDesc.Metadata ->KeyColumnNames .size ()) {
49+ return node;
50+ }
51+
52+ deleteInput = lookup.Cast ().LookupKeys ();
53+ if (skipNulMembers) {
54+ deleteInput = Build<TCoSkipNullMembers>(ctx, skipNulMembers.Cast ().Pos ())
55+ .Input (deleteInput.Cast ())
56+ .Members (skipNulMembers.Cast ().Members ())
57+ .Done ();
58+ }
59+ } else if (read) {
60+ if (deleteRows.Table ().Raw () != read.Cast ().Table ().Raw ()) {
61+ return node;
62+ }
63+
64+ const auto & rangeFrom = read.Cast ().Range ().From ();
65+ const auto & rangeTo = read.Cast ().Range ().To ();
66+
67+ if (!rangeFrom.Maybe <TKqlKeyInc>() || !rangeTo.Maybe <TKqlKeyInc>()) {
68+ return node;
69+ }
70+
71+ if (rangeFrom.Raw () != rangeTo.Raw ()) {
72+ return node;
73+ }
74+
75+ if (rangeFrom.ArgCount () != tableDesc.Metadata ->KeyColumnNames .size ()) {
76+ return node;
77+ }
78+
79+ TVector<TExprBase> structMembers;
80+ for (ui32 i = 0 ; i < rangeFrom.ArgCount (); ++i) {
81+ TCoAtom columnNameAtom (ctx.NewAtom (node.Pos (), tableDesc.Metadata ->KeyColumnNames [i]));
82+
83+ auto member = Build<TCoNameValueTuple>(ctx, node.Pos ())
84+ .Name (columnNameAtom)
85+ .Value (rangeFrom.Arg (i))
86+ .Done ();
87+
88+ structMembers.push_back (member);
89+ }
90+
91+ deleteInput = Build<TCoAsList>(ctx, node.Pos ())
92+ .Add <TCoAsStruct>()
93+ .Add (structMembers)
94+ .Build ()
5095 .Done ();
96+ } else if (rangeRead) {
97+ YQL_ENSURE (rangeRead);
98+ if (deleteRows.Table ().Raw () != rangeRead.Cast ().Table ().Raw ()) {
99+ return node;
100+ }
101+
102+ if (rangeRead.Cast ().Ranges ().Maybe <TCoVoid>()) {
103+ return node;
104+ }
105+
106+ auto prompt = TKqpReadTableExplainPrompt::Parse (rangeRead.Cast ());
107+ if (prompt.PointPrefixLen != tableDesc.Metadata ->KeyColumnNames .size ()) {
108+ return node;
109+ }
110+
111+ deleteInput = rangeRead.Cast ().PrefixPointsExpr ();
51112 }
52113
114+ YQL_ENSURE (deleteInput);
115+
53116 return Build<TKqlDeleteRows>(ctx, deleteRows.Pos ())
54117 .Table (deleteRows.Table ())
55- .Input (deleteInput)
118+ .Input (deleteInput. Cast () )
56119 .Done ();
57120}
58121
0 commit comments