Make eval_const_expressions simplify FieldSelect from a whole-row Var
authorTom Lane
Fri, 14 Mar 2003 00:55:17 +0000 (00:55 +0000)
committerTom Lane
Fri, 14 Mar 2003 00:55:17 +0000 (00:55 +0000)
into an ordinary one-field Var.  Per example from Chris Mungall.

src/backend/optimizer/util/clauses.c

index fcd583741893133c4b7b5cf31c15d749b6581761..bdad4d9b811f91b866a10a5c3d4e0c75e943dc01 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.131 2003/03/10 03:53:50 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.132 2003/03/14 00:55:17 tgl Exp $
  *
  * HISTORY
  *   AUTHOR            DATE            MAJOR EVENT
@@ -1497,6 +1497,28 @@ eval_const_expressions_mutator(Node *node, List *active_fns)
        newcoalesce->args = newargs;
        return (Node *) newcoalesce;
    }
+   if (IsA(node, FieldSelect))
+   {
+       /*
+        * We can optimize field selection from a whole-row Var into a
+        * simple Var.  (This case won't be generated directly by the
+        * parser, because ParseComplexProjection short-circuits it.
+        * But it can arise while simplifying functions.)  If the argument
+        * isn't a whole-row Var, just fall through to do generic processing.
+        */
+       FieldSelect *fselect = (FieldSelect *) node;
+       Var        *argvar = (Var *) fselect->arg;
+
+       if (argvar && IsA(argvar, Var) &&
+           argvar->varattno == InvalidAttrNumber)
+       {
+           return (Node *) makeVar(argvar->varno,
+                                   fselect->fieldnum,
+                                   fselect->resulttype,
+                                   fselect->resulttypmod,
+                                   argvar->varlevelsup);
+       }
+   }
 
    /*
     * For any node type not handled above, we recurse using