Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into SPARK-29783
  • Loading branch information
yaooqinn committed Nov 16, 2019
commit 08b7359fdcda41765b4d35485a6e72b798705797
Original file line number Diff line number Diff line change
Expand Up @@ -1798,14 +1798,6 @@ object SQLConf {
.checkValues(StoreAssignmentPolicy.values.map(_.toString))
.createWithDefault(StoreAssignmentPolicy.ANSI.toString)

val ANSI_ENABLED = buildConf("spark.sql.ansi.enabled")
.doc("When true, Spark tries to conform to the ANSI SQL specification: 1. Spark will " +
"throw a runtime exception if an overflow occurs in any operation on integral/decimal " +
"field. 2. Spark will forbid using the reserved keywords of ANSI SQL as identifiers in " +
"the SQL parser.")
.booleanConf
.createWithDefault(false)

object IntervalStyle extends Enumeration {
type IntervalStyle = Value
val SQL_STANDARD, ISO_8601, MULTI_UNITS = Value
Expand Down Expand Up @@ -2554,7 +2546,7 @@ class SQLConf extends Serializable with Logging {

def intervalOutputStyle: IntervalStyle.Value = IntervalStyle.withName(getConf(INTERVAL_STYLE))

def ansiEnabled: Boolean = getConf(ANSI_ENABLED)
def usePostgreSQLDialect: Boolean = getConf(DIALECT) == Dialect.POSTGRESQL.toString

def dialectSparkAnsiEnabled: Boolean = getConf(DIALECT_SPARK_ANSI_ENABLED)

Expand Down
204 changes: 164 additions & 40 deletions sql/core/src/test/resources/sql-tests/inputs/interval.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,45 +91,169 @@ select justify_days(interval '1 month 59 day -25 hour');
select justify_hours(interval '1 month 59 day -25 hour');
select justify_interval(interval '1 month 59 day -25 hour');

-- interval output style
set spark.sql.intervalOutputStyle=SQL_STANDARD;
SELECT
cast(null as interval), -- null
interval '0 day', -- 0
interval '1 year', -- year only
interval '1 month', -- month only
interval '1 year 2 month', -- year month only
interval '1 day -1 hours',
interval '-1 day -1 hours',
interval '-1 day 1 hours',
interval '-1 days +1 hours',
interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
- interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';

set spark.sql.intervalOutputStyle=ISO_8601;
-- interval literal
select interval 13.123456789 seconds, interval -13.123456789 second;
select interval 1 year 2 month 3 week 4 day 5 hour 6 minute 7 seconds 8 millisecond 9 microsecond;
select interval '30' year '25' month '-100' day '40' hour '80' minute '299.889987299' second;
select interval '0 0:0:0.1' day to second;
select interval '10-9' year to month;
select interval '20 15:40:32.99899999' day to hour;
select interval '20 15:40:32.99899999' day to minute;
select interval '20 15:40:32.99899999' day to second;
select interval '15:40:32.99899999' hour to minute;
select interval '15:40.99899999' hour to second;
select interval '15:40' hour to second;
select interval '15:40:32.99899999' hour to second;
select interval '20 40:32.99899999' minute to second;
select interval '40:32.99899999' minute to second;
select interval '40:32' minute to second;
select interval 30 day day;

-- ns is not supported
select interval 10 nanoseconds;

-- map + interval test
select map(1, interval 1 day, 2, interval 3 week);

-- typed interval expression
select interval 'interval 3 year 1 hour';
select interval '3 year 1 hour';

-- malformed interval literal
select interval;
select interval 1 fake_unit;
select interval 1 year to month;
select interval '1' year to second;
select interval '10-9' year to month '2-1' year to month;
select interval '10-9' year to month '12:11:10' hour to second;
select interval '1 15:11' day to minute '12:11:10' hour to second;
select interval 1 year '2-1' year to month;
select interval 1 year '12:11:10' hour to second;
select interval '10-9' year to month '1' year;
select interval '12:11:10' hour to second '1' year;
select interval (-30) day;
select interval (a + 1) day;
select interval 30 day day day;

-- sum interval values
-- null
select sum(cast(null as interval));

-- empty set
select sum(cast(v as interval)) from VALUES ('1 seconds') t(v) where 1=0;

-- basic interval sum
select sum(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null) t(v);
select sum(cast(v as interval)) from VALUES ('-1 seconds'), ('2 seconds'), (null) t(v);
select sum(cast(v as interval)) from VALUES ('-1 seconds'), ('-2 seconds'), (null) t(v);
select sum(cast(v as interval)) from VALUES ('-1 weeks'), ('2 seconds'), (null) t(v);

-- group by
select
i,
sum(cast(v as interval))
from VALUES (1, '-1 weeks'), (2, '2 seconds'), (3, null), (1, '5 days') t(i, v)
group by i;

-- having
select
sum(cast(v as interval)) as sv
from VALUES (1, '-1 weeks'), (2, '2 seconds'), (3, null), (1, '5 days') t(i, v)
having sv is not null;

-- window
SELECT
cast(null as interval), -- null
interval '0 day', -- 0
interval '1 year', -- year only
interval '1 month', -- month only
interval '1 year 2 month', -- year month only
interval '-1 day -1 hours',
interval '-1 day 1 hours',
interval '1 day -1 hours',
interval '-1 days +1 hours',
interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
- interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';

set spark.sql.intervalOutputStyle=MULTI_UNITS;
i,
sum(cast(v as interval)) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM VALUES(1, '1 seconds'), (1, '2 seconds'), (2, NULL), (2, NULL) t(i,v);

-- average with interval type
-- null
select avg(cast(v as interval)) from VALUES (null) t(v);

-- empty set
select avg(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null) t(v) where 1=0;

-- basic interval avg
select avg(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null) t(v);
select avg(cast(v as interval)) from VALUES ('-1 seconds'), ('2 seconds'), (null) t(v);
select avg(cast(v as interval)) from VALUES ('-1 seconds'), ('-2 seconds'), (null) t(v);
select avg(cast(v as interval)) from VALUES ('-1 weeks'), ('2 seconds'), (null) t(v);

-- group by
select
i,
avg(cast(v as interval))
from VALUES (1, '-1 weeks'), (2, '2 seconds'), (3, null), (1, '5 days') t(i, v)
group by i;

-- having
select
avg(cast(v as interval)) as sv
from VALUES (1, '-1 weeks'), (2, '2 seconds'), (3, null), (1, '5 days') t(i, v)
having sv is not null;

-- window
SELECT
cast(null as interval), -- null
interval '0 day', -- 0
interval '1 year', -- year only
interval '1 month', -- month only
interval '1 year 2 month', -- year month only
interval '-1 day -1 hours',
interval '-1 day 1 hours',
interval '1 day -1 hours',
interval '-1 days +1 hours',
interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
- interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
i,
avg(cast(v as interval)) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
FROM VALUES (1,'1 seconds'), (1,'2 seconds'), (2,NULL), (2,NULL) t(i,v);

-- Interval year-month arithmetic

create temporary view interval_arithmetic as
select CAST(dateval AS date), CAST(tsval AS timestamp) from values
('2012-01-01', '2012-01-01')
as interval_arithmetic(dateval, tsval);

select
dateval,
dateval - interval '2-2' year to month,
dateval - interval '-2-2' year to month,
dateval + interval '2-2' year to month,
dateval + interval '-2-2' year to month,
- interval '2-2' year to month + dateval,
interval '2-2' year to month + dateval
from interval_arithmetic;

select
tsval,
tsval - interval '2-2' year to month,
tsval - interval '-2-2' year to month,
tsval + interval '2-2' year to month,
tsval + interval '-2-2' year to month,
- interval '2-2' year to month + tsval,
interval '2-2' year to month + tsval
from interval_arithmetic;

select
interval '2-2' year to month + interval '3-3' year to month,
interval '2-2' year to month - interval '3-3' year to month
from interval_arithmetic;

-- Interval day-time arithmetic

select
dateval,
dateval - interval '99 11:22:33.123456789' day to second,
dateval - interval '-99 11:22:33.123456789' day to second,
dateval + interval '99 11:22:33.123456789' day to second,
dateval + interval '-99 11:22:33.123456789' day to second,
-interval '99 11:22:33.123456789' day to second + dateval,
interval '99 11:22:33.123456789' day to second + dateval
from interval_arithmetic;

select
tsval,
tsval - interval '99 11:22:33.123456789' day to second,
tsval - interval '-99 11:22:33.123456789' day to second,
tsval + interval '99 11:22:33.123456789' day to second,
tsval + interval '-99 11:22:33.123456789' day to second,
-interval '99 11:22:33.123456789' day to second + tsval,
interval '99 11:22:33.123456789' day to second + tsval
from interval_arithmetic;

select
interval '99 11:22:33.123456789' day to second + interval '10 9:8:7.123456789' day to second,
interval '99 11:22:33.123456789' day to second - interval '10 9:8:7.123456789' day to second
from interval_arithmetic;
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.