Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion server/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,12 @@ func nodeExpr(ctx *Context, node tree.Expr) (vitess.Expr, error) {
case *tree.DArray:
return nil, errors.Errorf("the statement is not yet supported")
case *tree.DBitArray:
return nil, errors.Errorf("the statement is not yet supported")
// We convert bitarray to string representation for engine representation purposes so that we don't have to
// represent another fundamental golang type. This means our representation in memory is more verbose.
bitStr := tree.AsStringWithFlags(node, tree.FmtPgwireText)
return vitess.InjectedExpr{
Expression: pgexprs.NewUnsafeLiteral(bitStr, pgtypes.Bit),
}, nil
case *tree.DBool:
return vitess.InjectedExpr{
Expression: pgexprs.NewRawLiteralBool(bool(*node)),
Expand Down
32 changes: 29 additions & 3 deletions server/ast/resolvable_type_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,38 @@ func nodeResolvableTypeReference(ctx *Context, typ tree.ResolvableTypeReference)
resolvedType = pgtypes.VarChar
} else {
resolvedType, err = pgtypes.NewVarCharType(int32(width))
}
if err != nil {
return nil, nil, err
if err != nil {
return nil, nil, err
}
}
case oid.T_xid:
resolvedType = pgtypes.Xid
case oid.T_bit:
width := uint32(columnType.Width())
if width > pgtypes.StringMaxLength {
return nil, nil, errors.Errorf("length for type bit cannot exceed %d", pgtypes.StringMaxLength)
} else if width == 0 {
// TODO: need to differentiate between definitions 'bit' (valid) and 'bit(0)' (invalid)
resolvedType = pgtypes.Bit
} else {
resolvedType, err = pgtypes.NewBitType(int32(width))
if err != nil {
return nil, nil, err
}
}
case oid.T_varbit:
width := uint32(columnType.Width())
if width > pgtypes.StringMaxLength {
return nil, nil, errors.Errorf("length for type varbit cannot exceed %d", pgtypes.StringMaxLength)
} else if width == 0 {
// TODO: need to differentiate between definitions 'varbit' (valid) and 'varbit(0)' (invalid)
resolvedType = pgtypes.VarBit
} else {
resolvedType, err = pgtypes.NewVarBitType(int32(width))
if err != nil {
return nil, nil, err
}
}
default:
return nil, nil, errors.Errorf("unknown type with oid: %d", uint32(columnType.Oid()))
}
Expand Down
102 changes: 102 additions & 0 deletions server/cast/bit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2026 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cast

import (
"github.com/cockroachdb/errors"
"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/doltgresql/postgres/parser/sem/tree"

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initBit handles all casts that are built-in. This comprises only the "From" types.
func initBit() {
bitExplicit()
bitImplicit()
}

// bitExplicit registers all explicit casts. This comprises only the "From" types.
func bitExplicit() {
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bit,
ToType: pgtypes.Int32,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
array, err := tree.ParseDBitArray(val.(string))
if err != nil {
return nil, err
}
if array.BitLen() > 32 {
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
}
return int32(array.AsInt64(32)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bit,
ToType: pgtypes.Int64,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
array, err := tree.ParseDBitArray(val.(string))
if err != nil {
return nil, err
}
if array.BitLen() > 64 {
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
}
return array.AsInt64(64), nil
},
})
}

// bitImplicit registers all implicit casts. This comprises only the "From" types.
func bitImplicit() {
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bit,
ToType: pgtypes.Bit,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
input := val.(string)
array, err := tree.ParseDBitArray(input)
if err != nil {
return nil, err
}
expectedLength := pgtypes.GetCharLengthFromTypmod(targetType.GetAttTypMod())
if array.BitLen() != uint(expectedLength) {
return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength)
}
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bit,
ToType: pgtypes.VarBit,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
input := val.(string)
array, err := tree.ParseDBitArray(input)
if err != nil {
return nil, err
}
atttypmod := targetType.GetAttTypMod()
if atttypmod != -1 {
maxLength := pgtypes.GetCharLengthFromTypmod(atttypmod)
if int32(array.BitLen()) > maxLength {
return nil, pgtypes.ErrVarBitLengthExceeded.New(maxLength)
}
}
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
},
})
}
2 changes: 2 additions & 0 deletions server/cast/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

