|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cast |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/cockroachdb/errors" |
| 19 | + "github.com/dolthub/go-mysql-server/sql" |
| 20 | + |
| 21 | + "github.com/dolthub/doltgresql/postgres/parser/sem/tree" |
| 22 | + |
| 23 | + "github.com/dolthub/doltgresql/server/functions/framework" |
| 24 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 25 | +) |
| 26 | + |
| 27 | +// initBit handles all casts that are built-in. This comprises only the "From" types. |
| 28 | +func initBit() { |
| 29 | + bitExplicit() |
| 30 | + bitImplicit() |
| 31 | +} |
| 32 | + |
| 33 | +// bitExplicit registers all explicit casts. This comprises only the "From" types. |
| 34 | +func bitExplicit() { |
| 35 | + framework.MustAddExplicitTypeCast(framework.TypeCast{ |
| 36 | + FromType: pgtypes.Bit, |
| 37 | + ToType: pgtypes.Int32, |
| 38 | + Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) { |
| 39 | + array, err := tree.ParseDBitArray(val.(string)) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + if array.BitLen() > 32 { |
| 44 | + return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range") |
| 45 | + } |
| 46 | + return int32(array.AsInt64(32)), nil |
| 47 | + }, |
| 48 | + }) |
| 49 | + framework.MustAddExplicitTypeCast(framework.TypeCast{ |
| 50 | + FromType: pgtypes.Bit, |
| 51 | + ToType: pgtypes.Int64, |
| 52 | + Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) { |
| 53 | + array, err := tree.ParseDBitArray(val.(string)) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + if array.BitLen() > 64 { |
| 58 | + return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range") |
| 59 | + } |
| 60 | + return array.AsInt64(64), nil |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// bitImplicit registers all implicit casts. This comprises only the "From" types. |
| 66 | +func bitImplicit() { |
| 67 | + framework.MustAddImplicitTypeCast(framework.TypeCast{ |
| 68 | + FromType: pgtypes.Bit, |
| 69 | + ToType: pgtypes.Bit, |
| 70 | + Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) { |
| 71 | + input := val.(string) |
| 72 | + array, err := tree.ParseDBitArray(input) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + expectedLength := pgtypes.GetCharLengthFromTypmod(targetType.GetAttTypMod()) |
| 77 | + if array.BitLen() != uint(expectedLength) { |
| 78 | + return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength) |
| 79 | + } |
| 80 | + return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil |
| 81 | + }, |
| 82 | + }) |
| 83 | + framework.MustAddImplicitTypeCast(framework.TypeCast{ |
| 84 | + FromType: pgtypes.Bit, |
| 85 | + ToType: pgtypes.VarBit, |
| 86 | + Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) { |
| 87 | + input := val.(string) |
| 88 | + array, err := tree.ParseDBitArray(input) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + atttypmod := targetType.GetAttTypMod() |
| 93 | + if atttypmod != -1 { |
| 94 | + maxLength := pgtypes.GetCharLengthFromTypmod(atttypmod) |
| 95 | + if int32(array.BitLen()) > maxLength { |
| 96 | + return nil, pgtypes.ErrVarBitLengthExceeded.New(maxLength) |
| 97 | + } |
| 98 | + } |
| 99 | + return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil |
| 100 | + }, |
| 101 | + }) |
| 102 | +} |
0 commit comments