Skip to Content

Strange behaviour of the FILTER operator

Hi ABAP experts,

I recently played around a little with table expressions. When using the FILTER operator I encountered an unexpected behaviour. Consider the following sample program:

REPORT zcd_test_filter.

TYPES: BEGIN OF grid_element,
         row   TYPE int4,
         col   TYPE int4,
         value TYPE string,
       END OF grid_element.

TYPES grid_type TYPE SORTED TABLE OF grid_element WITH UNIQUE KEY row col.

DATA(grid) = VALUE grid_type(
    ( row = 1 col = 1 value = 'Element 1,1')
    ( row = 1 col = 2 value = 'Element 1,2')
    ( row = 1 col = 3 value = 'Element 1,3')
    ( row = 2 col = 1 value = 'Element 2,1')
    ( row = 2 col = 2 value = 'Element 2,2')
    ( row = 2 col = 3 value = 'Element 2,3') ).

DATA(filtered_grid) = FILTER #( grid WHERE row <= 2 AND col <= 2 ).

cl_demo_output=>display( filtered_grid ).

I would have expected that the filtered_grid would contain the following elements:

    ( row = 1 col = 1 value = 'Element 1,1')
    ( row = 1 col = 2 value = 'Element 1,2')
    ( row = 2 col = 1 value = 'Element 2,1')
    ( row = 2 col = 2 value = 'Element 2,2')

However, when executing the program the filtered_grid only contains the following elements:

    ( row = 1 col = 1 value = 'Element 1,1')
    ( row = 1 col = 2 value = 'Element 1,2')

Can anyone explain this behaviour to me? I consider this result a bug. However, I might be missing something.

Thanks

Christian

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    Jan 30, 2017 at 07:29 AM

    I tested in a 7.50 system and found the same wrong behavior.

    ROW COL VALUE
    1 1 Element 1,1
    1 2 Element 1,2

    I tested in 7.51 (SP01) and everything is fine.

    ROW COL VALUE
    1 1 Element 1,1
    1 2 Element 1,2
    2 1 Element 2,1
    2 2 Element 2,2

    A kernel correction is available with SAP Note 2306379, "Filter by value with the relational operators <= or >= does not work as expected". The correction goes back to Kernel 7.42 (ABAP 7.40, SP08).

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 29, 2017 at 09:44 AM

    I also think that this is a bug. The documentation says that FILTER is an optimized shorthand for the corresponding FOR expression. In this case I would write as follows.

    DATA(filtered_grid) = VALUE grid_type( FOR line IN grid
                                           WHERE ( row <= 2 AND col <= 2 )
                                           ( line ) ).
    

    And this expression creates the expected result.

    Christian

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 29, 2017 at 05:45 PM

    Hello Christian,

    As per the latest FILTER documentation,

    A condition for the table key used in the FILTER expression must be specified after WHERE:

    • In the case of a hash key, precisely one comparison expression c op f for each key component. The only relational operator allowed for op is =.
    • In the case of a sorted key, an initial part of the key must be covered by comparison expressions c op f. op can be any binary relational operator.

    Now if i take the phrase "initial part of the key must be covered" and cross reference it with the documentation on LOOP...WHERE optimization,

    it seems like the initial part of the key can be covered only with the '=' operator.

    BR,

    Suhas

    PS - I agree with JNN that the compiler should raise a "syntax error" as in case of LOOP...WHERE.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 28, 2017 at 11:54 AM

    I can reproduce the bug in 7.50 SP02.

    JNN

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 28, 2017 at 01:14 PM

    At the first glance, it's very surprising, and the documentation doesn't give an explanation for this behavior, but after thinking, I guess it's normal and only a bug in the documentation.

    I guess it's normal because FILTER does not support standard tables, which surprised me a lot at the beginning, so I think the ABAP guys did it on purpose so that the performance is always good (so, the first location is found using a binary search). It also means that the FILTER behavior should avoid a full table scan. Consequently, it stops at the first line which does not meet the criteria.

    (PS: little bug in your question, the second code portion shows the 2 last lines with row = 1 instead of row = 2)

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 09, 2017 at 09:42 AM

    Hi Experts,

    I think the bug is with less that equal sign.

    DATA(filtered_grid)= FILTER#( grid WHERE row >=1 AND col >=2).

    I tried the above line, it gave expected result in 7.40.

    Regards,

    Gokul

    Add comment
    10|10000 characters needed characters exceeded