fix(driver): validate FROM clause in COUNT query generation #251
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes COUNT query generation to properly validate FROM clause existence before attempting to create COUNT(*) queries. Addresses upstream bug report where
select_with_totalraised confusing error for SQL with only ORDER BY clause.The Problem
select_with_total()raised "Cannot create COUNT query from empty SQL expression" when SELECT statement lacked FROM clause (e.g.,"SELECT * ORDER BY id"). The root cause was attempting to useexpr.args.get("from")without validating it wasn'tNone, which would passNoneto sqlglot's.from_()method.The Solution
Added explicit validation using sqlglot AST inspection to check
expr.args.get("from")exists before attempting COUNT generation. Provides clear, actionable error message: "SELECT statement missing FROM clause. COUNT queries require a FROM clause to determine which table to count rows from."Key Changes
if not expr.args.get("from")before building COUNT queryTest Coverage
Example Error Messages
Before (confusing):
After (clear):
Testing