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

add support for the LEAST function #2360

Merged
merged 5 commits into from
Dec 31, 2016
Merged

Conversation

zyguan
Copy link
Contributor

@zyguan zyguan commented Dec 30, 2016

Hi, @shenli , here is the LEAST function wanted by #112 , PTAL :)

@ngaut
Copy link
Member

ngaut commented Dec 30, 2016

Good job! @zyguan

}

var cmp int
if cmp, err = args[i].CompareDatum(sc, args[min]); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

When i == 0, it is not necessary to make this comparison.

sc := ctx.GetSessionVars().StmtCtx
for i := 0; i < len(args); i++ {
if args[i].IsNull() {
d.SetNull()
Copy link
Member

Choose a reason for hiding this comment

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

d.IsNull() is true by default, so we don't need this d.SetNull().

@zyguan
Copy link
Contributor Author

zyguan commented Dec 30, 2016

Thanks @ngaut ! this is the simplest function to implement since there is already a GREATEST function implementation.

I'm working on INTERVAL(), however, there is still a problem. eg:

SELECT DATE_ADD('2016-12-31',INTERVAL INTERVAL(1,1) DAY)

will return NULL instead of 2017-01-01 :(

I'll try to solve this as soon as possible.

@shenli
Copy link
Member

shenli commented Dec 30, 2016

@zyguan Thanks! If you need any help, please let us know.

@zyguan
Copy link
Contributor Author

zyguan commented Dec 30, 2016

@shenli 😄

func builtinLeast(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
min := 0
sc := ctx.GetSessionVars().StmtCtx
for i := 1; i < len(args); i++ {
Copy link
Member

@shenli shenli Dec 31, 2016

Choose a reason for hiding this comment

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

Should we check if args[0].IsNull() before the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right, I forget this.

GREATEST|LEAST should return NULL if any of its arguments is NULL.
@ngaut
Copy link
Member

ngaut commented Dec 31, 2016

PTAL

@shenli
Copy link
Member

shenli commented Dec 31, 2016

LGTM Well done! @zyguan
@coocood @hanfei1991 @tiancaiamao PTAL

Copy link
Member

@hanfei1991 hanfei1991 left a comment

Choose a reason for hiding this comment

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

LGTM

@hanfei1991 hanfei1991 merged commit f0fbe16 into pingcap:master Dec 31, 2016
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.

4 participants