Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement](ctas) Support create auto range partition table with CTAS command #48931

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
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);
Expand Down Expand Up @@ -154,8 +155,14 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
}
}
}
// 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);
Expand Down Expand Up @@ -199,6 +206,20 @@ void handleFallbackFailedCtas(ConnectContext ctx) {
}
}

private String getAutoRangePartitionNameOrNull() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe return a List add process manual range partition too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this restriction(should be not nullable) is only for auto range partition column. normal range partition column could be nullable when allow_partition_column_nullable = true. so maybe it's not necessary?

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public void extractPartitionColumns() throws AnalysisException {
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());
}
Expand All @@ -337,4 +338,8 @@ public void extractPartitionColumns() throws AnalysisException {
public boolean inIdentifierPartitions(String columnName) {
return identifierPartitionColumns != null && identifierPartitionColumns.contains(columnName);
}

public List<String> getIdentifierPartitionColumns() {
return identifierPartitionColumns;
}
}
Original file line number Diff line number Diff line change
@@ -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)
}