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

Trigger doesn't work correctly with bulk insert

Details

    Description

      CREATE TABLE `table1` (
       `id` int(11) DEFAULT NULL,
       `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
      ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1;
       
      CREATE TABLE `table2` (
       `id` int(11) DEFAULT NULL,
       `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
      ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1;
       
      CREATE TRIGGER `table1_before_insert` BEFORE INSERT ON `table1`
       FOR EACH ROW BEGIN
      	insert into table2(`id`, `name`) values (NEW.`id`, NEW.`name`);
      END
      

      import mariadb
      conn=mariadb.connect(user="test", database="test")
      cursor=conn.cursor()
      vals = [(1,'John'), (2,'Smith'), (3,'Carol')]
      cursor.executemany("INSERT INTO table1 VALUES (?,?)", vals)
      cursor.execute("SELECT * FROM table1")
      rows= cursor.fetchall()
      print(rows)
      

      Expected output:
      [(1, 'John', 1, 'John'), (2, 'Smith', 1, 'John'), (3, 'Carol', 1, 'John'), (1, 'John', 2, 'Smith'), (2, 'Smith', 2, 'Smith'), (3, 'Carol', 2, 'Smith'), (1, 'John', 3, 'Carol'), (2, 'Smith', 3, 'Carol'), (3, 'Carol', 3, 'Carol')]

      Current output:
      [(1, 'John', 1, 'John'), (1, 'John', 1, 'John')]

      Expected rows on tables:
      Table1:
      "1" "John"
      "2" "Smith"
      "3" "Carol"

      Table2:
      "1" "John"
      "2" "Smith"
      "3" "Carol"

      Current rows on tables:
      Table1:
      "1" "John"

      Table2:
      "1" "John"
      "1" "John"

      Running insert on mariadb console, works correctly:
      insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol');

      Attachments

        Issue Links

          Activity

            MarcT Marcelo Titonelli Pio da Cruz created issue -
            MarcT Marcelo Titonelli Pio da Cruz made changes -
            Field Original Value New Value
            Description
            {code:java}
            CREATE TABLE `table1` (
             `id` int(11) DEFAULT NULL,
             `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
            ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1

            CREATE TABLE `table2` (
             `id` int(11) DEFAULT NULL,
             `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
            ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1

            CREATE TRIGGER `table1_before_insert` BEFORE INSERT ON `table1`
             FOR EACH ROW BEGIN
            insert into table2(`id`, `name`) values (NEW.`id`, NEW.`name`);
            END
            {code}


            {code:java}
            import mariadb
            conn=mariadb.connect(user="test", database="test")
            cursor=conn.cursor()
            vals = [(1,'John'), (2,'Smith'), (3,'Carol')]
            cursor.executemany("INSERT INTO table1 VALUES (?,?)", vals)
            cursor.execute("SELECT * FROM table1")
            rows= cursor.fetchall()
            print(rows)
            {code}


            *Expected output:*
            [(1, 'John', 1, 'John'), (2, 'Smith', 1, 'John'), (3, 'Carol', 1, 'John'), (1, 'John', 2, 'Smith'), (2, 'Smith', 2, 'Smith'), (3, 'Carol', 2, 'Smith'), (1, 'John', 3, 'Carol'), (2, 'Smith', 3, 'Carol'), (3, 'Carol', 3, 'Carol')]

            *Current output:*
            [(1, 'John', 1, 'John'), (1, 'John', 1, 'John')]


            *Expected rows on tables:*
            Table1:
            "1" "John"
            "2" "Smith"
            "3" "Carol"

            Table2:
            "1" "John"
            "2" "Smith"
            "3" "Carol"


            *Current rows on tables:*
            Table1:
            "1" "John"

            Table2:
            "1" "John"
            "1" "John"


            +Running insert on mariadb console, works correctly:
            insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol');
            {code:java}
            CREATE TABLE `table1` (
             `id` int(11) DEFAULT NULL,
             `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
            ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1;

            CREATE TABLE `table2` (
             `id` int(11) DEFAULT NULL,
             `name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
            ) ENGINE=Aria DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PAGE_CHECKSUM=1;

            CREATE TRIGGER `table1_before_insert` BEFORE INSERT ON `table1`
             FOR EACH ROW BEGIN
            insert into table2(`id`, `name`) values (NEW.`id`, NEW.`name`);
            END
            {code}


            {code:java}
            import mariadb
            conn=mariadb.connect(user="test", database="test")
            cursor=conn.cursor()
            vals = [(1,'John'), (2,'Smith'), (3,'Carol')]
            cursor.executemany("INSERT INTO table1 VALUES (?,?)", vals)
            cursor.execute("SELECT * FROM table1")
            rows= cursor.fetchall()
            print(rows)
            {code}


            *Expected output:*
            [(1, 'John', 1, 'John'), (2, 'Smith', 1, 'John'), (3, 'Carol', 1, 'John'), (1, 'John', 2, 'Smith'), (2, 'Smith', 2, 'Smith'), (3, 'Carol', 2, 'Smith'), (1, 'John', 3, 'Carol'), (2, 'Smith', 3, 'Carol'), (3, 'Carol', 3, 'Carol')]

            *Current output:*
            [(1, 'John', 1, 'John'), (1, 'John', 1, 'John')]


            *Expected rows on tables:*
            Table1:
            "1" "John"
            "2" "Smith"
            "3" "Carol"

            Table2:
            "1" "John"
            "2" "Smith"
            "3" "Carol"


            *Current rows on tables:*
            Table1:
            "1" "John"

            Table2:
            "1" "John"
            "1" "John"


            +Running insert on mariadb console, works correctly:+
            insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol');
            georg Georg Richter added a comment -

            Thank you for your bug report, this is a server bug, so I will reclassify and reassign this ticket.

            static int test_conpy135(MYSQL *mysql)
            {
              int rc;
              MYSQL_STMT *stmt;
              MYSQL_BIND bind;
              unsigned int vals[]= {1,2,3};
              unsigned int array_size= 3;
              MYSQL_ROW row;
              MYSQL_RES *result;
             
              rc= mysql_query(mysql, "CREATE OR REPLACE TABLE t1 (a int)");
              check_mysql_rc(rc, mysql);
             
              rc= mysql_query(mysql, "CREATE OR REPLACE TABLE t2 (a int)");
              check_mysql_rc(rc, mysql);
             
              rc= mysql_query(mysql, "CREATE TRIGGER t1_before_insert BEFORE INSERT ON t1\n"
                                     "FOR EACH ROW BEGIN\n"
                                     "   INSERT INTO t2 (a) VALUES (NEW.a);\n"
                                     "END");
              check_mysql_rc(rc, mysql);
             
              stmt= mysql_stmt_init(mysql);
             
              rc= mysql_stmt_prepare(stmt, SL("INSERT INTO t1 VALUES (?)"));
              check_stmt_rc(rc, stmt);
             
              memset(&bind, 0, sizeof(MYSQL_BIND));
              bind.buffer_type= MYSQL_TYPE_LONG;
              bind.buffer= vals;
             
              rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
              check_stmt_rc(rc, stmt);
             
              rc= mysql_stmt_bind_param(stmt, &bind);
              check_stmt_rc(rc, stmt);
             
              rc= mysql_stmt_execute(stmt);
              check_stmt_rc(rc, stmt);
             
              diag("affected rows: %lld", mysql_stmt_affected_rows(stmt));
              rc= mysql_query(mysql, "SELECT 't1', a FROM t1 UNION SELECT 't2', a FROM t2");
              check_mysql_rc(rc, mysql);
             
              result= mysql_store_result(mysql);
              while ((row = mysql_fetch_row(result)))
                diag("%s | %s ", row[0], row[1]);
              mysql_free_result(result);
             
              return OK;
            }
            

            Output without trigger:

            # affected rows: 3
            # t1 | 1 
            # t1 | 2 
            # t1 | 3 
            # t2 | 1 
            # t2 | 2 
            # t2 | 3
            

            Output with trigger:

            # affected rows: 1
            # t1 | 1 
            # t2 | 1
            

            georg Georg Richter added a comment - Thank you for your bug report, this is a server bug, so I will reclassify and reassign this ticket. static int test_conpy135(MYSQL *mysql) { int rc; MYSQL_STMT *stmt; MYSQL_BIND bind; unsigned int vals[]= {1,2,3}; unsigned int array_size= 3; MYSQL_ROW row; MYSQL_RES *result;   rc= mysql_query(mysql, "CREATE OR REPLACE TABLE t1 (a int)"); check_mysql_rc(rc, mysql);   rc= mysql_query(mysql, "CREATE OR REPLACE TABLE t2 (a int)"); check_mysql_rc(rc, mysql);   rc= mysql_query(mysql, "CREATE TRIGGER t1_before_insert BEFORE INSERT ON t1\n" "FOR EACH ROW BEGIN\n" " INSERT INTO t2 (a) VALUES (NEW.a);\n" "END"); check_mysql_rc(rc, mysql);   stmt= mysql_stmt_init(mysql);   rc= mysql_stmt_prepare(stmt, SL("INSERT INTO t1 VALUES (?)")); check_stmt_rc(rc, stmt);   memset(&bind, 0, sizeof(MYSQL_BIND)); bind.buffer_type= MYSQL_TYPE_LONG; bind.buffer= vals;   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size); check_stmt_rc(rc, stmt);   rc= mysql_stmt_bind_param(stmt, &bind); check_stmt_rc(rc, stmt);   rc= mysql_stmt_execute(stmt); check_stmt_rc(rc, stmt);   diag("affected rows: %lld", mysql_stmt_affected_rows(stmt)); rc= mysql_query(mysql, "SELECT 't1', a FROM t1 UNION SELECT 't2', a FROM t2"); check_mysql_rc(rc, mysql);   result= mysql_store_result(mysql); while ((row = mysql_fetch_row(result))) diag("%s | %s ", row[0], row[1]); mysql_free_result(result);   return OK; } Output without trigger: # affected rows: 3 # t1 | 1 # t1 | 2 # t1 | 3 # t2 | 1 # t2 | 2 # t2 | 3 Output with trigger: # affected rows: 1 # t1 | 1 # t2 | 1
            georg Georg Richter made changes -
            Component/s Binary Protocol [ 16120 ]
            Component/s Other [ 16504 ]
            Key CONPY-135 MDEV-24411
            Affects Version/s 10.5.8 [ 25023 ]
            Affects Version/s 1.0.5 [ 25126 ]
            Project MariaDB Connector/Python [ 11400 ] MariaDB Server [ 10000 ]
            georg Georg Richter made changes -
            Assignee Georg Richter [ georg ] Oleksandr Byelkin [ sanja ]
            georg Georg Richter made changes -
            Priority Critical [ 2 ] Blocker [ 1 ]
            georg Georg Richter made changes -
            Priority Blocker [ 1 ] Critical [ 2 ]
            georg Georg Richter made changes -
            Summary Trigger doesn't work correctly with executemany Trigger doesn't work correctly with bulk insert
            elenst Elena Stepanova made changes -
            Fix Version/s 10.5 [ 23123 ]
            diego dupin Diego Dupin made changes -
            Labels CONNECTOR_RELATED
            diego dupin Diego Dupin made changes -
            Labels CONNECTOR_RELATED
            sanja Oleksandr Byelkin made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 116872 ] MariaDB v4 [ 144580 ]
            diego dupin Diego Dupin made changes -
            Labels CONNECTOR_RELATED
            diego dupin Diego Dupin made changes -
            Fix Version/s 10.2 [ 14601 ]
            Fix Version/s 10.3 [ 22126 ]
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.6 [ 24028 ]
            Fix Version/s 10.7 [ 24805 ]
            diego dupin Diego Dupin made changes -
            sanja Oleksandr Byelkin made changes -
            Assignee Oleksandr Byelkin [ sanja ] Dmitry Shulga [ JIRAUSER47315 ]
            ralf.gebhardt Ralf Gebhardt made changes -
            Fix Version/s 10.2 [ 14601 ]
            serg Sergei Golubchik made changes -
            Priority Critical [ 2 ] Major [ 3 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.7 [ 24805 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.3 [ 22126 ]
            diego dupin Diego Dupin made changes -
            diego dupin Diego Dupin made changes -
            diego dupin Diego Dupin made changes -
            diego dupin Diego Dupin made changes -
            diego dupin Diego Dupin made changes -

            Automated message:
            ----------------------------
            Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.

            julien.fritsch Julien Fritsch added a comment - Automated message: ---------------------------- Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
            JIraAutomate JiraAutomate made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            JIraAutomate JiraAutomate added a comment -

            Automated message:
            ----------------------------
            Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.

            JIraAutomate JiraAutomate added a comment - Automated message: ---------------------------- Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
            ralf.gebhardt Ralf Gebhardt made changes -
            Priority Major [ 3 ] Critical [ 2 ]
            Rudygunawan Rudy Gunawan added a comment - the bug also affects bulk update , as shown here : https://stackoverflow.com/questions/78204056/hibernate-sqlintegrityconstraintviolation-duplicate-data-entry-issue-after-upg https://dba.stackexchange.com/questions/339389/weird-behaviour-with-batch-and-triggermariadb?noredirect=1#comment660144_339389 let me know if it is better to create separate issue for bulk update.
            pandi.gurusamy Pandikrishnan Gurusamy made changes -
            Assignee Dmitry Shulga [ JIRAUSER47315 ] Pandikrishnan Gurusamy [ JIRAUSER52131 ]
            pandi.gurusamy Pandikrishnan Gurusamy made changes -
            Assignee Pandikrishnan Gurusamy [ JIRAUSER52131 ] Oleksandr Byelkin [ sanja ]
            pandi.gurusamy Pandikrishnan Gurusamy made changes -
            Priority Critical [ 2 ] Blocker [ 1 ]
            serg Sergei Golubchik made changes -
            Priority Blocker [ 1 ] Critical [ 2 ]
            serg Sergei Golubchik made changes -
            Assignee Oleksandr Byelkin [ sanja ] Dmitry Shulga [ JIRAUSER47315 ]
            shulga Dmitry Shulga made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            shulga Dmitry Shulga added a comment -

            The branch for review is bb-10.5-MDEV-24411

            shulga Dmitry Shulga added a comment - The branch for review is bb-10.5- MDEV-24411
            shulga Dmitry Shulga made changes -
            Assignee Dmitry Shulga [ JIRAUSER47315 ] Oleksandr Byelkin [ sanja ]
            Status In Progress [ 3 ] In Review [ 10002 ]

            Remove please adding of two emply lines and it is OK to push

            sanja Oleksandr Byelkin added a comment - Remove please adding of two emply lines and it is OK to push
            sanja Oleksandr Byelkin made changes -
            Assignee Oleksandr Byelkin [ sanja ] Dmitry Shulga [ JIRAUSER47315 ]
            Status In Review [ 10002 ] Stalled [ 10000 ]
            shulga Dmitry Shulga made changes -
            Fix Version/s 10.5.26 [ 29832 ]
            Fix Version/s 10.6.19 [ 29833 ]
            Fix Version/s 10.11.9 [ 29834 ]
            Fix Version/s 11.1.6 [ 29835 ]
            Fix Version/s 11.2.5 [ 29836 ]
            Fix Version/s 11.4.3 [ 29837 ]
            Fix Version/s 11.5.2 [ 29838 ]
            Fix Version/s 11.6.0 [ 29839 ]
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.5 [ 23123 ]
            Fix Version/s 10.6 [ 24028 ]
            shulga Dmitry Shulga made changes -
            Resolution Fixed [ 1 ]
            Status Stalled [ 10000 ] Closed [ 6 ]
            shulga Dmitry Shulga added a comment -

            Please check it and file the new bug report in case the update scenario is still reproducible.

            shulga Dmitry Shulga added a comment - Please check it and file the new bug report in case the update scenario is still reproducible.
            mariadb-jira-automation Jira Automation (IT) made changes -
            Zendesk Related Tickets 201572
            Zendesk active tickets 201572
            shulga Dmitry Shulga added a comment -

            I will check the use case for UPDATE statement and add results in comments.

            shulga Dmitry Shulga added a comment - I will check the use case for UPDATE statement and add results in comments.
            shulga Dmitry Shulga made changes -
            Resolution Fixed [ 1 ]
            Status Closed [ 6 ] Stalled [ 10000 ]
            shulga Dmitry Shulga made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            shulga Dmitry Shulga made changes -
            shulga Dmitry Shulga made changes -
            Resolution Fixed [ 1 ]
            Status In Progress [ 3 ] Closed [ 6 ]
            diego dupin Diego Dupin made changes -

            This bug is somehow related to MDEV-29218 ?

            lucianobarcaro Luciano Barcaro added a comment - This bug is somehow related to MDEV-29218 ?
            shulga Dmitry Shulga added a comment -

            MDEV-29218 should be fixed by a patch for the task MDEV-34958

            shulga Dmitry Shulga added a comment - MDEV-29218 should be fixed by a patch for the task MDEV-34958

            People

              shulga Dmitry Shulga
              MarcT Marcelo Titonelli Pio da Cruz
              Votes:
              9 Vote for this issue
              Watchers:
              16 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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