Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(core): add arithmetic functions ADD/SUBTRACT/MULTIPLY/DIVIDE/NEGATE - #193

Merged
brunomendola merged 1 commit into
queritylib:mainfrom
vage88sg1:feature/arithmetic-functions
Jul 11, 2026
Merged

brunomendola merged 1 commit into
queritylib:mainfrom
vage88sg1:feature/arithmetic-functions

Conversation

@vage88sg1

Copy link
Copy Markdown
Contributor

Summary

Implements #192: row-level arithmetic as prefix functions, consistent with the existing ABS/SQRT/MOD design. No grammar precedence machinery is introduced — the new functions slot into the existing functionCallFunction*FunctionMapper pipeline.

Function Meaning Arity JPA (CriteriaBuilder) MongoDB
ADD(a, b, ...) sum variadic, min 2 sum (fold) $add
SUBTRACT(a, b) a - b binary diff $subtract
MULTIPLY(a, b, ...) product variadic, min 2 prod (fold) $multiply
DIVIDE(a, b) a / b binary quot $divide
NEGATE(a) -a unary neg $multiply [-1, x]

Arity rationale: ADD/MULTIPLY are associative and commutative, so they map 1:1 to the natively-variadic $add/$multiply (and to a JPA fold, exactly like CONCAT today). SUBTRACT/DIVIDE are only left-associative and their backends are binary-only, so they stay strictly binary; chains are expressed by nesting: ADD(MULTIPLY(price, 1.22), fee).

Examples

select multiply(price, 1.22) as gross
where subtract(qty, reserved) > 0
select multiply(sum(price), 2)

Changes

  • Function enumADD(-1), SUBTRACT(2), MULTIPLY(-1), DIVIDE(2), NEGATE(1) in ARITHMETIC; getMinimumArguments() returns 2 for the variadic arithmetic functions
  • Lexer/Parser — five new function-name tokens; functionArgs already supports variadic args, nested calls and numeric literals, so no other grammar change
  • JpaFunctionMappercb.sum/diff/prod/quot/neg; add/multiply fold like concat
  • MongodbFunctionMapper$add/$subtract/$multiply/$divide; negate as $multiply [-1, x]
  • Querity factoryadd(...), subtract(a, b), multiply(...), divide(a, b), negate(a)
  • Elasticsearch — unsupported (throws UnsupportedOperationException), consistent with every other function

Notes

  • Integer division: DIVIDE maps to cb.quot, which follows the backend's integer-division semantics for integer operands; documented in the enum Javadoc. Happy to coerce to decimal instead if preferred.
  • Backward compatible: the new names are not valid anywhere in today's grammar.

Testing

  • Unit tests: enum arity/category, FunctionCall arity validation, parser (variadic, nested, decimal literals, case-insensitive), JPA mapper (incl. fold verification), MongoDB mapper (document shapes), factory helpers, Elasticsearch unsupported
  • Integration tests in QuerityGenericTestSuite (ADD/SUBTRACT/MULTIPLY in filters) — green on H2, PostgreSQL (Testcontainers) and MongoDB
  • ./mvnw verify — JaCoCo per-class coverage checks met on all touched modules

Closes #192

🤖 Generated with Claude Code

Expose row-level arithmetic as prefix functions, consistent with the
existing ABS/SQRT/MOD design and reusing the functionCall -> Function ->
*FunctionMapper pipeline (no grammar precedence changes).

- add/multiply variadic (min 2, associative), subtract/divide binary
  (left-associative, binary-only backends), negate unary
- JPA: CriteriaBuilder sum/diff/prod/quot/neg (variadic fold like concat)
- MongoDB: $add/$subtract/$multiply/$divide; negate via $multiply[-1, x]
- Elasticsearch: unsupported (throws), consistent with all functions
- Java factory helpers on Querity; end-to-end tests on H2 + embedded Mongo

Refs queritylib#192

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@brunomendola
brunomendola merged commit 257abbc into queritylib:main Jul 11, 2026
1 of 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.

Proposal: arithmetic functions (add / subtract / multiply / divide / negate)

2 participants