Skip to content

Statement cache serves stale statements after DROP TABLE and CREATE TABLE #2603

Closed

Description

@mathiasrw

The statement cache is not cleared when a table is dropped and created again. A cached statement keeps the column list and column types of the old table. The result is wrong and no error is raised.

Reproduced on develop (v4.20.0):

alasql('CREATE TABLE x (a INT)');
alasql('INSERT INTO x VALUES (1)');
alasql('SELECT * FROM x');                    // [{a:1}]

alasql('DROP TABLE x');
alasql('CREATE TABLE x (a INT, b INT)');
alasql('INSERT INTO x (a,b) VALUES (1,2)');
alasql('SELECT * FROM x');                    // [{a:1}]        expected [{a:1, b:2}]

With alasql.options.cache = false the last query returns [{a:1, b:2}].

INSERT is hit in the same way. It keeps the column types of the old table:

alasql('CREATE TABLE y (b INT)');
alasql('INSERT INTO y VALUES (NULL)');        // stores NaN (INT path, see #2590)
alasql('DROP TABLE y');
alasql('CREATE TABLE y (b STRING)');
alasql('INSERT INTO y VALUES (NULL)');        // stores NaN again, expected undefined

Cause

alasql.dexec (src/17alasql.js) serves a cached statement only if statement.dbversion === db.dbversion. db.dbversion changes on ALTER TABLE, on transaction rollback and in the localStorage and file storage engines. CREATE TABLE and DROP TABLE do not change it. So a statement compiled before a DROP/CREATE stays valid in the cache.

Direction

Change db.dbversion in every statement that changes the schema. As a minimum: CREATE TABLE, DROP TABLE, CREATE INDEX, DROP INDEX, CREATE VIEW, DROP VIEW. Check the other CREATE/DROP statements too.

Tests: the two examples above. Expected results are the same with the cache on and off.

Related: #2587 (statement cache key collision).

Activity

  1. added a commit that references this issue on Oct 20, 2026
    df2c07f
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