cockroach: github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder Index | Files

package optbuilder

import "github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder"

Index

Package Files

alter_table.go builder.go create_table.go create_view.go delete.go distinct.go explain.go export.go groupby.go insert.go join.go limit.go misc_statements.go mutation_builder.go opaque.go orderby.go project.go scalar.go scope.go scope_column.go select.go show_trace.go srfs.go subquery.go union.go update.go util.go values.go window.go with.go

func RegisterOpaque Uses

func RegisterOpaque(stmtType reflect.Type, opaqueType OpaqueType, fn BuildOpaqueFn)

RegisterOpaque registers an opaque handler for a specific statement type.

type BuildOpaqueFn Uses

type BuildOpaqueFn func(
    context.Context, *tree.SemaContext, *tree.EvalContext, tree.Statement,
) (opt.OpaqueMetadata, sqlbase.ResultColumns, error)

BuildOpaqueFn is a handler for building the metadata for an opaque statement.

type Builder Uses

type Builder struct {

    // AllowUnsupportedExpr is a control knob: if set, when building a scalar, the
    // builder takes any TypedExpr node that it doesn't recognize and wraps that
    // expression in an UnsupportedExpr node. This is temporary; it is used for
    // interfacing with the old planning code.
    AllowUnsupportedExpr bool

    // KeepPlaceholders is a control knob: if set, optbuilder will never replace
    // a placeholder operator with its assigned value, even when it is available.
    // This is used when re-preparing invalidated queries.
    KeepPlaceholders bool

    // HadPlaceholders is set to true if we replaced any placeholders with their
    // values.
    HadPlaceholders bool

    // DisableMemoReuse is set to true if we encountered a statement that is not
    // safe to cache the memo for. This is the case for various DDL and SHOW
    // statements.
    DisableMemoReuse bool
    // contains filtered or unexported fields
}

Builder holds the context needed for building a memo structure from a SQL statement. Builder.Build() is the top-level function to perform this build process. As part of the build process, it performs name resolution and type checking on the expressions within Builder.stmt.

The memo structure is the primary data structure used for query optimization, so building the memo is the first step required to optimize a query. The memo is maintained inside Builder.factory, which exposes methods to construct expression groups inside the memo. Once the expression tree has been built, the builder calls SetRoot on the memo to indicate the root memo group, as well as the set of physical properties (e.g., row and column ordering) that at least one expression in the root group must satisfy.

A memo is essentially a compact representation of a forest of logically- equivalent query trees. Each tree is either a logical or a physical plan for executing the SQL query. After the build process is complete, the memo forest will contain exactly one tree: the logical query plan corresponding to the AST of the original SQL statement with some number of "normalization" transformations applied. Normalization transformations include heuristics such as predicate push-down that should always be applied. They do not include "exploration" transformations whose benefit must be evaluated with the optimizer's cost model (e.g., join reordering).

See factory.go and memo.go inside the opt/xform package for more details about the memo structure.

func New Uses

func New(
    ctx context.Context,
    semaCtx *tree.SemaContext,
    evalCtx *tree.EvalContext,
    catalog cat.Catalog,
    factory *norm.Factory,
    stmt tree.Statement,
) *Builder

New creates a new Builder structure initialized with the given parsed SQL statement.

func (*Builder) Build Uses

func (b *Builder) Build() (err error)

Build is the top-level function to build the memo structure inside Builder.factory from the parsed SQL statement in Builder.stmt. See the comment above the Builder type declaration for details.

If any subroutines panic with a non-runtime error as part of the build process, the panic is caught here and returned as an error.

type OpaqueType Uses

type OpaqueType int

OpaqueType indicates whether an opaque statement can mutate data or change schema.

const (
    // OpaqueReadOnly is used for statements that do not mutate state as part of
    // the transaction, and can be run in read-only transactions.
    OpaqueReadOnly OpaqueType = iota

    // OpaqueMutation is used for statements that mutate data and cannot be run as
    // part of read-only transactions.
    OpaqueMutation

    // OpaqueDDL is used for statements that change a schema and cannot be
    // executed following a mutation in the same transaction.
    OpaqueDDL
)

type ScalarBuilder Uses

type ScalarBuilder struct {
    Builder
    // contains filtered or unexported fields
}

ScalarBuilder is a specialized variant of Builder that can be used to create a scalar from a TypedExpr. This is used to build scalar expressions for testing. It is also used temporarily to interface with the old planning code.

TypedExprs can refer to columns in the current scope using IndexedVars (@1, @2, etc). When we build a scalar, we have to provide information about these columns.

func NewScalar Uses

func NewScalar(
    ctx context.Context, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, factory *norm.Factory,
) *ScalarBuilder

NewScalar creates a new ScalarBuilder. The columns in the metadata are accessible from scalar expressions via IndexedVars.

func (*ScalarBuilder) Build Uses

func (sb *ScalarBuilder) Build(expr tree.TypedExpr) (err error)

Build a memo structure from a TypedExpr: the root group represents a scalar expression equivalent to expr.

Package optbuilder imports 29 packages (graph) and is imported by 6 packages. Updated 2019-11-18. Refresh now. Tools for package owners.