-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[9.0] ESQL: Refactor Greatest and Least functions to use evaluator map (#114036) #128429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[9.0] ESQL: Refactor Greatest and Least functions to use evaluator map (#114036) #128429
Conversation
This commit refactors the Greatest function to use an evaluator map for better code organization. The changes include: - Added EVALUATOR_MAP for evaluator factory mapping - Updated toEvaluator to use map-based lookup - Added NULL type validation - Updated error messages to use getWriteableName() The refactoring improves code readability and maintainability by: - Replacing if-else chains with a map-based lookup - Centralizing evaluator factory creation - Adding consistent NULL type validation - Using getWriteableName() for error messages Relates to elastic#114036
This commit refactors the Least function to use an evaluator map for better code organization. The changes include: - Added EVALUATOR_MAP for evaluator factory mapping - Updated toEvaluator to use map-based lookup - Added NULL type validation - Updated error messages to use getWriteableName() The refactoring improves code readability and maintainability by: - Replacing if-else chains with a map-based lookup - Centralizing evaluator factory creation - Adding consistent NULL type validation - Using getWriteableName() for error messages Relates to elastic#114036
This commit adds test cases for both Greatest and Least functions to verify: - All supported data types (boolean, double, integer, long, keyword, text, version, ip, datetime, date_nanos) - NULL value handling - String comparisons - Numeric comparisons - Boolean operations - Date/time comparisons The tests ensure that: - Both functions handle NULL values correctly - Type validation works as expected - Evaluator selection is correct for each data type - Results are computed correctly for all supported types Relates to elastic#114036
Pinging @elastic/es-analytical-engine (Team:Analytics) |
throw EsqlIllegalArgumentException.illegalDataType(dataType); | ||
} | ||
|
||
return evaluatorFactory.apply(factories); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is one of the things worth doing with java`s switch? Each data type is enum and compile time constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! This is really helpful. I'll work on refactoring the code based on your suggestions :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @shinsj4653 and thank you very much for your contribution!
I'm sorry it took us so long to get back to you.
Unfortunately, this PR does not contain tests that demonstrate that the earlier type check actually happens and returns the expected error message. We often add cases to the VerifierTests
in such situations.
Hi @alex-spies, I appreciate your feedback. I'll add a test case for this issue using VerifierTests |
…- Replace Map-based evaluator lookup with Java switch expressions- Add isSupportedDataType helper method for type validation- Improve error handling and type checking consistency- Ensure resolveType and toEvaluator are aligned for type support
… testGreatestLeastWithUnsupportedDataTypes test method- Test unsupported data types (geo_point, cartesian_point)- Test mixed type scenarios with proper error message validation- Ensure type consistency between resolveType and toEvaluator- Validate error messages for type mismatches
Thank you for the valuable feedback! I've implemented the suggested changes: ✅ idegtiarenko's feedback: Refactored to use switch expressions instead of Map-based evaluator lookup The changes ensure:
All tests are passing. PTAL and I'd appreciate your review! 🙏 |
ESQL: Refactor Greatest and Least functions to use switch statements
Description
This PR refactors the
Greatest
andLeast
functions to use Java switch expressions instead of Map-based evaluator lookup for better performance and code clarity. The changes include:EVALUATOR_MAP
with switch expressions intoEvaluator
methodisSupportedDataType
helper method using switch expressionVerifierTests
Changes
Greatest.java
:isSupportedDataType
method for type validationtoEvaluator
to use switch-based factory creationLeast.java
:isSupportedDataType
method for type validationtoEvaluator
to use switch-based factory creationVerifierTests.java
:testGreatestLeastWithUnsupportedDataTypes()
test methodresolveType
andtoEvaluator
Testing
GreatestTests.java
,LeastTests.java
VerifierTests.testGreatestLeastWithUnsupportedDataTypes
./gradlew :x-pack:plugin:esql:test --tests "VerifierTests.testGreatestLeastWithUnsupportedDataTypes" -x :libs:simdvec:compileMain21Java -x :libs:simdvec:compileMain22Java --console=plain --info
✅ Greatest/Least Function Tests
✅ VerifierTests - Type Validation
resolveType
andtoEvaluator
🔍 Error Scenario Validation
The new type validation correctly rejects unsupported data types:
geo_point
→ "Cannot use [geo_point] with function [Greatest]"cartesian_point
→ "Cannot use [cartesian_point] with function [Least]"Note: The error messages are validated by the test framework and don't appear in logs when tests pass, which is the expected behavior.
Related Issues
Closes #114036
Checklist
Backport