|
| 1 | +// Copyright 2024 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 logtailreplay |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + |
| 22 | + "github.com/matrixorigin/matrixone/pkg/common/mpool" |
| 23 | + "github.com/matrixorigin/matrixone/pkg/container/batch" |
| 24 | + "github.com/matrixorigin/matrixone/pkg/container/types" |
| 25 | + "github.com/matrixorigin/matrixone/pkg/container/vector" |
| 26 | +) |
| 27 | + |
| 28 | +func TestUpdateCNDataBatch_RemoveTSVector(t *testing.T) { |
| 29 | + mp := mpool.MustNewZero() |
| 30 | + |
| 31 | + // Create a batch with multiple vectors including T_TS type |
| 32 | + bat := batch.NewWithSize(3) |
| 33 | + |
| 34 | + // Create an int64 vector (non-TS) |
| 35 | + vec1 := vector.NewVec(types.T_int64.ToType()) |
| 36 | + vector.AppendFixed(vec1, int64(1), false, mp) |
| 37 | + vector.AppendFixed(vec1, int64(2), false, mp) |
| 38 | + bat.Vecs[0] = vec1 |
| 39 | + |
| 40 | + // Create a T_TS vector (should be removed) |
| 41 | + tsVec1 := vector.NewVec(types.T_TS.ToType()) |
| 42 | + vector.AppendFixed(tsVec1, types.BuildTS(1, 0), false, mp) |
| 43 | + vector.AppendFixed(tsVec1, types.BuildTS(2, 0), false, mp) |
| 44 | + bat.Vecs[1] = tsVec1 |
| 45 | + |
| 46 | + // Create another int64 vector (non-TS) |
| 47 | + vec2 := vector.NewVec(types.T_int64.ToType()) |
| 48 | + vector.AppendFixed(vec2, int64(3), false, mp) |
| 49 | + vector.AppendFixed(vec2, int64(4), false, mp) |
| 50 | + bat.Vecs[2] = vec2 |
| 51 | + |
| 52 | + bat.SetRowCount(2) |
| 53 | + |
| 54 | + // Verify initial state: should have 3 vectors |
| 55 | + require.Equal(t, 3, len(bat.Vecs)) |
| 56 | + require.Equal(t, types.T_int64, bat.Vecs[0].GetType().Oid) |
| 57 | + require.Equal(t, types.T_TS, bat.Vecs[1].GetType().Oid) |
| 58 | + require.Equal(t, types.T_int64, bat.Vecs[2].GetType().Oid) |
| 59 | + |
| 60 | + // Call updateCNDataBatch |
| 61 | + newCommitTS := types.BuildTS(100, 0) |
| 62 | + updateCNDataBatch(bat, newCommitTS, mp) |
| 63 | + |
| 64 | + // Verify T_TS vector is removed and new commitTS vector is added at the end |
| 65 | + require.Equal(t, 3, len(bat.Vecs)) |
| 66 | + require.Equal(t, types.T_int64, bat.Vecs[0].GetType().Oid) |
| 67 | + require.Equal(t, types.T_int64, bat.Vecs[1].GetType().Oid) |
| 68 | + require.Equal(t, types.T_TS, bat.Vecs[2].GetType().Oid) |
| 69 | + |
| 70 | + // Verify the new commitTS vector has the correct value |
| 71 | + require.True(t, bat.Vecs[2].IsConst()) |
| 72 | + tsVal := vector.MustFixedColWithTypeCheck[types.TS](bat.Vecs[2])[0] |
| 73 | + require.Equal(t, newCommitTS, tsVal) |
| 74 | + |
| 75 | + bat.Clean(mp) |
| 76 | +} |
| 77 | + |
| 78 | +func TestUpdateCNDataBatch_NoTSVector(t *testing.T) { |
| 79 | + mp := mpool.MustNewZero() |
| 80 | + |
| 81 | + // Create a batch without T_TS vectors |
| 82 | + bat := batch.NewWithSize(2) |
| 83 | + |
| 84 | + vec1 := vector.NewVec(types.T_int64.ToType()) |
| 85 | + vector.AppendFixed(vec1, int64(1), false, mp) |
| 86 | + vector.AppendFixed(vec1, int64(2), false, mp) |
| 87 | + bat.Vecs[0] = vec1 |
| 88 | + |
| 89 | + vec2 := vector.NewVec(types.T_int64.ToType()) |
| 90 | + vector.AppendFixed(vec2, int64(3), false, mp) |
| 91 | + vector.AppendFixed(vec2, int64(4), false, mp) |
| 92 | + bat.Vecs[1] = vec2 |
| 93 | + |
| 94 | + bat.SetRowCount(2) |
| 95 | + |
| 96 | + // Verify initial state: should have 2 vectors |
| 97 | + require.Equal(t, 2, len(bat.Vecs)) |
| 98 | + |
| 99 | + // Call updateCNDataBatch |
| 100 | + newCommitTS := types.BuildTS(100, 0) |
| 101 | + updateCNDataBatch(bat, newCommitTS, mp) |
| 102 | + |
| 103 | + // Verify commitTS vector is added at the end |
| 104 | + require.Equal(t, 3, len(bat.Vecs)) |
| 105 | + require.Equal(t, types.T_int64, bat.Vecs[0].GetType().Oid) |
| 106 | + require.Equal(t, types.T_int64, bat.Vecs[1].GetType().Oid) |
| 107 | + require.Equal(t, types.T_TS, bat.Vecs[2].GetType().Oid) |
| 108 | + |
| 109 | + bat.Clean(mp) |
| 110 | +} |
0 commit comments