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 8, 2017
1 parent 9d615b2 commit d49013e
Show file tree
Hide file tree
Showing 7 changed files with 880 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,
}

// We allocate *scalarProps and *expr in chunks of these sizes.
const exprAllocChunk = 16
const scalarPropsAllocChunk = 16

type buildContext struct {
preallocScalarProps []scalarProps
preallocExprs []expr
}

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))
}
initFunctionCallExpr(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
}
70 changes: 70 additions & 0 deletions pkg/sql/opt/expr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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/util/treeprinter"

// expr implements the node of a unified expressions tree for both relational
// and scalar expressions in a query.
//
// Expressions have optional inputs. Expressions also maintain properties. For
// scalar expressions, the properties are stored in scalarProps.
//
// Currently, it only supports scalar expressions and operators. More
// information pertaining to relational operators will be added when they are
// supported.
//
// TODO(radu): support relational operators and extend this description.
type expr struct {
op operator
// Child expressions. The interpretation of the children is operator
// dependent.
children []*expr
// Scalar properties (properties that pertain only to scalar operators).
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.Node, title string, exprs []*expr) {
if len(exprs) > 0 {
n := tp.Child(title)
for _, e := range exprs {
if e != nil {
e.format(n)
}
}
}
}

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

func (e *expr) String() string {
tp := treeprinter.New()
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...))
}
131 changes: 131 additions & 0 deletions pkg/sql/opt/operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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"

"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
)

type operator uint8

const (
unknownOp operator = iota

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

// -- Scalar operators --

// variableOp is a leaf expression that represents a non-constant value, like a column
// in a table.
variableOp

// constOp is a leaf expression that has a constant value.
constOp

// listOp is an unordered list of expressions. Currently unused.
listOp

// orderedListOp is an ordered list of expressions. Currently unused.
orderedListOp

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

functionCallOp

numOperators
)

// operatorInfo stores static information about an operator.
type operatorInfo struct {
// name of the operator, used when printing expressions.
name string
// class of the operator (see operatorClass).
class operatorClass
}

// operatorTab stores static information about all operators.
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
}

// registerOperator initializes the operator's entry in operatorTab.
// There must be a call to registerOperator in an init() function for every
// operator.
func registerOperator(op operator, name string, class operatorClass) {
operatorTab[op].name = name
operatorTab[op].class = class
}

// operatorClass implements functionality that is common for a subset of
// operators.
type operatorClass interface {
// format outputs information about the expr tree to a treePrinter.
format(e *expr, tp treeprinter.Node)
}
Loading

0 comments on commit d49013e

Please sign in to comment.