Showing posts with label inline declaration. Show all posts
Showing posts with label inline declaration. Show all posts

Friday, July 29, 2022

Replacing LOOP statement with ABAP Filter Operator

I tried to compare how ABAP filter operator compete with LOOP statement.

 

1. Classic LOOP statement

On top of itab t_data that is being looped over into a structure a new itab t_data1 is generated from the structure. A condition for the LOOP specified as hardcoded into WHERE part of the LOOP statement.

LOOP AT t_data INTO s_data WHERE ( col_1 = 'AAA' ).

  INSERT s_data INTO TABLE t_data1.

ENDLOOP.

 

2. Filter Operator

A Filter Operator is used to build the itab t_data2 based on th_data itab. Condition is specified as hardcoded one in the filter.

DATA(t_data2) = FILTER #( th_data WHERE col_1 = 'AAA' ).

 

Next, let assume that we have a multiple conditions for the LOOP so they are stored in a special itabs.

3. Classic LOOP statement with WHERE condition as a range

In a case of the class LOOP statement I leveraged range type of itab. The range (t_filter itab) is prepared prior the LOOP. Afterwards the range in used in the WHERE part of the LOOP by IN statement.

t_filter = VALUE #( BASE t_filter ( opt = 'EQ' sign = 'I' low = 'AAA' ) ).
 
LOOP AT th_data INTO s_data WHERE col_1 IN t_filter.
  INSERT s_data INTO TABLE t_data3.
ENDLOOP.

 

4. FILTER Operator with filter as itab

A hash itab (th_filter) is prepared with the condition prior the FILTER operator statement. A new itab (t_data4) is prepared by Filter operator. The Filter operator uses IN statement to specified the itab with the filter.

th_filter = VALUE #( BASE th_filter ( col_1 = 'AAA' ) ).
 
DATA(t_data4) = FILTER #( th_data IN th_filter WHERE col_1 = col_1 ).

 

Conclusion

In my system based on component SAP_ABA 75, SP04 I achieved the fastest the run no 2. Means Filter operator where the condition was hardcoded.



Few more remarks:

To count the no of the itab entries I used REDUCE (Reduction Operator – part of Iteration expression). The statement basically goes over all the entries of the itab and adding 1 into n variable as specified in NEXT statement.

v_count = REDUCE i( INIT n = 0 FOR x_data IN t_data4 NEXT n = n + 1 ).

 

Yes, I’m aware that I could do this by lines (a row function) as seen below. However, I wanted to try more the reduce operator instead.

DATA(v_cnt) = lines( t_data1 )

 

To calculate total of figure that is stored in column col_f in all the itabs I used again Reduction Operator. Just in this case the value of the column col_f is being added up into n variable.

v_count = REDUCE i( INIT n = 0 FOR x_data IN t_data4 NEXT n = n + x_data-col_f ).

Source code available in my Gitgub gist zmm_loop_replacement

Wednesday, April 12, 2017

ABAP inline declaration doesn’t work in SAP BW

Since I started to use ABAP inline declarations I liked it much. Naturally I’m using it in all SAP NetWeaver environments where I do ABAP programming. However in case of SAP BW I recently faced problem applying the inline declarations.

In particular I programmed a Start routine within SAP BW’s transformation. I used the inline declarations in the Start routine. If I put the same code I had in the routine into standalone ABAP program - the code seemed to work. But if I placed the same code into the routine I got error message while trying to activate the transformation – “Error in Start Routine”.

The transformation couldn’t not be activated. I tried to do hopeless thing – to remove the inline declarations. So I redid the code w/o it. And surprisingly it worked!

The version of the SAP BW system I used was:

SAP_BW                  740     0009   SAPKW74009  SAP BW
SAP_ABA                 740     0009   SAPKA74009  Cross-Application Component
Kernel release          742
Compilation              AIX 1 6 00F79A484C00 use-pr20160324 Mar 30 20
Sup.Pkg lvl.             329
ABAP Load               1981
CUA load                 40


As the inline declarations are available in NW release 7.40, SP05 I assume it should work in SP09. However turned out that BW doesn’t really work with it. In case it is used standalone (e.g. ABAP report or FM) without encapsulating it into BW object like the transformation it doesn’t. I need to try to upgrade my system perhaps to BW 75 to see if it is solved in there :)