dolthub / dolthub/doltlite

COUNT BETWEEN optimizations bypass application-defined count aggregates

Closed
#2,891 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
268
Forks
18
Avg merge
2h 27m
Merged PRs (30d)
447

Description

The two range-count shortcuts accept a function named count even without SQLITE_FUNC_COUNT, bypassing application-defined aggregate callbacks.

Reproduced at f5d4636e70 using sqlite3_create_function to register a zero-argument count aggregate whose final callback returns 42:

```sql
CREATE TABLE t(id INTEGER PRIMARY KEY,v INTEGER);
INSERT INTO t VALUES(1,1),(2,2),(3,3);
SELECT count(*) FROM t;
SELECT count(*) FROM t WHERE id BETWEEN 1 AND 3;
CREATE INDEX idx ON t(v);
SELECT count(*) FROM t WHERE v BETWEEN 1 AND 3;
```

Actual DoltLite: 42, 3, 3. Expected and freshly built stock SQLite: 42, 42, 42.

Minimal extension:
```c
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
static void step(sqlite3_context *c,int n,sqlite3_value **v) {(void)c;(void)n;(void)v;}
static void final(sqlite3_context *c) {sqlite3_result_int(c,42);}
int sqlite3_customcount_init(sqlite3 *db,char **err,const sqlite3_api_routines *api){
SQLITE_EXTENSION_INIT2(api);
(void)err;
return sqlite3_create_function(db,"count",0,SQLITE_UTF8,0,0,step,final);
}
```

Remove the function-name fallback in both shortcuts and add C API tests comparing range and ordinary execution with overridden aggregates. Preserve performance checks for the built-in aggregate.

Sources: https://github.com/dolthub/doltlite/blob/f5d4636e70/src/select.c#L5675 and https://github.com/dolthub/doltlite/blob/f5d4636e70/src/select.c#L5755

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/select.c at the range-count shortcuts around lines 5675 and 5755, then run the supplied custom count aggregate reproducer. Add C API tests comparing range and ordinary execution with an overridden aggregate, while preserving performance checks for the built-in aggregate. Done means both range queries return 42 like ordinary execution and the built-in count optimization remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, sqlite
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.