Skip to content

Commit

Permalink
Make Expr implement fmt.Stringer interface (#613)
Browse files Browse the repository at this point in the history
* Make Expr implement fmt.Stringer interface

For now we only alias `(Expr).Name`

Closes #521

* fix lint error
  • Loading branch information
gernest authored Dec 7, 2023
1 parent 1796578 commit c373f32
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions query/logicalplan/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package logicalplan

import (
"fmt"

"github.com/apache/arrow/go/v14/arrow"
"github.com/parquet-go/parquet-go"
)
Expand Down Expand Up @@ -60,6 +62,7 @@ type Expr interface {
DataType(*parquet.Schema) (arrow.DataType, error)
Accept(Visitor) bool
Name() string
fmt.Stringer

// ColumnsUsedExprs extracts all the expressions that are used that cause
// physical data to be read from a column.
Expand Down
24 changes: 22 additions & 2 deletions query/logicalplan/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func (e *BinaryExpr) Name() string {
return e.Left.Name() + " " + e.Op.String() + " " + e.Right.Name()
}

func (e *BinaryExpr) String() string { return e.Name() }

func (e *BinaryExpr) ColumnsUsedExprs() []Expr {
return append(e.Left.ColumnsUsedExprs(), e.Right.ColumnsUsedExprs()...)
}
Expand Down Expand Up @@ -147,6 +149,8 @@ func (c *Column) Name() string {
return c.ColumnName
}

func (c *Column) String() string { return c.Name() }

func (c *Column) DataType(s *parquet.Schema) (arrow.DataType, error) {
for _, field := range s.Fields() {
af, err := c.findField("", field)
Expand Down Expand Up @@ -368,6 +372,8 @@ func (c *DynamicColumn) Name() string {
return c.ColumnName
}

func (c *DynamicColumn) String() string { return c.Name() }

func (c *DynamicColumn) Accept(visitor Visitor) bool {
return visitor.PreVisit(c) && visitor.PostVisit(c)
}
Expand Down Expand Up @@ -408,6 +414,8 @@ func (e *LiteralExpr) Name() string {
return e.Value.String()
}

func (e *LiteralExpr) String() string { return e.Name() }

func (e *LiteralExpr) Accept(visitor Visitor) bool {
continu := visitor.PreVisit(e)
if !continu {
Expand Down Expand Up @@ -465,6 +473,8 @@ func (f *AggregationFunction) Name() string {
return f.Func.String() + "(" + f.Expr.Name() + ")"
}

func (f *AggregationFunction) String() string { return f.Name() }

func (f *AggregationFunction) ColumnsUsedExprs() []Expr {
return f.Expr.ColumnsUsedExprs()
}
Expand Down Expand Up @@ -560,6 +570,8 @@ func (e *AliasExpr) Name() string {
return e.Alias
}

func (e *AliasExpr) String() string { return e.Name() }

func (e *AliasExpr) Computed() bool {
return e.Expr.Computed()
}
Expand Down Expand Up @@ -632,6 +644,8 @@ func (d *DurationExpr) Name() string {
return ""
}

func (d *DurationExpr) String() string { return d.Name() }

func (d *DurationExpr) ColumnsUsedExprs() []Expr {
// DurationExpr expect to work on a timestamp column
return []Expr{Col("timestamp")}
Expand Down Expand Up @@ -667,6 +681,8 @@ func (a *AverageExpr) Name() string {
return a.Expr.Name()
}

func (a *AverageExpr) String() string { return a.Name() }

func (a *AverageExpr) ColumnsUsedExprs() []Expr {
return a.Expr.ColumnsUsedExprs()
}
Expand Down Expand Up @@ -724,6 +740,8 @@ func (a *RegexpColumnMatch) Name() string {
return a.match.String()
}

func (a *RegexpColumnMatch) String() string { return a.Name() }

func (a *RegexpColumnMatch) ColumnsUsedExprs() []Expr {
return []Expr{a}
}
Expand Down Expand Up @@ -764,7 +782,8 @@ func (a *AllExpr) Accept(visitor Visitor) bool {

return visitor.PostVisit(a)
}
func (a *AllExpr) Name() string { return "all" }
func (a *AllExpr) Name() string { return "all" }
func (a *AllExpr) String() string { return a.Name() }
func (a *AllExpr) ColumnsUsedExprs() []Expr {
return []Expr{&AllExpr{}}
}
Expand Down Expand Up @@ -792,7 +811,8 @@ func (n *NotExpr) Accept(visitor Visitor) bool {

return visitor.PostVisit(n)
}
func (n *NotExpr) Name() string { return "not " + n.Expr.Name() }
func (n *NotExpr) Name() string { return "not " + n.Expr.Name() }
func (n *NotExpr) String() string { return n.Name() }
func (n *NotExpr) ColumnsUsedExprs() []Expr {
return []Expr{&NotExpr{Expr: n.Expr}}
}
Expand Down
2 changes: 1 addition & 1 deletion query/logicalplan/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (e *ExprValidationError) Error() string {
message := make([]string, 0)
message = append(message, e.message)
message = append(message, ": ")
message = append(message, fmt.Sprintf("%s", e.expr))
message = append(message, e.expr.String())
for _, child := range e.children {
message = append(message, "\n -> invalid sub-expression: ")
message = append(message, child.Error())
Expand Down

0 comments on commit c373f32

Please sign in to comment.