-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
implement random_bytes()
#2365
Merged
Merged
implement random_bytes()
#2365
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,115 @@ | ||
// 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 function | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
|
||
"github.com/dolthub/vitess/go/sqltypes" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/expression" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
const randomBytesMax = 1024 | ||
|
||
// RandomBytes returns a random binary string of the given length. | ||
type RandomBytes struct { | ||
expression.UnaryExpression | ||
} | ||
|
||
var _ sql.FunctionExpression = (*RandomBytes)(nil) | ||
var _ sql.CollationCoercible = (*RandomBytes)(nil) | ||
|
||
// NewRandomBytes returns a new RANDOM_BYTES function. | ||
func NewRandomBytes(e sql.Expression) sql.Expression { | ||
return &RandomBytes{expression.UnaryExpression{Child: e}} | ||
} | ||
|
||
// FunctionName implements sql.FunctionExpression | ||
func (r *RandomBytes) FunctionName() string { | ||
return "random_bytes" | ||
} | ||
|
||
// Description implements sql.FunctionExpression | ||
func (r *RandomBytes) Description() string { | ||
return "returns a random binary string of the given length" | ||
} | ||
|
||
// WithChildren implements the Expression interface. | ||
func (r *RandomBytes) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
if len(children) != 1 { | ||
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 1) | ||
} | ||
|
||
return NewRandomBytes(children[0]), nil | ||
} | ||
|
||
// Type implements the sql.Expression interface. | ||
func (r *RandomBytes) Type() sql.Type { | ||
return types.MustCreateString(sqltypes.VarBinary, 1024, sql.Collation_binary) | ||
} | ||
|
||
// CollationCoercibility implements the interface sql.CollationCoercible. | ||
func (*RandomBytes) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { | ||
return sql.Collation_binary, 5 | ||
} | ||
|
||
// String implements the sql.Expression interface. | ||
func (r *RandomBytes) String() string { | ||
return fmt.Sprintf("%s(%s)", r.FunctionName(), r.Child) | ||
} | ||
|
||
// IsNonDeterministic implements the sql.Expression interface. | ||
func (r *RandomBytes) IsNonDeterministic() bool { | ||
return true | ||
} | ||
|
||
// Eval implements the sql.Expression interface. | ||
func (r *RandomBytes) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
val, err := r.Child.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if val == nil { | ||
return nil, nil | ||
} | ||
|
||
val, _, err = types.Int64.Convert(val) | ||
if err != nil { | ||
val = 0 | ||
ctx.Warn(1292, "Truncated incorrect INTEGER value") | ||
} | ||
|
||
length, ok := types.CoalesceInt(val) | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
if length <= 0 || length > randomBytesMax { | ||
return nil, sql.ErrValueOutOfRange.New(length, r.FunctionName()) | ||
} | ||
|
||
res := make([]byte, length) | ||
_, err = rand.Read(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains 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,107 @@ | ||
// 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 function | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/src-d/go-errors.v1" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/expression" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
func TestRandomBytes(t *testing.T) { | ||
testCases := []struct { | ||
expr sql.Expression | ||
exp interface{} | ||
skip bool | ||
err *errors.Kind | ||
}{ | ||
{ | ||
expr: expression.NewLiteral(nil, types.Null), | ||
exp: nil, | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(0), types.Int32), | ||
err: sql.ErrValueOutOfRange, | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(-1), types.Int32), | ||
err: sql.ErrValueOutOfRange, | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(randomBytesMax+1), types.Int32), | ||
err: sql.ErrValueOutOfRange, | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(1), types.Int32), | ||
exp: make([]byte, 1), | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(100), types.Int32), | ||
exp: make([]byte, 100), | ||
}, | ||
{ | ||
expr: expression.NewLiteral(int32(randomBytesMax), types.Int32), | ||
exp: make([]byte, randomBytesMax), | ||
}, | ||
{ | ||
expr: expression.NewLiteral(3.9, types.Float64), | ||
exp: make([]byte, 4), | ||
}, | ||
{ | ||
expr: expression.NewLiteral(3.4, types.Float64), | ||
exp: make([]byte, 3), | ||
}, | ||
{ | ||
expr: expression.NewLiteral("10", types.Text), | ||
exp: make([]byte, 10), | ||
}, | ||
{ | ||
expr: expression.NewLiteral("a", types.Text), | ||
err: sql.ErrValueOutOfRange, | ||
}, | ||
{ | ||
skip: true, | ||
expr: expression.NewLiteral("1abc", types.Text), | ||
exp: make([]byte, 1), | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(fmt.Sprintf("%s(%v)", "random_bytes", test.expr.String()), func(t *testing.T) { | ||
if test.skip { | ||
t.Skip() | ||
} | ||
ctx := sql.NewEmptyContext() | ||
f := NewRandomBytes(test.expr) | ||
res, err := f.Eval(ctx, nil) | ||
if test.err != nil { | ||
require.True(t, test.err.Is(err)) | ||
return | ||
} | ||
require.NoError(t, err) | ||
if test.exp == nil { | ||
require.Equal(t, test.exp, res) | ||
return | ||
} | ||
require.Equal(t, len(test.exp.([]byte)), len(res.([]byte))) | ||
}) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) maybe another error test case where the length is of a varchar type or something.