Skip to content

Commit

Permalink
fix default value
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 committed Dec 6, 2024
1 parent 811f264 commit 922404c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ private void validateNestedType(Type parent, Type child) throws AnalysisExceptio
*/
public Column translateToCatalogStyle() {
Column column = new Column(name, type.toCatalogDataType(), isKey, aggType, isNullable,
autoIncInitValue, defaultValue.map(DefaultValue::getRawValue).orElse(null), comment, isVisible,
autoIncInitValue, defaultValue.map(DefaultValue::getValue).orElse(null), comment, isVisible,
defaultValue.map(DefaultValue::getDefaultValueExprDef).orElse(null), Column.COLUMN_UNIQUE_ID_INIT_VALUE,
defaultValue.map(DefaultValue::getValue).orElse(null), onUpdateDefaultValue.isPresent(),
defaultValue.map(DefaultValue::getRawValue).orElse(null), onUpdateDefaultValue.isPresent(),
onUpdateDefaultValue.map(DefaultValue::getDefaultValueExprDef).orElse(null), clusterKeyId,
generatedColumnDesc.map(GeneratedColumnDesc::translateToInfo).orElse(null),
generatedColumnsThatReferToThis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

import org.apache.doris.analysis.DefaultValueExprDef;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.util.TimeUtils;
import org.apache.doris.nereids.exceptions.AnalysisException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* default value of a column.
*/
Expand Down Expand Up @@ -90,7 +94,8 @@ public static DefaultValue currentTimeStampDefaultValueWithPrecision(Long precis
}

public boolean isCurrentTimeStamp() {
return "CURRENT_TIMESTAMP".equals(value) && NOW.equals(defaultValueExprDef.getExprName());
return "CURRENT_TIMESTAMP".equals(value) && defaultValueExprDef != null
&& NOW.equals(defaultValueExprDef.getExprName());
}

public boolean isCurrentTimeStampWithPrecision() {
Expand All @@ -105,7 +110,33 @@ public long getCurrentTimeStampPrecision() {
return 0;
}

/**
* get string result of a default value expression.
*/
public String getRawValue() {
if (isCurrentTimeStamp()) {
return LocalDateTime.now(TimeUtils.getTimeZone().toZoneId()).toString().replace('T', ' ');
} else if (isCurrentTimeStampWithPrecision()) {
long precision = getCurrentTimeStampPrecision();
String format = "yyyy-MM-dd HH:mm:ss";
if (precision == 0) {
return LocalDateTime.now(TimeUtils.getTimeZone().toZoneId()).toString().replace('T', ' ');
} else if (precision == 1) {
format = "yyyy-MM-dd HH:mm:ss.S";
} else if (precision == 2) {
format = "yyyy-MM-dd HH:mm:ss.SS";
} else if (precision == 3) {
format = "yyyy-MM-dd HH:mm:ss.SSS";
} else if (precision == 4) {
format = "yyyy-MM-dd HH:mm:ss.SSSS";
} else if (precision == 5) {
format = "yyyy-MM-dd HH:mm:ss.SSSSS";
} else if (precision == 6) {
format = "yyyy-MM-dd HH:mm:ss.SSSSSS";
}
return LocalDateTime.now(TimeUtils.getTimeZone().toZoneId())
.format(DateTimeFormatter.ofPattern(format));
}
return value;
}

Expand Down

0 comments on commit 922404c

Please sign in to comment.