Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlbase: alias coltype.BigInt to VisibleType.BIGINT #20798

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/sql/coltypes/aliases.go
Expand Up @@ -21,6 +21,7 @@ var (
Bool = &TBool{Name: "BOOL"}
// Boolean is an immutable T instance.
Boolean = &TBool{Name: "BOOLEAN"}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I just noticed this was some surgical spacing. Carry on :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional
space-1

// Bit is an immutable T instance.
Bit = &TInt{Name: "BIT", Width: 1, ImplicitWidth: true}
// Int is an immutable T instance.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/table
Expand Up @@ -360,7 +360,7 @@ query TTBTT colnames
SHOW COLUMNS FROM test.alltypes
----
Field Type Null Default Indices
cbigint INT true NULL {}
cbigint BIGINT true NULL {}
cbigserial INT true unique_rowid() {}
cbit BIT(1) true NULL {}
cbit12 BIT(12) true NULL {}
Expand Down
33 changes: 19 additions & 14 deletions pkg/sql/sqlbase/table.go
Expand Up @@ -38,18 +38,23 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)

var nameToVisibleTypeMap = map[string]ColumnType_VisibleType{
"INTEGER": ColumnType_INTEGER,
"INT4": ColumnType_INTEGER,
"INT8": ColumnType_BIGINT,
"INT64": ColumnType_BIGINT,
"BIT": ColumnType_BIT,
"INT2": ColumnType_SMALLINT,
"SMALLINT": ColumnType_SMALLINT,
"FLOAT4": ColumnType_REAL,
"REAL": ColumnType_REAL,
"FLOAT8": ColumnType_DOUBLE_PRECISION,
"DOUBLE PRECISION": ColumnType_DOUBLE_PRECISION,
// aliasToVisibleTypeMap maps type aliases to ColumnType_VisibleType variants
// so that the alias is persisted. When adding new column type aliases or new
// VisibleType variants, consider adding to this mapping as well.
var aliasToVisibleTypeMap = map[string]ColumnType_VisibleType{
coltypes.Bit.Name: ColumnType_BIT,
coltypes.Int2.Name: ColumnType_SMALLINT,
coltypes.Int4.Name: ColumnType_INTEGER,
coltypes.Int8.Name: ColumnType_BIGINT,
coltypes.Int64.Name: ColumnType_BIGINT,
coltypes.Integer.Name: ColumnType_INTEGER,
coltypes.SmallInt.Name: ColumnType_SMALLINT,
coltypes.BigInt.Name: ColumnType_BIGINT,

coltypes.Real.Name: ColumnType_REAL,
coltypes.Float4.Name: ColumnType_REAL,
coltypes.Float8.Name: ColumnType_DOUBLE_PRECISION,
coltypes.Double.Name: ColumnType_DOUBLE_PRECISION,
}

func exprContainsVarsError(context string, Expr tree.Expr) error {
Expand Down Expand Up @@ -95,7 +100,7 @@ func populateTypeAttrs(
case *coltypes.TBool:
case *coltypes.TInt:
base.Width = int32(t.Width)
if val, present := nameToVisibleTypeMap[t.Name]; present {
if val, present := aliasToVisibleTypeMap[t.Name]; present {
base.VisibleType = val
}
case *coltypes.TFloat:
Expand All @@ -104,7 +109,7 @@ func populateTypeAttrs(
return ColumnType{}, errors.New("precision for type float must be at least 1 bit")
}
base.Precision = int32(t.Prec)
if val, present := nameToVisibleTypeMap[t.Name]; present {
if val, present := aliasToVisibleTypeMap[t.Name]; present {
base.VisibleType = val
}
case *coltypes.TDecimal:
Expand Down
30 changes: 30 additions & 0 deletions pkg/sql/table_test.go
Expand Up @@ -53,11 +53,41 @@ func TestMakeTableDescColumns(t *testing.T) {
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT},
true,
},
{
"INT2",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT, Width: 16, VisibleType: sqlbase.ColumnType_SMALLINT},
true,
},
{
"INT4",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT, Width: 32, VisibleType: sqlbase.ColumnType_INTEGER},
true,
},
{
"INT8",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT, VisibleType: sqlbase.ColumnType_BIGINT},
true,
},
{
"INT64",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT, VisibleType: sqlbase.ColumnType_BIGINT},
true,
},
{
"BIGINT",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_INT, VisibleType: sqlbase.ColumnType_BIGINT},
true,
},
{
"FLOAT(3)",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_FLOAT, Precision: 3},
true,
},
{
"DOUBLE PRECISION",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_FLOAT, VisibleType: sqlbase.ColumnType_DOUBLE_PRECISION},
true,
},
{
"DECIMAL(6,5)",
sqlbase.ColumnType{SemanticType: sqlbase.ColumnType_DECIMAL, Precision: 6, Width: 5},
Expand Down