Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 11.5.2
    • 10.5, 10.6, 10.11, 11.4
    • JSON, Optimizer
    • None
    • mariadb:11.5.2 docker image

    Description

      CREATE OR REPLACE TABLE test (
      	id INT,
      	name VARCHAR(255),
      	attribute VARCHAR(255)
      );
       
      INSERT INTO test (id, name, attribute) VALUES
      (1, 'John Doe', 'height: 6ft'),
      (1, 'John Doe', 'age: 30'),
      (2, 'Jane Smith', 'height: 5.5ft'),
      (2, 'Jane Smith', 'age: 28'),
      (3, 'Alice Johnson', 'height: 5.7ft'),
      (3, 'Alice Johnson', 'age: 35'),
      (4, 'Bob Brown', 'height: 6.1ft'),
      (4, 'Bob Brown', 'age: 40'),
      (5, 'Charlie Davis', 'height: 5.9ft'),
      (5, 'Charlie Davis', 'age: 32');
       
       
      SELECT id, name, attr FROM (
      	SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY attribute) attr FROM test GROUP BY id, name
      ) t
      WHERE JSON_VALUE(attr, '$[0]') = 'age: 30';
      -- returns empty result
       
      SELECT id, name, attr, JSON_VALUE(attr, '$[0]') = 'age: 30' as v FROM (
      	SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY attribute) attr FROM test GROUP BY id, name
      ) t
      HAVING v = 1;
      -- returns one result
       
      CREATE OR REPLACE TEMPORARY TABLE tmp
      SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY attribute) attr FROM test GROUP BY id, name;
       
      SELECT * FROM tmp WHERE JSON_VALUE(attr, '$[0]') = 'age: 30';
      -- returns one result
      

      I expect the first select statement to return one result

      Attachments

        Issue Links

          Activity

            Johnston Rex Johnston added a comment - - edited

            MariaDB [test]> SELECT id, name, attr FROM (SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id  having JSON_VALUE(attr, '$[0]') = 'age: 30' order by id) t;
            +------+----------+-----------------------------+
            | id   | name     | attr                        |
            +------+----------+-----------------------------+
            |    1 | John Doe | [["age: 30","height: 6ft"]] |
            +------+----------+-----------------------------+
            1 row in set (0.010 sec)
             
            MariaDB [test]> SELECT id, name, attr FROM (SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id  order by id) t where JSON_VALUE(attr, '$[0]') = 'age: 30';
            Empty set (0.010 sec)
            

            These 2 queries should return identical results.
            Internally, when executing

            SELECT id, name, attr FROM (SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id  order by id) t where JSON_VALUE(attr, '$[0]') = 'age: 30'
            

            the derived table is merged into the parent query and the select_lex attached to the executing join looks like this

            select test.`id` AS `id`,test.`name` AS `name`,json_arrayagg(test.attribute order by test.`id` ASC) AS attr from test group by test.`id` having json_value(attr,'$[0]') = 'age: 30' order by test.`id`
            

            Note the difference in the format of results in the column attr below

            MariaDB [test]> select test.`id` AS `id`,test.`name` AS `name`,json_arrayagg(test.attribute order by test.`id` ASC) AS attr from test group by test.`id` having json_value(attr,'$[0]') = 'age: 30' order by test.`id`;
            +------+----------+-----------------------------+
            | id   | name     | attr                        |
            +------+----------+-----------------------------+
            |    1 | John Doe | [["age: 30","height: 6ft"]] |
            +------+----------+-----------------------------+
            1 row in set (0.007 sec)
             
            MariaDB [test]> select test.`id` AS `id`,test.`name` AS `name`,json_arrayagg(test.attribute order by test.`id` ASC) AS attr from test group by test.`id` order by test.`id`;
            +------+---------------+-----------------------------+
            | id   | name          | attr                        |
            +------+---------------+-----------------------------+
            |    1 | John Doe      | ["age: 30","height: 6ft"]   |
            |    2 | Jane Smith    | ["age: 28","height: 5.5ft"] |
            |    3 | Alice Johnson | ["height: 5.7ft","age: 35"] |
            |    4 | Bob Brown     | ["age: 40","height: 6.1ft"] |
            |    5 | Charlie Davis | ["age: 32","height: 5.9ft"] |
            +------+---------------+-----------------------------+
            5 rows in set (0.007 sec)
            

            Johnston Rex Johnston added a comment - - edited MariaDB [test]> SELECT id, name , attr FROM ( SELECT id, name , JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id having JSON_VALUE(attr, '$[0]' ) = 'age: 30' order by id) t; + ------+----------+-----------------------------+ | id | name | attr | + ------+----------+-----------------------------+ | 1 | John Doe | [[ "age: 30" , "height: 6ft" ]] | + ------+----------+-----------------------------+ 1 row in set (0.010 sec)   MariaDB [test]> SELECT id, name , attr FROM ( SELECT id, name , JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id order by id) t where JSON_VALUE(attr, '$[0]' ) = 'age: 30' ; Empty set (0.010 sec) These 2 queries should return identical results. Internally, when executing SELECT id, name , attr FROM ( SELECT id, name , JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id order by id) t where JSON_VALUE(attr, '$[0]' ) = 'age: 30' the derived table is merged into the parent query and the select_lex attached to the executing join looks like this select test.`id` AS `id`,test.` name ` AS ` name `,json_arrayagg(test.attribute order by test.`id` ASC ) AS attr from test group by test.`id` having json_value(attr, '$[0]' ) = 'age: 30' order by test.`id` Note the difference in the format of results in the column attr below MariaDB [test]> select test.`id` AS `id`,test.` name ` AS ` name `,json_arrayagg(test.attribute order by test.`id` ASC ) AS attr from test group by test.`id` having json_value(attr, '$[0]' ) = 'age: 30' order by test.`id`; + ------+----------+-----------------------------+ | id | name | attr | + ------+----------+-----------------------------+ | 1 | John Doe | [[ "age: 30" , "height: 6ft" ]] | + ------+----------+-----------------------------+ 1 row in set (0.007 sec)   MariaDB [test]> select test.`id` AS `id`,test.` name ` AS ` name `,json_arrayagg(test.attribute order by test.`id` ASC ) AS attr from test group by test.`id` order by test.`id`; + ------+---------------+-----------------------------+ | id | name | attr | + ------+---------------+-----------------------------+ | 1 | John Doe | [ "age: 30" , "height: 6ft" ] | | 2 | Jane Smith | [ "age: 28" , "height: 5.5ft" ] | | 3 | Alice Johnson | [ "height: 5.7ft" , "age: 35" ] | | 4 | Bob Brown | [ "age: 40" , "height: 6.1ft" ] | | 5 | Charlie Davis | [ "age: 32" , "height: 5.9ft" ] | + ------+---------------+-----------------------------+ 5 rows in set (0.007 sec)
            Johnston Rex Johnston added a comment -

            further, note that if we disable pushing from where into derived table/having, we have no issue

            MariaDB [test]> set @@optimizer_switch="condition_pushdown_for_derived=OFF";
            Query OK, 0 rows affected (0.002 sec)
            MariaDB [test]> SELECT id, name, attr FROM (SELECT id, name, JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id  order by id) t where JSON_VALUE(attr, '$[0]') = 'age: 30';
            +------+----------+---------------------------+
            | id   | name     | attr                      |
            +------+----------+---------------------------+
            |    1 | John Doe | ["age: 30","height: 6ft"] |
            +------+----------+---------------------------+
            1 row in set (0.020 sec)
            

            Johnston Rex Johnston added a comment - further, note that if we disable pushing from where into derived table/having, we have no issue MariaDB [test]> set @@optimizer_switch= "condition_pushdown_for_derived=OFF" ; Query OK, 0 rows affected (0.002 sec) MariaDB [test]> SELECT id, name , attr FROM ( SELECT id, name , JSON_ARRAYAGG(attribute ORDER BY id) attr FROM test GROUP BY id order by id) t where JSON_VALUE(attr, '$[0]' ) = 'age: 30' ; + ------+----------+---------------------------+ | id | name | attr | + ------+----------+---------------------------+ | 1 | John Doe | [ "age: 30" , "height: 6ft" ] | + ------+----------+---------------------------+ 1 row in set (0.020 sec)

            People

              Johnston Rex Johnston
              Micke Mikael Nilsson
              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.