Skip to content

Commit 4c4166b

Browse files
authored
[Error Enhancement] Fix NPE when rex sits inside appendcol subsearch for Analytic Engine (#5574)
Signed-off-by: Jialiang Liang <jiallian@amazon.com>
1 parent b2fd268 commit 4c4166b

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

core/src/main/java/org/opensearch/sql/ast/tree/Rex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Rex attach(UnresolvedPlan child) {
7979

8080
@Override
8181
public List<UnresolvedPlan> getChild() {
82-
return ImmutableList.of(child);
82+
return this.child == null ? ImmutableList.of() : ImmutableList.of(this.child);
8383
}
8484

8585
@Override

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLAppendcolIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,18 @@ public void testAppendColOverride() throws IOException {
8181
rows("F", "DE", 101, null),
8282
rows("F", "FL", 310, null));
8383
}
84+
85+
/** Verifies that rex can be used as the first command of an appendcol subsearch. */
86+
@Test
87+
public void testAppendColWithRexInSubsearch() throws IOException {
88+
JSONObject actual =
89+
executeQuery(
90+
String.format(
91+
"source=%s | stats count() as cnt by gender"
92+
+ " | appendcol [ rex field=email '^(?<user>[^@]+)@.*' | fields user ]"
93+
+ " | head 2",
94+
TEST_INDEX_ACCOUNT));
95+
verifySchema(
96+
actual, schema("gender", "string"), schema("cnt", "bigint"), schema("user", "string"));
97+
}
8498
}

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLRexTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,14 @@ public void testRexWithMaxMatchAndOffsetField() {
306306
+ "FROM `scott`.`EMP`";
307307
verifyPPLToSparkSQL(root, expectedSparkSql);
308308
}
309+
310+
/** Verifies that rex plans correctly when it appears as the first command of a subsearch. */
311+
@Test
312+
public void testRexInsideSubsearch() {
313+
String ppl =
314+
"source=EMP | stats count() as base_c by JOB"
315+
+ " | appendcol [ rex field=ENAME '^(?<first>[A-Z])' | fields ENAME, first ]";
316+
RelNode root = getRelNode(ppl);
317+
org.junit.Assert.assertNotNull(root);
318+
}
309319
}

0 commit comments

Comments
 (0)