Skip to content

Commit

Permalink
[fix](sql_cache) fix sql cache result wrong of from_unixtime(col, 'yy…
Browse files Browse the repository at this point in the history
…yy-MM-dd HH:mm:ss') (#44631)

fix sql cache result wrong of from_unixtime(col, 'yyyy-MM-dd HH:mm:ss')
which introduced by #33262

the wrong result: is `yyyy-MM-dd HH:mm:ss`

(cherry picked from commit d592e27)
  • Loading branch information
924060929 committed Nov 30, 2024
1 parent 88926d2 commit dc1eec7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private static Expression translateJavaFormatter(Expression function, int format
return function;
}

private static Expression translateJavaFormatter(Expression formatterExpr) {
/** translateJavaFormatter */
public static Expression translateJavaFormatter(Expression formatterExpr) {
if (formatterExpr.isLiteral() && formatterExpr.getDataType().isStringLikeType()) {
Literal literal = (Literal) formatterExpr;
String originFormatter = literal.getStringValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Type;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.rules.expression.rules.SupportJavaDateFormatter;
import org.apache.doris.nereids.trees.expressions.ExecFunction;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
Expand Down Expand Up @@ -293,25 +294,29 @@ private static LocalDateTime firstDayOfWeek(LocalDateTime dateTime) {
*/
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}

@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
Expand Down Expand Up @@ -479,6 +484,8 @@ public static Expression fromUnixTime(BigIntLiteral second) {
*/
@ExecFunction(name = "from_unixtime")
public static Expression fromUnixTime(BigIntLiteral second, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);

// 32536771199L is max valid timestamp of mysql from_unix_time
if (second.getValue() < 0 || second.getValue() > 32536771199L) {
return new NullLiteral(VarcharType.SYSTEM_DEFAULT);
Expand Down Expand Up @@ -531,6 +538,7 @@ public static Expression unixTimestamp(DateTimeV2Literal date) {
*/
@ExecFunction(name = "unix_timestamp")
public static Expression unixTimestamp(StringLikeLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
DateTimeFormatter formatter = DateUtils.formatBuilder(format.getValue()).toFormatter();
LocalDateTime dateObj;
try {
Expand Down Expand Up @@ -616,6 +624,7 @@ public static Expression makeDate(IntegerLiteral year, IntegerLiteral dayOfYear)
*/
@ExecFunction(name = "str_to_date")
public static Expression strToDate(StringLikeLiteral str, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
if (org.apache.doris.analysis.DateLiteral.hasTimePart(format.getStringValue())) {
DataType returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME));
if (returnType instanceof DateTimeV2Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ public enum IgnoreSplitType {
+ "which can improve query concurrency. default is false."})
public boolean enableScanRunSerial = false;

@VariableMgr.VarAttr(name = ENABLE_SQL_CACHE)
@VariableMgr.VarAttr(name = ENABLE_SQL_CACHE, fuzzy = true)
public boolean enableSqlCache = false;

@VariableMgr.VarAttr(name = ENABLE_QUERY_CACHE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,13 @@ suite("parse_sql_from_sql_cache") {
assertTrue(profileString.contains("Is Cached: Yes"))
}
}
}),
extraThread("sql_cache_with_date_format", {
sql "set enable_sql_cache=true"
for (def i in 0..3) {
def result = sql "select FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd HH:mm:ss')"
assertNotEquals("yyyy-MM-dd HH:mm:ss", result[0][0])
}
})
).get()
}

0 comments on commit dc1eec7

Please sign in to comment.