github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/operations_test.go (about)

     1  // Copyright 2022 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 memoryengine
    16  
    17  import (
    18  	"encoding"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    25  	"github.com/matrixorigin/matrixone/pkg/container/types"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    27  	"github.com/matrixorigin/matrixone/pkg/testutil"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    29  )
    30  
    31  func TestOperationsMarshalAndUnmarshal(t *testing.T) {
    32  	// prepare data structure
    33  	mp := mpool.MustNewZero()
    34  	colA := testutil.NewVector(
    35  		5,
    36  		types.T_int64.ToType(),
    37  		mp,
    38  		false,
    39  		[]int64{
    40  			1, 2, 3, 4, 5,
    41  		},
    42  	)
    43  	colB := testutil.NewVector(
    44  		5,
    45  		types.T_int64.ToType(),
    46  		mp,
    47  		false,
    48  		[]int64{
    49  			6, 7, 8, 9, 10,
    50  		},
    51  	)
    52  	bat := batch.New(false, []string{"a", "b"})
    53  	bat.Vecs[0] = colA
    54  	bat.Vecs[1] = colB
    55  	bat.SetRowCount(5)
    56  	bat.Cnt = 1
    57  
    58  	tableDefA := &engine.AttributeDef{
    59  		Attr: engine.Attribute{
    60  			Name:    "a",
    61  			Type:    types.T_int64.ToType(),
    62  			Primary: true,
    63  		},
    64  	}
    65  	tableDefB := &engine.AttributeDef{
    66  		Attr: engine.Attribute{
    67  			Name:    "b",
    68  			Type:    types.T_int64.ToType(),
    69  			Primary: false,
    70  		},
    71  	}
    72  	tableDefC := &engine.ConstraintDef{
    73  		Cts: []engine.Constraint{
    74  			&engine.PrimaryKeyDef{
    75  				Pkey: &plan.PrimaryKeyDef{
    76  					PkeyColName: "a",
    77  					Names:       []string{"a"},
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	// prepare test cases
    84  	cases := []struct {
    85  		in           encoding.BinaryMarshaler
    86  		out          encoding.BinaryUnmarshaler
    87  		validateFunc validateFunc
    88  	}{
    89  		{
    90  			in: &OpenDatabaseResp{
    91  				ID:   ID(1),
    92  				Name: "testing",
    93  			},
    94  			out:          &OpenDatabaseResp{},
    95  			validateFunc: require.Equal,
    96  		},
    97  		{
    98  			in: &OpenDatabaseReq{
    99  				AccessInfo: AccessInfo{
   100  					AccountID: 1,
   101  					UserID:    2,
   102  					RoleID:    3,
   103  				},
   104  				Name: "testing",
   105  			},
   106  			out:          &OpenDatabaseReq{},
   107  			validateFunc: require.Equal,
   108  		},
   109  		{
   110  			in: &CreateDatabaseResp{
   111  				ID: ID(1),
   112  			},
   113  			out:          &CreateDatabaseResp{},
   114  			validateFunc: require.Equal,
   115  		},
   116  		{
   117  			in: &CreateDatabaseReq{
   118  				ID: ID(1),
   119  				AccessInfo: AccessInfo{
   120  					AccountID: 1,
   121  					UserID:    2,
   122  					RoleID:    3,
   123  				},
   124  				Name: "testing",
   125  			},
   126  			out:          &CreateDatabaseReq{},
   127  			validateFunc: require.Equal,
   128  		},
   129  		{
   130  			in: &GetDatabasesResp{
   131  				Names: []string{
   132  					"test1",
   133  					"test2",
   134  					"test3",
   135  				},
   136  			},
   137  			out:          &GetDatabasesResp{},
   138  			validateFunc: require.Equal,
   139  		},
   140  		{
   141  			in: &GetDatabasesReq{
   142  				AccessInfo: AccessInfo{
   143  					AccountID: 1,
   144  					UserID:    2,
   145  					RoleID:    3,
   146  				},
   147  			},
   148  			out:          &GetDatabasesReq{},
   149  			validateFunc: require.Equal,
   150  		},
   151  		{
   152  			in: &DeleteDatabaseResp{
   153  				ID: ID(1),
   154  			},
   155  			out:          &DeleteDatabaseResp{},
   156  			validateFunc: require.Equal,
   157  		},
   158  		{
   159  			in: &DeleteDatabaseReq{
   160  				AccessInfo: AccessInfo{
   161  					AccountID: 1,
   162  					UserID:    2,
   163  					RoleID:    3,
   164  				},
   165  				Name: "testing",
   166  			},
   167  			out:          &DeleteDatabaseReq{},
   168  			validateFunc: require.Equal,
   169  		},
   170  		{
   171  			in: &OpenRelationResp{
   172  				ID:           ID(1),
   173  				Type:         RelationTable,
   174  				DatabaseName: "database1",
   175  				RelationName: "relation1",
   176  			},
   177  			out:          &OpenRelationResp{},
   178  			validateFunc: require.Equal,
   179  		},
   180  		{
   181  			in: &OpenRelationReq{
   182  				DatabaseID:   ID(1),
   183  				DatabaseName: "database1",
   184  				Name:         "test1",
   185  			},
   186  			out:          &OpenRelationReq{},
   187  			validateFunc: require.Equal,
   188  		},
   189  		{
   190  			in: &CreateRelationResp{
   191  				ID: ID(1),
   192  			},
   193  			out:          &CreateRelationResp{},
   194  			validateFunc: require.Equal,
   195  		},
   196  		{
   197  			in: &CreateRelationReq{
   198  				ID:           ID(1),
   199  				DatabaseID:   ID(2),
   200  				DatabaseName: "database1",
   201  				Name:         "test1",
   202  				Type:         RelationTable,
   203  				Defs: []engine.TableDefPB{
   204  					tableDefA.ToPBVersion(),
   205  					tableDefB.ToPBVersion(),
   206  					tableDefC.ToPBVersion(),
   207  				},
   208  			},
   209  			out:          &CreateRelationReq{},
   210  			validateFunc: require.Equal,
   211  		},
   212  		{
   213  			in: &GetRelationsResp{
   214  				Names: []string{
   215  					"test1",
   216  					"test2",
   217  					"test3",
   218  				},
   219  			},
   220  			out:          &GetRelationsResp{},
   221  			validateFunc: require.Equal,
   222  		},
   223  		{
   224  			in: &GetRelationsReq{
   225  				DatabaseID: ID(1),
   226  			},
   227  			out:          &GetRelationsReq{},
   228  			validateFunc: require.Equal,
   229  		},
   230  		{
   231  			in: &DeleteRelationResp{
   232  				ID: ID(1),
   233  			},
   234  			out:          &DeleteRelationResp{},
   235  			validateFunc: require.Equal,
   236  		},
   237  		{
   238  			in: &DeleteRelationReq{
   239  				DatabaseID:   ID(1),
   240  				DatabaseName: "database1",
   241  				Name:         "test1",
   242  			},
   243  			out:          &DeleteRelationReq{},
   244  			validateFunc: require.Equal,
   245  		},
   246  		{
   247  			in: &GetTableDefsResp{
   248  				Defs: []engine.TableDefPB{
   249  					tableDefA.ToPBVersion(),
   250  					tableDefB.ToPBVersion(),
   251  					tableDefC.ToPBVersion(),
   252  				},
   253  			},
   254  			out:          &GetTableDefsResp{},
   255  			validateFunc: require.Equal,
   256  		},
   257  		{
   258  			in: &GetTableDefsReq{
   259  				TableID: ID(1),
   260  			},
   261  			out:          &GetTableDefsReq{},
   262  			validateFunc: require.Equal,
   263  		},
   264  		{
   265  			in:           &WriteResp{},
   266  			out:          &WriteResp{},
   267  			validateFunc: require.Equal,
   268  		},
   269  		{
   270  			in: &WriteReq{
   271  				TableID:      ID(1),
   272  				DatabaseName: "database1",
   273  				TableName:    "table1",
   274  				Batch:        bat,
   275  			},
   276  			out:          &WriteReq{},
   277  			validateFunc: validateWriteReq,
   278  		},
   279  		{
   280  			in: &NewTableIterResp{
   281  				IterID: ID(1),
   282  			},
   283  			out:          &NewTableIterResp{},
   284  			validateFunc: require.Equal,
   285  		},
   286  		{
   287  			in: &NewTableIterReq{
   288  				TableID: ID(1),
   289  			},
   290  			out:          &NewTableIterReq{},
   291  			validateFunc: require.Equal,
   292  		},
   293  		{
   294  			in: &ReadResp{
   295  				Batch: bat,
   296  			},
   297  			out:          &ReadResp{},
   298  			validateFunc: validateReadResp,
   299  		},
   300  		{
   301  			in: &ReadReq{
   302  				IterID: ID(1),
   303  				ColNames: []string{
   304  					"test1",
   305  					"test2",
   306  					"test3",
   307  				},
   308  			},
   309  			out:          &ReadReq{},
   310  			validateFunc: require.Equal,
   311  		},
   312  		{
   313  			in:           &DeleteResp{},
   314  			out:          &DeleteResp{},
   315  			validateFunc: require.Equal,
   316  		},
   317  		{
   318  			in: &DeleteReq{
   319  				TableID:      ID(10),
   320  				DatabaseName: "database1",
   321  				TableName:    "table1",
   322  				ColumnName:   "colume1",
   323  				Vector:       colB,
   324  			},
   325  			out:          &DeleteReq{},
   326  			validateFunc: validateDeleteReq,
   327  		},
   328  		{
   329  			in: &GetPrimaryKeysReq{
   330  				TableID: ID(11),
   331  			},
   332  			out:          &GetPrimaryKeysReq{},
   333  			validateFunc: require.Equal,
   334  		},
   335  		{
   336  			in: &GetPrimaryKeysResp{
   337  				Attrs: []engine.Attribute{
   338  					{
   339  						Name:    "a",
   340  						Type:    types.T_int64.ToType(),
   341  						Primary: true,
   342  					},
   343  					{
   344  						Name:    "b",
   345  						Type:    types.T_int64.ToType(),
   346  						Primary: false,
   347  					},
   348  				},
   349  			},
   350  			out:          &GetPrimaryKeysResp{},
   351  			validateFunc: require.Equal,
   352  		},
   353  		{
   354  			in: &CloseTableIterReq{
   355  				IterID: ID(12),
   356  			},
   357  			out:          &CloseTableIterReq{},
   358  			validateFunc: require.Equal,
   359  		},
   360  		{
   361  			in:           &CloseTableIterResp{},
   362  			out:          &CloseTableIterResp{},
   363  			validateFunc: require.Equal,
   364  		},
   365  		{
   366  			in: &GetHiddenKeysReq{
   367  				TableID: ID(14),
   368  			},
   369  			out:          &GetHiddenKeysReq{},
   370  			validateFunc: require.Equal,
   371  		},
   372  		{
   373  			in: &GetHiddenKeysResp{
   374  				Attrs: []engine.Attribute{
   375  					{
   376  						Name:    "a",
   377  						Type:    types.T_int64.ToType(),
   378  						Primary: true,
   379  					},
   380  					{
   381  						Name:    "b",
   382  						Type:    types.T_int64.ToType(),
   383  						Primary: false,
   384  					},
   385  				},
   386  			},
   387  			out:          &GetHiddenKeysResp{},
   388  			validateFunc: require.Equal,
   389  		},
   390  		{
   391  			in: &TableStatsReq{
   392  				TableID: ID(15),
   393  			},
   394  			out:          &TableStatsReq{},
   395  			validateFunc: require.Equal,
   396  		},
   397  		{
   398  			in: &TableStatsResp{
   399  				Rows: 16,
   400  			},
   401  			out:          &TableStatsResp{},
   402  			validateFunc: require.Equal,
   403  		},
   404  		{
   405  			in: &GetTableColumnsReq{
   406  				TableID: ID(16),
   407  			},
   408  			out:          &GetTableColumnsReq{},
   409  			validateFunc: require.Equal,
   410  		},
   411  		{
   412  			in: &GetTableColumnsResp{
   413  				Attrs: []engine.Attribute{
   414  					{
   415  						Name:    "a",
   416  						Type:    types.T_int64.ToType(),
   417  						Primary: true,
   418  					},
   419  					{
   420  						Name:    "b",
   421  						Type:    types.T_int64.ToType(),
   422  						Primary: false,
   423  					},
   424  				},
   425  			},
   426  			out:          &GetTableColumnsResp{},
   427  			validateFunc: require.Equal,
   428  		},
   429  		{
   430  			in: &TruncateRelationReq{
   431  				NewTableID:   ID(10),
   432  				OldTableID:   ID(11),
   433  				DatabaseID:   ID(12),
   434  				DatabaseName: "database1",
   435  				Name:         "table1",
   436  			},
   437  			out:          &TruncateRelationReq{},
   438  			validateFunc: require.Equal,
   439  		},
   440  		{
   441  			in: &TruncateRelationResp{
   442  				ID: ID(13),
   443  			},
   444  			out:          &TruncateRelationResp{},
   445  			validateFunc: require.Equal,
   446  		},
   447  		{
   448  			in: &AddTableDefReq{
   449  				TableID:      ID(11),
   450  				DatabaseName: "database1",
   451  				TableName:    "table1",
   452  				Def:          tableDefA.ToPBVersion(),
   453  			},
   454  			out:          &AddTableDefReq{},
   455  			validateFunc: require.Equal,
   456  		},
   457  		{
   458  			in:           &AddTableDefResp{},
   459  			out:          &AddTableDefResp{},
   460  			validateFunc: require.Equal,
   461  		},
   462  		{
   463  			in: &DelTableDefReq{
   464  				TableID:      ID(11),
   465  				DatabaseName: "database1",
   466  				TableName:    "table1",
   467  				Def:          tableDefA.ToPBVersion(),
   468  			},
   469  			out:          &DelTableDefReq{},
   470  			validateFunc: require.Equal,
   471  		},
   472  		{
   473  			in:           &DelTableDefResp{},
   474  			out:          &DelTableDefResp{},
   475  			validateFunc: require.Equal,
   476  		},
   477  		{
   478  			in: &UpdateReq{
   479  				TableID:      ID(1),
   480  				DatabaseName: "database1",
   481  				TableName:    "table1",
   482  				Batch:        bat,
   483  			},
   484  			out:          &UpdateReq{},
   485  			validateFunc: validateUpdateReq,
   486  		},
   487  	}
   488  
   489  	// test all cases
   490  	for i := 0; i < len(cases); i++ {
   491  		testCase := cases[i]
   492  		marshalThenUnmarshal(t, testCase.in, testCase.out)
   493  		testCase.validateFunc(t, testCase.in, testCase.out)
   494  	}
   495  }
   496  
   497  func marshalThenUnmarshal[
   498  	I encoding.BinaryMarshaler,
   499  	O encoding.BinaryUnmarshaler,
   500  ](t *testing.T, in I, out O) {
   501  	data, err := in.MarshalBinary()
   502  	require.NoError(t, err)
   503  
   504  	err = out.UnmarshalBinary(data)
   505  	require.NoError(t, err)
   506  }
   507  
   508  type validateFunc func(
   509  	t require.TestingT,
   510  	expected interface{},
   511  	actual interface{},
   512  	msgAndArgs ...interface{},
   513  )
   514  
   515  func validateWriteReq(
   516  	t require.TestingT,
   517  	expected interface{},
   518  	actual interface{},
   519  	msgAndArgs ...interface{},
   520  ) {
   521  	in, ok := expected.(*WriteReq)
   522  	require.True(t, ok)
   523  	out, ok := actual.(*WriteReq)
   524  	require.True(t, ok)
   525  
   526  	// it is irreversible between vector.Vector.MarshalBinary and
   527  	// vector.Vector.UnmarshalBinary.
   528  	in.Batch.Vecs = nil
   529  	out.Batch.Vecs = nil
   530  	require.Equal(t, in, out)
   531  }
   532  
   533  func validateUpdateReq(
   534  	t require.TestingT,
   535  	expected interface{},
   536  	actual interface{},
   537  	msgAndArgs ...interface{},
   538  ) {
   539  	in, ok := expected.(*UpdateReq)
   540  	require.True(t, ok)
   541  	out, ok := actual.(*UpdateReq)
   542  	require.True(t, ok)
   543  
   544  	// it is irreversible between vector.Vector.MarshalBinary and
   545  	// vector.Vector.UnmarshalBinary.
   546  	in.Batch.Vecs = nil
   547  	out.Batch.Vecs = nil
   548  	require.Equal(t, in, out)
   549  }
   550  
   551  func validateReadResp(
   552  	t require.TestingT,
   553  	expected interface{},
   554  	actual interface{},
   555  	msgAndArgs ...interface{},
   556  ) {
   557  	in, ok := expected.(*ReadResp)
   558  	require.True(t, ok)
   559  	out, ok := actual.(*ReadResp)
   560  	require.True(t, ok)
   561  
   562  	// it is irreversible between vector.Vector.MarshalBinary and
   563  	// vector.Vector.UnmarshalBinary.
   564  	in.Batch.Vecs = nil
   565  	out.Batch.Vecs = nil
   566  	require.Equal(t, in, out)
   567  }
   568  
   569  func validateDeleteReq(
   570  	t require.TestingT,
   571  	expected interface{},
   572  	actual interface{},
   573  	msgAndArgs ...interface{},
   574  ) {
   575  	in, ok := expected.(*DeleteReq)
   576  	require.True(t, ok)
   577  	out, ok := actual.(*DeleteReq)
   578  	require.True(t, ok)
   579  
   580  	// it is irreversible between vector.Vector.MarshalBinary and
   581  	// vector.Vector.UnmarshalBinary.
   582  	in.Vector = nil
   583  	out.Vector = nil
   584  	require.Equal(t, in, out)
   585  }