GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(transpose): return an empty record for empty input with --as-record by Surri757 · Pull Request #19681 · nushell/nushell

Conversation

@Surri757

@Surri757 Surri757 commented Nov 28, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

fix(transpose): return an empty record for empty input with --as-record

Description

transpose --as-record only transfers the result to a record when the result has exactly one row, so an empty table falls through to the list branch: [] | transpose --as-record returns an empty list. The repro from #17605 still holds on 0.117: [] | transpose --as-record | describe gives list<any> (stream).

With --as-record the user asked for a record, so an empty table should come back as an empty record: [] | transpose --as-record now returns {} instead of [].

The inverse of transpose for records is transpose -r -d, and it round-trips an empty record: {} | transpose | transpose -r -d gives {}.

Root cause: the guard in crates/nu-command/src/filters/transpose.rs checked result_data.len() == 1, so the 0-row case fell into the else branch that returns the (empty) list. The guard now accepts 0 rows (result_data.len() <= 1) and produces an empty record for them. Non-empty inputs take exactly the same code paths as before.

The --as-record flag help text mentions the empty case now, so the generated command docs stay in sync with the behavior.

Fixes #17605

User-facing changes (Release notes)

transpose --as-record (-d) now returns an empty record instead of an empty list when the result has no rows: [] | transpose -d gives {} instead of []. This is a behavior change, and scripts that treat an empty transpose -d result as a list will break.

Commands downstream of an empty transpose -d result now receive a record instead of a list:

  • [] | transpose -d | length now errors with only_supports_this_input_type (input type: record) instead of returning 0
  • [] | transpose -d | each {|r| 'ran' } | to nuon now returns "ran" instead of []
  • [] | transpose -d | append 1 | to nuon now returns [{}, 1] instead of [1]

The empty record can also be produced from inputs that do have rows, when there is no column left to turn into a row:

  • [{}] | transpose -d | to nuon now returns {} instead of []
  • [[key]; [a] [b]] | transpose -r -d | to nuon now returns {} instead of []

Additional notes

Tests: cargo test --package nu-command --test tests -- commands::transpose. Before the fix, on main, the two new issue-repro tests fail as expected:

test commands::transpose::as_record_on_empty_list_returns_empty_record ... FAILED
test commands::transpose::as_record_round_trips_empty_record ... FAILED

---- commands::transpose::as_record_on_empty_list_returns_empty_record stdout ----
Error: TestError {
    kind: UnexpectedValue {
        expected: String("{}"),
        got: String("[]"),
    },
}

test result: FAILED. 12 passed; 2 failed; 0 ignored; 0 filtered out; across 1 groups, finished in 0.04s

With the fix, all 14 tests in the file pass, and the command's example tests (cargo test -p nu-command --lib transpose) pass too:

test result: ok. 14 passed; 0 failed; 0 ignored; 0 filtered out; across 1 groups, finished in 0.03s

The 4 new tests in crates/nu-command/tests/commands/transpose.rs are the issue's minimal repro ([] | transpose --as-record), the empty-record round trip ({} | transpose | transpose -r -d), and two regression tests pinning the unchanged non-empty paths (a single row still becomes a record, multiple rows still stay a table).
cargo fmt --all -- --check and cargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used both pass.

  • Drafted with MiniMax Code (AI assistance); the diff has not been reviewed line by line by a human yet.
  • nushell has no AI usage policy, so this disclosure is voluntary.
    This PR is submitted as a regular PR rather than a draft. Note that the workflows on a fork pull request still have to be approved by a maintainer before they run, so please click "Approve and run workflows" if you are able to.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Surri757
fix(transpose): return an empty record for empty input with --as-record
24472c0
## Description

transpose --as-record only transfers the result to a record when the result has
exactly one row, so an empty table falls through to the list branch:
`[] | transpose --as-record` returns an empty list. On 0.117 the repro from
nushell#17605 still holds: `[] | transpose --as-record | describe` gives
`list<any> (stream)`.

With --as-record the user asked for a record, so an empty table should come back
as an empty record: `[] | transpose --as-record` now returns `{}` instead of
`[]`. This also makes --as-record a proper inverse of transpose for empty
records: `{} | transpose | transpose --as-record` round-trips to `{}`.

The root cause is the guard in crates/nu-command/src/filters/transpose.rs: it
checked result_data.len() == 1, so the 0-row case fell into the else branch that
returns the (empty) list. The guard now accepts 0 rows and produces an empty
record for them; non-empty inputs are unchanged. The --as-record flag help text
mentions the empty case now, so the generated docs stay in sync.

Fixes nushell#17605

## User-facing changes (Release notes)

Fixed an issue where transpose --as-record returned an empty list instead of an
empty record when the input was an empty table, keeping it consistent with -d
as the inverse of transpose.

