github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/recursive_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 plan
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    23  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestSplitRecursiveMember(t *testing.T) {
    28  	sql := "with recursive c as (select a from t1 union all select a+1 from c where a < 3 union all select a+1 from c where a < 4) select * from c"
    29  	stmts, err := parsers.Parse(context.TODO(), dialect.MYSQL, sql, 1, 0)
    30  	if err != nil {
    31  		t.Errorf("Parse(%q) err: %v", sql, err)
    32  		return
    33  	}
    34  	c := stmts[0].(*tree.Select).With.CTEs[0]
    35  	name := string(c.Name.Alias)
    36  	stmt := c.Stmt.(*tree.Select).Select
    37  	b := &QueryBuilder{}
    38  
    39  	var ss []tree.SelectStatement
    40  	left, err := b.splitRecursiveMember(&stmt, name, &ss)
    41  	if err != nil {
    42  		t.Errorf("splitRecursiveMember err: %v", err)
    43  		return
    44  	}
    45  
    46  	require.Equal(t, 2, len(ss))
    47  	require.Equal(t, true, left != nil)
    48  }