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

sql: add star (*) parsing and resolution #63

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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
12 changes: 12 additions & 0 deletions sql/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ func TestAnalyzer_Analyze(t *testing.T) {
assert.Nil(err)
assert.Equal(expected, analyzed)

notAnalyzed = plan.NewProject(
[]sql.Expression{expression.NewStar()},
plan.NewUnresolvedTable("mytable"),
)
analyzed, err = a.Analyze(notAnalyzed)
expected = plan.NewProject(
[]sql.Expression{expression.NewGetField(0, sql.Integer, "i")},
table,
)
assert.Nil(err)
assert.Equal(expected, analyzed)

notAnalyzed = plan.NewProject(
[]sql.Expression{
expression.NewAlias(
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