From d7c082b51575f86bc3be029107beea62f05004f6 Mon Sep 17 00:00:00 2001 From: Todd Gruben Date: Mon, 31 Jan 2022 12:32:05 -0600 Subject: [PATCH] add timestamp formating to type FieldRow used in GroupBy --- executor.go | 25 ++++++++++++++++++------- executor_test.go | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/executor.go b/executor.go index 24e36af61..4409637a0 100644 --- a/executor.go +++ b/executor.go @@ -3189,13 +3189,24 @@ func (fr *FieldRow) Clone() (clone *FieldRow) { // either a Key or an ID is included. func (fr FieldRow) MarshalJSON() ([]byte, error) { if fr.Value != nil { - return json.Marshal(struct { - Field string `json:"field"` - Value int64 `json:"value"` - }{ - Field: fr.Field, - Value: *fr.Value, - }) + if fr.FieldOptions.Type == FieldTypeTimestamp { + ts := FormatTimestampNano(int64(*fr.Value), fr.FieldOptions.Base, fr.FieldOptions.TimeUnit) + return json.Marshal(struct { + Field string `json:"field"` + Value string `json:"value"` + }{ + Field: fr.Field, + Value: ts, + }) + } else { + return json.Marshal(struct { + Field string `json:"field"` + Value int64 `json:"value"` + }{ + Field: fr.Field, + Value: *fr.Value, + }) + } } if fr.RowKey != "" { diff --git a/executor_test.go b/executor_test.go index 15d792987..1c534ac52 100644 --- a/executor_test.go +++ b/executor_test.go @@ -3490,6 +3490,28 @@ func TestExecutor_Execute_Remote_Row(t *testing.T) { } }) + t.Run("json format groupBy on timestamps", func(t *testing.T) { + //SUP-138 + c.CreateField(t, "t", pilosa.IndexOptions{TrackExistence: true}, "timestamp", pilosa.OptFieldTypeTimestamp(pilosa.DefaultEpoch, pilosa.TimeUnitSeconds)) + c.Query(t, "t", ` + Set(8, timestamp='2021-01-27T08:00:00Z') + Set(9, timestamp='2000-01-27T09:00:00Z') + Set(10, timestamp='2000-01-27T10:00:00Z') + `) + if res, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{ + Index: "t", + Query: `GroupBy(Rows(timestamp))`, + }); err != nil { + t.Fatalf("GroupBy querying: %v", err) + } else { + b, _ := res.MarshalJSON() + expected := `{"results":[[{"group":[{"field":"timestamp","value":"2000-01-27T09:00:00Z"}],"count":1},{"group":[{"field":"timestamp","value":"2000-01-27T10:00:00Z"}],"count":1},{"group":[{"field":"timestamp","value":"2021-01-27T08:00:00Z"}],"count":1}]]}` + if string(b) != expected { + t.Fatalf("JSON FORMAT not as expected: %v", err) + } + } + }) + t.Run("remote groupBy on ints", func(t *testing.T) { _, err = c.GetPrimary().API.CreateField(context.Background(), "i", "fint", pilosa.OptFieldTypeInt(-1000, 1000)) if err != nil {