// Init initializes all casts in this package.
func Init() {
initBit()
initBool()
initChar()
initDate()
Expand All @@ -44,6 +45,7 @@ func Init() {
initTimestamp()
initTimestampTZ()
initTimeTZ()
initVarBit()
initVarChar()

// This is a hack to get around import cycles. The types package needs these references for type conversions in
Expand Down
68 changes: 68 additions & 0 deletions server/cast/varbit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2026 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cast

import (
"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/doltgresql/postgres/parser/sem/tree"

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initVarBit handles all casts that are built-in. This comprises only the "From" types.
func initVarBit() {
varBitImplicit()
}

// varBitImplicit registers all implicit casts. This comprises only the "From" types.
func varBitImplicit() {
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.VarBit,
ToType: pgtypes.Bit,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
input := val.(string)
array, err := tree.ParseDBitArray(input)
if err != nil {
return nil, err
}
expectedLength := pgtypes.GetCharLengthFromTypmod(targetType.GetAttTypMod())
if array.BitLen() != uint(expectedLength) {
return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength)
}
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.VarBit,
ToType: pgtypes.VarBit,
Function: func(ctx *sql.Context, val any, targetType *pgtypes.DoltgresType) (any, error) {
input := val.(string)
array, err := tree.ParseDBitArray(input)
if err != nil {
return nil, err
}
atttypmod := targetType.GetAttTypMod()
if atttypmod != -1 {
maxLength := pgtypes.GetCharLengthFromTypmod(atttypmod)
if int32(array.BitLen()) > maxLength {
return nil, pgtypes.ErrVarBitLengthExceeded.New(maxLength)
}
}
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
},
})
}
32 changes: 32 additions & 0 deletions server/functions/binary/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

// initBinaryEqual registers the functions to the catalog.
func initBinaryEqual() {
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, biteq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, booleq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, bpchareq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, byteaeq)
Expand Down Expand Up @@ -72,6 +73,7 @@ func initBinaryEqual() {
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, timestamptz_eq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, timetz_eq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, uuid_eq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, varbiteq)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, xideqint4)
framework.RegisterBinaryFunction(framework.Operator_BinaryEqual, xideq)
}
Expand Down Expand Up @@ -523,6 +525,36 @@ func record_eq_callable(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1 any,
return compare.CompareRecords(ctx, framework.Operator_BinaryEqual, val1, val2)
}

// varbiteq represents the PostgreSQL function of the same name, taking the same parameters.
var varbiteq = framework.Function2{
Name: "varbiteq",
Return: pgtypes.Bool,
Parameters: [2]*pgtypes.DoltgresType{pgtypes.VarBit, pgtypes.VarBit},
Strict: true,
Callable: varbit_eq_callable,
}

// biteq represents the PostgreSQL function of the same name, taking the same parameters.
var biteq = framework.Function2{
Name: "biteq",
Return: pgtypes.Bool,
Parameters: [2]*pgtypes.DoltgresType{pgtypes.Bit, pgtypes.Bit},
Strict: true,
Callable: bit_eq_callable,
}

// varbit_eq_callable is the callable logic for the varbiteq function.
func varbit_eq_callable(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1 any, val2 any) (any, error) {
res, err := pgtypes.VarBit.Compare(ctx, val1, val2)
return res == 0, err
}

// bit_eq_callable is the callable logic for the varbiteq function.
func bit_eq_callable(ctx *sql.Context, _ [3]*pgtypes.DoltgresType, val1 any, val2 any) (any, error) {
res, err := pgtypes.Bit.Compare(ctx, val1, val2)
return res == 0, err
}

// record_eq represents the PostgreSQL function of the same name, taking the same parameters.
var record_eq = framework.Function2{
Name: "record_eq",
Expand Down
Loading
Loading