--echo #
--echo # MDEV-36132 Optimizer support for functional indexes: handle GROUP/ORDER BY
--echo #
--source include/not_embedded.inc
--source include/have_sequence.inc
create table t (c int, key (c));
insert into t select seq from seq_1_to_10000;
alter table t
  add column vc bigint as (c + 1),
  add index(vc);
set @old_optimizer_trace=@@optimizer_trace;
set optimizer_trace=1;

## order by
explain select c + 1 from t order by c + 1;
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;

## group by
explain select c + 1 from t group by c + 1;
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;

set optimizer_trace=@old_optimizer_trace;
drop table t;

# multiple items

create table t (c int, key (c));
insert into t select seq from seq_1_to_10000;
alter table t
  add column vc1 bigint as (c + 1),
  add index(vc1);
alter table t
  add column vc2 bigint as (1 - c),
  add index(vc2);
set @old_optimizer_trace=@@optimizer_trace;
set optimizer_trace=1;

## order by

explain select * from t order by c + 1, 1 - c;
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;

## group by

explain select * from t group by c + 1, 1 - c;
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;

set optimizer_trace=@old_optimizer_trace;
drop table t;
