Skip to content

Handle scalar rows in CSV export - #224

Merged
Mike Krüger (mkrueger) merged 3 commits into
mainfrom
dev/mkrueger/fix-csv-export-scalars
Oct 2, 2026
Merged

Mike Krüger (mkrueger) merged 3 commits into
mainfrom
dev/mkrueger/fix-csv-export-scalars

Conversation

@mkrueger

@mkrueger Mike Krüger (mkrueger) commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

export --format=csv collected CSV headers only from JSON object properties. Rows whose value is not an object (for example export out.csv --format=csv --query="SELECT VALUE c.id FROM c", or numbers, booleans, null, arrays) were written as empty lines, while the command still reported the exported row count.

Change

  • Non-object rows are written to a column with an empty header by default; set COSMOSDB_SHELL_CSV_SCALAR_COLUMN to name it, matching the existing scalar CSV rendering in CommandState.Table.cs (JsonElement.ToString() escaped with CommandState.EscapeCSV).
  • Mixed result sets keep both the object property columns and the scalar column, so no row loses data.
  • Object-only exports are unchanged.
  • docs/commands.md describes the empty default header, environment-variable override, and mixed results. Header collisions produce an error instead of combining values.

Tests in ExportCommandTests cover scalar strings that need escaping, numbers, booleans, null, arrays, object-only rows, mixed rows, and the default/environment-variable lookup.

Examples

CSV output below uses the default ; separator.

1. Scalar-only results

Container documents: {"id":"1"}, {"id":"2"}, {"id":"3"}

export ids.csv --format=csv --query="SELECT VALUE c.id FROM c"

Before (no header, one empty line per row, while the command still reported 3 exported rows):




After:

""
"1"
"2"
"3"

2. Mixed object and scalar results

Container documents:

{"id":"1","address":{"city":"Berlin","zip":"10115"}}
{"id":"2","address":"Hamburg; Hafen"}
{"id":"3","address":{"city":"Munich"}}
export addresses.csv --format=csv --query="SELECT VALUE c.address FROM c"

Before (the string address from document 2 was lost):

"city";"zip"
"Berlin";"10115"
"";""
"Munich";""

After (object columns are kept and the scalar goes into the empty-header column, escaped as usual):

"city";"zip";""
"Berlin";"10115";""
"";"";"Hamburg; Hafen"
"Munich";"";""

Add a value column for non-object CSV export rows so scalar and mixed query results keep their data while preserving existing object row behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The targeted implementation matches existing rendering behavior and is adequately documented and tested.

Review effort: Balanced
Findings: None

What changed in this PR

Adds CSV export support for scalar and mixed query results.

Changes:

  • Writes non-object rows to a value column.
  • Preserves object-only behavior and supports mixed rows.
  • Adds documentation and comprehensive tests.
File Description
docs/​commands.md Documents scalar and mixed CSV output.
ExportCommand.cs Adds scalar-value column generation.
ExportCommandTests.cs Covers scalar, object, array, and mixed rows.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-code-quality

github-code-quality Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall line coverage in commit 754d082 in the dev/mkrueger/fix-csv... branch is 65%. The line coverage in commit 6992beb in the main branch is 66%.

Show a line coverage summary of the most impacted files.
File main 6992beb dev/mkrueger/fix-csv... 754d082 +/-
D:\a\CosmosDBSh...akeDbCommand.cs 92% 38% -54%
D:\a\CosmosDBSh...reateCommand.cs 78% 26% -52%
D:\a\CosmosDBSh...ainerCommand.cs 85% 35% -50%
D:\a\CosmosDBSh...urceProvider.cs 62% 33% -29%
D:\a\CosmosDBSh...ceOperations.cs 20% 3% -17%
D:\a\CosmosDBSh...sourceFacade.cs 27% 15% -12%
D:\a\CosmosDBSh...ubscriptions.cs 89% 86% -3%
D:\a\CosmosDBSh...osmosCommand.cs 42% 40% -2%
D:\a\CosmosDBSh...xportCommand.cs 62% 65% +3%
D:\a\CosmosDBSh...mportCommand.cs 46% 50% +4%

Updated October 02, 2026 08:09 UTC

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 12:49
Comment thread CosmosDBShell.Tests/CommandTests/ImportCommandTests.cs Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The implemented empty default header contradicts the documented value column contract, and the environment-variable path lacks direct coverage.

Review effort: Balanced
Findings: 1 Low severity

Open (1)

Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/ExportCommand.cs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 08:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation, documentation, localization catalog, and focused regression coverage are consistent and complete.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@mkrueger
Mike Krüger (mkrueger) merged commit 6011539 into main Oct 2, 2026
12 checks passed
@mkrueger
Mike Krüger (mkrueger) deleted the dev/mkrueger/fix-csv-export-scalars branch October 2, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants