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

analyzer: fix pushdown of filters with repeated table names #498

Merged
merged 1 commit into from
Oct 24, 2018
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
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