Skip to content

Columns with dots in names dropped during JOIN star expansion #17

Description

@philcunliffe

Bug

Column names containing periods (e.g. Sp. Atk, Sp. Def) are silently dropped from results when using qualified star expansion (p.*) or unqualified star (*) in JOIN queries.

Reproduction

const { collect, executeSql } = require('squirreling')

const pokemon = [
  { '#': 1, Name: 'Bulbasaur', 'Sp. Atk': 65, 'Sp. Def': 65 },
]
const rowgroups = [{ numbers: 1 }]

// Qualified star — drops Sp. Atk and Sp. Def
const r1 = await collect(executeSql({
  tables: { pokemon, rowgroups },
  query: 'SELECT p.*, r.numbers FROM pokemon p JOIN rowgroups r ON p."#" = r.numbers',
}))
console.log(Object.keys(r1[0]))
// => ['#', 'Name', 'numbers']  — missing 'Sp. Atk' and 'Sp. Def'

// Unqualified star — mangles names
const r2 = await collect(executeSql({
  tables: { pokemon, rowgroups },
  query: 'SELECT * FROM pokemon p JOIN rowgroups r ON p."#" = r.numbers',
}))
console.log(Object.keys(r2[0]))
// => ['#', 'Name', ' Atk', ' Def', 'numbers']  — mangled to ' Atk' and ' Def'

// Explicit columns work fine
const r3 = await collect(executeSql({
  tables: { pokemon, rowgroups },
  query: 'SELECT p."#", p."Name", p."Sp. Atk", p."Sp. Def", r.numbers FROM pokemon p JOIN rowgroups r ON p."#" = r.numbers',
}))
console.log(Object.keys(r3[0]))
// => ['#', 'Name', 'Sp. Atk', 'Sp. Def', 'numbers']  — correct

Cause

mergeRows and prefixColumns in src/execute/join.js use key.includes('.') to detect "already-prefixed" columns (from previous joins). Column names like Sp. Atk contain a literal dot and get misidentified as already-prefixed, so the table.column alias is never created.

Then in executeProject (src/execute/execute.js), qualified star expansion (p.*) checks key.startsWith(prefix) which never matches these columns since they were never prefixed.

Workaround

Use explicit column names instead of p.* when columns contain dots.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions