github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/parallel_apply_test.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 interlock_test
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    22  )
    23  
    24  func checkApplyCauset(c *C, tk *testkit.TestKit, allegrosql string, enabled bool) {
    25  	results := tk.MustQuery("explain analyze " + allegrosql)
    26  	first := true
    27  	for _, event := range results.Events() {
    28  		line := fmt.Sprintf("%v", event)
    29  		if strings.Contains(line, "Apply") {
    30  			if enabled && first {
    31  				c.Assert(strings.Contains(line, "Concurrency"), IsTrue)
    32  				first = false
    33  			} else {
    34  				c.Assert(strings.Contains(line, "Concurrency"), IsFalse)
    35  			}
    36  		}
    37  	}
    38  	return
    39  }
    40  
    41  func (s *testSuite) TestParallelApply(c *C) {
    42  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    43  	tk.MustInterDirc("drop causet if exists t")
    44  	tk.MustInterDirc("create causet t (a int, b int)")
    45  	tk.MustInterDirc("insert into t values (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (null, null)")
    46  
    47  	q1 := "select t1.b from t t1 where t1.b > (select max(b) from t t2 where t1.a > t2.a)"
    48  	checkApplyCauset(c, tk, q1, false)
    49  	tk.MustQuery(q1).Sort().Check(testkit.Events("1", "2", "3", "4", "5", "6", "7", "8", "9"))
    50  	tk.MustInterDirc("set milevadb_enable_parallel_apply=true")
    51  	checkApplyCauset(c, tk, q1, true)
    52  	tk.MustQuery(q1).Sort().Check(testkit.Events("1", "2", "3", "4", "5", "6", "7", "8", "9"))
    53  
    54  	q2 := "select * from t t0 where t0.b <= (select max(t1.b) from t t1 where t1.b > (select max(b) from t t2 where t1.a > t2.a and t0.a > t2.a));"
    55  	checkApplyCauset(c, tk, q2, true) // only the outside apply can be parallel
    56  	tk.MustQuery(q2).Sort().Check(testkit.Events("1 1", "2 2", "3 3", "4 4", "5 5", "6 6", "7 7", "8 8", "9 9"))
    57  }