Skip to content

Commit

Permalink
sql: add star (*) parsing and resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
smola committed Nov 18, 2016
1 parent 003dc36 commit a50a69d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ gitql exposes the following tables:

gitql supports a subset of the SQL standard, currently including:

* `SELECT` (`*` not supported yet)
* `SELECT`
* `WHERE` (`=` only)
* `ORDER BY` (with `ASC` and `DESC`)
* `LIMIT`
Expand Down
1 change: 0 additions & 1 deletion cmd/gitql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func (c *CmdQuery) printQuery(schema sql.Schema, iter sql.RowIter) {
w := tablewriter.NewWriter(os.Stdout)
headers := []string{}
for _, f := range schema {
fmt.Printf("HEADER: %s\n", f.Name)
headers = append(headers, f.Name)
}
w.SetHeader(headers)
Expand Down
28 changes: 28 additions & 0 deletions sql/analyzer/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
var DefaultRules = []Rule{
{"resolve_tables", resolveTables},
{"resolve_columns", resolveColumns},
{"resolve_star", resolveStar},
}

func resolveTables(a *Analyzer, n sql.Node) sql.Node {
Expand All @@ -28,6 +29,33 @@ func resolveTables(a *Analyzer, n sql.Node) sql.Node {
})
}

func resolveStar(a *Analyzer, n sql.Node) sql.Node {
if n.Resolved() {
return n
}

p, ok := n.(*plan.Project)
if !ok {
return n
}

if len(p.Expressions) != 1 {
return n
}

if _, ok := p.Expressions[0].(*expression.Star); !ok {
return n
}

var exprs []sql.Expression
for i, e := range p.Child.Schema() {
gf := expression.NewGetField(i, e.Type, e.Name)
exprs = append(exprs, gf)
}

return plan.NewProject(exprs, p.Child)
}

func resolveColumns(a *Analyzer, n sql.Node) sql.Node {
if n.Resolved() {
return n
Expand Down
14 changes: 7 additions & 7 deletions sql/plan/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (

type Project struct {
UnaryNode
expressions []sql.Expression
Expressions []sql.Expression
}

func NewProject(expressions []sql.Expression, child sql.Node) *Project {
return &Project{
UnaryNode: UnaryNode{child},
expressions: expressions,
Expressions: expressions,
}
}

func (p *Project) Schema() sql.Schema {
var s sql.Schema
for _, e := range p.expressions {
for _, e := range p.Expressions {
f := sql.Field{
Name: e.Name(),
Type: e.Type(),
Expand All @@ -33,7 +33,7 @@ func (p *Project) Resolved() bool {
}

func (p *Project) expressionsResolved() bool {
for _, e := range p.expressions {
for _, e := range p.Expressions {
if !e.Resolved() {
return false
}
Expand All @@ -51,15 +51,15 @@ func (p *Project) RowIter() (sql.RowIter, error) {

func (p *Project) TransformUp(f func(sql.Node) sql.Node) sql.Node {
c := p.UnaryNode.Child.TransformUp(f)
n := NewProject(p.expressions, c)
n := NewProject(p.Expressions, c)

return f(n)
}

func (p *Project) TransformExpressionsUp(f func(sql.Expression) sql.Expression) sql.Node {
c := p.UnaryNode.Child.TransformExpressionsUp(f)
es := []sql.Expression{}
for _, e := range p.expressions {
for _, e := range p.Expressions {
te := e.TransformUp(f)
es = append(es, te)
}
Expand All @@ -78,7 +78,7 @@ func (i *iter) Next() (sql.Row, error) {
if err != nil {
return nil, err
}
return filterRow(i.p.expressions, childRow), nil
return filterRow(i.p.Expressions, childRow), nil
}

func filterRow(expressions []sql.Expression, row sql.Row) sql.Row {
Expand Down

0 comments on commit a50a69d

Please sign in to comment.