Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: use NonEmpty() instead of !IsEmpty() #14499

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (ts *tableSettings) Set(v string) error {
err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case sqlparser.TableName:
if !node.Name.IsEmpty() {
if node.Name.NonEmpty() {
if seenSourceTables[node.Name.String()] {
return false, fmt.Errorf("multiple source_expression queries use the same table: %q", node.Name.String())
}
Expand Down
24 changes: 12 additions & 12 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ func (idx *IndexDefinition) Format(buf *TrackedBuffer) {

// Format formats the node.
func (ii *IndexInfo) Format(buf *TrackedBuffer) {
if !ii.ConstraintName.IsEmpty() {
if ii.ConstraintName.NonEmpty() {
buf.astPrintf(ii, "constraint %v ", ii.ConstraintName)
}
switch ii.Type {
Expand All @@ -847,7 +847,7 @@ func (ii *IndexInfo) Format(buf *TrackedBuffer) {
case IndexTypeFullText:
buf.astPrintf(ii, "%s %s", keywordStrings[FULLTEXT], keywordStrings[KEY])
}
if !ii.Name.IsEmpty() {
if ii.Name.NonEmpty() {
buf.astPrintf(ii, " %v", ii.Name)
}
}
Expand Down Expand Up @@ -883,7 +883,7 @@ func (node VindexParam) Format(buf *TrackedBuffer) {

// Format formats the node.
func (c *ConstraintDefinition) Format(buf *TrackedBuffer) {
if !c.Name.IsEmpty() {
if c.Name.NonEmpty() {
buf.astPrintf(c, "constraint %v ", c.Name)
}
c.Details.Format(buf)
Expand Down Expand Up @@ -1114,7 +1114,7 @@ func (node *StarExpr) Format(buf *TrackedBuffer) {
// Format formats the node.
func (node *AliasedExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%v", node.Expr)
if !node.As.IsEmpty() {
if node.As.NonEmpty() {
buf.astPrintf(node, " as %v", node.As)
}
}
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func (node TableExprs) Format(buf *TrackedBuffer) {
// Format formats the node.
func (node *AliasedTableExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%v%v", node.Expr, node.Partitions)
if !node.As.IsEmpty() {
if node.As.NonEmpty() {
buf.astPrintf(node, " as %v", node.As)
if len(node.Columns) != 0 {
buf.astPrintf(node, "%v", node.Columns)
Expand All @@ -1189,7 +1189,7 @@ func (node TableName) Format(buf *TrackedBuffer) {
if node.IsEmpty() {
return
}
if !node.Qualifier.IsEmpty() {
if node.Qualifier.NonEmpty() {
buf.astPrintf(node, "%v.", node.Qualifier)
}
buf.astPrintf(node, "%v", node.Name)
Expand Down Expand Up @@ -1544,7 +1544,7 @@ func (node *CollateExpr) Format(buf *TrackedBuffer) {

// Format formats the node.
func (node *FuncExpr) Format(buf *TrackedBuffer) {
if !node.Qualifier.IsEmpty() {
if node.Qualifier.NonEmpty() {
buf.astPrintf(node, "%v.", node.Qualifier)
}
// Function names should not be back-quoted even
Expand Down Expand Up @@ -1598,7 +1598,7 @@ func (node *JSONStorageSizeExpr) Format(buf *TrackedBuffer) {
// Format formats the node
func (node *OverClause) Format(buf *TrackedBuffer) {
buf.WriteString("over")
if !node.WindowName.IsEmpty() {
if node.WindowName.NonEmpty() {
buf.astPrintf(node, " %v", node.WindowName)
}
if node.WindowSpec != nil {
Expand All @@ -1608,7 +1608,7 @@ func (node *OverClause) Format(buf *TrackedBuffer) {

// Format formats the node
func (node *WindowSpecification) Format(buf *TrackedBuffer) {
if !node.Name.IsEmpty() {
if node.Name.NonEmpty() {
buf.astPrintf(node, " %v", node.Name)
}
if node.PartitionClause != nil {
Expand Down Expand Up @@ -2020,7 +2020,7 @@ func (node *ShowBasic) Format(buf *TrackedBuffer) {
if !node.Tbl.IsEmpty() {
buf.astPrintf(node, " from %v", node.Tbl)
}
if !node.DbName.IsEmpty() {
if node.DbName.NonEmpty() {
buf.astPrintf(node, " from %v", node.DbName)
}
buf.astPrintf(node, "%v", node.Filter)
Expand Down Expand Up @@ -2070,7 +2070,7 @@ func (node *CreateDatabase) Format(buf *TrackedBuffer) {
// Format formats the node.
func (node *AlterDatabase) Format(buf *TrackedBuffer) {
buf.literal("alter database")
if !node.DBName.IsEmpty() {
if node.DBName.NonEmpty() {
buf.astPrintf(node, " %v", node.DBName)
}
if node.UpdateDataDirectory {
Expand Down Expand Up @@ -2354,7 +2354,7 @@ func (node *DropColumn) Format(buf *TrackedBuffer) {
// Format formats the node
func (node *DropKey) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "drop %s", node.Type.ToString())
if !node.Name.IsEmpty() {
if node.Name.NonEmpty() {
buf.astPrintf(node, " %v", node.Name)
}
}
Expand Down
24 changes: 12 additions & 12 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (node *AliasedTableExpr) RemoveHints() *AliasedTableExpr {

// TableName returns a TableName pointing to this table expr
func (node *AliasedTableExpr) TableName() (TableName, error) {
if !node.As.IsEmpty() {
if node.As.NonEmpty() {
return TableName{Name: node.As}, nil
}

Expand Down Expand Up @@ -868,6 +868,11 @@ func (node IdentifierCI) IsEmpty() bool {
return node.val == ""
}

// NonEmpty returns true if the name is not empty.
func (node IdentifierCI) NonEmpty() bool {
return !node.IsEmpty()
}

// String returns the unescaped column name. It must
// not be used for SQL generation. Use sqlparser.String
// instead. The Stringer conformance is for usage
Expand Down Expand Up @@ -935,6 +940,11 @@ func (node IdentifierCS) IsEmpty() bool {
return node.v == ""
}

// NonEmpty returns true if TabIdent is not empty.
func (node IdentifierCS) NonEmpty() bool {
return !node.IsEmpty()
}

// String returns the unescaped table name. It must
// not be used for SQL generation. Use sqlparser.String
// instead. The Stringer conformance is for usage
Expand Down Expand Up @@ -2099,7 +2109,7 @@ func GetAllSelects(selStmt SelectStatement) []*Select {

// ColumnName returns the alias if one was provided, otherwise prints the AST
func (ae *AliasedExpr) ColumnName() string {
if !ae.As.IsEmpty() {
if ae.As.NonEmpty() {
return ae.As.String()
}

Expand Down Expand Up @@ -2131,7 +2141,7 @@ func RemoveKeyspace(in SQLNode) {
_ = Walk(func(node SQLNode) (kontinue bool, err error) {
switch col := node.(type) {
case *ColName:
if !col.Qualifier.Qualifier.IsEmpty() {
if col.Qualifier.Qualifier.NonEmpty() {
col.Qualifier.Qualifier = NewIdentifierCS("")
}
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_rewriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (er *astRewriter) visitSelect(node *Select) {
}

aliasedExpr, ok := col.(*AliasedExpr)
if !ok || !aliasedExpr.As.IsEmpty() {
if !ok || aliasedExpr.As.NonEmpty() {
continue
}
buf := NewTrackedBuffer(nil)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ func ReplaceTableQualifiers(query, olddb, newdb string) (string, error) {
upd := Rewrite(in, func(cursor *Cursor) bool {
switch node := cursor.Node().(type) {
case TableName:
if !node.Qualifier.IsEmpty() &&
if node.Qualifier.NonEmpty() &&
node.Qualifier.String() == oldQualifier.String() {
node.Qualifier = newQualifier
cursor.Replace(node)
modified = true
}
case *ShowBasic: // for things like 'show tables from _vt'
if !node.DbName.IsEmpty() &&
if node.DbName.NonEmpty() &&
node.DbName.String() == oldQualifier.String() {
node.DbName = newQualifier
cursor.Replace(node)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/topotools/vschema_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, alterVschema *sqlpar
// already exists.
spec := alterVschema.VindexSpec
name := spec.Name.String()
if !spec.Type.IsEmpty() {
if spec.Type.NonEmpty() {
owner, params := spec.ParseParams()
if vindex, ok := ks.Vindexes[name]; ok {
if vindex.Type != spec.Type.String() {
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func buildAnalyzePlan(stmt sqlparser.Statement, _ *sqlparser.ReservedVars, vsche
var err error
dest := key.Destination(key.DestinationAllShards{})

if !analyzeStmt.Table.Qualifier.IsEmpty() && sqlparser.SystemSchema(analyzeStmt.Table.Qualifier.String()) {
if analyzeStmt.Table.Qualifier.NonEmpty() && sqlparser.SystemSchema(analyzeStmt.Table.Qualifier.String()) {
ks, err = vschema.AnyKeyspace()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/call_proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func buildCallProcPlan(stmt *sqlparser.CallProc, vschema plancontext.VSchema) (*planResult, error) {
var ks string
if !stmt.Name.Qualifier.IsEmpty() {
if stmt.Name.Qualifier.NonEmpty() {
ks = stmt.Name.Qualifier.String()
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func rewriteSingleTbl(del *sqlparser.Delete) (*sqlparser.Delete, error) {
if !ok {
return del, nil
}
if !atExpr.As.IsEmpty() && !sqlparser.Equals.IdentifierCS(del.Targets[0].Name, atExpr.As) {
if atExpr.As.NonEmpty() && !sqlparser.Equals.IdentifierCS(del.Targets[0].Name, atExpr.As) {
// Unknown table in MULTI DELETE
return nil, vterrors.VT03003(del.Targets[0].Name.String())
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (ap AliasedProjections) AddColumn(col *sqlparser.AliasedExpr) (ProjCols, in

func (pe *ProjExpr) String() string {
var alias, expr, info string
if !pe.Original.As.IsEmpty() {
if pe.Original.As.NonEmpty() {
alias = " AS " + pe.Original.As.String()
}
if sqlparser.Equals.Expr(pe.EvalExpr, pe.ColExpr) {
Expand Down Expand Up @@ -399,7 +399,7 @@ func (p *Projection) GetSelectExprs(*plancontext.PlanningContext) sqlparser.Sele
var output sqlparser.SelectExprs
for _, pe := range cols {
ae := &sqlparser.AliasedExpr{Expr: pe.EvalExpr}
if !pe.Original.As.IsEmpty() {
if pe.Original.As.NonEmpty() {
ae.As = pe.Original.As
} else if !sqlparser.Equals.Expr(ae.Expr, pe.Original.Expr) {
ae.As = sqlparser.NewIdentifierCI(pe.Original.ColumnName())
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/queryprojection.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (qp *QueryProjection) GetSimplifiedExpr(ctx *plancontext.PlanningContext, e
if !ok {
continue
}
aliased := !ae.As.IsEmpty()
aliased := ae.As.NonEmpty()
if aliased {
if in.Name.Equal(ae.As) {
err = check(ae.Expr)
Expand Down Expand Up @@ -818,7 +818,7 @@ func (qp *QueryProjection) FindSelectExprIndexForExpr(ctx *plancontext.PlanningC
continue
}
if isCol {
isAliasExpr := !aliasedExpr.As.IsEmpty()
isAliasExpr := aliasedExpr.As.NonEmpty()
if isAliasExpr && colExpr.Name.Equal(aliasedExpr.As) {
return &idx, aliasedExpr
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.
func (to *Table) ShortDescription() string {
tbl := to.VTable.String()
var alias, where string
if !to.QTable.Alias.As.IsEmpty() {
if to.QTable.Alias.As.NonEmpty() {
alias = " AS " + to.QTable.Alias.As.String()
}

Expand Down
Loading
Loading