Documentation ¶
Index ¶
- Variables
- func FixFieldIndexes(scope *Scope, schema sql.Schema, exp sql.Expression) (sql.Expression, error)
- func FixFieldIndexesForExpressions(node sql.Node, scope *Scope) (sql.Node, error)
- func FixFieldIndexesForTableNode(node sql.Node, scope *Scope) (sql.Node, error)
- func FixFieldIndexesOnExpressions(scope *Scope, schema sql.Schema, expressions ...sql.Expression) ([]sql.Expression, error)
- func OrderTriggers(triggers []*plan.CreateTrigger) (beforeTriggers []*plan.CreateTrigger, afterTriggers []*plan.CreateTrigger)
- type Analyzer
- func (a *Analyzer) Analyze(ctx *sql.Context, n sql.Node, scope *Scope) (sql.Node, error)
- func (a *Analyzer) Log(msg string, args ...interface{})
- func (a *Analyzer) LogDiff(prev, next sql.Node)
- func (a *Analyzer) LogNode(n sql.Node)
- func (a *Analyzer) PopDebugContext()
- func (a *Analyzer) PushDebugContext(msg string)
- type Batch
- type Builder
- func (ab *Builder) AddPostAnalyzeRule(name string, fn RuleFunc) *Builder
- func (ab *Builder) AddPostValidationRule(name string, fn RuleFunc) *Builder
- func (ab *Builder) AddPreAnalyzeRule(name string, fn RuleFunc) *Builder
- func (ab *Builder) AddPreValidationRule(name string, fn RuleFunc) *Builder
- func (ab *Builder) Build() *Analyzer
- func (ab *Builder) RemoveAfterAllRule(name string) *Builder
- func (ab *Builder) RemoveDefaultRule(name string) *Builder
- func (ab *Builder) RemoveOnceAfterRule(name string) *Builder
- func (ab *Builder) RemoveOnceBeforeRule(name string) *Builder
- func (ab *Builder) RemoveValidationRule(name string) *Builder
- func (ab *Builder) WithDebug() *Builder
- func (ab *Builder) WithParallelism(parallelism int) *Builder
- type JoinOrder
- type NameableNode
- type ProcedureCache
- type QueryHint
- type Releaser
- func (r *Releaser) Children() []sql.Node
- func (r *Releaser) Equal(n sql.Node) bool
- func (r *Releaser) Resolved() bool
- func (r *Releaser) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error)
- func (r *Releaser) Schema() sql.Schema
- func (r *Releaser) String() string
- func (r *Releaser) WithChildren(children ...sql.Node) (sql.Node, error)
- type Rule
- type RuleFunc
- type Scope
- type TableAliases
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFieldMissing is returned when the field is not on the schema. ErrFieldMissing = errors.NewKind("field %q is not on schema") // ErrOrderByColumnIndex is returned when in an order clause there is a // column that is unknown. ErrOrderByColumnIndex = errors.NewKind("unknown column %d in order by clause") )
var ( // ErrValidationResolved is returned when the plan can not be resolved. ErrValidationResolved = errors.NewKind("plan is not resolved because of node '%T'") // ErrValidationOrderBy is returned when the order by contains aggregation // expressions. ErrValidationOrderBy = errors.NewKind("OrderBy does not support aggregation expressions") // ErrValidationGroupBy is returned when the aggregation expression does not // appear in the grouping columns. ErrValidationGroupBy = errors.NewKind("GroupBy aggregate expression '%v' doesn't appear in the grouping columns") // ErrValidationSchemaSource is returned when there is any column source // that does not match the table name. ErrValidationSchemaSource = errors.NewKind("one or more schema sources are empty") // ErrProjectTuple is returned when there is a tuple of more than 1 column // inside a projection. ErrProjectTuple = errors.NewKind("selected field %d should have 1 column, but has %d") // ErrUnknownIndexColumns is returned when there are columns in the expr // to index that are unknown in the table. ErrUnknownIndexColumns = errors.NewKind("unknown columns to index for table %q: %s") // ErrCaseResultType is returned when one or more of the types of the values in // a case expression don't match. ErrCaseResultType = errors.NewKind( "expecting all case branches to return values of type %s, " + "but found value %q of type %s on %s", ) // ErrIntervalInvalidUse is returned when an interval expression is not // correctly used. ErrIntervalInvalidUse = errors.NewKind( "invalid use of an interval, which can only be used with DATE_ADD, " + "DATE_SUB and +/- operators to subtract from or add to a date", ) // ErrExplodeInvalidUse is returned when an EXPLODE function is used // outside a Project node. ErrExplodeInvalidUse = errors.NewKind( "using EXPLODE is not supported outside a Project node", ) // ErrSubqueryMultipleColumns is returned when an expression subquery returns // more than a single column. ErrSubqueryMultipleColumns = errors.NewKind( "subquery expressions can only return a single column", ) // ErrSubqueryFieldIndex is returned when an expression subquery references a field outside the range of the rows it // works on. ErrSubqueryFieldIndex = errors.NewKind( "subquery field index out of range for expression %s: only %d columns available", ) // ErrUnionSchemasMatch is returned when both sides of a UNION do not // have the same schema. ErrUnionSchemasMatch = errors.NewKind( "the schema of the left side of union does not match the right side, expected %s to match %s", ) )
var DefaultRules = []Rule{
{"resolve_natural_joins", resolveNaturalJoins},
{"resolve_orderby_literals", resolveOrderByLiterals},
{"resolve_functions", resolveFunctions},
{"flatten_table_aliases", flattenTableAliases},
{"pushdown_sort", pushdownSort},
{"pushdown_groupby_aliases", pushdownGroupByAliases},
{"qualify_columns", qualifyColumns},
{"resolve_columns", resolveColumns},
{"validate_check_constraint", validateCreateCheck},
{"resolve_bareword_set_variables", resolveBarewordSetVariables},
{"resolve_database", resolveDatabase},
{"expand_stars", expandStars},
{"resolve_having", resolveHaving},
{"merge_union_schemas", mergeUnionSchemas},
{"flatten_aggregation_exprs", flattenAggregationExpressions},
{"reorder_projection", reorderProjection},
{"resolve_subquery_exprs", resolveSubqueryExpressions},
{"move_join_conds_to_filter", moveJoinConditionsToFilter},
{"eval_filter", evalFilter},
{"optimize_distinct", optimizeDistinct},
}
DefaultRules to apply when analyzing nodes.
var DefaultValidationRules = []Rule{
{validateResolvedRule, validateIsResolved},
{validateOrderByRule, validateOrderBy},
{validateGroupByRule, validateGroupBy},
{validateSchemaSourceRule, validateSchemaSource},
{validateProjectTuplesRule, validateProjectTuples},
{validateIndexCreationRule, validateIndexCreation},
{validateCaseResultTypesRule, validateCaseResultTypes},
{validateIntervalUsageRule, validateIntervalUsage},
{validateExplodeUsageRule, validateExplodeUsage},
{validateSubqueryColumnsRule, validateSubqueryColumns},
{validateUnionSchemasMatchRule, validateUnionSchemasMatch},
}
DefaultValidationRules to apply while analyzing nodes.
var ErrInAnalysis = errors.NewKind("error in analysis: %s")
ErrInAnalysis is thrown for generic analyzer errors
var ErrInvalidNodeType = errors.NewKind("%s: invalid node of type: %T")
ErrInvalidNodeType is thrown when the analyzer can't handle a particular kind of node type
var ErrMaxAnalysisIters = errors.NewKind("exceeded max analysis iterations (%d)")
ErrMaxAnalysisIters is thrown when the analysis iterations are exceeded
var ( // ErrUnionSchemasDifferentLength is returned when the two sides of a // UNION do not have the same number of columns in their schemas. ErrUnionSchemasDifferentLength = errors.NewKind( "cannot union two queries whose schemas are different lengths; left has %d column(s) right has %d column(s).", ) )
var OnceAfterAll = []Rule{
{"track_process", trackProcess},
{"parallelize", parallelize},
{"clear_warnings", clearWarnings},
}
OnceAfterAll contains the rules to be applied just once after all other rules have been applied.
var OnceAfterDefault = []Rule{
{"load_triggers", loadTriggers},
{"process_truncate", processTruncate},
{"resolve_column_defaults", resolveColumnDefaults},
{"resolve_generators", resolveGenerators},
{"remove_unnecessary_converts", removeUnnecessaryConverts},
{"assign_catalog", assignCatalog},
{"prune_columns", pruneColumns},
{"optimize_joins", constructJoinPlan},
{"pushdown_filters", pushdownFilters},
{"subquery_indexes", applyIndexesFromOuterScope},
{"in_subquery_indexes", applyIndexesForSubqueryComparisons},
{"pushdown_projections", pushdownProjections},
{"set_join_scope_len", setJoinScopeLen},
{"erase_projection", eraseProjection},
{"resolve_subquery_exprs", resolveSubqueryExpressions},
{"cache_subquery_results", cacheSubqueryResults},
{"cache_subquery_aliases_in_joins", cacheSubqueryAlisesInJoins},
{"resolve_insert_rows", resolveInsertRows},
{"apply_triggers", applyTriggers},
{"apply_procedures", applyProcedures},
{"apply_row_update_accumulators", applyUpdateAccumulators},
}
OnceAfterDefault contains the rules to be applied just once after the DefaultRules.
var OnceBeforeDefault = []Rule{
{"load_stored_procedures", loadStoredProcedures},
{"resolve_views", resolveViews},
{"resolve_common_table_expressions", resolveCommonTableExpressions},
{"resolve_tables", resolveTables},
{"load_check_constraints", loadChecks},
{"resolve_set_variables", resolveSetVariables},
{"resolve_create_like", resolveCreateLike},
{"resolve_subqueries", resolveSubqueries},
{"resolve_unions", resolveUnions},
{"resolve_describe_query", resolveDescribeQuery},
{"check_unique_table_names", checkUniqueTableNames},
{"resolve_declarations", resolveDeclarations},
{"validate_create_trigger", validateCreateTrigger},
{"validate_create_procedure", validateCreateProcedure},
{"assign_info_schema", assignInfoSchema},
}
OnceBeforeDefault contains the rules to be applied just once before the DefaultRules.
var ( // ParallelQueryCounter describes a metric that accumulates // number of parallel queries monotonically. ParallelQueryCounter = discard.NewCounter() )
Functions ¶
func FixFieldIndexes ¶
func FixFieldIndexes(scope *Scope, schema sql.Schema, exp sql.Expression) (sql.Expression, error)
FixFieldIndexes transforms the given expression by correcting the indexes of columns in GetField expressions, according to the schema given. Used when combining multiple tables together into a single join result, or when otherwise changing / combining schemas in the node tree.
func FixFieldIndexesForExpressions ¶
Transforms the expressions in the Node given, fixing the field indexes.
func FixFieldIndexesForTableNode ¶
Transforms the expressions in the Node given, fixing the field indexes. This is useful for Table nodes that have expressions but no children.
func FixFieldIndexesOnExpressions ¶
func FixFieldIndexesOnExpressions(scope *Scope, schema sql.Schema, expressions ...sql.Expression) ([]sql.Expression, error)
FixFieldIndexesOnExpressions executes FixFieldIndexes on a list of exprs.
func OrderTriggers ¶
func OrderTriggers(triggers []*plan.CreateTrigger) (beforeTriggers []*plan.CreateTrigger, afterTriggers []*plan.CreateTrigger)
Types ¶
type Analyzer ¶
type Analyzer struct { // Whether to log various debugging messages Debug bool // Whether to output the query plan at each step of the analyzer Verbose bool Parallelism int // Batches of Rules to apply. Batches []*Batch // Catalog of databases and registered functions. Catalog *sql.Catalog // ProcedureCache is a cache of stored procedures. ProcedureCache *ProcedureCache // contains filtered or unexported fields }
Analyzer analyzes nodes of the execution plan and applies rules and validations to them.
func NewDefault ¶
NewDefault creates a default Analyzer instance with all default Rules and configuration. To add custom rules, the easiest way is use the Builder.
func (*Analyzer) Analyze ¶
Analyze applies the transformation rules to the node given. In the case of an error, the last successfully transformed node is returned along with the error.
func (*Analyzer) Log ¶
Log prints an INFO message to stdout with the given message and args if the analyzer is in debug mode.
func (*Analyzer) LogDiff ¶
LogDiff logs the diff between the query plans after a transformation rules has been applied. Only can print a diff when the string representations of the nodes differ, which isn't always the case.
func (*Analyzer) PopDebugContext ¶
func (a *Analyzer) PopDebugContext()
PopDebugContext pops a context message off the context stack.
func (*Analyzer) PushDebugContext ¶
PushDebugContext pushes the given context string onto the context stack, to use when logging debug messages.
type Batch ¶
Batch executes a set of rules a specific number of times. When this number of times is reached, the actual node and ErrMaxAnalysisIters is returned.
func (*Batch) Eval ¶
Eval executes the rules of the batch. On any error, the partially transformed node is returned along with the error. If the batch's max number of iterations is reached without achieving stabilization (batch evaluation no longer changes the node), then this method returns ErrMaxAnalysisIters.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides an easy way to generate Analyzer with custom rules and options.
func NewBuilder ¶
NewBuilder creates a new Builder from a specific catalog. This builder allow us add custom Rules and modify some internal properties.
func (*Builder) AddPostAnalyzeRule ¶
AddPostAnalyzeRule adds a new rule to the analyzer after standard analyzer rules.
func (*Builder) AddPostValidationRule ¶
AddPostValidationRule adds a new rule to the analyzer after standard validation rules.
func (*Builder) AddPreAnalyzeRule ¶
AddPreAnalyzeRule adds a new rule to the analyze before the standard analyzer rules.
func (*Builder) AddPreValidationRule ¶
AddPreValidationRule adds a new rule to the analyzer before standard validation rules.
func (*Builder) RemoveAfterAllRule ¶
RemoveAfterAllRule removes a default rule from the analyzer which would occur after all other rules
func (*Builder) RemoveDefaultRule ¶
RemoveDefaultRule removes a default rule from the analyzer that is executed as part of the analysis
func (*Builder) RemoveOnceAfterRule ¶
RemoveOnceAfterRule removes a default rule from the analyzer which would occur just once after the default analysis
func (*Builder) RemoveOnceBeforeRule ¶
RemoveOnceBeforeRule removes a default rule from the analyzer which would occur before other rules
func (*Builder) RemoveValidationRule ¶
RemoveValidationRule removes a default rule from the analyzer which would occur as part of the validation rules
func (*Builder) WithParallelism ¶
WithParallelism sets the parallelism level on the analyzer.
type JoinOrder ¶ added in v0.9.0
type JoinOrder struct {
// contains filtered or unexported fields
}
type ProcedureCache ¶ added in v0.9.0
type ProcedureCache struct { IsPopulating bool // contains filtered or unexported fields }
ProcedureCache contains all of the stored procedures for each database.
func NewProcedureCache ¶ added in v0.9.0
func NewProcedureCache() *ProcedureCache
NewProcedureCache returns a *ProcedureCache.
func (*ProcedureCache) AllForDatabase ¶ added in v0.9.0
func (pc *ProcedureCache) AllForDatabase(dbName string) []*plan.Procedure
AllForDatabase returns all of the stored procedures for the given database, sorted by name ascending. The database name is case-insensitive.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope of the analysis being performed, used when analyzing subqueries to give such analysis access to outer scope.
func (*Scope) InnerToOuter ¶
InnerToOuter returns the scope Nodes in order of innermost scope to outermost scope. When using these nodes for analysis, always inspect the children of the nodes, rather than the nodes themselves. The children define the schema of the rows being processed by the scope node itself.
func (*Scope) OuterToInner ¶
OuterToInner returns the scope nodes in order of outermost scope to innermost scope. When using these nodes for analysis, always inspect the children of the nodes, rather than the nodes themselves. The children define the schema of the rows being processed by the scope node itself.
type TableAliases ¶
Source Files ¶
- aggregations.go
- aliases.go
- analyzer.go
- apply_indexes_for_subquery_comparisons.go
- apply_indexes_from_outer_scope.go
- apply_update_accumulators.go
- assign_catalog.go
- assign_info_schema.go
- batch.go
- check_constraints.go
- declare.go
- describe.go
- expand_stars.go
- filters.go
- fix_field_indexes.go
- index_analyzer.go
- indexed_joins.go
- indexes.go
- inserts.go
- join_search.go
- load_triggers.go
- optimization_rules.go
- parallelize.go
- procedure_cache.go
- process.go
- process_truncate.go
- prune_columns.go
- pushdown.go
- releaser.go
- reorder_projections.go
- resolve_column_defaults.go
- resolve_columns.go
- resolve_create_like.go
- resolve_ctes.go
- resolve_database.go
- resolve_functions.go
- resolve_generators.go
- resolve_having.go
- resolve_natural_joins.go
- resolve_orderby.go
- resolve_subqueries.go
- resolve_tables.go
- resolve_unions.go
- resolve_variables.go
- resolve_views.go
- rules.go
- scope.go
- stored_procedures.go
- tables.go
- triggers.go
- validation_rules.go
- warnings.go