Skip to content

Commit 49adb2c

Browse files
authored
Merge pull request #2841 from dolthub/daylon/cast-fix
Fixes Issue #2549
2 parents 7b8b82d + 4ae5947 commit 49adb2c

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

server/cast/int32.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package cast
1616

1717
import (
18+
"strconv"
19+
"strings"
20+
1821
"github.com/cockroachdb/apd/v3"
1922
"github.com/cockroachdb/errors"
2023
"github.com/dolthub/go-mysql-server/sql"
@@ -34,6 +37,24 @@ func initInt32(builtInCasts map[id.Cast]casts.Cast) {
3437

3538
// int32Explicit registers all explicit casts. This comprises only the source types.
3639
func int32Explicit(builtInCasts map[id.Cast]casts.Cast) {
40+
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
41+
FromType: pgtypes.Int32,
42+
ToType: pgtypes.Bit,
43+
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
44+
width := 1
45+
if attTypMod := targetType.GetAttTypMod(); attTypMod != -1 {
46+
width = int(attTypMod)
47+
}
48+
bitStr := strconv.FormatInt(int64(val.(int32)), 2)
49+
if len(bitStr) > width {
50+
return bitStr[len(bitStr)-width:], nil
51+
} else if len(bitStr) < width {
52+
return strings.Repeat("0", width-len(bitStr)) + bitStr, nil
53+
} else {
54+
return bitStr, nil
55+
}
56+
},
57+
})
3758
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
3859
FromType: pgtypes.Int32,
3960
ToType: pgtypes.Bool,

testing/go/pgcatalog_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func TestPgCast(t *testing.T) {
484484
Assertions: []ScriptTestAssertion{
485485
{
486486
Query: `SELECT COUNT(*) FROM "pg_catalog"."pg_cast";`,
487-
Expected: []sql.Row{{117}},
487+
Expected: []sql.Row{{118}},
488488
},
489489
{ // Different cases and quoted, so it fails
490490
Query: `SELECT * FROM "PG_catalog"."pg_cast";`,
@@ -496,7 +496,7 @@ func TestPgCast(t *testing.T) {
496496
},
497497
{ // Different cases but non-quoted, so it works
498498
Query: "SELECT COUNT(*) FROM PG_catalog.pg_CAST ORDER BY oid;",
499-
Expected: []sql.Row{{117}},
499+
Expected: []sql.Row{{118}},
500500
},
501501
},
502502
},
@@ -5082,7 +5082,7 @@ ORDER BY 1;`,
50825082
{
50835083
// This is to make sure a full range scan works (we don't support a full range scan on the index yet)
50845084
Query: `SELECT relname from pg_catalog.pg_class order by oid limit 1;`,
5085-
Expected: []sql.Row{sql.Row{"pg_publication_namespace"}},
5085+
Expected: []sql.Row{{"pg_publication_namespace"}},
50865086
},
50875087
{
50885088
Query: `EXPLAIN SELECT c.oid
@@ -6045,9 +6045,9 @@ func TestSystemTablesInPgcatalog(t *testing.T) {
60456045
{245736992, "dolt_conflicts", 2200, "r"},
60466046
{1932298159, "dolt_constraint_violations", 2200, "r"},
60476047
{2357712556, "dolt_diff", 2200, "r"},
6048-
sql.Row{101228732, "dolt_diff_commit_hash_key", 2200, "i"},
6048+
{101228732, "dolt_diff_commit_hash_key", 2200, "i"},
60496049
{3491847678, "dolt_log", 2200, "r"},
6050-
sql.Row{2292720014, "dolt_log_commit_hash_key", 2200, "i"},
6050+
{2292720014, "dolt_log_commit_hash_key", 2200, "i"},
60516051
{604995978, "dolt_merge_status", 2200, "r"},
60526052
{887648921, "dolt_remote_branches", 2200, "r"},
60536053
{1471391189, "dolt_remote_branches_dolt_branches_name_idx_key", 2200, "i"},

testing/go/types_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ var typesTests = []ScriptTest{
9595
{2, pgtype.Bits{Bytes: []uint8{0x2b}, Len: 8, Valid: true}, pgtype.Bits{Bytes: []uint8{0x0}, Len: 3, Valid: true}},
9696
},
9797
},
98+
{
99+
Query: "SELECT 0::bit, 1::bit, 2::bit, 3::bit, 4::bit, 5::bit(2), 6::bit(2);",
100+
Expected: []sql.Row{{
101+
pgtype.Bits{Bytes: []uint8{0x00}, Len: 1, Valid: true},
102+
pgtype.Bits{Bytes: []uint8{0x80}, Len: 1, Valid: true},
103+
pgtype.Bits{Bytes: []uint8{0x00}, Len: 1, Valid: true},
104+
pgtype.Bits{Bytes: []uint8{0x80}, Len: 1, Valid: true},
105+
pgtype.Bits{Bytes: []uint8{0x00}, Len: 1, Valid: true},
106+
pgtype.Bits{Bytes: []uint8{0x40}, Len: 2, Valid: true},
107+
pgtype.Bits{Bytes: []uint8{0x80}, Len: 2, Valid: true},
108+
}},
109+
},
110+
{
111+
Query: "SELECT (-1)::bit, (-2)::bit, (-5)::bit(2), (-6::int4)::bit(2);",
112+
Expected: []sql.Row{{
113+
pgtype.Bits{Bytes: []uint8{0x80}, Len: 1, Valid: true},
114+
pgtype.Bits{Bytes: []uint8{0x00}, Len: 1, Valid: true},
115+
pgtype.Bits{Bytes: []uint8{0x40}, Len: 2, Valid: true},
116+
pgtype.Bits{Bytes: []uint8{0x80}, Len: 2, Valid: true},
117+
}},
118+
},
98119
{
99120
Query: "INSERT INTO t_bit VALUES (3, B'101', '111');",
100121
ExpectedErr: "bit string length 3 does not match type bit(8)",

0 commit comments

Comments
 (0)