## Additional notes

4 new tests in crates/nu-command/tests/commands/transpose.rs: the issue minimal
repro, the empty-record round trip, and two regression tests pinning the
unchanged non-empty paths (single row stays a record, multiple rows stay a
table).

Drafted with MiniMax Code; the diff has not been reviewed line by line by a
human yet.
@Surri757

Surri757 commented Nov 28, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

This PR comes from a fork, so the workflows need a maintainer to approve running them - could someone click "Approve and run workflows"? The change makes transpose --as-record return an empty record (instead of an empty list) when the input table is empty, keeping it consistent as the inverse of transpose. Happy to adjust anything if you would rather have it differently.
@cptpiepmatz — could you approve the workflows when you get a chance? The change is a one-line guard in transpose.rs plus four tests; happy to adjust anything.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fdncred
fdncred reviewed Nov 28, 2026
View reviewed changes

@fdncred fdncred left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

Review overview

🟡 Changes recommended

The fix works: [] | transpose -d gives {}, and the new tests, the example tests and clippy pass. It also makes [] | transpose -dr | load-env work, the pipe that std-rfc's kv universal-variable-hook ends in. Two small things are left: the release note should call out the behavior change, and the round-trip test should use -r -d.

Findings: 0 High severity · 0 Medium severity · 2 Low severity

Open (2)
  • Low severity Release note should flag the behavior change
  • Low severity Round-trip test leaves out -r

Notes from my clanker, checked on 24472c0. Built the branch and ran cargo test -p nu-command --test tests -- commands::transpose (14 passed), cargo test -p nu-command --lib transpose (2 passed), clippy with CI's flags (clean), cargo fmt --all --check and typos (clean). CI passed on this head too. Each case in the threads ran on the branch binary and on 0.117.1 (dc352ef).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread crates/nu-command/src/filters/transpose.rs
})
.collect::<Vec<Value>>();
if result_data.len() == 1 && args.as_record {
if args.as_record && result_data.len() <= 1 {

@fdncred fdncred Nov 28, 2026

Copy link
Copy Markdown
Contributor

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.

Low severity Release note should flag the behavior change

On 24472c0, any transpose -d whose result has no rows returns {} instead of []. Commands downstream that expected a list now act differently:

[] | transpose -d | length
# main:   0
# branch: Error: nu::shell::only_supports_this_input_type (input type: record)

[] | transpose -d | each {|r| 'ran' } | to nuon
# main:   []
# branch: "ran"

[] | transpose -d | append 1 | to nuon
# main:   [1]
# branch: [{}, 1]

Input with rows can end up here too, whenever no column is left to turn into a row:

[{}] | transpose -d | to nuon                  # main: []  branch: {}
[[key]; [a] [b]] | transpose -r -d | to nuon   # main: []  branch: {}

The code doesn't need to change for this. The release note should say that scripts which treat an empty transpose -d result as a list will break.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Surri757 Surri757 Nov 28, 2026

Copy link
Copy Markdown
Author

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.

@C:\Users\38235.minimax\bunny\spaces\GitHub-PR-反馈修改-2df9c5b7-dd63-4cc5-825c-7a6a607f837c\artifacts\f231d7a9-nushell-19681-维护者评审意见处理\reply-1-release-note.txt

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread crates/nu-command/tests/commands/transpose.rs
Comment on lines +106 to +110
// --as-record is the inverse of transpose for records, so the
// round trip of an empty record has to stay an empty record.
test()
.run("{} | transpose | transpose --as-record | to nuon")
.expect_value_eq("{}")

@fdncred fdncred Nov 28, 2026

Copy link
Copy Markdown
Contributor

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.

Low severity Round-trip test leaves out -r

The comment says --as-record is the inverse of transpose. On its own it isn't, so this test pins a round trip that only holds for {}:

{a: 1} | transpose | transpose --as-record | to nuon
# [["column0", "column1"]; ["column0", a], ["column1", 1]]

{a: 1} | transpose | transpose -r -d | to nuon
# {a: 1}

The inverse needs -r, as in the issue's ($record | transpose | transpose -rd) == $record and the transpose --ignore-titles -r -d example. Testing that form also covers the --header-row path with empty input, and it gives {} on the branch:

Suggested change
// --as-record is the inverse of transpose for records, so the
// round trip of an empty record has to stay an empty record.
test()
.run("{} | transpose | transpose --as-record | to nuon")
.expect_value_eq("{}")
// `transpose -r -d` is the inverse of `transpose` for records, so the
// round trip of an empty record has to stay an empty record.
test()
.run("{} | transpose | transpose -r -d | to nuon")
.expect_value_eq("{}")

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@fdncred fdncred fdncred left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

transpose --as-record returns empty list when input is empty list

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Surri757 @fdncred
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.