Skip to content

Commit

Permalink
opt: introduce optimizer expression trees and build them from TypedExprs
Browse files Browse the repository at this point in the history
This change brings in a subset of
https://github.com/petermattis/opttoy/tree/master/v3

This change introduces:

 - the expr tree: cascades-style optimizers operate on expression
   trees which can represent both scalar and relational expressions;
   this is a departure from the way we represent expressions and
   statements (sem/tree) so we need a new tree structure.

 - scalar operators: initially, we focus only on scalar expressions.

 - building an expr tree from a sem/tree.TypedExpr.

 - opt version of logic tests

See the RFC in cockroachdb#19135 for more context on the optimizer.

This is the first step of an initial project related to the optimizer:
generating index constraints from scalar expressions. This will be a
rewrite of the current index constraint generation code (which has
many problems, see cockroachdb#6346).  Roughly, the existing
`makeIndexConstraints` will call into the optimizer with a `TypedExpr`
and the optimizer will return index constraints.

Release note: None
  • Loading branch information
RaduBerinde committed Dec 7, 2017
1 parent 42aa2ad commit 1c1cc24
Show file tree
Hide file tree
Showing 9 changed files with 987 additions and 0 deletions.
165 changes: 165 additions & 0 deletions pkg/sql/opt/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package opt

import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)

// Map from tree.ComparisonOperator to operator.
var comparisonOpMap = [...]operator{
tree.EQ: eqOp,
tree.LT: ltOp,
tree.GT: gtOp,
tree.LE: leOp,
tree.GE: geOp,
tree.NE: neOp,
tree.In: inOp,
tree.NotIn: notInOp,
tree.Like: likeOp,
tree.NotLike: notLikeOp,
tree.ILike: iLikeOp,
tree.NotILike: notILikeOp,
tree.SimilarTo: similarToOp,
tree.NotSimilarTo: notSimilarToOp,
tree.RegMatch: regMatchOp,
tree.NotRegMatch: notRegMatchOp,
tree.RegIMatch: regIMatchOp,
tree.NotRegIMatch: notRegIMatchOp,
tree.IsDistinctFrom: isDistinctFromOp,
tree.IsNotDistinctFrom: isNotDistinctFromOp,
tree.Is: isOp,
tree.IsNot: isNotOp,
tree.Any: anyOp,
tree.Some: someOp,
tree.All: allOp,
}

// Map from tree.BinaryOperator to operator.
var binaryOpMap = [...]operator{
tree.Bitand: bitandOp,
tree.Bitor: bitorOp,
tree.Bitxor: bitxorOp,
tree.Plus: plusOp,
tree.Minus: minusOp,
tree.Mult: multOp,
tree.Div: divOp,
tree.FloorDiv: floorDivOp,
tree.Mod: modOp,
tree.Pow: powOp,
tree.Concat: concatOp,
tree.LShift: lShiftOp,
tree.RShift: rShiftOp,
}

// Map from tree.UnaryOperator to operator.
var unaryOpMap = [...]operator{
tree.UnaryPlus: unaryPlusOp,
tree.UnaryMinus: unaryMinusOp,
tree.UnaryComplement: unaryComplementOp,
}

type buildContext struct {
// We allocate *scalarProps and *expr in chunks.
preallocScalarProps []scalarProps
preallocExprs []expr
}

const exprAllocChunk = 16
const scalarPropsAllocChunk = 16

func (bc *buildContext) newScalarProps() *scalarProps {
if len(bc.preallocScalarProps) == 0 {
bc.preallocScalarProps = make([]scalarProps, scalarPropsAllocChunk)
}
p := &bc.preallocScalarProps[0]
bc.preallocScalarProps = bc.preallocScalarProps[1:]
return p
}

// newExpr returns a new *expr with a new, blank scalarProps.
func (bc *buildContext) newExpr() *expr {
if len(bc.preallocExprs) == 0 {
bc.preallocExprs = make([]expr, exprAllocChunk)
}
e := &bc.preallocExprs[0]
bc.preallocExprs = bc.preallocExprs[1:]
e.scalarProps = bc.newScalarProps()
return e
}

