MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / upgradeType

Method upgradeType

pkg/sql/types/types.go:2459–2646  ·  view source on GitHub ↗

upgradeType assumes its input was just unmarshaled from bytes that may have been serialized by any previous version of CRDB. It upgrades the object according to the requirements of the latest version by remapping fields and setting required values. This is necessary to preserve backwards- compatibil

()

Source from the content-addressed store, hash-verified

2457// setting required values. This is necessary to preserve backwards-
2458// compatibility with older formats (e.g. restoring database from old backup).
2459func (t *T) upgradeType() error {
2460 switch t.Family() {
2461 case IntFamily:
2462 // Check VisibleType field that was populated in previous versions.
2463 switch t.InternalType.VisibleType {
2464 case visibleSMALLINT:
2465 t.InternalType.Width = 16
2466 t.InternalType.Oid = oid.T_int2
2467 case visibleINTEGER:
2468 t.InternalType.Width = 32
2469 t.InternalType.Oid = oid.T_int4
2470 case visibleBIGINT:
2471 t.InternalType.Width = 64
2472 t.InternalType.Oid = oid.T_int8
2473 case visibleBIT, visibleNONE:
2474 // Pre-2.1 BIT was using IntFamily with arbitrary widths. Clamp them
2475 // to fixed/known widths. See #34161.
2476 switch t.Width() {
2477 case 16:
2478 t.InternalType.Oid = oid.T_int2
2479 case 32:
2480 t.InternalType.Oid = oid.T_int4
2481 default:
2482 // Assume INT8 if width is 0 or not valid.
2483 t.InternalType.Oid = oid.T_int8
2484 t.InternalType.Width = 64
2485 }
2486 default:
2487 return errors.AssertionFailedf("unexpected visible type: %d", t.InternalType.VisibleType)
2488 }
2489
2490 case FloatFamily:
2491 // Map visible REAL type to 32-bit width.
2492 switch t.InternalType.VisibleType {
2493 case visibleREAL:
2494 t.InternalType.Oid = oid.T_float4
2495 t.InternalType.Width = 32
2496 case visibleDOUBLE:
2497 t.InternalType.Oid = oid.T_float8
2498 t.InternalType.Width = 64
2499 case visibleNONE:
2500 switch t.Width() {
2501 case 32:
2502 t.InternalType.Oid = oid.T_float4
2503 case 64:
2504 t.InternalType.Oid = oid.T_float8
2505 default:
2506 // Pre-2.1 (before Width) there were 3 cases:
2507 // - VisibleType = DOUBLE PRECISION, Width = 0 -> now clearly FLOAT8
2508 // - VisibleType = NONE, Width = 0 -> now clearly FLOAT8
2509 // - VisibleType = NONE, Precision > 0 -> we need to derive the width.
2510 if t.Precision() >= 1 && t.Precision() <= 24 {
2511 t.InternalType.Oid = oid.T_float4
2512 t.InternalType.Width = 32
2513 } else {
2514 t.InternalType.Oid = oid.T_float8
2515 t.InternalType.Width = 64
2516 }

Callers 2

UnmarshalJSONPBMethod · 0.95
UnmarshalMethod · 0.95

Calls 5

FamilyMethod · 0.95
WidthMethod · 0.95
PrecisionMethod · 0.95
ArrayContentsMethod · 0.95
CalcArrayOidFunction · 0.85

Tested by

no test coverage detected