-
Notifications
You must be signed in to change notification settings - Fork 208
Support trendline command in Calcite #3741
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
Merged
LantaoJin
merged 8 commits into
opensearch-project:main
from
songkant-aws:trendline-command
Jun 10, 2025
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e46678
Support trendline command in Calcite
songkant-aws 773b37b
Merge branch 'main' into trendline-command
songkant-aws 4e9662b
Fix CalciteExplainIT for trendline command
songkant-aws 1be77f4
Merge branch 'main' into trendline-command
songkant-aws 68296b3
Update trendline.rst doc to callout new wma algorithm supported by Ca…
songkant-aws 4c178e7
Fix typo in the doctest and rephrase the doc and formula
songkant-aws 1e507da
Add missing trendline pushdown IT
songkant-aws ae63486
Remove unexpected change
songkant-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
121 changes: 121 additions & 0 deletions
121
integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLTrendlineIT.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.standalone; | ||
|
|
||
| import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; | ||
| import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES; | ||
| import static org.opensearch.sql.util.MatcherUtils.rows; | ||
| import static org.opensearch.sql.util.MatcherUtils.schema; | ||
| import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
| import static org.opensearch.sql.util.MatcherUtils.verifySchema; | ||
|
|
||
| import java.io.IOException; | ||
| import org.json.JSONObject; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class CalcitePPLTrendlineIT extends CalcitePPLIntegTestCase { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing CalcitePPLTrendlinePushdownIT if we add IT in standalone
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's added now. |
||
|
|
||
| @Override | ||
| public void init() throws IOException { | ||
| super.init(); | ||
| loadIndex(Index.BANK); | ||
| loadIndex(Index.BANK_WITH_NULL_VALUES); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineSma() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline sma(3, balance) as balance_trend |" | ||
| + " fields balance_trend", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("balance_trend", "double")); | ||
| verifyDataRows( | ||
| result, rows((Object) null), rows((Object) null), rows(37534.333333333336), rows(40488)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineWma() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline wma(3, balance) as balance_trend |" | ||
| + " fields balance_trend", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("balance_trend", "double")); | ||
| verifyDataRows( | ||
| result, rows((Object) null), rows((Object) null), rows(37753.5), rows(43029.333333333336)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineMultipleFields() throws Exception { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline sma(2, balance) as sma wma(3," | ||
| + " balance) as wma | fields balance, sma, wma", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema( | ||
| result, schema("balance", "long"), schema("sma", "double"), schema("wma", "double")); | ||
| verifyDataRows( | ||
| result, | ||
| rows(39225, null, null), | ||
| rows(32838, 36031.5, null), | ||
| rows(40540, 36689, 37753.5), | ||
| rows(48086, 44313, 43029.333333333336)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineNoAlias() throws Exception { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline sma(2, balance) | fields" | ||
| + " balance, balance_trendline", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("balance", "long"), schema("balance_trendline", "double")); | ||
| verifyDataRows( | ||
| result, rows(39225, null), rows(32838, 36031.5), rows(40540, 36689), rows(48086, 44313)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineOverwritesExisingField() throws Exception { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline sma(2, balance) as balance | fields" | ||
| + " balance", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("balance", "double")); | ||
| verifyDataRows(result, rows((Object) null), rows(36031.5), rows(36689), rows(44313)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlineWithSort() throws Exception { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | where balance > 30000 | trendline sort - balance sma(2, balance) |" | ||
| + " fields balance, balance_trendline", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("balance", "long"), schema("balance_trendline", "double")); | ||
| verifyDataRows( | ||
| result, rows(48086, null), rows(40540, 44313), rows(39225, 39882.5), rows(32838, 36031.5)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrendlinePreFilterNullValues() throws Exception { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | trendline sma(2, balance) | fields" + " balance, balance_trendline", | ||
| TEST_INDEX_BANK_WITH_NULL_VALUES)); | ||
| verifySchema(result, schema("balance", "long"), schema("balance_trendline", "double")); | ||
| verifyDataRows( | ||
| result, rows(39225, null), rows(32838, 36031.5), rows(4180, 18509), rows(48086, 26133)); | ||
| } | ||
| } | ||
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
6 changes: 6 additions & 0 deletions
6
integ-test/src/test/resources/expectedOutput/calcite/explain_trendline_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalProject(ageTrend=[CASE(>(COUNT() OVER (ROWS 1 PRECEDING), 1), /(SUM($8) OVER (ROWS 1 PRECEDING), CAST(COUNT($8) OVER (ROWS 1 PRECEDING)):DOUBLE NOT NULL), null:NULL)])\n LogicalFilter(condition=[IS NOT NULL($8)])\n LogicalSort(fetch=[5])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n", | ||
| "physical": "EnumerableCalc(expr#0..3=[{inputs}], expr#4=[1], expr#5=[>($t1, $t4)], expr#6=[CAST($t3):DOUBLE NOT NULL], expr#7=[/($t2, $t6)], expr#8=[null:NULL], expr#9=[CASE($t5, $t7, $t8)], ageTrend=[$t9])\n EnumerableWindow(window#0=[window(rows between $1 PRECEDING and CURRENT ROW aggs [COUNT(), $SUM0($0), COUNT($0)])])\n EnumerableCalc(expr#0=[{inputs}], expr#1=[IS NOT NULL($t0)], age=[$t0], $condition=[$t1])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[LIMIT->5, PROJECT->[age]], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":5,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}}, requestedTotalSize=5, pageSize=null, startFrom=0)])\n" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
integ-test/src/test/resources/expectedOutput/calcite/explain_trendline_sort_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalProject(ageTrend=[CASE(>(COUNT() OVER (ROWS 1 PRECEDING), 1), /(SUM($8) OVER (ROWS 1 PRECEDING), CAST(COUNT($8) OVER (ROWS 1 PRECEDING)):DOUBLE NOT NULL), null:NULL)])\n LogicalFilter(condition=[IS NOT NULL($8)])\n LogicalSort(sort0=[$8], dir0=[ASC])\n LogicalSort(fetch=[5])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n", | ||
| "physical": "EnumerableCalc(expr#0..3=[{inputs}], expr#4=[1], expr#5=[>($t1, $t4)], expr#6=[CAST($t3):DOUBLE NOT NULL], expr#7=[/($t2, $t6)], expr#8=[null:NULL], expr#9=[CASE($t5, $t7, $t8)], ageTrend=[$t9])\n EnumerableWindow(window#0=[window(rows between $1 PRECEDING and CURRENT ROW aggs [COUNT(), $SUM0($0), COUNT($0)])])\n EnumerableCalc(expr#0=[{inputs}], expr#1=[IS NOT NULL($t0)], age=[$t0], $condition=[$t1])\n EnumerableSort(sort0=[$0], dir0=[ASC])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[LIMIT->5, PROJECT->[age]], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":5,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}}, requestedTotalSize=5, pageSize=null, startFrom=0)])\n" | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Change to:
Starting with version 3.1.0, the
trendlinecommand requires all values in the specifiedfieldto be non-null. Any null values present in the calculation field will be automatically excluded from the command's output.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.
Done