Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Fixes the issue where CREATE VIEW with subqueries fails with "Table does not exist" error. The problem occurred because subqueries were being compiled and validated immediately during CREATE VIEW execution, before the view context was properly established.

Problem

When creating a view with a subquery in AlaSQL, the following error would occur:

db.exec('create view new_view as select (select count(id) from sub_table) as amount from from_table')
// Error: Table does not exist: sub_table

This happened because subqueries were precompiled during the CREATE VIEW statement execution, triggering table validation before the view was fully set up.

Solution

Implemented lazy compilation for views with subqueries:

  1. Deferred compilation: During CREATE VIEW, the select statement is stored uncompiled instead of being compiled immediately
  2. Query scoping: Subqueries are moved from the statement level to the select object for proper context
  3. Lazy compilation: The view's select statement (including all subqueries) is compiled when the view is first accessed/queried

Changes Made

  • src/41exists.js: Modified alasql.precompile to transfer queries to the select object for CREATE VIEW statements instead of precompiling them
  • src/60createtable.js: Changed view creation to store the uncompiled select statement (viewSelect) and database ID (viewDatabaseid)
  • src/420from.js: Added lazy compilation logic that compiles the view's select on first access
  • test/test950.js: Added comprehensive test suite covering view creation with subqueries, querying from views, and data change reflection

Testing

  • ✅ New test suite (test950) with 6 tests - all passing
  • ✅ Full test suite: 2301 tests passing, 0 failures, 248 pending
  • ✅ No regressions introduced
  • ✅ Views with subqueries now work correctly and reflect data changes dynamically

The changes are minimal and surgical, maintaining backward compatibility while fixing the reported issue.

Original prompt

This section details on the original issue you should resolve

<issue_title>Creating a view with subquery does not work</issue_title>
<issue_description>The following query list throws an error in alasql, but works in sqlite:

const alasql = require('alasql')
var db = new alasql.Database()
db.exec('create table sub_table(id int)')
db.exec('create table from_table(id int)')
db.exec('create view new_view as select (select count(id) from sub_table) as amount from from_table')

// Error: Table does not exist: sub_table

Basically one should be able to create a view by using a valid query and the used query is valid:

const alasql = require('alasql')
var db = new alasql.Database()
db.exec('create table sub_table(id int)')
db.exec('create table from_table(id int)')
var result = db.exec('select (select count(id) from sub_table) as amount from from_table')
console.log(result)</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@mathiasrw
Copy link
Member

@copilot COntinue

Copy link
Contributor Author

Copilot AI commented Dec 22, 2025

@copilot COntinue

Completed! The fix is working and ready for review.

Changes made:

  • Fixed CREATE VIEW with subqueries by implementing lazy compilation
  • All tests passing: test950 (6 tests) + full suite (2301 tests)
  • No regressions introduced

Key implementation:

  1. Deferred subquery compilation until view is first used (src/41exists.js)
  2. Store uncompiled select in view object (src/60createtable.js)
  3. Compile on first access (src/420from.js)

The issue where CREATE VIEW with subqueries would fail with "Table does not exist" error is now resolved. Views with subqueries work correctly and reflect data changes dynamically.

Commit: 838c52b

Copilot AI changed the title [WIP] Fix error when creating view with subquery in alasql Fix error when creating view with subquery in alasql Dec 22, 2025
@mathiasrw mathiasrw marked this pull request as ready for review December 22, 2025 15:31
@mathiasrw mathiasrw merged commit 86e8e46 into develop Dec 22, 2025
10 checks passed
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.

Creating a view with subquery does not work

2 participants