Skip to content
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

[Backport 0.5] ispresent implemented as function #653

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class FlintSparkPPLBuiltinFunctionITSuite
assert(results.sorted.sameElements(expectedResults.sorted))
}

test("test boolean condition functions - isnull isnotnull ifnull nullif") {
test("test boolean condition functions - isnull isnotnull ifnull nullif ispresent") {
val frameIsNull = sql(s"""
| source = $testNullTable | where isnull(name) | fields age
| """.stripMargin)
Expand Down Expand Up @@ -513,6 +513,27 @@ class FlintSparkPPLBuiltinFunctionITSuite
val expectedResults4: Array[Row] =
Array(Row("John", 25), Row("Jane", null), Row(null, 10), Row("Jake", 70), Row("Hello", 30))
assert(results4.sameElements(expectedResults4))

val frameIsPresent = sql(s"""
| source = $testNullTable | where ispresent(name) | fields name
| """.stripMargin)

val results5: Array[Row] = frameIsPresent.collect()
val expectedResults5: Array[Row] = Array(Row("John"), Row("Jane"), Row("Jake"), Row("Hello"))
assert(results5.sameElements(expectedResults5))

val frameEvalIsPresent = sql(s"""
| source = $testNullTable | eval hasName = ispresent(name) | fields name, hasName
| """.stripMargin)

val results6: Array[Row] = frameEvalIsPresent.collect()
val expectedResults6: Array[Row] = Array(
Row("John", true),
Row("Jane", true),
Row(null, false),
Row("Jake", true),
Row("Hello", true))
assert(results6.sameElements(expectedResults6))
}

test("test typeof function") {
Expand Down
2 changes: 2 additions & 0 deletions ppl-spark-integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ See the next samples of PPL queries :
- `source = table | where a < 1 | fields a,b,c`
- `source = table | where b != 'test' | fields a,b,c`
- `source = table | where c = 'test' | fields a,b,c | head 3`
- `source = table where ispresent(b)`

**Filters With Logical Conditions**
- `source = table | where c = 'test' AND a = 1 | fields a,b,c`
Expand All @@ -259,6 +260,7 @@ Assumptions: `a`, `b`, `c` are existing fields in `table`
- `source = table | eval f = a * 2 | eval h = f * 2 | fields a,f,h`
- `source = table | eval f = a * 2, h = f * 2 | fields a,f,h`
- `source = table | eval f = a * 2, h = b | stats avg(f) by h`
- `source = table | eval f = ispresent(a)`

Limitation: Overriding existing field is unsupported, following queries throw exceptions with "Reference 'a' is ambiguous"
- `source = table | eval a = 10 | fields a,b,c`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ CAST: 'CAST';
LIKE: 'LIKE';
ISNULL: 'ISNULL';
ISNOTNULL: 'ISNOTNULL';
ISPRESENT: 'ISPRESENT';

// FLOWCONTROL FUNCTIONS
IFNULL: 'IFNULL';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ conditionFunctionBase
| ISNOTNULL
| IFNULL
| NULLIF
| ISPRESENT
;

systemFunctionName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public enum BuiltinFunctionName {
IF(FunctionName.of("if")),
NULLIF(FunctionName.of("nullif")),
ISNULL(FunctionName.of("isnull")),
ISPRESENT(FunctionName.of("ispresent")),

ROW_NUMBER(FunctionName.of("row_number")),
RANK(FunctionName.of("rank")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class AstExpressionBuilder extends OpenSearchPPLParserBaseVisitor<Unresol
new ImmutableMap.Builder<String, String>()
.put("isnull", IS_NULL.getName().getFunctionName())
.put("isnotnull", IS_NOT_NULL.getName().getFunctionName())
.put("ispresent", IS_NOT_NULL.getName().getFunctionName())
.build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public interface BuiltinFunctionTranslator {
//condition functions
.put(IS_NULL, "isnull")
.put(IS_NOT_NULL, "isnotnull")
.put(BuiltinFunctionName.ISPRESENT, "isnotnull")
.build();

static Expression builtinFunction(org.opensearch.sql.ast.expression.Function function, List<Expression> args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,19 @@ class PPLLogicalPlanStringFunctionsTranslatorTestSuite
val expectedPlan = Project(projectList, filterPlan)
comparePlans(expectedPlan, logPlan, false)
}

test("test ispresent") {
val context = new CatalystPlanContext
val logPlan =
planTransformer.visit(plan(pplParser, "source=t a = ispresent(b)", false), context)

val table = UnresolvedRelation(Seq("t"))
val filterExpr = EqualTo(
UnresolvedAttribute("a"),
UnresolvedFunction("isnotnull", seq(UnresolvedAttribute("b")), isDistinct = false))
val filterPlan = Filter(filterExpr, table)
val projectList = Seq(UnresolvedStar(None))
val expectedPlan = Project(projectList, filterPlan)
comparePlans(expectedPlan, logPlan, false)
}
}
Loading