github.com/matrixorigin/matrixone@v0.7.0/pkg/frontend/util_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package frontend
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"fmt"
    21  	"github.com/golang/mock/gomock"
    22  	"github.com/matrixorigin/matrixone/pkg/config"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    25  	"github.com/matrixorigin/matrixone/pkg/testutil"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    27  	"math"
    28  	"os"
    29  	"sort"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/matrixorigin/matrixone/pkg/sql/parsers"
    34  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    35  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    36  	cvey "github.com/smartystreets/goconvey/convey"
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  func Test_PathExists(t *testing.T) {
    41  	cases := [...]struct {
    42  		path   string
    43  		exist  bool
    44  		isfile bool
    45  		noerr  bool
    46  	}{
    47  		{"test/file", true, true, true},
    48  		{"test/file-no", false, false, false},
    49  		//{"test/dir",true,false,true},
    50  		{"test/dir-no", false, false, false},
    51  		{"testx", false, false, false},
    52  	}
    53  
    54  	for _, c := range cases {
    55  		exist, isfile, err := PathExists(c.path)
    56  		require.True(t, (err == nil) == c.noerr)
    57  		require.True(t, exist == c.exist)
    58  		require.True(t, isfile == c.isfile)
    59  	}
    60  }
    61  
    62  func Test_closeFlag(t *testing.T) {
    63  	cvey.Convey("closeFlag", t, func() {
    64  		cf := &CloseFlag{}
    65  		cf.setClosed(0)
    66  		cvey.So(cf.IsOpened(), cvey.ShouldBeTrue)
    67  
    68  		cf.Open()
    69  		cvey.So(cf.IsOpened(), cvey.ShouldBeTrue)
    70  
    71  		cf.Close()
    72  		cvey.So(cf.IsClosed(), cvey.ShouldBeTrue)
    73  	})
    74  }
    75  
    76  func Test_MinMax(t *testing.T) {
    77  	cvey.Convey("min", t, func() {
    78  		cvey.So(Min(10, 9), cvey.ShouldEqual, 9)
    79  		cvey.So(Min(9, 10), cvey.ShouldEqual, 9)
    80  	})
    81  
    82  	cvey.Convey("minInt64", t, func() {
    83  		cvey.So(MinInt64(10, 9), cvey.ShouldEqual, 9)
    84  		cvey.So(MinInt64(9, 10), cvey.ShouldEqual, 9)
    85  	})
    86  
    87  	cvey.Convey("minUint64", t, func() {
    88  		cvey.So(MinUint64(10, 9), cvey.ShouldEqual, 9)
    89  		cvey.So(MinUint64(9, 10), cvey.ShouldEqual, 9)
    90  	})
    91  
    92  	cvey.Convey("max", t, func() {
    93  		cvey.So(Max(10, 9), cvey.ShouldEqual, 10)
    94  		cvey.So(Max(9, 10), cvey.ShouldEqual, 10)
    95  	})
    96  
    97  	cvey.Convey("maxInt64", t, func() {
    98  		cvey.So(MaxInt64(10, 9), cvey.ShouldEqual, 10)
    99  		cvey.So(MaxInt64(9, 10), cvey.ShouldEqual, 10)
   100  	})
   101  
   102  	cvey.Convey("maxUint64", t, func() {
   103  		cvey.So(MaxUint64(10, 9), cvey.ShouldEqual, 10)
   104  		cvey.So(MaxUint64(9, 10), cvey.ShouldEqual, 10)
   105  	})
   106  }
   107  
   108  func Test_uint64list(t *testing.T) {
   109  	cvey.Convey("uint64list", t, func() {
   110  		var l = make(Uint64List, 3)
   111  		cvey.So(l.Len(), cvey.ShouldEqual, 3)
   112  		cvey.So(l.Less(0, 1), cvey.ShouldBeFalse)
   113  		a, b := l[0], l[1]
   114  		l.Swap(0, 1)
   115  		cvey.So(a == l[1] && b == l[0], cvey.ShouldBeTrue)
   116  	})
   117  }
   118  
   119  func Test_routineid(t *testing.T) {
   120  	cvey.Convey("rtid", t, func() {
   121  		x := GetRoutineId()
   122  		cvey.So(x, cvey.ShouldBeGreaterThanOrEqualTo, 0)
   123  	})
   124  }
   125  
   126  func Test_debugcounter(t *testing.T) {
   127  	cvey.Convey("debugCounter", t, func() {
   128  		dc := NewDebugCounter(3)
   129  		dc.Set(0, 1)
   130  		cvey.So(dc.Get(0), cvey.ShouldEqual, 1)
   131  		dc.Add(0, 1)
   132  		cvey.So(dc.Get(0), cvey.ShouldEqual, 2)
   133  		cvey.So(dc.Len(), cvey.ShouldEqual, 3)
   134  
   135  		go dc.DCRoutine()
   136  
   137  		time.Sleep(6 * time.Second)
   138  
   139  		dc.Cf.Close()
   140  	})
   141  }
   142  
   143  func Test_timeout(t *testing.T) {
   144  	cvey.Convey("timeout", t, func() {
   145  		to := NewTimeout(5*time.Second, true)
   146  		to.UpdateTime(time.Now())
   147  		cvey.So(to.isTimeout(), cvey.ShouldBeFalse)
   148  	})
   149  }
   150  
   151  func Test_substringFromBegin(t *testing.T) {
   152  	cvey.Convey("ssfb", t, func() {
   153  		cvey.So(SubStringFromBegin("abcdef", 3), cvey.ShouldEqual, "abc...")
   154  	})
   155  }
   156  
   157  func Test_makedebuginfo(t *testing.T) {
   158  	cvey.Convey("makedebuginfo", t, func() {
   159  		MakeDebugInfo([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   160  			6, 3)
   161  	})
   162  }
   163  
   164  func TestWildcardMatch(t *testing.T) {
   165  	//sort by string
   166  
   167  	patterns := []string{
   168  		"%",
   169  		"%%%%%%%%a%%%%%%%%b%%%%%%%%b%%%%%%%%",
   170  		"%%%%%%%%a%%%%%%%%b%%%%%%%%c%%%%%%%%",
   171  		"%%%a%b%c%%%",
   172  		"%.%",
   173  		"%.zi%",
   174  		"%.zi_",
   175  		"%.zip",
   176  		"%12%12%",
   177  		"%12%23",
   178  		"%Abac%",
   179  		"%SIP%",
   180  		"%_",
   181  		"%_%_%",
   182  		"%_%_%.zip",
   183  		"%_%_.zip",
   184  		"%_.zip",
   185  		"%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%",
   186  		"%a%b%ba%ca%a%aa%aaa%fa%ga%b%",
   187  		"%a%b%ba%ca%a%x%aaa%fa%ga%b%",
   188  		"%a%b%ba%ca%aaaa%fa%ga%ggg%b%",
   189  		"%a%b%ba%ca%aaaa%fa%ga%gggg%b%",
   190  		"%aa%",
   191  		"%aa_",
   192  		"%aabbaa%a%",
   193  		"%ab%cd%",
   194  		"%abac%",
   195  		"%ccd",
   196  		"%issip%PI",
   197  		"%issip%ss%",
   198  		"%oWn%",
   199  		"%sip%",
   200  		"%zi%",
   201  		"%zi_",
   202  		"%zip",
   203  		"._",
   204  		"XY%Z%XYz",
   205  		"_",
   206  		"_%",
   207  		"_%%_%&_",
   208  		"_%%_%_",
   209  		"_%%_c_",
   210  		"_%%_d_",
   211  		"_%._%",
   212  		"_%_",
   213  		"_%_.zip",
   214  		"_%b%_%d%_",
   215  		"_.",
   216  		"_._",
   217  		"_.zip",
   218  		"_LaH",
   219  		"_Lah",
   220  		"__",
   221  		"_a",
   222  		"_a%__",
   223  		"_a_",
   224  		"_aa%",
   225  		"_b%__",
   226  		"a%",
   227  		"a%_%_",
   228  		"a%_%_%.zip",
   229  		"a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%",
   230  		"a%a%a%a%a%a%aa%aaa%a%a%b",
   231  		"a%aar",
   232  		"a%b",
   233  		"a%zz%",
   234  		"a12b",
   235  		"a_",
   236  		"ab%_%xy",
   237  		"ab%cd%xy",
   238  		"abc",
   239  		"abc%abc%abc%abc%abc",
   240  		"abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%",
   241  		"abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%",
   242  		"abc%abc%abc%abc%abc%abc%abc%abc%abc%abc%abcd",
   243  		"bL_h",
   244  		"bLaH",
   245  		"bLa_",
   246  		"bLah",
   247  		"mi%Sip%",
   248  		"mi%sip%",
   249  		"xxx%zzy%f",
   250  		"xxxx%zzy%f",
   251  		"xxxx%zzy%fffff",
   252  		"xy%xyz",
   253  		"xy%z%xyz",
   254  	}
   255  
   256  	targets := []string{
   257  		"%",
   258  		"%%%%%%%%a%%%%%%%%b%%%%%%%%c%%%%%%%%",
   259  		"%abc%",
   260  		".a",
   261  		".a.",
   262  		".a.a",
   263  		".a.aa",
   264  		".a.b",
   265  		".a.bcd",
   266  		".aa.",
   267  		".ab",
   268  		".ab.ab.ab.cd.cd.",
   269  		".ab.cd.ab.cd.abcd.",
   270  		".axb.cxd.ab.cd.abcd.",
   271  		".axb.cxd.ab.cd.abcd.xy",
   272  		".axb.cyd.ab.cyd.axbcd.",
   273  		".zip",
   274  		"A12b12",
   275  		"XYXYXYZYXYz",
   276  		"a",
   277  		"a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%a%",
   278  		"a%abab",
   279  		"a%ar",
   280  		"a%r",
   281  		"a.",
   282  		"a.a",
   283  		"a.a.zip",
   284  		"a.a.zippo",
   285  		"a.ab.ab.ab.cd.cd.xy",
   286  		"a.b",
   287  		"a.bcd",
   288  		"a.zip",
   289  		"a12B12",
   290  		"a12b12",
   291  		"aAazz",
   292  		"aa",
   293  		"aa.",
   294  		"aa.a",
   295  		"aa.ba.ba",
   296  		"aaa",
   297  		"aaaa.zip",
   298  		"aaaaaaaaaaaaaaaa",
   299  		"aaaaaaaaaaaaaaaaa",
   300  		"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",
   301  		"aaabbaabbaab",
   302  		"aaazz",
   303  		"aannn",
   304  		"ab",
   305  		"ab.",
   306  		"ab.ab.cd.ab.cd.abcdxy.",
   307  		"ab.axb.cd.xyab.cyd.axbcd.",
   308  		"ab.axb.cd.xyab.cyd.axbcd.xy",
   309  		"ab.xy",
   310  		"abAbac",
   311  		"abababababababababababababababababababaacacacacacacacadaeafagahaiajakalaaaaaaaaaaaaaaaaaffafagaagggagaaaaaaaab",
   312  		"ababac",
   313  		"abanabnabncd",
   314  		"abanabnabncdef",
   315  		"abancda.bnxyabncdefxy",
   316  		"abancdabnxyabncdef",
   317  		"abancdabnxyabncdefxy",
   318  		"abc",
   319  		"abc%abcd%abcd%abc%abcd",
   320  		"abc%abcd%abcd%abc%abcd%abcd%abc%abcd%abc%abc%abcd",
   321  		"abc%abcd%abcde%abcdef%abcdefg%abcdefgh%abcdefghi%abcdefghij%abcdefghijk%abcdefghijkl%abcdefghijklm%abcdefghijklmn",
   322  		"abcccd",
   323  		"abcd",
   324  		"abcde",
   325  		"abcdx_y",
   326  		"abxy",
   327  		"ax",
   328  		"bLaH",
   329  		"bLaaa",
   330  		"bLah",
   331  		"baa.",
   332  		"caa.ba.ba",
   333  		"miSsissippi",
   334  		"missisSIPpi",
   335  		"mississipPI",
   336  		"mississipissippi",
   337  		"mississippi",
   338  		"oWn",
   339  		"xa",
   340  		"xaab",
   341  		"xab",
   342  		"xab_anabnabncd_xy",
   343  		"xxa",
   344  		"xxab",
   345  		"xxxx%zzzzzzzzy%f",
   346  		"xxxxzzzzzzzzyf",
   347  		"xyxyxyxyz",
   348  		"xyxyxyzyxyz",
   349  		"xyz.bcd",
   350  		"zip"}
   351  
   352  	want := map[int][]int{
   353  		0:  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   354  		1:  {11, 12, 13, 14, 15, 21, 28, 38, 44, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 75, 85},
   355  		2:  {1, 2, 8, 11, 12, 13, 14, 15, 28, 30, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 85},
   356  		3:  {1, 2, 8, 11, 12, 13, 14, 15, 28, 30, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 85},
   357  		4:  {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38, 40, 48, 49, 50, 51, 52, 58, 74, 75, 92},
   358  		5:  {16, 26, 27, 31, 40},
   359  		6:  {16, 26, 31, 40},
   360  		7:  {16, 26, 31, 40},
   361  		8:  {17, 32, 33},
   362  		9:  {},
   363  		10: {53},
   364  		11: {77},
   365  		12: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   366  		13: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   367  		14: {26, 40},
   368  		15: {26, 40},
   369  		16: {26, 31, 40},
   370  		17: {20, 42, 43, 54},
   371  		18: {54},
   372  		19: {},
   373  		20: {54},
   374  		21: {},
   375  		22: {6, 9, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 54, 72, 74, 75, 83},
   376  		23: {9, 36, 39, 41, 42, 43, 44, 54, 72, 74, 83},
   377  		24: {44},
   378  		25: {11, 12, 13, 14, 15, 28, 49, 50, 51, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 85},
   379  		26: {55},
   380  		27: {65},
   381  		28: {78},
   382  		29: {79},
   383  		30: {81},
   384  		31: {76, 78, 79, 80},
   385  		32: {16, 26, 27, 31, 40, 93},
   386  		33: {16, 26, 31, 40, 93},
   387  		34: {16, 26, 31, 40, 93},
   388  		35: {3},
   389  		36: {18},
   390  		37: {0, 19},
   391  		38: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   392  		39: {},
   393  		40: {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   394  		41: {2, 8, 30, 56, 62, 63, 65, 66, 92},
   395  		42: {11, 12, 13, 15, 50, 67},
   396  		43: {5, 6, 7, 8, 11, 12, 13, 14, 15, 25, 26, 27, 28, 29, 30, 31, 37, 38, 40, 49, 50, 51, 52, 58, 75, 92},
   397  		44: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93},
   398  		45: {26, 40},
   399  		46: {11, 12, 13, 14, 15, 28, 49, 50, 51, 54, 57, 58, 59, 60, 62, 63, 64, 67, 68, 85},
   400  		47: {24},
   401  		48: {25, 29},
   402  		49: {31},
   403  		50: {71},
   404  		51: {73},
   405  		52: {3, 24, 35, 47, 70, 82},
   406  		53: {3, 35, 82},
   407  		54: {2, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 37, 38, 40, 41, 42, 43, 44, 45, 46, 74, 75, 83, 85},
   408  		55: {4, 10, 36, 39, 84},
   409  		56: {9, 39, 40, 41, 42, 43, 44, 45, 74, 75, 83},
   410  		57: {49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69},
   411  		58: {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70},
   412  		59: {20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69},
   413  		60: {26, 40},
   414  		61: {20, 42, 43, 54},
   415  		62: {43, 54},
   416  		63: {},
   417  		64: {21, 29, 43, 44, 47, 54},
   418  		65: {34, 45},
   419  		66: {},
   420  		67: {24, 35, 47, 70},
   421  		68: {51, 52, 58, 60},
   422  		69: {51, 58, 60},
   423  		70: {61},
   424  		71: {},
   425  		72: {64},
   426  		73: {},
   427  		74: {63},
   428  		75: {73},
   429  		76: {71},
   430  		77: {71, 73},
   431  		78: {73},
   432  		79: {},
   433  		80: {76, 78, 79, 80},
   434  		81: {88, 89},
   435  		82: {88, 89},
   436  		83: {},
   437  		84: {90, 91},
   438  		85: {91},
   439  	}
   440  
   441  	cvey.Convey("", t, func() {
   442  		for i := 0; i < len(patterns); i++ {
   443  			for j := 0; j < len(targets); j++ {
   444  
   445  				//fmt.Println(pat[i], str[j])
   446  				ret := WildcardMatch(patterns[i], targets[j])
   447  				resArr := want[i]
   448  				idx := sort.SearchInts(resArr, j)
   449  				if idx >= len(resArr) || resArr[idx] != j {
   450  					cvey.So(ret, cvey.ShouldBeFalse)
   451  				} else {
   452  					cvey.So(ret, cvey.ShouldBeTrue)
   453  				}
   454  			}
   455  		}
   456  	})
   457  }
   458  
   459  func TestGetSimpleExprValue(t *testing.T) {
   460  	ctx := context.TODO()
   461  	cvey.Convey("", t, func() {
   462  		type args struct {
   463  			sql     string
   464  			wantErr bool
   465  			want    interface{}
   466  		}
   467  
   468  		kases := []args{
   469  			{"set @@x=1", false, 1},
   470  			{"set @@x=-1", false, -1},
   471  			{"set @@x=1.0", false, "1"},
   472  			{"set @@x=-1.0", false, "-1"},
   473  			{fmt.Sprintf("set @@x=%d", math.MaxInt64), false, math.MaxInt64},
   474  			{fmt.Sprintf("set @@x=%d", -math.MaxInt64), false, -math.MaxInt64},
   475  			{"set @@x=true", false, true},
   476  			{"set @@x=false", false, false},
   477  			{"set @@x=on", false, "on"},
   478  			{"set @@x=off", false, "off"},
   479  			{"set @@x=abc", false, "abc"},
   480  			{"set @@x=null", false, nil},
   481  			{"set @@x=-null", false, nil},
   482  			{"set @@x=-x", true, nil},
   483  		}
   484  		ctrl := gomock.NewController(t)
   485  		ses := NewSession(&FakeProtocol{}, testutil.NewProc().Mp(), config.NewParameterUnit(nil, mock_frontend.NewMockEngine(ctrl), mock_frontend.NewMockTxnClient(ctrl), nil, nil), nil, false)
   486  		ses.txnCompileCtx.SetProcess(testutil.NewProc())
   487  		for _, kase := range kases {
   488  			stmt, err := parsers.ParseOne(ctx, dialect.MYSQL, kase.sql)
   489  			cvey.So(err, cvey.ShouldBeNil)
   490  
   491  			sv, ok := stmt.(*tree.SetVar)
   492  			cvey.So(ok, cvey.ShouldBeTrue)
   493  			value, err := GetSimpleExprValue(sv.Assignments[0].Value, ses)
   494  			if kase.wantErr {
   495  				cvey.So(err, cvey.ShouldNotBeNil)
   496  			} else {
   497  				cvey.So(err, cvey.ShouldBeNil)
   498  				cvey.So(value, cvey.ShouldEqual, kase.want)
   499  			}
   500  		}
   501  
   502  	})
   503  }
   504  
   505  func TestFileExists(t *testing.T) {
   506  	cvey.Convey("test file exists", t, func() {
   507  		exist, err := fileExists("test.txt")
   508  		cvey.So(err, cvey.ShouldBeNil)
   509  		cvey.So(exist, cvey.ShouldBeFalse)
   510  	})
   511  }
   512  
   513  func TestGetAttrFromTableDef(t *testing.T) {
   514  	cvey.Convey("test get attr from table def", t, func() {
   515  		want := []string{"id", "a", "b"}
   516  		defs := []engine.TableDef{
   517  			&engine.AttributeDef{
   518  				Attr: engine.Attribute{
   519  					Name: "id",
   520  				},
   521  			},
   522  			&engine.AttributeDef{
   523  				Attr: engine.Attribute{
   524  					Name: "a",
   525  				},
   526  			},
   527  			&engine.AttributeDef{
   528  				Attr: engine.Attribute{
   529  					Name: "b",
   530  				},
   531  			},
   532  			&engine.AttributeDef{
   533  				Attr: engine.Attribute{
   534  					Name:    "c",
   535  					IsRowId: true,
   536  				},
   537  			},
   538  			&engine.AttributeDef{
   539  				Attr: engine.Attribute{
   540  					Name:     "d",
   541  					IsHidden: true,
   542  				},
   543  			},
   544  		}
   545  		act, isView, err := getAttrFromTableDef(defs)
   546  		cvey.So(err, cvey.ShouldBeNil)
   547  		cvey.So(isView, cvey.ShouldBeFalse)
   548  		cvey.So(act, cvey.ShouldResemble, want)
   549  	})
   550  }
   551  
   552  func TestGetDDL(t *testing.T) {
   553  	ctx := context.TODO()
   554  
   555  	cvey.Convey("test get ddl", t, func() {
   556  		ctrl := gomock.NewController(t)
   557  		defer ctrl.Finish()
   558  		want0 := []byte("create database test;")
   559  		want1 := []byte("create table test.t1 (id int);")
   560  		rs0 := []interface{}{&MysqlResultSet{Data: [][]interface{}{{want0, want0}}}}
   561  		rs1 := []interface{}{&MysqlResultSet{Data: [][]interface{}{{want0, want1}}}}
   562  		bh := mock_frontend.NewMockBackgroundExec(ctrl)
   563  
   564  		bh.EXPECT().ClearExecResultSet().Return().AnyTimes()
   565  		bh.EXPECT().Close().Return().AnyTimes()
   566  		cnt := -1
   567  		bh.EXPECT().GetExecResultSet().DoAndReturn(func() ([]interface{}, error) {
   568  			cnt++
   569  			if cnt == 0 {
   570  				return rs0, nil
   571  			}
   572  			return rs1, nil
   573  		}).AnyTimes()
   574  		bh.EXPECT().Exec(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
   575  		ret, err := getDDL(bh, ctx, "")
   576  		cvey.So(err, cvey.ShouldBeNil)
   577  		cvey.So(ret, cvey.ShouldResemble, string(want0))
   578  		ret, err = getDDL(bh, ctx, "")
   579  		cvey.So(err, cvey.ShouldBeNil)
   580  		cvey.So(ret, cvey.ShouldResemble, string(want1))
   581  	})
   582  }
   583  
   584  func TestConvertValueBat2Str(t *testing.T) {
   585  	var (
   586  		typs = []types.Type{
   587  			types.T_bool.ToType(),
   588  			types.T_int8.ToType(),
   589  			types.T_int16.ToType(),
   590  			types.T_int32.ToType(),
   591  			types.T_int64.ToType(),
   592  			types.T_uint8.ToType(),
   593  			types.T_uint16.ToType(),
   594  			types.T_uint32.ToType(),
   595  			types.T_uint64.ToType(),
   596  			types.T_float32.ToType(),
   597  			types.T_float64.ToType(),
   598  			types.T_decimal64.ToType(),
   599  			types.T_decimal128.ToType(),
   600  			types.T_date.ToType(),
   601  			types.T_datetime.ToType(),
   602  			types.T_timestamp.ToType(),
   603  			types.T_varchar.ToType(),
   604  			types.T_char.ToType(),
   605  			types.T_json.ToType(),
   606  		}
   607  	)
   608  	before := testutil.TestUtilMp.CurrNB()
   609  	bat := testutil.NewBatch(typs, true, 5, testutil.TestUtilMp)
   610  	rbat, err := convertValueBat2Str(context.TODO(), bat, testutil.TestUtilMp, time.Local)
   611  	require.Nil(t, err)
   612  	require.NotNil(t, rbat)
   613  	bat.Clean(testutil.TestUtilMp)
   614  	rbat.Clean(testutil.TestUtilMp)
   615  	after := testutil.TestUtilMp.CurrNB()
   616  	require.Equal(t, before, after)
   617  }
   618  
   619  func TestGenDumpFileName(t *testing.T) {
   620  	base := "test.sql"
   621  	want := "test_1.sql"
   622  	got := genDumpFileName(base, 1)
   623  	require.Equal(t, want, got)
   624  }
   625  
   626  func TestCreateDumpFile(t *testing.T) {
   627  	base := "test_dump_" + time.Now().Format("20060102150405") + ".sql"
   628  	var f *os.File
   629  	defer func() {
   630  		if f != nil {
   631  			f.Close()
   632  		}
   633  		os.RemoveAll(base)
   634  	}()
   635  	f, err := createDumpFile(context.TODO(), base)
   636  	require.Nil(t, err)
   637  	require.NotNil(t, f)
   638  }
   639  
   640  func TestWriteDump2File(t *testing.T) {
   641  	ctx := context.TODO()
   642  	base := "test_dump_" + time.Now().Format("20060102150405") + ".sql"
   643  	var f *os.File
   644  	defer func() {
   645  		if f != nil {
   646  			f.Close()
   647  		}
   648  		removeFile(base, 1)
   649  		removeFile(base, 2)
   650  	}()
   651  	f, err := createDumpFile(ctx, base)
   652  	require.Nil(t, err)
   653  	require.NotNil(t, f)
   654  	dump := &tree.MoDump{
   655  		OutFile:     base,
   656  		MaxFileSize: 1,
   657  	}
   658  	buf := bytes.NewBufferString("test")
   659  	curFileSize, curFileIdx := int64(0), int64(1)
   660  	_, _, _, err = writeDump2File(ctx, buf, dump, f, curFileIdx, curFileSize)
   661  	require.NotNil(t, err)
   662  	dump.MaxFileSize = 1024
   663  	bufSize := buf.Len()
   664  	f, curFileIdx, curFileSize, err = writeDump2File(ctx, buf, dump, f, curFileIdx, curFileSize)
   665  	require.Nil(t, err)
   666  	require.NotNil(t, f)
   667  	require.Equal(t, int64(1), curFileIdx)
   668  	require.Equal(t, int64(bufSize), curFileSize)
   669  	dump.MaxFileSize = 9
   670  	buf.WriteString("123456")
   671  	bufSize = buf.Len()
   672  	f, curFileIdx, curFileSize, err = writeDump2File(ctx, buf, dump, f, curFileIdx, curFileSize)
   673  	require.Nil(t, err)
   674  	require.NotNil(t, f)
   675  	require.Equal(t, int64(2), curFileIdx)
   676  	require.Equal(t, int64(bufSize), curFileSize)
   677  }
   678  
   679  func TestMaybeAppendExtension(t *testing.T) {
   680  	base := "test"
   681  	want := "test.sql"
   682  	got := maybeAppendExtension(base)
   683  	require.Equal(t, want, got)
   684  	got = maybeAppendExtension(want)
   685  	require.Equal(t, want, got)
   686  }