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

expression,parser: add support for INTERVAL function #2370

Merged
merged 3 commits into from
Jan 5, 2017

Conversation

zyguan
Copy link
Contributor

@zyguan zyguan commented Jan 1, 2017

This is the last builtin function wanted by #112 :)

@ngaut
Copy link
Member

ngaut commented Jan 1, 2017

Cool staff.

@shenli
Copy link
Member

shenli commented Jan 1, 2017

"INTERVAL" is a reserved keyword of MySQL. See http://dev.mysql.com/doc/refman/5.7/en/keywords.html

Please add it to the ReservedKeyWord rule list https://github.com/pingcap/tidb/blob/master/parser/parser.y#L2069 and add a test here: https://github.com/pingcap/tidb/blob/master/parser/parser_test.go#L60

@zyguan
Copy link
Contributor Author

zyguan commented Jan 1, 2017

I see It's already been in the
ReservedKeyWord@https://github.com/pingcap/tidb/blob/master/parser/parser.y#L2079
and
reservedKws@https://github.com/pingcap/tidb/blob/master/parser/parser_test.go#L51

Is there any thing else I should do? @shenli

@shenli
Copy link
Member

shenli commented Jan 1, 2017

@zyguan Yes, you are right.
LGTM @coocood @tiancaiamao PTAL

v2, _ := d2.ToFloat64(sc)
return v1 < v2
}
v1, _ := d1.ToInt64(sc)
Copy link
Contributor

@tiancaiamao tiancaiamao Jan 3, 2017

Choose a reason for hiding this comment

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

I guess the actual behavior is use float for comparison:

mysql> SELECT INTERVAL(23, 1.7, 15.3, 23.1, 30, 44, 200);
+--------------------------------------------+
| INTERVAL(23, 1.7, 15.3, 23.1, 30, 44, 200) |
+--------------------------------------------+
|                                          2 |
+--------------------------------------------+
1 row in set (0.00 sec)

for _, t := range []struct {
args []types.Datum
ret int64
}{
Copy link
Contributor

Choose a reason for hiding this comment

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

add test case:
23, 1, 23, 23, 23, 30, 44, 200 // same integer appear many times
23, 1.7, 15.3, 23.1, 30, 44, 200 // float

sc := ctx.GetSessionVars().StmtCtx

lt := func(d1, d2 types.Datum) bool {
if d1.Kind() == types.KindString || d2.Kind() == types.KindString {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need take much consideration for this.
user will get wrong result because their input is illegal.
when the N1...Nn is not sorted, the result is also undefined since we use binary search.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this: if there is any non-integer datum, we compare them as float ?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's more reasonable.

@@ -2448,6 +2451,7 @@ FunctionNameConflict:
| "UTC_DATE"
| "CURRENT_DATE"
| "VERSION"
| "INTERVAL" %prec lowerThanIntervalKeyword
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I think I do not catch your idea, could you please explain further?

Copy link
Contributor

Choose a reason for hiding this comment

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

INTERVAL is a reserved keyword in MySQL, for reserved keyword, you need add it here , here and test it here

This is because reserved keyword can't be used as table name, database name and so on, not the same as normal identifier.

Copy link
Contributor

Choose a reason for hiding this comment

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

ignore this comment, INTERVAL is already added @zyguan

@shenli
Copy link
Member

shenli commented Jan 4, 2017

@tiancaiamao PTAL

if d1.Kind() == types.KindUint64 && d2.Kind() == types.KindInt64 {
return d2.GetInt64() > 0 && d1.GetUint64() < d2.GetUint64()
}
v1, _ := d1.ToFloat64(sc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ignore error here means if input is wrong, it always give a result.

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, it is. Maybe we need check for some special cases, but I'm still unable to make INTERVAL produce an error in mysql.

BTW, can we close #112 now?

@tiancaiamao
Copy link
Contributor

LGTM

@tiancaiamao tiancaiamao merged commit 8e109b7 into pingcap:master Jan 5, 2017
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