Skip to content

Commit

Permalink
sql: refactor usage of sqlbase.DataSourceInfo
Browse files Browse the repository at this point in the history
1. Remove MultiSourceInfo, as it never needs to be used with more than one
   DataSourceInfo.

2. Remove support for multiple source aliases in DataSourceInfo, since
   more than one is never used.

3. Simplify several usages of DataSourceInfo to just use ResultColumns
   instead, in cases where the source alias is never needed.

Release note: none
  • Loading branch information
andy-kimball committed Dec 4, 2019
1 parent 36b9462 commit fe6a1c4
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 383 deletions.
6 changes: 3 additions & 3 deletions pkg/sql/analyze_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import (
func (p *planner) analyzeExpr(
ctx context.Context,
raw tree.Expr,
sources sqlbase.MultiSourceInfo,
source *sqlbase.DataSourceInfo,
iVarHelper tree.IndexedVarHelper,
expectedType *types.T,
requireType bool,
typingContext string,
) (tree.TypedExpr, error) {
// Perform optional name resolution.
resolved := raw
if sources != nil {
if source != nil {
var err error
resolved, _, err = p.resolveNames(raw, sources, iVarHelper)
resolved, _, err = p.resolveNames(raw, source, iVarHelper)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/apply_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func newApplyJoinNode(
rightProps: rightProps,
rightCols: rightCols,
right: right,
columns: pred.info.SourceColumns,
columns: pred.cols,
}, nil
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,9 @@ func makeTableDescIfAs(
}

func dequalifyColumnRefs(
ctx context.Context, sources sqlbase.MultiSourceInfo, expr tree.Expr,
ctx context.Context, source *sqlbase.DataSourceInfo, expr tree.Expr,
) (tree.Expr, error) {
resolver := sqlbase.ColumnResolver{Sources: sources}
resolver := sqlbase.ColumnResolver{Source: source}
return tree.SimpleVisit(
expr,
func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
Expand All @@ -987,9 +987,8 @@ func dequalifyColumnRefs(
if err != nil {
return false, nil, err
}
srcIdx := resolver.ResolverState.SrcIdx
colIdx := resolver.ResolverState.ColIdx
col := sources[srcIdx].SourceColumns[colIdx]
col := source.SourceColumns[colIdx]
return false, &tree.ColumnItem{ColumnName: tree.Name(col.Name)}, nil
}
}
Expand Down Expand Up @@ -1086,7 +1085,6 @@ func MakeTableDesc(
sourceInfo := sqlbase.NewSourceInfoForSingleTable(
n.Table, sqlbase.ResultColumnsFromColDescs(desc.Columns),
)
sources := sqlbase.MultiSourceInfo{sourceInfo}

for i := range desc.Columns {
col := &desc.Columns[i]
Expand All @@ -1096,7 +1094,7 @@ func MakeTableDesc(
return desc, err
}

expr, err = dequalifyColumnRefs(ctx, sources, expr)
expr, err = dequalifyColumnRefs(ctx, sourceInfo, expr)
if err != nil {
return desc, err
}
Expand Down Expand Up @@ -1603,9 +1601,8 @@ func MakeCheckConstraint(
sourceInfo := sqlbase.NewSourceInfoForSingleTable(
tableName, sqlbase.ResultColumnsFromColDescs(desc.TableDesc().AllNonDropColumns()),
)
sources := sqlbase.MultiSourceInfo{sourceInfo}

expr, err = dequalifyColumnRefs(ctx, sources, d.Expr)
expr, err = dequalifyColumnRefs(ctx, sourceInfo, d.Expr)
if err != nil {
return nil, err
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/sql/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ package sql

import "github.com/cockroachdb/cockroach/pkg/sql/sqlbase"

// For more detailed documentation on DataSourceInfos, see
// sqlbase/data_source.go.

// planDataSource contains the data source information for data
// produced by a planNode.
type planDataSource struct {
// info which describe the columns.
info *sqlbase.DataSourceInfo
// columns gives the result columns (always anonymous source).
columns sqlbase.ResultColumns

// plan which can be used to retrieve the data.
plan planNode
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func (f *filterNode) IndexedVarEval(idx int, ctx *tree.EvalContext) (tree.Datum,

// IndexedVarResolvedType implements the tree.IndexedVarContainer interface.
func (f *filterNode) IndexedVarResolvedType(idx int) *types.T {
return f.source.info.SourceColumns[idx].Typ
return f.source.columns[idx].Typ
}

// IndexedVarNodeFormatter implements the tree.IndexedVarContainer interface.
func (f *filterNode) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
return f.source.info.NodeFormatter(idx)
return f.source.columns.NodeFormatter(idx)
}

func (f *filterNode) startExec(runParams) error {
Expand Down
23 changes: 1 addition & 22 deletions pkg/sql/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *planner) makeJoinNode(
right: right,
joinType: pred.joinType,
pred: pred,
columns: pred.info.SourceColumns,
columns: pred.cols,
}
return n
}
Expand All @@ -74,27 +74,6 @@ func (n *joinNode) Close(ctx context.Context) {
n.left.plan.Close(ctx)
}

// commonColumns returns the names of columns common on the
// right and left sides, for use by NATURAL JOIN.
func commonColumns(left, right *sqlbase.DataSourceInfo) tree.NameList {
var res tree.NameList
for _, cLeft := range left.SourceColumns {
if cLeft.Hidden {
continue
}
for _, cRight := range right.SourceColumns {
if cRight.Hidden {
continue
}

if cLeft.Name == cRight.Name {
res = append(res, tree.Name(cLeft.Name))
}
}
}
return res
}

// interleavedNodes returns the ancestor on which an interleaved join is
// defined as well as the descendants of this ancestor which participate in
// the join. One of the left/right scan nodes is the ancestor and the other
Expand Down
105 changes: 17 additions & 88 deletions pkg/sql/join_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type joinPredicate struct {
// equality columns).
onCond tree.TypedExpr

leftInfo *sqlbase.DataSourceInfo
rightInfo *sqlbase.DataSourceInfo
info *sqlbase.DataSourceInfo
leftCols sqlbase.ResultColumns
rightCols sqlbase.ResultColumns
cols sqlbase.ResultColumns

// If set, the left equality columns form a key in the left input. Used as a
// hint for optimizing execution.
Expand All @@ -70,7 +70,7 @@ type joinPredicate struct {
// makePredicate constructs a joinPredicate object for joins. The join condition
// includes equality between usingColumns.
func makePredicate(
joinType sqlbase.JoinType, left, right *sqlbase.DataSourceInfo, usingColumns []usingColumn,
joinType sqlbase.JoinType, left, right sqlbase.ResultColumns,
) (*joinPredicate, error) {
// For anti and semi joins, the right columns are omitted from the output (but
// they must be available internally for the ON condition evaluation).
Expand All @@ -80,97 +80,26 @@ func makePredicate(
// The structure of the join data source results is like this:
// - all the left columns,
// - then all the right columns (except for anti/semi join).
columns := make(sqlbase.ResultColumns, 0, len(left.SourceColumns)+len(right.SourceColumns))
columns = append(columns, left.SourceColumns...)
columns := make(sqlbase.ResultColumns, 0, len(left)+len(right))
columns = append(columns, left...)
if !omitRightColumns {
columns = append(columns, right.SourceColumns...)
}

// Compute the mappings from table aliases to column sets from
// both sides into a new alias-columnset mapping for the result
// rows. We need to be extra careful about the aliases
// for the anonymous table, which needs to be merged.
aliases := make(sqlbase.SourceAliases, 0, len(left.SourceAliases)+len(right.SourceAliases))

var anonymousCols util.FastIntSet

collectAliases := func(sourceAliases sqlbase.SourceAliases, offset int) {
for _, alias := range sourceAliases {
newSet := alias.ColumnSet.Shift(offset)
if alias.Name == sqlbase.AnonymousTable {
anonymousCols.UnionWith(newSet)
} else {
aliases = append(aliases, sqlbase.SourceAlias{Name: alias.Name, ColumnSet: newSet})
}
}
}
collectAliases(left.SourceAliases, 0)
if !omitRightColumns {
collectAliases(right.SourceAliases, len(left.SourceColumns))
}
if !anonymousCols.Empty() {
aliases = append(aliases, sqlbase.SourceAlias{
Name: sqlbase.AnonymousTable,
ColumnSet: anonymousCols,
})
columns = append(columns, right...)
}

pred := &joinPredicate{
joinType: joinType,
numLeftCols: len(left.SourceColumns),
numRightCols: len(right.SourceColumns),
leftInfo: left,
rightInfo: right,
info: &sqlbase.DataSourceInfo{
SourceColumns: columns,
SourceAliases: aliases,
},
numLeftCols: len(left),
numRightCols: len(right),
leftCols: left,
rightCols: right,
cols: columns,
}
// We must initialize the indexed var helper in all cases, even when
// there is no on condition, so that getNeededColumns() does not get
// confused.
pred.curRow = make(tree.Datums, len(left.SourceColumns)+len(right.SourceColumns))
pred.curRow = make(tree.Datums, len(left)+len(right))
pred.iVarHelper = tree.MakeIndexedVarHelper(pred, len(pred.curRow))

// Prepare the arrays populated below.
pred.leftEqualityIndices = make([]int, 0, len(usingColumns))
pred.rightEqualityIndices = make([]int, 0, len(usingColumns))
colNames := make(tree.NameList, 0, len(usingColumns))

// Find out which columns are involved in EqualityPredicate.
for i := range usingColumns {
uc := &usingColumns[i]

if !uc.leftType.Equivalent(uc.rightType) {
// Issue #22519: we can't have two equality columns of mismatched types
// because the hash-joiner assumes the encodings are the same. Move the
// equality to the ON condition.

// First, check if the comparison would even be valid.
_, found := tree.FindEqualComparisonFunction(uc.leftType, uc.rightType)
if !found {
return nil, pgerror.Newf(pgcode.DatatypeMismatch,
"JOIN/USING types %s for left and %s for right cannot be matched for column %s",
uc.leftType, uc.rightType, uc.name,
)
}
expr := tree.NewTypedComparisonExpr(
tree.EQ,
pred.iVarHelper.IndexedVar(uc.leftIdx),
pred.iVarHelper.IndexedVar(uc.rightIdx+pred.numLeftCols),
)
pred.onCond = mergeConj(pred.onCond, expr)
continue
}

// Remember the indices.
pred.leftEqualityIndices = append(pred.leftEqualityIndices, uc.leftIdx)
pred.rightEqualityIndices = append(pred.rightEqualityIndices, uc.rightIdx)
colNames = append(colNames, uc.name)
}
pred.leftColNames = colNames
pred.rightColNames = colNames

return pred, nil
}

Expand All @@ -182,17 +111,17 @@ func (p *joinPredicate) IndexedVarEval(idx int, ctx *tree.EvalContext) (tree.Dat
// IndexedVarResolvedType implements the tree.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarResolvedType(idx int) *types.T {
if idx < p.numLeftCols {
return p.leftInfo.SourceColumns[idx].Typ
return p.leftCols[idx].Typ
}
return p.rightInfo.SourceColumns[idx-p.numLeftCols].Typ
return p.rightCols[idx-p.numLeftCols].Typ
}

// IndexedVarNodeFormatter implements the tree.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
if idx < p.numLeftCols {
return p.leftInfo.NodeFormatter(idx)
return p.leftCols.NodeFormatter(idx)
}
return p.rightInfo.NodeFormatter(idx - p.numLeftCols)
return p.rightCols.NodeFormatter(idx - p.numLeftCols)
}

// eval for joinPredicate runs the on condition across the columns that do
Expand Down
Loading

0 comments on commit fe6a1c4

Please sign in to comment.