|
| 1 | +// Copyright 2026 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 plan |
| 16 | + |
| 17 | +import ( |
| 18 | + "math" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/matrixorigin/matrixone/pkg/container/types" |
| 22 | + planpb "github.com/matrixorigin/matrixone/pkg/pb/plan" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestDisableMemoryUnsafeRightDedupUsesCombinedMapSize(t *testing.T) { |
| 27 | + const combinedMapBytes = 64*1024 + 128*1024 |
| 28 | + |
| 29 | + t.Run("combined maps fit", func(t *testing.T) { |
| 30 | + builder, joins := makeChainedRightDedupBuilder(combinedMapBytes) |
| 31 | + |
| 32 | + builder.disableMemoryUnsafeRightDedup(4) |
| 33 | + |
| 34 | + require.True(t, joins[0].IsRightJoin) |
| 35 | + require.True(t, joins[1].IsRightJoin) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("combined maps exceed budget", func(t *testing.T) { |
| 39 | + builder, joins := makeChainedRightDedupBuilder(combinedMapBytes - 1) |
| 40 | + |
| 41 | + builder.disableMemoryUnsafeRightDedup(4) |
| 42 | + |
| 43 | + require.False(t, joins[0].IsRightJoin) |
| 44 | + require.False(t, joins[1].IsRightJoin) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestDisableMemoryUnsafeRightDedupHonorsRowThreshold(t *testing.T) { |
| 49 | + t.Run("combined keys fit", func(t *testing.T) { |
| 50 | + builder, joins := makeChainedRightDedupBuilder(2201) |
| 51 | + |
| 52 | + builder.disableMemoryUnsafeRightDedup(4) |
| 53 | + |
| 54 | + require.True(t, joins[0].IsRightJoin) |
| 55 | + require.True(t, joins[1].IsRightJoin) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("combined keys reach threshold", func(t *testing.T) { |
| 59 | + builder, joins := makeChainedRightDedupBuilder(2200) |
| 60 | + |
| 61 | + builder.disableMemoryUnsafeRightDedup(4) |
| 62 | + |
| 63 | + require.False(t, joins[0].IsRightJoin) |
| 64 | + require.False(t, joins[1].IsRightJoin) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestDisableMemoryUnsafeRightDedupRejectsUnknownCardinality(t *testing.T) { |
| 69 | + tests := []struct { |
| 70 | + name string |
| 71 | + stats *planpb.Stats |
| 72 | + }{ |
| 73 | + {name: "non-finite", stats: &planpb.Stats{Outcnt: math.NaN()}}, |
| 74 | + {name: "default sentinel", stats: DefaultStats()}, |
| 75 | + } |
| 76 | + for _, test := range tests { |
| 77 | + t.Run(test.name, func(t *testing.T) { |
| 78 | + builder, joins := makeChainedRightDedupBuilder(1 << 30) |
| 79 | + builder.qry.Nodes[0].Stats = test.stats |
| 80 | + |
| 81 | + builder.disableMemoryUnsafeRightDedup(4) |
| 82 | + |
| 83 | + require.False(t, joins[0].IsRightJoin) |
| 84 | + require.False(t, joins[1].IsRightJoin) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func makeChainedRightDedupBuilder(joinSpillMem int64) (*QueryBuilder, []*planpb.Node) { |
| 90 | + source := &planpb.Node{NodeType: planpb.Node_VALUE_SCAN, Stats: &planpb.Stats{Outcnt: 1000}} |
| 91 | + targetPK := &planpb.Node{NodeType: planpb.Node_TABLE_SCAN, Stats: &planpb.Stats{Outcnt: 100}} |
| 92 | + pkDedup := &planpb.Node{ |
| 93 | + NodeType: planpb.Node_JOIN, |
| 94 | + JoinType: planpb.Node_DEDUP, |
| 95 | + Children: []int32{1, 0}, |
| 96 | + OnList: []*planpb.Expr{makeRightDedupEquality(types.T_int64)}, |
| 97 | + OnDuplicateAction: planpb.Node_FAIL, |
| 98 | + IsRightJoin: true, |
| 99 | + Stats: &planpb.Stats{Outcnt: 100}, // stale pre-swap RIGHT DEDUP estimate |
| 100 | + } |
| 101 | + targetUnique := &planpb.Node{NodeType: planpb.Node_TABLE_SCAN, Stats: &planpb.Stats{Outcnt: 100}} |
| 102 | + uniqueDedup := &planpb.Node{ |
| 103 | + NodeType: planpb.Node_JOIN, |
| 104 | + JoinType: planpb.Node_DEDUP, |
| 105 | + Children: []int32{3, 2}, |
| 106 | + OnList: []*planpb.Expr{makeRightDedupEquality(types.T_varchar)}, |
| 107 | + OnDuplicateAction: planpb.Node_FAIL, |
| 108 | + IsRightJoin: true, |
| 109 | + Stats: &planpb.Stats{Outcnt: 100}, |
| 110 | + } |
| 111 | + return &QueryBuilder{ |
| 112 | + qry: &planpb.Query{Nodes: []*planpb.Node{source, targetPK, pkDedup, targetUnique, uniqueDedup}}, |
| 113 | + joinSpillMem: joinSpillMem, |
| 114 | + }, []*planpb.Node{pkDedup, uniqueDedup} |
| 115 | +} |
| 116 | + |
| 117 | +func makeRightDedupEquality(typ types.T) *planpb.Expr { |
| 118 | + planType := planpb.Type{Id: int32(typ)} |
| 119 | + return &planpb.Expr{ |
| 120 | + Typ: planpb.Type{Id: int32(types.T_bool)}, |
| 121 | + Expr: &planpb.Expr_F{F: &planpb.Function{Args: []*planpb.Expr{ |
| 122 | + {Typ: planType, Expr: &planpb.Expr_Col{Col: &planpb.ColRef{RelPos: 0, ColPos: 0}}}, |
| 123 | + {Typ: planType, Expr: &planpb.Expr_Col{Col: &planpb.ColRef{RelPos: 1, ColPos: 0}}}, |
| 124 | + }}}, |
| 125 | + } |
| 126 | +} |
0 commit comments