Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-24837

HAVING clause yields wrong results in 10.2 and 10.3, fine in 10.1

Details

    • Bug
    • Status: In Review (View Workflow)
    • Critical
    • Resolution: Unresolved
    • 10.2.26, 10.3.12, 10.2(EOL), 10.3(EOL), 10.4(EOL), 10.5, 10.6, 10.7(EOL), 10.8(EOL), 10.9(EOL), 10.10(EOL)
    • 10.5, 10.6
    • Optimizer

    Description

      As you can see in the attached test case a query behaves differently in 10.1 vs 10.2 and 10.3

      10.1

      select distinct concat(table1.T1_VAL,table1.T1_VAR) as CC, concat(table1.T1_VAL,table1.T1_VAR) = 'VALB' as chk, group_concat(table2.T3_ID) as id from test.table1 LEFT JOIN (test.table2,test.table3) on concat(T1_VAL,T1_VAR) = table2.T2_VALVAR and table2.T3_ID = table3.T3_ID group by T1_ID having CC = 'VALB';
      +------+-----+------+
      | CC   | chk | id   |
      +------+-----+------+
      | VALB |   1 | 1    |
      +------+-----+------+
      1 row in set (0.00 sec)
      

      10.2 & 10.3

      select distinct concat(table1.T1_VAL,table1.T1_VAR) as CC, concat(table1.T1_VAL,table1.T1_VAR) = 'VALB' as chk, group_concat(table2.T3_ID) as id from test.table1 LEFT JOIN (test.table2,test.table3) on concat(T1_VAL,T1_VAR) = table2.T2_VALVAR and table2.T3_ID = table3.T3_ID group by T1_ID having CC = 'VALB';
      Empty set (0.01 sec)
      
      

      – No Results –

      Attachments

        Activity

          oleg.smirnov Oleg Smirnov added a comment -

          Another observation: the following variation of SQL causes server crash:

          select concat(t1.T1_VAL,t1.T1_VAR) as CC
          from t1 LEFT JOIN (t2, t3) on
              concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID;
          

          oleg.smirnov Oleg Smirnov added a comment - Another observation: the following variation of SQL causes server crash: select concat(t1.T1_VAL,t1.T1_VAR) as CC from t1 LEFT JOIN (t2, t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID;

          oleg.smirnov, following the discussion on the optimizer call: as the crash is a separate issue, please file a separate self-contained MDEV for it, mention its number here, assign it to yourself.

          psergei Sergei Petrunia added a comment - oleg.smirnov , following the discussion on the optimizer call: as the crash is a separate issue, please file a separate self-contained MDEV for it, mention its number here, assign it to yourself.
          oleg.smirnov Oleg Smirnov added a comment - - edited

          Some other observations so far.
          The bug reveals itself only when both conditions are satisfied:

          • group_concat(t2.T3_ID) is present in the field list
          • t2.T3_ID = t3.T3_ID equality is present

          select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id 
            from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID  
            group by T1_ID having CC='valb';
          

          After removing any of them:

          select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC 
            from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID  
            group by T1_ID having CC='valb';
          

          select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id 
            from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR   
            group by T1_ID having CC='valb';
          

          the result of SQL is correct. Removing t2.T3_ID = t3.T3_ID equality leads to another execution plan (t3 accessed with "scan" against "eq_ref").

          Also the result is correct after modifying the HAVING clause to "HAVING CC='valc'":

          select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id 
            from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID  
            group by T1_ID having CC='valc';
          

          oleg.smirnov Oleg Smirnov added a comment - - edited Some other observations so far. The bug reveals itself only when both conditions are satisfied: group_concat(t2.T3_ID) is present in the field list t2.T3_ID = t3.T3_ID equality is present select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID group by T1_ID having CC= 'valb' ; After removing any of them: select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID group by T1_ID having CC= 'valb' ; select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR group by T1_ID having CC= 'valb' ; the result of SQL is correct. Removing t2.T3_ID = t3.T3_ID equality leads to another execution plan (t3 accessed with "scan" against "eq_ref"). Also the result is correct after modifying the HAVING clause to "HAVING CC='valc'": select distinct concat(t1.T1_VAL,t1.T1_VAR) as CC, group_concat(t2.T3_ID) as id from t1 LEFT JOIN (t2,t3) on concat(T1_VAL,T1_VAR) = t2.T2_VALVAR and t2.T3_ID = t3.T3_ID group by T1_ID having CC= 'valc' ;
          oleg.smirnov Oleg Smirnov added a comment -

          Applied the Monty's suggested patch and added a test case.

          oleg.smirnov Oleg Smirnov added a comment - Applied the Monty's suggested patch and added a test case.
          alice Alice Sherepa added a comment -

          I checked the test case on current 10.4-11.4 and it returned the correct result

          alice Alice Sherepa added a comment - I checked the test case on current 10.4-11.4 and it returned the correct result

          People

            psergei Sergei Petrunia
            claudio.nanni Claudio Nanni
            Votes:
            2 Vote for this issue
            Watchers:
            12 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.