github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/simple_rewriter_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 memex
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    19  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    20  )
    21  
    22  func (s *testEvaluatorSuite) TestSimpleRewriter(c *C) {
    23  	ctx := mock.NewContext()
    24  	sch := NewSchema()
    25  	_, err := ParseSimpleExprsWithSchema(ctx, "NULLIF(1, 2, 3)", sch)
    26  	c.Assert(err, NotNil)
    27  
    28  	exprs, err := ParseSimpleExprsWithSchema(ctx, "NULLIF(1, 2)", sch)
    29  	c.Assert(err, IsNil)
    30  	num, _, _ := exprs[0].EvalInt(ctx, chunk.Event{})
    31  	c.Assert(num, Equals, int64(1))
    32  
    33  	exprs, err = ParseSimpleExprsWithSchema(ctx, "NULLIF(1, 1)", sch)
    34  	c.Assert(err, IsNil)
    35  	_, isNull, _ := exprs[0].EvalInt(ctx, chunk.Event{})
    36  	c.Assert(isNull, IsTrue)
    37  
    38  	exprs, err = ParseSimpleExprsWithSchema(ctx, "+1", sch)
    39  	c.Assert(err, IsNil)
    40  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    41  	c.Assert(num, Equals, int64(1))
    42  
    43  	exprs, err = ParseSimpleExprsWithSchema(ctx, "-1", sch)
    44  	c.Assert(err, IsNil)
    45  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    46  	c.Assert(num, Equals, int64(-1))
    47  
    48  	exprs, err = ParseSimpleExprsWithSchema(ctx, "'abc' like '%b%'", sch)
    49  	c.Assert(err, IsNil)
    50  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    51  	c.Assert(num, Equals, int64(1))
    52  
    53  	exprs, err = ParseSimpleExprsWithSchema(ctx, "'abcdef' REGEXP '.*cd.*'", sch)
    54  	c.Assert(err, IsNil)
    55  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    56  	c.Assert(num, Equals, int64(1))
    57  
    58  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 1) = (1, 1)", sch)
    59  	c.Assert(err, IsNil)
    60  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    61  	c.Assert(num, Equals, int64(1))
    62  
    63  	exprs, err = ParseSimpleExprsWithSchema(ctx, "5 between 1 and 10", sch)
    64  	c.Assert(err, IsNil)
    65  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    66  	c.Assert(num, Equals, int64(1))
    67  
    68  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 not between 5 and 10", sch)
    69  	c.Assert(err, IsNil)
    70  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    71  	c.Assert(num, Equals, int64(1))
    72  
    73  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 is true", sch)
    74  	c.Assert(err, IsNil)
    75  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    76  	c.Assert(num, Equals, int64(1))
    77  
    78  	exprs, err = ParseSimpleExprsWithSchema(ctx, "0 is not true", sch)
    79  	c.Assert(err, IsNil)
    80  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    81  	c.Assert(num, Equals, int64(1))
    82  
    83  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 in (1, 2, 3)", sch)
    84  	c.Assert(err, IsNil)
    85  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    86  	c.Assert(num, Equals, int64(1))
    87  
    88  	_, err = ParseSimpleExprsWithSchema(ctx, "1 in ()", sch)
    89  	c.Assert(err, NotNil)
    90  
    91  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 in (1, 1, 1, 1)", sch)
    92  	c.Assert(err, IsNil)
    93  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    94  	c.Assert(num, Equals, int64(1))
    95  
    96  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 2) in ((1, 2), (2, 2))", sch)
    97  	c.Assert(err, IsNil)
    98  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
    99  	c.Assert(num, Equals, int64(1))
   100  
   101  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 2) not in ((2, 2))", sch)
   102  	c.Assert(err, IsNil)
   103  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   104  	c.Assert(num, Equals, int64(1))
   105  
   106  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 < 2", sch)
   107  	c.Assert(err, IsNil)
   108  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   109  	c.Assert(num, Equals, int64(1))
   110  
   111  	exprs, err = ParseSimpleExprsWithSchema(ctx, "1 <= 2", sch)
   112  	c.Assert(err, IsNil)
   113  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   114  	c.Assert(num, Equals, int64(1))
   115  
   116  	exprs, err = ParseSimpleExprsWithSchema(ctx, "2 >= 1", sch)
   117  	c.Assert(err, IsNil)
   118  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   119  	c.Assert(num, Equals, int64(1))
   120  
   121  	exprs, err = ParseSimpleExprsWithSchema(ctx, "2 > 1", sch)
   122  	c.Assert(err, IsNil)
   123  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   124  	c.Assert(num, Equals, int64(1))
   125  
   126  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 2) < (1, 3)", sch)
   127  	c.Assert(err, IsNil)
   128  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   129  	c.Assert(num, Equals, int64(1))
   130  
   131  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 2) <= (1, 3)", sch)
   132  	c.Assert(err, IsNil)
   133  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   134  	c.Assert(num, Equals, int64(1))
   135  
   136  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 3) > (1, 2)", sch)
   137  	c.Assert(err, IsNil)
   138  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   139  	c.Assert(num, Equals, int64(1))
   140  
   141  	exprs, err = ParseSimpleExprsWithSchema(ctx, "(1, 3) >= (1, 2)", sch)
   142  	c.Assert(err, IsNil)
   143  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   144  	c.Assert(num, Equals, int64(1))
   145  
   146  	_, err = ParseSimpleExprsWithSchema(ctx, "abs(?)", sch)
   147  	c.Assert(err, IsNil)
   148  
   149  	exprs, err = ParseSimpleExprsWithSchema(ctx, "cast('1' as unsigned)", sch)
   150  	c.Assert(err, IsNil)
   151  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   152  	c.Assert(num, Equals, int64(1))
   153  
   154  	exprs, err = ParseSimpleExprsWithSchema(ctx, "trim(leading 'z' from 'zxyz')", sch)
   155  	c.Assert(err, IsNil)
   156  	str, _, _ := exprs[0].EvalString(ctx, chunk.Event{})
   157  	c.Assert(str, Equals, "xyz")
   158  
   159  	exprs, err = ParseSimpleExprsWithSchema(ctx, "get_format(datetime, 'ISO')", sch)
   160  	c.Assert(err, IsNil)
   161  	str, _, _ = exprs[0].EvalString(ctx, chunk.Event{})
   162  	c.Assert(str, Equals, "%Y-%m-%d %H:%i:%s")
   163  
   164  	exprs, err = ParseSimpleExprsWithSchema(ctx, "extract(day_minute from '2184-07-03 18:42:18.895059')", sch)
   165  	c.Assert(err, IsNil)
   166  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   167  	c.Assert(num, Equals, int64(31842))
   168  
   169  	exprs, err = ParseSimpleExprsWithSchema(ctx, "unix_timestamp('2008-05-01 00:00:00')", sch)
   170  	c.Assert(err, IsNil)
   171  	num, _, _ = exprs[0].EvalInt(ctx, chunk.Event{})
   172  	c.Assert(num, Equals, int64(1209571200))
   173  }