Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
analyzer: fix pushdown of filters with repeated table names
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
  • Loading branch information
erizocosmico committed Oct 24, 2018
1 parent 6d0f641 commit fd90e6d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
12 changes: 8 additions & 4 deletions sql/analyzer/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ func (f filters) merge(f2 filters) {
func exprToTableFilters(expr sql.Expression) filters {
filtersByTable := make(filters)
for _, expr := range splitExpression(expr) {
var tables []string
var seenTables = make(map[string]struct{})
var lastTable string
_, _ = expr.TransformUp(func(e sql.Expression) (sql.Expression, error) {
f, ok := e.(*expression.GetField)
if ok {
tables = append(tables, f.Table())
if _, ok := seenTables[f.Table()]; !ok {
seenTables[f.Table()] = struct{}{}
lastTable = f.Table()
}
}

return e, nil
})

if len(tables) == 1 {
filtersByTable[tables[0]] = append(filtersByTable[tables[0]], expr)
if len(seenTables) == 1 {
filtersByTable[lastTable] = append(filtersByTable[lastTable], expr)
}
}

Expand Down
28 changes: 25 additions & 3 deletions sql/analyzer/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ func TestExprToTableFilters(t *testing.T) {
require := require.New(t)
expr := expression.NewAnd(
expression.NewAnd(
expression.NewAnd(
expression.NewEquals(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3.14, sql.Float64),
),
expression.NewGreaterThan(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3., sql.Float64),
),
),
expression.NewIsNull(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable2", "i2", false),
),
),
expression.NewOr(
expression.NewEquals(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3.14, sql.Float64),
Expand All @@ -117,9 +132,6 @@ func TestExprToTableFilters(t *testing.T) {
expression.NewLiteral(3., sql.Float64),
),
),
expression.NewIsNull(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable2", "i2", false),
),
)

expected := filters{
Expand All @@ -132,6 +144,16 @@ func TestExprToTableFilters(t *testing.T) {
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3., sql.Float64),
),
expression.NewOr(
expression.NewEquals(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3.14, sql.Float64),
),
expression.NewGreaterThan(
expression.NewGetFieldWithTable(0, sql.Int64, "mytable", "f", false),
expression.NewLiteral(3., sql.Float64),
),
),
},
"mytable2": []sql.Expression{
expression.NewIsNull(
Expand Down

0 comments on commit fd90e6d

Please sign in to comment.