#
# Tests for r_engine_stats in ANALYZE FORMAT=JSON output
#
--source include/analyze-format.inc
--source include/have_sequence.inc
--source include/have_innodb.inc

create table t1 (
  pk int not null, 
  a varchar(64),
  b varchar(64),
  c varchar(64)
) engine=innodb;

insert into t1 select
  seq, seq, seq, seq
from
  seq_1_to_10000;

analyze table t1 persistent for all;

--echo # Note the r_engine_stats below.  Only non-zero members are printed
let $out=`
ANALYZE FORMAT=json
select * from t1 where pk < 120000;
`;

# Don't use "source include/analyze-format.inc" as it replaces r_engine_stats
# Replace the "pages_accessed" value, too, as it is different for some
# platforms...
--replace_regex /("(r_[a-z_]*_time(_in_progress)?_ms|r_buffer_size|pages_accessed)": )[^, \n]*/\1"REPLACED"/
evalp select '$out' as X;

evalp set @js='$out';
set @out=(select json_extract(@js,'$**.r_engine_stats.pages_accessed'));
select cast(json_extract(@out,'$[0]') as DOUBLE) > 0 as PAGES_ACCESSED_MORE_THAN_ZERO;

--echo #
--echo # Try an UPDATE
--echo #

let $out=`analyze format=json update t1 set b = b-1 where pk < 120000`;

--replace_regex /("(r_[a-z_]*_time_ms|pages_accessed|pages_updated)": )[^, \n]*/\1"REPLACED"/
evalp select '$out' as X;

evalp set @js='$out';
set @out=(select json_extract(@js,'$**.r_engine_stats.pages_updated'));
select cast(json_extract(@out,'$[0]') as DOUBLE) > 0 as PAGES_UPDATED_MORE_THAN_ZERO;

--echo #
--echo # Try a DELETE
--echo #
let $out=`analyze format=json delete from t1 where mod(pk,2)=1`;

--replace_regex /("(r_[a-z_]*_time_ms|pages_accessed|pages_updated)": )[^, \n]*/\1"REPLACED"/
evalp select '$out' as X;

evalp set @js='$out';
set @out=(select json_extract(@js,'$**.r_engine_stats.pages_updated'));
select cast(json_extract(@out,'$[0]') as DOUBLE) > 0 as PAGES_UPDATED_MORE_THAN_ZERO;

drop table t1;

--echo #
--echo # MDEV-31764: ASAN use-after-poison in trace_engine_stats upon ANALYZE FORMAT=JSON
--echo #

--disable_result_log
ANALYZE FORMAT=JSON SELECT count(*) FROM information_schema.GLOBAL_STATUS;
--enable_result_log

--echo # Another testcase without I_S:

CREATE TABLE t1 (a INT);
INSERT INTO t1 SELECT seq FROM seq_1_to_100;
CREATE TABLE t2 (s INT);
INSERT INTO t2 SELECT seq FROM seq_1_to_10;

--echo # Must use SJ-Materialization to hit the issue with temp.table:
--source include/analyze-format.inc
ANALYZE FORMAT=JSON SELECT * FROM t1 WHERE a IN (SELECT s FROM t2);

DROP TABLE t1, t2;
