github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/explain/tpch_explain_test.go (about)

     1  // Copyright 2021 - 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 explain
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    26  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/sql/parsers"
    29  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    30  )
    31  
    32  func Test_TPCH_Plan2(t *testing.T) {
    33  	ctx := context.TODO()
    34  
    35  	mock := plan.NewMockOptimizer(false)
    36  	es := &ExplainOptions{
    37  		Verbose: true,
    38  		Analyze: false,
    39  		Format:  EXPLAIN_FORMAT_TEXT,
    40  	}
    41  
    42  	_, fn, _, _ := runtime.Caller(0)
    43  	dir := filepath.Dir(filepath.Dir(fn))
    44  
    45  	ddlf, err := os.ReadFile(dir + "/tpch/ddl.sql")
    46  	if err != nil {
    47  		t.Errorf("Cannot open ddl file, error %v", err)
    48  	}
    49  
    50  	ddls, err := parsers.Parse(ctx, dialect.MYSQL, string(ddlf), 1, 0)
    51  	if ddls == nil || err != nil {
    52  		t.Errorf("DDL Parser failed, error %v", err)
    53  	}
    54  
    55  	/*
    56  		BROKEN: Will crash.
    57  		for _, ast := range ddls {
    58  			_, err := mock.Optimize(ast)
    59  			if err == nil {
    60  				t.Logf("Optimizer failed, NYI")
    61  			}
    62  		}
    63  	*/
    64  
    65  	// test simple sql
    66  	qf, err := os.ReadFile(dir + "/tpch/simple.sql")
    67  	t.Logf("# tpch file: /tpch/simple.sql")
    68  	if err != nil {
    69  		t.Errorf("Cannot open queries file, error %v", err)
    70  	}
    71  	qs, err := parsers.Parse(ctx, dialect.MYSQL, string(qf), 1, 0)
    72  	if qs == nil || err != nil {
    73  		t.Errorf("Query Parser failed, error %v", err)
    74  	}
    75  	for _, ast := range qs {
    76  		out := tree.String(ast, dialect.MYSQL)
    77  		t.Logf("SQL: %v\n", out)
    78  		query, err := mock.Optimize(ast)
    79  		if err != nil {
    80  			t.Errorf("Optimizer failed, NYI")
    81  		}
    82  		buffer := NewExplainDataBuffer()
    83  		explainQuery := NewExplainQueryImpl(query)
    84  		err = explainQuery.ExplainPlan(ctx, buffer, es)
    85  		if err != nil {
    86  			t.Errorf("explain failed, WXL")
    87  		}
    88  	}
    89  
    90  	// test tpch query
    91  	for qn := 1; qn <= 22; qn += 1 {
    92  		// qn := 15
    93  		qnf, err := os.ReadFile(fmt.Sprintf("%s/tpch/q%d.sql", dir, qn))
    94  		if err != nil {
    95  			t.Errorf("Cannot open file of query %d, error %v", qn, err)
    96  		}
    97  		t.Logf("--<%d> tpch file: %s/tpch/q%d.sql\n", qn, dir, qn)
    98  		t.Logf("SQL : %v \n", string(qnf))
    99  
   100  		qns, err := parsers.Parse(ctx, dialect.MYSQL, string(qnf), 1, 0)
   101  		if qns == nil || err != nil {
   102  			t.Errorf("Query %d Parser failed, error %v", qn, err)
   103  		}
   104  		for _, ast := range qns {
   105  			query, err := mock.Optimize(ast)
   106  			if err != nil {
   107  				t.Errorf("Optimizer %d failed, error %v", qn, err)
   108  			}
   109  			buffer := NewExplainDataBuffer()
   110  			explainQuery := NewExplainQueryImpl(query)
   111  			err = explainQuery.ExplainPlan(ctx, buffer, es)
   112  			if err != nil {
   113  				t.Errorf("explain failed, WXL %v", err)
   114  			}
   115  		}
   116  	}
   117  }