.ad 8
.bm 8
.fm 4
.bt $Copyright by SAP AG, 2002$$Page %$
.tm 12
.hm 6
.hs 3
.TT 1 $SQL$Project Distributed Database System$VKB722$
.tt 2 $$$
.TT 3 $$Unionhandling$1999-08-13$
***********************************************************
.nf
 
 
    ========== licence begin  GPL
    Copyright (C) 2000 SAP AG
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
 
.fo
.nf
.sp
MODULE  : Unionhandling
=========
.sp
Purpose : Two temporary resulttables are unioned
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :
 
        PROCEDURE
              k722_union_select (VAR m : tgg_mess_block);
 
        PROCEDURE
              k722_add_union_record (VAR t : tgg00_TransContext;
                    VAR selrec   : tkb7_select_param);
 
.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :
 
        FROM
              filesysteminterface_1 : VBD01;
 
        PROCEDURE
              b01move_filecontent (VAR t : tgg00_TransContext;
                    VAR source_id : tgg00_FileId;
                    VAR target_id : tgg00_FileId);
 
        PROCEDURE
              b01tcreate_file (VAR t : tgg00_TransContext;
                    VAR act_tree_id : tgg00_FileId);
 
        PROCEDURE
              b01destroy_file (VAR t : tgg00_TransContext;
                    VAR act_tree_id : tgg00_FileId);
 
      ------------------------------ 
 
        FROM
              filesysteminterface_7 : VBD07;
 
        PROCEDURE
              b07cadd_record (VAR t : tgg00_TransContext;
                    VAR act_tree_id : tgg00_FileId;
                    VAR b           : tgg00_Rec);
 
        PROCEDURE
              b07cappend_record (VAR t : tgg00_TransContext;
                    VAR file_id  : tgg00_FileId;
                    VAR tree_pos : tgg00_FilePos;
                    VAR b        : tgg00_Rec);
 
        PROCEDURE
              b07cnext_record (VAR t : tgg00_TransContext;
                    VAR act_tree_id : tgg00_FileId;
                    VAR rk          : tgg00_Lkey;
                    VAR set_result  : tgg00_BdSetResultRecord;
                    VAR tree_pos    : tgg00_FilePos;
                    VAR b           : tsp_buf);
 
        PROCEDURE
              b07cdel_record (VAR t : tgg00_TransContext;
                    VAR act_tree_id : tgg00_FileId;
                    VAR rk          : tgg00_Lkey);
 
      ------------------------------ 
 
        FROM
              ref_statistic : VBD73;
 
        PROCEDURE
              b73cmd_count (statement_kind : tgg00_RefInfoIndex);
 
        PROCEDURE
              b73cmds_count (statement_kind : tgg00_RefInfoIndex;
                    count : tsp00_Int4);
 
      ------------------------------ 
 
        FROM
              Configuration_Parameter : VGG01;
 
        VAR
              g01code      : tgg_code_globals;
 
      ------------------------------ 
 
        FROM
              Kernel_move_and_fill : VGG10;
 
        PROCEDURE
              g10fil (mod_id : tsp_c6;              mod_num  : tsp_int4;
                    obj_upb  : tsp_int4;
                    VAR obj  : tsp_key;             obj_pos  : tsp_int4;
                    length   : tsp_int4;            fillchar : char;
                    VAR e    : tgg_basis_error);
 
        PROCEDURE
              g10fil1 (mod_id : tsp_c6;             mod_num  : tsp_int4;
                    obj_upb   : tsp_int4;
                    VAR obj   : tgg_mess_info_buf;  obj_pos  : tsp_int4;
                    length    : tsp_int4;           fillchar : char;
                    VAR e     : tgg_basis_error);
 
        PROCEDURE
              g10mv1 (mod_id   : tsp_c6;            mod_num  : tsp_int4;
                    source_upb : tsp_int4;          dest_upb : tsp_int4;
                    VAR source : tsp_buf;           src_pos  : tsp_int4;
                    VAR destin : tsp_key;           dest_pos : tsp_int4;
                    length     : tsp_int4;
                    VAR e      : tgg_basis_error);
 
        PROCEDURE
              g10mv2 (mod_id   : tsp_c6;            mod_num  : tsp_int4;
                    source_upb : tsp_int4;          dest_upb : tsp_int4;
                    VAR source : tsp_buf;           src_pos  : tsp_int4;
                    VAR destin : tgg00_FileId;       dest_pos : tsp_int4;
                    length     : tsp_int4;
                    VAR e      : tgg_basis_error);
 
        PROCEDURE
              g10mv3 (mod_id   : tsp_c6;            mod_num  : tsp_int4;
                    source_upb : tsp_int4;          dest_upb : tsp_int4;
                    VAR source : tsp_buf;           src_pos  : tsp_int4;
                    VAR destin : tsp_buf;           dest_pos : tsp_int4;
                    length     : tsp_int4;
                    VAR e      : tgg_basis_error);
 
      ------------------------------ 
 
        FROM
              RTE-Extension-20 : VSP20;
 
        PROCEDURE
              s20ch4sw (val    : tsp_int4;
                    sourceswap : tsp_swap_kind;
                    VAR dest   : tgg_mess_info_buf;
                    di         : tsp_int4;
                    destswap   : tsp_swap_kind);
 
        FUNCTION
              s20or4a (VAR str : tsp_buf;
                    si : tsp_int4) : tsp_int4;
 
      ------------------------------ 
 
        FROM
              RTE-Extension-30 : VSP30;
 
        PROCEDURE
              s30lcm (VAR buf1   : tsp_buf;
                    fieldpos1    : tsp_int4;
                    fieldlength1 : tsp_int4;
                    VAR buf2     : tsp_buf;
                    fieldpos2    : tsp_int4;
                    fieldlength2 : tsp_int4;
                    VAR l_result : tsp_lcomp_result);
 
      ------------------------------ 
 
        FROM
              KB_trans_state: VKB50;
 
        PROCEDURE
              k50hash (VAR buf : tsp00_Buf;
                    pos        : tsp00_Int4;
                    len        : tsp00_Int4;
                    VAR resbuf : tsp00_Buf;
                    respos     : integer);
 
      ------------------------------ 
 
        FROM
              PUT-Conversions : VSP41;
 
        PROCEDURE
              s41p4int (VAR buf : tsp_resnum;
                    pos     : tsp_int4;
                    source  : tsp_int4;
                    VAR res : tsp_num_error);
