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.
- Loading branch information
1 parent
ac8678c
commit 3d9aaa7
Showing
12 changed files
with
235 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
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/opensearch/sql/ast/tree/AddField.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,48 @@ | ||
/* | ||
* 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 lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
import org.opensearch.sql.ast.expression.Literal; | ||
|
||
/** AST node represent AddField operation. */ | ||
@Getter | ||
@Setter | ||
@ToString | ||
@EqualsAndHashCode(callSuper = false) | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class AddField extends UnresolvedPlan { | ||
/** Child Plan. */ | ||
private UnresolvedPlan child; | ||
|
||
private final Literal fieldName; | ||
private final Literal fieldValue; | ||
|
||
@Override | ||
public UnresolvedPlan attach(final UnresolvedPlan child) { | ||
this.child = child; | ||
return this; | ||
} | ||
|
||
@Override | ||
public List<UnresolvedPlan> getChild() { | ||
return ImmutableList.of(this.child); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitAddField(this, context); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
core/src/test/java/org/opensearch/sql/planner/logical/LogicalAddFieldTest.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,29 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.planner.logical; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
|
||
import org.apache.commons.lang3.tuple.ImmutablePair; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.sql.analysis.AnalyzerTestBase; | ||
import org.opensearch.sql.ast.dsl.AstDSL; | ||
import org.opensearch.sql.expression.DSL; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class LogicalAddFieldTest extends AnalyzerTestBase { | ||
@Test | ||
public void analyze_addfield_with_one_field() { | ||
assertAnalyzeEqual( | ||
LogicalPlanDSL.eval( | ||
LogicalPlanDSL.relation("schema", table), | ||
ImmutablePair.of(DSL.ref("x", STRING), DSL.literal("foo"))), | ||
AstDSL.addField( | ||
AstDSL.relation("schema"), AstDSL.stringLiteral("x"), AstDSL.stringLiteral("foo"))); | ||
} | ||
} |
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,44 @@ | ||
============= | ||
addfield | ||
============= | ||
|
||
.. rubric:: Table of contents | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 2 | ||
|
||
|
||
Description | ||
============ | ||
| The ``addfield`` command adds a field with a constant value. | ||
|
||
Syntax | ||
============ | ||
addfield <field_name> <field_value> | ||
|
||
* <field_name>: mandatory string. name of field to add | ||
* <field_value>: mandatory string. value of new field | ||
|
||
Example 1: Add a field named "foo" with the value "bar" | ||
=========================================== | ||
|
||
The example show maximum 10 results from accounts index. | ||
|
||
PPL query:: | ||
|
||
os> source=accounts | fields firstname, age | addfield 'foo' 'bar'; | ||
fetched rows / total rows = 4/4 | ||
+-------------+-------+-------+ | ||
| firstname | age | foo | | ||
|-------------+-------+-------| | ||
| Amber | 32 | bar | | ||
| Hattie | 36 | bar | | ||
| Nanette | 28 | bar | | ||
| Dale | 33 | bar | | ||
+-------------+-------+-------+ | ||
|
||
Limitation | ||
========== | ||
The ``addfield`` command is not rewritten to OpenSearch DSL, it is only executed on the coordination node. |
55 changes: 55 additions & 0 deletions
55
integ-test/src/test/java/org/opensearch/sql/ppl/AddFieldCommandIT.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,55 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.ppl; | ||
|
||
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; | ||
import static org.opensearch.sql.util.MatcherUtils.rows; | ||
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; | ||
|
||
import java.io.IOException; | ||
import org.json.JSONObject; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AddFieldCommandIT extends PPLIntegTestCase { | ||
@Before | ||
public void beforeTest() throws IOException { | ||
setQuerySizeLimit(200); | ||
} | ||
|
||
@After | ||
public void afterTest() throws IOException { | ||
resetQuerySizeLimit(); | ||
resetMaxResultWindow(TEST_INDEX_ACCOUNT); | ||
} | ||
|
||
@Override | ||
public void init() throws IOException { | ||
loadIndex(Index.ACCOUNT); | ||
} | ||
|
||
@Test | ||
public void testAddField() throws IOException { | ||
JSONObject result = | ||
executeQuery( | ||
String.format( | ||
"source=%s | fields firstname, age | head | addfield 'x' 'foo'", | ||
TEST_INDEX_ACCOUNT)); | ||
verifyDataRows( | ||
result, | ||
rows("Amber", 32, "foo"), | ||
rows("Hattie", 36, "foo"), | ||
rows("Nanette", 28, "foo"), | ||
rows("Dale", 33, "foo"), | ||
rows("Elinor", 36, "foo"), | ||
rows("Virginia", 39, "foo"), | ||
rows("Dillard", 34, "foo"), | ||
rows("Mcgee", 39, "foo"), | ||
rows("Aurelia", 37, "foo"), | ||
rows("Fulton", 23, "foo")); | ||
} | ||
} |
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
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