Skip to content

Commit

Permalink
Fix bugs in analyze_empty_tables behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Aakash Arayambeth <aarayambeth@bloomberg.net>
  • Loading branch information
Aakash Arayambeth committed Dec 5, 2024
1 parent 1b72251 commit 85641a2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
10 changes: 5 additions & 5 deletions sqlite/src/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */

Expand Down
Empty file added tests/analyze.test/t16.req
Empty file.
10 changes: 10 additions & 0 deletions tests/analyze.test/t16.req.out
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions tests/analyze.test/t16.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/analyze.test/t16.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t16.sh

0 comments on commit 85641a2

Please sign in to comment.