[MDEV-30334] Optimizer trace produces invalid JSON with WHERE subquery Created: 2023-01-03  Updated: 2023-03-15  Resolved: 2023-03-15

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 11.1.0, 10.11.3, 11.0.2, 10.4.29, 10.5.20, 10.6.13, 10.8.8, 10.9.6, 10.10.4

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Rex Johnston
Resolution: Fixed Votes: 0
Labels: None


 Description   

SET optimizer_trace= 'enabled=on';
 
CREATE TABLE t1 (id INT PRIMARY KEY);
INSERT INTO t1 VALUES (1),(2);
 
CREATE TABLE t2 (a INT);
INSERT INTO t2 VALUES (3),(4);
 
SELECT * FROM t1 WHERE id < ( SELECT SUM(a) FROM t2 );
 
SELECT JSON_VALID(trace) FROM information_schema.optimizer_trace;
 
# Cleanup
DROP TABLE t1, t2;

SELECT * FROM t1 WHERE id < ( SELECT SUM(a) FROM t2 );
id
1
2
SELECT JSON_VALID(trace) FROM information_schema.optimizer_trace;
JSON_VALID(trace)
0

The plan is

{
  "query_optimization": {
    "r_total_time_ms": 0.214919473
  },
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": 0.015704535,
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["id"],
          "r_loops": 1,
          "rows": 2,
          "r_rows": 2,
          "r_table_time_ms": 0.004733423,
          "r_other_time_ms": 0.007802125,
          "filtered": 100,
          "r_filtered": 100,
          "attached_condition": "t1.`id` < (subquery#2)",
          "using_index": true
        }
      }
    ],
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "r_loops": 1,
          "r_total_time_ms": 0.019324802,
          "nested_loop": [
            {
              "table": {
                "table_name": "t2",
                "access_type": "ALL",
                "r_loops": 1,
                "rows": 2,
                "r_rows": 2,
                "r_table_time_ms": 0.00379075,
                "r_other_time_ms": 0.007952552,
                "filtered": 100,
                "r_filtered": 100
              }
            }
          ]
        }
      }
    ]
  }

The problematic part of the optimizer trace is

{
  "substitute_best_equal": {
    "condition": "WHERE",
    "resulting_condition": "t1.`id` < (/* select#2 */ select #^B)"
  }
},
{
  "attaching_conditions_to_tables": {
    "attached_conditions_computation": [],
    "attached_conditions_summary": [
      {
        "table": "t1",
        "attached": "t1.`id` < (/* select#2 */ select #^B)"
      }
    ]
  }
}

where #^B are actually special symbols.



 Comments   
Comment by Rex Johnston [ 2023-02-01 ]

Hi Sergei, simple one here.

Comment by Sergei Petrunia [ 2023-02-13 ]

Hi Rex, review input provided in the PR.

Comment by Rex Johnston [ 2023-02-14 ]

Hi Sergei, comments and test added.

Comment by Sergei Petrunia [ 2023-02-14 ]

Ok to push.

Generated at Thu Feb 08 10:15:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.