generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Support count(eval) expression with stats command
#4103
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
vamsimanohar
merged 8 commits into
opensearch-project:main
from
dai-chen:support-count-eval-function
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbb03db
Rewrite count(eval) expression to support filtered counting
dai-chen 1aa23e7
Refactor count eval UTs
dai-chen 06afcde
Add count eval ITs
dai-chen 06f3080
Add count eval doctest
dai-chen ed488aa
Fix doctest failure
dai-chen 93ad059
Add more UT for AST builder
dai-chen c71cedb
Merge branch 'main' into support-count-eval-function
dai-chen dcb8f85
Resolve conflicts and more changes for shortcut c
dai-chen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,37 @@ private Node plan(PPLSyntaxParser parser, String query) { | |
| return builder.visit(parser.parse(query)); | ||
| } | ||
|
|
||
| /** | ||
| * Fluent API for building count(eval) test cases. Provides a clean and readable way to define PPL | ||
| * queries and their expected outcomes. | ||
| */ | ||
| protected PPLQueryTestBuilder withPPLQuery(String ppl) { | ||
|
Collaborator
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. nice! |
||
| return new PPLQueryTestBuilder(ppl); | ||
| } | ||
|
|
||
| protected class PPLQueryTestBuilder { | ||
| private final RelNode relNode; | ||
|
|
||
| public PPLQueryTestBuilder(String ppl) { | ||
| this.relNode = getRelNode(ppl); | ||
| } | ||
|
|
||
| public PPLQueryTestBuilder expectLogical(String expectedLogical) { | ||
| verifyLogical(relNode, expectedLogical); | ||
| return this; | ||
| } | ||
|
|
||
| public PPLQueryTestBuilder expectResult(String expectedResult) { | ||
| verifyResult(relNode, expectedResult); | ||
| return this; | ||
| } | ||
|
|
||
| public PPLQueryTestBuilder expectSparkSQL(String expectedSparkSql) { | ||
| verifyPPLToSparkSQL(relNode, expectedSparkSql); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| /** Verify the logical plan of the given RelNode */ | ||
| public void verifyLogical(RelNode rel, String expectedLogical) { | ||
| assertThat(rel, hasTree(expectedLogical)); | ||
|
|
||
110 changes: 110 additions & 0 deletions
110
ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLCountEvalTest.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,110 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ppl.calcite; | ||
|
|
||
| import org.apache.calcite.test.CalciteAssert; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Unit tests for count(eval) functionality in CalcitePPL engine. Tests various scenarios of | ||
| * filtered count aggregations. | ||
| */ | ||
| public class CalcitePPLCountEvalTest extends CalcitePPLAbstractTest { | ||
|
|
||
| public CalcitePPLCountEvalTest() { | ||
| super(CalciteAssert.SchemaSpec.SCOTT_WITH_TEMPORAL); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountEvalSimpleCondition() { | ||
| withPPLQuery("source=EMP | stats count(eval(SAL > 2000)) as c") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], c=[COUNT($0)])\n" | ||
| + " LogicalProject($f1=[CASE(>($5, 2000), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("c=6\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `SAL` > 2000 THEN 1 ELSE NULL END) `c`\nFROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountEvalComplexCondition() { | ||
| withPPLQuery("source=EMP | stats count(eval(SAL > 2000 and DEPTNO < 30)) as c") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], c=[COUNT($0)])\n" | ||
| + " LogicalProject($f2=[CASE(AND(>($5, 2000), <($7, 30)), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("c=5\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `SAL` > 2000 AND `DEPTNO` < 30 THEN 1 ELSE NULL END) `c`\n" | ||
| + "FROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountEvalStringComparison() { | ||
| withPPLQuery("source=EMP | stats count(eval(JOB = 'MANAGER')) as manager_count") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], manager_count=[COUNT($0)])\n" | ||
| + " LogicalProject($f1=[CASE(=($2, 'MANAGER':VARCHAR), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("manager_count=3\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `JOB` = 'MANAGER' THEN 1 ELSE NULL END) `manager_count`\n" | ||
| + "FROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountEvalArithmeticExpression() { | ||
| withPPLQuery("source=EMP | stats count(eval(SAL / COMM > 10)) as high_ratio") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], high_ratio=[COUNT($0)])\n" | ||
| + " LogicalProject($f2=[CASE(>(DIVIDE($5, $6), 10), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("high_ratio=0\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `DIVIDE`(`SAL`, `COMM`) > 10 THEN 1 ELSE NULL END)" | ||
| + " `high_ratio`\n" | ||
| + "FROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCountEvalWithNullHandling() { | ||
| withPPLQuery("source=EMP | stats count(eval(isnotnull(MGR))) as non_null_mgr") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], non_null_mgr=[COUNT($0)])\n" | ||
| + " LogicalProject($f1=[CASE(IS NOT NULL($3), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("non_null_mgr=13\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `MGR` IS NOT NULL THEN 1 ELSE NULL END) `non_null_mgr`\n" | ||
| + "FROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShortcutCEvalSimpleCondition() { | ||
| withPPLQuery("source=EMP | stats c(eval(SAL > 2000)) as c") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], c=[COUNT($0)])\n" | ||
| + " LogicalProject($f1=[CASE(>($5, 2000), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("c=6\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `SAL` > 2000 THEN 1 ELSE NULL END) `c`\nFROM `scott`.`EMP`"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShortcutCEvalComplexCondition() { | ||
| withPPLQuery("source=EMP | stats c(eval(JOB = 'MANAGER')) as manager_count") | ||
| .expectLogical( | ||
| "LogicalAggregate(group=[{}], manager_count=[COUNT($0)])\n" | ||
| + " LogicalProject($f1=[CASE(=($2, 'MANAGER':VARCHAR), 1, null:NULL)])\n" | ||
| + " LogicalTableScan(table=[[scott, EMP]])\n") | ||
| .expectResult("manager_count=3\n") | ||
| .expectSparkSQL( | ||
| "SELECT COUNT(CASE WHEN `JOB` = 'MANAGER' THEN 1 ELSE NULL END) `manager_count`\n" | ||
| + "FROM `scott`.`EMP`"); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.