# History
# 	30-mar-2001 (gupsh01)
#		Modified to work correctly.
#	09-oct-2001 (gupsh01)
#		Modified the awk scripts to include DEC argument 
# 		This enables the decrement of the index also.
#

Adding new operators
--------------------

1.	Background
	----------

	This is a short document detailing how to add a new operator
	to the DBMS.

	Operators are listed in the operator table in 'adgoptab.c'.

	They map to one or more 'function instances' in the f.i. table
   'adgfitab.roc'.

	The f.i is mapped to the underlying function call by a fast
	lookup table 'adgfilkup.roc'.


2.	Links between the various tables
	--------------------------------

	This section illustrates the linkes between the various tables
	using the function 'seclabel_disjoint()' as an example.

a)	adgoptab.roc
	------------

/*----------------------------------------------------------------------------*/
/*  Each array element in the table looks like this:                 */
/*----------------------------------------------------------------------------*/
/*{ {op_name},          op_id,		type_of_op,	    op_usage,	      */
/*	valid_qlangs,	    valid_systems,				      */
/*		#_fi's,			    index_into_fi_table		    } */
/*----------------------------------------------------------------------------*/

example: seclabel_disjoint()

{ {"seclabel_disjoint"},  ADI_SECLABEL_DISJOINT_OP,	ADI_NORM_FUNC,	    
									^
									|
									operator id, unique
ADI_PREFIX, DB_SQL|DB_QUEL,	    ADI_INGRES_6,
		ADO_SECLABEL_DISJOINT_CNT,   ADZ_SECLABEL_DISJOINT_FIIDX},
										^
										|
										 == 535 = offset into adgfitab.roc

b)	adgfitab.roc
	------------

/*----------------------------------------------------------------------------*/
/*  Each array element in the function instance table looks like this:        */
/*----------------------------------------------------------------------------*/
/* /* [nnnn]:  Comment describing the function instance ...                   */
/*                                                                            */
/*  {   fi_ID,                          complment_fi_ID,                      */
/*          type_of_fi,         fi_flags,		op_ID,                */
/*              {     length_spec    },   aggr_w/s_dv_length, null_pre-instr, */
/*                  #_args,  result_dt,                                       */
/*                           arg_#1_dt,                                       */
/*                           arg_#2_dt    }                                   */
/*                                                                            */
/*----------------------------------------------------------------------------*/

example: seclabel_disjoint()
/*
** [ 535]:  secid  :  seclabel_disjoint(sec,sec)
*/
 {   ADFI_909_SEC_DISJOINT_SEC_SEC,                      ADI_NOFI,
			^
			|
			== 909 = offset into adgfilkup.roc

         ADI_NORM_FUNC,  ADI_F0_NOFIFLAGS,    ADI_SECLABEL_DISJOINT_OP,
													^
													|
													== 130 = operator id.
          { ADI_FIXED, 4, 0 },      0,        ADE_1CXI_SET_SKIP,
                 2,  DB_INT_TYPE,
                     DB_SEC_TYPE,
                     DB_SEC_TYPE  },


c)	adgfilkup.roc
	-------------

/*----------------------------------------------------------------------------*/
/*  Each array element in the function instance lookup table looks like this: */
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*  { pointer into      & f.i.          & agbgn         & agend        /* f.i.*/
/*     f.i. table,       routine,        routine,        routine }     /* ID  */
/*                                                                            */
/*----------------------------------------------------------------------------*/

example: seclabel_disjoint()


    { &Adi_fis[535],  adu_13seclbl_disjoint, NULL,      NULL },        /* 909 */

	Note that the fis index does NOT have to be the same as the lkup index.


3. Adding new operators
   --------------------

a) Adding entry to adgoptab.roc
   ----------------------------

   New operator requires:

   - correct new entry (see adgoptab.roc
   - new #define constant to identify the operator 
	(see common!adf!hdr	adfint.h)
   - new #define constant to identify the first function id
	  in adgfitab.roc for the operator
	(see common!adf!hdr	adffiids.h)

b) Adding entry to adgfitab.roc
   ----------------------------

   New FI requires:

   - correct new entry (see adgfitab.roc for format)
     which includes cross-ref to op-id (see section 1)

   - new #define constant with offset into adgfilkup.roc

	FIs must be correctly grouped by type:

	- Comparisons			(<>)
	- Operators 			(+/-)
	- Aggregate functions 	(any,all, max)
	- 'Normal' functions	(_bintim() etc)
	- 'Copy coercion' FIs
	- Coercion FIs.

	Within the group, the function instances must be
	sorted in operator sequence, e.g. all adds are
	consecutive.

	Adding/removing an entry from the f.i. table 
	will result in the offsets of all subsequent
	entries being moved up or down. 
	
	Assuming that our new function has been added
	to the end of the 'normals', 
	the following other changes are necessary:

	- adjust the 'pointer into f.i.' field of the entries in the
	  adgfilkup.roc table by the right amount for all the entries
	  moved up/down.

	- adjust the comment detailing the offset in the f.i. table
	  in adgfitab.roc

	'awk' scripts are provided at the end of this document
	to automate these last 2 chores.
	
	- increment the constants in common!adf!hdr  adfopfis.h
	which indicate the start of the 'copy coercion' and
	'coercion' groups


