Skip to content

Commit

Permalink
Push down expressions to the storage level (matrixorigin#2212)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnsgmsone authored Apr 13, 2022
1 parent 064822e commit 93583a4
Show file tree
Hide file tree
Showing 22 changed files with 119 additions and 692 deletions.
3 changes: 2 additions & 1 deletion pkg/frontend/test/engine_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (a *Attribute) Attributes() []string {
return []string{a.Name}
}

func (a *Attribute) ExtendAttributes() []*Attribute {
return []*Attribute{a}
}

func (a *Attribute) ReturnType() types.T {
return a.Type
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (e *BinaryExtend) Attributes() []string {
return append(e.Left.Attributes(), e.Right.Attributes()...)
}

func (e *BinaryExtend) ExtendAttributes() []*Attribute {
return append(e.Left.ExtendAttributes(), e.Right.ExtendAttributes()...)
}

func (e *BinaryExtend) ReturnType() types.T {
if fn, ok := BinaryReturnTypes[e.Op]; ok {
return fn(e.Left, e.Right)
Expand Down
16 changes: 15 additions & 1 deletion pkg/sql/colexec/extend/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ func (_ *FuncExtend) IsConstant() bool {
}

func (a *FuncExtend) Attributes() []string {
return []string{a.Name}
var attrs []string

for _, arg := range a.Args {
attrs = append(attrs, arg.Attributes()...)
}
return attrs
}

func (a *FuncExtend) ExtendAttributes() []*Attribute {
var attrs []*Attribute

for _, arg := range a.Args {
attrs = append(attrs, arg.ExtendAttributes()...)
}
return attrs
}

func (a *FuncExtend) ReturnType() types.T {
Expand Down
10 changes: 10 additions & 0 deletions pkg/sql/colexec/extend/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ func (_ *MultiExtend) IsConstant() bool {

func (e *MultiExtend) Attributes() []string {
var attrs []string

for _, arg := range e.Args {
attrs = append(attrs, arg.Attributes()...)
}
return attrs
}

func (e *MultiExtend) ExtendAttributes() []*Attribute {
var attrs []*Attribute

for _, arg := range e.Args {
attrs = append(attrs, arg.ExtendAttributes()...)
}
return attrs
}

func (e *MultiExtend) ReturnType() types.T {
if fn, ok := MultiReturnTypes[e.Op]; ok {
return fn(e.Args)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/paren.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (e *ParenExtend) Attributes() []string {
return e.E.Attributes()
}

func (e *ParenExtend) ExtendAttributes() []*Attribute {
return e.E.ExtendAttributes()
}

func (e *ParenExtend) Eval(bat *batch.Batch, proc *process.Process) (*vector.Vector, types.T, error) {
return e.E.Eval(bat, proc)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (a *StarExtend) Attributes() []string {
return []string{}
}

func (a *StarExtend) ExtendAttributes() []*Attribute {
return nil
}

func (a *StarExtend) ReturnType() types.T {
return 0
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/colexec/extend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Extend interface {
IsConstant() bool
ReturnType() types.T
Attributes() []string
ExtendAttributes() []*Attribute
Eval(*batch.Batch, *process.Process) (*vector.Vector, types.T, error)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (e *UnaryExtend) Attributes() []string {
return e.E.Attributes()
}

func (e *UnaryExtend) ExtendAttributes() []*Attribute {
return e.E.ExtendAttributes()
}

func (e *UnaryExtend) ReturnType() types.T {
if fn, ok := UnaryReturnTypes[e.Op]; ok {
return fn(e.E)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/colexec/extend/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (_ *ValueExtend) Attributes() []string {
return nil
}

func (_ *ValueExtend) ExtendAttributes() []*Attribute {
return nil
}

func (a *ValueExtend) Eval(_ *batch.Batch, _ *process.Process) (*vector.Vector, types.T, error) {
return a.V, a.V.Typ.Oid, nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/compile/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func (s *Scope) RunQ(e engine.Engine) error {
return err
}
defer rel.Close()
rds = rel.NewReader(mcpu)
rds = rel.NewReader(mcpu, nil, nil)
}
ss := make([]*Scope, mcpu)
for i := 0; i < mcpu; i++ {
Expand Down Expand Up @@ -809,7 +809,7 @@ func (s *Scope) RunAQ(e engine.Engine) error {
return err
}
defer rel.Close()
rds = rel.NewReader(mcpu)
rds = rel.NewReader(mcpu, nil, nil)
}
ss := make([]*Scope, mcpu)
arg := s.Instructions[0].Arg.(*transform.Argument)
Expand Down Expand Up @@ -998,7 +998,7 @@ func (s *Scope) RunCQ(e engine.Engine, op *join.Argument) error {
return err
}
defer rel.Close()
rds = rel.NewReader(mcpu)
rds = rel.NewReader(mcpu, nil, nil)
}
ss := make([]*Scope, mcpu)
for i := 0; i < mcpu; i++ {
Expand Down Expand Up @@ -1336,7 +1336,7 @@ func (s *Scope) RunCAQ(e engine.Engine, op *times.Argument) error {
return err
}
defer rel.Close()
rds = rel.NewReader(mcpu)
rds = rel.NewReader(mcpu, nil, nil)
}
ss := make([]*Scope, mcpu)
for i := 0; i < mcpu; i++ {
Expand Down
190 changes: 31 additions & 159 deletions pkg/sql/plan/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,6 @@ import (
"github.com/matrixorigin/matrixone/pkg/sql/errors"
)

func isSingle(s *Scope) interface{} {
switch op := s.Op.(type) {
case *Order:
return isSingle(s.Children[0])
case *Dedup:
return isSingle(s.Children[0])
case *Limit:
return isSingle(s.Children[0])
case *Offset:
return isSingle(s.Children[0])
case *Restrict:
return isSingle(s.Children[0])
case *Projection:
return isSingle(s.Children[0])
case *ResultProjection:
return isSingle(s.Children[0])
case *Rename:
return isSingle(s.Children[0])
case *Untransform:
return isSingle(s.Children[0])
case *Relation:
return op
case *DerivedRelation:
return op
case *Join:
return nil
}
return nil
}

// check if it is conjunctive aggregation query.
func isCAQ(flg bool, s *Scope) bool {
switch s.Op.(type) {
Expand Down Expand Up @@ -168,29 +138,7 @@ func pushDownUntransform(s *Scope, fvars []string) {
}

func pushDownRestrict(flg bool, e extend.Extend, qry *Query) extend.Extend {
if op := isSingle(qry.Scope); flg && op != nil {
switch rel := op.(type) {
case *Relation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: e,
Left: rel.Cond,
}
} else {
rel.Cond = e
}
case *DerivedRelation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: e,
Left: rel.Cond,
}
} else {
rel.Cond = e
}
}
if pushDownRestrictExtend(e, qry) {
return nil
}
if es := extend.AndExtends(e, nil); len(es) > 0 {
Expand Down Expand Up @@ -425,122 +373,46 @@ func pushDownProjectionExtend(e extend.Extend, qry *Query) extend.Extend {
func pushDownRestrictExtend(e extend.Extend, qry *Query) bool {
var s *Scope

v, ok := e.(*extend.BinaryExtend)
if !ok {
attrs := e.ExtendAttributes()
if len(attrs) == 0 {
return false
}
left, lok := v.Left.(*extend.Attribute)
right, rok := v.Right.(*extend.Attribute)
switch {
case lok && rok:
var lname, rname string

if s = findScopeWithAttribute(left.Name, &lname, qry.Scope); s == nil {
return false
}
t := findScopeWithAttribute(right.Name, &rname, qry.Scope)
aliases := make([]string, len(attrs))
if s = findScopeWithAttribute(attrs[0].Name, &aliases[0], qry.Scope); s == nil {
return false
}
for i := 1; i < len(attrs); i++ {
t := findScopeWithAttribute(attrs[i].Name, &aliases[i], qry.Scope)
if t != s {
return false
}
v.Left = &extend.Attribute{
Name: lname,
Type: left.Type,
}
v.Right = &extend.Attribute{
Name: rname,
Type: right.Type,
}
switch rel := s.Op.(type) {
case *Relation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
}
case *DerivedRelation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
}
}
return true
case !lok && rok:
var rname string

if s = findScopeWithAttribute(right.Name, &rname, qry.Scope); s == nil {
return false
}
v.Right = &extend.Attribute{
Name: rname,
Type: right.Type,
}
switch rel := s.Op.(type) {
case *Relation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
}
case *DerivedRelation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
}
for i, attr := range attrs {
attr.Name = aliases[i]
}
switch rel := s.Op.(type) {
case *Relation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: e,
Left: rel.Cond,
}
} else {
rel.Cond = e
}
return true
case lok && !rok:
var lname string

if s = findScopeWithAttribute(left.Name, &lname, qry.Scope); s == nil {
return false
}
v.Left = &extend.Attribute{
Name: lname,
Type: left.Type,
}
switch rel := s.Op.(type) {
case *Relation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
}
case *DerivedRelation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: v,
Left: rel.Cond,
}
} else {
rel.Cond = v
case *DerivedRelation:
if rel.Cond != nil {
rel.Cond = &extend.BinaryExtend{
Op: overload.And,
Right: e,
Left: rel.Cond,
}
} else {
rel.Cond = e
}
return true
}
return false
return true
}

func findScopeWithAttribute(name string, alias *string, s *Scope) *Scope {
Expand Down
Loading

0 comments on commit 93583a4

Please sign in to comment.