[Feat](nereids) support column default value current_date (#32268)

This commit is contained in:
feiniaofeiafei
2024-03-15 14:11:29 +08:00
committed by yiguolei
parent 42ccc5489e
commit afa9f6e5d6
6 changed files with 115 additions and 4 deletions

View File

@ -93,6 +93,7 @@ public class ColumnDef {
this.defaultValueExprDef = new DefaultValueExprDef(exprName, precision);
}
public static String CURRENT_DATE = "CURRENT_DATE";
// default "CURRENT_TIMESTAMP", only for DATETIME type
public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
public static String NOW = "now";
@ -466,7 +467,15 @@ public class ColumnDef {
break;
case DATE:
case DATEV2:
new DateLiteral(defaultValue, scalarType);
if (defaultValueExprDef == null) {
new DateLiteral(defaultValue, scalarType);
} else {
if (defaultValueExprDef.getExprName().equalsIgnoreCase(DefaultValue.CURRENT_DATE)) {
break;
} else {
throw new AnalysisException("date literal [" + defaultValue + "] is invalid");
}
}
break;
case DATETIME:
case DATETIMEV2:
@ -520,6 +529,16 @@ public class ColumnDef {
throw new AnalysisException("Types other than DATETIME and DATETIMEV2 "
+ "cannot use current_timestamp as the default value");
}
} else if (null != defaultValueExprDef
&& defaultValueExprDef.getExprName().equals(DefaultValue.CURRENT_DATE.toLowerCase())) {
switch (primitiveType) {
case DATE:
case DATEV2:
break;
default:
throw new AnalysisException("Types other than DATE and DATEV2 "
+ "cannot use current_date as the default value");
}
}
}

View File

@ -2539,7 +2539,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
defaultValue = Optional.of(new DefaultValue(toStringValue(ctx.stringValue.getText())));
} else if (ctx.nullValue != null) {
defaultValue = Optional.of(DefaultValue.NULL_DEFAULT_VALUE);
} else if (ctx.CURRENT_TIMESTAMP() != null) {
} else if (ctx.defaultTimestamp != null) {
if (ctx.defaultValuePrecision == null) {
defaultValue = Optional.of(DefaultValue.CURRENT_TIMESTAMP_DEFAULT_VALUE);
} else {
@ -2547,6 +2547,8 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
.currentTimeStampDefaultValueWithPrecision(
Long.valueOf(ctx.defaultValuePrecision.getText())));
}
} else if (ctx.CURRENT_DATE() != null) {
defaultValue = Optional.of(DefaultValue.CURRENT_DATE_DEFAULT_VALUE);
}
}
if (ctx.UPDATE() != null) {

View File

@ -24,8 +24,10 @@ import org.apache.doris.catalog.ScalarType;
* default value of a column.
*/
public class DefaultValue {
public static String CURRENT_DATE = "CURRENT_DATE";
public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
public static String NOW = "now";
public static DefaultValue CURRENT_DATE_DEFAULT_VALUE = new DefaultValue(CURRENT_DATE, CURRENT_DATE.toLowerCase());
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(CURRENT_TIMESTAMP, NOW);
// default null
public static DefaultValue NULL_DEFAULT_VALUE = new DefaultValue(null);