Skip to content

Commit

Permalink
fix checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Oct 30, 2024
1 parent 20eee0b commit a3411b4
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
public class DateLiteral extends Literal {
public static final String JAVA_DATE_FORMAT = "yyyy-MM-dd";

public static final Set<Character> punctuations = ImmutableSet.of('!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'-', '+', '=', '_', '{', '}', '[', ']', '|', '\\', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', '~',
'`');

// for cast datetime type to date type.
private static final LocalDateTime START_OF_A_DAY = LocalDateTime.of(0, 1, 1, 0, 0, 0);
private static final LocalDateTime END_OF_A_DAY = LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999999000);
private static final DateLiteral MIN_DATE = new DateLiteral(0, 1, 1);
private static final DateLiteral MAX_DATE = new DateLiteral(9999, 12, 31);
private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public static final Set<Character> punctuations = ImmutableSet.of('!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'-', '+', '=', '_', '{', '}', '[', ']', '|', '\\', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', '~',
'`');

protected long year;
protected long month;
protected long day;
Expand Down Expand Up @@ -209,7 +209,9 @@ static Result<String, AnalysisException> normalize(String s) {
}
} else {
final String currentString = s;
return Result.err(() -> new AnalysisException("date/datetime literal [" + currentString + "] is invalid"));
return Result.err(
() -> new AnalysisException("date/datetime literal [" + currentString + "] is invalid")
);
}
i = j;
partNumber += 1;
Expand All @@ -230,7 +232,9 @@ static Result<String, AnalysisException> normalize(String s) {
sb.append(':');
} else {
final String currentString = s;
return Result.err(() -> new AnalysisException("date/datetime literal [" + currentString + "] is invalid"));
return Result.err(
() -> new AnalysisException("date/datetime literal [" + currentString + "] is invalid")
);
}
} else {
break;
Expand Down Expand Up @@ -264,6 +268,7 @@ static Result<String, AnalysisException> normalize(String s) {
return Result.ok(sb.toString());
}

/** parseDateLiteral */
public static Result<DateLiteral, AnalysisException> parseDateLiteral(String s) {
Result<TemporalAccessor, AnalysisException> parseResult = parseDateTime(s);
if (parseResult.isError()) {
Expand All @@ -280,6 +285,7 @@ public static Result<DateLiteral, AnalysisException> parseDateLiteral(String s)
return Result.ok(new DateLiteral(year, month, day));
}

/** parseDateTime */
public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s) {
// fast parse '2022-01-01'
if (s.length() == 10 && s.charAt(4) == '-' && s.charAt(7) == '-') {
Expand Down Expand Up @@ -336,7 +342,9 @@ public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s

// if Year is not present, throw exception
if (!dateTime.isSupported(ChronoField.YEAR)) {
return Result.err(() -> new AnalysisException("date/datetime literal [" + originalString + "] is invalid"));
return Result.err(
() -> new AnalysisException("date/datetime literal [" + originalString + "] is invalid")
);
}

return Result.ok(dateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static int determineScale(String s) {
return scale;
}

/** parseDateTimeLiteral */
public static Result<DateTimeLiteral, AnalysisException> parseDateTimeLiteral(String s, boolean isV2) {
Result<TemporalAccessor, AnalysisException> parseResult = parseDateTime(s);
if (parseResult.isError()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,4 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
public LiteralExpr toLegacyLiteral() {
return new org.apache.doris.analysis.FloatLiteral(getDouble(), Type.FLOAT);
}

public static boolean isValidFloat(String str) {
if (str == null || str.isEmpty()) {
return true;
}

int index = 0;
char c = str.charAt(index);
if (c == '+' || c == '-') {
index++;
}

while (index < str.length()) {
c = str.charAt(index++);
if (c == 'E' || c == '.') {
break;
}
if (!Character.isDigit(c)) {
return false;
}
}

for (; index < str.length(); index++) {
c = str.charAt(index);
if (!('0' <= c && c <= '9')) {
return false;
}
}
return true;
}

// private boolean parseIntegerParts(String str, int index) {
// int index = 0;
// char c = str.charAt(index);
// if (c == '+' || c == '-') {
// index++;
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,4 @@ public long getLongValue() {
}

public abstract Number getNumber();

public static boolean isValidInteger(String str) {
if (str == null || str.isEmpty()) {
return true;
}

int index = 0;

char c = str.charAt(index);
if (c == '+' || c == '-') {
index++;
}

for (; index < str.length(); index++) {
c = str.charAt(index);
if (!('0' <= c && c <= '9')) {
return false;
}
}
return true;
}

public static void main(String[] args) {
System.out.println(isValidInteger("01"));
System.out.println(isValidInteger("+01"));
System.out.println(isValidInteger("-01"));
System.out.println(isValidInteger("- 01"));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

import java.util.List;

/** AndChecker */
public class AndChecker extends FormatChecker {
private final List<FormatChecker> checkers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.function.Predicate;

/** AtLeastChecker */
public class AtLeastChecker extends FormatChecker {
private int minCount;
private int maxRead;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

/** CharChecker */
public class CharChecker extends FormatChecker {
public final char c;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package org.apache.doris.nereids.trees.expressions.literal.format;
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

import java.util.function.Predicate;

/** CustomCharChecker */
public class CustomCharChecker extends FormatChecker {
private Predicate<Character> checker;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;

/** DateTimeChecker */
public class DateTimeChecker extends FormatChecker {
private DateTimeChecker(StringInspect stringInspect) {
super(stringInspect);
Expand All @@ -15,47 +33,47 @@ public static boolean isValidDateTime(String str) {
@Override
protected boolean doCheck() {
FormatChecker dateFormatChecker = and(
or(
// date
and(
or(
// 20241012
number(8, 8),
// 2024-10-12
and(
number(4, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2) // day
)
),
option(ch('Z'))
),
// datetime
and(
or(
// 20241012010203
number(14, 14),
// 2024-01-01 01:02:03
and(
number(4, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2), // day
atLeast(1, c -> c == 'T' || c == ' ' || DateLiteral.punctuations.contains(c)),
number(2, 2), // hour
chars(DateLiteral.punctuations::contains),
number(2, 2), // minute
chars(DateLiteral.punctuations::contains),
number(2, 2) // second
)
or(
// date
and(
or(
// 20241012
number(8, 8),
// 2024-10-12
and(
number(4, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2) // day
)
),
option(ch('Z'))
),
option(nanoSecond()),
option(timeZone())
// datetime
and(
or(
// 20241012010203
number(14, 14),
// 2024-01-01 01:02:03
and(
number(4, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2), // day
atLeast(1, c -> c == 'T' || c == ' ' || DateLiteral.punctuations.contains(c)),
number(2, 2), // hour
chars(DateLiteral.punctuations::contains),
number(2, 2), // minute
chars(DateLiteral.punctuations::contains),
number(2, 2) // second
)
),
option(nanoSecond()),
option(timeZone())
)
)
)
);
return dateFormatChecker.check();
}
Expand Down
Loading

0 comments on commit a3411b4

Please sign in to comment.