summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Midenkov <midenok@gmail.com>2019-08-28 11:57:16 +0300
committerAleksey Midenkov <midenok@gmail.com>2019-09-01 14:04:25 +0300
commitc3f35ea55a7150b186284e2ef717d12e6f81e13e (patch)
tree3ad2bd5855280e9b58f95d5e58cc50318ea19cfd
parenta3e49c0d36c0347e9dee43f9996ca7c407836a51 (diff)
downloadmariadb-git-c3f35ea55a7150b186284e2ef717d12e6f81e13e.tar.gz
MDEV-18501 Partition pruning doesn't work for historical queries (refactoring)
SYSTEM_TYPE partitioning: COLUMN properties removed. Partitioning is now pure RANGE based on UNIX_TIMESTAMP(row_end). DECIMAL type is now allowed as RANGE partitioning, we can partition by UNIX_TIMESTAMP() (but not for DATETIME which depends on local timezone of course).
-rw-r--r--mysql-test/main/partition_range.result27
-rw-r--r--mysql-test/main/partition_range.test27
-rw-r--r--sql/partition_element.h23
-rw-r--r--sql/partition_info.cc22
-rw-r--r--sql/partition_info.h4
-rw-r--r--sql/sql_lex.cc4
-rw-r--r--sql/sql_partition.cc16
7 files changed, 83 insertions, 40 deletions
diff --git a/mysql-test/main/partition_range.result b/mysql-test/main/partition_range.result
index 7ae029d488c..00171e3e0bd 100644
--- a/mysql-test/main/partition_range.result
+++ b/mysql-test/main/partition_range.result
@@ -981,4 +981,31 @@ a MAX(b)
SHOW status LIKE 'handler_read_key';
Variable_name Value
Handler_read_key 4
+#
+# MDEV-18501 Partition pruning doesn't work for historical queries
+#
+set time_zone= '+00:00';
+create or replace table t1 (d datetime(6))
+partition by range (unix_timestamp(d)) (
+partition p0 values less than (1),
+partition p1 values less than (maxvalue));
+ERROR HY000: Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed
+# DECIMAL functions are now allowed, partitioning is done by integer part
+create or replace table t1 (d timestamp(6))
+partition by range (unix_timestamp(d)) (
+partition p0 values less than (946684801),
+partition p1 values less than (maxvalue));
+insert into t1 values
+# go to p0
+('2000-01-01 00:00:00'),
+('2000-01-01 00:00:00.000001'),
+# goes to p1
+('2000-01-01 00:00:01');
+select * from t1 partition (p0);
+d
+2000-01-01 00:00:00.000000
+2000-01-01 00:00:00.000001
+select * from t1 partition (p1);
+d
+2000-01-01 00:00:01.000000
DROP TABLE t1, t2;
diff --git a/mysql-test/main/partition_range.test b/mysql-test/main/partition_range.test
index 3ff03248d88..628602ffa25 100644
--- a/mysql-test/main/partition_range.test
+++ b/mysql-test/main/partition_range.test
@@ -971,4 +971,31 @@ SELECT a, MAX(b) FROM t2 WHERE a IN (10, 100) GROUP BY a;
--echo # Should be no more than 4 reads.
SHOW status LIKE 'handler_read_key';
+--echo #
+--echo # MDEV-18501 Partition pruning doesn't work for historical queries
+--echo #
+set time_zone= '+00:00';
+let $ts= `select unix_timestamp('2000-01-01 00:00:00') + 1`;
+
+--error ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR
+create or replace table t1 (d datetime(6))
+partition by range (unix_timestamp(d)) (
+ partition p0 values less than (1),
+ partition p1 values less than (maxvalue));
+
+--echo # DECIMAL functions are now allowed, partitioning is done by integer part
+eval create or replace table t1 (d timestamp(6))
+partition by range (unix_timestamp(d)) (
+ partition p0 values less than ($ts),
+ partition p1 values less than (maxvalue));
+
+insert into t1 values
+ # go to p0
+ ('2000-01-01 00:00:00'),
+ ('2000-01-01 00:00:00.000001'),
+ # goes to p1
+ ('2000-01-01 00:00:01');
+select * from t1 partition (p0);
+select * from t1 partition (p1);
+
DROP TABLE t1, t2;
diff --git a/sql/partition_element.h b/sql/partition_element.h
index a3eb6953be1..ff0d0d59fc4 100644
--- a/sql/partition_element.h
+++ b/sql/partition_element.h
@@ -98,7 +98,7 @@ enum stat_trx_field
class partition_element :public Sql_alloc
{
public:
- enum elem_type
+ enum elem_type_enum
{
CONVENTIONAL= 0,
CURRENT,
@@ -125,19 +125,7 @@ public:
bool max_value; // MAXVALUE range
uint32 id;
bool empty;
-
- // TODO: subclass partition_element by partitioning type to avoid such semantic
- // mixup
- elem_type type()
- {
- return (elem_type)(int(signed_flag) << 1 | int(max_value));
- }
-
- void type(elem_type val)
- {
- max_value= (bool)(val & 1);
- signed_flag= (bool)(val & 2);
- }
+ elem_type_enum type;
partition_element()
: part_max_rows(0), part_min_rows(0), range_value(0),
@@ -148,7 +136,8 @@ public:
nodegroup_id(UNDEF_NODEGROUP), has_null_value(FALSE),
signed_flag(FALSE), max_value(FALSE),
id(UINT_MAX32),
- empty(true)
+ empty(true),
+ type(CONVENTIONAL)
{}
partition_element(partition_element *part_elem)
: part_max_rows(part_elem->part_max_rows),
@@ -164,13 +153,13 @@ public:
nodegroup_id(part_elem->nodegroup_id),
has_null_value(FALSE),
id(part_elem->id),
- empty(part_elem->empty)
+ empty(part_elem->empty),
+ type(CONVENTIONAL)
{}
~partition_element() {}
part_column_list_val& get_col_val(uint idx)
{
- DBUG_ASSERT(type() == CONVENTIONAL || list_val_list.elements == 1);
part_elem_value *ev= list_val_list.head();
DBUG_ASSERT(ev);
DBUG_ASSERT(ev->col_val_array);
diff --git a/sql/partition_info.cc b/sql/partition_info.cc
index 65cf0298f4f..f9669931eab 100644
--- a/sql/partition_info.cc
+++ b/sql/partition_info.cc
@@ -894,15 +894,16 @@ bool partition_info::vers_setup_expression(THD * thd, uint32 alter_add)
DBUG_ASSERT(part_type == VERSIONING_PARTITION);
DBUG_ASSERT(table->versioned(VERS_TIMESTAMP));
- DBUG_ASSERT(num_columns == 1);
if (!alter_add)
{
Field *row_end= table->vers_end_field();
- part_field_list.push_back(row_end->field_name.str, thd->mem_root);
- DBUG_ASSERT(part_field_list.elements == 1);
// needed in handle_list_of_fields()
row_end->flags|= GET_FIXED_FIELDS_FLAG;
+ Name_resolution_context *context= &thd->lex->current_select->context;
+ Item *row_end_item= new (thd->mem_root) Item_field(thd, context, row_end);
+ Item *row_end_ts= new (thd->mem_root) Item_func_unix_timestamp(thd, row_end_item);
+ set_part_expr(thd, row_end_ts, false);
}
if (alter_add)
@@ -911,12 +912,12 @@ bool partition_info::vers_setup_expression(THD * thd, uint32 alter_add)
partition_element *el;
for(uint32 id= 0; ((el= it++)); id++)
{
- DBUG_ASSERT(el->type() != partition_element::CONVENTIONAL);
+ DBUG_ASSERT(el->type != partition_element::CONVENTIONAL);
/* Newly added element is inserted before AS_OF_NOW. */
- if (el->id == UINT_MAX32 || el->type() == partition_element::CURRENT)
+ if (el->id == UINT_MAX32 || el->type == partition_element::CURRENT)
{
el->id= id;
- if (el->type() == partition_element::CURRENT)
+ if (el->type == partition_element::CURRENT)
break;
}
}
@@ -1343,13 +1344,13 @@ bool partition_info::check_partition_info(THD *thd, handlerton **eng_type,
}
if (part_type == VERSIONING_PARTITION)
{
- if (part_elem->type() == partition_element::HISTORY)
+ if (part_elem->type == partition_element::HISTORY)
{
hist_parts++;
}
else
{
- DBUG_ASSERT(part_elem->type() == partition_element::CURRENT);
+ DBUG_ASSERT(part_elem->type == partition_element::CURRENT);
now_parts++;
}
}
@@ -2690,9 +2691,8 @@ bool check_partition_dirs(partition_info *part_info)
bool partition_info::vers_init_info(THD * thd)
{
part_type= VERSIONING_PARTITION;
- list_of_part_fields= TRUE;
- column_list= TRUE;
- num_columns= 1;
+ list_of_part_fields= true;
+ column_list= false;
vers_info= new (thd->mem_root) Vers_part_info;
if (unlikely(!vers_info))
return true;
diff --git a/sql/partition_info.h b/sql/partition_info.h
index 253e88174dd..2aed6cbc5db 100644
--- a/sql/partition_info.h
+++ b/sql/partition_info.h
@@ -55,11 +55,11 @@ struct Vers_part_info : public Sql_alloc
if (now_part)
{
DBUG_ASSERT(now_part->id != UINT_MAX32);
- DBUG_ASSERT(now_part->type() == partition_element::CURRENT);
+ DBUG_ASSERT(now_part->type == partition_element::CURRENT);
if (hist_part)
{
DBUG_ASSERT(hist_part->id != UINT_MAX32);
- DBUG_ASSERT(hist_part->type() == partition_element::HISTORY);
+ DBUG_ASSERT(hist_part->type == partition_element::HISTORY);
}
return true;
}
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 8d8ef73fcd1..1825ce7c1ce 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -8168,7 +8168,7 @@ bool LEX::part_values_current(THD *thd)
create_last_non_select_table->table_name.str);
return true;
}
- elem->type(partition_element::CURRENT);
+ elem->type= partition_element::CURRENT;
DBUG_ASSERT(part_info->vers_info);
part_info->vers_info->now_part= elem;
if (unlikely(part_info->init_column_part(thd)))
@@ -8202,7 +8202,7 @@ bool LEX::part_values_history(THD *thd)
create_last_non_select_table->table_name.str);
return true;
}
- elem->type(partition_element::HISTORY);
+ elem->type= partition_element::HISTORY;
if (unlikely(part_info->init_column_part(thd)))
return true;
return false;
diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc
index 34f8a4e3aee..ba6d0f14aeb 100644
--- a/sql/sql_partition.cc
+++ b/sql/sql_partition.cc
@@ -1975,7 +1975,6 @@ bool fix_partition_func(THD *thd, TABLE *table, bool is_create_table_ind)
}
}
DBUG_ASSERT(part_info->part_type != NOT_A_PARTITION);
- DBUG_ASSERT(part_info->part_type != VERSIONING_PARTITION || part_info->column_list);
/*
Partition is defined. We need to verify that partitioning
function is correct.
@@ -2008,15 +2007,15 @@ bool fix_partition_func(THD *thd, TABLE *table, bool is_create_table_ind)
{
if (part_info->column_list)
{
- if (part_info->part_type == VERSIONING_PARTITION &&
- part_info->vers_setup_expression(thd))
- goto end;
List_iterator<const char> it(part_info->part_field_list);
if (unlikely(handle_list_of_fields(thd, it, table, part_info, FALSE)))
goto end;
}
else
{
+ if (part_info->part_type == VERSIONING_PARTITION &&
+ part_info->vers_setup_expression(thd))
+ goto end;
if (unlikely(fix_fields_part_func(thd, part_info->part_expr,
table, FALSE, is_create_table_ind)))
goto end;
@@ -2032,7 +2031,8 @@ bool fix_partition_func(THD *thd, TABLE *table, bool is_create_table_ind)
goto end;
}
if (unlikely(!part_info->column_list &&
- part_info->part_expr->result_type() != INT_RESULT))
+ part_info->part_expr->result_type() != INT_RESULT &&
+ part_info->part_expr->result_type() != DECIMAL_RESULT))
{
part_info->report_part_expr_error(FALSE);
goto end;
@@ -2541,7 +2541,7 @@ static int add_partition_values(String *str, partition_info *part_info,
}
else if (part_info->part_type == VERSIONING_PARTITION)
{
- switch (p_elem->type())
+ switch (p_elem->type)
{
case partition_element::CURRENT:
err+= str->append(STRING_WITH_LEN(" CURRENT"));
@@ -5321,7 +5321,7 @@ that are reorganised.
partition_element *el;
while ((el= it++))
{
- if (el->type() == partition_element::CURRENT)
+ if (el->type == partition_element::CURRENT)
{
it.remove();
now_part= el;
@@ -5417,7 +5417,7 @@ that are reorganised.
{
if (tab_part_info->part_type == VERSIONING_PARTITION)
{
- if (part_elem->type() == partition_element::CURRENT)
+ if (part_elem->type == partition_element::CURRENT)
{
my_error(ER_VERS_WRONG_PARTS, MYF(0), table->s->table_name.str);
goto err;