Skip to content

Statement cache returns another query's result when two SQL texts share a hash #2587

Closed

Description

@mathiasrw

The statement cache is keyed on a 32-bit hash of the SQL text, and the text is not compared when an entry is found. Two different SQL strings with the same hash share one cache entry, so the second one runs the first one's compiled statement and returns its result. No error is raised.

Reproduced on v4.20.0:

alasql('SELECT 369825 AS a');     // [{a: 369825}]
alasql('SELECT 1025164 AS a');    // [{a: 369825}]   wrong: the first query's result
alasql.options.cache = false;
alasql('SELECT 369825 AS a');     // [{a: 369825}]
alasql('SELECT 1025164 AS a');    // [{a: 1025164}]  correct

Both strings hash to the same key (alasql.utils.hash('SELECT 369825 AS a|joinstar:overwrite') and the same call for 1025164 both give 1329214344).

How likely

The cache holds up to alasql.MAXSQLCACHESIZE (10,000) statements per database. With 10,000 distinct statements in a 32-bit key space, the chance that some pair collides is about 1.2% (birthday estimate). Code that passes values as parameters has few distinct statements and is very unlikely to see it. Code that builds SQL text with the values inside it creates thousands of distinct statements, and that is where it bites: a wrong row set comes back for a query that looks correct.

Cause

alasql.dexec in src/17alasql.js:

var hh = hash(sql + '|joinstar:' + alasql.options.joinstar);

if (alasql.options.cache) {
	let statement = db.sqlCache[hh];
	if (statement && db.dbversion === statement.dbversion) {
		var res = statement(params, cb);     // statement.sql is never compared with sql

db.astCache[hh] a few lines below uses the same key, so a parsed syntax tree can be reused for the wrong text in the same way.

The lookup by hash dates from the first versions of the cache (2014–2015). The joinstar part of the key was added later.

Fix

Two ways. Both were tried on a patched copy of the v4.20.0 build with the reproduction above and a timing loop (a query over a 10-row array with WHERE and ORDER BY, cache on). The test suite was not run against either.

Reproduction One cached call
Today wrong 1,523 ns
A. Keep the hash, compare the text on a hit (statement.sql === sql, and the same for the syntax-tree cache) correct 1,508 ns
B. Use the text itself as the key: var hh = sql + '|joinstar:' + alasql.options.joinstar; correct 838 ns

B is one line, cannot collide, and is faster: computing the hash loops over every character of the SQL on every call, and that loop is close to half the cost of a cached call today. The JavaScript engine's own string keys do the same job natively. statement.sql already keeps the text alive, so B holds no extra memory.

Expected

  • A statement is only served from the cache for the exact SQL text (and options) it was compiled from.
  • A test with the colliding pair above.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    ec4938e
  2. mathiasrw commented on Oct 19, 2026

    @mathiasrw
    MemberAuthor

    Released as part of v4.20.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions