Skip to content

Commit

Permalink
Revert "[fix](date_function) fix str_to_date function return wrong mi…
Browse files Browse the repository at this point in the history
…crosecond issue" (#47441)
  • Loading branch information
yiguolei authored Jan 26, 2025
1 parent 631d1f4 commit 803d3a1
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public class DateLiteral extends LiteralExpr {
private static Map<String, Integer> WEEK_DAY_NAME_DICT = Maps.newHashMap();
private static Set<Character> TIME_PART_SET = Sets.newHashSet();
private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static String MICRO_SECOND_FORMATTER = "%f";
private static final WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);

static {
Expand Down Expand Up @@ -718,7 +717,7 @@ public String getStringValue() {
int scale = ((ScalarType) type).getScalarScale();
long scaledMicroseconds = (long) (microsecond / SCALE_FACTORS[scale]);

if (scale > 0) {
if (scaledMicroseconds != 0) {
dateTimeChars[19] = '.';
fillPaddedValue(dateTimeChars, 20, (int) scaledMicroseconds, scale);
return new String(dateTimeChars, 0, 20 + scale);
Expand Down Expand Up @@ -1063,10 +1062,6 @@ public static boolean hasTimePart(String format) {
return format.chars().anyMatch(c -> TIME_PART_SET.contains((char) c));
}

public static boolean hasMicroSecondPart(String format) {
return format.indexOf(MICRO_SECOND_FORMATTER) != -1;
}

// Return the date stored in the dateliteral as pattern format.
// eg : "%Y-%m-%d" or "%Y-%m-%d %H:%i:%s"
public String dateFormat(String pattern) throws AnalysisException {
Expand Down Expand Up @@ -1609,9 +1604,6 @@ public int fromDateFormatStr(String format, String value, boolean hasSubVal) thr
case 'T':
partUsed |= timePart;
break;
case 'f':
partUsed |= fracPart;
break;
default:
break;
}
Expand Down Expand Up @@ -1673,7 +1665,7 @@ public int fromDateFormatStr(String format, String value, boolean hasSubVal) thr

// Compute timestamp type
if ((partUsed & datePart) != 0) { // Ymd part only
if (hasMicroSecondPart(format)) {
if ((partUsed & fracPart) != 0) {
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
} else if ((partUsed & timePart) != 0) {
this.type = ScalarType.getDefaultDateType(Type.DATETIME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1993,11 +1993,7 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
Expr child1Result = getChild(1).getResultValue(false);
if (child1Result instanceof StringLiteral) {
if (DateLiteral.hasTimePart(child1Result.getStringValue())) {
if (DateLiteral.hasMicroSecondPart(child1Result.getStringValue())) {
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
} else {
this.type = Type.DEFAULT_DATETIMEV2;
}
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
} else {
this.type = Type.DATEV2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,8 @@ public static Expression strToDate(StringLikeLiteral str, StringLikeLiteral form
if (org.apache.doris.analysis.DateLiteral.hasTimePart(format.getStringValue())) {
DataType returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME));
if (returnType instanceof DateTimeV2Type) {
boolean hasMicroPart = org.apache.doris.analysis.DateLiteral
.hasMicroSecondPart(format.getStringValue());
return DateTimeV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()), hasMicroPart ? 6 : 0);
.toFormatter(), str.getValue()));
} else {
return DateTimeLiteral.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public FunctionSignature computeSignature(FunctionSignature signature) {
if (getArgument(1) instanceof StringLikeLiteral) {
if (DateLiteral.hasTimePart(((StringLikeLiteral) getArgument(1)).getStringValue())) {
returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME));
if (returnType.isDateTimeV2Type()
&& DateLiteral.hasMicroSecondPart(((StringLikeLiteral) getArgument(1)).getStringValue())) {
if (returnType.isDateTimeV2Type()) {
returnType = DataType.fromCatalogType(Type.DATETIMEV2_WITH_MAX_SCALAR);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
["2022-12-02 22:23:25.000", "2022-12-02 22:23:23.998"]

-- !select --
["2022-12-02 22:23:25.000", "2022-12-02 22:23:23.998"]
["2022-12-02 22:23:25", "2022-12-02 22:23:23.998"]

-- !select --
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ suite("test_oracle_jdbc_catalog", "p0,external,oracle,external_docker,external_d
order_qt_date4 """ select * from TEST_DATE where (T1 > '2022-01-21 00:00:00' and T1 < '2022-01-22 00:00:00') or (T1 > '2022-01-20 00:00:00' and T1 < '2022-01-23 00:00:00'); """
order_qt_date5 """ select * from TEST_DATE where T1 < '2022-01-22 00:00:00' or T1 = '2022-01-21 05:23:01'; """
order_qt_date6 """ select * from TEST_DATE where (T1 < '2022-01-22 00:00:00' or T1 > '2022-01-20 00:00:00') and (T1 < '2022-01-23 00:00:00' or T1 > '2022-01-19 00:00:00'); """
order_qt_date7 """select * from TEST_TIMESTAMP where T2 < str_to_date('2020-12-21 12:34:56', '%Y-%m-%d %H:%i:%s.%f');"""
order_qt_date7 """select * from TEST_TIMESTAMP where T2 < str_to_date('2020-12-21 12:34:56', '%Y-%m-%d %H:%i:%s');"""

// test nvl
explain {
Expand Down

This file was deleted.

0 comments on commit 803d3a1

Please sign in to comment.