-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: add buffer and scanBuffer nodes
Adds bufferNode that consumes its input, stores all the rows in a buffer, and then proceeds on passing the rows through. The buffer can be iterated over multiple times using scanBuffer node that is referencing a single bufferNode. Release note: None
- Loading branch information
1 parent
ba58a7e
commit 5c1d0f9
Showing
9 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2019 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 sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/rowcontainer" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase" | ||
) | ||
|
||
// bufferNode consumes its input one row at a time, stores it in the buffer, | ||
// and passes the row through. The buffered rows can be iterated over multiple | ||
// times. | ||
type bufferNode struct { | ||
plan planNode | ||
|
||
// TODO(yuzefovich): the buffer should probably be backed by disk. If so, the | ||
// comments about TempStorage suggest that it should be used by DistSQL | ||
// processors, but this node is local. | ||
bufferedRows *rowcontainer.RowContainer | ||
passThruNextRowIdx int | ||
} | ||
|
||
func (n *bufferNode) startExec(params runParams) error { | ||
n.bufferedRows = rowcontainer.NewRowContainer( | ||
params.EvalContext().Mon.MakeBoundAccount(), | ||
sqlbase.ColTypeInfoFromResCols(getPlanColumns(n.plan, false /* mut */)), | ||
0, /* rowCapacity */ | ||
) | ||
return nil | ||
} | ||
|
||
func (n *bufferNode) Next(params runParams) (bool, error) { | ||
if err := params.p.cancelChecker.Check(); err != nil { | ||
return false, err | ||
} | ||
ok, err := n.plan.Next(params) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !ok { | ||
return false, nil | ||
} | ||
if _, err = n.bufferedRows.AddRow(params.ctx, n.plan.Values()); err != nil { | ||
return false, err | ||
} | ||
n.passThruNextRowIdx++ | ||
return true, nil | ||
} | ||
|
||
func (n *bufferNode) Values() tree.Datums { | ||
return n.bufferedRows.At(n.passThruNextRowIdx - 1) | ||
} | ||
|
||
func (n *bufferNode) Close(ctx context.Context) { | ||
n.plan.Close(ctx) | ||
n.bufferedRows.Close(ctx) | ||
} | ||
|
||
// scanBufferNode behaves like an iterator into the bufferNode it is | ||
// referencing. The bufferNode can be iterated over multiple times | ||
// simultaneously, however, a new scanBufferNode is needed. | ||
type scanBufferNode struct { | ||
buffer *bufferNode | ||
|
||
nextRowIdx int | ||
} | ||
|
||
func (n *scanBufferNode) startExec(runParams) error { | ||
return nil | ||
} | ||
|
||
func (n *scanBufferNode) Next(runParams) (bool, error) { | ||
n.nextRowIdx++ | ||
return n.nextRowIdx <= n.buffer.bufferedRows.Len(), nil | ||
} | ||
|
||
func (n *scanBufferNode) Values() tree.Datums { | ||
return n.buffer.bufferedRows.At(n.nextRowIdx - 1) | ||
} | ||
|
||
func (n *scanBufferNode) Close(context.Context) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters