Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION alter col leaves sqlitesequence table behind note that i'm unsure of…

  1. User

    ISSUE DESCRIPTION

    alter col leaves sqlite_sequence table behind note that i'm unsure of alter column rules, i was not able to find any docs or rules around this, but my expectation is when we change a autoincrement col to plain text there shouldn't be a stale sqlite_sequence.

    turso> CREATE TABLE t(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
      INSERT INTO t(b) VALUES('x'),('y');
    
      ALTER TABLE t ALTER COLUMN a TO a2 TEXT;
    
      SELECT * FROM sqlite_sequence;
      SELECT sql FROM sqlite_schema WHERE name='t';
    ┌──────┬─────┐
    │ name │ seq │
    ├──────┼─────┤
    │ t    │   2 │
    └──────┴─────┘
    ┌──────────────────────────────────┐
    │ sql                              │
    ├──────────────────────────────────┤
    │ CREATE TABLE t (a2 TEXT, b TEXT) │
    └──────────────────────────────────┘
    turso> select * from sqlite_schema;
    ┌───────┬─────────────────┬─────────────────┬──────────┬────────────────────────────────────────┐
    │ type  │ name            │ tbl_name        │ rootpage │ sql                                    │
    ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤
    │ table │ sqlite_sequence │ sqlite_sequence │        2 │ CREATE TABLE sqlite_sequence(name,seq) │
    ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤
    │ table │ t               │ t               │        3 │ CREATE TABLE t (a2 TEXT, b TEXT)       │
    └───────┴─────────────────┴─────────────────┴──────────┴────────────────────────────────────────┘
    

    Interface

    No new interfaces are introduced.

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /turso, current file: none) bash-$

  2. GLM Assistant

    Let me explore the repository structure to understand the codebase.

  3. GLM Assistant

    Let me look for the ALTER COLUMN / ALTER TABLE implementation in the core.

  4. GLM Assistant

    The file is large. Let me look at the ALTER COLUMN handling specifically. Let me search for where the column type is altered (AlterColumn).

  5. GLM Assistant

    Let me look at how the AlterColumn insn is executed and how autoincrement is tracked.

  6. GLM Assistant

    Now I understand the flow. The ALTER COLUMN doesn't handle the autoincrement cleanup. When an AUTOINCREMENT column is altered to a non-autoincrement column (or removed from being the primary key), the sqlite_sequence table should be cleaned up.

    Let me understand how SQLite handles this. In SQLite:

    • When a table no longer has AUTOINCREMENT (e.g. via DROP COLUMN or ALTER COLUMN removing the autoincrement property), the sqlite_sequence row for that table is removed.
    • If no tables have autoincrement, the sqlite_sequence table itself is dropped.

    Actually wait - this is a custom Turso ALTER COLUMN feature (not standard SQLite). Let me understand what has_autoincrement is and how the table tracks it.

    Let me look at the schema for BTreeTable to understand has_autoincrement.

  7. GLM Assistant

    Now I understand the structure. has_autoincrement is a field on the BTreeTable. When we alter a column from autoincrement to text, the new column no longer has the autoincrement property, so has_autoincrement should become false. But the current ALTER COLUMN code doesn't recompute has_autoincrement.

    Wait, let me check. When rename is false, the code does:

    btree.columns_mut()[*column_index] = new_column.clone();
    

    But it doesn't update has_autoincrement. The has_autoincrement is a table-level flag. So after ALTER COLUMN to text, the table still has has_autoincrement = true. This means:

    1. The sqlite_sequence table is left behind
    2. The table still thinks it has autoincrement

    So I need to:

    1. After ALTER COLUMN (non-rename), recompute has_autoincrement based on whether the PK column still has autoincrement. Actually, has_autoincrement is computed from whether the PK constraint has auto_increment: true. When we replace the column with a new definition that doesn't have PRIMARY KEY AUTOINCREMENT, the table no longer has autoincrement.

    Wait, but actually, looking at the alter.rs translation code, ALTER COLUMN doesn't allow changing PRIMARY KEY constraint:

    if definition.constraints.iter().any(|c| matches!(c.constraint, ast::ColumnConstraint::PrimaryKey { .. })) {
        return Err(LimboError::ParseError("PRIMARY KEY constraint cannot be altered".to_string(),));
    }
    

    So you can't add a PK constraint via ALTER COLUMN. But the original column had INTEGER PRIMARY KEY AUTOINCREMENT. When we alter it to TEXT, the new definition has no PK constraint. So the old PK is being dropped?

    Hmm, this is interesting. The issue example: ALTER TABLE t ALTER COLUMN a TO a2 TEXT;. The original column a is INTEGER PRIMARY KEY AUTOINCREMENT. The new definition is a2 TEXT with no constraints.

    So this ALTER COLUMN is effectively dropping the PRIMARY KEY and AUTOINCREMENT. But the validation says you can't ALTER a PK constraint. Let me re-read - the validation rejects if the NEW definition has a PK constraint. It doesn't check that the OLD column had a PK.

    Actually wait, let me reconsider. The issue says "ALTER TABLE t ALTER COLUMN a TO a2 TEXT;" works and produces CREATE TABLE t (a2 TEXT, b TEXT). So the PK and AUTOINCREMENT were dropped. The validation only checks that the new definition doesn't ADD a primary key constraint. It doesn't prevent removing an existing PK.

    So after this alter:

    • The column a2 is no longer a rowid alias (since it's TEXT not INTEGER PRIMARY KEY)
    • has_autoincrement should be false
    • primary_key_columns should be empty
    • The sqlite_sequence entry for table t should be removed
    • If no other tables use autoincrement, the sqlite_sequence table itself should be dropped

    Now, how does SQLite handle this? Let me think about what the proper behavior is. Actually, this is a Turso extension (ALTER COLUMN), so there's no direct SQLite reference. But the issue expectation is clear: when autoincrement is removed, the sqlite_sequence should be cleaned up.

    Let me look at how the existing code handles has_autoincrement recomputation. Let me check the op_alter_column more carefully to see if it recomputes has_autoincrement.

    Looking at the op_alter_column code, in the non-rename branch:

    btree.columns_mut()[*column_index] = new_column.clone();
    

    And then:

    // Maintain rowid-alias bit after change/rename (INTEGER PRIMARY KEY)
    if !*rename {
        // recompute alias from `new_column`
        btree.columns_mut()[*column_index].set_rowid_alias(new_column.is_rowid_alias());
    }
    

    But has_autoincrement is NOT recomputed. And primary_key_columns is not updated either (though that's the rename branch only).

    Hmm wait, actually this is getting complex. Let me focus on the specific issue: the sqlite_sequence table is left behind. The fix should:

    1. Recompute has_autoincrement on the table after ALTER COLUMN.
    2. If the table no longer has autoincrement, remove its row from sqlite_sequence.
    3. If no tables in the schema have autoincrement, drop the sqlite_sequence table.

    But actually, this is a complex change involving both translation (emit insns to modify sqlite_sequence) and execution (update in-memory schema). Let me think about the cleanest approach.

    Let me look at how the translation phase emits the rename of sqlite_sequence entry (emit_rename_sqlite_sequence_entry) and how DROP COLUMN handles autoincrement. Let me check if DROP COLUMN has similar handling.

  8. GLM Assistant

    DROP COLUMN doesn't handle autoincrement cleanup either, but it errors out for PK columns. So DROP COLUMN won't drop an autoincrement column because PK columns can't be dropped. So DROP COLUMN is fine.

    Now, the issue is specifically about ALTER COLUMN. Let me focus on this.

    The flow for ALTER COLUMN (non-rename) in translation (alter.rs):

    1. Replaces the column with the new definition.
    2. The validation prevents adding a PK constraint, but allows removing one.
    3. The rewritten_table is created by cloning btree and replacing the column.
    4. The sqlite_schema is updated via the AlterColumn function which rewrites the CREATE TABLE SQL.
    5. If physical layout changes, the table rows are rewritten.
    6. The SetCookie and AlterColumn insn are emitted.

    Now, the issue: when the autoincrement column is changed to text:

    • The new column doesn't have a PK constraint, so has_autoincrement should become false.
    • The sqlite_sequence entry for the table should be removed.
    • If no other tables use autoincrement, the sqlite_sequence table should be dropped.

    Let me check what SQLite does. Actually, in SQLite, the standard ALTER TABLE doesn't support ALTER COLUMN. This is a Turso extension. So there's no direct reference. But the expectation is clear: clean up sqlite_sequence.

    Let me look at how the schema change function (AlterColumn function in execute.rs around line 8462) handles the CREATE TABLE SQL rewrite. And let me check if has_autoincrement is recomputed anywhere.

    Actually, let me look at the AlterColumn function at line 8462.

  9. GLM Assistant

    Let me first reproduce the issue. Let me build the CLI and run the SQL.

  10. GLM Assistant

    Confirmed the issue. Let me check how DROP TABLE handles sqlite_sequence cleanup in Turso, as that's the reference behavior.

  11. GLM Assistant

    Let me test what Turso's DROP TABLE does with sqlite_sequence, for reference behavior.

  12. GLM Assistant

    Let me check the row too for DROP TABLE, and look at how the schema stores sqlite_sequence.

  13. GLM Assistant

    Now let me understand the full picture. The approach:

    In the ALTER COLUMN translation (alter.rs), I need to:

    1. Determine if the table previously had autoincrement.
    2. Determine if the table will still have autoincrement after the alter.
    3. If it had autoincrement but no longer will: a. Emit insns to delete the row from sqlite_sequence for this table. b. If no other tables in the schema have autoincrement, drop the sqlite_sequence table (delete its row from sqlite_schema AND free its btree).

    In op_alter_column (execute.rs):

    1. Update has_autoincrement on the in-memory table.

    Let me think about how to determine if the table "will still have autoincrement after the alter".

    The has_autoincrement is computed from PK constraints with auto_increment. When we ALTER COLUMN the autoincrement column to a non-PK definition, the inline PK is gone. But there could be a table-level PK constraint too. Let me check how has_autoincrement is computed in schema.rs to understand.

    Actually, let me re-examine. The translation in alter.rs computes the rewritten_table by cloning the btree and replacing the column. But has_autoincrement is a field on BTreeTable that is NOT recomputed when the column is replaced. So the rewritten_table still has has_autoincrement = true even though the column no longer has a PK.

    Hmm. So the in-memory schema after op_alter_column would still have has_autoincrement = true unless I update it.

    Let me check: how does has_autoincrement get recomputed? Is there a method? Let me look at the BTreeTable struct and related methods.

  14. GLM Assistant

    Now let me understand the column's primary_key and how has_autoincrement relates to columns. Let me look at the Column struct.

  15. GLM Assistant

    Now let me understand the approach. I'll determine whether the table still has autoincrement after the alter.

    The simplest approach: After replacing the column with the new definition, recompute has_autoincrement. The has_autoincrement is true if any column has a PRIMARY KEY with auto_increment, OR any table-level PK constraint has auto_increment.

    Wait, but the column-level PK with auto_increment — how is that stored on the Column? Let me check the column-level PK handling (around line 3622 and 3668).

  16. GLM Assistant

    Now I have a good understanding. The has_autoincrement is set if the table-level PK constraint has auto_increment: true OR if the column-level PK constraint has auto_increment: true. Let me check the column-level handling to find where auto_increment is checked.

  17. GLM Assistant

    Let me look at to_sql() for BTreeTable to understand how AUTOINCREMENT is rendered.

  18. GLM Assistant

    Let me look at the full to_sql() to see how table-level constraints are rendered.

  19. GLM Assistant

    Let me check how table-level PK affects column's is_primary_key flag.

  20. GLM Assistant

    Let me check whether table-level PK with AUTOINCREMENT is even valid in Turso.

  21. GLM Assistant

    Let me look at the DROP TABLE translation to understand how sqlite_sequence table would be dropped.

  22. GLM Assistant

    Let me look at how Turso handles this in tests and check existing behavior expectations.

  23. GLM Assistant

    Let me look at how sqlite_sequence is added/removed from the in-memory schema.

  24. GLM Assistant

    There's existing code in DROP TABLE that deletes the row from sqlite_sequence. Let me verify what Turso's DROP TABLE actually does with the row.

  25. GLM Assistant

    Let me check if there are tests that rely on sqlite_sequence persisting after all autoincrement tables are dropped.

  26. GLM Assistant

    Let me look further in the autoincr.sqltest file for drop-related tests.

  27. GLM Assistant

    Very important finding! There's a cross-checked test autoinc-drop-last-table-empties-sequence that expects the sqlite_sequence table to REMAIN (but empty) after dropping the last autoincrement table. So the expected behavior is: remove the stale row, keep the table. Let me verify the current state of ALTER COLUMN tests.

  28. GLM Assistant

    No autoincrement tests in alter_column.sqltest. Let me look at the structure of the alter_column.sqltest file and the test around line 16.

  29. GLM Assistant

    Now I understand the test format. Let me re-read the AlterColumn translation branch end to find the insertion point.

  30. GLM Assistant

    Let me look at the section right before SetCookie/AlterColumn insn.

  31. GLM Assistant

    Let me look at where the sqlite_schema rewrite loop ends (the AlterColumn function loop).

  32. GLM Assistant

    Now I have a good understanding. Let me write the helper function and add the detection. First, let me view the full emit_rename_sqlite_sequence_entry helper to model the delete version.

  33. GLM Assistant

    Now let me add the helper function after emit_rename_sqlite_sequence_entry:

  34. GLM Assistant

    Let me check the cursor_loop signature to write the helper correctly.

  35. GLM Assistant

    Let me rewrite the helper file correctly, using the closure's program parameter:

  36. GLM Assistant

    Now let me insert the helper after emit_rename_sqlite_sequence_entry. Let me open the file at that location.

  37. GLM Assistant

    Now let me add the detection and the call. Let me find the rewritten_table computation (shifted by ~46 lines now).

  38. GLM Assistant

    Let me check what name is stored in sqlite_sequence for a table (to know whether to normalize).

  39. GLM Assistant

    Let me find how table_name_reg is set in init_autoincrement.

  40. GLM Assistant

    Let me check the call site of emit_rename_sqlite_sequence_entry to see how table_name is normalized.

  41. GLM Assistant

    Now let me add the detection after the rewritten_table block. Let me view and edit.

  42. GLM Assistant

    Now let me insert the detection after line 1755 (the };):

  43. GLM Assistant

    Now let me add the emit call before the SetCookie. Let me find the new location of SetCookie/AlterColumn insn.

  44. GLM Assistant

    Now the translation part is done. Now I need to update the in-memory has_autoincrement in op_alter_column (execute.rs). Let me find where the column replacement happens.

  45. GLM Assistant

    Let me look at the start of the with_database_schema_mut closure in op_alter_column.

  46. GLM Assistant

    I'll capture the old column's rowid-alias status before replacement, then update has_autoincrement. Let me edit.

  47. GLM Assistant

    Now let me add the has_autoincrement update right after the rowid-alias bit maintenance:

  48. GLM Assistant

    I made an error - the first if !*rename block lost its closing brace. Let me fix the structure.

  49. GLM Assistant

    There's a duplicate comment line. Let me remove the duplicate.

  50. GLM Assistant

    Now let me build to check for compilation errors.

  51. GLM Assistant

    Build succeeded. Now let me run the reproduction script.

  52. GLM Assistant

    The stale row is now gone, and the table remains (matching the cross-check test). Let me test edge cases.

  53. GLM Assistant

    All edge cases work. Now let me test the reopen scenario to verify the persisted schema is consistent.

  54. GLM Assistant

    Excellent! Reopen works perfectly. Now let me run the existing tests. First, let me check how to run the relevant tests.

  55. GLM Assistant

    Let me build the test runner and run the relevant sqltest files.

  56. GLM Assistant

    All alter_column tests pass. Now let me run the integration tests for alter table reopen and the broader alter table sqltest.

  57. GLM Assistant

    The test build has a pre-existing turso_ext version mismatch. Let me check how integration tests are normally run.

  58. GLM Assistant

    The integration tests are in the core_tester package. Let me run the alter-related integration tests there.

  59. GLM Assistant

    The alter_table_reopen tests pass. Now let me run the broader alter table sqltest and other alter-related integration tests.

  60. GLM Assistant

    Let me run all alter-related integration tests.

  61. GLM Assistant

    All alter tests pass. Let me run the cli backend tests for autoincr and alter_column to verify the bytecode path.

  62. GLM Assistant

    Let me verify the rename case for an autoincrement column still preserves autoincrement.

  63. GLM Assistant

    Rename of the autoincrement PK column preserves autoincrement. Let me verify table rename (which uses emit_rename_sqlite_sequence_entry) still works, to ensure I didn't break anything.

  64. GLM Assistant

    Let me test ALTER COLUMN on a plain INTEGER PRIMARY KEY (no autoincrement) to ensure no spurious cleanup.

  65. GLM Assistant

    Let me test altering the autoincrement column to INTEGER (no PK) - should also remove autoincrement.

  66. GLM Assistant

    I notice the existing rows show NULL for a2 after altering the rowid-alias PK to a non-PK column. Let me verify this is pre-existing behavior (not caused by my change) by stashing my changes.

  67. GLM Assistant

    Confirmed: the NULL behavior for existing rows is pre-existing (the rowid-alias-to-non-PK data migration is a separate issue). Before my change, the stale t|2 row remained. Let me restore my changes.

  68. GLM Assistant

    My changes are restored and build successfully. Let me run the full sqltests suite to ensure nothing is broken.

  69. GLM Assistant

    There are 8 failures. Let me see which tests failed.

  70. GLM Assistant

    The grep for FAIL didn't match because of color codes. Let me search for the failing test names differently. The summary said "8 failed". Let me look for lines with "failed" near the test name or look at the test names with the red color code. Let me strip colors.

  71. GLM Assistant

    The grep matched PASS lines with "fail" in names. Let me grep case-sensitively for "FAIL".

  72. GLM Assistant

    Let me redirect the output to a file and search for the failures.

  73. GLM Assistant

    The 8 failures are MISMATCH tests about hash-joins and q13, unrelated to my change. Let me confirm they're pre-existing by stashing and running them on the original code.

  74. GLM Assistant

    This is a bytecode MISMATCH for hash joins, unrelated to my change. Let me confirm it's pre-existing by stashing.

  75. GLM Assistant

    Confirmed: the hash-join test fails on the original code too (pre-existing, unrelated to my change). Let me restore my changes.

  76. GLM Assistant

    The test runner created .snap.new artifact files. Let me remove these test artifacts to keep the working tree clean.

  77. GLM Assistant

    Working tree is clean with only my two changed files. Let me run the turso-tests suite to ensure no regressions.

  78. GLM Assistant

    All turso-tests pass. Let me run the vacuum integration tests since they involve sqlite_sequence.

  79. GLM Assistant

    All vacuum tests pass. Let me check whether CDC is involved in the ALTER COLUMN path, since the DROP TABLE sqlite_sequence cleanup has CDC handling.

  80. GLM Assistant

    No CDC handling in alter.rs at all (consistent with the rename helper). Let me verify transactional rollback restores the sqlite_sequence row.

  81. GLM Assistant

    Transactional rollback works correctly. Let me run the stmt_journal integration tests since they involve autoincrement multi-write behavior.

  82. GLM Assistant

    All stmt_journal tests pass. Let me check the fuzz test names that might be affected by schema/alter changes.

  83. GLM Assistant

    All tests pass. Let me review my final changes.

  84. GLM Assistant

    The diff looks clean. Let me test the rare table-level PK AUTOINCREMENT edge case to understand its behavior.

  85. GLM Assistant

    Let me check the reopen behavior for the table-level PK case (to confirm it's a pre-existing issue, not caused by my change).

  86. GLM Assistant

    Let me confirm this table-level PK reopen failure is pre-existing (not caused by my change) by stashing.

  87. GLM Assistant

    Confirmed: the table-level PK reopen failure is pre-existing (occurs on original code too). My change improves it by removing the stale row. Let me rebuild with my changes and do a final verification.

  88. GLM Assistant

    The fix is confirmed. Let me clean up temporary test files and verify the final git status.

  89. GLM Assistant

    The working tree is clean. Let me verify there are no compiler warnings with a debug build of the core crate.

  90. GLM Assistant

    No warnings. The build is clean. Let me do a final review of the complete diff before submitting.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.