home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / planner / util / clauseinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  6.3 KB  |  256 lines

  1.  
  2. /*     
  3.  *      FILE
  4.  *         clauseinfo
  5.  *     
  6.  *      DESCRIPTION
  7.  *         ClauseInfo node manipulation routines.
  8.  *      $Header: /private/postgres/src/planner/util/RCS/clauseinfo.c,v 1.11 1992/07/12 10:59:41 joey Exp $ 
  9.  */
  10.  
  11. /*     
  12.  *      EXPORTS
  13.  *             valid-or-clause
  14.  *             get-actual-clauses
  15.  *             get_relattvals
  16.  *             get_joinvars
  17.  *             get_opnos
  18.  */
  19.  
  20. #include "tmp/c.h"
  21.  
  22. #include "nodes/relation.h"
  23. #include "nodes/relation.a.h"
  24.  
  25. #include "planner/internal.h"
  26. #include "planner/clauses.h"
  27. #include "planner/clauseinfo.h"
  28.  
  29. /*    
  30.  *        valid-or-clause
  31.  *    
  32.  *        Returns t iff the clauseinfo node contains a 'normal' 'or' clause.
  33.  *    
  34.  */
  35.  
  36. /*  .. create-or-index-paths, match-index-orclauses, set_rest_selec
  37.  */
  38.  
  39. bool
  40. valid_or_clause (clauseinfo)
  41.     LispValue clauseinfo ;
  42. {
  43.      if (clauseinfo != LispNil && 
  44.      !single_node (get_clause ((CInfo)clauseinfo)) && 
  45.      !get_notclause ((CInfo)clauseinfo) &&
  46.      or_clause ((LispValue)get_clause ((CInfo)clauseinfo))) 
  47.        return(true);
  48.      else
  49.        return(false);
  50. }
  51.  
  52. /*    
  53.  *        get-actual-clauses
  54.  *    
  55.  *        Returns a list containing the clauses from 'clauseinfo-list'.
  56.  *    
  57.  */
  58.  
  59. /*  .. create_indexscan_node, create_join_node, create_scan_node
  60.  */
  61. LispValue
  62. get_actual_clauses (clauseinfo_list)
  63.      LispValue clauseinfo_list ;
  64. {
  65.   LispValue temp = LispNil;
  66.   LispValue result = LispNil;
  67.   CInfo clause = (CInfo)NULL;
  68.  
  69.   foreach(temp,clauseinfo_list) {
  70.     clause = (CInfo)CAR(temp);
  71.     result = nappend1(result,(LispValue)get_clause(clause));
  72.   }
  73.   return(result);
  74. }
  75.  
  76. /*    
  77.  *     XXX NOTE:
  78.  *        The following routines must return their contents in the same order
  79.  *        (e.g., the first clause's info should be first, and so on) or else
  80.  *        get_index_sel() won't work.
  81.  *    
  82.  */
  83.  
  84. /*    
  85.  *        get_relattvals
  86.  *    
  87.  *        For each member of  a list of clauseinfo nodes to be used with an
  88.  *        index, create a vectori-long specifying:
  89.  *            the attnos,
  90.  *            the values of the clause constants, and 
  91.  *            flags indicating the type and location of the constant within
  92.  *                each clause.
  93.  *        Each clause is of the form (op var some_type_of_constant), thus the
  94.  *        flag indicating whether the constant is on the left or right should
  95.  *        always be *SELEC-CONSTANT-RIGHT*.
  96.  *    
  97.  *        'clauseinfo-list' is a list of clauseinfo nodes
  98.  *    
  99.  *        Returns a list of vectori-longs.
  100.  *    
  101.  */
  102.  
  103. /*  .. create_index_path
  104.  */
  105. LispValue
  106. get_relattvals (clauseinfo_list)
  107.      LispValue clauseinfo_list ;
  108. {
  109.      LispValue result1 = LispNil;
  110.      LispValue result2 = LispNil;
  111.      LispValue result3 = LispNil;
  112.      LispValue relattvals = LispNil;
  113.      CInfo temp = (CInfo)NULL;
  114.      LispValue i = LispNil;
  115.      LispValue temp2 = LispNil;
  116.      
  117.      foreach (i,clauseinfo_list) {
  118.      temp = (CInfo)CAR(i);
  119.      relattvals = nappend1(relattvals,
  120.                    (LispValue)get_relattval (get_clause (temp)));
  121.      }
  122.  
  123.      foreach(i,relattvals) {
  124.      temp2 = CAR(i);
  125.      result1 = nappend1(result1,CADR(temp2));
  126.      }
  127.      foreach(i,relattvals) {
  128.      temp2 = CAR(i);
  129.      result2 = nappend1(result2,CADDR(temp2));
  130.      }
  131.      foreach(i,relattvals) {
  132.      temp2 = CAR(i);
  133.      result3 = nappend1(result3,CADDR(CDR(temp2)));
  134.      }
  135.      return(lispCons(result1,
  136.              lispCons(result2,
  137.                   lispCons(result3, LispNil))));
  138. }
  139.  
  140. /*    
  141.  *        get_joinvars 
  142.  *    
  143.  *        Given a list of join clauseinfo nodes to be used with the index
  144.  *        of an inner join relation, return three lists consisting of:
  145.  *            the attributes corresponding to the inner join relation
  146.  *            the value of the inner var clause (always "")
  147.  *            whether the attribute appears on the left or right side of
  148.  *                the operator.
  149.  *    
  150.  *        'relid' is the inner join relation
  151.  *        'clauseinfo-list' is a list of qualification clauses to be used with
  152.  *            'rel'
  153.  *    
  154.  */
  155.  
  156. /*  .. index-innerjoin
  157.  */
  158. LispValue
  159. get_joinvars (relid,clauseinfo_list)
  160.      LispValue relid,clauseinfo_list ;
  161. {
  162.      LispValue result1 = LispNil;
  163.      LispValue result2 = LispNil;
  164.      LispValue result3 = LispNil;
  165.      LispValue relattvals = LispNil;
  166.      LispValue temp;
  167.      
  168.      foreach(temp,clauseinfo_list) {
  169.       relattvals = nappend1(relattvals,
  170.                 get_joinvar((ObjectId)CInteger(relid),
  171.                         (CInfo)CAR(temp)));
  172.      }
  173.      
  174.      foreach(temp,relattvals) {
  175.       result1 = nappend1(result1,CAR(CAR(temp)));
  176.      }
  177.      foreach(temp,relattvals) {
  178.       result2 = nappend1(result2,CADR(CAR(temp)));
  179.      }
  180.      foreach(temp,relattvals) {
  181.       result3 = nappend1(result3,CADDR(CAR(temp)));
  182.      }
  183.      return(lispCons (result1,
  184.               lispCons(result2,
  185.                    lispCons(result3,LispNil))));
  186. }
  187.  
  188. /*    
  189.  *        get_joinvar
  190.  *    
  191.  *        Given a clause and a relid corresponding to one of the clause operands,
  192.  *        create a list consisting of:
  193.  *            the var's attno,
  194.  *            value, and
  195.  *            a flag indicating whether the join operand in a clause whose
  196.  *                relation corresponds to input 'relid' appears to the
  197.  *                right or left of the operator.
  198.  *        The operand corresponding to 'relid' is always a var node and "" is 
  199.  *        always returned for 'value'.
  200.  *    
  201.  *        Returns a list containing the above information.
  202.  *    
  203.  */
  204.  
  205. /*  .. get_joinvars
  206.  */
  207. LispValue
  208. get_joinvar (relid,clauseinfo)
  209.      ObjectId relid;
  210.      CInfo clauseinfo ;
  211. {
  212.     LispValue clause = get_clause (clauseinfo);
  213.  
  214.     if( IsA (get_leftop (clause),Var) &&
  215.        (relid == get_varno( get_leftop(clause)))) {
  216.  
  217.     return(lispCons(lispInteger(get_varattno(get_leftop(clause))),
  218.               lispCons(lispString(""),
  219.                    lispCons(lispInteger
  220.                         (_SELEC_CONSTANT_RIGHT_),
  221.                         LispNil))));
  222.      } else {
  223.      return(lispCons (lispInteger(get_varattno(get_rightop(clause))),
  224.               lispCons(lispString(""),
  225.                    lispCons(lispInteger(_SELEC_CONSTANT_LEFT_),
  226.                         LispNil))));
  227.      } 
  228. }
  229.  
  230. /*    
  231.  *        get_opnos
  232.  *    
  233.  *        Create and return a list containing the clause operators of each member
  234.  *        of a list of clauseinfo nodes to be used with an index.
  235.  *    
  236.  */
  237.  
  238. /*  .. create_index_path, index-innerjoin
  239.  */
  240. LispValue
  241. get_opnos (clauseinfo_list)
  242.      LispValue clauseinfo_list ;
  243. {
  244.     CInfo temp = (CInfo)NULL;
  245.     LispValue result = LispNil;
  246.     LispValue i = LispNil;
  247.  
  248.      foreach(i,clauseinfo_list) {
  249.      temp = (CInfo)CAR(i);
  250.       result =
  251.         nappend1(result,
  252.              (LispValue)get_opno((Oper)get_op( (LispValue)get_clause(temp))));
  253.      }
  254.     return(result);
  255. }
  256.