-
-
Notifications
You must be signed in to change notification settings - Fork 218
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 elt()
and field()
function
#2262
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e855153
implement, unit test, engine test
66d6d3a
[ga-format-pr] Run ./format_repo.sh to fix formatting
jycor f249291
field
f61b2ae
Merge branch 'james/field' of https://github.com/dolthub/go-mysql-ser…
db43bb5
[ga-format-pr] Run ./format_repo.sh to fix formatting
jycor a4ef3ec
not nullable
bf9c95b
merge with main
32e77fd
updating tests
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,142 @@ | ||
// 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" | ||
"strings" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
// Elt joins several strings together. | ||
type Elt struct { | ||
args []sql.Expression | ||
} | ||
|
||
var _ sql.FunctionExpression = (*Elt)(nil) | ||
var _ sql.CollationCoercible = (*Elt)(nil) | ||
|
||
// NewElt creates a new Elt UDF. | ||
func NewElt(args ...sql.Expression) (sql.Expression, error) { | ||
if len(args) < 2 { | ||
return nil, sql.ErrInvalidArgumentNumber.New("ELT", "2 or more", len(args)) | ||
} | ||
|
||
return &Elt{args}, nil | ||
} | ||
|
||
// FunctionName implements sql.FunctionExpression | ||
func (e *Elt) FunctionName() string { | ||
return "elt" | ||
} | ||
|
||
// Description implements sql.FunctionExpression | ||
func (e *Elt) Description() string { | ||
return "returns the string at index number." | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (e *Elt) Type() sql.Type { | ||
return types.LongText | ||
} | ||
|
||
// CollationCoercibility implements the interface sql.CollationCoercible. | ||
func (e *Elt) CollationCoercibility(ctx *sql.Context) (sql.CollationID, byte) { | ||
if len(e.args) == 0 { | ||
return sql.Collation_binary, 6 | ||
} | ||
collation, coercibility := sql.GetCoercibility(ctx, e.args[0]) | ||
for i := 1; i < len(e.args); i++ { | ||
nextCollation, nextCoercibility := sql.GetCoercibility(ctx, e.args[i]) | ||
collation, coercibility = sql.ResolveCoercibility(collation, coercibility, nextCollation, nextCoercibility) | ||
} | ||
return collation, coercibility | ||
} | ||
|
||
// IsNullable implements the Expression interface. | ||
func (e *Elt) IsNullable() bool { | ||
return true | ||
} | ||
|
||
// String implements the Stringer interface. | ||
func (e *Elt) String() string { | ||
var args = make([]string, len(e.args)) | ||
for i, arg := range e.args { | ||
args[i] = arg.String() | ||
} | ||
return fmt.Sprintf("%s(%s)", e.FunctionName(), strings.Join(args, ",")) | ||
} | ||
|
||
// WithChildren implements the Expression interface. | ||
func (*Elt) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
return NewElt(children...) | ||
} | ||
|
||
// Resolved implements the Expression interface. | ||
func (e *Elt) Resolved() bool { | ||
for _, arg := range e.args { | ||
if !arg.Resolved() { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Children implements the Expression interface. | ||
func (e *Elt) Children() []sql.Expression { | ||
return e.args | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (e *Elt) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
if e.args[0] == nil { | ||
return nil, nil | ||
} | ||
|
||
index, err := e.args[0].Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if index == nil { | ||
return nil, nil | ||
} | ||
|
||
indexInt, _, err := types.Int64.Convert(index) | ||
if err != nil { | ||
// TODO: truncate | ||
ctx.Warn(1292, "Truncated incorrect INTEGER value: '%v'", index) | ||
jycor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
indexInt = int64(0) | ||
} | ||
|
||
idx := int(indexInt.(int64)) | ||
if idx <= 0 || idx >= len(e.args) { | ||
return nil, nil | ||
} | ||
|
||
str, err := e.args[idx].Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, _, err := types.Text.Convert(str) | ||
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,150 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/expression" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
func TestElt(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []sql.Expression | ||
exp interface{} | ||
err bool | ||
skip bool | ||
}{ | ||
{ | ||
name: "null argument", | ||
args: []sql.Expression{ | ||
nil, | ||
nil, | ||
}, | ||
exp: nil, | ||
}, | ||
{ | ||
name: "zero returns null", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(0, types.Int32), | ||
expression.NewLiteral("foo", types.Text), | ||
}, | ||
exp: nil, | ||
}, | ||
{ | ||
name: "negative returns null", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(-10, types.Int32), | ||
expression.NewLiteral("foo", types.Text), | ||
}, | ||
exp: nil, | ||
}, | ||
{ | ||
name: "too large returns null", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(100, types.Int32), | ||
expression.NewLiteral("foo", types.Text), | ||
}, | ||
exp: nil, | ||
}, | ||
{ | ||
name: "simple case", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(1, types.Int32), | ||
expression.NewLiteral("foo", types.Text), | ||
}, | ||
exp: "foo", | ||
}, | ||
{ | ||
name: "simple case again", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(3, types.Int32), | ||
expression.NewLiteral("foo1", types.Text), | ||
expression.NewLiteral("foo2", types.Text), | ||
expression.NewLiteral("foo3", types.Text), | ||
}, | ||
exp: "foo3", | ||
}, | ||
{ | ||
name: "index is float", | ||
args: []sql.Expression{ | ||
expression.NewLiteral(2.9, types.Float64), | ||
expression.NewLiteral("foo1", types.Text), | ||
expression.NewLiteral("foo2", types.Text), | ||
expression.NewLiteral("foo3", types.Text), | ||
}, | ||
exp: "foo3", | ||
}, | ||
{ | ||
name: "index is valid string", | ||
args: []sql.Expression{ | ||
expression.NewLiteral("2", types.Text), | ||
expression.NewLiteral("foo1", types.Text), | ||
expression.NewLiteral("foo2", types.Text), | ||
expression.NewLiteral("foo3", types.Text), | ||
}, | ||
exp: "foo2", | ||
}, | ||
{ | ||
name: "args are cast to string", | ||
args: []sql.Expression{ | ||
expression.NewLiteral("3", types.Text), | ||
expression.NewLiteral("foo1", types.Text), | ||
expression.NewLiteral("foo2", types.Text), | ||
expression.NewLiteral(123, types.Int32), | ||
}, | ||
exp: "123", | ||
}, | ||
{ | ||
// we don't do truncation yet | ||
// https://github.com/dolthub/dolt/issues/7302 | ||
name: "scientific string is truncated", | ||
args: []sql.Expression{ | ||
expression.NewLiteral("1e1", types.Text), | ||
expression.NewLiteral("foo1", types.Text), | ||
expression.NewLiteral("foo2", types.Text), | ||
expression.NewLiteral("foo3", types.Int32), | ||
}, | ||
exp: "foo3", | ||
skip: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.skip { | ||
t.Skip() | ||
} | ||
|
||
ctx := sql.NewEmptyContext() | ||
f, err := NewElt(tt.args...) | ||
require.NoError(t, err) | ||
|
||
res, err := f.Eval(ctx, nil) | ||
if tt.err { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tt.exp, res) | ||
}) | ||
} | ||
} |
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.
should we validate the arg types? what happens if one of these isn't a string?
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.
it looks like non-string types are OK actually, these two both return 2