github.com/m3db/m3@v1.5.0/src/query/plan/logical_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package plan
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/query/functions"
    27  	"github.com/m3db/m3/src/query/functions/aggregation"
    28  	"github.com/m3db/m3/src/query/parser"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestSingleChildParentRelation(t *testing.T) {
    35  	fetchTransform := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
    36  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
    37  	require.NoError(t, err)
    38  	countTransform := parser.NewTransformFromOperation(agg, 2)
    39  	transforms := parser.Nodes{fetchTransform, countTransform}
    40  	edges := parser.Edges{
    41  		parser.Edge{
    42  			ParentID: fetchTransform.ID,
    43  			ChildID:  countTransform.ID,
    44  		},
    45  	}
    46  
    47  	lp, err := NewLogicalPlan(transforms, edges)
    48  	require.NoError(t, err)
    49  	assert.Len(t, lp.Steps[countTransform.ID].Parents, 1)
    50  	assert.Len(t, lp.Steps[fetchTransform.ID].Children, 1)
    51  	assert.Len(t, lp.Steps[fetchTransform.ID].Parents, 0)
    52  	assert.Len(t, lp.Steps[countTransform.ID].Children, 0)
    53  	assert.Equal(t, lp.Steps[fetchTransform.ID].Children[0], countTransform.ID)
    54  	assert.Equal(t, lp.Steps[countTransform.ID].Parents[0], fetchTransform.ID)
    55  	assert.Equal(t, lp.Steps[countTransform.ID].ID(), countTransform.ID)
    56  	// Will get better once we implement ops. Then we can test for existence of ops
    57  	assert.Contains(t, lp.String(), "Parents")
    58  }
    59  
    60  func TestSingleParentMultiChild(t *testing.T) {
    61  	fetchTransform := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
    62  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
    63  	require.NoError(t, err)
    64  	countTransform1 := parser.NewTransformFromOperation(agg, 2)
    65  	countTransform2 := parser.NewTransformFromOperation(agg, 3)
    66  	transforms := parser.Nodes{fetchTransform, countTransform1, countTransform2}
    67  	edges := parser.Edges{
    68  		parser.Edge{
    69  			ParentID: fetchTransform.ID,
    70  			ChildID:  countTransform1.ID,
    71  		},
    72  		parser.Edge{
    73  			ParentID: fetchTransform.ID,
    74  			ChildID:  countTransform2.ID,
    75  		},
    76  	}
    77  
    78  	lp, err := NewLogicalPlan(transforms, edges)
    79  	require.NoError(t, err)
    80  	assert.Len(t, lp.Steps[countTransform1.ID].Parents, 1)
    81  	assert.Len(t, lp.Steps[fetchTransform.ID].Children, 2)
    82  	assert.Len(t, lp.Steps[fetchTransform.ID].Parents, 0)
    83  	assert.Len(t, lp.Steps[countTransform2.ID].Parents, 1)
    84  	assert.Equal(t, lp.Steps[countTransform1.ID].Parents[0], lp.Steps[countTransform2.ID].Parents[0])
    85  }
    86  
    87  func TestMultiParent(t *testing.T) {
    88  	fetchTransform1 := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
    89  	fetchTransform2 := parser.NewTransformFromOperation(functions.FetchOp{}, 2)
    90  	// TODO: change this to a real multi parent operation such as asPercent
    91  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
    92  	require.NoError(t, err)
    93  	countTransform := parser.NewTransformFromOperation(agg, 3)
    94  
    95  	transforms := parser.Nodes{fetchTransform1, fetchTransform2, countTransform}
    96  	edges := parser.Edges{
    97  		parser.Edge{
    98  			ParentID: fetchTransform1.ID,
    99  			ChildID:  countTransform.ID,
   100  		},
   101  		parser.Edge{
   102  			ParentID: fetchTransform2.ID,
   103  			ChildID:  countTransform.ID,
   104  		},
   105  	}
   106  
   107  	lp, err := NewLogicalPlan(transforms, edges)
   108  	require.NoError(t, err)
   109  	assert.Len(t, lp.Steps[countTransform.ID].Parents, 2)
   110  	assert.Len(t, lp.Steps[fetchTransform1.ID].Children, 1)
   111  	assert.Len(t, lp.Steps[fetchTransform2.ID].Children, 1)
   112  }