This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new e6957e29f14 [Enhancement] (branch-2.0) when partition column is 
datetime, date can work in create table command (#32844)
e6957e29f14 is described below

commit e6957e29f14260d20c3b556f6789342c8f297884
Author: Jensen <[email protected]>
AuthorDate: Mon Apr 15 17:45:14 2024 +0800

    [Enhancement] (branch-2.0) when partition column is datetime, date can work 
in create table command (#32844)
---
 .../org/apache/doris/catalog/PartitionKey.java     | 22 +++++++++++-
 .../multi_partition/test_multi_partition.groovy    | 41 +++++++++++++++++++++-
 .../multi_partition/test_range_partition.groovy    | 28 +++++++++++++++
 .../test_partition_table_err_msg.groovy            |  5 ++-
 4 files changed, 91 insertions(+), 5 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/catalog/PartitionKey.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/PartitionKey.java
index 4e273de373c..1f07be566de 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/PartitionKey.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/PartitionKey.java
@@ -29,6 +29,9 @@ import org.apache.doris.common.AnalysisException;
 import org.apache.doris.common.io.Text;
 import org.apache.doris.common.io.Writable;
 import org.apache.doris.datasource.hive.HiveMetaStoreCache;
+import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
 
 import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
@@ -89,7 +92,14 @@ public class PartitionKey implements 
Comparable<PartitionKey>, Writable {
         Preconditions.checkArgument(keys.size() <= columns.size());
         int i;
         for (i = 0; i < keys.size(); ++i) {
-            
partitionKey.keys.add(keys.get(i).getValue(columns.get(i).getType()));
+            Type keyType = columns.get(i).getType();
+            // If column type is datatime and key type is date, we should 
convert date to datetime.
+            if (keyType.isDatetime() || keyType.isDatetimeV2()) {
+                Literal dateTimeLiteral = 
getDateTimeLiteral(keys.get(i).getStringValue(), keyType);
+                partitionKey.keys.add(dateTimeLiteral.toLegacyLiteral());
+            } else {
+                partitionKey.keys.add(keys.get(i).getValue(keyType));
+            }
             partitionKey.types.add(columns.get(i).getDataType());
         }
 
@@ -103,6 +113,16 @@ public class PartitionKey implements 
Comparable<PartitionKey>, Writable {
         return partitionKey;
     }
 
+    private static Literal getDateTimeLiteral(String value, Type type) throws 
AnalysisException {
+        if (type.isDatetime()) {
+            return new DateTimeLiteral(value);
+        } else if (type.isDatetimeV2()) {
+            return new DateTimeV2Literal(value);
+        }
+        throw new AnalysisException("date convert to datetime failed, "
+                + "value is [" + value + "], type is [" + type + "].");
+    }
+
     public static PartitionKey 
createListPartitionKeyWithTypes(List<PartitionValue> values, List<Type> types,
             boolean isHive)
             throws AnalysisException {
diff --git 
a/regression-test/suites/partition_p0/multi_partition/test_multi_partition.groovy
 
b/regression-test/suites/partition_p0/multi_partition/test_multi_partition.groovy
index c0deb9146ed..69b1242b5cb 100644
--- 
a/regression-test/suites/partition_p0/multi_partition/test_multi_partition.groovy
+++ 
b/regression-test/suites/partition_p0/multi_partition/test_multi_partition.groovy
@@ -453,4 +453,43 @@ suite("test_multi_partition") {
     assertEquals(result2.size(), 45)
     sql "drop table multi_par12"
 
-}
\ No newline at end of file
+
+    // create one table without datetime partition, but with date string
+    sql """set enable_fallback_to_original_planner=false"""
+    sql """
+        CREATE TABLE IF NOT EXISTS range_date_cast_to_datetime ( 
+            id int,
+            name string,
+            pdate DATETIME ) 
+        PARTITION BY RANGE(pdate)(
+            FROM ("2023-04-16") TO ("2023-04-20") INTERVAL 1 DAY
+        )
+        DISTRIBUTED BY HASH(id) BUCKETS 1 properties("replication_num" = "1")
+        """
+    result1  = sql "show tables like 'range_date_cast_to_datetime'"
+    logger.info("${result1}")
+    assertEquals(result1.size(), 1)
+    result2  = sql "show partitions from range_date_cast_to_datetime"
+    logger.info("${result2}")
+    assertEquals(result2.size(), 4)
+    sql "drop table range_date_cast_to_datetime"
+
+    sql """set enable_fallback_to_original_planner=true"""
+    sql """
+        CREATE TABLE IF NOT EXISTS range_date_cast_to_datetime ( 
+            id int,
+            name string,
+            pdate DATETIME ) 
+        PARTITION BY RANGE(pdate)(
+            FROM ("2023-04-16") TO ("2023-04-20") INTERVAL 1 DAY
+        )
+        DISTRIBUTED BY HASH(id) BUCKETS 1 properties("replication_num" = "1")
+        """
+    result1  = sql "show tables like 'range_date_cast_to_datetime'"
+    logger.info("${result1}")
+    assertEquals(result1.size(), 1)
+    result2  = sql "show partitions from range_date_cast_to_datetime"
+    logger.info("${result2}")
+    assertEquals(result2.size(), 4)
+    sql "drop table range_date_cast_to_datetime"
+}
diff --git 
a/regression-test/suites/partition_p0/multi_partition/test_range_partition.groovy
 
b/regression-test/suites/partition_p0/multi_partition/test_range_partition.groovy
index 64f5deff4a8..c9e75718e38 100644
--- 
a/regression-test/suites/partition_p0/multi_partition/test_range_partition.groovy
+++ 
b/regression-test/suites/partition_p0/multi_partition/test_range_partition.groovy
@@ -296,4 +296,32 @@ suite("test_range_partition", "p0") {
             "DISTRIBUTED BY RANDOM BUCKETS 13"
     )
 
+    // create one table without datetime partition, but with date string
+    sql """set enable_fallback_to_original_planner=false"""
+    sql """
+        CREATE TABLE IF NOT EXISTS range_date_cast_to_datetime ( 
+            id int,
+            name string,
+            pdate DATETIME ) 
+        PARTITION BY RANGE(pdate)(
+            PARTITION pd20230418 VALUES less than ("2023-04-20")
+        )
+        DISTRIBUTED BY HASH(id) BUCKETS 1 properties("replication_num" = "1")
+        """
+    sql "insert into range_date_cast_to_datetime values (1, 'name', 
'2023-04-19 08:08:30')"
+    sql "drop table range_date_cast_to_datetime"
+
+    sql """set enable_fallback_to_original_planner=true"""
+    sql """
+        CREATE TABLE IF NOT EXISTS range_date_cast_to_datetime ( 
+            id int,
+            name string,
+            pdate DATETIME ) 
+        PARTITION BY RANGE(pdate)(
+            PARTITION pd20230418 VALUES less than ("2023-04-20")
+        )
+        DISTRIBUTED BY HASH(id) BUCKETS 1 properties("replication_num" = "1")
+        """
+    sql "insert into range_date_cast_to_datetime values (1, 'name', 
'2023-04-19 08:08:30')"
+    sql "drop table range_date_cast_to_datetime"
 }
