Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](analysis)fix use regex determine whether time part exists may cause backtracking #24882

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -58,8 +59,10 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class DateLiteral extends LiteralExpr {
private static final Logger LOG = LogManager.getLogger(DateLiteral.class);
Expand Down Expand Up @@ -93,6 +96,7 @@ public class DateLiteral extends LiteralExpr {
private static Map<String, Integer> MONTH_NAME_DICT = Maps.newHashMap();
private static Map<String, Integer> MONTH_ABBR_NAME_DICT = Maps.newHashMap();
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 final int ALLOW_SPACE_MASK = 4 | 64;
private static final int MAX_DATE_PARTS = 8;
Expand Down Expand Up @@ -127,6 +131,7 @@ public class DateLiteral extends LiteralExpr {
.appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, false)
.toFormatter().withResolverStyle(ResolverStyle.STRICT),
DATETIMEKEY_FORMATTER, DATEKEY_FORMATTER);
TIME_PART_SET = "HhIiklrSsTp".chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
} catch (AnalysisException e) {
LOG.error("invalid date format", e);
System.exit(-1);
Expand Down Expand Up @@ -175,12 +180,10 @@ public class DateLiteral extends LiteralExpr {
MONTH_ABBR_NAME_DICT.put("sun", 6);
}

//Regex used to determine if the TIME field exists int date_format
private static final Pattern HAS_TIME_PART = Pattern.compile("^.*[HhIiklrSsTp]+.*$");
private static final Pattern HAS_OFFSET_PART = Pattern.compile("[\\+\\-]\\d{2}:\\d{2}");

//Date Literal persist type in meta
private enum DateLiteralType {
// Date Literal persist type in meta
private enum DateLiteralType {
DATETIME(0),
DATE(1),

Expand Down Expand Up @@ -429,7 +432,7 @@ private void init(String s, Type type) throws AnalysisException {
if (s.contains(" ")) {
builder.appendLiteral(" ");
}
String[] timePart = s.contains(" ") ? s.split(" ")[1].split(":") : new String[]{};
String[] timePart = s.contains(" ") ? s.split(" ")[1].split(":") : new String[] {};
if (timePart.length > 0 && (type.equals(Type.DATE) || type.equals(Type.DATEV2))) {
throw new AnalysisException("Invalid date value: " + s);
}
Expand Down Expand Up @@ -556,7 +559,7 @@ public ByteBuffer getHashValue(PrimitiveType type) {
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(value);
} else if (type == PrimitiveType.DATETIMEV2) {
long value = (year << 46) | (month << 42) | (day << 37) | (hour << 32)
long value = (year << 46) | (month << 42) | (day << 37) | (hour << 32)
| (minute << 26) | (second << 20) | (microsecond % (1 << 20));
buffer = ByteBuffer.allocate(8);
buffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -780,7 +783,7 @@ private void fromPackedDateV2(long packedTime) {

private long makePackedDatetimeV2() {
return (year << 46) | (month << 42) | (day << 37) | (hour << 32)
| (minute << 26) | (second << 20) | (microsecond % (1 << 20));
| (minute << 26) | (second << 20) | (microsecond % (1 << 20));
}

private long makePackedDateV2() {
Expand All @@ -790,7 +793,7 @@ private long makePackedDateV2() {
@Override
public void write(DataOutput out) throws IOException {
super.write(out);
//set flag bit in meta, 0 is DATETIME and 1 is DATE
// set flag bit in meta, 0 is DATETIME and 1 is DATE
if (this.type.equals(Type.DATETIME)) {
out.writeShort(DateLiteralType.DATETIME.value());
out.writeLong(makePackedDatetime());
Expand Down Expand Up @@ -896,11 +899,11 @@ public long unixTimestamp(TimeZone timeZone) {
}

public static boolean hasTimePart(String format) {
return HAS_TIME_PART.matcher(format).matches();
return format.chars().anyMatch(c -> TIME_PART_SET.contains((char) c));
}

//Return the date stored in the dateliteral as pattern format.
//eg : "%Y-%m-%d" or "%Y-%m-%d %H:%i:%s"
// 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 {
TemporalAccessor accessor;
if (type.equals(Type.DATE) || type.equals(Type.DATEV2)) {
Expand Down