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

zhangstar333 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new c4e61fb52c1 [Enhancement](ctas) Support create auto range partition 
table with CTAS command (#48931)
c4e61fb52c1 is described below

commit c4e61fb52c1175d3dd35d380a8eb55294d099564
Author: zclllyybb <zhaochan...@selectdb.com>
AuthorDate: Tue Mar 25 10:20:10 2025 +0800

    [Enhancement](ctas) Support create auto range partition table with CTAS 
command (#48931)
    
    ### What problem does this PR solve?
    Support create auto range partition table with CTAS command
---
 .../trees/plans/commands/CreateTableCommand.java   | 25 +++++-
 .../plans/commands/info/PartitionTableInfo.java    |  5 ++
 .../create_table/test_ctas_auto_partition.groovy   | 94 ++++++++++++++++++++++
 3 files changed, 122 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
index 865ce6e243a..f76d272aded 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateTableCommand.java
@@ -109,6 +109,7 @@ public class CreateTableCommand extends Command implements 
NeedAuditEncryption,
         if (slots.size() != ctasCols.size()) {
             throw new AnalysisException("ctas column size is not equal to the 
query's");
         }
+        String autoRangePartitionName = getAutoRangePartitionNameOrNull();
         ImmutableList.Builder<ColumnDefinition> columnsOfQuery = 
ImmutableList.builder();
         for (int i = 0; i < slots.size(); i++) {
             Slot s = slots.get(i);
@@ -154,8 +155,14 @@ public class CreateTableCommand extends Command implements 
NeedAuditEncryption,
                     }
                 }
             }
-            // if the column is an expression, we set it to nullable, 
otherwise according to the nullable of the slot.
-            columnsOfQuery.add(new ColumnDefinition(s.getName(), dataType, 
!s.isColumnFromTable() || s.nullable()));
+            if (autoRangePartitionName != null && 
autoRangePartitionName.equalsIgnoreCase(s.getName())) {
+                // for auto range partition column, it must be not nullable. 
so keep its origin.
+                columnsOfQuery.add(new ColumnDefinition(s.getName(), dataType, 
s.nullable()));
+            } else {
+                // if the column is an expression, we set it to nullable, 
otherwise according to the nullable of the
+                // slot.
+                columnsOfQuery.add(new ColumnDefinition(s.getName(), dataType, 
!s.isColumnFromTable() || s.nullable()));
+            }
         }
         List<String> qualifierTableName = RelationUtil.getQualifierName(ctx, 
createTableInfo.getTableNameParts());
         createTableInfo.validateCreateTableAsSelect(qualifierTableName, 
columnsOfQuery.build(), ctx);
@@ -199,6 +206,20 @@ public class CreateTableCommand extends Command implements 
NeedAuditEncryption,
         }
     }
 
+    private String getAutoRangePartitionNameOrNull() {
+        try {
+            if (createTableInfo.getPartitionTableInfo().isAutoPartition()
+                    && 
createTableInfo.getPartitionTableInfo().getPartitionType().equalsIgnoreCase("RANGE"))
 {
+                // should collect first before use them.
+                
createTableInfo.getPartitionTableInfo().extractPartitionColumns();
+                return 
createTableInfo.getPartitionTableInfo().getIdentifierPartitionColumns().get(0);
+            }
+        } catch (Exception e) {
+            return null;
+        }
+        return null;
+    }
+
     public boolean isCtasCommand() {
         return ctasQuery.isPresent();
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/PartitionTableInfo.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/PartitionTableInfo.java
index 7d85d24f6cb..48002bdfb65 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/PartitionTableInfo.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/PartitionTableInfo.java
@@ -329,6 +329,7 @@ public class PartitionTableInfo {
         try {
             identifierPartitionColumns = 
PartitionDesc.getColNamesFromExpr(exprs,
                     partitionType.equalsIgnoreCase(PartitionType.LIST.name()), 
isAutoPartition);
+            // check of it will be done in validatePartitionInfo later.
         } catch (Exception e) {
             throw new AnalysisException(e.getMessage(), e.getCause());
         }
@@ -337,4 +338,8 @@ public class PartitionTableInfo {
     public boolean inIdentifierPartitions(String columnName) {
         return identifierPartitionColumns != null && 
identifierPartitionColumns.contains(columnName);
     }
+
+    public List<String> getIdentifierPartitionColumns() {
+        return identifierPartitionColumns;
+    }
 }
diff --git 
a/regression-test/suites/nereids_p0/create_table/test_ctas_auto_partition.groovy
 
b/regression-test/suites/nereids_p0/create_table/test_ctas_auto_partition.groovy
new file mode 100644
index 00000000000..67367159c60
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/create_table/test_ctas_auto_partition.groovy
@@ -0,0 +1,94 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_ctas_auto_partition") {
+    sql "drop table if exists test_partition__dbt_tmp3"
+    test {
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select nullable(current_date()) as today;
+        """
+        exception "AUTO RANGE PARTITION doesn't support NULL column"
+    }
+
+    test {
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select cast(null as date) as today;
+        """
+        exception "AUTO RANGE PARTITION doesn't support NULL column"
+    }
+
+    test {
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select 1 as today;
+        """
+        exception "partition expr date_trunc is illegal!"
+    }
+
+    test{
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day'), 
date_trunc(`today1`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select 1 as today;
+        """
+        exception "auto create partition only support one slotRef in function 
expr"
+    }
+
+    test{
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select current_date() as today, current_date() + 1 as today;
+        """
+        exception "Duplicate column name 'today'"
+    }
+
+    test{
+        sql """
+            create table `test_partition__dbt_tmp3`
+            AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+            PROPERTIES ("replication_num" = "1" )
+            as
+            select current_date() as today2, current_date() + 1 as today1;
+        """
+        exception "partition key today is not exists"
+    }
+
+    sql """
+        create table `test_partition__dbt_tmp3`
+        AUTO PARTITION BY RANGE (date_trunc(`today`, 'day')) ()
+        PROPERTIES ("replication_num" = "1" )
+        as
+        select 1 as id, 'cyril' as name, 18 as age, 100 as score, 
current_date() as today;
+    """
+    assertEquals((sql "select count() from test_partition__dbt_tmp3")[0][0], 1)
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to