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

[SPARK-32840][SQL] Invalid interval value can happen to be just adhesive with the unit #29708

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,8 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
}
}

private final lazy val intervalValStrPattern = "^[+-]?\\s*\\d*\\.?\\d*$".r.pattern

/**
* Creates a [[CalendarInterval]] with multiple unit value pairs, e.g. 1 YEAR 2 DAYS.
*/
Expand All @@ -2132,7 +2134,12 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
val kvs = units.indices.map { i =>
val u = units(i).getText
val v = if (values(i).STRING() != null) {
string(values(i).STRING())
val value = string(values(i).STRING()).trim
if (!intervalValStrPattern.matcher(value).matches()) {
Copy link
Contributor

@cloud-fan cloud-fan Sep 10, 2020

Choose a reason for hiding this comment

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

how about !value.forall { c => c == '.' || c == "+" || c == "-" || Character.isDigit(c)}? We don't have to use regex.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok maybe the first version is better, as we basically just want to avoid the value string to contain unit. value.exists(Character.isLetter) should be good enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

And we should add comments to explain why we need the check.

throw new ParseException("Can only use numbers in the interval value part for" +
s" multiple unit value pairs interval form, but got invalid value: $value", ctx)
}
value
} else {
values(i).getText
}
Expand Down
4 changes: 4 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/interval.sql
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,7 @@ select interval '1.2';
select interval '- 2';
select interval '1 day -';
select interval '1 day 1';

select interval '1 day 2' day;
select interval 'interval 1' day;
select interval '-\t 1' day;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 107
-- Number of queries: 110


-- !query
Expand Down Expand Up @@ -1122,3 +1122,39 @@ Cannot parse the INTERVAL value: 1 day 1(line 1, pos 7)
== SQL ==
select interval '1 day 1'
-------^^^


-- !query
select interval '1 day 2' day
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.parser.ParseException

Can only use numbers in the interval value part for multiple unit value pairs interval form, but got invalid value: 1 day 2(line 1, pos 16)

== SQL ==
select interval '1 day 2' day
----------------^^^


-- !query
select interval 'interval 1' day
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.parser.ParseException

Can only use numbers in the interval value part for multiple unit value pairs interval form, but got invalid value: interval 1(line 1, pos 16)

== SQL ==
select interval 'interval 1' day
----------------^^^


-- !query
select interval '-\t 1' day
-- !query schema
struct<INTERVAL '-1 days':interval>
-- !query output
-1 days
38 changes: 37 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/interval.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 107
-- Number of queries: 110


-- !query
Expand Down Expand Up @@ -1110,3 +1110,39 @@ Cannot parse the INTERVAL value: 1 day 1(line 1, pos 7)
== SQL ==
select interval '1 day 1'
-------^^^


-- !query
select interval '1 day 2' day
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.parser.ParseException

Can only use numbers in the interval value part for multiple unit value pairs interval form, but got invalid value: 1 day 2(line 1, pos 16)

== SQL ==
select interval '1 day 2' day
----------------^^^


-- !query
select interval 'interval 1' day
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.parser.ParseException

Can only use numbers in the interval value part for multiple unit value pairs interval form, but got invalid value: interval 1(line 1, pos 16)

== SQL ==
select interval 'interval 1' day
----------------^^^


-- !query
select interval '-\t 1' day
-- !query schema
struct<INTERVAL '-1 days':interval>
-- !query output
-1 days