From 6aa47fae304bd1f3efc0ebd2035bc1481ded73a3 Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Tue, 29 Oct 2024 11:39:17 +0100 Subject: [PATCH] MDEV-35276 Assertion failure in find_producing_item upon a query from a view Two problem solved: 1) Item_default_value makes a shallow copy so the copy should not delete field belong to the Item 2) Item_default_value should not inherit derived_field_transformer_for_having and derived_field_transformer_for_where (in this variant pushing DEFAULT(f) is prohibited (return NULL) but if return "this" it will be allowed (should go with a lot of tests)) --- mysql-test/main/derived_cond_pushdown.result | 27 ++++++++++++++++++ mysql-test/main/derived_cond_pushdown.test | 30 ++++++++++++++++++++ sql/item.cc | 3 +- sql/item.h | 23 ++++++++++++--- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result index 4fa8867e2f4..ca63e138fe7 100644 --- a/mysql-test/main/derived_cond_pushdown.result +++ b/mysql-test/main/derived_cond_pushdown.result @@ -19051,4 +19051,31 @@ EXPLAIN } } drop table t1, t2; +# +# MDEV-35276: Assertion failure in find_producing_item upon a query +# from a view +# +# Note: for all followng queries with DEFAULT(f) hishdown is +# prohibited +CREATE VIEW v AS select 0 AS f; +SELECT * FROM v WHERE f = DEFAULT(f); +f +0 +CREATE TABLE t1 (c int DEFAULT 0); +insert into t1 values (0), (1); +SELECT * FROM v,t1 WHERE f = DEFAULT(c); +f c +0 0 +0 1 +ALTER TABLE t1 ALTER c SET DEFAULT 1; +SELECT * FROM v,t1 WHERE f = DEFAULT(c); +f c +drop table t1; +create table t1 (a int, c int DEFAULT a); +insert into t1 values (0, -1), (1, -2); +SELECT * FROM v,t1 WHERE f = DEFAULT(c); +f a c +0 0 -1 +DROP VIEW v; +DROP TABLE t1; # End of 10.5 tests diff --git a/mysql-test/main/derived_cond_pushdown.test b/mysql-test/main/derived_cond_pushdown.test index 179e033889f..4e91bcaa372 100644 --- a/mysql-test/main/derived_cond_pushdown.test +++ b/mysql-test/main/derived_cond_pushdown.test @@ -4188,4 +4188,34 @@ execute stmt; drop table t1, t2; +--echo # +--echo # MDEV-35276: Assertion failure in find_producing_item upon a query +--echo # from a view +--echo # +--echo # Note: for all followng queries with DEFAULT(f) hishdown is +--echo # prohibited + +CREATE VIEW v AS select 0 AS f; + +SELECT * FROM v WHERE f = DEFAULT(f); + +CREATE TABLE t1 (c int DEFAULT 0); +insert into t1 values (0), (1); + +SELECT * FROM v,t1 WHERE f = DEFAULT(c); + +ALTER TABLE t1 ALTER c SET DEFAULT 1; + +SELECT * FROM v,t1 WHERE f = DEFAULT(c); + +drop table t1; +create table t1 (a int, c int DEFAULT a); +insert into t1 values (0, -1), (1, -2); + +SELECT * FROM v,t1 WHERE f = DEFAULT(c); + +DROP VIEW v; +DROP TABLE t1; + + --echo # End of 10.5 tests diff --git a/sql/item.cc b/sql/item.cc index 078d223bb25..439778f654c 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -9659,7 +9659,8 @@ bool Item_default_value::fix_fields(THD *thd, Item **items) void Item_default_value::cleanup() { - delete field; // Free cached blob data + if (!m_share_field) + delete field; // Free cached blob data Item_field::cleanup(); } diff --git a/sql/item.h b/sql/item.h index 3e1e9b25c71..204f98b7677 100644 --- a/sql/item.h +++ b/sql/item.h @@ -6646,8 +6646,9 @@ public: class Item_default_value : public Item_field { - bool vcol_assignment_ok; - bool m_associated= false; + bool vcol_assignment_ok:1; + bool m_associated:1; + bool m_share_field:1; void calculate(); public: @@ -6655,7 +6656,11 @@ public: Item_default_value(THD *thd, Name_resolution_context *context_arg, Item *a, bool vcol_assignment_arg) : Item_field(thd, context_arg), - vcol_assignment_ok(vcol_assignment_arg), arg(a) {} + vcol_assignment_ok(vcol_assignment_arg), arg(a) + { + m_associated= false; + m_share_field= false; + } Type type() const override { return DEFAULT_VALUE_ITEM; } bool eq(const Item *item, bool binary_cmp) const override; bool fix_fields(THD *, Item **) override; @@ -6717,6 +6722,10 @@ public: Item *transform(THD *thd, Item_transformer transformer, uchar *args) override; + Item *derived_field_transformer_for_having(THD *thd, uchar *arg) override + { return NULL; } + Item *derived_field_transformer_for_where(THD *thd, uchar *arg) override + { return NULL; } Field *create_tmp_field_ex(MEM_ROOT *root, TABLE *table, Tmp_field_src *src, const Tmp_field_param *param) override; @@ -6726,7 +6735,13 @@ public: */ bool associate_with_target_field(THD *thd, Item_field *field) override; Item *do_get_copy(THD *thd) const override - { return get_item_copy(thd, this); } + { + Item_default_value *new_item= + (Item_default_value *) get_item_copy(thd, this); + // This is a copy so do not manage the field and should not delete it + new_item->m_share_field= 1; + return new_item; + } Item* do_build_clone(THD *thd) const override { return get_copy(thd); } private: bool tie_field(THD *thd);