Skip to content

[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
mjf:space-lua-fix-liq-ordering
Mar 3, 2026
Merged

[LIQ] Fix group by: order by with select aliases, variable binding in having and nil column ordering#1858
zefhemel merged 11 commits intosilverbulletmd:mainfrom
mjf:space-lua-fix-liq-ordering

Conversation

@mjf
Copy link
Contributor

@mjf mjf commented Feb 26, 2026

  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.

mjf added 2 commits February 26, 2026 15:32
…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>
* 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>
@mjf mjf marked this pull request as draft February 27, 2026 11:15
mjf added 4 commits February 27, 2026 15:30
- 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>
mjf added 3 commits March 2, 2026 14:07
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>
@mjf mjf marked this pull request as ready for review March 3, 2026 08:30
@zefhemel zefhemel merged commit 82f10fa into silverbulletmd:main Mar 3, 2026
2 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.

2 participants