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 all 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 @@ -2132,7 +2132,16 @@ 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())
// SPARK-32840: For invalid cases, e.g. INTERVAL '1 day 2' hour,
// INTERVAL 'interval 1' day, we need to check ahead before they are concatenated with
// units and become valid ones, e.g. '1 day 2 hour'.
// Ideally, we only ensure the value parts don't contain any units here.
if (value.exists(Character.isLetter)) {
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