From 9c0d29f0adcad49d8775defe02ec15cc416a8a50 Mon Sep 17 00:00:00 2001 From: meegoo Date: Tue, 19 Nov 2024 21:02:03 -0800 Subject: [PATCH] [Enhancement] Support partition by minute granularity time_slice (#52996) Signed-off-by: meegoo --- .../com/starrocks/sql/analyzer/AnalyzerUtils.java | 11 ++++++++--- .../R/test_automatic_partition | 12 ++++++++++++ .../T/test_automatic_partition | 6 ++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AnalyzerUtils.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AnalyzerUtils.java index 5074617228acf..8b241cde7a8b3 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AnalyzerUtils.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AnalyzerUtils.java @@ -149,7 +149,7 @@ public class AnalyzerUtils { ImmutableSet.of("hour", "day", "month", "year"); // The partition format supported by time_slice public static final Set TIME_SLICE_SUPPORTED_PARTITION_FORMAT = - ImmutableSet.of("hour", "day", "month", "year"); + ImmutableSet.of("minute", "hour", "day", "month", "year"); // The partition format supported by mv date_trunc public static final Set MV_DATE_TRUNC_SUPPORTED_PARTITION_FORMAT = ImmutableSet.of("hour", "day", "week", "month", "year"); @@ -1426,6 +1426,11 @@ private static AddPartitionClause getAddPartitionClauseForRangePartition( // The start date here is passed by BE through function calculation, // so it must be the start date of a certain partition. switch (granularity.toLowerCase()) { + case "minute": + beginTime = beginTime.withSecond(0).withNano(0); + partitionName = partitionPrefix + beginTime.format(DateUtils.MINUTE_FORMATTER_UNIX); + endTime = beginTime.plusMinutes(interval); + break; case "hour": beginTime = beginTime.withMinute(0).withSecond(0).withNano(0); partitionName = partitionPrefix + beginTime.format(DateUtils.HOUR_FORMATTER_UNIX); @@ -1760,14 +1765,14 @@ public static List checkAndExtractPartitionCol(FunctionCallExpr expr, Li private static void checkPartitionColumnTypeValid(FunctionCallExpr expr, List columnDefs, NodePosition pos, String partitionColumnName, String fmt) { // For materialized views currently columnDefs == null - if (columnDefs != null && "hour".equalsIgnoreCase(fmt)) { + if (columnDefs != null && ("hour".equalsIgnoreCase(fmt) || "minute".equalsIgnoreCase(fmt))) { ColumnDef partitionDef = findPartitionDefByName(columnDefs, partitionColumnName); if (partitionDef == null) { throw new ParsingException(PARSER_ERROR_MSG.unsupportedExprWithInfo(expr.toSql(), "PARTITION BY"), pos); } if (partitionDef.getType() != Type.DATETIME) { throw new ParsingException(PARSER_ERROR_MSG.unsupportedExprWithInfoAndExplain(expr.toSql(), - "PARTITION BY", "The hour parameter only supports datetime type"), pos); + "PARTITION BY", "The hour/minute parameter only supports datetime type"), pos); } } } diff --git a/test/sql/test_automatic_partition/R/test_automatic_partition b/test/sql/test_automatic_partition/R/test_automatic_partition index 3f16b07fd8fa8..1fef639cd8383 100644 --- a/test/sql/test_automatic_partition/R/test_automatic_partition +++ b/test/sql/test_automatic_partition/R/test_automatic_partition @@ -639,3 +639,15 @@ select * from ss; 2002-01-07 1 2 2002-01-06 1 2 -- !result + +-- name: test_time_slice_with_minute_granularity +create table tt(k datetime) partition by time_slice(k, interval 10 minute); +-- result: +-- !result +insert into tt values(now()); +-- result: +-- !result +select count(*) from tt; +-- result: +1 +-- !result diff --git a/test/sql/test_automatic_partition/T/test_automatic_partition b/test/sql/test_automatic_partition/T/test_automatic_partition index bbfc506c4744a..312dc94781d63 100644 --- a/test/sql/test_automatic_partition/T/test_automatic_partition +++ b/test/sql/test_automatic_partition/T/test_automatic_partition @@ -200,3 +200,9 @@ insert into ss values('2002-01-08', 1, 2); select sleep(3); function: wait_alter_table_not_pending() select * from ss; + + +-- name: test_time_slice_with_minute_granularity +create table tt(k datetime) partition by time_slice(k, interval 10 minute); +insert into tt values(now()); +select count(*) from tt;