&       ifdef TRACE
 
      ------------------------------ 
 
        FROM
              Test_Procedures : VTA01;
 
        PROCEDURE
              t01lkey (level : tgg00_Debug;
                    VAR k : tgg00_Lkey);
 
        PROCEDURE
              t01int4 (debug : tgg00_Debug;
                    nam      : tsp00_Sname;
                    int      : tsp00_Int4);
 
        PROCEDURE
              t01site (debug : tgg00_Debug;
                    nam      : tsp00_Sname;
                    site     : tgg00_ServerdbNo);
 
        PROCEDURE
              t01treeid  (level : tgg00_Debug;
                    nam      : tsp00_Sname;
                    VAR tree : tgg00_FileId);
&       endif
 
.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :
 
        PROCEDURE
              b07cnext_record;
 
              tsp00_MoveObj tsp_buf
 
        PROCEDURE
              g10fil;
 
              tsp_moveobj tsp_key
 
        PROCEDURE
              g10mv1;
 
              tsp_moveobj tsp_buf
              tsp_moveobj tsp_key
 
        PROCEDURE
              g10mv2;
 
              tsp_moveobj tsp_buf
              tsp_moveobj tgg00_FileId
 
        PROCEDURE
              g10mv3;
 
              tsp_moveobj tsp_buf
 
        PROCEDURE
              g10fil1;
 
              tsp_moveobj tgg_mess_info_buf
 
        PROCEDURE
              s20ch4sw;
 
              tsp_moveobj tgg_mess_info_buf
 
        FUNCTION
              s20or4a;
 
              tsp_moveobj tsp_buf
 
        PROCEDURE
              s30lcm;
 
              tsp_moveobj tsp_buf
 
        PROCEDURE
              s41p4int;
 
              tsp_moveobj tsp_resnum
 
