[LIQ] Fix group by: order by with select aliases, variable binding in having and nil column ordering#1858
Merged
zefhemel merged 11 commits intosilverbulletmd:mainfrom Mar 3, 2026
Conversation
…ing in `having` and `nil` column ordering 1. Fix `order by` inability to access `select` output aliases and aggregate functions after `group by`. 2. Introduce `LUA_SQL_NULL` sentinel to preserve column ordering when `select` expressions evaluate to `nil`. Note: In Lua, setting a table key to `nil` deletes it so rows with `nil` values lose those keys and produced inconsistent insertion order across result tables. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
zefhemel
requested changes
Feb 27, 2026
* Fix `LUA_SQL_NULL` leaking into `order by` comparisons
Note: SQL `NULLS LAST` is now default behaviour!
* Fix `objectVariable` unset for empty groups
* Fix non-grouped `having` used wrong environment builder
Note: Queries similar to the following one now works correctly:
```sql
${query[[
from
tags.page
having
name == 'index'
]]}
```
* Fix `generateKey` did not handle `LUA_SQL_NULL`
* Refactor/deduplicate duplicated `order by` comparator
* Add `isSqlNull` helper
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
- Fix `group by` key building: populate array indices from `keyParts` instead of only from `keyRecord`, which was empty for some expressions (e.g. `tags[1]`). - Fix collection handling: treat record-like `LuaTables` (no array part) as items instead of extracting zero items. - Update test suite. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Check for already-queryable objects (e.g. `DataStoreQueryCollection`) before `LuaTable` extraction or `luaValueToJS` conversion, preventing them from being destroyed by the collection normalization pipeline. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Extend the `order by` clause with optional `nulls first` and `nulls last` modifiers following standard SQL semantics. Each sort key can independently specify where `nil` values should appear in the result. By default, ascending sorts place nulls last and descending sorts place nulls first. An explicit nulls first or nulls last overrides this default. The `asc` keyword is also now accepted as an explicit alternative to the default ascending direction. Documentation and tests were updated accordingly. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
The new null-aware ordering in `orderByCompare` changed the default placement of undefined values for descending sorts to `nulls first` (SQL standard). This caused Space Lua scripts without a `-- priority:` comment to load before the `Std` library scripts and breaking the boot sequence for user scripts ran before `schema`, `config`, `widget` and other core definitions were available. Add explicit `nulls: "last"` to the reload query so scripts without priority always load last, matching the documented behavior. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Implements `filter(where <cond>)` for aggregate functions in LIQ queries which allows per-row conditional filtering within grouped aggregates. Also extends test suite and documentation. Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix
order byinability to accessselectoutput aliases and aggregate functions aftergroup by.Introduce
LUA_SQL_NULLsentinel to preserve column ordering whenselectexpressions evaluate tonil.Note: In Lua, setting a table key to
nildeletes it so rows withnilvalues lose those keys and produced inconsistent insertion order across result tables.