From 0c51d54b66313228223273f26e256b26eb66622d 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 --- sqlite/src/analyze.c | 10 ++++----- tests/analyze.test/t16.req | 0 tests/analyze.test/t16.req.out | 10 +++++++++ tests/analyze.test/t16.sh | 39 ++++++++++++++++++++++++++++++++++ tests/analyze.test/t16.tool | 1 + 5 files changed, 55 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.sh create mode 100755 tests/analyze.test/t16.tool 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..16e121c6f0 --- /dev/null +++ b/tests/analyze.test/t16.req.out @@ -0,0 +1,10 @@ +(count(*)=0) +(count(*)=0) +TURNING ON ANALYZE EMPTY TABLES +(count(*)=1) +(count(*)=1) +INSERTING RECORDS +(rows inserted=50000) +RUNNING ANALYZE +(count(*)=1) +(count(*)=24) diff --git a/tests/analyze.test/t16.sh b/tests/analyze.test/t16.sh new file mode 100755 index 0000000000..c23b1a6030 --- /dev/null +++ b/tests/analyze.test/t16.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +function purge_stats +{ + ${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "truncate sqlite_stat1" + ${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "truncate sqlite_stat4" +} +function count_stats +{ + ${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "select count(*) from sqlite_stat1" + ${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "select count(*) from sqlite_stat4" +} + +purge_stats +#default behavior +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "DROP TABLE IF EXISTS empty" +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "CREATE TABLE empty(a int primary key)" +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "ANALYZE empty" +count_stats + +echo "TURNING ON ANALYZE EMPTY TABLES" +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "exec procedure sys.cmd.send('setsqlattr analyze_empty_tables on')" +purge_stats +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "ANALYZE empty" +count_stats + +echo "INSERTING RECORDS" +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "INSERT INTO EMPTY SELECT * FROM generate_series(0,49999)" +echo "RUNNING ANALYZE" +purge_stats +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "ANALYZE empty" +count_stats + +echo "TURNING OFF ANALYZE EMPTY TABLES" +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "exec procedure sys.cmd.send('setsqlattr analyze_empty_tables off')" +echo "RUNNING ANALYZE" +purge_stats +${CDB2SQL_EXE} ${CDB2_OPTIONS} ${DBNAME} local "ANALYZE empty" +count_stats diff --git a/tests/analyze.test/t16.tool b/tests/analyze.test/t16.tool new file mode 100755 index 0000000000..3fd0e4ac39 --- /dev/null +++ b/tests/analyze.test/t16.tool @@ -0,0 +1 @@ +t16.sh