Skip to content

Resolve unqualified outer columns in correlated subqueries to close #2644 - #3017

Open
mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2644-outer-unqualified-column
Open

mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2644-outer-unqualified-column

Conversation

@mateuscmtropical

Copy link
Copy Markdown
Contributor

Closes #2644

What was wrong

Inside a subquery, an unqualified column was always looked up in the subquery's own sources. So ak in SELECT b.id FROM b WHERE bk = ak read b.ak, which does not exist, and the subquery returned nothing. The correlation check from #2281 only looked at table-qualified columns, so it would not have marked such a subquery as correlated either.

What changed

  • New Column.isOuter(defcols, columnid). It is true when every source of the query has known columns and none of them has the name.
  • In that case Column.toJS reads the column from the nearest enclosing query row through the new alasql.utils.outerColumn. The scope of a subquery already holds the outer rows under their aliases.
  • The correlation check in compileQueries uses Column.isOuter too, so these subqueries are not cached.
  • A name that one of the subquery's sources has still resolves to that source. Sources without column definitions keep the old behavior.

Tests

test/test2644.js covers IN, NOT IN, EXISTS and a scalar subquery in the select list with an unqualified outer column, plus a name that both queries have. The results match SQLite. Five of the six cases fail without the fix. The other one is the qualified form, which already worked.

yarn test-only gives 2557 pass. The only failures are the 5 Test 418 cases that load files from the internet.

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.

Correlated subquery with an unqualified outer column returns no rows

2 participants