.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  :
.sp
.cp 3
Created : 1991-04-26
.sp
.cp 3
Version : 2002-05-13
.sp
.cp 3
Release :      Date : 1999-08-13
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:
.sp 2
This module puts the results of two selects together. The results of
the first select are stored in the file with treeid resf_id and of
the second select in the file with treeid source_id. The unionresult
is put into resf_id-file.
.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 102
.fo
.oc _/1
Description:
.sp 2
PROCEDURE K722_UNION_SELECT
.sp 2
This is the kb-mainprocedure to put two select results together.
First the parameters given in messagebuffer part1 are used to
initialize the variables. In case of the union_kind some special
initializations are made. To process the union the procedure
kb722_process_union is called.
.br;In case of intersection all results, which are in both select
results were put into an auxiliaryfile. At last the auxiliaryfile
is moved to the resultfile.
.br;In case of union the results, of 2.nd select
results were added into the resultfile.
.br;In case of except the results, of 2.nd select
results were deleted from the resultfile.
.br;The resultfile of the 2.nd select is destroyed and if
no error is detected then the countresult, selr_key_cnt_offs and the
resulttreeid are moved into part1 of the returning messagebuffer.
.sp
PROCEDURE KB722_PROCESS_UNION
.sp 2
In a loop all resultrecords of the second select are read. For each
record k722_add_union_record is called.
.sp 2
PROCEDURE k722_add_union_record
.sp 2
First the unionkey is build by calling kb722_build_union_invkey.
Then a search for the unionkey in the resultfile is made. In case
of the union_kind union the record will be inserted into the resultfile
if no same record was found or if unionall is specified.
.br;In case of union_kind except and record was found into resultfile
it will be deleted there.
.br;If intersect is specified and the record was found into resultfile
then the record is inserted into an auxiliaryfile.
.sp 2
PROCEDURE KB722_BUILD_UNION_INVKEY
.sp 2
This procedure valuate a hash-value in case of the resultrecord and
puts it into the inv_key for returning.
.sp
.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:
 
.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
 
 
(*------------------------------*) 
 
PROCEDURE
      kb722_build_union_invkey (VAR result : tgg_rec_buf;
            VAR inv_key : tgg_lkey;
            VAR b_err   : tgg_basis_error);
 
VAR
      i      : integer;
      recpos : integer;
 
BEGIN
WITH result DO
    BEGIN
    recpos      := cgg_rec_key_offset + keylen + 2;
    inv_key.len := keylen;
    i           := len - keylen - cgg_rec_key_offset - 1;
    k50hash (buf, recpos, i, buf,
          cgg_rec_key_offset + keylen - mxgg_rescnt -
          cgg_cdistinct_bytes + 1);
    g10mv1 ('VKB722',   1,    
          sizeof(buf), sizeof(inv_key.k), buf, cgg_rec_key_offset+1,
          inv_key.k, 1, inv_key.len, b_err);
    g10fil ('VKB722',   2,    
          sizeof(inv_key.k), inv_key.k,
          inv_key.len - mxgg_rescnt + 1, mxgg_rescnt,
          chr (0), b_err);
&   ifdef trace
    t01int4  (kb, 'inv_key.len ', inv_key.len);
    t01lkey (kb, inv_key);
&   endif
    END
