Skip to content

Commit 7d11364

Browse files
committed
Fail analysis if window function uses partitioning on non orderable column
Currently execution requires symbols used for window function partitioning to be orderable (not just comparable). We use PagesIndex and iterating over it in order to determine partition boundaries. This commit fixes logic of determining invalid queries.
1 parent b672f1f commit 7d11364

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

core/trino-main/src/main/java/io/trino/sql/analyzer/ExpressionAnalyzer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,11 @@ private void analyzeWindow(ResolvedWindow window, Context context, Node original
14691469
if (!type.isComparable()) {
14701470
throw semanticException(TYPE_MISMATCH, expression, "%s is not comparable, and therefore cannot be used in window function PARTITION BY", type);
14711471
}
1472+
if (!type.isOrderable()) {
1473+
// TODO this is not a hard requirement, just the implication of how we do partitioning right now, using PagesIndex and iterating over ordered values
1474+
// to find partition boundaries.
1475+
throw semanticException(TYPE_MISMATCH, expression, "%s is not orderable, and therefore cannot be used in window function PARTITION BY", type);
1476+
}
14721477
}
14731478
}
14741479

core/trino-main/src/test/java/io/trino/sql/analyzer/TestAnalyzer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ public void testNonComparableWindowPartition()
280280
.hasMessage("line 1:40: HyperLogLog is not comparable, and therefore cannot be used in window function PARTITION BY");
281281
}
282282

283+
@Test
284+
public void testNonOrderableWindowPartition()
285+
{
286+
assertFails("SELECT row_number() OVER (PARTITION BY t.x) FROM (VALUES(MAP(ARRAY['key1', 'key2', 'key3' ], ARRAY[2373, 3463, 45837])) )AS t(x)")
287+
.hasErrorCode(TYPE_MISMATCH)
288+
.hasMessage("line 1:40: map(varchar(4), integer) is not orderable, and therefore cannot be used in window function PARTITION BY");
289+
}
290+
283291
@Test
284292
public void testNonComparableWindowOrder()
285293
{

0 commit comments

Comments
 (0)