Skip to content
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 8 commits into from
Jan 16, 2024
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
16 changes: 16 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8848,6 +8848,22 @@ from typestable`,
{math.Atan2(3, 1), math.Atan2(3, 5)},
},
},
{
Query: "select elt(i, 'a', 'b') from mytable;",
Expected: []sql.Row{
{"a"},
{"b"},
{nil},
},
},
{
Query: "select field(i, '1', '2', '3') from mytable;",
Expected: []sql.Row{
{1},
{2},
{3},
},
},
}

var KeylessQueries = []QueryTest{
Expand Down
142 changes: 142 additions & 0 deletions sql/expression/function/elt.go
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
Copy link
Contributor

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?

Copy link
Contributor

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

select field(2, 'a',2);
select field(2, 'a','2');

}

// 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)
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
}
150 changes: 150 additions & 0 deletions sql/expression/function/elt_test.go
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)
})
}
}
Loading