Skip to content

Improve JOIN execution, FK enforcement and table alias support - #52

Open
efremropelato wants to merge 3 commits into
crossdb-org:mainfrom
OSS-Mind:main
Open

Improve JOIN execution, FK enforcement and table alias support#52
efremropelato wants to merge 3 commits into
crossdb-org:mainfrom
OSS-Mind:main

Conversation

@efremropelato

@efremropelato efremropelato commented Jul 11, 2026

Copy link
Copy Markdown

Summary of Changes

1. Foreign Key — Critical Crash and Missing Enforcement

Files: src/core/xdb_fkey.c, src/core/xdb_fkey.h, src/core/xdb_crud.c

  • Crash fixed: xdb_create_fkey was writing through an xdb_filter_t* pointer that was never allocated (NULL), causing an immediate SIGSEGV on any CREATE TABLE ... FOREIGN KEY. Added real storage (fkey_filters[]) in the xdb_fkeym_t struct.
  • Collateral bug fixed: wrong shift (>>8 instead of >>3) in the field bitmap computation, which left memory uninitialized.
  • Enforcement implemented (previously absent, only dead code under #if 0):
    • Existence check on INSERT/UPDATE: rejects with XDB_E_CONSTRAINT if the FK value does not exist in the referenced table.
    • RESTRICT constraint on DELETE: rejects deletion of the parent if child rows exist (also required fixing the reverse index fkeyref_objm, which was registered on the wrong table).
  • Known limitation: RESTRICT only, not CASCADE/SET NULL (deferred to a later increment).

2. Multi-Table JOIN — From Completely Non-Functional to Working

Files: src/parser/xdb_parser_dml.c, src/core/xdb_crud.c

JOIN was dead code (#if 0) that was never completed. Distinct bugs fixed:

  • ON clause parser was disabled → restored with multi-column support.
  • Recursive query never executed in xdb_sql_join, wrong row indexing (pRowPtrs[level] instead of level-1).
  • Wrong combined metadata layout (columns copied from the wrong offset → crash on output).
  • NULL bitmap never merged across joined tables (NOT NULL columns appeared as NULL).
  • Design choice: only "chained joins" supported (each table joins against the previous one); a non-chained JOIN is rejected with a clean error instead of silently producing wrong results.
  • Known limitation: JOIN with VARCHAR/VARBINARY/JSON columns not supported (clean error, not a crash).

3. Table Alias and Self-Join

File: src/parser/xdb_parser_dml.c

  • Support for FROM tbl AS alias and JOIN tbl AS alias (the as_name field already existed in xdb_reftbl_t but was never populated).
  • xdb_stmt_find_table fixed to resolve by alias — necessary to disambiguate a self-join (same table twice).
  • Once the alias is assigned, the real table name is no longer resolvable (standard SQL semantics).

4. WHERE/ORDER BY Consistency on Joined Tables

Files: src/parser/xdb_parser_dml.c, src/core/xdb_crud.c, src/parser/xdb_stmt.h

  • Crash fixed: a WHERE referencing only a joined table (not the base one) left the base table's index state inconsistent → NULL dereference.
  • WHERE filter on a joined table never applied: conditions were parsed but ignored at execution time (xdb_sql_join only considered ON conditions).
  • ORDER BY did not support qualified references (tbl.col) — it failed even on simple JOINs.
  • Row corruption bug in ORDER BY/LIMIT: both functions assumed one row = one pointer, whereas a JOIN result is a batch of reftbl_count consecutive pointers; sorting/truncation shuffled single pointers instead of whole batches, producing rows with columns mismatched across different tables. Fixed with the correct stride and a new order_reftbl_id field (constraint: all ORDER BY columns must belong to the same joined table).
  • Also fixed a relative indexing bug (pRefTbl[i] instead of pStmt->ref_tbl[i]) that broke multi-table resolution after the first table switch.

5. Tests Added

Files: test/xdb_smoke_ddl.c, test/xdb_smoke_dml.c

18 new test cases (139 → 147 counting the double fixture instances), including: FK crash, FK enforcement (insert-check + RESTRICT), 2/3-table join, join with index, non-chained join rejected, combined join + WHERE + ORDER BY + LIMIT, ambiguous column, table alias, self-join. Full suite verified with no regressions under ASan.

@efremropelato efremropelato changed the title Improve JOIN, SELF-JOIN & ALIAS Improve JOIN execution, FK enforcement and table alias support Jul 11, 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.

1 participant