github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/window_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 interlock_test
    15  
    16  import (
    17  	"fmt"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    21  )
    22  
    23  func (s *testSuite7) TestWindowFunctions(c *C) {
    24  	tk := testkit.NewTestKit(c, s.causetstore)
    25  	tk.MustInterDirc("set @@milevadb_window_concurrency = 1")
    26  	doTestWindowFunctions(tk)
    27  }
    28  
    29  func (s *testSuite7) TestWindowParallelFunctions(c *C) {
    30  	tk := testkit.NewTestKit(c, s.causetstore)
    31  	tk.MustInterDirc("set @@milevadb_window_concurrency = 4")
    32  	doTestWindowFunctions(tk)
    33  }
    34  
    35  func doTestWindowFunctions(tk *testkit.TestKit) {
    36  	var result *testkit.Result
    37  	tk.MustInterDirc("use test")
    38  	tk.MustInterDirc("drop causet if exists t")
    39  	tk.MustInterDirc("create causet t (a int, b int, c int)")
    40  	tk.MustInterDirc("set @@milevadb_enable_window_function = 1")
    41  	defer func() {
    42  		tk.MustInterDirc("set @@milevadb_enable_window_function = 0")
    43  	}()
    44  	tk.MustInterDirc("insert into t values (1,2,3),(4,3,2),(2,3,4)")
    45  	result = tk.MustQuery("select count(a) over () from t")
    46  	result.Check(testkit.Events("3", "3", "3"))
    47  	result = tk.MustQuery("select sum(a) over () + count(a) over () from t")
    48  	result.Check(testkit.Events("10", "10", "10"))
    49  	result = tk.MustQuery("select sum(a) over (partition by a) from t").Sort()
    50  	result.Check(testkit.Events("1", "2", "4"))
    51  	result = tk.MustQuery("select 1 + sum(a) over (), count(a) over () from t")
    52  	result.Check(testkit.Events("8 3", "8 3", "8 3"))
    53  	result = tk.MustQuery("select sum(t1.a) over() from t t1, t t2")
    54  	result.Check(testkit.Events("21", "21", "21", "21", "21", "21", "21", "21", "21"))
    55  	result = tk.MustQuery("select _milevadb_rowid, sum(t.a) over() from t")
    56  	result.Check(testkit.Events("1 7", "2 7", "3 7"))
    57  
    58  	result = tk.MustQuery("select a, row_number() over() from t")
    59  	result.Check(testkit.Events("1 1", "4 2", "2 3"))
    60  	result = tk.MustQuery("select a, row_number() over(partition by a) from t").Sort()
    61  	result.Check(testkit.Events("1 1", "2 1", "4 1"))
    62  
    63  	result = tk.MustQuery("select a, sum(a) over(rows between unbounded preceding and 1 following) from t")
    64  	result.Check(testkit.Events("1 5", "4 7", "2 7"))
    65  	result = tk.MustQuery("select a, sum(a) over(rows between 1 preceding and 1 following) from t")
    66  	result.Check(testkit.Events("1 5", "4 7", "2 6"))
    67  	result = tk.MustQuery("select a, sum(a) over(rows between unbounded preceding and 1 preceding) from t")
    68  	result.Check(testkit.Events("1 <nil>", "4 1", "2 5"))
    69  
    70  	tk.MustInterDirc("drop causet t")
    71  	tk.MustInterDirc("create causet t(a int, b date)")
    72  	tk.MustInterDirc("insert into t values (null,null),(1,20190201),(2,20190202),(3,20190203),(5,20190205)")
    73  	result = tk.MustQuery("select a, sum(a) over(order by a range between 1 preceding and 2 following) from t")
    74  	result.Check(testkit.Events("<nil> <nil>", "1 6", "2 6", "3 10", "5 5"))
    75  	result = tk.MustQuery("select a, sum(a) over(order by a desc range between 1 preceding and 2 following) from t")
    76  	result.Check(testkit.Events("5 8", "3 6", "2 6", "1 3", "<nil> <nil>"))
    77  	result = tk.MustQuery("select a, b, sum(a) over(order by b range between interval 1 day preceding and interval 2 day following) from t")
    78  	result.Check(testkit.Events("<nil> <nil> <nil>", "1 2020-02-01 6", "2 2020-02-02 6", "3 2020-02-03 10", "5 2020-02-05 5"))
    79  	result = tk.MustQuery("select a, b, sum(a) over(order by b desc range between interval 1 day preceding and interval 2 day following) from t")
    80  	result.Check(testkit.Events("5 2020-02-05 8", "3 2020-02-03 6", "2 2020-02-02 6", "1 2020-02-01 3", "<nil> <nil> <nil>"))
    81  
    82  	tk.MustInterDirc("drop causet t")
    83  	tk.MustInterDirc("CREATE TABLE t (id INTEGER, sex CHAR(1))")
    84  	tk.MustInterDirc("insert into t values (1, 'M'), (2, 'F'), (3, 'F'), (4, 'F'), (5, 'M'), (10, NULL), (11, NULL)")
    85  	result = tk.MustQuery("SELECT sex, id, RANK() OVER (PARTITION BY sex ORDER BY id DESC) FROM t").Sort()
    86  	result.Check(testkit.Events("<nil> 10 2", "<nil> 11 1", "F 2 3", "F 3 2", "F 4 1", "M 1 2", "M 5 1"))
    87  
    88  	tk.MustInterDirc("drop causet t")
    89  	tk.MustInterDirc("create causet t(a int, b int)")
    90  	tk.MustInterDirc("insert into t values (1,1),(1,2),(2,1),(2,2)")
    91  	result = tk.MustQuery("select a, b, rank() over() from t")
    92  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 1 1", "2 2 1"))
    93  	result = tk.MustQuery("select a, b, rank() over(order by a) from t")
    94  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 1 3", "2 2 3"))
    95  	result = tk.MustQuery("select a, b, rank() over(order by a, b) from t")
    96  	result.Check(testkit.Events("1 1 1", "1 2 2", "2 1 3", "2 2 4"))
    97  
    98  	result = tk.MustQuery("select a, b, dense_rank() over() from t")
    99  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 1 1", "2 2 1"))
   100  	result = tk.MustQuery("select a, b, dense_rank() over(order by a) from t")
   101  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 1 2", "2 2 2"))
   102  	result = tk.MustQuery("select a, b, dense_rank() over(order by a, b) from t")
   103  	result.Check(testkit.Events("1 1 1", "1 2 2", "2 1 3", "2 2 4"))
   104  
   105  	result = tk.MustQuery("select row_number() over(rows between 1 preceding and 1 following) from t")
   106  	result.Check(testkit.Events("1", "2", "3", "4"))
   107  	result = tk.MustQuery("show warnings")
   108  	result.Check(testkit.Events("Note 3599 Window function 'row_number' ignores the frame clause of window '<unnamed window>' and aggregates over the whole partition"))
   109  
   110  	result = tk.MustQuery("select a, sum(a) over() from t")
   111  	result.Check(testkit.Events("1 6", "1 6", "2 6", "2 6"))
   112  	result = tk.MustQuery("select a, sum(a) over(order by a) from t")
   113  	result.Check(testkit.Events("1 2", "1 2", "2 6", "2 6"))
   114  	result = tk.MustQuery("select a, sum(a) over(order by a, b) from t")
   115  	result.Check(testkit.Events("1 1", "1 2", "2 4", "2 6"))
   116  
   117  	result = tk.MustQuery("select a, first_value(a) over(), last_value(a) over() from t")
   118  	result.Check(testkit.Events("1 1 2", "1 1 2", "2 1 2", "2 1 2"))
   119  	result = tk.MustQuery("select a, first_value(a) over(rows between 1 preceding and 1 following), last_value(a) over(rows between 1 preceding and 1 following) from t")
   120  	result.Check(testkit.Events("1 1 1", "1 1 2", "2 1 2", "2 2 2"))
   121  	result = tk.MustQuery("select a, first_value(a) over(rows between 1 following and 1 following), last_value(a) over(rows between 1 following and 1 following) from t")
   122  	result.Check(testkit.Events("1 1 1", "1 2 2", "2 2 2", "2 <nil> <nil>"))
   123  	result = tk.MustQuery("select a, first_value(rand(0)) over(), last_value(rand(0)) over() from t")
   124  	result.Check(testkit.Events("1 0.15522042769493574 0.33109208227236947", "1 0.15522042769493574 0.33109208227236947",
   125  		"2 0.15522042769493574 0.33109208227236947", "2 0.15522042769493574 0.33109208227236947"))
   126  
   127  	result = tk.MustQuery("select a, b, cume_dist() over() from t")
   128  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 1 1", "2 2 1"))
   129  	result = tk.MustQuery("select a, b, cume_dist() over(order by a) from t")
   130  	result.Check(testkit.Events("1 1 0.5", "1 2 0.5", "2 1 1", "2 2 1"))
   131  	result = tk.MustQuery("select a, b, cume_dist() over(order by a, b) from t")
   132  	result.Check(testkit.Events("1 1 0.25", "1 2 0.5", "2 1 0.75", "2 2 1"))
   133  
   134  	result = tk.MustQuery("select a, nth_value(a, null) over() from t")
   135  	result.Check(testkit.Events("1 <nil>", "1 <nil>", "2 <nil>", "2 <nil>"))
   136  	result = tk.MustQuery("select a, nth_value(a, 1) over() from t")
   137  	result.Check(testkit.Events("1 1", "1 1", "2 1", "2 1"))
   138  	result = tk.MustQuery("select a, nth_value(a, 4) over() from t")
   139  	result.Check(testkit.Events("1 2", "1 2", "2 2", "2 2"))
   140  	result = tk.MustQuery("select a, nth_value(a, 5) over() from t")
   141  	result.Check(testkit.Events("1 <nil>", "1 <nil>", "2 <nil>", "2 <nil>"))
   142  
   143  	result = tk.MustQuery("select ntile(3) over() from t")
   144  	result.Check(testkit.Events("1", "1", "2", "3"))
   145  	result = tk.MustQuery("select ntile(2) over() from t")
   146  	result.Check(testkit.Events("1", "1", "2", "2"))
   147  	result = tk.MustQuery("select ntile(null) over() from t")
   148  	result.Check(testkit.Events("<nil>", "<nil>", "<nil>", "<nil>"))
   149  
   150  	result = tk.MustQuery("select a, percent_rank() over() from t")
   151  	result.Check(testkit.Events("1 0", "1 0", "2 0", "2 0"))
   152  	result = tk.MustQuery("select a, percent_rank() over(order by a) from t")
   153  	result.Check(testkit.Events("1 0", "1 0", "2 0.6666666666666666", "2 0.6666666666666666"))
   154  	result = tk.MustQuery("select a, b, percent_rank() over(order by a, b) from t")
   155  	result.Check(testkit.Events("1 1 0", "1 2 0.3333333333333333", "2 1 0.6666666666666666", "2 2 1"))
   156  
   157  	result = tk.MustQuery("select a, lead(a) over (), lag(a) over() from t")
   158  	result.Check(testkit.Events("1 1 <nil>", "1 2 1", "2 2 1", "2 <nil> 2"))
   159  	result = tk.MustQuery("select a, lead(a, 0) over(), lag(a, 0) over() from t")
   160  	result.Check(testkit.Events("1 1 1", "1 1 1", "2 2 2", "2 2 2"))
   161  	result = tk.MustQuery("select a, lead(a, 1, a) over(), lag(a, 1, a) over() from t")
   162  	result.Check(testkit.Events("1 1 1", "1 2 1", "2 2 1", "2 2 2"))
   163  	result = tk.MustQuery("select a, lead(a, 1, 'lead') over(), lag(a, 1, 'lag') over() from t")
   164  	result.Check(testkit.Events("1 1 lag", "1 2 1", "2 2 1", "2 lead 2"))
   165  
   166  	result = tk.MustQuery("SELECT CUME_DIST() OVER (ORDER BY null);")
   167  	result.Check(testkit.Events("1"))
   168  
   169  	tk.MustQuery("select lead(a) over(partition by null) from t").Sort().Check(testkit.Events("1", "2", "2", "<nil>"))
   170  
   171  	tk.MustInterDirc("create causet issue10494(a INT, b CHAR(1), c DATETIME, d BLOB)")
   172  	tk.MustInterDirc("insert into issue10494 VALUES (1,'x','2010-01-01','blob'), (2, 'y', '2011-01-01', ''), (3, 'y', '2012-01-01', ''), (4, 't', '2012-01-01', 'blob'), (5, null, '2020-01-01', null)")
   173  	tk.MustQuery("SELECT a, b, c, SUM(a) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM issue10494 order by a;").Check(
   174  		testkit.Events(
   175  			"1 x 2010-01-01 00:00:00 15",
   176  			"2 y 2011-01-01 00:00:00 15",
   177  			"3 y 2012-01-01 00:00:00 15",
   178  			"4 t 2012-01-01 00:00:00 15",
   179  			"5 <nil> 2020-01-01 00:00:00 15",
   180  		),
   181  	)
   182  
   183  	tk.MustInterDirc("CREATE TABLE td_dec (id DECIMAL(10,2), sex CHAR(1));")
   184  	tk.MustInterDirc("insert into td_dec value (2.0, 'F'), (NULL, 'F'), (1.0, 'F')")
   185  	tk.MustQuery("SELECT id, FIRST_VALUE(id) OVER w FROM td_dec WINDOW w AS (ORDER BY id);").Check(
   186  		testkit.Events("<nil> <nil>", "1.00 <nil>", "2.00 <nil>"),
   187  	)
   188  
   189  	result = tk.MustQuery("select sum(a) over w, sum(b) over w from t window w as (order by a)")
   190  	result.Check(testkit.Events("2 3", "2 3", "6 6", "6 6"))
   191  	result = tk.MustQuery("select row_number() over w, sum(b) over w from t window w as (order by a)")
   192  	result.Check(testkit.Events("1 3", "2 3", "3 6", "4 6"))
   193  	result = tk.MustQuery("select row_number() over w, sum(b) over w from t window w as (rows between 1 preceding and 1 following)")
   194  	result.Check(testkit.Events("1 3", "2 4", "3 5", "4 3"))
   195  
   196  	tk.Se.GetStochastikVars().MaxChunkSize = 1
   197  	result = tk.MustQuery("select a, row_number() over (partition by a) from t").Sort()
   198  	result.Check(testkit.Events("1 1", "1 2", "2 1", "2 2"))
   199  }
   200  
   201  func (s *testSuite7) TestWindowFunctionsDataReference(c *C) {
   202  	// see https://github.com/whtcorpsinc/milevadb/issues/11614
   203  	tk := testkit.NewTestKit(c, s.causetstore)
   204  	tk.MustInterDirc("use test")
   205  	tk.MustInterDirc("drop causet if exists t")
   206  	tk.MustInterDirc("create causet t(a int, b int)")
   207  	tk.MustInterDirc("insert into t values (2,1),(2,2),(2,3)")
   208  
   209  	tk.Se.GetStochastikVars().MaxChunkSize = 2
   210  	result := tk.MustQuery("select a, b, rank() over (partition by a order by b) from t")
   211  	result.Check(testkit.Events("2 1 1", "2 2 2", "2 3 3"))
   212  	result = tk.MustQuery("select a, b, PERCENT_RANK() over (partition by a order by b) from t")
   213  	result.Check(testkit.Events("2 1 0", "2 2 0.5", "2 3 1"))
   214  	result = tk.MustQuery("select a, b, CUME_DIST() over (partition by a order by b) from t")
   215  	result.Check(testkit.Events("2 1 0.3333333333333333", "2 2 0.6666666666666666", "2 3 1"))
   216  
   217  	// see https://github.com/whtcorpsinc/milevadb/issues/12415
   218  	result = tk.MustQuery("select b, first_value(b) over (order by b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) from t")
   219  	result.Check(testkit.Events("1 1", "2 1", "3 1"))
   220  	result = tk.MustQuery("select b, first_value(b) over (order by b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) from t")
   221  	result.Check(testkit.Events("1 1", "2 1", "3 1"))
   222  }
   223  
   224  func (s *testSuite7) TestSlidingWindowFunctions(c *C) {
   225  	tk := testkit.NewTestKit(c, s.causetstore)
   226  	tk.MustInterDirc("use test;")
   227  	idTypes := []string{"FLOAT", "DOUBLE"}
   228  	useHighPrecisions := []string{"ON", "OFF"}
   229  	for _, idType := range idTypes {
   230  		for _, useHighPrecision := range useHighPrecisions {
   231  			tk.MustInterDirc("drop causet if exists t;")
   232  			tk.MustInterDirc(fmt.Sprintf("CREATE TABLE t (id %s, sex CHAR(1));", idType))
   233  			tk.MustInterDirc(fmt.Sprintf("SET SESSION windowing_use_high_precision = %s;", useHighPrecision))
   234  			baseTestSlidingWindowFunctions(tk)
   235  		}
   236  	}
   237  }
   238  
   239  func baseTestSlidingWindowFunctions(tk *testkit.TestKit) {
   240  	var result *testkit.Result
   241  	tk.MustInterDirc("insert into t values (1,'M')")
   242  	tk.MustInterDirc("insert into t values (2,'F')")
   243  	tk.MustInterDirc("insert into t values (3,'F')")
   244  	tk.MustInterDirc("insert into t values (4,'F')")
   245  	tk.MustInterDirc("insert into t values (5,'M')")
   246  	tk.MustInterDirc("insert into t values (10,null)")
   247  	tk.MustInterDirc("insert into t values (11,null)")
   248  	tk.MustInterDirc("PREPARE p FROM 'SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? PRECEDING and ? PRECEDING) FROM t';")
   249  	tk.MustInterDirc("SET @p1= 1;")
   250  	tk.MustInterDirc("SET @p2= 2;")
   251  	result = tk.MustQuery("EXECUTE p USING @p1, @p2;")
   252  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   253  	result = tk.MustQuery("EXECUTE p USING @p2, @p1;")
   254  	result.Check(testkit.Events("M 0", "F 1", "F 2", "F 2", "M 2", "<nil> 2", "<nil> 2"))
   255  	tk.MustInterDirc("DROP PREPARE p;")
   256  	tk.MustInterDirc("PREPARE p FROM 'SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN ? FOLLOWING and ? FOLLOWING) FROM t';")
   257  	tk.MustInterDirc("SET @p1= 1;")
   258  	tk.MustInterDirc("SET @p2= 2;")
   259  	result = tk.MustQuery("EXECUTE p USING @p2, @p1;")
   260  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   261  	result = tk.MustQuery("EXECUTE p USING @p1, @p2;")
   262  	result.Check(testkit.Events("M 2", "F 2", "F 2", "F 2", "M 2", "<nil> 1", "<nil> 0"))
   263  	tk.MustInterDirc("DROP PREPARE p;")
   264  
   265  	// COUNT ROWS
   266  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   267  	result.Check(testkit.Events("M 2", "F 2", "F 2", "F 2", "M 2", "<nil> 1", "<nil> 0"))
   268  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   269  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   270  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   271  	result.Check(testkit.Events("M 0", "F 1", "F 2", "F 2", "M 2", "<nil> 2", "<nil> 2"))
   272  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   273  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   274  
   275  	// COUNT RANGE
   276  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   277  	result.Check(testkit.Events("M 2", "F 2", "F 2", "F 1", "M 0", "<nil> 1", "<nil> 0"))
   278  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   279  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   280  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   281  	result.Check(testkit.Events("M 0", "F 1", "F 2", "F 2", "M 2", "<nil> 0", "<nil> 1"))
   282  	result = tk.MustQuery("SELECT sex, COUNT(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   283  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   284  
   285  	// SUM ROWS
   286  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   287  	result.Check(testkit.Events("M 5", "F 7", "F 9", "F 15", "M 21", "<nil> 11", "<nil> <nil>"))
   288  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   289  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   290  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   291  	result.Check(testkit.Events("M <nil>", "F 1", "F 3", "F 5", "M 7", "<nil> 9", "<nil> 15"))
   292  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   293  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   294  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING and 1 FOLLOWING) FROM t;")
   295  	result.Check(testkit.Events("M 3", "F 6", "F 10", "F 15", "M 25", "<nil> 36", "<nil> 36"))
   296  
   297  	// SUM RANGE
   298  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   299  	result.Check(testkit.Events("M 5", "F 7", "F 9", "F 5", "M <nil>", "<nil> 11", "<nil> <nil>"))
   300  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   301  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   302  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   303  	result.Check(testkit.Events("M <nil>", "F 1", "F 3", "F 5", "M 7", "<nil> <nil>", "<nil> 10"))
   304  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   305  	result.Check(testkit.Events("M 6", "F 10", "F 14", "F 12", "M 9", "<nil> 21", "<nil> 21"))
   306  	result = tk.MustQuery("SELECT sex, SUM(id) OVER (ORDER BY id DESC RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   307  	result.Check(testkit.Events("<nil> 21", "<nil> 21", "M 12", "F 14", "F 10", "F 6", "M 3"))
   308  
   309  	// AVG ROWS
   310  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   311  	result.Check(testkit.Events("M 2.5", "F 3.5", "F 4.5", "F 7.5", "M 10.5", "<nil> 11", "<nil> <nil>"))
   312  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   313  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   314  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   315  	result.Check(testkit.Events("M <nil>", "F 1", "F 1.5", "F 2.5", "M 3.5", "<nil> 4.5", "<nil> 7.5"))
   316  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   317  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   318  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING and 1 FOLLOWING) FROM t;")
   319  	result.Check(testkit.Events("M 1.5", "F 2", "F 2.5", "F 3", "M 4.166666666666667", "<nil> 5.142857142857143", "<nil> 5.142857142857143"))
   320  
   321  	// AVG RANGE
   322  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   323  	result.Check(testkit.Events("M 2.5", "F 3.5", "F 4.5", "F 5", "M <nil>", "<nil> 11", "<nil> <nil>"))
   324  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   325  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   326  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   327  	result.Check(testkit.Events("M <nil>", "F 1", "F 1.5", "F 2.5", "M 3.5", "<nil> <nil>", "<nil> 10"))
   328  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   329  	result.Check(testkit.Events("M 2", "F 2.5", "F 3.5", "F 4", "M 4.5", "<nil> 10.5", "<nil> 10.5"))
   330  	result = tk.MustQuery("SELECT sex, AVG(id) OVER (ORDER BY id DESC RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   331  	result.Check(testkit.Events("<nil> 10.5", "<nil> 10.5", "M 4", "F 3.5", "F 2.5", "F 2", "M 1.5"))
   332  
   333  	// BIT_XOR ROWS
   334  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   335  	result.Check(testkit.Events("M 1", "F 7", "F 1", "F 15", "M 1", "<nil> 11", "<nil> 0"))
   336  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   337  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   338  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   339  	result.Check(testkit.Events("M 0", "F 1", "F 3", "F 1", "M 7", "<nil> 1", "<nil> 15"))
   340  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   341  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   342  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING and 1 FOLLOWING) FROM t;")
   343  	result.Check(testkit.Events("M 3", "F 0", "F 4", "F 1", "M 11", "<nil> 0", "<nil> 0"))
   344  
   345  	// BIT_XOR RANGE
   346  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   347  	result.Check(testkit.Events("M 1", "F 7", "F 1", "F 5", "M 0", "<nil> 11", "<nil> 0"))
   348  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   349  	result.Check(testkit.Events("M 0", "F 0", "F 0", "F 0", "M 0", "<nil> 0", "<nil> 0"))
   350  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   351  	result.Check(testkit.Events("M 0", "F 1", "F 3", "F 1", "M 7", "<nil> 0", "<nil> 10"))
   352  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   353  	result.Check(testkit.Events("M 0", "F 4", "F 0", "F 2", "M 1", "<nil> 1", "<nil> 1"))
   354  	result = tk.MustQuery("SELECT sex, BIT_XOR(id) OVER (ORDER BY id DESC RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   355  	result.Check(testkit.Events("<nil> 1", "<nil> 1", "M 2", "F 0", "F 4", "F 0", "M 3"))
   356  
   357  	// MIN ROWS
   358  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   359  	result.Check(testkit.Events("M 2", "F 3", "F 4", "F 5", "M 10", "<nil> 11", "<nil> <nil>"))
   360  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   361  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   362  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   363  	result.Check(testkit.Events("M <nil>", "F 1", "F 1", "F 2", "M 3", "<nil> 4", "<nil> 5"))
   364  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   365  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   366  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING and 1 FOLLOWING) FROM t;")
   367  	result.Check(testkit.Events("M 1", "F 1", "F 1", "F 1", "M 1", "<nil> 1", "<nil> 1"))
   368  
   369  	// MIN RANGE
   370  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   371  	result.Check(testkit.Events("M 2", "F 3", "F 4", "F 5", "M <nil>", "<nil> 11", "<nil> <nil>"))
   372  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   373  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   374  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   375  	result.Check(testkit.Events("M <nil>", "F 1", "F 1", "F 2", "M 3", "<nil> <nil>", "<nil> 10"))
   376  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   377  	result.Check(testkit.Events("M 1", "F 1", "F 2", "F 3", "M 4", "<nil> 10", "<nil> 10"))
   378  	result = tk.MustQuery("SELECT sex, MIN(id) OVER (ORDER BY id DESC RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   379  	result.Check(testkit.Events("<nil> 10", "<nil> 10", "M 3", "F 2", "F 1", "F 1", "M 1"))
   380  
   381  	// MAX ROWS
   382  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id ROWS BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   383  	result.Check(testkit.Events("M 3", "F 4", "F 5", "F 10", "M 11", "<nil> 11", "<nil> <nil>"))
   384  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id ROWS BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   385  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   386  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   387  	result.Check(testkit.Events("M <nil>", "F 1", "F 2", "F 3", "M 4", "<nil> 5", "<nil> 10"))
   388  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING and 3 PRECEDING) FROM t;")
   389  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   390  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING and 1 FOLLOWING) FROM t;")
   391  	result.Check(testkit.Events("M 2", "F 3", "F 4", "F 5", "M 10", "<nil> 11", "<nil> 11"))
   392  
   393  	// MAX RANGE
   394  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id RANGE BETWEEN 1 FOLLOWING and 2 FOLLOWING) FROM t;")
   395  	result.Check(testkit.Events("M 3", "F 4", "F 5", "F 5", "M <nil>", "<nil> 11", "<nil> <nil>"))
   396  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id RANGE BETWEEN 3 FOLLOWING and 1 FOLLOWING) FROM t;")
   397  	result.Check(testkit.Events("M <nil>", "F <nil>", "F <nil>", "F <nil>", "M <nil>", "<nil> <nil>", "<nil> <nil>"))
   398  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id RANGE BETWEEN 2 PRECEDING and 1 PRECEDING) FROM t;")
   399  	result.Check(testkit.Events("M <nil>", "F 1", "F 2", "F 3", "M 4", "<nil> <nil>", "<nil> 10"))
   400  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   401  	result.Check(testkit.Events("M 3", "F 4", "F 5", "F 5", "M 5", "<nil> 11", "<nil> 11"))
   402  	result = tk.MustQuery("SELECT sex, MAX(id) OVER (ORDER BY id DESC RANGE BETWEEN 1 PRECEDING and 2 FOLLOWING) FROM t;")
   403  	result.Check(testkit.Events("<nil> 11", "<nil> 11", "M 5", "F 5", "F 4", "F 3", "M 2"))
   404  }