Timezone variable support one digital time (#2513)

Support time zone variable like "-8:00","+8:00","8:00"
Time zone variable like "-8:00" is illegal in time-zone ID ,so we mush transfer it to standard format
This commit is contained in:
HangyuanLiu
2019-12-20 07:45:29 +08:00
committed by ZHAO Chun
parent 6815979ba5
commit 11b78008cd
8 changed files with 55 additions and 8 deletions

View File

@ -328,8 +328,7 @@ public class CreateRoutineLoadStmt extends DdlStmt {
if (ConnectContext.get() != null) {
timezone = ConnectContext.get().getSessionVariable().getTimeZone();
}
timezone = jobProperties.getOrDefault(LoadStmt.TIMEZONE, timezone);
TimeUtils.checkTimeZoneValid(timezone);
timezone = TimeUtils.checkTimeZoneValidAndStandardize(jobProperties.getOrDefault(LoadStmt.TIMEZONE, timezone));
}
private void checkDataSourceProperties() throws AnalysisException {

View File

@ -201,7 +201,8 @@ public class LoadStmt extends DdlStmt {
// time zone
final String timezone = properties.get(TIMEZONE);
if (timezone != null) {
TimeUtils.checkTimeZoneValid(timezone);
properties.put(TIMEZONE, TimeUtils.checkTimeZoneValidAndStandardize(
properties.getOrDefault(LoadStmt.TIMEZONE, TimeUtils.DEFAULT_TIME_ZONE)));
}
}

View File

@ -70,7 +70,7 @@ public class TimeUtils {
+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"
+ "(\\s(((0?[0-9])|([1][0-9])|([2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
private static final Pattern TIMEZONE_OFFSET_FORMAT_REG = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$");
private static final Pattern TIMEZONE_OFFSET_FORMAT_REG = Pattern.compile("^[+-]{0,1}\\d{1,2}\\:\\d{2}$");
public static Date MIN_DATE = null;
public static Date MAX_DATE = null;
@ -235,8 +235,11 @@ public class TimeUtils {
}
// Check if the time zone_value is valid
public static void checkTimeZoneValid(String value) throws DdlException {
public static String checkTimeZoneValidAndStandardize(String value) throws DdlException {
try {
if (value == null) {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, "null");
}
// match offset type, such as +08:00, -07:00
Matcher matcher = TIMEZONE_OFFSET_FORMAT_REG.matcher(value);
// it supports offset and region timezone type, "CST" use here is compatibility purposes.
@ -245,6 +248,11 @@ public class TimeUtils {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value);
}
if (match) {
boolean postive = value.charAt(0) != '-';
value = (postive ? "+" : "-") + String.format("%02d:%02d",
Integer.parseInt(value.replaceAll("[+-]", "").split(":")[0]),
Integer.parseInt(value.replaceAll("[+-]", "").split(":")[1]));
// timezone offsets around the world extended from -12:00 to +14:00
int tz = Integer.parseInt(value.substring(1, 3)) * 100 + Integer.parseInt(value.substring(4, 6));
if (value.charAt(0) == '-' && tz > 1200) {
@ -254,8 +262,10 @@ public class TimeUtils {
}
}
ZoneId.of(value, timeZoneAliasMap);
return value;
} catch (DateTimeException ex) {
ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value);
}
throw new DdlException("Parse time zone " + value + " error");
}
}

View File

@ -19,6 +19,7 @@ package org.apache.doris.qe;
import org.apache.doris.analysis.SetType;
import org.apache.doris.analysis.SetVar;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.analysis.SysVariableDesc;
import org.apache.doris.catalog.Catalog;
import org.apache.doris.catalog.Type;
@ -215,7 +216,9 @@ public class VariableMgr {
checkUpdate(setVar, ctx.getFlag());
// Check variable time_zone value is valid
if (setVar.getVariable().toLowerCase().equals("time_zone")) {
TimeUtils.checkTimeZoneValid(setVar.getValue().getStringValue());
setVar = new SetVar(
setVar.getType(), setVar.getVariable(),
new StringLiteral(TimeUtils.checkTimeZoneValidAndStandardize(setVar.getValue().getStringValue())));
}
// To modify to default value.

View File

@ -160,8 +160,7 @@ public class StreamLoadTask {
strictMode = request.isStrictMode();
}
if (request.isSetTimezone()) {
timezone = request.getTimezone();
TimeUtils.checkTimeZoneValid(timezone);
timezone = TimeUtils.checkTimeZoneValidAndStandardize(request.getTimezone());
}
if (request.isSetExecMemLimit()) {
execMemLimit = request.getExecMemLimit();

View File

@ -96,6 +96,7 @@ public class CreateRoutineLoadStmtTest {
String topicName = "topic1";
String serverAddress = "127.0.0.1:8080";
String kafkaPartitionString = "1,2,3";
String timeZone = "8:00";
List<String> partitionNameString = Lists.newArrayList();
partitionNameString.add("p1");
PartitionNames partitionNames = new PartitionNames(partitionNameString);
@ -108,6 +109,7 @@ public class CreateRoutineLoadStmtTest {
loadPropertyList.add(partitionNames);
Map<String, String> properties = Maps.newHashMap();
properties.put(CreateRoutineLoadStmt.DESIRED_CONCURRENT_NUMBER_PROPERTY, "2");
properties.put(LoadStmt.TIMEZONE, timeZone);
String typeName = LoadDataSourceType.KAFKA.name();
Map<String, String> customProperties = Maps.newHashMap();
@ -134,6 +136,7 @@ public class CreateRoutineLoadStmtTest {
Assert.assertEquals(0, createRoutineLoadStmt.getMaxErrorNum());
Assert.assertEquals(serverAddress, createRoutineLoadStmt.getKafkaBrokerList());
Assert.assertEquals(topicName, createRoutineLoadStmt.getKafkaTopic());
Assert.assertEquals("+08:00", createRoutineLoadStmt.getTimezone());
}
}

View File

@ -22,6 +22,7 @@ import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.junit.Assert;
import org.junit.Test;
@ -134,4 +135,23 @@ public class TimeUtilsTest {
Assert.assertEquals(20150301120000L, datetime.getRealValue());
}
@Test
public void testTimezone() throws AnalysisException {
try {
Assert.assertEquals("CST", TimeUtils.checkTimeZoneValidAndStandardize("CST"));
Assert.assertEquals("+08:00", TimeUtils.checkTimeZoneValidAndStandardize("+08:00"));
Assert.assertEquals("+08:00", TimeUtils.checkTimeZoneValidAndStandardize("+8:00"));
Assert.assertEquals("-08:00", TimeUtils.checkTimeZoneValidAndStandardize("-8:00"));
Assert.assertEquals("+08:00", TimeUtils.checkTimeZoneValidAndStandardize("8:00"));
} catch (DdlException ex) {
Assert.fail();
}
try {
TimeUtils.checkTimeZoneValidAndStandardize("FOO");
Assert.fail();
} catch (DdlException ex) {
Assert.assertTrue(ex.getMessage().contains("Unknown or incorrect time zone: 'FOO'"));
}
}
}

View File

@ -124,6 +124,18 @@ public class VariableMgrTest {
VariableMgr.setVar(var, setVar4);
Assert.assertEquals(2L, var.getSqlMode());
// Test checkTimeZoneValidAndStandardize
SetVar setVar5 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("+8:00"));
VariableMgr.setVar(var, setVar5);
Assert.assertEquals("+08:00", VariableMgr.newSessionVariable().getTimeZone());
SetVar setVar6 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("8:00"));
VariableMgr.setVar(var, setVar6);
Assert.assertEquals("+08:00", VariableMgr.newSessionVariable().getTimeZone());
SetVar setVar7 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("-8:00"));
VariableMgr.setVar(var, setVar7);
Assert.assertEquals("-08:00", VariableMgr.newSessionVariable().getTimeZone());
}
@Test(expected = DdlException.class)