[Enhancement] when partition column is datetime, date can work in create table command (#32335)

This commit is contained in:
chen
2024-03-20 21:55:20 +08:00
committed by yiguolei
parent 7486e96b12
commit 085696744d
5 changed files with 107 additions and 20 deletions

View File

@ -29,6 +29,9 @@ import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
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 org.apache.doris.qe.SessionVariable;
import com.google.common.base.Joiner;
@ -90,7 +93,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());
}
@ -104,6 +114,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 {