diff --git 
a/regression-test/suites/partition_p0/test_partition_table_err_msg.groovy 
b/regression-test/suites/partition_p0/test_partition_table_err_msg.groovy
index e900c24b10f..7099e967822 100644
--- a/regression-test/suites/partition_p0/test_partition_table_err_msg.groovy
+++ b/regression-test/suites/partition_p0/test_partition_table_err_msg.groovy
@@ -72,7 +72,7 @@ suite("test_partition_table_err_msg", "p0") {
               PARTITION partition_d VALUES LESS THAN ("10000-01-01 00:00:00") 
) 
             DISTRIBUTED BY HASH(k1) BUCKETS 13
         """
-        exception "date literal [10000-01-01 00:00:00] is invalid"
+        exception "date/datetime literal [10000-01-01 00:00:00] is invalid"
     }
     test {
         sql """
@@ -91,8 +91,7 @@ suite("test_partition_table_err_msg", "p0") {
               PARTITION partition_d VALUES LESS THAN ("9999-12-31 24:00:00") ) 
             DISTRIBUTED BY HASH(k1) BUCKETS 13
         """
-        exception "date literal [9999-12-31 24:00:00] is invalid: Text 
'9999-12-31 24:00:00' could not be parsed: " +
-                "Invalid value for HourOfDay (valid values 0 - 23): 24"
+        exception "date/datetime literal [9999-12-31 24:00:00] is invalid"
     }
     test {
         sql """


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to