Skip to content

Commit

Permalink
fix(tianmu): fix mysqld crash when assigning return values using bot…
Browse files Browse the repository at this point in the history
…h custom variables and derived tables (#1707)
  • Loading branch information
adofsauron authored and mergify[bot] committed May 8, 2023
1 parent c95d263 commit 2daac7c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
34 changes: 34 additions & 0 deletions mysql-test/suite/tianmu/r/issue1707.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CREATE DATABASE IF NOT EXISTS test_db_1707;
USE test_db_1707;
CREATE TABLE tianmu_table (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
salary INT NOT NULL,
address VARCHAR(100) NOT NULL
) ENGINE=tianmu;
INSERT INTO tianmu_table
(id, name, age, gender, salary, address)
VALUES
(1, 'John', 25, 'Male', 50000, '123 Main St'),
(2, 'Jane', 30, 'Female', 60000, '456 Elm St'),
(3, 'Bob', 35, 'Male', 70000, '789 Maple St');
select @age_cutoff := age
from (
SELECT name, age, gender, salary
FROM (
SELECT name, age, gender, salary
FROM tianmu_table
WHERE age < 30
UNION ALL
SELECT name, age, gender, salary
FROM tianmu_table
WHERE age >= 30
) AS derived_table
WHERE salary > 30 ) H;
@age_cutoff := age
25
30
35
drop database test_db_1707;
41 changes: 41 additions & 0 deletions mysql-test/suite/tianmu/t/issue1707.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--source include/have_tianmu.inc

--disable_warnings

CREATE DATABASE IF NOT EXISTS test_db_1707;

USE test_db_1707;

CREATE TABLE tianmu_table (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
salary INT NOT NULL,
address VARCHAR(100) NOT NULL
) ENGINE=tianmu;

INSERT INTO tianmu_table
(id, name, age, gender, salary, address)
VALUES
(1, 'John', 25, 'Male', 50000, '123 Main St'),
(2, 'Jane', 30, 'Female', 60000, '456 Elm St'),
(3, 'Bob', 35, 'Male', 70000, '789 Maple St');


select @age_cutoff := age
from (
SELECT name, age, gender, salary
FROM (
SELECT name, age, gender, salary
FROM tianmu_table
WHERE age < 30
UNION ALL
SELECT name, age, gender, salary
FROM tianmu_table
WHERE age >= 30
) AS derived_table
WHERE salary > 30 ) H;


drop database test_db_1707;
2 changes: 1 addition & 1 deletion sql/sql_union.cc
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ bool st_select_lex_unit::cleanup(bool full)
#ifndef NDEBUG
void st_select_lex_unit::assert_not_fully_clean()
{
assert(cleaned < UC_CLEAN);
assert(cleaned <= UC_CLEAN);
SELECT_LEX *sl= first_select();
for (;;)
{
Expand Down
14 changes: 8 additions & 6 deletions storage/tianmu/core/query_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,18 +1148,20 @@ QueryRouteTo Query::Compile(CompiledQuery *compiled_query, SELECT_LEX *selects_l
}
}

// partial optimization of LOJ conditions, JOIN::optimize(part=3)
// necessary due to already done basic transformation of conditions
// see comments in sql_select.cc:JOIN::optimize()
if (IsLOJ(join_list) &&
((!sl->join->where_cond) || (sl->join->where_cond && (uint64_t)sl->join->where_cond != 0x01))) {
sl->join->optimize(OptimizePhase::Finish_LOJ_Transform);
}

Item *field_for_subselect;
Item *cond_to_reinsert = nullptr;
List<Item> *list_to_reinsert = nullptr;

TabID tmp_table;
try {
// partial optimization of LOJ conditions, JOIN::optimize(part=3)
// necessary due to already done basic transformation of conditions
// see comments in sql_select.cc:JOIN::optimize()
if (IsLOJ(join_list))
sl->join->optimize(OptimizePhase::Finish_LOJ_Transform);

if (left_expr_for_subselect)
if (!ClearSubselectTransformation(*oper_for_subselect, field_for_subselect, conds, having, cond_to_reinsert,
list_to_reinsert, left_expr_for_subselect))
Expand Down

0 comments on commit 2daac7c

Please sign in to comment.