Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Add IF(), IFNULL(), NULLIF() #527

Closed
theodesp opened this issue Nov 2, 2018 · 6 comments
Closed

Add IF(), IFNULL(), NULLIF() #527

theodesp opened this issue Nov 2, 2018 · 6 comments
Assignees
Labels

Comments

@theodesp
Copy link
Contributor Author

theodesp commented Nov 6, 2018

Will work on this now

@theodesp
Copy link
Contributor Author

theodesp commented Nov 7, 2018

There might be an issue of determining the Type() of NullIfand If functions.
The case is when you know the result you can return it but in that case, we don't know the result type before the function evaluates.

For example in NullIf:
We need to know if Left and Right are equal so we return sql.Null otherwise we return Left.Type()

In If:
We need to know if Left evaluates to true so we return Left.Type() otherwise we return Right.Type()

What is the recommended approach?

@ajnavarro
Copy link
Contributor

You can check how the type is chosen on MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html#function_if

But basically, using IF(expr1,expr2,expr3) as an example:

  • If expr2 or expr3 produce a string, the result is a string.
  • If expr2 and expr3 are both strings, the result is case-sensitive if either string is case sensitive.
  • If expr2 or expr3 produce a floating-point value, the result is a floating-point value.
  • If expr2 or expr3 produce an integer, the result is an integer.

@theodesp
Copy link
Contributor Author

theodesp commented Nov 8, 2018

Thank you. The question remains for the NullIf is that how we can check if Left and Right are equal as I can see that the Type() gets called before the function evaluates. I mean that I will have to know the result of the Eval beforehand or return a type that will resolve to either sql.Null or Left.Type()

For example if I just return Left.Type() then if the following call:

select nullif(2,2);

will return 0 instead of NULL

@ajnavarro
Copy link
Contributor

The type selection will be done as I described before, the only difference is that the result will be null. Null is not a type itself. I will open an issue to remove it from types list.

@ajnavarro
Copy link
Contributor

Done in: #555

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants