Inconsistent format with "--output table" and "--query"
- Dominant language
- Python
- Stars
- 17.3k
- Forks
- 4.6k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 13
Description
### Describe the bug
When using the option `--output table` together with `--query`, no matter what query you make, if the result is just one (1) row, the table is formatted one way (faulty) but if the result is two lines or more (>2), the table is formatted correctly.
The bug is this inconsistency. You can't batch-process the results (like cut:ing a particular column or using sed to post-process the output) since their format dynamically differ.
Example of a single set (1) of output (faulty table output):
```
# aws rds describe-db-cluster-snapshots --query 'DBClusterSnapshots[*].{c1_Id:DBClusterSnapshotIdentifier,c2_Size:AllocatedStorage,c3_Time:SnapshotCreateTime} | sort_by(@, &c3_Time)' --output table
-----------------------------------------------------------------
| DescribeDBClusterSnapshots |
+---------+-----------------------------------------------------+
| c1_Id | database-1-instance-1-eu-north-1b-final-snapshot |
| c2_Size| 0 |
| c3_Time| 2019-11-15T15:04:40.902000+00:00 |
+---------+-----------------------------------------------------+
```
This is not what you expect from a table. The three columns in the single data set should be **columns**, not rows.
Example of the same query, but now with four (4) sets of output (correct table output):
```
# aws rds describe-db-cluster-snapshots --query 'DBClusterSnapshots[*].{c1_Id:DBClusterSnapshotIdentifier,c2_Size:AllocatedStorage,c3_Time:SnapshotCreateTime} | sort_by(@, &c3_Time)' --output table
-------------------------------------------------------------------------------------------------------
| DescribeDBClusterSnapshots |
+-----------------------------------------------------+----------+------------------------------------+
| c1_Id | c2_Size | c3_Time |
+-----------------------------------------------------+----------+------------------------------------+
| devdb-2023-03-08-05-45-snapshot | 6 | 2022-03-08T09:35:56.124000+00:00 |
| devdb-1-restore-cluster-snapshot | 6 | 2022-03-08T10:03:21.712000+00:00 |
| maindb-cluster-test-2-snapshot | 108 | 2022-03-21T11:57:34.037000+00:00 |
| preupgrade-devdbs-10-21-to-12-12-2023-06-28-14-02 | 7 | 2023-06-28T14:07:56.890000+00:00 |
+-----------------------------------------------------+----------+------------------------------------+
```
This is correct. The output is formatted in a table as expected.
I redirect the output from the commands to a file, `>> cluster-snapshots.txt`.
Later I run `egrep '2019-|2020-|2021-|2022-' cluster-snapshots.txt` and expect to see the name, size and snapshot-date of old (prior to the year 2023) db-snapshots that I should delete, but the result is broken:
```
| c3_Time| 2019-11-15T15:04:40.902000+00:00 |
| devdb-2023-03-08-05-45-snapshot | 6 | 2022-03-08T09:35:56.124000+00:00 |
| devdb-1-restore-cluster-snapshot | 6 | 2022-03-08T10:03:21.712000+00:00 |
| maindb-cluster-test-2-snapshot | 108 | 2022-03-21T11:57:34.037000+00:00 |
```
### Expected Behavior
The table should always have each field in a column, even if it is just one set (one row) of results.
It is expected that the table output from a CLI tool is always consistent.
The output from the first example should look like this:
```
# aws rds describe-db-cluster-snapshots --query 'DBClusterSnapshots[*].{c1_Id:DBClusterSnapshotIdentifier,c2_Size:AllocatedStorage,c3_Time:SnapshotCreateTime} | sort_by(@, &c3_Time)' --output table
-------------------------------------------------------------------------------------------------------
| DescribeDBClusterSnapshots |
+-----------------------------------------------------+----------+------------------------------------+
| c1_Id | c2_Size | c3_Time |
+-----------------------------------------------------+----------+------------------------------------+
| database-1-instance-1-eu-north-1b-final-snapshot | 0 | 2019-11-15T15:04:40.902000+00:00 |
+-----------------------------------------------------+----------+------------------------------------+
```
### Current Behavior
One (1) set of results are listed over multiple rows in a key:value style approach while multiple sets are listed as rows and columns.
### Reproduction Steps
See examples above.
### Possible Solution
_No response_
### Additional Information/Context
aws-cli v1 and v2 has the same bug.
### CLI version used
aws-cli v1 and the same thing in v2.
### Environment details (OS name and version, etc.)
Debian linux 11
Contributor guide
Assessment
This issue has not been assessed yet.