github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/plan/physical_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 "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 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func testRequestParams() models.RequestParams { 37 return models.RequestParams{ 38 Now: time.Now(), 39 LookbackDuration: 5 * time.Minute, 40 Step: time.Second, 41 } 42 } 43 44 func TestResultNode(t *testing.T) { 45 fetchTransform := parser.NewTransformFromOperation(functions.FetchOp{}, 1) 46 agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{}) 47 require.NoError(t, err) 48 countTransform := parser.NewTransformFromOperation(agg, 2) 49 transforms := parser.Nodes{fetchTransform, countTransform} 50 edges := parser.Edges{ 51 parser.Edge{ 52 ParentID: fetchTransform.ID, 53 ChildID: countTransform.ID, 54 }, 55 } 56 57 lp, err := NewLogicalPlan(transforms, edges) 58 require.NoError(t, err) 59 p, err := NewPhysicalPlan(lp, testRequestParams()) 60 require.NoError(t, err) 61 node, err := p.leafNode() 62 require.NoError(t, err) 63 assert.Equal(t, node.ID(), countTransform.ID) 64 assert.Equal(t, p.ResultStep.Parent, countTransform.ID) 65 } 66 67 func TestShiftTime(t *testing.T) { 68 tests := []struct { 69 name string 70 fetchOp functions.FetchOp 71 lookbackDuration time.Duration 72 step time.Duration 73 wantShiftBy time.Duration 74 }{ 75 { 76 name: "shift by lookbackDuration", 77 fetchOp: functions.FetchOp{}, 78 lookbackDuration: 15 * time.Minute, 79 step: time.Second, 80 wantShiftBy: 15 * time.Minute, 81 }, 82 { 83 name: "shift by range", 84 fetchOp: functions.FetchOp{Range: time.Hour}, 85 lookbackDuration: 5 * time.Minute, 86 step: time.Second, 87 wantShiftBy: time.Hour, 88 }, 89 { 90 name: "align the lookback based shift by step", 91 fetchOp: functions.FetchOp{}, 92 lookbackDuration: 5 * time.Second, 93 step: 15 * time.Second, 94 wantShiftBy: 15 * time.Second, // lookback = 5, aligned to 1x step (15) 95 }, 96 { 97 name: "align the range based shift by step", 98 fetchOp: functions.FetchOp{Range: 16 * time.Second}, 99 lookbackDuration: 5 * time.Second, 100 step: 15 * time.Second, 101 wantShiftBy: 30 * time.Second, // range = 16, aligned to 2x step (2 * 15) 102 }, 103 { 104 name: "keep the same shift if already aligned by step", 105 fetchOp: functions.FetchOp{Range: 30 * time.Second}, 106 lookbackDuration: 5 * time.Second, 107 step: 15 * time.Second, 108 wantShiftBy: 30 * time.Second, // range = 30, divisible by step 109 }, 110 } 111 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 115 fetchTransform := parser.NewTransformFromOperation(tt.fetchOp, 1) 116 agg, err := aggregation.NewAggregationOp(aggregation.CountType, aggregation.NodeParams{}) 117 require.NoError(t, err) 118 119 countTransform := parser.NewTransformFromOperation(agg, 2) 120 transforms := parser.Nodes{fetchTransform, countTransform} 121 edges := parser.Edges{ 122 parser.Edge{ 123 ParentID: fetchTransform.ID, 124 ChildID: countTransform.ID, 125 }, 126 } 127 128 lp, _ := NewLogicalPlan(transforms, edges) 129 130 params := models.RequestParams{ 131 Now: time.Now(), 132 LookbackDuration: tt.lookbackDuration, 133 Step: tt.step, 134 } 135 136 p, err := NewPhysicalPlan(lp, params) 137 require.NoError(t, err) 138 assert.Equal(t, tt.wantShiftBy.String(), params.Start.Sub(p.TimeSpec.Start).String(), "start time shifted by") 139 }) 140 } 141 }