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
27
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.
+828
−3
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d96741c
transpose command implementation
asifabashar 553f271
transpose rows to columns
asifabashar cee333c
added argument type missing map and hashmap
asifabashar f3e1c2c
added tests
asifabashar 3e09ff9
added tests
asifabashar 74222be
added tests
asifabashar fd65a6a
added tests
asifabashar 5705880
added tests
asifabashar d25027f
added tests
asifabashar 391da92
added tests
asifabashar 6362dbd
added tests
asifabashar b1f496d
added more validations
asifabashar 1e49063
added validation
asifabashar ae17ccd
index.md formatting fix
asifabashar fc7b259
doc format
asifabashar 87497b8
coderabbit review fixes
asifabashar 6598cc6
added recommended changes
asifabashar fa411c6
added recommended changes
asifabashar b7af26b
for cross cluster failure debugging
asifabashar 4335727
for cross cluster failure debugging
asifabashar 9db3989
for cross cluster failure debugging
asifabashar 469f47e
trim columnName
asifabashar 528f0b3
per review moved to class varialble.
asifabashar 32dee64
per review moved to class varialble.
asifabashar 2de1009
added field resolution
asifabashar d7ce9cf
fix by removing metadata field
asifabashar 804272e
fixed explain test after removing of metadata fields in transpose result
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
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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
| /* | ||
| * 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) | ||
| public class Transpose extends UnresolvedPlan { | ||
| private final @NonNull java.util.Map<String, Argument> arguments; | ||
| private UnresolvedPlan child; | ||
| private static final int MAX_LIMIT_TRANSPOSE = 10000; | ||
| private static final int DEFAULT_MAX_ROWS = 5; | ||
| private static final String DEFAULT_COLUMN_NAME = "column"; | ||
| private final int maxRows; | ||
| private final String columnName; | ||
|
|
||
| public Transpose(java.util.Map<String, Argument> arguments) { | ||
|
|
||
| this.arguments = arguments; | ||
| int tempMaxRows = DEFAULT_MAX_ROWS; | ||
| if (arguments.containsKey("number") && arguments.get("number").getValue() != null) { | ||
| try { | ||
| tempMaxRows = Integer.parseInt(arguments.get("number").getValue().toString()); | ||
| } catch (NumberFormatException e) { | ||
| // log warning and use default | ||
|
|
||
| } | ||
| } | ||
| maxRows = tempMaxRows; | ||
| if (maxRows > MAX_LIMIT_TRANSPOSE) { | ||
| throw new IllegalArgumentException( | ||
| StringUtils.format("Maximum limit to transpose is %s", MAX_LIMIT_TRANSPOSE)); | ||
| } | ||
| if (arguments.containsKey("columnName") && arguments.get("columnName").getValue() != null) { | ||
| columnName = arguments.get("columnName").getValue().toString(); | ||
| } else { | ||
| columnName = DEFAULT_COLUMN_NAME; | ||
| } | ||
| } | ||
|
|
||
| @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
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 10000. | ||
| * 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. | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```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. | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```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. | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```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.