starocean999 commented on code in PR #64026:
URL: https://github.com/apache/doris/pull/64026#discussion_r3418971672
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/PartitionDefinition.java:
##########
@@ -189,12 +200,73 @@ public void setPartitionTypes(List<DataType>
partitionTypes) {
this.partitionTypes = partitionTypes;
}
+ protected void ensurePartitionTypesInitialized() {
+ if (partitionTypes == null) {
+ throw new AnalysisException("partitionTypes should be initialized
before validating partition definition");
+ }
+ }
+
+ protected Expression typedPartitionExpression(Expression expression, int
index) {
+ ensurePartitionTypesInitialized();
+ return strictTypedPartitionExpression(expression,
partitionTypes.get(index));
+ }
+
+ static Expression strictTypedPartitionExpression(Expression expression,
DataType targetType) {
+ if (expression instanceof MaxValue || expression instanceof
MaxLiteral) {
+ return expression;
+ }
+ if (expression.isNullLiteral()) {
+ return expression.checkedCastTo(targetType);
+ }
+ if (!expression.isLiteral()) {
+ throw new AnalysisException("Partition value must be literal: " +
expression.toSql());
+ }
+ String value = ((Literal) expression).getStringValue();
+ try {
+ if (targetType.isDateTimeType() || targetType.isDateTimeV2Type()
|| targetType.isTimeStampTzType()) {
+ return convertPartitionLiteral(value,
targetType).checkedCastTo(targetType);
+ }
+ validateCharacterLength(value, targetType);
+ LiteralExpr typedLiteral = LiteralExprUtils.createLiteral(
+ value, targetType.toCatalogDataType());
+ return Literal.fromLegacyLiteral(typedLiteral,
typedLiteral.getType());
+ } catch (org.apache.doris.common.AnalysisException e) {
+ throw new AnalysisException(e.getMessage(), e);
+ }
+ }
+
+ private static void validateCharacterLength(String value, DataType
targetType) {
+ if (!targetType.isCharType() && !targetType.isVarcharType()) {
+ return;
+ }
+ CharacterType characterType = (CharacterType) targetType;
+ if (characterType.isLengthSet() && value.length() >
characterType.getLen()) {
+ throw new AnalysisException(String.format(
+ "Partition value %s's length exceeds type length: %d > %d
for %s",
+ value, value.length(), characterType.getLen(),
targetType));
+ }
+ }
+
+ private static Literal convertPartitionLiteral(String value, DataType
targetType) {
+ if (targetType.isDateTimeType()) {
+ return new DateTimeLiteral(value);
+ }
+ if (targetType.isDateTimeV2Type()) {
+ return new DateTimeV2Literal(value);
Review Comment:
统一没有使用TYPE,TYPE只是报错。这里是用字符串创建对应类型(忽略目标精度信息),后续CAST到targetType需要一个无损CAST检查才最为合理
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]