From bfbd2af45e6b93273ea817619330fa81a55145db Mon Sep 17 00:00:00 2001 From: Morgan Douglas Date: Thu, 3 Oct 2024 11:34:15 -0400 Subject: [PATCH] Fix bug Signed-off-by: Morgan Douglas --- sqlite/ext/comdb2/files_util.c | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/sqlite/ext/comdb2/files_util.c b/sqlite/ext/comdb2/files_util.c index efb9e0e3bc..91f5fc1a49 100644 --- a/sqlite/ext/comdb2/files_util.c +++ b/sqlite/ext/comdb2/files_util.c @@ -475,6 +475,8 @@ static int is_file_type_constraint(const constraint_type_t constraint_name) { int files_util_filter(sqlite3_vtab_cursor *pVtabCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { + logmsg(LOGMSG_DEBUG, "%s\n", __func__); + int rc = SQLITE_OK; systbl_files_cursor *pCur = (systbl_files_cursor *)pVtabCursor; @@ -483,22 +485,20 @@ int files_util_filter(sqlite3_vtab_cursor *pVtabCursor, int idxNum, for (int i=0; inConstraint); - if (!constraints) { return ENOMEM; } - memset(constraints, 0, sizeof(constraint_type_t)*pIdxInfo->nConstraint); + constraint_type_t * constraints = NULL; + if (pIdxInfo->nConstraint > 0) { + constraints = sqlite3_malloc(sizeof(constraint_type_t)*pIdxInfo->nConstraint); + if (!constraints) { return ENOMEM; } + memset(constraints, 0, sizeof(constraint_type_t)*pIdxInfo->nConstraint); + } const struct sqlite3_index_constraint *pConstraint; pConstraint = pIdxInfo->aConstraint; - for (i = 0; i < pIdxInfo->nConstraint; i++, pConstraint++) { + for (int i = 0; i < pIdxInfo->nConstraint; i++, pConstraint++) { if (pConstraint->usable == 0) continue; if (parse_constraint(pConstraint, i, constraints) != SQLITE_OK) { return SQLITE_ERROR; } - if (constraints[i] != 0) { add_constraint(pIdxInfo, i, &nArg, omit); } + if (constraints[i] != 0) { add_constraint(pIdxInfo, i, &nArg, 1 /* omit */); } } pIdxInfo->orderByConsumed = 0; pIdxInfo->estimatedCost = estimate_cost(constraints, pIdxInfo->nConstraint); pIdxInfo->idxStr = (char *) constraints; - pIdxInfo->needToFreeIdxStr = 1; + pIdxInfo->needToFreeIdxStr = constraints != NULL; + + logmsg(LOGMSG_DEBUG, "%s: Estimated cost for index with %d constraints is %f\n", + __func__, nArg, pIdxInfo->estimatedCost); + return SQLITE_OK; }