ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Strange SQL PL compile bug

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange SQL PL compile bug

    I'm writing a simple UDTF with a cursor for loop and pipe() as a demo for less-sql-knowledgeable colleagues, and I'm encountering a strange compile issue that I don't understand.

    Here is the source:
    Code:
    -- Requires TZTEST in library list to compile
    create or replace function TZTEST/SQLTFPPF_TableFuncExample_SQLPLPipe (
      filter1                varchar(20) default null
    )
      returns table (
      Column1                varchar(20),
      Column2                varchar(20),
      Column3                varchar(20),
      Column4                varchar(50)
    )
    
      specific SQLTFPPF
    
      statement deterministic
      reads sql data
    
      set option dbgview = *source,
                 commit  = *none,
                 output = *print
    
      begin
        declare Col1Out varchar(20);
        declare Col2Out varchar(20);
        declare Col3Out varchar(20);
        declare Col4Out varchar(50);
    
        for myCursor as
          select Col1,
                 case when Col2 in ('3','4') then null else Col2 end as Col2,
                 Col3
            from SQLEGPB1
            -- Apply filter to col1. Unless it's null parameter is set which
            -- for our purposes means match anything
           where (
             filter1 is null
             or Col1 like '%'||filter1||'%'
           )
          do
    
          -- Any SQL postprocessing of results?
    
          -- Col1 is straight output
          set Col1Out = Col1;
    
          
          -- ***ERROR START***
          
          -- This way of mapping Col2 to Col2Out works
          set Col2Out = Col2;
          if Col2Out in('3','4') then
            set Col2Out = null;
          end if;
          
          -- This way of mapping Col2 to Col2Out produdes a C compile error
          
          if Col2 in('3','4') then
            set Col2Out = null;
          else
            set Col2Out = Col2;  
          end if;  
          
          -- ***ERROR END***  
    
          pipe(Col1,Col2,Col3,''); -- Write an output row
    
        end for;
    
        return;
      end;​
    If I try and use one of the cursor output columns in the predicate of the IF statement, I get C compile errors:

    Code:
                                                                    |    852​
    
    MSG ID      SEV     TEXT                                                            
                        <SEQNBR>-<FILE NO>:<FILE LINE NO>                                
    CZM0058     30      Label  has already been defined on line  of "".                  
                         ( 573-0:566 )                                                  
    CZM1332     30      A function with return type "" may not return a value of type "".
                         ( 782-0:774 )                                                  
    CZM0006     30      Label  is undefined.                                            
                         ( 850-0:839 )                                                  ​
    Any thoughts?

  • #2
    When you code SQL PL, it generates/compiles C code under the covers. In this case, it seems to be generating bad code -- which is something we can't really help much with. I'd contact IBM Support.

    Comment


    • #3
      Thanks Scott. I just wanted verification I hadn't done something stupid

      If this is an IBM bug, that will be the third one I've found!
      We are still on v7.3 with a v7.4 upgrade imminent, so I'll wait and see if it still happens in v7.4 - or if I find the time I'll try and reproduce on pub400.com. (The last bug I found I am also waiting to test post upgrade, the first one we reported and IBM fixed years ago)

      Comment

      Working...
      X