|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.39 2000/08/13 02:50:04 tgl Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.40 2000/10/25 21:48:12 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -371,24 +371,40 @@ clause_selectivity(Query *root,
|
371 | 371 | return s1;
|
372 | 372 | if (IsA(clause, Var))
|
373 | 373 | {
|
| 374 | + Var *var = (Var *) clause; |
374 | 375 |
|
375 | 376 | /*
|
376 |
| - * we have a bool Var. This is exactly equivalent to the clause: |
377 |
| - * reln.attribute = 't' so we compute the selectivity as if that |
378 |
| - * is what we have. The magic #define constants are a hack. I |
379 |
| - * didn't want to have to do system cache look ups to find out all |
380 |
| - * of that info. |
| 377 | + * We probably shouldn't ever see an uplevel Var here, but if we |
| 378 | + * do, return the default selectivity... |
381 | 379 | */
|
382 |
| - Index varno = ((Var *) clause)->varno; |
383 |
| - |
384 |
| - if (varRelid == 0 || varRelid == (int) varno) |
385 |
| - s1 = restriction_selectivity(F_EQSEL, |
386 |
| - BooleanEqualOperator, |
387 |
| - getrelid(varno, root->rtable), |
388 |
| - ((Var *) clause)->varattno, |
389 |
| - BoolGetDatum(true), |
390 |
| - SEL_CONSTANT | SEL_RIGHT); |
391 |
| - /* an outer-relation bool var is taken as always true... */ |
| 380 | + if (var->varlevelsup == 0 && |
| 381 | + (varRelid == 0 || varRelid == (int) var->varno)) |
| 382 | + { |
| 383 | + RangeTblEntry *rte = rt_fetch(var->varno, root->rtable); |
| 384 | + |
| 385 | + if (rte->subquery) |
| 386 | + { |
| 387 | + /* |
| 388 | + * XXX not smart about subquery references... |
| 389 | + * any way to do better? |
| 390 | + */ |
| 391 | + s1 = 0.5; |
| 392 | + } |
| 393 | + else |
| 394 | + { |
| 395 | + /* |
| 396 | + * A Var at the top of a clause must be a bool Var. |
| 397 | + * This is equivalent to the clause reln.attribute = 't', |
| 398 | + * so we compute the selectivity as if that is what we have. |
| 399 | + */ |
| 400 | + s1 = restriction_selectivity(F_EQSEL, |
| 401 | + BooleanEqualOperator, |
| 402 | + rte->relid, |
| 403 | + var->varattno, |
| 404 | + BoolGetDatum(true), |
| 405 | + SEL_CONSTANT | SEL_RIGHT); |
| 406 | + } |
| 407 | + } |
392 | 408 | }
|
393 | 409 | else if (IsA(clause, Param))
|
394 | 410 | {
|
|
0 commit comments