Skip to content
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 @@ -547,8 +547,7 @@ class Analyzer(
case a: Aggregate if containsStar(a.aggregateExpressions) =>
if (conf.groupByOrdinal && a.groupingExpressions.exists(IntegerIndex.unapply(_).nonEmpty)) {
failAnalysis(
"Group by position: star is not allowed to use in the select list " +
"when using ordinals in group by")
"Star (*) is not allowed in select list when GROUP BY ordinal position is used")
} else {
a.copy(aggregateExpressions = buildExpandedProjectList(a.aggregateExpressions, a.child))
}
Expand Down Expand Up @@ -723,9 +722,9 @@ class Analyzer(
if (index > 0 && index <= child.output.size) {
SortOrder(child.output(index - 1), direction)
} else {
throw new UnresolvedException(s,
s"Order/sort By position: $index does not exist " +
s"The Select List is indexed from 1 to ${child.output.size}")
s.failAnalysis(
s"ORDER BY position $index is not in select list " +
s"(valid range is [1, ${child.output.size}])")
}
case o => o
}
Expand All @@ -737,17 +736,18 @@ class Analyzer(
if conf.groupByOrdinal && aggs.forall(_.resolved) &&
groups.exists(IntegerIndex.unapply(_).nonEmpty) =>
val newGroups = groups.map {
case IntegerIndex(index) if index > 0 && index <= aggs.size =>
case ordinal @ IntegerIndex(index) if index > 0 && index <= aggs.size =>
aggs(index - 1) match {
case e if ResolveAggregateFunctions.containsAggregate(e) =>
throw new UnresolvedException(a,
s"Group by position: the '$index'th column in the select contains an " +
s"aggregate function: ${e.sql}. Aggregate functions are not allowed in GROUP BY")
ordinal.failAnalysis(
s"GROUP BY position $index is an aggregate function, and " +
"aggregate functions are not allowed in GROUP BY")
case o => o
}
case IntegerIndex(index) =>
throw new UnresolvedException(a,
s"Group by position: '$index' exceeds the size of the select list '${aggs.size}'.")
case ordinal @ IntegerIndex(index) =>
ordinal.failAnalysis(
s"GROUP BY position $index is not in select list " +
s"(valid range is [1, ${aggs.size}])")
case o => o
}
Aggregate(newGroups, aggs, child)
Expand Down
26 changes: 26 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/arithmetic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

-- unary minus and plus
select -100;
select +230;
select -5.2;
select +6.8e0;
select -key, +key from testdata where key = 2;
select -(key + 1), - key + 1, +(key + 5) from testdata where key = 1;
select -max(key), +max(key) from testdata;
select - (-10);
select + (-key) from testdata where key = 32;
select - (+max(key)) from testdata;
select - - 3;
select - + 20;
select + + 100;
select - - max(key) from testdata;
select + - key from testdata where key = 33;

-- other arithmetics
select 1 + 2;
select 1 - 2;
select 2 * 5;
select 5 / 2;
select 5 div 2;
select 5 % 3;
select pmod(-7, 3);
50 changes: 50 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/group-by-ordinal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- group by ordinal positions

create temporary view data as select * from values
(1, 1),
(1, 2),
(2, 1),
(2, 2),
(3, 1),
(3, 2)
as data(a, b);

-- basic case
select a, sum(b) from data group by 1;

-- constant case
select 1, 2, sum(b) from data group by 1, 2;

-- duplicate group by column
select a, 1, sum(b) from data group by a, 1;
select a, 1, sum(b) from data group by 1, 2;

-- group by a non-aggregate expression's ordinal
select a, b + 2, count(2) from data group by a, 2;

-- with alias
select a as aa, b + 2 as bb, count(2) from data group by 1, 2;

-- foldable non-literal: this should be the same as no grouping.
select sum(b) from data group by 1 + 0;

-- negative cases: ordinal out of range
select a, b from data group by -1;
select a, b from data group by 0;
select a, b from data group by 3;

-- negative case: position is an aggregate expression
select a, b, sum(b) from data group by 3;
select a, b, sum(b) + 2 from data group by 3;

-- negative case: nondeterministic expression
select a, rand(0), sum(b) from data group by a, 2;

-- negative case: star
select * from data group by a, b, 1;

-- turn of group by ordinal
set spark.sql.groupByOrdinal=false;

-- can now group by negative literal
select sum(b) from data group by -1;
36 changes: 36 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/order-by-ordinal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- order by and sort by ordinal positions

create temporary view data as select * from values
(1, 1),
(1, 2),
(2, 1),
(2, 2),
(3, 1),
(3, 2)
as data(a, b);

select * from data order by 1 desc;

-- mix ordinal and column name
select * from data order by 1 desc, b desc;

-- order by multiple ordinals
select * from data order by 1 desc, 2 desc;

-- 1 + 0 is considered a constant (not an ordinal) and thus ignored
select * from data order by 1 + 0 desc, b desc;

-- negative cases: ordinal position out of range
select * from data order by 0;
select * from data order by -1;
select * from data order by 3;

-- sort by ordinal
select * from data sort by 1 desc;

-- turn off order by ordinal
set spark.sql.orderByOrdinal=false;

-- 0 is now a valid literal
select * from data order by 0;
select * from data sort by 0;
178 changes: 178 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/arithmetic.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 22


-- !query 0
select -100
-- !query 0 schema
struct<(-100):int>
-- !query 0 output
-100


-- !query 1
select +230
-- !query 1 schema
struct<230:int>
-- !query 1 output
230


-- !query 2
select -5.2
-- !query 2 schema
struct<(-5.2):decimal(2,1)>
-- !query 2 output
-5.2


-- !query 3
select +6.8e0
-- !query 3 schema
struct<6.8:double>
-- !query 3 output
6.8


-- !query 4
select -key, +key from testdata where key = 2
-- !query 4 schema
struct<(-key):int,key:int>
-- !query 4 output
-2 2


-- !query 5
select -(key + 1), - key + 1, +(key + 5) from testdata where key = 1
-- !query 5 schema
struct<(-(key + 1)):int,((-key) + 1):int,(key + 5):int>
-- !query 5 output
-2 0 6


-- !query 6
select -max(key), +max(key) from testdata
-- !query 6 schema
struct<(-max(key)):int,max(key):int>
-- !query 6 output
-100 100


-- !query 7
select - (-10)
-- !query 7 schema
struct<(-(-10)):int>
-- !query 7 output
10


-- !query 8
select + (-key) from testdata where key = 32
-- !query 8 schema
struct<(-key):int>
-- !query 8 output
-32


-- !query 9
select - (+max(key)) from testdata
-- !query 9 schema
struct<(-max(key)):int>
-- !query 9 output
-100


-- !query 10
select - - 3
-- !query 10 schema
struct<(-(-3)):int>
-- !query 10 output
3


-- !query 11
select - + 20
-- !query 11 schema
struct<(-20):int>
-- !query 11 output
-20


-- !query 12
select + + 100
-- !query 12 schema
struct<100:int>
-- !query 12 output
100


-- !query 13
select - - max(key) from testdata
-- !query 13 schema
struct<(-(-max(key))):int>
-- !query 13 output
100


-- !query 14
select + - key from testdata where key = 33
-- !query 14 schema
struct<(-key):int>
-- !query 14 output
-33


-- !query 15
select 1 + 2
-- !query 15 schema
struct<(1 + 2):int>
-- !query 15 output
3


-- !query 16
select 1 - 2
-- !query 16 schema
struct<(1 - 2):int>
-- !query 16 output
-1


-- !query 17
select 2 * 5
-- !query 17 schema
struct<(2 * 5):int>
-- !query 17 output
10


-- !query 18
select 5 / 2
-- !query 18 schema
struct<(CAST(5 AS DOUBLE) / CAST(2 AS DOUBLE)):double>
-- !query 18 output
2.5


-- !query 19
select 5 div 2
-- !query 19 schema
struct<CAST((CAST(5 AS DOUBLE) / CAST(2 AS DOUBLE)) AS BIGINT):bigint>
-- !query 19 output
2


-- !query 20
select 5 % 3
-- !query 20 schema
struct<(5 % 3):int>
-- !query 20 output
2


-- !query 21
select pmod(-7, 3)
-- !query 21 schema
struct<pmod((-7), 3):int>
-- !query 21 output
2
Loading