Skip to content

JOINSTAR Feature Incompatibility with Qualified Table Names #2197

@mathiasrw

Description

@mathiasrw

test421.test.js: - JOINSTAR Feature Incompatibility

JOINSTAR modes 'json' and 'underscore' fail with qualified names.
Expected: {one: {a: 1}, two: {a: 10}} (json mode)
Actual: {a: 10} (falls back to overwrite mode)
Root cause: Feature can't extract base table name from "test421.one"
Possible fix: Update JOINSTAR parser logic in the jison with a few changes as possible.


In AlaSQL, the JOINSTAR feature provides different ways to handle SELECT * queries with multiple tables. However, when using qualified table names (with database prefixes like db.table), the json and underscore modes don't work as expected.*

example

// Create test database and tables
alasql('CREATE DATABASE test_db');
alasql('CREATE TABLE test_db.customers (id INT, name STRING)');
alasql('CREATE TABLE test_db.orders (id INT, customer_id INT, amount DECIMAL)');

// Insert sample data
alasql('INSERT INTO test_db.customers VALUES (1, "Alice"), (2, "Bob")');
alasql('INSERT INTO test_db.orders VALUES (101, 1, 99.99), (102, 2, 149.99)');

// Expected behavior with 'json' mode:
// Results should have nested objects by table name
alasql.options.joinstar = 'json';

// EXPECTATION:
// [
//   { customers: {id: 1, name: "Alice"}, orders: {id: 101, customer_id: 1, amount: 99.99} },
//   { customers: {id: 2, name: "Bob"}, orders: {id: 102, customer_id: 2, amount: 149.99} }
// ]

// ACTUAL RESULT with qualified names:
// [
//   { id: 101, customer_id: 1, amount: 99.99 },
//   { id: 102, customer_id: 2, amount: 149.99 }
// ]
const results = alasql('SELECT * FROM test_db.customers JOIN test_db.orders ON customers.id = orders.customer_id');
console.log(results);

// Similarly with 'underscore' mode, columns should be prefixed with table names
alasql.options.joinstar = 'underscore';

// EXPECTATION:
// [
//   { customers_id: 1, customers_name: "Alice", orders_id: 101, orders_customer_id: 1, orders_amount: 99.99 },
//   { customers_id: 2, customers_name: "Bob", orders_id: 102, orders_customer_id: 2, orders_amount: 149.99 }
// ]

// ACTUAL RESULT with qualified names:
// [
//   { id: 101, customer_id: 1, amount: 99.99 },
//   { id: 102, customer_id: 2, amount: 149.99 }
// ]
const results2 = alasql('SELECT * FROM test_db.customers JOIN test_db.orders ON customers.id = orders.customer_id');
console.log(results2);

Root Cause

When table names are qualified (e.g., test_db.customers), the AlaSQL parser correctly identifies the database and table components, but this information is not properly propagated through the query execution pipeline. As a result, in JOINSTAR modes, the system cannot correctly attribute columns to their original tables for proper nesting or prefixing.

The issue stems from how table aliases are handled in the
compileFrom
and
compileSelectStar
functions, where qualified table names lose their structure when being processed for output formatting.

Metadata

Metadata

Assignees

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