Skip to content

Commit 2a42eb7

Browse files
committed
Improve handling of negative GRACE PERIOD
Previously, providing a negative grace period resulted in a low-level internal exception: Caused by: java.lang.IllegalArgumentException: gracePeriod cannot be negative: Optional[PT-1H] at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217) at io.trino.metadata.MaterializedViewDefinition.<init>(MaterializedViewDefinition.java:49)
1 parent b0a3bd9 commit 2a42eb7

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

core/trino-parser/src/main/java/io/trino/sql/parser/AstBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,11 @@ public Node visitCreateMaterializedView(SqlBaseParser.CreateMaterializedViewCont
610610
{
611611
Optional<IntervalLiteral> gracePeriod = Optional.empty();
612612
if (context.GRACE() != null) {
613-
gracePeriod = Optional.of((IntervalLiteral) visit(context.interval()));
613+
IntervalLiteral interval = (IntervalLiteral) visit(context.interval());
614+
if (interval.getSign() == IntervalLiteral.Sign.NEGATIVE) {
615+
throw parseError("GRACE PERIOD cannot be negative", context.interval());
616+
}
617+
gracePeriod = Optional.of(interval);
614618
}
615619

616620
Optional<String> comment = Optional.empty();

core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,6 +5973,10 @@ public void testCreateMaterializedView()
59735973
ImmutableList.of(),
59745974
Optional.empty()));
59755975

5976+
assertThatThrownBy(() -> SQL_PARSER.createStatement("CREATE MATERIALIZED VIEW a GRACE PERIOD INTERVAL -'2' DAY AS SELECT * FROM t"))
5977+
.isInstanceOf(ParsingException.class)
5978+
.hasMessageMatching("line 1:41: GRACE PERIOD cannot be negative");
5979+
59765980
// OR REPLACE, COMMENT, WITH properties
59775981
assertThat(statement(
59785982
"""

testing/trino-testing/src/main/java/io/trino/testing/BaseConnectorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,10 @@ public void testMaterializedViewGracePeriod()
15111511
return;
15121512
}
15131513

1514+
assertQueryFails(
1515+
"CREATE MATERIALIZED VIEW " + viewName + " GRACE PERIOD INTERVAL -'1' HOUR AS SELECT * FROM nation",
1516+
"line 1:71: GRACE PERIOD cannot be negative");
1517+
15141518
try (TestTable table = newTrinoTable(
15151519
"test_base_table",
15161520
"AS TABLE region")) {

0 commit comments

Comments
 (0)