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

UPDATE/DELETE WHERE with Window Function Selects Different Rows Compared to SELECT WHERE

    XMLWordPrintable

Details

    Description

      Description:
      When executing an UPDATE or DELETE statement using a WHERE condition that includes a subquery with a window function, the number of affected rows is inconsistent with the number of rows returned by SELECT WHERE. Specifically:
      SELECT WHERE returns two rows.
      UPDATE WHERE modifies only one row.
      DELETE WHERE removes only one row.

      Steps to Reproduce:
      Step 1: Create Tables and Insert Data

      CREATE TABLE `t1` (
        `c1` int
      );
       
      INSERT INTO `t1` VALUES (1),(2);
       
      CREATE TABLE `t2` (
        `c2` int
      );
       
      INSERT INTO `t2` VALUES (1),(2);
      

      Step 2: Execute SELECT WHERE Query

      select 
          *
      from t1
      where (((((-45) in (select  
                      DENSE_RANK() over w0 as c_0
                    from 
                      t2 as ref_1
                    window w0 as (partition by false order by t1.c1 asc, ref_1.c2 asc)
                  ))) is null))
      

      Output:

      +------+
      | c1   |
      +------+
      |    1 |
      |    2 |
      +------+
      2 rows in set (0.00 sec)
      

      Step 3: Execute UPDATE WHERE Query

      update t1 set 
        c1 = 3
      where (((((-45) in (select  
                      DENSE_RANK() over w0 as c_0
                    from 
                      t2 as ref_1
                    window w0 as (partition by false order by t1.c1 asc, ref_1.c2 asc)
                  ))) is null))
      

      Output:

      Query OK, 1 row affected (0.01 sec)
      Rows matched: 1  Changed: 1  Warnings: 0
      

      Check t1 after update:

      select * from t1;
      

      Output:

      +------+
      | c1   |
      +------+
      |    3 |
      |    2 |
      +------+
      2 rows in set (0.00 sec)
      

      Step 4: Rollback update operation and Execute DELETE WHERE Query

      delete from t1
      where (((((-45) in (select  
                      DENSE_RANK() over w0 as c_0
                    from 
                      t2 as ref_1
                    window w0 as (partition by false order by t1.c1 asc, ref_1.c2 asc)
                  ))) is null))
      Query OK, 1 row affected (0.01 sec)
      

      Output:

      Query OK, 1 row affected (0.01 sec)
      

      Check t1 after delete:

      select * from t1;
      

      Output:

      +------+
      | c1   |
      +------+
      |    2 |
      +------+
      1 row in set (0.00 sec)
      

      Expected Behavior:
      SELECT WHERE, UPDATE WHERE, and DELETE WHERE should operate on the same set of rows.
      If SELECT WHERE returns two rows, then UPDATE WHERE should modify both, and DELETE WHERE should remove both.

      Actual Behavior:
      SELECT WHERE correctly returns two rows.
      UPDATE WHERE modifies only one row.
      DELETE WHERE removes only one row.
      The number of affected rows is inconsistent across different SQL operations.

      Attachments

        Activity

          People

            psergei Sergei Petrunia
            orange chengzhiqiang
            Votes:
            0 Vote for this issue
            Watchers:
            3 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.