[MDEV-15314] Incorrect result when subquery has SUM(outer_select_field) Created: 2018-02-14  Updated: 2023-04-27

Status: Stalled
Project: MariaDB Server
Component/s: None
Affects Version/s: 5.5, 10.0, 10.1, 10.2, 10.3
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Varun Gupta (Inactive) Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: upstream

Sprint: 10.3.6-1

 Description   

MTR test

CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
INSERT INTO t1 VALUES (1,1), (2,2);
 
CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
INSERT INTO t2 VALUES (1,1), (3,3);
 
set join_cache_level=4;
SELECT SQL_NO_CACHE 
  (SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
   WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;
 minid
 NULL
 
set join_cache_level= 2;
SELECT SQL_NO_CACHE 
  (SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
   WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;
minid
1
DROP TABLE t1,t2;



 Comments   
Comment by Varun Gupta (Inactive) [ 2018-02-14 ]

Explain for the query with join_cache_level=4

explain
SELECT SQL_NO_CACHE 
(SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	c	ALL	NULL	NULL	NULL	NULL	2	Using where
1	PRIMARY	t	hash_index	PRIMARY	#hash#PRIMARY:PRIMARY	4:4	test.c.b	2	Using index; Using join buffer (flat, BNLH join)
2	DEPENDENT SUBQUERY	ttt	eq_ref	PRIMARY	PRIMARY	4	test.t.a	1	Using index
2	DEPENDENT SUBQUERY	ccc	hash_ALL	NULL	#hash#$hj	5	test.t.a	2	Using where; Using join buffer (flat, BNLH join)

Comment by Varun Gupta (Inactive) [ 2018-02-15 ]

Out of explain format=json

explain format=json
SELECT SQL_NO_CACHE 
(SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "table": {
      "table_name": "c",
      "access_type": "ALL",
      "rows": 2,
      "filtered": 100,
      "attached_condition": "c.b is not null"
    },
    "block-nl-join": {
      "table": {
        "table_name": "t",
        "access_type": "hash_index",
        "possible_keys": ["PRIMARY"],
        "key": "#hash#PRIMARY:PRIMARY",
        "key_length": "4:4",
        "used_key_parts": ["a"],
        "ref": ["test.c.b"],
        "rows": 2,
        "filtered": 50,
        "using_index": true
      },
      "buffer_type": "flat",
      "buffer_size": "256Kb",
      "join_type": "BNLH"
    },
    "subqueries": [
      {
        "query_block": {
          "select_id": 2,
          "table": {
            "table_name": "ttt",
            "access_type": "eq_ref",
            "possible_keys": ["PRIMARY"],
            "key": "PRIMARY",
            "key_length": "4",
            "used_key_parts": ["a"],
            "ref": ["test.t.a"],
            "rows": 1,
            "filtered": 100,
            "using_index": true
          },
          "block-nl-join": {
            "table": {
              "table_name": "ccc",
              "access_type": "hash_ALL",
              "key": "#hash#$hj",
              "key_length": "5",
              "used_key_parts": ["b"],
              "ref": ["test.t.a"],
              "rows": 2,
              "filtered": 100
            },
            "buffer_type": "flat",
            "buffer_size": "256Kb",
            "join_type": "BNLH",
            "attached_condition": "ccc.b = ttt.a"
          }
        }
      }
    ]
  }
}

Comment by Varun Gupta (Inactive) [ 2018-02-16 ]

Simplified test-case:

SET optimizer_switch='subquery_cache=off';
CREATE TABLE t1 (a INT , b INT);
INSERT INTO t1 VALUES (1,1) ,(2,2);
CREATE TABLE t2 (a INT , b INT);
INSERT INTO t2 VALUES (1,1), (3,3);
set join_cache_level=2;
SELECT 
(SELECT sum(c.a) FROM t1 ttt, t2 ccc 
WHERE ttt.a = ccc.b AND ttt.a = t.a ) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;

Comment by Sergei Petrunia [ 2018-02-21 ]

notes from the phone call:

  • this is not a stopper for changing the server defaults
  • need to figure out what the semantics of the query should be (check other databases)
  • * should aggregation be done in the upper query or in the subquery
  • is it fixed in MySQL?
Comment by Sergei Golubchik [ 2018-03-08 ]

what versions are affected?

Comment by Varun Gupta (Inactive) [ 2018-03-28 ]

Running query on different databases

1) SQL Server

Error(s), warning(s):
Column 't1.a' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

2) Oracle

SELECT 
(SELECT sum(c.a) FROM t1 ttt, t2 ccc 
WHERE ttt.a = ccc.b AND ttt.a = t.a ) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;

Output:
MINID
1

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