home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / planner / sys / cfi.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  7.1 KB  |  272 lines

  1.  
  2. /*     
  3.  *      FILE
  4.  *         cfi
  5.  *     
  6.  *      DESCRIPTION
  7.  *         All planner routines that (directly) call C routines.
  8.  *     
  9.  *    $Header: /private/postgres/src/planner/sys/RCS/cfi.c,v 1.16 1991/11/18 17:29:14 mer Exp $
  10.  *      EXPORTS
  11.  *             intermediate-rule-lock-data
  12.  *             intermediate-rule-lock
  13.  *             print-rule-lock-intermediate
  14.  *             rule-insert-catalog
  15.  *             relation-info
  16.  *             index-info
  17.  *             index-selectivity
  18.  *             restriction-selectivity
  19.  *             join-selectivity
  20.  *             find-inheritance-children
  21.  *             find-version-parents
  22.  *             function-index-info
  23.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. #include "tmp/c.h"
  28. #include "nodes/pg_lisp.h"
  29. #include "planner/internal.h"
  30. #include "planner/plancat.h"
  31.  
  32. /*    
  33.  *        index-info
  34.  *    
  35.  *        Retrieves catalog information on an index on a given relation.
  36.  *    
  37.  *        The index relation is opened on the first invocation of
  38.  *        util/plancat.c:IndexCatalogInformation.  The current call retrieves
  39.  *        the next index relation within the catalog that has not already been
  40.  *        retrieved by a previous call.  The index catalog is closed by
  41.  *        IndexCatalogInformation when no more indices for 'relid' can be
  42.  *        found.
  43.  *    
  44.  *        See util/plancat.c:IndexCatalogInformation for the format of 
  45.  *        'indexinfo'.
  46.  *    
  47.  *        'not-first' is 0 if this is the first call 
  48.  *        'relid' is the OID of the relation for which indices are being located
  49.  *    
  50.  *        Returns the list of index info.
  51.  *    
  52.  */
  53.  
  54. /*  .. find-secondary-index    */
  55.  
  56.  
  57. List
  58. index_info (not_first,relid)
  59.      bool not_first;
  60.      ObjectId  relid ;
  61. {
  62.     int indexinfo[32];
  63.     int i = 0;
  64.     LispValue ikey = LispNil;
  65.     LispValue iord = LispNil;
  66.     LispValue am_ops = LispNil;
  67.     List returnList;
  68.     
  69.     /*    Retrieve information for a single index (if there is another one). */
  70.     if(1 == IndexCatalogInformation (not_first,
  71.                      CInteger(getrelid(relid,
  72.                                _query_range_table_)),
  73.                      _query_is_archival_,indexinfo)) {
  74.     /*    First extract the index OID and the basic statistics (pages,
  75.      *     tuples, and indexrelid), then collect the index keys, operators,
  76.      *     and operator classes into lists.  Only include the 
  77.      *     actual keys in the list, i.e., no 0's.
  78.      */
  79.     
  80.     /*    Keys over which we're indexing: */
  81.     
  82.     for (i = _MAX_KEYS_+2 ; i > 2; --i ) 
  83.       ikey = lispCons(lispInteger(indexinfo[i]),ikey);
  84.     
  85.     /*    Operators used by the index (for ordering purposes): */
  86.     
  87.     for (i = _MAX_KEYS_+10 ; i > 10; --i ) 
  88.       iord = lispCons(lispInteger(indexinfo[i]),iord);
  89.     
  90.     /*    Classes of the AM operators used by index: */
  91.     
  92.     for (i = _MAX_KEYS_+18 ; i > 18; --i ) 
  93.       am_ops = lispCons(lispInteger(indexinfo[i]),am_ops);
  94.  
  95.     /*
  96.      * Cons up a list the old fashioned way -- with an intermediate
  97.      * variable.  I just could not get the old statement to stop screen
  98.      * wrapping and becoming unreadable.  -mer
  99.      */
  100.     returnList = lispCons(lispInteger(indexinfo[27]), LispNil);
  101.     returnList = lispCons(am_ops, returnList);
  102.     returnList = lispCons(iord, returnList);
  103.     returnList = lispCons(ikey, returnList);
  104.     returnList = lispCons(lispInteger(indexinfo[2]), returnList);
  105.     returnList = lispCons(lispInteger(indexinfo[1]), returnList);
  106.     returnList = lispCons(lispInteger(indexinfo[0]), returnList);
  107.  
  108.     return (returnList);
  109.     }
  110.     return(NULL);
  111. }
  112.            
  113. /*    
  114.  *        index-selectivity
  115.  *    
  116.  *        Call util/plancat.c:IndexSelectivity with the indicated arguments.
  117.  *    
  118.  *        'indid' is the index OID
  119.  *        'classes' is a list of index key classes
  120.  *        'opnos' is a list of index key operator OIDs
  121.  *        'relid' is the OID of the relation indexed
  122.  *        'attnos' is a list of the relation attnos which the index keys over
  123.  *        'values' is a list of the values of the clause's constants
  124.  *        'flags' is a list of fixnums which describe the constants
  125.  *        'nkeys' is the number of index keys
  126.  *    
  127.  *        Returns a list of two floats: index pages and index selectivity.
  128.  *    
  129.  */
  130.  
  131. /*  .. best-or-subclause-index, create_index_path, index-innerjoin
  132.  */
  133. List
  134. index_selectivity (indid,classes,opnos,relid,attnos,values,flags,nkeys)
  135.      ObjectId     indid,relid;
  136.      List    attnos;
  137.      List     values;
  138.      List    opnos;
  139.      List    classes;
  140.      List    flags;
  141.      int32    nkeys ;
  142. {
  143.     int *class_array,*opno_array,*attno_array,*value_array,*flag_array;
  144.  
  145.     if (nkeys == length (opnos)  &&
  146.        nkeys == length (attnos) &&
  147.        nkeys == length (values) && 
  148.        nkeys == length (flags)) {
  149.     
  150.     float param[2];
  151.     int i = 0;
  152.     LispValue xclass,xopno,xattno,value,flag;
  153.  
  154.     class_array = (int *)palloc(_MAX_KEYS_*sizeof(ObjectId));
  155.     opno_array = (int *)palloc(nkeys*sizeof(ObjectId));
  156.     attno_array = (int *)palloc(nkeys*sizeof(int32));
  157.     value_array = (int *)palloc(nkeys*sizeof(char *));
  158.     flag_array = (int *)palloc(nkeys*sizeof(int32));
  159.  
  160.     foreach(xclass,classes) {
  161.         class_array[i++] = CInteger(CAR(xclass));
  162.     }
  163.     i = 0;
  164.     foreach(xopno,opnos) {
  165.         opno_array[i++] = LISPVALUE_INTEGER(xopno);
  166.     }
  167.       i = 0;
  168.     foreach(xattno,attnos) {
  169.         attno_array[i++] = CInteger(CAR(xattno));
  170.     }
  171.     i = 0;
  172.     foreach(value,values) {
  173.         if (IsA(CAR(value),LispStr))
  174.         value_array[i++] = (int)CString(CAR(value));
  175.         else
  176.             value_array[i++] = CInteger(CAR(value));
  177.     }
  178.     i = 0;
  179.     foreach(flag,flags) {
  180.         flag_array[i++] = CInteger(CAR(flag));
  181.     }
  182.  
  183.     IndexSelectivity (indid,relid,nkeys,
  184.               (ObjectId *)class_array,
  185.               (ObjectId *)opno_array,
  186.               attno_array,
  187.               (char **)value_array,
  188.               flag_array,
  189.               param);
  190.     
  191.     return (lispCons(lispFloat(param[0]),
  192.              lispCons(lispFloat(param[1]),LispNil)));
  193.     } else 
  194.       return(lispCons(lispFloat(0.0),
  195.               lispCons(lispFloat(1.0),LispNil)));
  196. }
  197.  
  198. /*    
  199.  *        restriction-selectivity  
  200.  *    
  201.  *        Returns the selectivity of an operator, given the restriction clause
  202.  *        information.
  203.  *      The routine is now merged with 
  204.  *      RestrictionClauseSelectivity as defined in plancat.c
  205.  *
  206.  *      This routine is called by comput_selec
  207.  */
  208.  
  209. /*    
  210.  *        join-selectivity
  211.  *    
  212.  *        Returns the selectivity of an operator, given the join clause
  213.  *        information.
  214.  *
  215.  *      Similarly, this routine is merged with JoinClauseSelectivity in
  216.  *      plancat.c
  217.  *      Routin is used in compute_selec.
  218.  */
  219.  
  220.  
  221. /*    
  222.  *        find-version-parents
  223.  *    
  224.  *        Returns all relations that are base relations for the version relation
  225.  *        represented by 'relation-oid'.
  226.  *    
  227.  */
  228.  
  229. /*  .. plan-union-queries
  230.  */
  231. LispValue
  232. find_version_parents (relation_oid)
  233.      LispValue relation_oid ;
  234. {
  235.     return( VersionGetParents (relation_oid,
  236.                     lispList()));
  237. }
  238.  
  239. /*    
  240.  *        function-index-info        XXX FUNCS
  241.  *    
  242.  *        Returns an integer iff 'function-oid' and 'index-oid' correspond
  243.  *        to a valid function-index mapping.
  244.  *    
  245.  */
  246. #ifdef funcindex
  247. /*  .. function-index-clause-p     */
  248.  
  249. int32
  250. function_index_info (function_oid,index_oid)
  251.      int32 function_oid,index_oid ;
  252. {
  253.      int32      info[4];
  254.      if (!zerop (FunctionIndexInformation (index_oid,info)) && 
  255.      info[0] == function_oid)
  256.        return(function_oid);
  257.  
  258. }
  259. #endif
  260. #ifndef funcindex
  261.  
  262. /*  .. function-index-clause-p    */
  263.  
  264. int32
  265. function_index_info (function_oid,index_oid)
  266.      int32 function_oid,index_oid ;
  267. {
  268.      return((int32)LispNil);
  269. }
  270. #endif
  271.  
  272.