forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
107668: colbuilder: fix recently exposed type schema corruption r=yuzefovich a=yuzefovich This commit fixes recently exposed (or introduced, depending on how you look at this) type schema corruption that can occur when planning filter expressions. In particular, the bug was exactly as the comment deleted in 85fd4fb described: ``` // As an example, consider the following scenario in the context of // planFilterExpr method: // 1. r.ColumnTypes={types.Bool} with len=1 and cap=4 // 2. planSelectionOperators adds another types.Int column, so // filterColumnTypes={types.Bool, types.Int} with len=2 and cap=4 // Crucially, it uses exact same underlying array as r.ColumnTypes // uses. // 3. we project out second column, so r.ColumnTypes={types.Bool} // 4. later, we add another types.Float column, so // r.ColumnTypes={types.Bool, types.Float}, but there is enough // capacity in the array, so we simply overwrite the second slot // with the new type which corrupts filterColumnTypes to become // {types.Bool, types.Float}, and we can get into a runtime type // mismatch situation. ``` More concretely, in `planFilterExpr` we are using the passed-in type schema to append new types for the intermediate projection operators, and then we create a "simple project op" that removes those intermediate operators. If we later try to add more output columns, we will overwrite types captured by the intermediate projected away operators. The bug was that the simple project op did not create a new type schema like it's supposed to do. This is now fixed, and we now enforce that the simple project op in `colbuilder` package can only be created by the helper method that explicitly returns the updated type schema, hoping that this will encourage the callers to think about the type schema management to prevent such issues in the future. Fixes: cockroachdb#107615. Release note: None Co-authored-by: Yahor Yuzefovich <[email protected]>
- Loading branch information
Showing
5 changed files
with
138 additions
and
50 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package colbuilder | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase" | ||
"github.com/cockroachdb/cockroach/pkg/sql/colexecop" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
) | ||
|
||
// addProjection adds a simple projection on top of op according to projection | ||
// and returns the updated operator and type schema. | ||
// | ||
// Note that this method is the only place that's allowed to create a simple | ||
// project op in colbuilder package (enforced by the linter) in order to force | ||
// the caller to think about the type schema to prevent type schema corruption | ||
// issues like #47889 and #107615. | ||
func addProjection( | ||
op colexecop.Operator, typs []*types.T, projection []uint32, | ||
) (colexecop.Operator, []*types.T) { | ||
newTypes := make([]*types.T, len(projection)) | ||
for i, j := range projection { | ||
newTypes[i] = typs[j] | ||
} | ||
return colexecbase.NewSimpleProjectOp(op, len(typs), projection), newTypes | ||
} |
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