forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FILLNULL command in PPL (opensearch-project#3032)
* Add FILLNULL command in PPL Signed-off-by: Norman Jordan <[email protected]>
- Loading branch information
1 parent
3b86612
commit 7186378
Showing
12 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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
88 changes: 88 additions & 0 deletions
88
core/src/main/java/org/opensearch/sql/ast/tree/FillNull.java
This file contains 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,88 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.ast.tree; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
import org.opensearch.sql.ast.Node; | ||
import org.opensearch.sql.ast.expression.Field; | ||
import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
|
||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class FillNull extends UnresolvedPlan { | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public static class NullableFieldFill { | ||
@NonNull private final Field nullableFieldReference; | ||
@NonNull private final UnresolvedExpression replaceNullWithMe; | ||
} | ||
|
||
public interface ContainNullableFieldFill { | ||
List<NullableFieldFill> getNullFieldFill(); | ||
|
||
static ContainNullableFieldFill ofVariousValue(List<NullableFieldFill> replacements) { | ||
return new VariousValueNullFill(replacements); | ||
} | ||
|
||
static ContainNullableFieldFill ofSameValue( | ||
UnresolvedExpression replaceNullWithMe, List<Field> nullableFieldReferences) { | ||
return new SameValueNullFill(replaceNullWithMe, nullableFieldReferences); | ||
} | ||
} | ||
|
||
private static class SameValueNullFill implements ContainNullableFieldFill { | ||
@Getter(onMethod_ = @Override) | ||
private final List<NullableFieldFill> nullFieldFill; | ||
|
||
public SameValueNullFill( | ||
UnresolvedExpression replaceNullWithMe, List<Field> nullableFieldReferences) { | ||
Objects.requireNonNull(replaceNullWithMe, "Null replacement is required"); | ||
this.nullFieldFill = | ||
Objects.requireNonNull(nullableFieldReferences, "Nullable field reference is required") | ||
.stream() | ||
.map(nullableReference -> new NullableFieldFill(nullableReference, replaceNullWithMe)) | ||
.toList(); | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static class VariousValueNullFill implements ContainNullableFieldFill { | ||
@NonNull | ||
@Getter(onMethod_ = @Override) | ||
private final List<NullableFieldFill> nullFieldFill; | ||
} | ||
|
||
private UnresolvedPlan child; | ||
|
||
@NonNull private final ContainNullableFieldFill containNullableFieldFill; | ||
|
||
public List<NullableFieldFill> getNullableFieldFills() { | ||
return containNullableFieldFill.getNullFieldFill(); | ||
} | ||
|
||
@Override | ||
public UnresolvedPlan attach(UnresolvedPlan child) { | ||
this.child = child; | ||
return this; | ||
} | ||
|
||
@Override | ||
public List<? extends Node> getChild() { | ||
return child == null ? List.of() : List.of(child); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitFillNull(this, context); | ||
} | ||
} |
This file contains 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 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,67 @@ | ||
============= | ||
fillnull | ||
============= | ||
|
||
.. rubric:: Table of contents | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 2 | ||
|
||
|
||
Description | ||
============ | ||
| The ``fillnull`` command replaces null values for one or more fields. | ||
|
||
Syntax | ||
============ | ||
fillnull "with" <expression> <field> ["," <field> ]... | ||
|
||
* field: mandatory. Name of an existing field that was piped into ``fillnull``. Null values for all specified fields are replaced with the value of expression. | ||
* expression: mandatory. Any expression support by the system. The expression value type must match the type of field. | ||
|
||
fillnull "using" <field> "=" <expression> ["," <field> "=" <expression> ]... | ||
|
||
* field: mandatory. Name of an existing field that was piped into ``fillnull``. | ||
* expression: mandatory. Any expression support by the system. The expression value type must match the type of field. | ||
|
||
Example 1: Replace null values with the same value for multiple fields | ||
====================================================================== | ||
|
||
The example show to replace null values for email and host with "<not found>". | ||
|
||
PPL query:: | ||
|
||
os> source=accounts | fields email, host | fillnull with '<not found>' email, host ; | ||
fetched rows / total rows = 4/4 | ||
+-----------------------+------------+ | ||
| email | host | | ||
|-----------------------+------------| | ||
| [email protected] | pyrami.com | | ||
| [email protected] | netagy.com | | ||
| <not found> | | | ||
| [email protected] | boink.com | | ||
+-----------------------+------------+ | ||
|
||
Example 2: Replace null values for multiple fields with different values | ||
======================================================================== | ||
|
||
The example show to replace null values for email with "<not found>" and null values for host with "<no host>". | ||
|
||
PPL query:: | ||
|
||
os> source=accounts | fields email, host | fillnull using email = '<not found>', host = '<no host>' ; | ||
fetched rows / total rows = 4/4 | ||
+-----------------------+------------+ | ||
| email | host | | ||
|-----------------------+------------| | ||
| [email protected] | pyrami.com | | ||
| [email protected] | netagy.com | | ||
| <not found> | | | ||
| [email protected] | boink.com | | ||
+-----------------------+------------+ | ||
|
||
Limitation | ||
========== | ||
The ``fillnull`` command is not rewritten to OpenSearch DSL, it is only executed on the coordination node. |
This file contains 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 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.