generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
[Feature] implement transpose command as in the roadmap #4786 #5011
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
Open
asifabashar
wants to merge
17
commits into
opensearch-project:main
Choose a base branch
from
asifabashar:feature-transpose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
642d144
transpose command implementation
asifabashar 98a0cc8
transpose rows to columns
asifabashar 1872a3d
added argument type missing map and hashmap
asifabashar d7975da
added tests
asifabashar 63a9d6d
Merge branch 'opensearch-project:main' into feature-transpose
asifabashar 648914b
added tests
asifabashar 9bde0d0
added tests
asifabashar 3080c78
added tests
asifabashar 45be8ec
added tests
asifabashar b0db7ba
added tests
asifabashar 4db8d2a
added tests
asifabashar 914ad4e
added tests
asifabashar 1cf9083
added more validations
asifabashar 913ec30
added validation
asifabashar 0ee8357
index.md formatting fix
asifabashar 616f536
doc format
asifabashar 46d6b0b
coderabbit review fixes
asifabashar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
core/src/main/java/org/opensearch/sql/ast/tree/Transpose.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * 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.*; | ||
| import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
| import org.opensearch.sql.ast.expression.Argument; | ||
| import org.opensearch.sql.common.utils.StringUtils; | ||
|
|
||
| /** AST node represent Transpose operation. */ | ||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| @EqualsAndHashCode(callSuper = false) | ||
| @RequiredArgsConstructor | ||
| public class Transpose extends UnresolvedPlan { | ||
| private final @NonNull java.util.Map<String, Argument> arguments; | ||
| private UnresolvedPlan child; | ||
| private static final int max_limit = 1000; | ||
|
|
||
| public Integer getMaxRows() { | ||
| Integer maxRows = 5; | ||
| if (arguments.containsKey("number")) { | ||
| try { | ||
| maxRows = Integer.parseInt(arguments.get("number").getValue().toString()); | ||
| } catch (NumberFormatException e) { | ||
| // log warning and use default | ||
| maxRows = 5; | ||
| } | ||
| } | ||
| if (maxRows > max_limit) { | ||
| throw new IllegalArgumentException( | ||
| StringUtils.format("Maximum limit to transpose is %s", max_limit)); | ||
| } | ||
| return maxRows; | ||
| } | ||
|
|
||
| public String getColumnName() { | ||
| String columnName = "column"; | ||
| if (arguments.containsKey("columnName")) { | ||
| columnName = arguments.get("columnName").getValue().toString(); | ||
| } | ||
| return columnName; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public Transpose attach(UnresolvedPlan child) { | ||
| this.child = 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.visitTranspose(this, context); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| # transpose | ||
|
|
||
| ## Description | ||
|
|
||
| The `transpose` command outputs the requested number of rows as columns, effectively transposing each result row into a corresponding column of field values. | ||
|
|
||
| ## Syntax | ||
|
|
||
| transpose [int] [column_name=<string>] | ||
|
|
||
| * number-of-rows: optional. The number of rows to transform into columns. Default value is 5. Maximum allowed is 1000. | ||
| * column_name: optional. The name of the first column to use when transposing rows. This column holds the field names. | ||
|
|
||
|
|
||
| ## Example 1: Transpose results | ||
|
|
||
| This example shows transposing wihtout any parameters. It transforms 5 rows into columns as default is 5. | ||
|
|
||
| ```ppl | ||
| source=accounts | ||
| | head 5 | ||
| | fields account_number, firstname, lastname, balance | ||
| | transpose | ||
| ``` | ||
|
|
||
| Expected output: | ||
|
|
||
| ```text | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+-------+--------+---------+-------+-------+ | ||
| | column | row 1 | row 2 | row 3 | row 4 | row 5 | | ||
| |----------------+-------+--------+---------+-------+-------| | ||
| | account_number | 1 | 6 | 13 | 18 | null | | ||
| | firstname | Amber | Hattie | Nanette | Dale | null | | ||
| | balance | 39225 | 5686 | 32838 | 4180 | null | | ||
| | lastname | Duke | Bond | Bates | Adams | null | | ||
| +----------------+-------+--------+---------+-------+-------+ | ||
| ``` | ||
|
|
||
| ## Example 2: Tranpose results up to a provided number of rows. | ||
|
|
||
| This example shows transposing wihtout any parameters. It transforms 4 rows into columns as default is 5. | ||
|
|
||
| ```ppl | ||
| source=accounts | ||
| | head 5 | ||
| | fields account_number, firstname, lastname, balance | ||
| | transpose 4 | ||
| ``` | ||
|
|
||
| Expected output: | ||
|
|
||
| ```text | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+-------+--------+---------+-------+ | ||
| | column | row 1 | row 2 | row 3 | row 4 | | ||
| |----------------+-------+--------+---------+-------| | ||
| | account_number | 1 | 6 | 13 | 18 | | ||
| | firstname | Amber | Hattie | Nanette | Dale | | ||
| | balance | 39225 | 5686 | 32838 | 4180 | | ||
| | lastname | Duke | Bond | Bates | Adams | | ||
| +----------------+-------+--------+---------+-------+ | ||
| ``` | ||
|
|
||
| ## Example 2: Tranpose results up to a provided number of rows and first column with specified column name. | ||
|
|
||
| This example shows transposing wihtout any parameters. It transforms 4 rows into columns as default is 5. | ||
|
|
||
| ```ppl | ||
| source=accounts | ||
| | head 5 | ||
| | fields account_number, firstname, lastname, balance | ||
| | transpose 4 column_name='column_names' | ||
| ``` | ||
|
|
||
| Expected output: | ||
|
|
||
| ```text | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+-------+--------+---------+-------+ | ||
| | column_names | row 1 | row 2 | row 3 | row 4 | | ||
| |----------------+-------+--------+---------+-------| | ||
| | account_number | 1 | 6 | 13 | 18 | | ||
| | firstname | Amber | Hattie | Nanette | Dale | | ||
| | balance | 39225 | 5686 | 32838 | 4180 | | ||
| | lastname | Duke | Bond | Bates | Adams | | ||
| +----------------+-------+--------+---------+-------+ | ||
| ``` | ||
|
|
||
| ## Limitations | ||
|
|
||
| The `transpose` command transforms up to a number of rows specified and if not enough rows found, it shows those transposed rows as null columns. |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.