github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/embedded/mock.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 embedded
    15  
    16  import (
    17  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    18  	"github.com/whtcorpsinc/BerolinaSQL/auth"
    19  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    20  	"github.com/whtcorpsinc/milevadb/petri"
    21  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    22  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    23  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    24  	"github.com/whtcorpsinc/milevadb/types"
    25  )
    26  
    27  func newLongType() types.FieldType {
    28  	return *(types.NewFieldType(allegrosql.TypeLong))
    29  }
    30  
    31  func newStringType() types.FieldType {
    32  	ft := types.NewFieldType(allegrosql.TypeVarchar)
    33  	ft.Charset, ft.DefCauslate = types.DefaultCharsetForType(allegrosql.TypeVarchar)
    34  	return *ft
    35  }
    36  
    37  func newDateType() types.FieldType {
    38  	ft := types.NewFieldType(allegrosql.TypeDate)
    39  	return *ft
    40  }
    41  
    42  // MockSignedBlock is only used for plan related tests.
    43  func MockSignedBlock() *perceptron.BlockInfo {
    44  	// column: a, b, c, d, e, c_str, d_str, e_str, f, g
    45  	// PK: a
    46  	// indices: c_d_e, e, f, g, f_g, c_d_e_str, c_d_e_str_prefix
    47  	indices := []*perceptron.IndexInfo{
    48  		{
    49  			Name: perceptron.NewCIStr("c_d_e"),
    50  			DeferredCausets: []*perceptron.IndexDeferredCauset{
    51  				{
    52  					Name:   perceptron.NewCIStr("c"),
    53  					Length: types.UnspecifiedLength,
    54  					Offset: 2,
    55  				},
    56  				{
    57  					Name:   perceptron.NewCIStr("d"),
    58  					Length: types.UnspecifiedLength,
    59  					Offset: 3,
    60  				},
    61  				{
    62  					Name:   perceptron.NewCIStr("e"),
    63  					Length: types.UnspecifiedLength,
    64  					Offset: 4,
    65  				},
    66  			},
    67  			State:  perceptron.StatePublic,
    68  			Unique: true,
    69  		},
    70  		{
    71  			Name: perceptron.NewCIStr("e"),
    72  			DeferredCausets: []*perceptron.IndexDeferredCauset{
    73  				{
    74  					Name:   perceptron.NewCIStr("e"),
    75  					Length: types.UnspecifiedLength,
    76  					Offset: 4,
    77  				},
    78  			},
    79  			State:  perceptron.StateWriteOnly,
    80  			Unique: true,
    81  		},
    82  		{
    83  			Name: perceptron.NewCIStr("f"),
    84  			DeferredCausets: []*perceptron.IndexDeferredCauset{
    85  				{
    86  					Name:   perceptron.NewCIStr("f"),
    87  					Length: types.UnspecifiedLength,
    88  					Offset: 8,
    89  				},
    90  			},
    91  			State:  perceptron.StatePublic,
    92  			Unique: true,
    93  		},
    94  		{
    95  			Name: perceptron.NewCIStr("g"),
    96  			DeferredCausets: []*perceptron.IndexDeferredCauset{
    97  				{
    98  					Name:   perceptron.NewCIStr("g"),
    99  					Length: types.UnspecifiedLength,
   100  					Offset: 9,
   101  				},
   102  			},
   103  			State: perceptron.StatePublic,
   104  		},
   105  		{
   106  			Name: perceptron.NewCIStr("f_g"),
   107  			DeferredCausets: []*perceptron.IndexDeferredCauset{
   108  				{
   109  					Name:   perceptron.NewCIStr("f"),
   110  					Length: types.UnspecifiedLength,
   111  					Offset: 8,
   112  				},
   113  				{
   114  					Name:   perceptron.NewCIStr("g"),
   115  					Length: types.UnspecifiedLength,
   116  					Offset: 9,
   117  				},
   118  			},
   119  			State:  perceptron.StatePublic,
   120  			Unique: true,
   121  		},
   122  		{
   123  			Name: perceptron.NewCIStr("c_d_e_str"),
   124  			DeferredCausets: []*perceptron.IndexDeferredCauset{
   125  				{
   126  					Name:   perceptron.NewCIStr("c_str"),
   127  					Length: types.UnspecifiedLength,
   128  					Offset: 5,
   129  				},
   130  				{
   131  					Name:   perceptron.NewCIStr("d_str"),
   132  					Length: types.UnspecifiedLength,
   133  					Offset: 6,
   134  				},
   135  				{
   136  					Name:   perceptron.NewCIStr("e_str"),
   137  					Length: types.UnspecifiedLength,
   138  					Offset: 7,
   139  				},
   140  			},
   141  			State: perceptron.StatePublic,
   142  		},
   143  		{
   144  			Name: perceptron.NewCIStr("e_d_c_str_prefix"),
   145  			DeferredCausets: []*perceptron.IndexDeferredCauset{
   146  				{
   147  					Name:   perceptron.NewCIStr("e_str"),
   148  					Length: types.UnspecifiedLength,
   149  					Offset: 7,
   150  				},
   151  				{
   152  					Name:   perceptron.NewCIStr("d_str"),
   153  					Length: types.UnspecifiedLength,
   154  					Offset: 6,
   155  				},
   156  				{
   157  					Name:   perceptron.NewCIStr("c_str"),
   158  					Length: 10,
   159  					Offset: 5,
   160  				},
   161  			},
   162  			State: perceptron.StatePublic,
   163  		},
   164  	}
   165  	pkDeferredCauset := &perceptron.DeferredCausetInfo{
   166  		State:     perceptron.StatePublic,
   167  		Offset:    0,
   168  		Name:      perceptron.NewCIStr("a"),
   169  		FieldType: newLongType(),
   170  		ID:        1,
   171  	}
   172  	col0 := &perceptron.DeferredCausetInfo{
   173  		State:     perceptron.StatePublic,
   174  		Offset:    1,
   175  		Name:      perceptron.NewCIStr("b"),
   176  		FieldType: newLongType(),
   177  		ID:        2,
   178  	}
   179  	col1 := &perceptron.DeferredCausetInfo{
   180  		State:     perceptron.StatePublic,
   181  		Offset:    2,
   182  		Name:      perceptron.NewCIStr("c"),
   183  		FieldType: newLongType(),
   184  		ID:        3,
   185  	}
   186  	col2 := &perceptron.DeferredCausetInfo{
   187  		State:     perceptron.StatePublic,
   188  		Offset:    3,
   189  		Name:      perceptron.NewCIStr("d"),
   190  		FieldType: newLongType(),
   191  		ID:        4,
   192  	}
   193  	col3 := &perceptron.DeferredCausetInfo{
   194  		State:     perceptron.StatePublic,
   195  		Offset:    4,
   196  		Name:      perceptron.NewCIStr("e"),
   197  		FieldType: newLongType(),
   198  		ID:        5,
   199  	}
   200  	colStr1 := &perceptron.DeferredCausetInfo{
   201  		State:     perceptron.StatePublic,
   202  		Offset:    5,
   203  		Name:      perceptron.NewCIStr("c_str"),
   204  		FieldType: newStringType(),
   205  		ID:        6,
   206  	}
   207  	colStr2 := &perceptron.DeferredCausetInfo{
   208  		State:     perceptron.StatePublic,
   209  		Offset:    6,
   210  		Name:      perceptron.NewCIStr("d_str"),
   211  		FieldType: newStringType(),
   212  		ID:        7,
   213  	}
   214  	colStr3 := &perceptron.DeferredCausetInfo{
   215  		State:     perceptron.StatePublic,
   216  		Offset:    7,
   217  		Name:      perceptron.NewCIStr("e_str"),
   218  		FieldType: newStringType(),
   219  		ID:        8,
   220  	}
   221  	col4 := &perceptron.DeferredCausetInfo{
   222  		State:     perceptron.StatePublic,
   223  		Offset:    8,
   224  		Name:      perceptron.NewCIStr("f"),
   225  		FieldType: newLongType(),
   226  		ID:        9,
   227  	}
   228  	col5 := &perceptron.DeferredCausetInfo{
   229  		State:     perceptron.StatePublic,
   230  		Offset:    9,
   231  		Name:      perceptron.NewCIStr("g"),
   232  		FieldType: newLongType(),
   233  		ID:        10,
   234  	}
   235  	col6 := &perceptron.DeferredCausetInfo{
   236  		State:     perceptron.StatePublic,
   237  		Offset:    10,
   238  		Name:      perceptron.NewCIStr("h"),
   239  		FieldType: newLongType(),
   240  		ID:        11,
   241  	}
   242  	col7 := &perceptron.DeferredCausetInfo{
   243  		State:     perceptron.StatePublic,
   244  		Offset:    11,
   245  		Name:      perceptron.NewCIStr("i_date"),
   246  		FieldType: newDateType(),
   247  		ID:        12,
   248  	}
   249  	pkDeferredCauset.Flag = allegrosql.PriKeyFlag | allegrosql.NotNullFlag
   250  	// DeferredCauset 'b', 'c', 'd', 'f', 'g' is not null.
   251  	col0.Flag = allegrosql.NotNullFlag
   252  	col1.Flag = allegrosql.NotNullFlag
   253  	col2.Flag = allegrosql.NotNullFlag
   254  	col4.Flag = allegrosql.NotNullFlag
   255  	col5.Flag = allegrosql.NotNullFlag
   256  	col6.Flag = allegrosql.NoDefaultValueFlag
   257  	causet := &perceptron.BlockInfo{
   258  		DeferredCausets: []*perceptron.DeferredCausetInfo{pkDeferredCauset, col0, col1, col2, col3, colStr1, colStr2, colStr3, col4, col5, col6, col7},
   259  		Indices:         indices,
   260  		Name:            perceptron.NewCIStr("t"),
   261  		PKIsHandle:      true,
   262  	}
   263  	return causet
   264  }
   265  
   266  // MockUnsignedBlock is only used for plan related tests.
   267  func MockUnsignedBlock() *perceptron.BlockInfo {
   268  	// column: a, b
   269  	// PK: a
   270  	// indeices: b
   271  	indices := []*perceptron.IndexInfo{
   272  		{
   273  			Name: perceptron.NewCIStr("b"),
   274  			DeferredCausets: []*perceptron.IndexDeferredCauset{
   275  				{
   276  					Name:   perceptron.NewCIStr("b"),
   277  					Length: types.UnspecifiedLength,
   278  					Offset: 1,
   279  				},
   280  			},
   281  			State:  perceptron.StatePublic,
   282  			Unique: true,
   283  		},
   284  		{
   285  			Name: perceptron.NewCIStr("b_c"),
   286  			DeferredCausets: []*perceptron.IndexDeferredCauset{
   287  				{
   288  					Name:   perceptron.NewCIStr("b"),
   289  					Length: types.UnspecifiedLength,
   290  					Offset: 1,
   291  				},
   292  				{
   293  					Name:   perceptron.NewCIStr("c"),
   294  					Length: types.UnspecifiedLength,
   295  					Offset: 2,
   296  				},
   297  			},
   298  			State: perceptron.StatePublic,
   299  		},
   300  	}
   301  	pkDeferredCauset := &perceptron.DeferredCausetInfo{
   302  		State:     perceptron.StatePublic,
   303  		Offset:    0,
   304  		Name:      perceptron.NewCIStr("a"),
   305  		FieldType: newLongType(),
   306  		ID:        1,
   307  	}
   308  	col0 := &perceptron.DeferredCausetInfo{
   309  		State:     perceptron.StatePublic,
   310  		Offset:    1,
   311  		Name:      perceptron.NewCIStr("b"),
   312  		FieldType: newLongType(),
   313  		ID:        2,
   314  	}
   315  	col1 := &perceptron.DeferredCausetInfo{
   316  		State:     perceptron.StatePublic,
   317  		Offset:    2,
   318  		Name:      perceptron.NewCIStr("c"),
   319  		FieldType: newLongType(),
   320  		ID:        3,
   321  	}
   322  	pkDeferredCauset.Flag = allegrosql.PriKeyFlag | allegrosql.NotNullFlag | allegrosql.UnsignedFlag
   323  	// DeferredCauset 'b', 'c', 'd', 'f', 'g' is not null.
   324  	col0.Flag = allegrosql.NotNullFlag
   325  	col1.Flag = allegrosql.UnsignedFlag
   326  	causet := &perceptron.BlockInfo{
   327  		DeferredCausets: []*perceptron.DeferredCausetInfo{pkDeferredCauset, col0, col1},
   328  		Indices:         indices,
   329  		Name:            perceptron.NewCIStr("t2"),
   330  		PKIsHandle:      true,
   331  	}
   332  	return causet
   333  }
   334  
   335  // MockView is only used for plan related tests.
   336  func MockView() *perceptron.BlockInfo {
   337  	selectStmt := "select b,c,d from t"
   338  	col0 := &perceptron.DeferredCausetInfo{
   339  		State:  perceptron.StatePublic,
   340  		Offset: 0,
   341  		Name:   perceptron.NewCIStr("b"),
   342  		ID:     1,
   343  	}
   344  	col1 := &perceptron.DeferredCausetInfo{
   345  		State:  perceptron.StatePublic,
   346  		Offset: 1,
   347  		Name:   perceptron.NewCIStr("c"),
   348  		ID:     2,
   349  	}
   350  	col2 := &perceptron.DeferredCausetInfo{
   351  		State:  perceptron.StatePublic,
   352  		Offset: 2,
   353  		Name:   perceptron.NewCIStr("d"),
   354  		ID:     3,
   355  	}
   356  	view := &perceptron.ViewInfo{SelectStmt: selectStmt, Security: perceptron.SecurityDefiner, Definer: &auth.UserIdentity{Username: "root", Hostname: ""}, DefCauss: []perceptron.CIStr{col0.Name, col1.Name, col2.Name}}
   357  	causet := &perceptron.BlockInfo{
   358  		Name:            perceptron.NewCIStr("v"),
   359  		DeferredCausets: []*perceptron.DeferredCausetInfo{col0, col1, col2},
   360  		View:            view,
   361  	}
   362  	return causet
   363  }
   364  
   365  // MockContext is only used for plan related tests.
   366  func MockContext() stochastikctx.Context {
   367  	ctx := mock.NewContext()
   368  	ctx.CausetStore = &mock.CausetStore{
   369  		Client: &mock.Client{},
   370  	}
   371  	ctx.GetStochastikVars().CurrentDB = "test"
   372  	do := &petri.Petri{}
   373  	do.CreateStatsHandle(ctx)
   374  	petri.BindPetri(ctx, do)
   375  	return ctx
   376  }
   377  
   378  // MockPartitionSchemaReplicant mocks an info schemaReplicant for partition causet.
   379  func MockPartitionSchemaReplicant(definitions []perceptron.PartitionDefinition) schemareplicant.SchemaReplicant {
   380  	blockInfo := MockSignedBlock()
   381  	defcaus := make([]*perceptron.DeferredCausetInfo, 0, len(blockInfo.DeferredCausets))
   382  	defcaus = append(defcaus, blockInfo.DeferredCausets...)
   383  	last := blockInfo.DeferredCausets[len(blockInfo.DeferredCausets)-1]
   384  	defcaus = append(defcaus, &perceptron.DeferredCausetInfo{
   385  		State:     perceptron.StatePublic,
   386  		Offset:    last.Offset + 1,
   387  		Name:      perceptron.NewCIStr("ptn"),
   388  		FieldType: newLongType(),
   389  		ID:        last.ID + 1,
   390  	})
   391  	partition := &perceptron.PartitionInfo{
   392  		Type:        perceptron.PartitionTypeRange,
   393  		Expr:        "ptn",
   394  		Enable:      true,
   395  		Definitions: definitions,
   396  	}
   397  	blockInfo.DeferredCausets = defcaus
   398  	blockInfo.Partition = partition
   399  	is := schemareplicant.MockSchemaReplicant([]*perceptron.BlockInfo{blockInfo})
   400  	return is
   401  }