From b79537a4f0909a943132edf78f94137e4d01c79d Mon Sep 17 00:00:00 2001 From: Rahul Iyer Date: Mon, 30 Nov 2026 10:42:04 +0000 Subject: [PATCH] query results: fail -o json on a failed run, skip the empty table when rows are withheld Amp-Thread-ID: https://ampcode.com/threads/T-01a2147b-3c2e-76b3-aeaf-0aafbe53d879 Co-authored-by: Amp --- cmd/query/result_preview.go | 6 ++++++ cmd/query/results.go | 6 +++++- cmd/query/results_test.go | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/query/result_preview.go b/cmd/query/result_preview.go index 357355e..00287f0 100644 --- a/cmd/query/result_preview.go +++ b/cmd/query/result_preview.go @@ -48,6 +48,12 @@ func printExecutionResult(w io.Writer, r executionResult) { fmt.Fprintf(w, "State: %s\n", r.State) return } + // Withheld rows come back as a notice without data; a table would read as + // an empty result. + if r.Data == nil && r.Notice != "" { + fmt.Fprintln(w, r.Notice) + return + } var columns []string total := 0 credits := "" diff --git a/cmd/query/results.go b/cmd/query/results.go index c94450f..f2f6c48 100644 --- a/cmd/query/results.go +++ b/cmd/query/results.go @@ -65,7 +65,11 @@ func runResults(cmd *cobra.Command, args []string) error { } w := cmd.OutOrStdout() if output.FormatFromCmd(cmd) == output.FormatJSON { - return output.PrintRawJSON(w, raw) + // A failed stored run still fails the command, so scripts see it. + if err := output.PrintRawJSON(w, raw); err != nil { + return err + } + return res.Err() } if err := res.Err(); err != nil { return err diff --git a/cmd/query/results_test.go b/cmd/query/results_test.go index d426e72..03eda7d 100644 --- a/cmd/query/results_test.go +++ b/cmd/query/results_test.go @@ -44,6 +44,7 @@ func TestResultsWithheldRowsShowTheNotice(t *testing.T) { out, err := runResults(fake, "42") require.NoError(t, err) assert.Contains(t, out, "Rows withheld: licensed dataset") + assert.NotContains(t, out, "0 of 0 rows") } func TestResultsFailedRunIsAnError(t *testing.T) { @@ -51,6 +52,11 @@ func TestResultsFailedRunIsAnError(t *testing.T) { "executionId":"01E","state":"FAILED","errorMessage":"line 1: syntax error"}`}} _, err := runResults(fake, "42") assert.EqualError(t, err, "execution 01E failed: line 1: syntax error") + + // -o json prints the result and still fails, so scripts see the failure. + out, err := runResults(fake, "42", "-o", "json") + assert.EqualError(t, err, "execution 01E failed: line 1: syntax error") + assert.Contains(t, out, `"state": "FAILED"`) } func TestResultsRejectsBadQueryID(t *testing.T) {