Skip to content

fix: keep first duplicate column name and align columnsNames with row keys - #285

Open
Gioee wants to merge 1 commit into
mainfrom
fix/duplicate-column-names
Open

Gioee wants to merge 1 commit into
mainfrom
fix/duplicate-column-names

Conversation

@Gioee

@Gioee Gioee commented Oct 19, 2026

Copy link
Copy Markdown
Member

Fixes #282

Problem

When a query returns several columns with the same name (e.g. SELECT * over a join), SQLiteCloudRowset renames them so each SQLiteCloudRow can hold them as object keys. Two bugs:

  1. The renaming was backwards. The loop renamed the current column whenever any other column had the same name, so the first id became id_0, the second id_0_1, and only the last kept id. row.id returned the value of the last id column instead of the first.
  2. columnsNames didn't match the row keys. The constructor renamed a local copy, but the getter rebuilt names from metadata.columns on every call. Code that read row[name] for each name in columnsNames silently got the last duplicate's value in every duplicate column.

Fix

  • Unique names are computed once in the constructor (getUniqueColumnsNames):
    • missing names become column_N (unchanged)
    • the first occurrence keeps its name; later duplicates get _1, _2, …
    • suffixes that already belong to another column are skipped (id, id, id_1 → id, id_2, id_1)
  • columnsNames returns those same names (as a copy), so columnsNames + row[name] is consistent with getData().
  • New originalColumnsNames getter returns the names exactly as the query returned them, duplicates included.
  • slice() / filter() rebuild from metadata, so they get the same names.

⚠️ Behaviour change

  • Rows with duplicate column names now use different keys: before { id_0: 7, id: 42 }, now { id: 7, id_1: 42 }.
  • columnsNames now returns deduplicated names (and column_N for empty names) instead of the raw metadata names. If you need the raw names, use originalColumnsNames.

Queries without duplicate or empty column names are not affected. This should go in the release notes.

Tests

test/rowset.test.ts:

  • offline unit tests: renaming order, columnsNames matching row keys, suffix collisions, missing names, columnsNames returning a copy, and slice/filter
  • integration test against chinook with a join that returns duplicate AlbumId/Title columns

Run locally: rowset, row, database, connection-tls, connection-ws suites all pass. tsc is clean.

… keys

Duplicate column names in a rowset (e.g. SELECT * over a join) were renamed
starting from the first occurrence, so row.id returned the last id column
instead of the first. columnsNames also returned the raw metadata names, which
did not match the keys used in each row.

- keep the first occurrence of a name and suffix later duplicates with _1, _2,
  skipping suffixes already used by another column
- columnsNames now returns the same unique names used as row keys
- add originalColumnsNames with the names exactly as returned by the query

Fixes #282
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.45%. Comparing base (bb3026e) to head (124f87c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #285      +/-   ##
==========================================
+ Coverage   85.23%   85.45%   +0.21%     
==========================================
  Files          11       11              
  Lines        1131     1141      +10     
  Branches      374      375       +1     
==========================================
+ Hits          964      975      +11     
+ Misses        167      154      -13     
- Partials        0       12      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Gioee
Gioee requested a review from danielebriggi October 22, 2026 13:59

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLiteCloudRowset renames duplicate column names backwards, and columnsNames doesn't match the row keys

2 participants