Skip to content

Commit 3f7a56b

Browse files
committed
Support function coalesce with Calcite
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent e6116bc commit 3f7a56b

6 files changed

Lines changed: 45 additions & 0 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public enum BuiltinFunctionName {
226226
IF(FunctionName.of("if")),
227227
NULLIF(FunctionName.of("nullif")),
228228
ISNULL(FunctionName.of("isnull")),
229+
COALESCE(FunctionName.of("coalesce")),
229230

230231
ROW_NUMBER(FunctionName.of("row_number")),
231232
RANK(FunctionName.of("rank")),

core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void populate() {
202202
registerOperator(IS_NULL, SqlStdOperatorTable.IS_NULL);
203203
registerOperator(IF, SqlStdOperatorTable.CASE);
204204
registerOperator(IFNULL, SqlStdOperatorTable.COALESCE);
205+
registerOperator(COALESCE, SqlStdOperatorTable.COALESCE);
205206

206207
// Register library operator
207208
registerOperator(REGEXP, SqlLibraryOperators.REGEXP);

docs/user/ppl/functions/condition.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,29 @@ Example::
238238
| Dale | Adams | 33 |
239239
+-----------+----------+-----+
240240

241+
COALESCE
242+
--------
243+
244+
Description
245+
>>>>>>>>>>>
246+
247+
Version: 3.1.0
248+
249+
Usage: coalesce(field1, field2, ...) return the first non-null value in the argument list.
250+
251+
Argument type: all the supported data type, (NOTE : if parameters have different type, you will fail semantic check)
252+
253+
Return type: any
254+
255+
Example::
256+
257+
os> source=accounts | eval result = coalesce(employer, firstname, lastname) | fields result, firstname, lastname, employer
258+
fetched rows / total rows = 4/4
259+
+---------+-----------+----------+----------+
260+
| result | firstname | lastname | employer |
261+
|---------+-----------+----------+----------|
262+
| Pyrami | Amber | Duke | Pyrami |
263+
| Netagy | Hattie | Bond | Netagy |
264+
| Quility | Nanette | Bates | Quility |
265+
| Dale | Dale | Adams | null |
266+
+---------+-----------+----------+----------+

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLConditionBuiltinFunctionIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ public void testIfNull() {
108108
rows("Hello", 30));
109109
}
110110

111+
@Test
112+
public void testCoalesce() {
113+
JSONObject actual =
114+
executeQuery(
115+
String.format(
116+
"source=%s | where age = 10 | eval new_country = coalesce(name, state, country) | fields name, state, country, new_country",
117+
TEST_INDEX_STATE_COUNTRY_WITH_NULL));
118+
119+
verifySchema(actual, schema("name", "string"), schema("state", "string"), schema("country", "string"), schema("new_country", "string"));
120+
121+
verifyDataRows(
122+
actual,
123+
rows(null, null, "Canada", "Canada"));
124+
}
125+
111126
@Test
112127
public void testIf() {
113128
JSONObject actual =

ppl/src/main/antlr/OpenSearchPPLLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ IFNULL: 'IFNULL';
376376
NULLIF: 'NULLIF';
377377
IF: 'IF';
378378
TYPEOF: 'TYPEOF';
379+
COALESCE: 'COALESCE';
379380

380381
// RELEVANCE FUNCTIONS AND PARAMETERS
381382
MATCH: 'MATCH';

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ flowControlFunctionName
841841
: IF
842842
| IFNULL
843843
| NULLIF
844+
| COALESCE
844845
;
845846

846847
systemFunctionName

0 commit comments

Comments
 (0)