-
Notifications
You must be signed in to change notification settings - Fork 180
Add replace command with Calcite #4451
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
Changes from all commits
6128a7e
00eee9e
1288c44
3e70cbd
1790fd8
f92545a
a2df2fc
af95e74
638ae34
d099aed
72defe9
9d1a2fc
b6fe452
d5beab7
38eeb2f
c0d7ffe
4b4217e
0ff7d97
e49d99f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.tree; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
| import org.opensearch.sql.ast.expression.Field; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class Replace extends UnresolvedPlan { | ||
| private final List<ReplacePair> replacePairs; | ||
| private final Set<Field> fieldList; | ||
| @Nullable private UnresolvedPlan child; | ||
|
|
||
| /** | ||
| * Constructor with multiple pattern/replacement pairs. | ||
| * | ||
| * @param replacePairs List of pattern/replacement pairs | ||
| * @param fieldList Set of fields to apply replacements to | ||
| */ | ||
| public Replace(List<ReplacePair> replacePairs, Set<Field> fieldList) { | ||
| this.replacePairs = replacePairs; | ||
| this.fieldList = fieldList; | ||
| } | ||
|
|
||
| @Override | ||
| public Replace attach(UnresolvedPlan child) { | ||
| if (null == this.child) { | ||
| this.child = child; | ||
| } else { | ||
| this.child.attach(child); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public List<UnresolvedPlan> getChild() { | ||
| return this.child == null ? ImmutableList.of() : ImmutableList.of(this.child); | ||
| } | ||
|
|
||
| @Override | ||
| public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
| return nodeVisitor.visitReplace(this, context); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.tree; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.ToString; | ||
| import org.opensearch.sql.ast.expression.Literal; | ||
|
|
||
| /** A pair of pattern and replacement literals for the Replace command. */ | ||
| @Getter | ||
| @AllArgsConstructor | ||
| @EqualsAndHashCode | ||
| @ToString | ||
| public class ReplacePair { | ||
| private final Literal pattern; | ||
| private final Literal replacement; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| ============= | ||
| replace | ||
| ============= | ||
|
|
||
| .. rubric:: Table of contents | ||
|
|
||
| .. contents:: | ||
| :local: | ||
| :depth: 2 | ||
|
|
||
|
|
||
| Description | ||
| ============ | ||
| Using ``replace`` command to replace text in one or more fields in the search result. | ||
|
|
||
| Note: This command is only available when Calcite engine is enabled. | ||
|
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. Shall we mention version since? I forgot what's the standard way we proposed.. @ritvibhatt
Contributor
Author
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. I think for the current proposal, we don't need to add version info |
||
|
|
||
|
|
||
| Syntax | ||
| ============ | ||
| replace '<pattern>' WITH '<replacement>' [, '<pattern>' WITH '<replacement>']... IN <field-name>[, <field-name>]... | ||
|
|
||
|
|
||
| Parameters | ||
| ========== | ||
| * **pattern**: mandatory. The text pattern you want to replace. Currently supports only plain text literals (no wildcards or regular expressions). | ||
| * **replacement**: mandatory. The text you want to replace with. | ||
| * **field-name**: mandatory. One or more field names where the replacement should occur. | ||
|
|
||
|
|
||
| Examples | ||
| ======== | ||
|
|
||
| Example 1: Replace text in one field | ||
| ------------------------------------ | ||
|
|
||
| The example shows replacing text in one field. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | replace "IL" WITH "Illinois" IN state | fields state; | ||
| fetched rows / total rows = 4/4 | ||
| +----------+ | ||
| | state | | ||
| |----------| | ||
| | Illinois | | ||
| | TN | | ||
| | VA | | ||
| | MD | | ||
| +----------+ | ||
|
|
||
|
|
||
| Example 2: Replace text in multiple fields | ||
| ------------------------------------ | ||
|
|
||
| The example shows replacing text in multiple fields. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | replace "IL" WITH "Illinois" IN state, address | fields state, address; | ||
| fetched rows / total rows = 4/4 | ||
| +----------+----------------------+ | ||
| | state | address | | ||
| |----------+----------------------| | ||
| | Illinois | 880 Holmes Lane | | ||
| | TN | 671 Bristol Street | | ||
| | VA | 789 Madison Street | | ||
| | MD | 467 Hutchinson Court | | ||
| +----------+----------------------+ | ||
|
|
||
|
|
||
| Example 3: Replace with other commands in a pipeline | ||
| ------------------------------------ | ||
|
|
||
| The example shows using replace with other commands in a query pipeline. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | replace "IL" WITH "Illinois" IN state | where age > 30 | fields state, age; | ||
| fetched rows / total rows = 3/3 | ||
| +----------+-----+ | ||
| | state | age | | ||
| |----------+-----| | ||
| | Illinois | 32 | | ||
| | TN | 36 | | ||
| | MD | 33 | | ||
| +----------+-----+ | ||
|
|
||
| Example 4: Replace with multiple pattern/replacement pairs | ||
| ------------------------------------ | ||
|
|
||
| The example shows using multiple pattern/replacement pairs in a single replace command. The replacements are applied sequentially. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | replace "IL" WITH "Illinois", "TN" WITH "Tennessee" IN state | fields state; | ||
| fetched rows / total rows = 4/4 | ||
| +-----------+ | ||
| | state | | ||
| |-----------| | ||
| | Illinois | | ||
| | Tennessee | | ||
| | VA | | ||
| | MD | | ||
| +-----------+ | ||
|
|
||
| Example 5: Pattern matching with LIKE and replace | ||
| ------------------------------------ | ||
|
|
||
| Since replace command only supports plain string literals, you can use LIKE command with replace for pattern matching needs. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | where LIKE(address, '%Holmes%') | replace "Holmes" WITH "HOLMES" IN address | fields address, state, gender, age, city; | ||
| fetched rows / total rows = 1/1 | ||
| +-----------------+-------+--------+-----+--------+ | ||
| | address | state | gender | age | city | | ||
| |-----------------+-------+--------+-----+--------| | ||
| | 880 HOLMES Lane | IL | M | 32 | Brogan | | ||
| +-----------------+-------+--------+-----+--------+ | ||
|
|
||
|
|
||
| Limitations | ||
| =========== | ||
| * Only supports plain text literals for pattern matching. Wildcards and regular expressions are not supported. | ||
| * Pattern and replacement values must be string literals. | ||
| * The replace command modifies the specified fields in-place. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: consider using
@Value