Add per_second function support for timechart command#4464
Merged
dai-chen merged 28 commits intoopensearch-project:mainfrom Oct 13, 2025
Merged
Conversation
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
Signed-off-by: Chen Dai <daichen@amazon.com>
RyanL1997
reviewed
Oct 10, 2025
|
|
||
| return eval( | ||
| timechart(AstDSL.alias(perFunc.aggName, sum(perFunc.aggArg))), | ||
| let(perFunc.aggName).multiply(perFunc.seconds).dividedBy(spanSeconds)); |
Collaborator
There was a problem hiding this comment.
What happens if the span evaluates to 0 seconds (e.g., with millisecond spans or edge cases)? Should there be validation or error handling for division by zero?
Collaborator
Author
There was a problem hiding this comment.
Good catch. Let me check if timechart has the validation or not. Thanks!
Collaborator
Author
There was a problem hiding this comment.
I quick tested timechart command and found negative span value is not allowed in our grammar. But zero-span validation is missing. Let me create an issue and raise separate PR to fix.
opensearchsql> source=test_data_2023 | timechart span=0m per_second(packets);
TransportError(500, 'SearchPhaseExecutionException', {'error':
{'reason':'Error occurred in OpenSearch engine: all shards failed', 'details': 'Shard[0]:
java.lang.IllegalArgumentException: Zero or negative time interval not supported\n\n
For more details, please send request for Json format to see the raw response from OpenSearch engine.',
'type': 'SearchPhaseExecutionException'}, 'status': 400})
opensearchsql> source=test_data_2023 | timechart span=-1m per_second(packets);
{'reason': 'Invalid Query', 'details': "[-] is not a valid term at this part of the query: '...23
| timechart span=-' <-- HERE. extraneous input '-' expecting {SPANLENGTH, INTEGER_LITERAL}",
'type': 'SyntaxCheckException'}
ykmr1224
previously approved these changes
Oct 10, 2025
core/src/main/java/org/opensearch/sql/calcite/utils/PlanUtils.java
Outdated
Show resolved
Hide resolved
ahkcs
reviewed
Oct 10, 2025
integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteTimechartPerFunctionIT.java
Show resolved
Hide resolved
Signed-off-by: Chen Dai <daichen@amazon.com>
RyanL1997
approved these changes
Oct 13, 2025
ahkcs
approved these changes
Oct 13, 2025
ykmr1224
approved these changes
Oct 13, 2025
opensearch-trigger-bot bot
pushed a commit
that referenced
this pull request
Oct 13, 2025
Add per_second() support to the timechart command by implementing Option 3 (Eval Transformation). --------- Signed-off-by: Chen Dai <daichen@amazon.com> (cherry picked from commit 4d416db) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
8 tasks
dai-chen
pushed a commit
that referenced
this pull request
Oct 15, 2025
) Add per_second() support to the timechart command by implementing Option 3 (Eval Transformation). --------- (cherry picked from commit 4d416db) Signed-off-by: Chen Dai <daichen@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Lantao Jin <ltjin@amazon.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Lantao Jin <ltjin@amazon.com>
This was referenced Jan 11, 2026
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.
Description
This PR adds
per_second()support to thetimechartcommand by implementing Option 3 (Eval Transformation). As a short-term solution, it extends theTimechartAST to recognize the per_second function and rewrites into the equivalent math formula shown in the examples below. See issue #4350 for background and alternatives.Examples
For spans whose length varies with the calendar (month/quarter/year), the number of seconds depends on actual start/end timestamps (e.g., September has 30 days; October has 31 days; February in a leap year has 29 days). To ensure correctness, the rewrite computes the exact bucket length dynamically:
-- Example: span=2mon source=logs | timechart span=2mon sum(packets) as "per_second(packets)" | eval `per_second(packets)` = `per_second(packets)` / timestampdiff( SECOND, @timestamp, -- span start timestampadd(MONTH, 2, @timestamp) -- span end (start + 2 months) )Related Issues
Resolves partially #4350.
Check List
--signoffor-s.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.