github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/executor/state_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 executor
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/query/functions"
    28  	"github.com/m3db/m3/src/query/functions/aggregation"
    29  	"github.com/m3db/m3/src/query/models"
    30  	"github.com/m3db/m3/src/query/parser"
    31  	"github.com/m3db/m3/src/query/plan"
    32  	"github.com/m3db/m3/src/query/storage"
    33  	"github.com/m3db/m3/src/query/storage/mock"
    34  	"github.com/m3db/m3/src/x/instrument"
    35  
    36  	"github.com/stretchr/testify/assert"
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  var (
    41  	defaultLookbackDuration = time.Minute
    42  )
    43  
    44  func testRequestParams() models.RequestParams {
    45  	return models.RequestParams{
    46  		Now:              time.Now(),
    47  		LookbackDuration: defaultLookbackDuration,
    48  		Step:             time.Second,
    49  	}
    50  }
    51  
    52  func TestValidState(t *testing.T) {
    53  	fetchTransform := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
    54  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
    55  	require.NoError(t, err)
    56  	countTransform := parser.NewTransformFromOperation(agg, 2)
    57  	transforms := parser.Nodes{fetchTransform, countTransform}
    58  	edges := parser.Edges{
    59  		parser.Edge{
    60  			ParentID: fetchTransform.ID,
    61  			ChildID:  countTransform.ID,
    62  		},
    63  	}
    64  
    65  	lp, err := plan.NewLogicalPlan(transforms, edges)
    66  	require.NoError(t, err)
    67  	store := mock.NewMockStorage()
    68  	p, err := plan.NewPhysicalPlan(lp, testRequestParams())
    69  	require.NoError(t, err)
    70  	state, err := GenerateExecutionState(p, store, storage.NewFetchOptions(),
    71  		instrument.NewOptions())
    72  	require.NoError(t, err)
    73  	require.Len(t, state.sources, 1)
    74  	err = state.Execute(models.NoopQueryContext())
    75  	assert.NoError(t, err)
    76  }
    77  
    78  func TestWithoutSources(t *testing.T) {
    79  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
    80  	require.NoError(t, err)
    81  	countTransform := parser.NewTransformFromOperation(agg, 2)
    82  	transforms := parser.Nodes{countTransform}
    83  	edges := parser.Edges{}
    84  	lp, err := plan.NewLogicalPlan(transforms, edges)
    85  	require.NoError(t, err)
    86  	p, err := plan.NewPhysicalPlan(lp, testRequestParams())
    87  	require.NoError(t, err)
    88  	_, err = GenerateExecutionState(p, nil, storage.NewFetchOptions(), instrument.NewOptions())
    89  	assert.Error(t, err)
    90  }
    91  
    92  func TestOnlySources(t *testing.T) {
    93  	fetchTransform := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
    94  	transforms := parser.Nodes{fetchTransform}
    95  	edges := parser.Edges{}
    96  	lp, err := plan.NewLogicalPlan(transforms, edges)
    97  	require.NoError(t, err)
    98  	p, err := plan.NewPhysicalPlan(lp, testRequestParams())
    99  	require.NoError(t, err)
   100  	state, err := GenerateExecutionState(p, nil, storage.NewFetchOptions(),
   101  		instrument.NewOptions())
   102  	assert.NoError(t, err)
   103  	require.Len(t, state.sources, 1)
   104  }
   105  
   106  func TestMultipleSources(t *testing.T) {
   107  	fetchTransform1 := parser.NewTransformFromOperation(functions.FetchOp{}, 1)
   108  	agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{})
   109  	require.NoError(t, err)
   110  	countTransform := parser.NewTransformFromOperation(agg, 2)
   111  	fetchTransform2 := parser.NewTransformFromOperation(functions.FetchOp{}, 3)
   112  	transforms := parser.Nodes{fetchTransform1, fetchTransform2, countTransform}
   113  	edges := parser.Edges{
   114  		parser.Edge{
   115  			ParentID: fetchTransform1.ID,
   116  			ChildID:  countTransform.ID,
   117  		},
   118  		parser.Edge{
   119  			ParentID: fetchTransform2.ID,
   120  			ChildID:  countTransform.ID,
   121  		},
   122  	}
   123  
   124  	lp, err := plan.NewLogicalPlan(transforms, edges)
   125  	require.NoError(t, err)
   126  	p, err := plan.NewPhysicalPlan(lp, testRequestParams())
   127  	require.NoError(t, err)
   128  	state, err := GenerateExecutionState(p, nil, storage.NewFetchOptions(),
   129  		instrument.NewOptions())
   130  	assert.NoError(t, err)
   131  	require.Len(t, state.sources, 2)
   132  	assert.Contains(t, state.String(), "sources")
   133  }