Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed Feb 3, 2025
1 parent d0410bf commit 8538140
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 24 deletions.
8 changes: 6 additions & 2 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type (

ColumnResults interface {
GetColumnCount() int
GetColumns() SelectExprs
GetColumns() *SelectExprs2
}

Withable interface {
Expand Down Expand Up @@ -295,7 +295,7 @@ type (
With *With
From []TableExpr
Comments *ParsedComments
SelectExprs SelectExprs
SelectExprs *SelectExprs2
Where *Where
GroupBy *GroupBy
Having *Where
Expand Down Expand Up @@ -2042,6 +2042,10 @@ type ParsedComments struct {
// SelectExprs represents SELECT expressions.
type SelectExprs []SelectExpr

type SelectExprs2 struct {
Expressions []SelectExpr
}

type (
// SelectExpr represents a SELECT expression.
SelectExpr interface {
Expand Down
1 change: 0 additions & 1 deletion go/vt/sqlparser/ast_clone.go

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

9 changes: 9 additions & 0 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,15 @@ func (node SelectExprs) Format(buf *TrackedBuffer) {
}
}

// Format formats the node.
func (node *SelectExprs2) Format(buf *TrackedBuffer) {
var prefix string
for _, n := range node.Expressions {
buf.astPrintf(node, "%s%v", prefix, n)
prefix = ", "
}
}

// Format formats the node.
func (node *StarExpr) Format(buf *TrackedBuffer) {
if !node.TableName.IsEmpty() {
Expand Down
10 changes: 10 additions & 0 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.

27 changes: 19 additions & 8 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ func NewLimitWithoutOffset(rowCount int) *Limit {
// NewSelect is used to create a select statement
func NewSelect(
comments Comments,
exprs SelectExprs,
exprs *SelectExprs2,
selectOptions []string,
into *SelectInto,
from TableExprs,
Expand Down Expand Up @@ -1195,8 +1195,8 @@ func compliantName(in string) string {
return buf.String()
}

func (node *Select) AddSelectExprs(selectExprs SelectExprs) {
node.SelectExprs = append(node.SelectExprs, selectExprs...)
func (node *Select) AddSelectExprs(selectExprs SelectExprs2) {
node.SelectExprs.Expressions = append(node.SelectExprs.Expressions, selectExprs.Expressions...)
}

// AddOrder adds an order by element
Expand Down Expand Up @@ -1256,11 +1256,11 @@ func (node *Select) IsDistinct() bool {

// GetColumnCount return SelectExprs count.
func (node *Select) GetColumnCount() int {
return len(node.SelectExprs)
return len(node.SelectExprs.Expressions)
}

// GetColumns gets the columns
func (node *Select) GetColumns() SelectExprs {
func (node *Select) GetColumns() *SelectExprs2 {
return node.SelectExprs
}

Expand Down Expand Up @@ -1362,7 +1362,7 @@ func (node *Union) GetLimit() *Limit {
}

// GetColumns gets the columns
func (node *Union) GetColumns() SelectExprs {
func (node *Union) GetColumns() *SelectExprs2 {
return node.Left.GetColumns()
}

Expand Down Expand Up @@ -2419,6 +2419,16 @@ func (s SelectExprs) AllAggregation() bool {
return true
}

// AllAggregation returns true if all the expressions contain aggregation
func (s *SelectExprs2) AllAggregation() bool {
for _, k := range s.Expressions {
if !ContainsAggregation(k) {
return false
}
}
return true
}

// RemoveKeyspaceInCol removes the Qualifier.Qualifier on all ColNames in the AST
func RemoveKeyspaceInCol(in SQLNode) {
// Walk will only return an error if we return an error from the inner func. safe to ignore here
Expand Down Expand Up @@ -3053,10 +3063,11 @@ func (node *ValuesStatement) GetColumnCount() int {
panic("no columns available") // TODO: we need a better solution than a panic
}

func (node *ValuesStatement) GetColumns() (result SelectExprs) {
func (node *ValuesStatement) GetColumns() *SelectExprs2 {
sel := new(SelectExprs2)
columnCount := node.GetColumnCount()
for i := range columnCount {
result = append(result, &AliasedExpr{Expr: NewColName(fmt.Sprintf("column_%d", i))})
sel.Expressions = append(sel.Expressions, &AliasedExpr{Expr: NewColName(fmt.Sprintf("column_%d", i))})
}
panic("no columns available") // TODO: we need a better solution than a panic
}
Expand Down
6 changes: 0 additions & 6 deletions go/vt/sqlparser/cached_size.go

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

8 changes: 4 additions & 4 deletions go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ func (nz *normalizer) unnestSubQueries(cursor *Cursor, subquery *Subquery) {
return
}

if len(sel.SelectExprs) != 1 ||
if len(sel.SelectExprs.Expressions) != 1 ||
len(sel.OrderBy) != 0 ||
sel.GroupBy != nil ||
len(sel.From) != 1 ||
Expand All @@ -766,7 +766,7 @@ func (nz *normalizer) unnestSubQueries(cursor *Cursor, subquery *Subquery) {
if !ok || table.Name.String() != "dual" {
return
}
expr, ok := sel.SelectExprs[0].(*AliasedExpr)
expr, ok := sel.SelectExprs.Expressions[0].(*AliasedExpr)
if !ok {
return
}
Expand Down Expand Up @@ -810,8 +810,8 @@ func (nz *normalizer) existsRewrite(cursor *Cursor, node *ExistsExpr) {

// Simplify the subquery by selecting a constant.
// WHERE EXISTS(SELECT 1 FROM ...)
sel.SelectExprs = SelectExprs{
&AliasedExpr{Expr: NewIntLiteral("1")},
sel.SelectExprs = &SelectExprs2{
Expressions: []SelectExpr{&AliasedExpr{Expr: NewIntLiteral("1")}},
}
sel.GroupBy = nil
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/parsed_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ func TestCastBindVars(t *testing.T) {
}

s := &Select{
SelectExprs: SelectExprs{
NewAliasedExpr(argument, ""),
SelectExprs: &SelectExprs2{
Expressions: []SelectExpr{NewAliasedExpr(argument, "")},
},
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/random_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (g *Generator) existsExpr(genConfig ExprGeneratorConfig) Expr {
} else {
// if g.subqueryExpr doesn't return a valid subquery, replace with
// select 1
selectExprs := SelectExprs{NewAliasedExpr(NewIntLiteral("1"), "")}
selectExprs := &SelectExprs2{Expressions: []SelectExpr{NewAliasedExpr(NewIntLiteral("1"), "")}}
from := TableExprs{NewAliasedTableExpr(NewTableName("dual"), "")}
expr = NewExistsExpr(NewSubquery(NewSelect(nil, selectExprs, nil, nil, from, nil, nil, nil, nil)))
}
Expand Down

0 comments on commit 8538140

Please sign in to comment.