Merge pull request #1898 from molecula/sup-138

[SUP-138] add timestamp formatting to type FieldRow used in GroupBy
This commit is contained in:
tgruben 2022-01-31 14:52:54 -06:00 committed by GitHub
commit 8816583cd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View file

@ -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 != "" {

View file

@ -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 {