-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Interval implementation of InlineExpressionParser SPI
- Loading branch information
1 parent
a0519cf
commit 1ebdf52
Showing
9 changed files
with
447 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>shardingsphere-infra-expr-type</artifactId> | ||
<version>5.4.2-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>shardingsphere-infra-expr-interval</artifactId> | ||
<name>${project.artifactId}</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>shardingsphere-infra-expr-spi</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.shardingsphere</groupId> | ||
<artifactId>shardingsphere-test-util</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
207 changes: 207 additions & 0 deletions
207
...in/java/org/apache/shardingsphere/infra/expr/interval/IntervalInlineExpressionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* 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.shardingsphere.infra.expr.interval; | ||
|
||
import com.google.common.base.Strings; | ||
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; | ||
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; | ||
import org.apache.shardingsphere.infra.expr.spi.InlineExpressionParser; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.Month; | ||
import java.time.Year; | ||
import java.time.YearMonth; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.ChronoUnit; | ||
import java.time.temporal.Temporal; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.LongStream; | ||
|
||
/** | ||
* Interval inline expression parser. | ||
*/ | ||
public class IntervalInlineExpressionParser implements InlineExpressionParser { | ||
|
||
/** | ||
* Abbreviation for prefix. | ||
*/ | ||
private static final String PREFIX_KEY = "P"; | ||
|
||
/** | ||
* Abbreviation for suffix pattern. | ||
*/ | ||
private static final String SUFFIX_PATTERN_KEY = "SP"; | ||
|
||
/** | ||
* Abbreviation for datetime interval amount. | ||
*/ | ||
private static final String INTERVAL_AMOUNT_KEY = "DIA"; | ||
|
||
/** | ||
* Abbreviation for datetime interval unit. | ||
*/ | ||
private static final String INTERVAL_UNIT_KEY = "DIU"; | ||
|
||
/** | ||
* Abbreviation for datetime lower. | ||
*/ | ||
private static final String DATE_TIME_LOWER_KEY = "DL"; | ||
|
||
/** | ||
* Abbreviation for datetime upper. | ||
*/ | ||
private static final String DATE_TIME_UPPER_KEY = "DU"; | ||
|
||
private TemporalAccessor startTime; | ||
|
||
private TemporalAccessor endTime; | ||
|
||
private String prefix; | ||
|
||
private DateTimeFormatter dateTimeFormatterForSuffixPattern; | ||
|
||
private int stepAmount; | ||
|
||
private ChronoUnit stepUnit; | ||
|
||
private String inlineExpression; | ||
|
||
@Override | ||
public void init(final Properties props) { | ||
inlineExpression = props.getProperty(INLINE_EXPRESSION_KEY); | ||
Map<String, String> propsMap = Arrays.stream(inlineExpression.split(";")) | ||
.collect(Collectors.toMap(string -> string.split("=")[0], string -> string.split("=")[1])); | ||
prefix = getPrefix(propsMap); | ||
dateTimeFormatterForSuffixPattern = getSuffixPattern(propsMap); | ||
startTime = getDateTimeLower(propsMap); | ||
endTime = getDateTimeUpper(propsMap); | ||
stepAmount = Integer.parseInt(propsMap.getOrDefault(INTERVAL_AMOUNT_KEY, "1")); | ||
stepUnit = propsMap.containsKey(INTERVAL_UNIT_KEY) ? getStepUnit(propsMap.get(INTERVAL_UNIT_KEY)) : ChronoUnit.DAYS; | ||
} | ||
|
||
@Override | ||
public String handlePlaceHolder() { | ||
return inlineExpression; | ||
} | ||
|
||
@Override | ||
public List<String> splitAndEvaluate() { | ||
return Strings.isNullOrEmpty(inlineExpression) ? Collections.emptyList() : split(); | ||
} | ||
|
||
private String getPrefix(final Map<String, String> props) { | ||
ShardingSpherePreconditions.checkState(props.containsKey(PREFIX_KEY), | ||
() -> new RuntimeException(String.format("%s can not be null.", PREFIX_KEY))); | ||
return props.get(PREFIX_KEY); | ||
} | ||
|
||
private TemporalAccessor getDateTimeLower(final Map<String, String> props) { | ||
ShardingSpherePreconditions.checkState(props.containsKey(DATE_TIME_LOWER_KEY), | ||
() -> new RuntimeException(String.format("%s can not be null.", DATE_TIME_LOWER_KEY))); | ||
return getDateTime(props.get(DATE_TIME_LOWER_KEY)); | ||
} | ||
|
||
private TemporalAccessor getDateTimeUpper(final Map<String, String> props) { | ||
ShardingSpherePreconditions.checkState(props.containsKey(DATE_TIME_UPPER_KEY), | ||
() -> new RuntimeException(String.format("%s can not be null.", DATE_TIME_UPPER_KEY))); | ||
return getDateTime(props.get(DATE_TIME_UPPER_KEY)); | ||
} | ||
|
||
private TemporalAccessor getDateTime(final String dateTimeValue) { | ||
try { | ||
return dateTimeFormatterForSuffixPattern.parse(dateTimeValue); | ||
} catch (final DateTimeParseException dateTimeParseException) { | ||
throw new RuntimeException(dateTimeParseException); | ||
} | ||
} | ||
|
||
private DateTimeFormatter getSuffixPattern(final Map<String, String> props) { | ||
String suffix = props.get(SUFFIX_PATTERN_KEY); | ||
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(suffix), | ||
() -> new RuntimeException(String.format("%s can not be null or empty.", SUFFIX_PATTERN_KEY))); | ||
return DateTimeFormatter.ofPattern(suffix); | ||
} | ||
|
||
private ChronoUnit getStepUnit(final String stepUnit) { | ||
return Arrays.stream(ChronoUnit.values()) | ||
.filter(chronoUnit -> chronoUnit.toString().equalsIgnoreCase(stepUnit)) | ||
.findFirst() | ||
.orElseThrow(() -> new UnsupportedSQLOperationException(String.format("Cannot find step unit for specified %s property: `%s`", INTERVAL_UNIT_KEY, stepUnit))); | ||
} | ||
|
||
private List<String> split() { | ||
TemporalAccessor calculateTime = startTime; | ||
if (!calculateTime.isSupported(ChronoField.NANO_OF_DAY)) { | ||
if (calculateTime.isSupported(ChronoField.EPOCH_DAY)) { | ||
return convertStringFromTemporal(startTime.query(LocalDate::from), endTime.query(LocalDate::from)); | ||
} | ||
if (calculateTime.isSupported(ChronoField.YEAR) && calculateTime.isSupported(ChronoField.MONTH_OF_YEAR)) { | ||
return convertStringFromTemporal(startTime.query(YearMonth::from), endTime.query(YearMonth::from)); | ||
} | ||
if (calculateTime.isSupported(ChronoField.YEAR)) { | ||
return convertStringFromTemporal(startTime.query(Year::from), endTime.query(Year::from)); | ||
} | ||
if (calculateTime.isSupported(ChronoField.MONTH_OF_YEAR)) { | ||
return convertStringFromMonth(); | ||
} | ||
} | ||
if (!calculateTime.isSupported(ChronoField.EPOCH_DAY)) { | ||
return convertStringFromTemporal(startTime.query(LocalTime::from), endTime.query(LocalTime::from)); | ||
} | ||
return convertStringFromTemporal(startTime.query(LocalDateTime::from), endTime.query(LocalDateTime::from)); | ||
} | ||
|
||
private List<String> convertStringFromMonth() { | ||
Month startTimeAsMonth = startTime.query(Month::from); | ||
Month endTimeAsMonth = endTime.query(Month::from); | ||
return LongStream.iterate(0, x -> x + stepAmount) | ||
.limit((endTimeAsMonth.getValue() - startTimeAsMonth.getValue()) / stepAmount + 1) | ||
.parallel() | ||
.boxed() | ||
.map(startTimeAsMonth::plus) | ||
.map(dateTimeFormatterForSuffixPattern::format) | ||
.map(suffix -> prefix + suffix) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<String> convertStringFromTemporal(final Temporal startTimeAsTemporal, final Temporal endTimeAsTemporal) { | ||
return LongStream.iterate(0, x -> x + stepAmount) | ||
.limit(stepUnit.between(startTimeAsTemporal, endTimeAsTemporal) / stepAmount + 1) | ||
.parallel() | ||
.boxed() | ||
.map(arithmeticSequence -> startTimeAsTemporal.plus(arithmeticSequence, stepUnit)) | ||
.map(dateTimeFormatterForSuffixPattern::format) | ||
.map(suffix -> prefix + suffix) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return "INTERVAL"; | ||
} | ||
} |
Oops, something went wrong.