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

implement elt() and field() function #2262

merged 8 commits into from
Jan 16, 2024

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Jan 15, 2024

This PR implements the ELT() function, which just returns the nth argument as a string.
This PR also implements the FIELD() function, which returns the first case-insensitive match to the first argument.

MySQL Docs:

@jycor jycor changed the title implement elt() function implement elt() and field() function Jan 16, 2024
Copy link
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm mostly, testing for this one is a bit tricker

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');


// Type implements the Expression interface.
func (f *Field) Type() sql.Type {
return types.Int32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql reports this as a long when I'm testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I get when running on MySQL with --column-type-info

mysql> select field('a', 'a');
Field 1
Name:      `field('a', 'a')`
Org_name:  ``
Catalog:   `def`
Database:  ``
Table:     ``
Org_table: ``
Type:      Integer
DbType:    TINY
Collation: binary (63)
Length:    3
Decimals:  0
Flags:     NOT_NULL NUM

+-----------------+
| field('a', 'a') |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.0003 sec)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what i get, mysql 8.0.2

mysql> select field('a', 'a');
Field   1:  `field('a', 'a')`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     3
Max_length: 1
Decimals:   0
Flags:      NOT_NULL BINARY NUM


+-----------------+
| field('a', 'a') |
+-----------------+
|               1 |
+-----------------+

Copy link
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@jycor jycor merged commit 61b1d08 into main Jan 16, 2024
7 checks passed
@jycor jycor deleted the james/field branch January 16, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants