Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] Fix temporal function tag application bug #2231

Merged
merged 9 commits into from
Apr 2, 2020
29 changes: 28 additions & 1 deletion src/cmd/services/m3comparator/main/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package parser

import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"time"
)
Expand All @@ -45,10 +47,35 @@ type Datapoints []Datapoint

// Datapoint is a JSON serializeable datapoint for the series.
type Datapoint struct {
Value float64 `json:"val"`
Value Value `json:"val"`
Timestamp time.Time `json:"ts"`
}

// Value is a JSON serizlizable float64 that allows NaNs.
type Value float64

// MarshalJSON returns state as the JSON encoding of a Value.
func (v Value) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%g", float64(v)))
}

// UnmarshalJSON unmarshals JSON-encoded data into a Value.
func (v *Value) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}

f, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}

*v = Value(f)
return nil
}

// IDOrGenID gets the ID for this result.
func (r *Series) IDOrGenID() string {
if len(r.id) == 0 {
Expand Down
59 changes: 59 additions & 0 deletions src/cmd/services/m3comparator/main/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The abovale copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package parser

import (
"encoding/json"
"math"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestElectionStateJSONMarshal(t *testing.T) {
for _, input := range []struct {
val Value
str string
}{
{val: 1231243.123123, str: `"1.231243123123e+06"`},
{val: 0.000000001, str: `"1e-09"`},
{val: Value(math.NaN()), str: `"NaN"`},
{val: Value(math.Inf(1)), str: `"+Inf"`},
{val: Value(math.Inf(-1)), str: `"-Inf"`},
} {
b, err := json.Marshal(input.val)
require.NoError(t, err)
assert.Equal(t, input.str, string(b))

var val Value
json.Unmarshal([]byte(input.str), &val)
if math.IsNaN(float64(input.val)) {
assert.True(t, math.IsNaN(float64(val)))
} else if math.IsInf(float64(input.val), 1) {
assert.True(t, math.IsInf(float64(val), 1))
} else if math.IsInf(float64(input.val), -1) {
assert.True(t, math.IsInf(float64(val), -1))
} else {
assert.Equal(t, input.val, val)
}
}
}
2 changes: 2 additions & 0 deletions src/query/block/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func (cb ColumnBlockBuilder) PopulateColumns(size int) {
for i := range cb.block.columns {
cb.block.columns[i] = column{Values: cols[size*i : size*(i+1)]}
}

cb.block.seriesMeta = make([]SeriesMeta, size)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add test coverage that this is always size of cols now? Seems like was just missing before?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, luckily the new test fails without this statement 👍

}

// SetRow sets a given block row to the given values and metadata.
Expand Down
67 changes: 63 additions & 4 deletions src/query/block/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,83 @@ package block

import (
"context"
"fmt"
"testing"
"time"

"github.com/m3db/m3/src/query/cost"
"github.com/m3db/m3/src/query/models"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally"
)

func TestColumnBuilderInfoTypes(t *testing.T) {
ctx := models.NewQueryContext(context.Background(),
tally.NoopScope, cost.NoopChainedEnforcer(),
models.QueryContextOptions{})
var ctx = models.NewQueryContext(context.Background(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

super nit: Maybe make this a method? In case contexts conflict across tests?

tally.NoopScope, cost.NoopChainedEnforcer(),
models.QueryContextOptions{})

func TestColumnBuilderInfoTypes(t *testing.T) {
builder := NewColumnBlockBuilder(ctx, Metadata{}, []SeriesMeta{})
block := builder.Build()
assert.Equal(t, BlockDecompressed, block.Info().blockType)

block = builder.BuildAsType(BlockScalar)
assert.Equal(t, BlockScalar, block.Info().blockType)
}

func TestSetRow(t *testing.T) {
buildMeta := func(i int) SeriesMeta {
name := fmt.Sprint(i)

return SeriesMeta{
Name: []byte(name),
Tags: models.MustMakeTags("name", name),
}
}

size := 10
metas := make([]SeriesMeta, size)
for i := range metas {
metas[i] = buildMeta(i)
}

builder := NewColumnBlockBuilder(ctx, Metadata{
Bounds: models.Bounds{StepSize: time.Minute, Duration: time.Minute},
}, metas)

require.NoError(t, builder.AddCols(1))
builder.PopulateColumns(size)
// NB: set the row metas backwards.
j := 0
for i := size - 1; i >= 0; i-- {
err := builder.SetRow(j, []float64{float64(i)}, metas[i])
require.NoError(t, err)
j++
}

bl := builder.Build()
it, err := bl.StepIter()
require.NoError(t, err)

actualMetas := it.SeriesMeta()
for i, m := range actualMetas {
ex := fmt.Sprint(size - 1 - i)
assert.Equal(t, ex, string(m.Name))
require.Equal(t, 1, m.Tags.Len())
tag, found := m.Tags.Get([]byte("name"))
require.True(t, found)
assert.Equal(t, ex, string(tag))
}

assert.True(t, it.Next())
exVals := make([]float64, size)
for i := range exVals {
exVals[i] = float64(size - 1 - i)
}

vals := it.Current().Values()
assert.Equal(t, exVals, vals)
assert.False(t, it.Next())
assert.NoError(t, it.Err())
}
Loading