c) Adding entry to adgfilkup.roc
   -----------------------------

   Requires:

   - correct new entry (see adgfilkup.roc for format)
   - correct offset into adgfitab.roc
   - cross-ref to underlying function which implements.

d) Add new function
   ----------------

   Requires:

   - new module in common!adf!adu, or add to existing (relevant)
   module

   - add function prototype to common!adf!hdr aduint.h
4. 	Scripts to help the process
	--------------------------

4.1	Driver script
	--------------

-------------------------------------------------------------------------------
:
#-----------------------------------------------------------
#	script to renumber the 'offset into f.i.' tab fields
#	in adgfilkup.roc
#
#	Script assumes that functions have been added,
#	not removed.
#
#	Params:
#
#	NUM	-	number from which to start renumbering
#	INC	-	amount by which to renumber.
#
#	thus awk -f $1.awk -v NUM=659 v INC=1 .... 
#	will renumber all numbers after 659 adding one
#	to them (assumes new function added at position
#	659 in f.i. table)
# History
#	5-jul-1996 (angusm)
#		Initial version - will do until original
#		build tools for this are found
#-----------------------------------------------------------
if [ $# -ne 1 ]
then
	echo "Usage: $0 filkup | filtab"
	exit
fi
awk -f $1.awk NUM=659 INC=1 adg$1.roc >adg$1.tmp 

-------------------------------------------------------------------------------

Modified the awk files so that they work properly.
Run the following commands to generate the script 

  To add two functions after index 22 in the fitab file do the following.

  awk -f fitab.awk NUM=22 INC=2 adgfitab.roc > adgfitab.tmp
  awk -f filkup.awk NUM=23 INC=2 adgfilkup.roc > adgfilkup.tmp

  To change the header file adgopfis.h run in adf/hdr the following

 awk -f opfis.awk NUM=22 INC=2 adfopfis.h > adfopfis.tmp

  use awk -f opfis.awk NUM=xxx INC=y adfopfis.h > adfopfis.tmp
  then go to the operator entry in adfopfis.tmp and increase the CNT by y.


fitab.awk
----------
#-----------------------------------------------------------
#       script to renumber the 'offset into f.i.' tab fields
#       in adgfitab.roc
#
#       Script assumes that functions have been added,
#       not removed.
#
#       Params:
#
#       NUM     -       number from which to start renumbering
#       DEC     -       number from which to decrement for renumbering
#       INC     -       amount by which to renumber.
#
#       awk -f fitab.awk -v NUM=659 v INC=1 adgfitab.roc > adgfitab.tmp
#       This will renumber all numbers after 659 adding one
#       to them (assumes new function added at position
#       659 in f.i. table)
#
#       Decrement operation can also be carried out in the same manner.
#       eg.
#
#       awk -f fitab.awk NUM=996 DEC=4 adgfitab.roc > adgfitab.tmp
#       This will decrement all numbers from 996 on by 4.
#
# History
#       5-jul-1996 (angusm)
#               Initial version
#       30-mar-2001 (gupsh01)
#               Modified to work correctly.
#       05-oct-2001 (gupsh01)
#               Modified to work for 4 digit numbers.
#               Added DEC so the decrement operation can also be done
#-----------------------------------------------------------

BEGIN { }

$0 !~ /\[ [ 0-9][0-9][0-9][0-9]\]:/

$0 ~ /\[ [ 0-9][0-9][0-9][0-9]\]:/{
        ind = index($0, "[")
        num = int(substr($0, ind+2, 4))

        if (num < NUM)
        {
                print;
        }
        else if ((num == NUM) && (prev != NUM))
        {
                print;
        }
        else
        {
                if ( INC > 0 )
                num += INC
                if ( DEC > 0 )
                num -= DEC
                p1 = substr($0, 0, ind+1)
                ind = index($0, "]")
                p2 = substr($0, ind)
                printf("%s%4d%s\n", p1, num, p2);
        }
        prev = num;
}

filkup.awk
----------
#-----------------------------------------------------------
#       script to renumber the 'offset into f.i.' tab fields
#       in adgfilkup.roc
#
#       Script assumes that functions have been added,
#       not removed.
#
#       Params:
#
#       NUM     -       number from which to start renumbering
#       INC     -       amount by which to renumber.
#       DEC     -       number from which to decrement for renumbering
#
#       thus awk -f $1.awk -v NUM=659 v INC=1 ....
#       will renumber all numbers after 659 adding one
#       to them (assumes new function added at position
#       659 in f.i. table)
#       Decrement operation can also be carried out in the same manner.
#       eg.
#
#       awk -f filkup.awk NUM=996 DEC=4 adgfilkup.roc > adgfilkup.tmp
#       This will decrement all numbers from 996 on by 4.
#
# History
#       5-jul-1996 (angusm)
#               Initial version - will do until original
#               build tools for this are found
#       30-mar-2001 (gupsh01)
#               Modified to work correctly.
#       05-oct-2001 (gupsh01)
#               Added DEC so the decrement operation can also be done
#--------------------------------------------------------------------------

BEGIN { }

$0 !~ /&Adi_fis\[[0-9 ][0-9 ][0-9 ][0-9 ]\]/

/&Adi_fis\[[0-9 ][0-9 ][0-9 ][0-9 ]\]/ {

        ind = index($0, "&Adi_fis[");
        ind += 9;
        num = int(substr($0, ind, 4));
        if (num < NUM)
        {
                print;
        }
        else
        {
                if ( INC > 0 )
                num += INC
                if ( DEC > 0 )
                num -= DEC

                ind--
                p1 = substr($0, 0, ind);
                ind += 5;
                p2 = substr($0, ind);
                printf("%s%4d%s\n", p1, num, p2);
        }
        prev = num }


opfis.awk
----------
:
#-----------------------------------------------------------
#       script to renumber the offset values in adfopfis.h.'  
#
#       Script assumes that functions have been added,
#       not removed.
#
#       Params:
#
#       NUM     -       number from which to start renumbering
#       INC     -       amount by which to renumber.
#
#       use 
#       awk -f opfis.awk NUM=xxx INC=y adfopfis.h > adfopfis.tmp
#       then go to the operator entry in adfopfis.tmp and 
#       increase the CNT by y.
#       
# History
#       30-mar-2001 (gupsh01)
#	    Added.
#-----------------------------------------------------------


BEGIN { }

$0 !~ /[0-9 ]/

/[0-9 ]/ {

        header = substr($0, 0, 7);
        num = int($3)
        variable = $2
        string = substr($2, 0, 3);

        if (header != "#define" || num < NUM || string == "ADO" )
        {
                print;
        }
        else
        {
                num += INC
                printf("#define \t\t %s \t\t %4d\n", variable,num);
        }
}

if we run out of column space in adgflkup.roc and  need to increase 
the coloumn space by one then run addone. 

addone.awk : changes awailable space from 3 to 4.
----------
awk -f addone.awk adgfilkup.roc > adgfilkup.tmp


BEGIN { }

$0 !~ /&Adi_fis\[[0-9 ][0-9 ][0-9]\]/

/&Adi_fis\[[0-9 ][0-9 ][0-9]\]/ {

        ind = index($0, "&Adi_fis[");
        ind += 9;
        num = int(substr($0, ind, 3));
        ind--
        p1 = substr($0, 0, ind);
        ind += 4;
        p2 = substr($0, ind);
        printf("%s%4d%s\n", p1, num, p2);
        prev = num
}


5.	Example of new function added
	-----------------------------

	t.b.d
