github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/identifier.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 tree
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  )
    22  
    23  // IdentifierName is referenced in the expression
    24  type IdentifierName interface {
    25  	Expr
    26  }
    27  
    28  // sql indentifier
    29  type Identifier string
    30  
    31  func (node *Identifier) Format(ctx *FmtCtx) {
    32  	ctx.WriteString(string(*node))
    33  }
    34  
    35  type UnrestrictedIdentifier string
    36  
    37  // the list of identifiers.
    38  type IdentifierList []Identifier
    39  
    40  func (node *IdentifierList) Format(ctx *FmtCtx) {
    41  	for i := range *node {
    42  		if i > 0 {
    43  			ctx.WriteString(", ")
    44  		}
    45  		ctx.WriteString(string((*node)[i]))
    46  	}
    47  }
    48  
    49  type ColumnItem struct {
    50  	IdentifierName
    51  
    52  	//the name of the column
    53  	ColumnName Identifier
    54  }
    55  
    56  // the unresolved qualified name like column name.
    57  type UnresolvedName struct {
    58  	exprImpl
    59  	//the number of name parts specified, including the star. Always 1 or greater.
    60  	NumParts int
    61  
    62  	//the name ends with a star. then the first element is empty in the Parts
    63  	Star bool
    64  
    65  	// Parts are the name components (at most 4: column, table, db/schema, catalog.), in reverse order.
    66  	Parts NameParts
    67  }
    68  
    69  func (node *UnresolvedName) Format(ctx *FmtCtx) {
    70  	for i := node.NumParts - 1; i >= 0; i-- {
    71  		ctx.WriteString(node.Parts[i])
    72  		if i > 0 {
    73  			ctx.WriteByte('.')
    74  		}
    75  	}
    76  	if node.Star && node.NumParts > 0 {
    77  		ctx.WriteString(".*")
    78  	} else if node.Star {
    79  		ctx.WriteByte('*')
    80  	}
    81  }
    82  
    83  // GetNames dbName, tableName, colName
    84  func (node *UnresolvedName) GetNames() (string, string, string) {
    85  	return node.Parts[2], node.Parts[1], node.Parts[0]
    86  }
    87  
    88  // the path in an UnresolvedName.
    89  type NameParts = [4]string
    90  
    91  func NewUnresolvedName(ctx context.Context, parts ...string) (*UnresolvedName, error) {
    92  	l := len(parts)
    93  	if l < 1 || l > 4 {
    94  		return nil, moerr.NewInternalError(ctx, "the count of name parts among [1,4]")
    95  	}
    96  	u := &UnresolvedName{
    97  		NumParts: len(parts),
    98  		Star:     false,
    99  	}
   100  	for i := 0; i < len(parts); i++ {
   101  		u.Parts[i] = parts[l-1-i]
   102  	}
   103  	return u, nil
   104  }
   105  
   106  func SetUnresolvedName(parts ...string) *UnresolvedName {
   107  	l := len(parts)
   108  	u := &UnresolvedName{
   109  		NumParts: len(parts),
   110  		Star:     false,
   111  	}
   112  	for i := 0; i < len(parts); i++ {
   113  		u.Parts[i] = parts[l-1-i]
   114  	}
   115  	return u
   116  }
   117  
   118  func NewUnresolvedNameWithStar(ctx context.Context, parts ...string) (*UnresolvedName, error) {
   119  	l := len(parts)
   120  	if l < 1 || l > 3 {
   121  		return nil, moerr.NewInternalError(ctx, "the count of name parts among [1,3]")
   122  	}
   123  	u := &UnresolvedName{
   124  		NumParts: len(parts),
   125  		Star:     true,
   126  	}
   127  	u.Parts[0] = ""
   128  	for i := 0; i < len(parts); i++ {
   129  		u.Parts[i] = parts[l-1-i]
   130  	}
   131  	return u, nil
   132  }
   133  
   134  func SetUnresolvedNameWithStar(parts ...string) *UnresolvedName {
   135  	l := len(parts)
   136  	u := &UnresolvedName{
   137  		NumParts: len(parts),
   138  		Star:     true,
   139  	}
   140  	for i := 0; i < len(parts); i++ {
   141  		u.Parts[i] = parts[l-1-i]
   142  	}
   143  	return u
   144  }
   145  
   146  // variable in the scalar expression
   147  type VarName interface {
   148  	Expr
   149  }
   150  
   151  var _ VarName = &UnresolvedName{}
   152  var _ VarName = UnqualifiedStar{}
   153  
   154  // '*' in the scalar expression
   155  type UnqualifiedStar struct {
   156  	VarName
   157  }
   158  
   159  func (node UnqualifiedStar) Format(ctx *FmtCtx) {
   160  	ctx.WriteByte('*')
   161  }
   162  
   163  var starName VarName = UnqualifiedStar{}
   164  
   165  func StarExpr() VarName {
   166  	return starName
   167  }