// buildScalar converts a tree.TypedExpr to an expr tree.
func buildScalar(buildCtx *buildContext, pexpr tree.TypedExpr) *expr {
switch t := pexpr.(type) {
case *tree.ParenExpr:
return buildScalar(buildCtx, t.TypedInnerExpr())
}

e := buildCtx.newExpr()
e.scalarProps.typ = pexpr.ResolvedType()

switch t := pexpr.(type) {
case *tree.AndExpr:
initBinaryExpr(
e, andOp,
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.OrExpr:
initBinaryExpr(
e, orOp,
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.NotExpr:
initUnaryExpr(e, notOp, buildScalar(buildCtx, t.TypedInnerExpr()))

case *tree.BinaryExpr:
initBinaryExpr(
e, binaryOpMap[t.Operator],
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.ComparisonExpr:
initBinaryExpr(
e, comparisonOpMap[t.Operator],
buildScalar(buildCtx, t.TypedLeft()),
buildScalar(buildCtx, t.TypedRight()),
)
case *tree.UnaryExpr:
initUnaryExpr(e, unaryOpMap[t.Operator], buildScalar(buildCtx, t.TypedInnerExpr()))

case *tree.FuncExpr:
def, err := t.Func.Resolve(tree.SearchPath{})
if err != nil {
panic(err.Error())
}
children := make([]*expr, len(t.Exprs))
for i, pexpr := range t.Exprs {
children[i] = buildScalar(buildCtx, pexpr.(tree.TypedExpr))
}
initFunctionExpr(e, def, children)

case *tree.IndexedVar:
initVariableExpr(e, t.Idx)

case tree.Datum:
initConstExpr(e, t)

default:
panicf("node %T not supported", t)
}
return e
}
61 changes: 61 additions & 0 deletions pkg/sql/opt/expr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package opt

// expr is a unified interface for both relational and scalar expressions in a
// query. TODO(radu): currently, it only supports scalar expressions.
type expr struct {
op operator
// Child expressions. The interpretation of the children is operator
// dependent.
children []*expr
// Scalar properties.
scalarProps *scalarProps
// Operator-dependent data used by this expression. For example, constOp
// stores a pointer to the constant value.
private interface{}
}

func (e *expr) opClass() operatorClass {
return operatorTab[e.op].class
}

func (e *expr) inputs() []*expr {
return e.children
}

func formatExprs(tp *treePrinter, title string, exprs []*expr) {
if len(exprs) > 0 {
tp.Add(title)
tp.Enter()
for _, e := range exprs {
if e != nil {
e.format(tp)
}
}
tp.Exit()
}
}

// format is part of the operatorInfo interface.
func (e *expr) format(tp *treePrinter) {
e.opClass().format(e, tp)
}

func (e *expr) String() string {
tp := makeTreePrinter()
e.format(&tp)
return tp.String()
}
21 changes: 21 additions & 0 deletions pkg/sql/opt/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package opt

import "fmt"

func panicf(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
111 changes: 111 additions & 0 deletions pkg/sql/opt/operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package opt

import "fmt"

type operator uint8

const (
unknownOp operator = iota

// TODO(radu): no relational operators yet.

// Scalar operators
variableOp
constOp
listOp
orderedListOp

existsOp

andOp
orOp
notOp

eqOp
ltOp
gtOp
leOp
geOp
neOp
inOp
notInOp
likeOp
notLikeOp
iLikeOp
notILikeOp
similarToOp
notSimilarToOp
regMatchOp
notRegMatchOp
regIMatchOp
notRegIMatchOp
isDistinctFromOp
isNotDistinctFromOp
isOp
isNotOp
anyOp
someOp
allOp

bitandOp
bitorOp
bitxorOp
plusOp
minusOp
multOp
divOp
floorDivOp
modOp
powOp
concatOp
lShiftOp
rShiftOp

unaryPlusOp
unaryMinusOp
unaryComplementOp

functionOp

numOperators
)

type operatorInfo struct {
name string
class operatorClass
}

var operatorTab = [numOperators]operatorInfo{
unknownOp: {name: "unknown"},
}

func (op operator) String() string {
if op >= numOperators {
return fmt.Sprintf("operator(%d)", op)
}
return operatorTab[op].name
}

func registerOperator(op operator, name string, class operatorClass) {
operatorTab[op].name = name
operatorTab[op].class = class
}

type operatorClass interface {
// format outputs information about the expr tree to a treePrinter.
format(e *expr, tp *treePrinter)
}
Loading

0 comments on commit 1c1cc24

Please sign in to comment.