(*ENDWITH*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      kb722_process_union  (VAR m : tgg_mess_block;
            VAR selrec    : tkb7_select_param;
            VAR infile_id : tgg00_FileId);
 
VAR
      set_result : tgg_bd_set_result_record;
      next_error : tgg_basis_error;
      search_key : tgg_lkey;
      recpos     : integer;
      buflen     : integer;
      tree1_pos  : tgg00_FilePos;
 
BEGIN
WITH m, selrec, selr_selectbuffer DO
    BEGIN
    WITH set_result DO
        BEGIN
        bd_key_check_len := 0;
        bd_max_rec_cnt   := csp_maxint2;
        bd_max_fill_len  := mxsp_buf;
        bd_next          := false;
        bd_drop_page     := false;
        END;
    (*ENDWITH*) 
    search_key.len  := 0;
    tree1_pos.tpsPno_gg00 := NIL_PAGE_NO_GG00;
    mb_trns^.trError_gg00 := e_buffer_limit;
    WHILE mb_trns^.trError_gg00 = e_buffer_limit DO
        BEGIN
        b07cnext_record (mb_trns^, infile_id, search_key,
              set_result, tree1_pos, mb_data^.mbp_4kbuf);
        IF  mb_trns^.trRteCommPtr_gg00^.to_cancel
        THEN
            mb_trns^.trError_gg00 := e_cancelled;
        (*ENDIF*) 
        IF  mb_trns^.trError_gg00 = e_key_not_found
        THEN
            BEGIN
            next_error      := e_ok;
            mb_trns^.trError_gg00 := e_ok
            END
        ELSE
            IF  mb_trns^.trError_gg00 = e_buffer_limit
            THEN
                BEGIN
                next_error        := e_buffer_limit;
                m.mb_trns^.trError_gg00 := e_ok
                END
            ELSE
                next_error := mb_trns^.trError_gg00;
            (*ENDIF*) 
        (*ENDIF*) 
        IF  mb_trns^.trError_gg00 = e_ok
        THEN
            BEGIN
            recpos            := 0;
            buflen            := set_result.bd_fill_len;
&           ifdef trace
            t01int4 (kb, 'keylen      ', mb_data^.mbp_keylen);
            t01int4 (kb, 'reclen      ', mb_data^.mbp_reclen);
            t01int4 (kb, 'buflen      ', buflen);
&           endif
            WHILE (recpos          < buflen) AND
                  (mb_trns^.trError_gg00 = e_ok)       DO
                BEGIN
&               ifdef trace
                t01int4 (kb, 'recpos      ', recpos);
&               endif
                g10mv3 ('VKB722',   3,    
                      mb_data_size, sizeof(result.buf),
                      mb_data^.mbp_4kbuf, recpos + 1,
                      result.buf, 1, mb_data^.mbp_reclen,
                      mb_trns^.trError_gg00);
                IF  mb_trns^.trError_gg00 = e_ok
                THEN
                    BEGIN
                    k722_add_union_record (mb_trns^, selrec);
                    recpos := recpos + mb_data^.mbp_reclen;
                    END
                (*ENDIF*) 
                END;
            (*ENDWHILE*) 
            IF  m.mb_trns^.trError_gg00 = e_ok
            THEN
                m.mb_trns^.trError_gg00 := next_error
            (*ENDIF*) 
            END;
        (*ENDIF*) 
        END;
    (*ENDWHILE*) 
    IF  mb_trns^.trError_gg00 = e_no_next_record
    THEN
        mb_trns^.trError_gg00 := e_ok;
    (*ENDIF*) 
    END;
(*ENDWITH*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      k722_add_union_record (VAR t : tgg00_TransContext;
            VAR selrec   : tkb7_select_param);
 
VAR
      set_result : tgg_bd_set_result_record;
      is_equal   : boolean;
      lc         : tsp_lcomp_result;
      next_error : tgg_basis_error;
      recpos     : integer;
      buflen     : integer;
      tree1_pos  : tgg00_FilePos;
      inv_key    : tgg_lkey;
      res_key    : tgg_lkey;
 
BEGIN
WITH selrec, selr_selectbuffer, result DO
    BEGIN
    is_equal := false;
    IF  selr_append_all
    THEN
        BEGIN
        g10fil1 ('VKB722',   4,    
              sizeof(result.info), result.info,
              1, result.keylen - mxgg_rescnt, chr (0), t.trError_gg00);
        selr_countresult := succ (selr_countresult);
        s20ch4sw (selr_countresult + selr_key_cnt_offs,
              g01code.kernel_swap, result.info,
              result.keylen - 3, sw_normal);
        IF  t.trError_gg00 = e_ok
        THEN
            BEGIN
            b07cappend_record (t, selr_resf_id, selr_tree_pos, result);
            IF  t.trRteCommPtr_gg00^.to_cancel
            THEN
                t.trError_gg00 := e_cancelled;
            (*ENDIF*) 
            END
        (*ENDIF*) 
        END
    ELSE
        BEGIN
        kb722_build_union_invkey (result, inv_key, t.trError_gg00);
        WITH set_result DO
            BEGIN
            bd_key_check_len := inv_key.len - mxgg_rescnt;
            bd_max_rec_cnt   := csp_maxint2;
            bd_max_fill_len  := mxsp_buf;
            bd_next          := false;
            bd_drop_page     := false;
            END;
        (*ENDWITH*) 
        tree1_pos.tpsPno_gg00 := NIL_PAGE_NO_GG00;
        IF  (t.trError_gg00 = e_ok) AND
            NOT (selr_isunion AND selr_isall)
        THEN
            REPEAT
                b07cnext_record (t, selr_resf_id, inv_key,
                      set_result, tree1_pos, buf_rec.buf);
                is_equal := false;
                recpos   := 0;
                IF  t.trRteCommPtr_gg00^.to_cancel
                THEN
                    t.trError_gg00 := e_cancelled;
                (*ENDIF*) 
                IF  t.trError_gg00 = e_key_not_found
                THEN
                    BEGIN
                    next_error := e_ok;
                    t.trError_gg00   := e_ok
                    END
                ELSE
                    IF  t.trError_gg00 = e_buffer_limit
                    THEN
                        BEGIN
                        next_error := e_buffer_limit;
                        t.trError_gg00   := e_ok
                        END
                    ELSE
                        next_error := t.trError_gg00;
                    (*ENDIF*) 
                (*ENDIF*) 
                IF  t.trError_gg00 = e_ok
                THEN
                    BEGIN
                    buflen   := set_result.bd_fill_len;
                    is_equal := false;
                    WHILE (NOT is_equal) AND (recpos < buflen) DO
                        BEGIN
                        s30lcm (buf_rec.buf, recpos + keylen + cgg_rec_key_offset + 1,
                              len - keylen - cgg_rec_key_offset - 1,
                              result.buf, keylen + cgg_rec_key_offset + 1,
                              len - keylen - cgg_rec_key_offset - 1, lc);
                        is_equal := lc = l_equal;
                        recpos   := recpos + len
                        END;
                    (*ENDWHILE*) 
                    g10mv1 ('VKB722',   5,    
                          sizeof(buf_rec.buf), sizeof(res_key.k),
                          buf_rec.buf, recpos + cgg_rec_key_offset + 1 - len,
                          res_key.k, 1, keylen, t.trError_gg00);
                    res_key.len := keylen;
&                   ifdef TRACE
                    t01lkey (kb, res_key);
                    t01lkey (kb, inv_key);
&                   endif
                    END;
                (*ENDIF*) 
                IF  t.trError_gg00 = e_ok
                THEN
                    t.trError_gg00 := next_error;
                (*ENDIF*) 
            UNTIL
                is_equal OR (t.trError_gg00 <> e_buffer_limit);
            (*ENDREPEAT*) 
        (*ENDIF*) 
        IF  (t.trError_gg00 = e_no_next_record) OR
            (t.trError_gg00 = e_buffer_limit)
        THEN
            t.trError_gg00 := e_ok;
        (*ENDIF*) 
        IF  t.trError_gg00 = e_ok
        THEN
            BEGIN
            IF  selr_isexcept
            THEN
                IF  is_equal
                THEN
                    BEGIN
&                   ifdef TRACE
                    t01lkey (kb, res_key);
&                   endif
                    b07cdel_record (t, selr_resf_id, res_key);
                    selr_countresult := pred (selr_countresult);
                    IF  t.trError_gg00 = e_key_not_found
                    THEN
                        t.trError_gg00    := e_ok
                    (*ENDIF*) 
                    END;
                (*ENDIF*) 
            (*ENDIF*) 
            IF  selr_isintersect  AND
                is_equal
            THEN
                BEGIN
                IF  ((selr_countresult = 2) AND (selr_maxresult = 0))
                THEN
                    t.trError_gg00 := e_too_many_resultsets
                ELSE
                    BEGIN
                    (* *** intersected record is inserted into      *)
                    (*     selr_intersect_id auxfile            *** *)
                    selr_countresult := succ (selr_countresult);
                    s20ch4sw (selr_countresult + selr_key_cnt_offs,
                          g01code.kernel_swap, result.info,
                          result.keylen - 3, sw_normal);
                    b07cadd_record (t, selr_intersect_id, result);
                    (* *** delete union_id record. This makes        *)
                    (*     an one to one relation between the two    *)
                    (*     selects (intersect all)               *** *)
                    IF  t.trError_gg00 = e_ok
                    THEN
                        b07cdel_record (t, selr_resf_id, res_key);
                    (*ENDIF*) 
                    IF  t.trError_gg00 = e_ok
                    THEN
                        IF  selr_countresult = selr_maxresult
                        THEN
                            t.trError_gg00 := e_enough_resultsets;
                        (*ENDIF*) 
                    (*ENDIF*) 
                    END
                (*ENDIF*) 
                END;
            (*ENDIF*) 
            IF  selr_isunion          AND
                (selr_isall        OR
                (NOT is_equal)  )
            THEN
                BEGIN
                selr_countresult := succ (selr_countresult);
                s20ch4sw (selr_countresult + selr_key_cnt_offs,
                      g01code.kernel_swap, result.info,
                      result.keylen - 3, sw_normal);
                b07cadd_record (t, selr_resf_id, result);
                END
            (*ENDIF*) 
            END
        (*ENDIF*) 
        END
    (*ENDIF*) 
    END
(*ENDWITH*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      k722_union_select (VAR m : tgg_mess_block);
 
VAR
      union_kind : tsp00_Uint1;
      res        : tsp_num_error;
      ic2        : tsp_int_map_c2;
      old_cntres : tsp_int4;
      aux_error  : tgg_basis_error;
      source_id  : tgg00_FileId;
      selrec     : tkb7_select_param;
 
BEGIN
WITH m, selrec, selr_selectbuffer DO
    BEGIN
    IF  (selrec.selr_keyl > mxgg_rescnt) (* PTS 1113927 *)
    THEN
        b73cmd_count (iins_sorts);
    (*ENDIF*) 
    selr_resf_id := mb_qual^.mtree;
    g10mv2 ('VKB722',   6,    
          mb_qual_size, sizeof(source_id),
          mb_qual^.buf, mxgg_mb_part1head + 1,
          source_id, 1, sizeof(source_id), mb_trns^.trError_gg00);
    union_kind := ord (mb_qual^.buf[ mxgg_mb_part1head +
          mxgg_treeid + 1 ]);
    selr_append_all := union_kind > cgg_union_append;
    IF  selr_append_all
    THEN
        BEGIN
        union_kind          := union_kind - cgg_union_append;
        selr_tree_pos.tpsPno_gg00 := NIL_PAGE_NO_GG00;
        END;
    (*ENDIF*) 
    selr_isall := union_kind > 3;
    IF  selr_isall
    THEN
        union_kind := union_kind - 3;
    (*ENDIF*) 
    selr_isunion     := union_kind = cgg_union_byte;
    selr_isexcept    := union_kind = cgg_except_byte;
    selr_isintersect := union_kind = cgg_intersect_byte;
    IF  selr_isintersect
    THEN
        BEGIN
        selr_countresult              := 0;
        selr_intersect_id             := mb_qual^.mtree;
        selr_intersect_id.fileTfnTemp_gg00 := ttfnUnionIntersect_egg00;
        selr_intersect_id.fileRoot_gg00    := NIL_PAGE_NO_GG00;
        b01tcreate_file (mb_trns^, selr_intersect_id);
        END
    ELSE
        selr_countresult := s20or4a (mb_qual^.buf,
              mxgg_mb_part1head + mxgg_treeid + 2);
    (*ENDIF*) 
    old_cntres := selr_countresult;
    selr_key_cnt_offs := s20or4a (mb_qual^.buf,
          mxgg_mb_part1head + mxgg_treeid + 6);
    (* *** determine the number of records wanted *** *)
    ic2.map_c2 [ 1 ] := mb_qual^.buf[ mb_qual_len  - 1 ];
    ic2.map_c2 [ 2 ] := mb_qual^.buf[ mb_qual_len  ];
    IF  ic2.map_int = cgg_subquery_one_record
    THEN
        selr_maxresult := 0
    ELSE
        IF  ic2.map_int = cgg_at_least_one_record
        THEN
            selr_maxresult := 1
        ELSE
            selr_maxresult := csp_maxint4;
        (*ENDIF*) 
    (*ENDIF*) 
&   ifdef trace
    t01treeid (kb, 'result_id   ', selr_resf_id);
    t01treeid (kb, 'source_id   ', source_id);
    t01int4    (kb, 'countresult ', selr_countresult);
    t01int4    (kb, 'key_cnt_offs', selr_key_cnt_offs);
    t01int4    (kb, 'union_kind  ', ord(union_kind));
    t01int4    (kb, 'isunion     ', ord(selr_isunion));
    t01int4    (kb, 'isexcept    ', ord(selr_isexcept));
    t01int4    (kb, 'isintersect ', ord(selr_isintersect));
    t01int4    (kb, 'isall       ', ord(selr_isall));
    t01site   (kb, 'src_id.rsite', source_id.fileResultSite_gg00);
&   endif
    (* *** begin source_id file (2. select) handling *** *)
    IF  (m.mb_trns^.trError_gg00 =  e_ok)
    THEN
        kb722_process_union  (m, selrec, source_id);
    (* *** end   source_id file (2. select) handling *** *)
    (*ENDIF*) 
    IF  (mb_trns^.trError_gg00 = e_no_next_record)   OR
        (mb_trns^.trError_gg00 = e_enough_resultsets)
    THEN
        mb_trns^.trError_gg00 := e_ok;
    (*ENDIF*) 
    IF  selr_isexcept
    THEN
        IF  ((selr_countresult >= 2) AND (selr_maxresult = 0))
        THEN
            mb_trns^.trError_gg00 := e_too_many_resultsets;
        (*ENDIF*) 
    (*ENDIF*) 
    IF  selr_isintersect
    THEN
        BEGIN
        IF  (mb_trns^.trError_gg00 = e_ok)
        THEN
            b01move_filecontent (mb_trns^,
                  selr_intersect_id, selr_resf_id);
        (*ENDIF*) 
        aux_error := mb_trns^.trError_gg00;
        b01destroy_file (mb_trns^, selr_intersect_id);
        IF  aux_error <> e_ok
        THEN
            mb_trns^.trError_gg00 := aux_error;
        (*ENDIF*) 
        END;
    (*ENDIF*) 
    aux_error := mb_trns^.trError_gg00;
    b01destroy_file (mb_trns^, source_id);
    IF  aux_error <> e_ok
    THEN
        mb_trns^.trError_gg00 := aux_error;
    (*ENDIF*) 
    IF  mb_trns^.trError_gg00 = e_ok
    THEN
        (* *** put return parameters 'countresult',                        *)
        (*     'selr_key_cnt_offs', and 'resf_id' into message_buffer. *** *)
        BEGIN
        IF  (selr_countresult - old_cntres > 0) AND
            (selrec.selr_keyl > mxgg_rescnt) (* PTS 1113927 *)
        THEN
            b73cmds_count (iins_sorts_rows_ins,
                  (selr_countresult - old_cntres));
&       ifdef trace
        (*ENDIF*) 
        t01int4 (kb, 'countresult ', selr_countresult);
        t01int4 (kb, 'key_cnt_offs', selr_key_cnt_offs);
&       endif
        s41p4int (mb_qual^.mr_resnum, 2, selr_countresult, res);
        mb_qual^.mr_res_build   := true;
        mb_qual^.mr_resnum[ 1 ] := csp_defined_byte;
        mb_qual^.mr_restree     := selr_resf_id;
        s41p4int (mb_qual^.mr_keycnt, 2, selr_key_cnt_offs, res);
        mb_qual^.mr_keycnt[ 1 ] := csp_defined_byte;
        mb_qual_len  := mxgg_mb_part1head +
              2 * (sizeof(tsp_resnum) + 1) + mxgg_treeid;
        mb_data_len   := 0;
        mb_type       := m_return_result;
        mb_type2      := mm_file
        END
    (*ENDIF*) 
    END
(*ENDWITH*) 
END;
 
.CM *-END-* code ----------------------------------------
.SP 2 
***********************************************************
*-PRETTY-*  statements    :        191
*-PRETTY-*  lines of code :        490        PRETTYX 3.10 
*-PRETTY-*  lines in file :        888         1997-12-10 
.PA 
