[Enhancement](function) Support date_trunc(date) and use it in auto partition (#24341)
Support date_trunc(date) and use it in auto partition
This commit is contained in:
@ -314,7 +314,7 @@ public class IntLiteral extends LiteralExpr {
|
||||
return res;
|
||||
}
|
||||
return this;
|
||||
} else if (targetType.isDateLike()) {
|
||||
} else if (targetType.isDateType()) {
|
||||
try {
|
||||
//int like 20200101 can be cast to date(2020,01,01)
|
||||
DateLiteral res = new DateLiteral("" + value, targetType);
|
||||
|
||||
@ -198,9 +198,10 @@ public class PartitionDesc {
|
||||
}
|
||||
if (this instanceof RangePartitionDesc && partitionExprs != null) {
|
||||
if (partitionExprs.get(0) instanceof FunctionCallExpr) {
|
||||
if (!columnDef.getType().isDatetime() && !columnDef.getType().isDatetimeV2()) {
|
||||
if (!columnDef.getType().isDateType()) {
|
||||
throw new AnalysisException(
|
||||
"auto create partition function expr need datetime/datetimev2 type. "
|
||||
"Auto range partition needs Date/DateV2/"
|
||||
+ "Datetime/DatetimeV2 column as partition column"
|
||||
+ partitionExprs.get(0).toSql());
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@ public class PartitionExprUtil {
|
||||
filterPartitionValues.add(value);
|
||||
if (partitionType == PartitionType.RANGE) {
|
||||
String beginTime = value;
|
||||
DateLiteral beginDateTime = new DateLiteral(beginTime, Type.DATETIMEV2);
|
||||
DateLiteral beginDateTime = new DateLiteral(beginTime, partitionColumnType);
|
||||
partitionName += String.format(DATETIME_NAME_FORMATTER,
|
||||
beginDateTime.getYear(), beginDateTime.getMonth(), beginDateTime.getDay(),
|
||||
beginDateTime.getHour(), beginDateTime.getMinute(), beginDateTime.getSecond());
|
||||
|
||||
@ -320,6 +320,16 @@ public class DateTimeExtractAndTransform {
|
||||
return DateTimeV2Literal.fromJavaDateType(dateTruncHelper(date.toJavaDateType(), trunc.getValue()));
|
||||
}
|
||||
|
||||
@ExecFunction(name = "date_trunc", argTypes = { "DATE", "VARCHAR" }, returnType = "DATE")
|
||||
public static Expression dateTrunc(DateLiteral date, VarcharLiteral trunc) {
|
||||
return DateLiteral.fromJavaDateType(dateTruncHelper(date.toJavaDateType(), trunc.getValue()));
|
||||
}
|
||||
|
||||
@ExecFunction(name = "date_trunc", argTypes = { "DATEV2", "VARCHAR" }, returnType = "DATEV2")
|
||||
public static Expression dateTrunc(DateV2Literal date, VarcharLiteral trunc) {
|
||||
return DateV2Literal.fromJavaDateType(dateTruncHelper(date.toJavaDateType(), trunc.getValue()));
|
||||
}
|
||||
|
||||
private static LocalDateTime dateTruncHelper(LocalDateTime dateTime, String trunc) {
|
||||
int year = dateTime.getYear();
|
||||
int month = dateTime.getMonthValue();
|
||||
|
||||
@ -27,6 +27,8 @@ import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
|
||||
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
|
||||
import org.apache.doris.nereids.types.DateTimeType;
|
||||
import org.apache.doris.nereids.types.DateTimeV2Type;
|
||||
import org.apache.doris.nereids.types.DateType;
|
||||
import org.apache.doris.nereids.types.DateV2Type;
|
||||
import org.apache.doris.nereids.types.VarcharType;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
@ -44,7 +46,10 @@ public class DateTrunc extends ScalarFunction
|
||||
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
|
||||
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT)
|
||||
.args(DateTimeV2Type.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT),
|
||||
FunctionSignature.ret(DateTimeType.INSTANCE).args(DateTimeType.INSTANCE, VarcharType.SYSTEM_DEFAULT)
|
||||
FunctionSignature.ret(DateTimeType.INSTANCE).args(DateTimeType.INSTANCE, VarcharType.SYSTEM_DEFAULT),
|
||||
FunctionSignature.ret(DateV2Type.INSTANCE)
|
||||
.args(DateV2Type.INSTANCE, VarcharType.SYSTEM_DEFAULT),
|
||||
FunctionSignature.ret(DateType.INSTANCE).args(DateType.INSTANCE, VarcharType.SYSTEM_DEFAULT)
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
@ -523,8 +523,8 @@ public class FEFunctions {
|
||||
}
|
||||
|
||||
@FEFunction(name = "date_trunc", argTypes = {"DATETIME", "VARCHAR"}, returnType = "DATETIME")
|
||||
public static DateLiteral dateTrunc(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateLike()) {
|
||||
public static DateLiteral dateTruncDatetime(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateType()) {
|
||||
DateLiteral dateLiteral = ((DateLiteral) date);
|
||||
LocalDateTime localDate = dateTruncHelper(LocalDateTime.of(
|
||||
(int) dateLiteral.getYear(), (int) dateLiteral.getMonth(), (int) dateLiteral.getDay(),
|
||||
@ -538,8 +538,8 @@ public class FEFunctions {
|
||||
}
|
||||
|
||||
@FEFunction(name = "date_trunc", argTypes = {"DATETIMEV2", "VARCHAR"}, returnType = "DATETIMEV2")
|
||||
public static DateLiteral dateTruncV2(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateLike()) {
|
||||
public static DateLiteral dateTruncDatetimeV2(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateType()) {
|
||||
DateLiteral dateLiteral = ((DateLiteral) date);
|
||||
LocalDateTime localDate = dateTruncHelper(LocalDateTime.of(
|
||||
(int) dateLiteral.getYear(), (int) dateLiteral.getMonth(), (int) dateLiteral.getDay(),
|
||||
@ -552,6 +552,34 @@ public class FEFunctions {
|
||||
return null;
|
||||
}
|
||||
|
||||
@FEFunction(name = "date_trunc", argTypes = { "DATE", "VARCHAR" }, returnType = "DATE")
|
||||
public static DateLiteral dateTruncDate(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateType()) {
|
||||
DateLiteral dateLiteral = ((DateLiteral) date);
|
||||
LocalDateTime localDate = dateTruncHelper(LocalDateTime.of(
|
||||
(int) dateLiteral.getYear(), (int) dateLiteral.getMonth(), (int) dateLiteral.getDay(), 0, 0, 0),
|
||||
truncate.getStringValue());
|
||||
|
||||
return new DateLiteral(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth(),
|
||||
localDate.getHour(), localDate.getMinute(), localDate.getSecond(), date.getType());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@FEFunction(name = "date_trunc", argTypes = { "DATEV2", "VARCHAR" }, returnType = "DATEV2")
|
||||
public static DateLiteral dateTruncDateV2(LiteralExpr date, LiteralExpr truncate) {
|
||||
if (date.getType().isDateType()) {
|
||||
DateLiteral dateLiteral = ((DateLiteral) date);
|
||||
LocalDateTime localDate = dateTruncHelper(LocalDateTime.of(
|
||||
(int) dateLiteral.getYear(), (int) dateLiteral.getMonth(), (int) dateLiteral.getDay(), 0, 0, 0),
|
||||
truncate.getStringValue());
|
||||
|
||||
return new DateLiteral(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth(),
|
||||
localDate.getHour(), localDate.getMinute(), localDate.getSecond(), date.getType());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static LocalDateTime dateTruncHelper(LocalDateTime dateTime, String trunc) {
|
||||
int year = dateTime.getYear();
|
||||
int month = dateTime.getMonthValue();
|
||||
|
||||
Reference in New Issue
Block a user