Repository navigation
fix(crud): emit count() and math:: aggregates SurrealDB can parse - #81
Merged
Merged
Conversation
count(), sum(), avg(), min() and max() on ReadQL and AggregationQueryBuilder
rendered COUNT(*), SUM(f), AVG(f), MIN(f) and MAX(f). SurrealQL has none of
these: COUNT(*) is a parse error on the `*`, and SUM/AVG/MIN/MAX are not
function names, so every query that used one of these methods failed before
it ran.
A row count now renders count(), a field count count(field), and the others
math::sum(f), math::mean(f), math::min(f) and math::max(f). Aliases are
unchanged, and count('*') is still accepted and renders count().
The aggregate calls in the having(...) examples (JSDoc, docs/queries.md,
docs/examples/index.md, examples/) are corrected the same way. Unit tests pin
the new rendering, and a live test in integration_client executes the
aggregates against SurrealDB.
Merged
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.
The query builder's aggregate methods rendered SQL-style calls that SurrealDB rejects at parse time, so any query built with them failed before it ran.
Fixed
count(),sum(),avg(),min(), andmax()onReadQL(whatclient.query(...)returns) and onAggregationQueryBuilderrenderedCOUNT(*),SUM(f),AVG(f),MIN(f), andMAX(f). Against SurrealDB 3.3.0:COUNT(*)fails withParse error: Unexpected token `*`, expected an expression.SUM(f)/AVG(f)/MIN(f)/MAX(f)fail withParse error: Invalid function/constant path.They now render the SurrealQL forms:
count()/count('*')COUNT(*) as countcount() as countcount('f')COUNT(f) as countcount(f) as countsum('f')SUM(f) as sum_fmath::sum(f) as sum_favg('f')AVG(f) as avg_fmath::mean(f) as avg_fmin('f')MIN(f) as min_fmath::min(f) as min_fmax('f')MAX(f) as max_fmath::max(f) as max_fAliases are unchanged.
count('*')was the old default and is still accepted; it renderscount().count(field)counts the rows where that field is truthy.The aggregate calls inside the
having(...)examples (JSDoc inread.ts/having.ts,docs/queries.md,docs/examples/index.md,examples/basicCrud.ts,examples/userManagement.ts) are corrected the same way. The immutableQueryexpression helpers (count,mathSum,sum_,avg, ...) already rendered the SurrealQL forms and are unchanged. Historical changelog entries are left as they were.Tests
capabilities_extended.test.ts, the HAVING strings inquery.test.ts) are updated.read.test.tspin the full statementReadQLsends forcount(),count('*'),count(field), the fourmath::aggregates, and a nested field; a guard incapabilities_extended.test.tsfails if any SQL-style aggregate reappears.integration_client.test.ts(run by the integration workflow) executesgroupBy().count().sum().avg().min().max()andcount(field)and checks the computed values. Run against the unfixed code it fails with theUnexpected token `*`parse error; with the fix it passes.Verification
deno fmt --check,deno lint,deno check mod.ts,deno task check: clean.test.ymldoes: 232 passed (1901 steps), 0 failed. Baseline on main: 232 passed (1893 steps).deno test -A src/test/integration_client.test.tsagainst SurrealDB 3.3.0: 1 passed (27 steps), 0 failed.GROUP BY, pluscount()/math::sum()underGROUP ALL.Not in this change
Found while verifying, left for a separate change because each alters behaviour beyond the aggregate spelling:
HAVINGclause.having(...)appendsHAVING ..., which SurrealDB 3.3.0 rejects (Unexpected token `an identifier`, expected Eof) whatever the condition. The equivalent is a filter over a grouped subquery:SELECT * FROM (SELECT ... GROUP BY ...) WHERE count > 5.groupBy()emit noGROUPclause, so they run per row:count()returns one{ count: 1 }per record andmath::sum(f)errors on a scalar.GROUP ALLis the whole-table form.id, and the default no-mapper path callsid.toString(), so an aggregate query without.map(...)fails withRecord mapping failed!. The new integration test passes a mapper for this reason.