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

Wrong result (NULL returned as 0/empty) with server-side cursor and FederatedX select handler over LEFT JOIN with merged derived table

    XMLWordPrintable

Details

    Description

      A query with a LEFT JOIN whose inner side contains a merged derived table returns wrong results through a server-side cursor when the whole query is pushed to FederatedX by the select handler (federated_pushdown=1). The derived table's columns, which should be NULL-complemented, come back as 0 and '' instead of NULL.

      It reproduces with mtr --cursor-protocol, and the same applies to any read-only server-side cursor (COM_STMT_EXECUTE with CURSOR_TYPE_READ_ONLY, stored routine cursors).

      Found by federated.federatedx_create_handlers, which fails on every 11.4+ build in the "cursor test" step of amd64-ubuntu-2204-fulltest, e.g. https://buildbot.mariadb.org/#/builders/913/builds/23017/steps/5/logs/stdio

      @@ -574,8 +574,8 @@
       WHERE id=2) dt2) dt
       ) ON t10.a=t11.a;
       a	b	a	b	id	name
      -1	1	NULL	NULL	NULL	NULL
      -2	2	NULL	NULL	NULL	NULL
      +1	1	NULL	NULL	0
      +2	2	NULL	NULL	0

      Test case (run with --cursor-protocol, put in suite/federated):

      --source have_federatedx.inc
      --source include/federated.inc
      connection slave;
      CREATE TABLE federated.t1 (id int NOT NULL, name varchar(16) NOT NULL default '');
      INSERT INTO federated.t1 VALUES (3,'xxx'),(7,'yyy');
      CREATE TABLE federated.t10 (a INT,b INT);
      CREATE TABLE federated.t11 (a INT, b INT);
      INSERT INTO federated.t10 VALUES (1,1),(2,2);
      INSERT INTO federated.t11 VALUES (1,1),(2,2);
      connection master;
      --replace_result $SLAVE_MYPORT SLAVE_PORT
      eval CREATE TABLE federated.t1 ENGINE=FEDERATED CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/federated/t1';
      --replace_result $SLAVE_MYPORT SLAVE_PORT
      eval CREATE TABLE federated.t10 ENGINE=FEDERATED CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/federated/t10';
      --replace_result $SLAVE_MYPORT SLAVE_PORT
      eval CREATE TABLE federated.t11 ENGINE=FEDERATED CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/federated/t11';
      use federated;
      set global federated_pushdown=1;
      --enable_metadata
      SELECT * FROM t10 LEFT JOIN (t11, (SELECT * FROM (SELECT * FROM t1 where id=3) dt2 WHERE id=2) dt) ON t10.a=t11.a;
      --disable_metadata
      EXPLAIN SELECT * FROM t10 LEFT JOIN (t11, (SELECT * FROM (SELECT * FROM t1 where id=3) dt2 WHERE id=2) dt) ON t10.a=t11.a;
      set global federated_pushdown=0;
      DROP TABLE t1,t10,t11;
      connection slave;
      DROP TABLE federated.t1,federated.t10,federated.t11;
      connection default;
      source include/federated_cleanup.inc;

      Actual result (13.1, --cursor-protocol). The metadata shows dt.id and dt.name as NOT NULL (Is_null N, flags 36865):

      Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
      def	federated	t10	t10	a	a	3	11	11	Y	32768	0	63
      def	federated	t10	t10	b	b	3	11	11	Y	32768	0	63
      def	federated	t11	t11	a	a	3	11	0	Y	32768	0	63
      def	federated	t11	t11	b	b	3	11	0	Y	32768	0	63
      def	federated	dt	dt	id	id	3	11	11	N	36865	0	63
      def	federated	dt	dt	name	name	253	16	0	N	1	0	8
      a	b	a	b	id	name
      1	1	NULL	NULL	0
      2	2	NULL	NULL	0
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	PUSHED SELECT	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL

      Expected: 1 1 NULL NULL NULL NULL and 2 2 NULL NULL NULL NULL. That is what the server returns without the cursor protocol, with federated_pushdown=0, and on 10.11 in either case.

      Versions: reproduced on 11.4, 12.3 and 13.1 (debug and RelWithDebInfo). 10.11 is not affected.

      Analysis:

      • Since MDEV-25080 (11.1.1), the FederatedX select handler checks the leaf tables in sel_lex->join->tables_list. Before, it checked thd->lex->query_tables, where a merged derived table has table == NULL and caused a refusal. So 11.1+ pushes the whole query (PUSHED SELECT), while 10.11 pushed only the derived table (PUSHED DERIVED) and ran the outer join in the server.
      • When a select handler is used, JOIN::optimize() skips optimize_inner(), so select_lex->update_used_tables() is never called. It is that call that runs Item_direct_view_ref::update_used_tables() -> set_null_ref_table() -> set_maybe_null() for the merged derived table's columns once join tabs exist (TABLE_LIST::get_real_join_table() needs reginfo.join_tab). The select-list items for dt.id and dt.name therefore stay NOT NULL even though dt is inside an outer-join nest.
      • Without a cursor this goes unnoticed, because metadata and rows come from the select handler's own tmp table, where every column is nullable.
      • With a cursor, Select_materialize::send_result_set_metadata() builds the materialization tmp table from unit->get_column_types(true), i.e. from the original select-list items. The dt columns become NOT NULL, and NULLs from the pushed result are stored as 0 and ''.

      The test was hidden under --disable_cursor_protocol until MDEV-31846 (13dd0737422, April 2025) re-enabled the cursor protocol for federatedx_create_handlers. That was correct for 10.6/10.11 but exposed this bug on 11.4+.

      Attachments

        Issue Links

          Activity

            People

              oleg.smirnov Oleg Smirnov
              sanja Oleksandr Byelkin
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Remaining Estimate - 3d
                  3d
                  Logged:
                  Time Spent - Not Specified
                  Not Specified

                  Git Integration

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