Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
117456: colinfo: optimize and refactor ResultColumns.NodeFormatter r=mgartner a=mgartner

#### colinfo: eliminate allocations in ResultColumns.NodeFormatter

This commit eliminates allocations in `ResultColumns.NodeFormatter` by
returning an already-allocated `*string` casted to a `*tree.Name`.

Release notes: None

#### colinfo: refactor and rename ResultColumns.NodeFormatter

This commit replaces `ResultColumns.NodeFormatter` with
`ResultColumns.Name` with the return type of `*tree.Name` instead of
`tree.NodeFormatter`. `scanNode.IndexedVarNodeFormatter` has been
updated to use this new `Name` method.

Epic: None

Release note: None


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Jan 8, 2024
2 parents 5243cc3 + bdcda99 commit b39e708
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pkg/sql/catalog/colinfo/result_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ func (r ResultColumns) TypesEqual(other ResultColumns) bool {
return true
}

// NodeFormatter returns a tree.NodeFormatter that, when formatted,
// represents the column at the input column index.
func (r ResultColumns) NodeFormatter(colIdx int) tree.NodeFormatter {
return &varFormatter{ColumnName: tree.Name(r[colIdx].Name)}
// Name returns the name of the column at the given index.
func (r ResultColumns) Name(idx int) *tree.Name {
return (*tree.Name)(&r[idx].Name)
}

// String formats result columns to a string.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (f *filterNode) IndexedVarResolvedType(idx int) *types.T {

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

func (f *filterNode) startExec(runParams) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/join_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (p *joinPredicate) IndexedVarResolvedType(idx int) *types.T {
// IndexedVarNodeFormatter implements the tree.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
if idx < p.numLeftCols {
return p.leftCols.NodeFormatter(idx)
return p.leftCols.Name(idx)
}
return p.rightCols.NodeFormatter(idx - p.numLeftCols)
return p.rightCols.Name(idx - p.numLeftCols)
}

// eval for joinPredicate runs the on condition across the columns that do
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *renderNode) IndexedVarResolvedType(idx int) *types.T {

// IndexedVarNodeFormatter implements the tree.IndexedVarContainer interface.
func (r *renderNode) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
return r.source.columns.NodeFormatter(idx)
return r.source.columns.Name(idx)
}

func (r *renderNode) startExec(runParams) error {
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,21 @@ func (p *planner) Scan() *scanNode {
// scanNode implements eval.IndexedVarContainer.
var _ eval.IndexedVarContainer = &scanNode{}

// IndexedVarEval implements the eval.IndexedVarContainer interface.
func (n *scanNode) IndexedVarEval(
ctx context.Context, idx int, e tree.ExprEvaluator,
) (tree.Datum, error) {
panic("scanNode can't be run in local mode")
}

// IndexedVarResolvedType implements the tree.IndexedVarContainer interface.
func (n *scanNode) IndexedVarResolvedType(idx int) *types.T {
return n.resultColumns[idx].Typ
}

// IndexedVarNodeFormatter implements the tree.IndexedVarContainer interface.
func (n *scanNode) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
return (*tree.Name)(&n.resultColumns[idx].Name)
return n.resultColumns.Name(idx)
}

func (n *scanNode) startExec(params runParams) error {
Expand Down

0 comments on commit b39e708

Please sign in to comment.