Repository navigation
fix(#5799): sum() and avg() reject non-numeric input instead of silently ignoring it - #5908
Conversation
…tly ignoring it Cypher's sum() (shared with SQL's sum() via SQLFunctionSum) and avg() (CypherAvgFunction) only advanced their accumulator when the aggregated value was a Number, silently skipping anything else - a STRING, a BOOLEAN, ... - the same way they skip null. That made an all-invalid input indistinguishable from an all-null one (sum(['1','2']) == 0, same as sum([null,null])) and let a mixed input report a plausible but incomplete total (sum([true, 1]) == 1). Both now throw a client-facing type error instead: SQLFunctionSum throws IllegalArgumentException (matching the existing SQL-function convention, e.g. SQLFunctionAbsoluteValue), and CypherAvgFunction uses CypherFunctionHelper.requireNumberArgument(), the same helper the numeric math family (abs(), sqrt(), ...) already uses since #5484 for this exact class of defect. Both map to HTTP 400 at AbstractServerHttpHandler. Null handling and empty-input identities (sum() -> 0, avg() -> null) are unchanged.
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
🟢 Coverage 100.00% diff coverage · -6.83% coverage variation
Metric Results Coverage variation ✅ -6.83% coverage variation Diff coverage ✅ 100.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (6bbb0e7) 152707 117904 77.21% Head commit (4f182e6) 185054 (+32347) 130239 (+12335) 70.38% (-6.83%) Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#5908) 10 10 100.00% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
ReviewReviewed the diff ( 1.
Since 2. Behavior change scope for plain SQL Because Nits
Test coverage Good coverage for the Cypher-facing behavior described in the PR. There's no test exercising the plain-SQL Nice work tracing this back to the shared |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5908 +/- ##
==========================================
- Coverage 68.39% 68.01% -0.39%
==========================================
Files 1794 1794
Lines 152707 152711 +4
Branches 32366 32365 -1
==========================================
- Hits 104450 103862 -588
- Misses 34843 35469 +626
+ Partials 13414 13380 -34 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Brings the fork up to 26.9.1-SNAPSHOT, which carries the August fixes we filed: ArcadeData#5711 (HTTP row truncation), ArcadeData#5745/ArcadeData#5788 (GAV), ArcadeData#5746 (Cypher through GAVFusedChain), ArcadeData#5747 (close-time HNSW rebuild, our PR ArcadeData#5787), ArcadeData#5796 (Redis port), ArcadeData#5824 (@Rid IN replanned as FETCH FROM RIDs). All verified against the shipped jars before this merge. Conflicts resolved: - 15 .github files kept deleted. Upstream CI the fork does not run; we carry only the five Python workflows. - test-python-examples.yml took upstream's two dependabot SHA bumps (setup-java v5.6.0 -> v5.7.0, cache v4.3.0 -> v6.1.0), staying aligned with the SHA-pinning their review requires. - test_server_wire_protocols.py kept ours. Upstream carries the version from PR ArcadeData#5807, which still has the redis xfail; ArcadeData#5796 is fixed and the marker was retired in a5d82c0. Behaviour changes to watch, none of which renamed or removed a JPype-facing API: ArcadeData#5969 now validates SQL function and method arity, ArcadeData#5908 makes sum/avg reject non-numeric input, ArcadeData#5983 bounds regex backtracking, and ArcadeData#5786 caps vector.neighbors groupBy output per index. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0173qFfapQBiGxaV7apSZ4X8
What does this PR do?
sum()andavg()in Cypher now raise a client-facing type error (HTTP 400) instead ofsilently ignoring non-numeric input.
SQLFunctionSum(shared by SQL'ssum()and Cypher'ssum(), reached viaSQLFunctionBridge): a non-numeric, non-null value used to be silently skipped in thesingle-argument aggregate path, and blindly cast (throwing an undecorated
ClassCastException) in the multi-argument per-row and list-element paths. All three nowvalidate the value and throw
IllegalArgumentExceptionwith a clear message, matching theexisting SQL-function convention (e.g.
SQLFunctionAbsoluteValue).CypherAvgFunction(Cypher-onlyavg()): now usesCypherFunctionHelper.requireNumberArgument(), the same helper the numeric math family(
abs(),sqrt(), ...) already uses since [OpenCypher] abs() returns HTTP 500 for invalid argument instead of client-facing error #5484 for this exact class of defect, throwingCommandSemanticException.Null handling and empty-input identities are unchanged:
sum()over nulls/empty still returns0,avg()over nulls/empty still returnsnull.Motivation
Silently ignoring out-of-domain values let
sum()/avg()return a plausible but wrong resultwith no error at all:
UNWIND ['1', '2'] AS x RETURN sum(x)returned0, indistinguishable from summing[null, null].UNWIND [true, 1] AS x RETURN sum(x)returned1, hiding the invalid boolean.UNWIND ['a', 'b'] AS x RETURN avg(x)returnednull, indistinguishable from averaging[null, null].Neo4j reports a type error for all of these. This is the same class of defect fixed for the
numeric math family (
abs(),sqrt(), ...) in #5484.Why two different exception types
SQLFunctionSumis genuinely shared with plain SQL (SELECT sum(...) FROM ...), so it followsthe existing SQL-function convention (
IllegalArgumentException) rather than pulling in theCypher-only
CommandSemanticException/CypherFunctionHelper.CypherAvgFunctionhas no SQLcounterpart reachable from Cypher, so it follows the Cypher convention directly. Both map to
HTTP 400 at
AbstractServerHttpHandler, so the externally observable behavior is the same.Why
sum()didn't get its own Cypher-specific executorA tempting alternative was giving Cypher its own
CypherSumFunction(mirroringCypherAvgFunction). This was rejected:CypherFunctionArityRegistryTestpinssuminNARROWER_IN_CYPHERspecifically because the sharedSQLFunctionSumexecutor is variadic(
sum(a,b,c)) while the Cypher parser only allows the single-argument aggregation, and thattest asserts the pin still bites. A dedicated single-arg-only Cypher executor would make the
parser and executor arities match exactly, breaking that existing regression test for an
unrelated reason. Fixing
SQLFunctionSumin place keeps its arity untouched.Related issues
Closes #5799
Same class of defect as #5484.
Additional Notes
Verification performed (see the tracking doc at
docs/5799-cypher-sum-avg-silently-ignore-non-numeric.mdfor full detail):unmodified code.
OpenCypherAggregatingFunctionsComprehensiveTest+SQLFunctionSumAverageMultiArgTest: pass.CypherFunctionArityRegistryTest+CypherNumericFunctionArgumentIssue5484Test+CypherFunctionFactoryTest+CypherFunctionFactoryExtendedTest: pass (confirms thesumarity pin still holds and the [OpenCypher] abs() returns HTTP 500 for invalid argument instead of client-facing error #5484 numeric-family tests are unaffected).
com.arcadedb.query.opencypher.**+com.arcadedb.function.**packages: 4415 tests, 0failures, 0 errors.
com.arcadedb.query.sql.**package: 404 tests, 0 failures, 0 errors.sum(/avg(usages withstring literals across wire-protocol test modules (postgresw, mongodbw, graphql, bolt): none
found outside
engine/.Test plan
UNWIND ['1', '2'] AS x RETURN sum(x)raises a client error instead of returning0UNWIND [true, 1] AS x RETURN sum(x)raises a client error instead of returning1UNWIND ['a', 'b'] AS x RETURN avg(x)raises a client error instead of returningnullUNWIND [1, null] AS x RETURN sum(x)still returns1(nulls still skipped)UNWIND [] AS x RETURN sum(x)still returns0(empty-input identity unchanged)UNWIND [] AS x RETURN avg(x)still returnsnull(empty-input identity unchanged)mvn -pl engine -am test -Dtest=OpenCypherAggregatingFunctionsComprehensiveTest,SQLFunctionSumAverageMultiArgTestpassesChecklist
mvn clean packagecommand