github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_lead_lag_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package aggfuncs_test
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    19  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    20  	"github.com/whtcorpsinc/milevadb/interlock/aggfuncs"
    21  	"github.com/whtcorpsinc/milevadb/memex"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  )
    24  
    25  func (s *testSuite) TestLeadLag(c *C) {
    26  	zero := memex.NewZero()
    27  	one := memex.NewOne()
    28  	two := &memex.Constant{
    29  		Value:   types.NewCauset(2),
    30  		RetType: types.NewFieldType(allegrosql.TypeTiny),
    31  	}
    32  	three := &memex.Constant{
    33  		Value:   types.NewCauset(3),
    34  		RetType: types.NewFieldType(allegrosql.TypeTiny),
    35  	}
    36  	million := &memex.Constant{
    37  		Value:   types.NewCauset(1000000),
    38  		RetType: types.NewFieldType(allegrosql.TypeLong),
    39  	}
    40  	defaultArg := &memex.DeferredCauset{RetType: types.NewFieldType(allegrosql.TypeLonglong), Index: 0}
    41  
    42  	numEvents := 3
    43  	tests := []windowTest{
    44  		// lag(field0, N)
    45  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    46  			[]memex.Expression{zero}, 0, numEvents, 0, 1, 2),
    47  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    48  			[]memex.Expression{one}, 0, numEvents, nil, 0, 1),
    49  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    50  			[]memex.Expression{two}, 0, numEvents, nil, nil, 0),
    51  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    52  			[]memex.Expression{three}, 0, numEvents, nil, nil, nil),
    53  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    54  			[]memex.Expression{million}, 0, numEvents, nil, nil, nil),
    55  		// lag(field0, N, 1000000)
    56  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    57  			[]memex.Expression{zero, million}, 0, numEvents, 0, 1, 2),
    58  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    59  			[]memex.Expression{one, million}, 0, numEvents, 1000000, 0, 1),
    60  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    61  			[]memex.Expression{two, million}, 0, numEvents, 1000000, 1000000, 0),
    62  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    63  			[]memex.Expression{three, million}, 0, numEvents, 1000000, 1000000, 1000000),
    64  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    65  			[]memex.Expression{million, million}, 0, numEvents, 1000000, 1000000, 1000000),
    66  		// lag(field0, N, field0)
    67  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    68  			[]memex.Expression{zero, defaultArg}, 0, numEvents, 0, 1, 2),
    69  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    70  			[]memex.Expression{one, defaultArg}, 0, numEvents, 0, 0, 1),
    71  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    72  			[]memex.Expression{two, defaultArg}, 0, numEvents, 0, 1, 0),
    73  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    74  			[]memex.Expression{three, defaultArg}, 0, numEvents, 0, 1, 2),
    75  		buildWindowTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
    76  			[]memex.Expression{million, defaultArg}, 0, numEvents, 0, 1, 2),
    77  
    78  		// lead(field0, N)
    79  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    80  			[]memex.Expression{zero}, 0, numEvents, 0, 1, 2),
    81  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    82  			[]memex.Expression{one}, 0, numEvents, 1, 2, nil),
    83  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    84  			[]memex.Expression{two}, 0, numEvents, 2, nil, nil),
    85  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    86  			[]memex.Expression{three}, 0, numEvents, nil, nil, nil),
    87  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    88  			[]memex.Expression{million}, 0, numEvents, nil, nil, nil),
    89  		// lead(field0, N, 1000000)
    90  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    91  			[]memex.Expression{zero, million}, 0, numEvents, 0, 1, 2),
    92  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    93  			[]memex.Expression{one, million}, 0, numEvents, 1, 2, 1000000),
    94  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    95  			[]memex.Expression{two, million}, 0, numEvents, 2, 1000000, 1000000),
    96  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    97  			[]memex.Expression{three, million}, 0, numEvents, 1000000, 1000000, 1000000),
    98  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
    99  			[]memex.Expression{million, million}, 0, numEvents, 1000000, 1000000, 1000000),
   100  		// lead(field0, N, field0)
   101  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   102  			[]memex.Expression{zero, defaultArg}, 0, numEvents, 0, 1, 2),
   103  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   104  			[]memex.Expression{one, defaultArg}, 0, numEvents, 1, 2, 2),
   105  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   106  			[]memex.Expression{two, defaultArg}, 0, numEvents, 2, 1, 2),
   107  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   108  			[]memex.Expression{three, defaultArg}, 0, numEvents, 0, 1, 2),
   109  		buildWindowTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   110  			[]memex.Expression{million, defaultArg}, 0, numEvents, 0, 1, 2),
   111  	}
   112  	for _, test := range tests {
   113  		s.testWindowFunc(c, test)
   114  	}
   115  
   116  }
   117  
   118  func (s *testSuite) TestMemLeadLag(c *C) {
   119  	zero := memex.NewZero()
   120  	one := memex.NewOne()
   121  	two := &memex.Constant{
   122  		Value:   types.NewCauset(2),
   123  		RetType: types.NewFieldType(allegrosql.TypeTiny),
   124  	}
   125  	three := &memex.Constant{
   126  		Value:   types.NewCauset(3),
   127  		RetType: types.NewFieldType(allegrosql.TypeTiny),
   128  	}
   129  	million := &memex.Constant{
   130  		Value:   types.NewCauset(1000000),
   131  		RetType: types.NewFieldType(allegrosql.TypeLong),
   132  	}
   133  
   134  	numEvents := 3
   135  	tests := []windowMemTest{
   136  		// lag(field0, N)
   137  		buildWindowMemTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
   138  			[]memex.Expression{zero}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   139  		buildWindowMemTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
   140  			[]memex.Expression{one}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   141  		buildWindowMemTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
   142  			[]memex.Expression{two}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   143  		buildWindowMemTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
   144  			[]memex.Expression{three}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   145  		buildWindowMemTesterWithArgs(ast.WindowFuncLag, allegrosql.TypeLonglong,
   146  			[]memex.Expression{million}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   147  
   148  		// lead(field0, N)
   149  		buildWindowMemTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   150  			[]memex.Expression{zero}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   151  		buildWindowMemTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   152  			[]memex.Expression{one}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   153  		buildWindowMemTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   154  			[]memex.Expression{two}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   155  		buildWindowMemTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   156  			[]memex.Expression{three}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   157  		buildWindowMemTesterWithArgs(ast.WindowFuncLead, allegrosql.TypeLonglong,
   158  			[]memex.Expression{million}, 0, numEvents, aggfuncs.DefPartialResult4LeadLagSize, rowMemDeltaGens),
   159  	}
   160  
   161  	for _, test := range tests {
   162  		s.testWindowAggMemFunc(c, test)
   163  	}
   164  
   165  }