Skip to content

Commit 0dbcd6e

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 0dbcd6e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
import io.trino.sql.tree.Identifier;
177177
import io.trino.sql.tree.Insert;
178178
import io.trino.sql.tree.Intersect;
179+
import io.trino.sql.tree.IntervalLiteral;
179180
import io.trino.sql.tree.Join;
180181
import io.trino.sql.tree.JoinCriteria;
181182
import io.trino.sql.tree.JoinOn;
@@ -338,6 +339,7 @@
338339
import static io.trino.spi.StandardErrorCode.INVALID_COPARTITIONING;
339340
import static io.trino.spi.StandardErrorCode.INVALID_DEFAULT_COLUMN_VALUE;
340341
import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
342+
import static io.trino.spi.StandardErrorCode.INVALID_GRACE_PERIOD;
341343
import static io.trino.spi.StandardErrorCode.INVALID_LIMIT_CLAUSE;
342344
import static io.trino.spi.StandardErrorCode.INVALID_ORDER_BY;
343345
import static io.trino.spi.StandardErrorCode.INVALID_PARTITION_BY;
@@ -1478,7 +1480,12 @@ protected Scope visitCreateMaterializedView(CreateMaterializedView node, Optiona
14781480
if (node.isReplace() && node.isNotExists()) {
14791481
throw semanticException(NOT_SUPPORTED, node, "'CREATE OR REPLACE' and 'IF NOT EXISTS' clauses can not be used together");
14801482
}
1481-
node.getGracePeriod().ifPresent(gracePeriod -> analyzeExpression(gracePeriod, Scope.create()));
1483+
node.getGracePeriod().ifPresent(gracePeriod -> {
1484+
if (gracePeriod.getSign() == IntervalLiteral.Sign.NEGATIVE) {
1485+
throw semanticException(INVALID_GRACE_PERIOD, gracePeriod, "Grace period cannot be negative");
1486+
}
1487+
analyzeExpression(gracePeriod, Scope.create());
1488+
});
14821489

14831490
// analyze the query that creates the view
14841491
StatementAnalyzer analyzer = statementAnalyzerFactory.createStatementAnalyzer(analysis, session, warningCollector, CorrelationSupport.ALLOWED);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_REFERENCE;
142142
import static io.trino.spi.StandardErrorCode.INVALID_COPARTITIONING;
143143
import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
144+
import static io.trino.spi.StandardErrorCode.INVALID_GRACE_PERIOD;
144145
import static io.trino.spi.StandardErrorCode.INVALID_LABEL;
145146
import static io.trino.spi.StandardErrorCode.INVALID_LIMIT_CLAUSE;
146147
import static io.trino.spi.StandardErrorCode.INVALID_LITERAL;
@@ -5833,6 +5834,15 @@ public void testAnalyzeMaterializedViewWithAccessControl()
58335834
.hasMessage("Access Denied: Cannot select from columns [a, b] in table or view tpch.s1.fresh_materialized_view");
58345835
}
58355836

5837+
@Test
5838+
public void testCreateMaterializedViewWithNegativeGracePeriod()
5839+
{
5840+
assertFails("CREATE MATERIALIZED VIEW mv_negative_grace_period GRACE PERIOD INTERVAL -'1' HOUR AS SELECT * FROM nation")
5841+
.hasErrorCode(INVALID_GRACE_PERIOD)
5842+
.hasMessage("line 1:64: Grace period cannot be negative")
5843+
.hasLocation(1, 64);
5844+
}
5845+
58365846
@Test
58375847
public void testJsonContextItemType()
58385848
{

core/trino-spi/src/main/java/io/trino/spi/StandardErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public enum StandardErrorCode
159159
BRANCH_ALREADY_EXISTS(135, USER_ERROR),
160160
INVALID_BRANCH_PROPERTY(136, USER_ERROR),
161161
INVALID_DEFAULT_COLUMN_VALUE(137, USER_ERROR),
162+
INVALID_GRACE_PERIOD(138, USER_ERROR),
162163

163164
GENERIC_INTERNAL_ERROR(65536, INTERNAL_ERROR),
164165
TOO_MANY_REQUESTS_FAILED(65537, INTERNAL_ERROR),

0 commit comments

Comments
 (0)