@@ -24,9 +24,9 @@ namespace {
24
24
* Check if a callable is an attribute of some table
25
25
* Currently just return a boolean and cover only basic cases
26
26
*/
27
- TMaybe<TCoMember > IsAttribute (const TExprBase& input) {
27
+ std::optional<TString > IsAttribute (const TExprBase& input) {
28
28
if (auto member = input.Maybe <TCoMember>()) {
29
- return member.Cast ();
29
+ return TString ( member.Cast (). Name () );
30
30
} else if (auto cast = input.Maybe <TCoSafeCast>()) {
31
31
return IsAttribute (cast.Cast ().Value ());
32
32
} else if (auto ifPresent = input.Maybe <TCoIfPresent>()) {
@@ -42,8 +42,37 @@ namespace {
42
42
} else if (auto exists = input.Maybe <TCoExists>()) {
43
43
auto child = TExprBase (input.Ptr ()->ChildRef (0 ));
44
44
return IsAttribute (child);
45
+ } else if (auto argument = input.Maybe <TCoArgument>()) {
46
+ TString argumentName = TString (argument.Cast ().Name ());
47
+ TStringBuf olapApplyMemberPrefix = " members_" ;
48
+ if (argumentName.StartsWith (olapApplyMemberPrefix)) {
49
+ return argumentName.substr (olapApplyMemberPrefix.length (), argumentName.size () - olapApplyMemberPrefix.length ());
50
+ } else {
51
+ return argumentName;
52
+ }
45
53
}
54
+ return std::nullopt;
55
+ }
46
56
57
+ TMaybe<TCoMember> IsMember (const TExprBase& input) {
58
+ if (auto member = input.Maybe <TCoMember>()) {
59
+ return member.Cast ();
60
+ } else if (auto cast = input.Maybe <TCoSafeCast>()) {
61
+ return IsMember (cast.Cast ().Value ());
62
+ } else if (auto ifPresent = input.Maybe <TCoIfPresent>()) {
63
+ return IsMember (ifPresent.Cast ().Optional ());
64
+ } else if (auto just = input.Maybe <TCoJust>()) {
65
+ return IsMember (just.Cast ().Input ());
66
+ } else if (input.Ptr ()->IsCallable (" PgCast" )) {
67
+ auto child = TExprBase (input.Ptr ()->ChildRef (0 ));
68
+ return IsMember (child);
69
+ } else if (input.Ptr ()->IsCallable (" FromPg" )) {
70
+ auto child = TExprBase (input.Ptr ()->ChildRef (0 ));
71
+ return IsMember (child);
72
+ } else if (auto exists = input.Maybe <TCoExists>()) {
73
+ auto child = TExprBase (input.Ptr ()->ChildRef (0 ));
74
+ return IsMember (child);
75
+ }
47
76
return Nothing ();
48
77
}
49
78
@@ -223,12 +252,12 @@ double NYql::NDq::TPredicateSelectivityComputer::ComputeInequalitySelectivity(co
223
252
return ComputeInequalitySelectivity (right, left, GetOppositePredicateType (predicate));
224
253
}
225
254
226
- if (auto maybeMember = IsAttribute (left)) {
255
+ if (auto attribute = IsAttribute (left)) {
227
256
// It seems like this is not possible in current version.
228
257
if (IsAttribute (right)) {
229
258
return 0.3 ;
230
259
} else if (IsConstantExprWithParams (right.Ptr ())) {
231
- const TString attributeName = maybeMember. Get ()-> Name (). StringValue ();
260
+ const TString attributeName = attribute. value ();
232
261
if (!IsConstantExpr (right.Ptr ())) {
233
262
return DefaultSelectivity (Stats, attributeName);
234
263
}
@@ -259,26 +288,32 @@ double NYql::NDq::TPredicateSelectivityComputer::ComputeEqualitySelectivity(cons
259
288
return ComputeEqualitySelectivity (right, left);
260
289
}
261
290
262
- if (auto maybeMember = IsAttribute (left)) {
291
+ if (auto attribute = IsAttribute (left)) {
263
292
// In case both arguments refer to an attribute, return 0.2
264
- if (auto maybeAnotherMember = IsAttribute (right)) {
293
+ if (IsAttribute (right)) {
265
294
if (CollectMemberEqualities) {
266
- MemberEqualities.Add (*maybeMember.Get (), *maybeAnotherMember.Get ());
295
+ auto maybeMember = IsMember (left);
296
+ auto maybeAnotherMember = IsMember (right);
297
+ if (maybeMember && maybeAnotherMember) {
298
+ MemberEqualities.Add (*maybeMember.Get (), *maybeAnotherMember.Get ());
299
+ }
267
300
}
268
301
return 0.3 ;
269
302
}
270
303
// In case the right side is a constant that can be extracted, compute the selectivity using statistics
271
304
// Currently, with the basic statistics we just return 1/nRows
272
305
273
306
else if (IsConstantExprWithParams (right.Ptr ())) {
274
- TString attributeName = maybeMember. Get ()-> Name (). StringValue ();
307
+ TString attributeName = attribute. value ();
275
308
if (!IsConstantExpr (right.Ptr ())) {
276
309
return DefaultSelectivity (Stats, attributeName);
277
310
}
278
311
279
312
if (Stats == nullptr || Stats->ColumnStatistics == nullptr ) {
280
313
if (CollectColumnsStatUsedMembers) {
281
- ColumnStatsUsedMembers.AddEquality (*maybeMember.Get ());
314
+ if (auto maybeMember = IsMember (left)) {
315
+ ColumnStatsUsedMembers.AddEquality (*maybeMember.Get ());
316
+ }
282
317
}
283
318
return DefaultSelectivity (Stats, attributeName);
284
319
}
0 commit comments