diff --git a/src/Interpreters/MonotonicityCheckVisitor.h b/src/Interpreters/MonotonicityCheckVisitor.h index 4b9f36ab72d..a7ce0774862 100644 --- a/src/Interpreters/MonotonicityCheckVisitor.h +++ b/src/Interpreters/MonotonicityCheckVisitor.h @@ -70,6 +70,9 @@ public: if (!pos) return false; + if (*pos >= tables.size()) + return false; + if (auto data_type_and_name = tables[*pos].columns.tryGetByName(identifier->shortName())) { arg_data_type = data_type_and_name->type; diff --git a/tests/queries/0_stateless/02147_order_by_optimizations.sql b/tests/queries/0_stateless/02147_order_by_optimizations.sql index 7aa631ff432..3925e92bffc 100644 --- a/tests/queries/0_stateless/02147_order_by_optimizations.sql +++ b/tests/queries/0_stateless/02147_order_by_optimizations.sql @@ -13,3 +13,7 @@ SET optimize_monotonous_functions_in_order_by = 1; EXPLAIN SYNTAX SELECT * FROM t_02147 ORDER BY toStartOfHour(date), v; EXPLAIN SYNTAX SELECT * FROM t_02147_dist ORDER BY toStartOfHour(date), v; EXPLAIN SYNTAX SELECT * FROM t_02147_merge ORDER BY toStartOfHour(date), v; + +drop table t_02147; +CREATE TABLE t_02147 (date DateTime, v UInt32) ENGINE = MergeTree ORDER BY date; +select *, toString(t.v) as s from t_02147_merge as t order by date, s;