Skip to content

fix(sync-rules): use real table name for output in explicit JOIN queries - #566

Closed
jmalmo wants to merge 4 commits into
powersync-ja:mainfrom
jmalmo:fix/join-alias-output-table-name
Closed

jmalmo wants to merge 4 commits into
powersync-ja:mainfrom
jmalmo:fix/join-alias-output-table-name

Conversation

@jmalmo

@jmalmo jmalmo commented Mar 14, 2026

Copy link
Copy Markdown

Summary

Fixes #565 — aliasing the synced table in an edition 3 INNER JOIN query causes 0 rows to sync.

RowEvaluator.outputName returns the SQL alias (e.g. "cm") as the output table name. WAL matching works fine (uses the real table name), but output rows are labeled with the alias. The client expects chat_messages, gets cm, so nothing syncs.

Approach

The alias-as-rename behavior is intentional for single-table queries (e.g. SELECT * FROM new_table AS old_name). But in explicit JOINs, table aliases are for SQL column disambiguation, not output renaming. WAL events always use real table names, so alias-as-rename in any JOIN involving a physical table would cause silent data loss.

The fix tracks hasExplicitJoins in StreamQueryParser, set when the AST FROM list contains a physical table entry (from.type == 'table') with a .join clause. When true, RowEvaluator.outputName returns the real table name instead of the alias.

Alias-as-rename still works for:

  • Single-table queries: SELECT * FROM users u → table: 'u'
  • Subquery-backed streams (lowered to joins internally): SELECT * FROM comments c WHERE id IN (SELECT ...) → table: 'c'
  • Source table with TVF on right: FROM stores s INNER JOIN json_each(s.tags) → table: 's'
  • Aliased wildcards: always use the alias

Alias dropped (real table name used) for:

  • Any query where a physical table entry appears in an explicit INNER JOIN:
    • SELECT cm.* FROM chat_messages cm INNER JOIN other ON ... → table: 'chat_messages'
    • SELECT u.* FROM json_each(...) j INNER JOIN users u ON ... → table: 'users'

Changed files

File Change
packages/sync-rules/src/compiler/parser.ts Track hasExplicitJoins from AST FROM entries
packages/sync-rules/src/compiler/rows.ts Add hasJoinedTables to RowEvaluator; outputName returns real table name when set; wildcard aliases always preserved
packages/sync-rules/src/compiler/querier_graph.ts Pass hasExplicitJoins when constructing RowEvaluator
evaluator.test.ts 4 new tests covering JOIN alias scenarios
2 snapshot files Updated for JOIN queries

Test plan

  • 768 tests pass, 4 skipped
  • JOIN with aliased source table → real table name
  • JOIN with only joined table aliased → real table name
  • Subquery with alias → alias-as-rename preserved
  • TVF-left, source-joined-right → real table name
  • Single-table alias → alias-as-rename preserved
  • TVF-right, source-left → alias-as-rename preserved
  • Schema generation tests pass
  • Prettier formatting passes

Note: 17 engine.test.ts (SQLite engine) failures are pre-existing on main and unrelated to this change — they appear to be a Node version issue (experimental SQLite API).

Filed #565 and this PR together since the bug, root cause, and fix were all clear. Happy to adjust the approach if needed.

jmalmo added 4 commits March 14, 2026 23:38
In edition 3 compiled streams, aliasing the synced table in a JOIN
query (e.g. `SELECT cm.* FROM chat_messages cm INNER JOIN ...`) caused
RowEvaluator.outputName to return the alias ("cm") as the output table
name. The client expects rows in "chat_messages", so all rows were
silently lost.

The alias-as-rename behavior is intentional for single-table queries
(e.g. `SELECT * FROM new_table AS old_name`), but in JOINs, aliases
serve SQL column disambiguation, not output renaming.

Add `hasJoinedTables` flag to RowEvaluator. When the query joins other
physical tables, outputName returns the real table name. Table-valued
function JOINs (e.g. json_each) are not affected — alias-as-rename is
preserved for those.
- Track hasExplicitJoins in StreamQueryParser based on whether the AST
  FROM list contains physical table entries with a .join clause.
- Subqueries lowered into joins by the compiler don't set this flag,
  preserving alias-as-rename for subquery-backed streams.
- Table-valued function JOINs (e.g. json_each) also don't set the flag,
  since only FROM entries with type 'table' count.
- Fix wildcard edge case: aliased wildcards now always use the alias
  regardless of hasJoinedTables.
- Add test verifying subquery alias-as-rename is preserved.
Covers the case where the source table is on the right side of a
table-valued function join (e.g. `FROM json_each(...) j INNER JOIN
users u ON ...`). The alias is correctly dropped since WAL events
use the real table name.
@CLAassistant

CLAassistant commented Mar 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@changeset-bot

changeset-bot Bot commented Mar 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b9102e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@powersync/service-sync-rules Patch
@powersync/service-jpgwire Patch
@powersync/service-core-tests Patch
@powersync/service-core Patch
@powersync/lib-services-framework Patch
@powersync/service-module-mongodb-storage Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mssql Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres-storage Patch
@powersync/service-module-postgres Patch
@powersync/lib-service-postgres Patch
@powersync/service-module-core Patch
@powersync/service-image Patch
test-client Patch
@powersync/service-rsocket-router Patch
@powersync/lib-service-mongodb Patch
@powersync/service-schema Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@simolus3

Copy link
Copy Markdown
Contributor

As discussed in #565, this is working as intended. So I'll close this, but we can keep the issue open until we've improved docs / a potential warning around this.

@simolus3 simolus3 closed this Mar 16, 2026
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.

Edition 3: aliasing synced table in JOIN query causes 0 rows to sync

3 participants