github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/util/util.go (about)

     1  // Copyright 2021 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 util
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/catalog"
    19  	"github.com/matrixorigin/matrixone/pkg/defines"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    21  	"go/constant"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  func SplitTableAndColumn(name string) (string, string) {
    27  	var schema string
    28  
    29  	{ // determine if it is a function call
    30  		i, j := strings.Index(name, "("), strings.Index(name, ")")
    31  		if i >= 0 && j >= 0 && j > i {
    32  			return "", name
    33  		}
    34  	}
    35  	xs := strings.Split(name, ".")
    36  	for i := 0; i < len(xs)-1; i++ {
    37  		if i > 0 {
    38  			schema += "."
    39  		}
    40  		schema += xs[i]
    41  	}
    42  	return schema, xs[len(xs)-1]
    43  }
    44  
    45  // TableIsClusterTable check the table type is cluster table
    46  func TableIsClusterTable(tableType string) bool {
    47  	return tableType == catalog.SystemClusterRel
    48  }
    49  
    50  // Build the filter condition AST expression for mo_database, as follows:
    51  // account_id = cur_accountId or (account_id = 0 and datname in ('mo_catalog'))
    52  func BuildMoDataBaseFilter(curAccountId uint64) tree.Expr {
    53  	// left is: account_id = cur_accountId
    54  	left := makeAccountIdEqualAst(curAccountId)
    55  
    56  	datnameColName := &tree.UnresolvedName{
    57  		NumParts: 1,
    58  		Parts:    tree.NameParts{catalog.SystemDBAttr_Name},
    59  	}
    60  
    61  	mo_catalogConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_CATALOG), catalog.MO_CATALOG, false, tree.P_char)
    62  	inValues := tree.NewTuple(tree.Exprs{mo_catalogConst})
    63  	// datname in ('mo_catalog')
    64  	inExpr := tree.NewComparisonExpr(tree.IN, datnameColName, inValues)
    65  
    66  	// account_id = 0
    67  	accountIdEqulZero := makeAccountIdEqualAst(0)
    68  	// andExpr is:account_id = 0 and datname in ('mo_catalog')
    69  	andExpr := tree.NewAndExpr(accountIdEqulZero, inExpr)
    70  
    71  	// right is:(account_id = 0 and datname in ('mo_catalog'))
    72  	right := tree.NewParenExpr(andExpr)
    73  	// return is: account_id = cur_accountId or (account_id = 0 and datname in ('mo_catalog'))
    74  	return tree.NewOrExpr(left, right)
    75  }
    76  
    77  // Build the filter condition AST expression for mo_tables, as follows:
    78  // account_id = cur_account_id or (account_id = 0 and (relname in ('mo_tables','mo_database','mo_columns') or relkind = 'cluster'))
    79  func BuildMoTablesFilter(curAccountId uint64) tree.Expr {
    80  	// left is: account_id = cur_accountId
    81  	left := makeAccountIdEqualAst(curAccountId)
    82  
    83  	relnameColName := &tree.UnresolvedName{
    84  		NumParts: 1,
    85  		Parts:    tree.NameParts{catalog.SystemRelAttr_Name},
    86  	}
    87  
    88  	mo_databaseConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_DATABASE), catalog.MO_DATABASE, false, tree.P_char)
    89  	mo_tablesConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_TABLES), catalog.MO_TABLES, false, tree.P_char)
    90  	mo_columnsConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_COLUMNS), catalog.MO_COLUMNS, false, tree.P_char)
    91  	inValues := tree.NewTuple(tree.Exprs{mo_databaseConst, mo_tablesConst, mo_columnsConst})
    92  
    93  	// relname in ('mo_tables','mo_database','mo_columns')
    94  	inExpr := tree.NewComparisonExpr(tree.IN, relnameColName, inValues)
    95  	//relkindEqualAst := makeRelkindEqualAst("cluster")
    96  
    97  	relkindEqualAst := makeStringEqualAst(catalog.SystemRelAttr_Kind, "cluster")
    98  
    99  	// (relname in ('mo_tables','mo_database','mo_columns') or relkind = 'cluster')
   100  	tempExpr := tree.NewOrExpr(inExpr, relkindEqualAst)
   101  	tempExpr2 := tree.NewParenExpr(tempExpr)
   102  	// account_id = 0
   103  	accountIdEqulZero := makeAccountIdEqualAst(0)
   104  	// andExpr is: account_id = 0 and (relname in ('mo_tables','mo_database','mo_columns') or relkind = 'cluster')
   105  	andExpr := tree.NewAndExpr(accountIdEqulZero, tempExpr2)
   106  
   107  	// right is: (account_id = 0 and (relname in ('mo_tables','mo_database','mo_columns') or relkind = 'cluster'))
   108  	right := tree.NewParenExpr(andExpr)
   109  
   110  	// return is: account_id = cur_account_id or (account_id = 0 and (relname in ('mo_tables','mo_database','mo_columns') or relkind = 'cluster'));
   111  	return tree.NewOrExpr(left, right)
   112  }
   113  
   114  // Build the filter condition AST expression for mo_columns, as follows:
   115  // account_id = current_id or (account_id = 0 and (att_relname in ('mo_database','mo_tables','mo_columns')))
   116  func BuildMoColumnsFilter(curAccountId uint64) tree.Expr {
   117  	// left is: account_id = cur_accountId
   118  	left := makeAccountIdEqualAst(curAccountId)
   119  
   120  	att_relnameColName := &tree.UnresolvedName{
   121  		NumParts: 1,
   122  		Parts:    tree.NameParts{catalog.SystemColAttr_RelName},
   123  	}
   124  
   125  	mo_databaseConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_DATABASE), catalog.MO_DATABASE, false, tree.P_char)
   126  	mo_tablesConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_TABLES), catalog.MO_TABLES, false, tree.P_char)
   127  	mo_columnsConst := tree.NewNumValWithType(constant.MakeString(catalog.MO_COLUMNS), catalog.MO_COLUMNS, false, tree.P_char)
   128  
   129  	inValues := tree.NewTuple(tree.Exprs{mo_databaseConst, mo_tablesConst, mo_columnsConst})
   130  	// att_relname in ('mo_database','mo_tables','mo_columns')
   131  	inExpr := tree.NewComparisonExpr(tree.IN, att_relnameColName, inValues)
   132  
   133  	// account_id = 0
   134  	accountIdEqulZero := makeAccountIdEqualAst(0)
   135  	// andExpr is: account_id = 0 and (att_relname in ('mo_database','mo_tables','mo_columns'))
   136  	andExpr := tree.NewAndExpr(accountIdEqulZero, inExpr)
   137  
   138  	// right is: (account_id = 0 and (att_relname in ('mo_database','mo_tables','mo_columns')))
   139  	right := tree.NewParenExpr(andExpr)
   140  	// return is: account_id = current_id or (account_id = 0 and (att_relname in ('mo_database','mo_tables','mo_columns')))
   141  	return tree.NewOrExpr(left, right)
   142  }
   143  
   144  // build equal ast expr: colName = 'xxx'
   145  func makeStringEqualAst(lColName, rValue string) tree.Expr {
   146  	relkindColName := &tree.UnresolvedName{
   147  		NumParts: 1,
   148  		Parts:    tree.NameParts{lColName},
   149  	}
   150  	clusterConst := tree.NewNumValWithType(constant.MakeString(rValue), rValue, false, tree.P_char)
   151  	return tree.NewComparisonExpr(tree.EQUAL, relkindColName, clusterConst)
   152  }
   153  
   154  // build ast expr: account_id = cur_accountId
   155  func makeAccountIdEqualAst(curAccountId uint64) tree.Expr {
   156  	accountIdColName := &tree.UnresolvedName{
   157  		NumParts: 1,
   158  		Parts:    tree.NameParts{"account_id"},
   159  	}
   160  	curAccountIdConst := tree.NewNumValWithType(constant.MakeUint64(uint64(curAccountId)), strconv.Itoa(int(curAccountId)), false, tree.P_int64)
   161  	return tree.NewComparisonExpr(tree.EQUAL, accountIdColName, curAccountIdConst)
   162  }
   163  
   164  var (
   165  	clusterTableAttributeName = "account_id"
   166  	clusterTableAttributeType = &tree.T{InternalType: tree.InternalType{
   167  		Family:   tree.IntFamily,
   168  		Width:    32,
   169  		Oid:      uint32(defines.MYSQL_TYPE_LONG),
   170  		Unsigned: true,
   171  	}}
   172  )
   173  
   174  func GetClusterTableAttributeName() string {
   175  	return clusterTableAttributeName
   176  }
   177  
   178  func GetClusterTableAttributeType() *tree.T {
   179  	return clusterTableAttributeType
   180  }
   181  
   182  func IsClusterTableAttribute(name string) bool {
   183  	return name == clusterTableAttributeName
   184  }