-
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: move MergeResultTypes from physicalplan package and unexport
Release note: None
- Loading branch information
1 parent
0392845
commit 3fc46fb
Showing
6 changed files
with
113 additions
and
79 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,48 @@ | ||
// Copyright 2021 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 sql | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// mergeResultTypes reconciles the ResultTypes between two plans. It enforces | ||
// that each pair of ColumnTypes must either match or be null, in which case the | ||
// non-null type is used. This logic is necessary for cases like | ||
// SELECT NULL UNION SELECT 1. | ||
func mergeResultTypes(left, right []*types.T) ([]*types.T, error) { | ||
if len(left) != len(right) { | ||
return nil, errors.Errorf("ResultTypes length mismatch: %d and %d", len(left), len(right)) | ||
} | ||
merged := make([]*types.T, len(left)) | ||
for i := range left { | ||
leftType, rightType := left[i], right[i] | ||
if rightType.Family() == types.UnknownFamily { | ||
merged[i] = leftType | ||
} else if leftType.Family() == types.UnknownFamily { | ||
merged[i] = rightType | ||
} else if equivalentTypes(leftType, rightType) { | ||
merged[i] = leftType | ||
} else { | ||
return nil, errors.Errorf( | ||
"conflicting ColumnTypes: %s and %s", leftType.DebugString(), rightType.DebugString()) | ||
} | ||
} | ||
return merged, nil | ||
} | ||
|
||
// equivalentType checks whether a column type is equivalent to another for the | ||
// purpose of UNION. Precision, Width, Oid, etc. do not affect the merging of | ||
// values. | ||
func equivalentTypes(c, other *types.T) bool { | ||
return c.Equivalent(other) | ||
} |
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,62 @@ | ||
// Copyright 2021 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 sql | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
) | ||
|
||
func TestMergeResultTypes(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
empty := []*types.T{} | ||
null := []*types.T{types.Unknown} | ||
typeInt := []*types.T{types.Int} | ||
|
||
testData := []struct { | ||
name string | ||
left []*types.T | ||
right []*types.T | ||
expected *[]*types.T | ||
err bool | ||
}{ | ||
{"both empty", empty, empty, &empty, false}, | ||
{"left empty", empty, typeInt, nil, true}, | ||
{"right empty", typeInt, empty, nil, true}, | ||
{"both null", null, null, &null, false}, | ||
{"left null", null, typeInt, &typeInt, false}, | ||
{"right null", typeInt, null, &typeInt, false}, | ||
{"both int", typeInt, typeInt, &typeInt, false}, | ||
} | ||
for _, td := range testData { | ||
t.Run(td.name, func(t *testing.T) { | ||
result, err := mergeResultTypes(td.left, td.right) | ||
if td.err { | ||
if err == nil { | ||
t.Fatalf("expected error, got %+v", result) | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if !reflect.DeepEqual(*td.expected, result) { | ||
t.Fatalf("expected %+v, got %+v", *td.expected, result) | ||
} | ||
}) | ||
} | ||
} |
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