From 4b5d25c91975566bed045dba5ad0ff3fde78ab2d Mon Sep 17 00:00:00 2001 From: Aakash Arayambeth Date: Thu, 5 Dec 2024 13:48:53 -0500 Subject: [PATCH] Fix bugs in analyze_empty_tables behavior Signed-off-by: Aakash Arayambeth --- sqlite/src/analyze.c | 10 ++++----- tests/analyze.test/t16.req | 0 tests/analyze.test/t16.req.out | 14 ++++++++++++ tests/analyze.test/t16.req.tool | 1 + tests/analyze.test/t16.sh | 40 +++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100755 tests/analyze.test/t16.req create mode 100755 tests/analyze.test/t16.req.out create mode 100755 tests/analyze.test/t16.req.tool create mode 100755 tests/analyze.test/t16.sh diff --git a/sqlite/src/analyze.c b/sqlite/src/analyze.c index a12e06a8ff..1ac5857de4 100644 --- a/sqlite/src/analyze.c +++ b/sqlite/src/analyze.c @@ -1575,6 +1575,11 @@ static void analyzeOneTable( } #endif /* defined(SQLITE_BUILDING_FOR_COMDB2) */ addrNext = sqlite3VdbeCurrentAddr(v); +#if defined(SQLITE_BUILDING_FOR_COMDB2) + if( sqlite3_gbl_tunables.analyze_empty_tables ){ + sqlite3VdbeJumpHere(v, addrRewind); + } +#endif /* defined(SQLITE_BUILDING_FOR_COMDB2) */ #if !defined(SQLITE_BUILDING_FOR_COMDB2) callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid); addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); @@ -1606,11 +1611,6 @@ static void analyzeOneTable( sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid); sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */ sqlite3VdbeJumpHere(v, addrIsNull); -#if defined(SQLITE_BUILDING_FOR_COMDB2) - if( sqlite3_gbl_tunables.analyze_empty_tables ){ - sqlite3VdbeJumpHere(v, addrRewind); - } -#endif /* defined(SQLITE_BUILDING_FOR_COMDB2) */ } #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ diff --git a/tests/analyze.test/t16.req b/tests/analyze.test/t16.req new file mode 100755 index 0000000000..e69de29bb2 diff --git a/tests/analyze.test/t16.req.out b/tests/analyze.test/t16.req.out new file mode 100755 index 0000000000..a06d15ff6c --- /dev/null +++ b/tests/analyze.test/t16.req.out @@ -0,0 +1,14 @@ +(count(*)=0) +(count(*)=0) +TURNING ON ANALYZE EMPTY TABLES +(count(*)=1) +(count(*)=1) +INSERTING RECORDS +(rows inserted=50000) +RUNNING ANALYZE +(count(*)=1) +(count(*)=24) +TURNING OFF ANALYZE EMPTY TABLES +RUNNING ANALYZE +(count(*)=1) +(count(*)=24) diff --git a/tests/analyze.test/t16.req.tool b/tests/analyze.test/t16.req.tool new file mode 100755 index 0000000000..3fd0e4ac39 --- /dev/null +++ b/tests/analyze.test/t16.req.tool @@ -0,0 +1 @@ +t16.sh diff --git a/tests/analyze.test/t16.sh b/tests/analyze.test/t16.sh new file mode 100755 index 0000000000..a67d5f7b69 --- /dev/null +++ b/tests/analyze.test/t16.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +rep=$(${CDB2SQL_EXE} --tabs ${CDB2_OPTIONS} ${DBNAME} default "select host from comdb2_cluster where is_master='N' and coherent_state='coherent' limit 1") +function purge_stats +{ + ${CDB2SQL_EXE} ${DBNAME} --host $rep "truncate sqlite_stat1" + ${CDB2SQL_EXE} ${DBNAME} --host $rep "truncate sqlite_stat4" +} +function count_stats +{ + ${CDB2SQL_EXE} ${DBNAME} --host $rep "select count(*) from sqlite_stat1" + ${CDB2SQL_EXE} ${DBNAME} --host $rep "select count(*) from sqlite_stat4" +} + +purge_stats +${CDB2SQL_EXE} ${DBNAME} --host $rep "exec procedure sys.cmd.send('setsqlattr analyze_empty_tables off')" +${CDB2SQL_EXE} ${DBNAME} --host $rep "DROP TABLE IF EXISTS empty" +${CDB2SQL_EXE} ${DBNAME} --host $rep "CREATE TABLE empty(a int primary key)" +${CDB2SQL_EXE} ${DBNAME} --host $rep "ANALYZE empty" +count_stats + +echo "TURNING ON ANALYZE EMPTY TABLES" +${CDB2SQL_EXE} ${DBNAME} --host $rep "exec procedure sys.cmd.send('setsqlattr analyze_empty_tables on')" +purge_stats +${CDB2SQL_EXE} ${DBNAME} --host $rep "ANALYZE empty" +count_stats + +echo "INSERTING RECORDS" +${CDB2SQL_EXE} ${DBNAME} --host $rep "INSERT INTO EMPTY SELECT * FROM generate_series(0,49999)" +echo "RUNNING ANALYZE" +purge_stats +${CDB2SQL_EXE} ${DBNAME} --host $rep "ANALYZE empty" +count_stats + +echo "TURNING OFF ANALYZE EMPTY TABLES" +${CDB2SQL_EXE} ${DBNAME} --host $rep "exec procedure sys.cmd.send('setsqlattr analyze_empty_tables off')" +echo "RUNNING ANALYZE" +purge_stats +${CDB2SQL_EXE} ${DBNAME} --host $rep "ANALYZE empty" +count_stats