-
-
Notifications
You must be signed in to change notification settings - Fork 58
Added UUID type #125
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
Merged
Merged
Added UUID type #125
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // Copyright 2024 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 types | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "reflect" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| "github.com/dolthub/vitess/go/sqltypes" | ||
| "github.com/dolthub/vitess/go/vt/proto/query" | ||
|
|
||
| "github.com/dolthub/doltgresql/postgres/parser/uuid" | ||
| ) | ||
|
|
||
| // Uuid is the UUID type. | ||
| var Uuid = UuidType{} | ||
|
|
||
| // UuidType is the extended type implementation of the PostgreSQL UUID. | ||
| type UuidType struct{} | ||
|
|
||
| var _ types.ExtendedType = UuidType{} | ||
|
|
||
| // CollationCoercibility implements the types.ExtendedType interface. | ||
| func (b UuidType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { | ||
| return sql.Collation_binary, 5 | ||
| } | ||
|
|
||
| // Compare implements the types.ExtendedType interface. | ||
| func (b UuidType) Compare(v1 any, v2 any) (int, error) { | ||
| if v1 == nil && v2 == nil { | ||
| return 0, nil | ||
| } else if v1 != nil && v2 == nil { | ||
| return 1, nil | ||
| } else if v1 == nil && v2 != nil { | ||
| return -1, nil | ||
| } | ||
|
|
||
| ac, _, err := b.Convert(v1) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| bc, _, err := b.Convert(v2) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| ab := ac.(uuid.UUID) | ||
Hydrocharged marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bb := bc.(uuid.UUID) | ||
| return bytes.Compare(ab.GetBytesMut(), bb.GetBytesMut()), nil | ||
| } | ||
|
|
||
| // Convert implements the types.ExtendedType interface. | ||
| func (b UuidType) Convert(val any) (any, sql.ConvertInRange, error) { | ||
| if val == nil { | ||
| return nil, sql.InRange, nil | ||
| } | ||
|
|
||
| switch val := val.(type) { | ||
| case string: | ||
| uuidVal, err := uuid.FromString(val) | ||
Hydrocharged marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, sql.OutOfRange, err | ||
| } | ||
| return uuidVal, sql.InRange, nil | ||
| case uuid.UUID: | ||
| return val, sql.InRange, nil | ||
| default: | ||
| return nil, sql.OutOfRange, sql.ErrInvalidType.New(b) | ||
| } | ||
| } | ||
|
|
||
| // Equals implements the types.ExtendedType interface. | ||
| func (b UuidType) Equals(otherType sql.Type) bool { | ||
| if otherExtendedType, ok := otherType.(types.ExtendedType); ok { | ||
| return bytes.Equal(MustSerializeType(b), MustSerializeType(otherExtendedType)) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // FormatSerializedValue implements the types.ExtendedType interface. | ||
| func (b UuidType) FormatSerializedValue(val []byte) (string, error) { | ||
| deserialized, err := b.DeserializeValue(val) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return b.FormatValue(deserialized) | ||
| } | ||
|
|
||
| // FormatValue implements the types.ExtendedType interface. | ||
| func (b UuidType) FormatValue(val any) (string, error) { | ||
| if val == nil { | ||
| return "", nil | ||
| } | ||
| converted, _, err := b.Convert(val) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return converted.(uuid.UUID).String(), nil | ||
| } | ||
|
|
||
| // MaxSerializedWidth implements the types.ExtendedType interface. | ||
| func (b UuidType) MaxSerializedWidth() types.ExtendedTypeSerializedWidth { | ||
| return types.ExtendedTypeSerializedWidth_64K | ||
| } | ||
|
|
||
| // MaxTextResponseByteLength implements the types.ExtendedType interface. | ||
| func (b UuidType) MaxTextResponseByteLength(ctx *sql.Context) uint32 { | ||
| return 16 | ||
| } | ||
|
|
||
| // Promote implements the types.ExtendedType interface. | ||
| func (b UuidType) Promote() sql.Type { | ||
| return b | ||
| } | ||
|
|
||
| // SerializedCompare implements the types.ExtendedType interface. | ||
| func (b UuidType) SerializedCompare(v1 []byte, v2 []byte) (int, error) { | ||
| if len(v1) == 0 && len(v2) == 0 { | ||
| return 0, nil | ||
| } else if len(v1) > 0 && len(v2) == 0 { | ||
| return 1, nil | ||
| } else if len(v1) == 0 && len(v2) > 0 { | ||
| return -1, nil | ||
| } | ||
|
|
||
| return bytes.Compare(v1, v2), nil | ||
Hydrocharged marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // SQL implements the types.ExtendedType interface. | ||
| func (b UuidType) SQL(ctx *sql.Context, dest []byte, v any) (sqltypes.Value, error) { | ||
| if v == nil { | ||
| return sqltypes.NULL, nil | ||
| } | ||
| value, _, err := b.Convert(v) | ||
| if err != nil { | ||
| return sqltypes.Value{}, err | ||
| } | ||
| return sqltypes.MakeTrusted(b.Type(), types.AppendAndSliceBytes(dest, []byte(value.(uuid.UUID).String()))), nil | ||
| } | ||
|
|
||
| // String implements the types.ExtendedType interface. | ||
| func (b UuidType) String() string { | ||
| return "uuid" | ||
| } | ||
|
|
||
| // Type implements the types.ExtendedType interface. | ||
| func (b UuidType) Type() query.Type { | ||
| return sqltypes.Text | ||
| } | ||
|
|
||
| // ValueType implements the types.ExtendedType interface. | ||
| func (b UuidType) ValueType() reflect.Type { | ||
| return reflect.TypeOf(uuid.UUID{}) | ||
| } | ||
|
|
||
| // Zero implements the types.ExtendedType interface. | ||
| func (b UuidType) Zero() any { | ||
| return uuid.UUID{} | ||
| } | ||
|
|
||
| // SerializeValue implements the types.ExtendedType interface. | ||
| func (b UuidType) SerializeValue(val any) ([]byte, error) { | ||
| if val == nil { | ||
| return nil, nil | ||
| } | ||
| converted, _, err := b.Convert(val) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return converted.(uuid.UUID).GetBytes(), nil | ||
| } | ||
|
|
||
| // DeserializeValue implements the types.ExtendedType interface. | ||
| func (b UuidType) DeserializeValue(val []byte) (any, error) { | ||
| if len(val) == 0 { | ||
| return nil, nil | ||
| } | ||
| return uuid.FromBytes(val) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.