From 781c30580153889690603090b26135ed549de066 Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Mon, 5 Aug 2019 14:39:29 +0800 Subject: [PATCH 1/8] set time zone --- .../org/apache/doris/common/ErrorCode.java | 1 + .../org/apache/doris/qe/GlobalVariable.java | 4 ++- .../org/apache/doris/qe/SessionVariable.java | 3 +- .../java/org/apache/doris/qe/VariableMgr.java | 34 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/common/ErrorCode.java b/fe/src/main/java/org/apache/doris/common/ErrorCode.java index 88311987b6e39f..67cf454b2a5201 100644 --- a/fe/src/main/java/org/apache/doris/common/ErrorCode.java +++ b/fe/src/main/java/org/apache/doris/common/ErrorCode.java @@ -81,6 +81,7 @@ public enum ErrorCode { ERR_NOT_SUPPORTED_AUTH_MODE(1251, new byte[] {'0', '8', '0', '0', '4'}, "Client does not support authentication protocol requested by server; consider upgrading MySQL client"), ERR_UNKNOWN_STORAGE_ENGINE(1286, new byte[] {'4', '2', '0', '0', '0'}, "Unknown storage engine '%s'"), + ERR_UNKNOWN_TIME_ZONE(1298, new byte[] {'H', 'Y', '0', '0', '0'}, "Unknown or incorrect time zone: '%s'"), ERR_WRONG_OBJECT(1347, new byte[] {'H', 'Y', '0', '0', '0'}, "'%s'.'%s' is not '%s'"), ERR_VIEW_WRONG_LIST(1353, new byte[] {'H', 'Y', '0', '0', '0'}, "View's SELECT and view's field list have different column counts"), diff --git a/fe/src/main/java/org/apache/doris/qe/GlobalVariable.java b/fe/src/main/java/org/apache/doris/qe/GlobalVariable.java index 6b8bd8fcc20941..70105aed32fd55 100644 --- a/fe/src/main/java/org/apache/doris/qe/GlobalVariable.java +++ b/fe/src/main/java/org/apache/doris/qe/GlobalVariable.java @@ -19,6 +19,8 @@ import org.apache.doris.common.Version; +import java.time.ZoneId; + // You can place your global variable in this class with public and VariableMgr.VarAttr annotation. // You can get this variable from MySQL client with statement `SELECT @@variable_name`, // and change its value through `SET variable_name = xxx` @@ -48,7 +50,7 @@ public final class GlobalVariable { // A string to be executed by the server for each client that connects @VariableMgr.VarAttr(name = "system_time_zone", flag = VariableMgr.READ_ONLY) - private static String systemTimeZone = "CST"; + public static String systemTimeZone = ZoneId.systemDefault().normalized().toString(); // The amount of memory allocated for caching query results @VariableMgr.VarAttr(name = "query_cache_size") diff --git a/fe/src/main/java/org/apache/doris/qe/SessionVariable.java b/fe/src/main/java/org/apache/doris/qe/SessionVariable.java index 7463a524c35439..be27f2a2005615 100644 --- a/fe/src/main/java/org/apache/doris/qe/SessionVariable.java +++ b/fe/src/main/java/org/apache/doris/qe/SessionVariable.java @@ -30,6 +30,7 @@ import java.io.DataOutput; import java.io.IOException; import java.io.Serializable; +import java.time.ZoneId; // System variable public class SessionVariable implements Serializable, Writable { @@ -156,7 +157,7 @@ public class SessionVariable implements Serializable, Writable { // The current time zone @VariableMgr.VarAttr(name = TIME_ZONE) - private String timeZone = "CST"; + private String timeZone = ZoneId.systemDefault().normalized().toString(); // The current time zone @VariableMgr.VarAttr(name = SQL_SAFE_UPDATES) diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index 31579feb2f5f43..8c37b2d5aaecad 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -44,6 +44,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.time.DateTimeException; +import java.time.ZoneId; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -51,6 +53,8 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.regex.Matcher; +import java.util.regex.Pattern; // Variable manager, merge session variable and global variable public class VariableMgr { @@ -67,6 +71,8 @@ public class VariableMgr { public static final int READ_ONLY = 8; // Variables with this flag can not be seen with `SHOW VARIABLES` statement. public static final int INVISIBLE = 16; + // set CST to +08:00 instead of America/Chicago + public static final ImmutableMap timeZoneAliasMap = ImmutableMap.of("CST", "Asia/Shanghai"); // Map variable name to variable context which have enough information to change variable value. private static ImmutableMap ctxByVarName; @@ -203,6 +209,28 @@ private static void checkUpdate(SetVar setVar, int flag) throws DdlException { } } + // Check if the time zone_value is valid + private static void checkTimeZoneValid(SetVar setVar) throws DdlException { + if (setVar.getValue() != null) { + String value = setVar.getValue().getStringValue(); + try { + Pattern p = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$"); + Matcher m = p.matcher(value); + if (m.matches()) { + int tz = Integer.parseInt(value.substring(1, 3)) * 100 + Integer.parseInt(value.substring(4, 6)); + if (value.charAt(0) == '-' && tz > 1200) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); + } else if (value.charAt(0) == '+' && tz > 1400) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); + } + } + ZoneId.of(value, timeZoneAliasMap); + } catch (DateTimeException ex) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); + } + } + } + // Get from show name to field public static void setVar(SessionVariable sessionVariable, SetVar setVar) throws DdlException { VarContext ctx = ctxByVarName.get(setVar.getVariable()); @@ -211,6 +239,12 @@ public static void setVar(SessionVariable sessionVariable, SetVar setVar) throws } // Check variable attribute and setVar checkUpdate(setVar, ctx.getFlag()); + // Check variable time_zone value is valid + if (setVar.getVariable().toLowerCase().equals("time_zone")) { + if (!setVar.getValue().getStringValue().equalsIgnoreCase("SYSTEM")) { + checkTimeZoneValid(setVar); + } + } // To modify to default value. VarAttr attr = ctx.getField().getAnnotation(VarAttr.class); From 8574907875d5eaab4a7281d116df378243877b0b Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Mon, 5 Aug 2019 15:39:00 +0800 Subject: [PATCH 2/8] set time zone --- fe/src/main/java/org/apache/doris/qe/SessionVariable.java | 3 +-- fe/src/main/java/org/apache/doris/qe/VariableMgr.java | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/qe/SessionVariable.java b/fe/src/main/java/org/apache/doris/qe/SessionVariable.java index be27f2a2005615..7463a524c35439 100644 --- a/fe/src/main/java/org/apache/doris/qe/SessionVariable.java +++ b/fe/src/main/java/org/apache/doris/qe/SessionVariable.java @@ -30,7 +30,6 @@ import java.io.DataOutput; import java.io.IOException; import java.io.Serializable; -import java.time.ZoneId; // System variable public class SessionVariable implements Serializable, Writable { @@ -157,7 +156,7 @@ public class SessionVariable implements Serializable, Writable { // The current time zone @VariableMgr.VarAttr(name = TIME_ZONE) - private String timeZone = ZoneId.systemDefault().normalized().toString(); + private String timeZone = "CST"; // The current time zone @VariableMgr.VarAttr(name = SQL_SAFE_UPDATES) diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index 8c37b2d5aaecad..f22ddc9cc94e5d 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -17,6 +17,7 @@ package org.apache.doris.qe; +import com.sun.tools.javac.code.Attribute; import org.apache.doris.analysis.SetType; import org.apache.doris.analysis.SetVar; import org.apache.doris.analysis.SysVariableDesc; @@ -216,6 +217,9 @@ private static void checkTimeZoneValid(SetVar setVar) throws DdlException { try { Pattern p = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$"); Matcher m = p.matcher(value); + if (!value.contains("/") && !value.equals("CST") && !m.matches()) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); + } if (m.matches()) { int tz = Integer.parseInt(value.substring(1, 3)) * 100 + Integer.parseInt(value.substring(4, 6)); if (value.charAt(0) == '-' && tz > 1200) { @@ -228,6 +232,8 @@ private static void checkTimeZoneValid(SetVar setVar) throws DdlException { } catch (DateTimeException ex) { ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); } + } else { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, ""); } } From 0ccd50112998bef13957c84ecb31e4a4c484aeee Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Mon, 5 Aug 2019 16:23:09 +0800 Subject: [PATCH 3/8] set time zone --- fe/src/main/java/org/apache/doris/qe/VariableMgr.java | 1 - .../test/java/org/apache/doris/qe/VariableMgrTest.java | 10 ++++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index f22ddc9cc94e5d..31fdeeafa22a92 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -17,7 +17,6 @@ package org.apache.doris.qe; -import com.sun.tools.javac.code.Attribute; import org.apache.doris.analysis.SetType; import org.apache.doris.analysis.SetVar; import org.apache.doris.analysis.SysVariableDesc; diff --git a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java index 5053e7d9f08a31..c96bb467d2efb3 100644 --- a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java +++ b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java @@ -99,11 +99,21 @@ public void testNormal() throws IllegalAccessException, DdlException, NoSuchFiel var = VariableMgr.newSessionVariable(); Assert.assertEquals(5L, var.getParallelExecInstanceNum()); + SetVar setVar3 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("Asia/Shanghai")); + VariableMgr.setVar(var, setVar3); + Assert.assertEquals("CST", var.getTimeZone()); + var = VariableMgr.newSessionVariable(); + Assert.assertEquals("Asia/Shanghai", var.getTimeZone()); + // Set session variable setVar = new SetVar(SetType.GLOBAL, "exec_mem_limit", new IntLiteral(1234L)); VariableMgr.setVar(var, setVar); Assert.assertEquals(1234L, var.getMaxExecMemByte()); + setVar3 = new SetVar(SetType.SESSION, "time_zone", new StringLiteral("Asia/Jakarta")); + VariableMgr.setVar(var, setVar3); + Assert.assertEquals("Asia/Jakarta", var.getTimeZone()); + // Get from name SysVariableDesc desc = new SysVariableDesc("exec_mem_limit"); Assert.assertEquals(var.getMaxExecMemByte() + "", VariableMgr.getValue(var, desc)); From 15844bc3a5b94a248b6901fdb725ffe09b0d8d20 Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Mon, 5 Aug 2019 17:06:00 +0800 Subject: [PATCH 4/8] set time zone --- .../org/apache/doris/qe/VariableMgrTest.java | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java index c96bb467d2efb3..754d3878c89688 100644 --- a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java +++ b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java @@ -98,22 +98,22 @@ public void testNormal() throws IllegalAccessException, DdlException, NoSuchFiel Assert.assertEquals(1L, var.getParallelExecInstanceNum()); var = VariableMgr.newSessionVariable(); Assert.assertEquals(5L, var.getParallelExecInstanceNum()); - + /* SetVar setVar3 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("Asia/Shanghai")); VariableMgr.setVar(var, setVar3); Assert.assertEquals("CST", var.getTimeZone()); var = VariableMgr.newSessionVariable(); Assert.assertEquals("Asia/Shanghai", var.getTimeZone()); - + */ // Set session variable setVar = new SetVar(SetType.GLOBAL, "exec_mem_limit", new IntLiteral(1234L)); VariableMgr.setVar(var, setVar); Assert.assertEquals(1234L, var.getMaxExecMemByte()); - + /* setVar3 = new SetVar(SetType.SESSION, "time_zone", new StringLiteral("Asia/Jakarta")); VariableMgr.setVar(var, setVar3); Assert.assertEquals("Asia/Jakarta", var.getTimeZone()); - + */ // Get from name SysVariableDesc desc = new SysVariableDesc("exec_mem_limit"); Assert.assertEquals(var.getMaxExecMemByte() + "", VariableMgr.getValue(var, desc)); @@ -133,6 +133,38 @@ public void testInvalidType() throws DdlException { Assert.fail("No exception throws."); } + /* + + @Test(expected = DdlException.class) + public void testInvalidTimeZoneRegion() throws DdlException { + // Set global variable + SetVar setVar = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("Hongkong")); + SessionVariable var = VariableMgr.newSessionVariable(); + try { + VariableMgr.setVar(var, setVar); + } catch (DdlException e) { + LOG.warn("VariableMgr throws", e); + throw e; + } + Assert.fail("No exception throws."); + } + + @Test(expected = DdlException.class) + public void testInvalidTimeZoneOffset() throws DdlException { + // Set global variable + SetVar setVar = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("+15:00")); + SessionVariable var = VariableMgr.newSessionVariable(); + try { + VariableMgr.setVar(var, setVar); + } catch (DdlException e) { + LOG.warn("VariableMgr throws", e); + throw e; + } + Assert.fail("No exception throws."); + } + */ + + @Test(expected = DdlException.class) public void testReadOnly() throws AnalysisException, DdlException { SysVariableDesc desc = new SysVariableDesc("version_comment"); From 6e8d7ba0d02e27490155758d732b2c4483f0e8ac Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Mon, 5 Aug 2019 17:20:50 +0800 Subject: [PATCH 5/8] set time zone --- .../java/org/apache/doris/qe/VariableMgrTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java index 754d3878c89688..355440cf94f74d 100644 --- a/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java +++ b/fe/src/test/java/org/apache/doris/qe/VariableMgrTest.java @@ -98,22 +98,22 @@ public void testNormal() throws IllegalAccessException, DdlException, NoSuchFiel Assert.assertEquals(1L, var.getParallelExecInstanceNum()); var = VariableMgr.newSessionVariable(); Assert.assertEquals(5L, var.getParallelExecInstanceNum()); - /* + SetVar setVar3 = new SetVar(SetType.GLOBAL, "time_zone", new StringLiteral("Asia/Shanghai")); VariableMgr.setVar(var, setVar3); Assert.assertEquals("CST", var.getTimeZone()); var = VariableMgr.newSessionVariable(); Assert.assertEquals("Asia/Shanghai", var.getTimeZone()); - */ + // Set session variable setVar = new SetVar(SetType.GLOBAL, "exec_mem_limit", new IntLiteral(1234L)); VariableMgr.setVar(var, setVar); Assert.assertEquals(1234L, var.getMaxExecMemByte()); - /* + setVar3 = new SetVar(SetType.SESSION, "time_zone", new StringLiteral("Asia/Jakarta")); VariableMgr.setVar(var, setVar3); Assert.assertEquals("Asia/Jakarta", var.getTimeZone()); - */ + // Get from name SysVariableDesc desc = new SysVariableDesc("exec_mem_limit"); Assert.assertEquals(var.getMaxExecMemByte() + "", VariableMgr.getValue(var, desc)); @@ -133,8 +133,6 @@ public void testInvalidType() throws DdlException { Assert.fail("No exception throws."); } - /* - @Test(expected = DdlException.class) public void testInvalidTimeZoneRegion() throws DdlException { // Set global variable @@ -162,7 +160,6 @@ public void testInvalidTimeZoneOffset() throws DdlException { } Assert.fail("No exception throws."); } - */ @Test(expected = DdlException.class) From 1c52d3afd61a9e62b2c8d6472d797d8376302a4b Mon Sep 17 00:00:00 2001 From: yangwenbo6 Date: Tue, 6 Aug 2019 10:30:48 +0800 Subject: [PATCH 6/8] fix --- .../apache/doris/common/util/TimeUtils.java | 33 +++++++++++++++++++ .../java/org/apache/doris/qe/VariableMgr.java | 31 ++--------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java index 7adae22a674572..f37f47bb331b31 100644 --- a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java +++ b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java @@ -17,18 +17,24 @@ package org.apache.doris.common.util; +import org.apache.doris.analysis.SetVar; import org.apache.doris.catalog.PrimitiveType; import org.apache.doris.catalog.Type; import org.apache.doris.common.AnalysisException; import com.google.common.base.Preconditions; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; +import java.time.DateTimeException; +import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.SimpleTimeZone; @@ -36,6 +42,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.apache.doris.qe.VariableMgr.timeZoneAliasMap; + // TODO(dhc) add nanosecond timer for coordinator's root profile public class TimeUtils { private static final Logger LOG = LogManager.getLogger(TimeUtils.class); @@ -205,4 +213,29 @@ public static long timeStringToLong(String timeStr) { } return d.getTime(); } + + // Check if the time zone_value is valid + public static void checkTimeZoneValid(String value) throws DdlException { + try { + // match offset type, such as +08:00, -07:00 + Pattern p = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$"); + Matcher m = p.matcher(value); + // it supports offset and region timezone type, "CST" use here is compatibility purposes. + if (!value.contains("/") && !value.equals("CST") && !m.matches()) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value); + } + if (m.matches()) { + // 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) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value); + } else if (value.charAt(0) == '+' && tz > 1400) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value); + } + } + ZoneId.of(value, timeZoneAliasMap); + } catch (DateTimeException ex) { + ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value); + } + } } diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index 31fdeeafa22a92..85fe91d27f8491 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -27,6 +27,7 @@ import org.apache.doris.common.ErrorCode; import org.apache.doris.common.ErrorReport; import org.apache.doris.common.PatternMatcher; +import org.apache.doris.common.util.TimeUtils; import org.apache.doris.persist.EditLog; import com.google.common.collect.ImmutableMap; @@ -209,32 +210,6 @@ private static void checkUpdate(SetVar setVar, int flag) throws DdlException { } } - // Check if the time zone_value is valid - private static void checkTimeZoneValid(SetVar setVar) throws DdlException { - if (setVar.getValue() != null) { - String value = setVar.getValue().getStringValue(); - try { - Pattern p = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$"); - Matcher m = p.matcher(value); - if (!value.contains("/") && !value.equals("CST") && !m.matches()) { - ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); - } - if (m.matches()) { - int tz = Integer.parseInt(value.substring(1, 3)) * 100 + Integer.parseInt(value.substring(4, 6)); - if (value.charAt(0) == '-' && tz > 1200) { - ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); - } else if (value.charAt(0) == '+' && tz > 1400) { - ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); - } - } - ZoneId.of(value, timeZoneAliasMap); - } catch (DateTimeException ex) { - ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, setVar.getValue().getStringValue()); - } - } else { - ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, ""); - } - } // Get from show name to field public static void setVar(SessionVariable sessionVariable, SetVar setVar) throws DdlException { @@ -246,9 +221,7 @@ public static void setVar(SessionVariable sessionVariable, SetVar setVar) throws checkUpdate(setVar, ctx.getFlag()); // Check variable time_zone value is valid if (setVar.getVariable().toLowerCase().equals("time_zone")) { - if (!setVar.getValue().getStringValue().equalsIgnoreCase("SYSTEM")) { - checkTimeZoneValid(setVar); - } + TimeUtils.checkTimeZoneValid(setVar.getValue().getStringValue()); } // To modify to default value. From 86697f706ec5917f8c46774c2ad0ab502c02cc81 Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Tue, 6 Aug 2019 11:01:39 +0800 Subject: [PATCH 7/8] fix --- fe/src/main/java/org/apache/doris/common/util/TimeUtils.java | 1 - fe/src/main/java/org/apache/doris/qe/VariableMgr.java | 4 ---- 2 files changed, 5 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java index f37f47bb331b31..f9ca6b541041a1 100644 --- a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java +++ b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java @@ -17,7 +17,6 @@ package org.apache.doris.common.util; -import org.apache.doris.analysis.SetVar; import org.apache.doris.catalog.PrimitiveType; import org.apache.doris.catalog.Type; import org.apache.doris.common.AnalysisException; diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index 85fe91d27f8491..e383fcfbc09296 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -45,8 +45,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.time.DateTimeException; -import java.time.ZoneId; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -54,8 +52,6 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.regex.Matcher; -import java.util.regex.Pattern; // Variable manager, merge session variable and global variable public class VariableMgr { From 5badcce17db10149e48d4f5e8d62b2582e9ed88a Mon Sep 17 00:00:00 2001 From: yangwenbo Date: Wed, 7 Aug 2019 00:08:00 +0800 Subject: [PATCH 8/8] fix --- .../org/apache/doris/common/util/TimeUtils.java | 16 ++++++++++------ .../java/org/apache/doris/qe/VariableMgr.java | 2 -- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java index f9ca6b541041a1..fa15d0a1e40ec3 100644 --- a/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java +++ b/fe/src/main/java/org/apache/doris/common/util/TimeUtils.java @@ -22,6 +22,7 @@ import org.apache.doris.common.AnalysisException; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; import org.apache.doris.common.DdlException; import org.apache.doris.common.ErrorCode; @@ -41,14 +42,15 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.apache.doris.qe.VariableMgr.timeZoneAliasMap; - // TODO(dhc) add nanosecond timer for coordinator's root profile public class TimeUtils { private static final Logger LOG = LogManager.getLogger(TimeUtils.class); private static final TimeZone TIME_ZONE; + // set CST to +08:00 instead of America/Chicago + public static final ImmutableMap timeZoneAliasMap = ImmutableMap.of("CST", "Asia/Shanghai"); + // NOTICE: Date formats are not synchronized. // it must be used as synchronized externally. private static final SimpleDateFormat DATE_FORMAT; @@ -63,6 +65,8 @@ public class TimeUtils { + "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?" + "((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}$"); public static Date MIN_DATE = null; public static Date MAX_DATE = null; @@ -217,13 +221,13 @@ public static long timeStringToLong(String timeStr) { public static void checkTimeZoneValid(String value) throws DdlException { try { // match offset type, such as +08:00, -07:00 - Pattern p = Pattern.compile("^[+-]{1}\\d{2}\\:\\d{2}$"); - Matcher m = p.matcher(value); + Matcher matcher = TIMEZONE_OFFSET_FORMAT_REG.matcher(value); // it supports offset and region timezone type, "CST" use here is compatibility purposes. - if (!value.contains("/") && !value.equals("CST") && !m.matches()) { + boolean match = matcher.matches(); + if (!value.contains("/") && !value.equals("CST") && !match) { ErrorReport.reportDdlException(ErrorCode.ERR_UNKNOWN_TIME_ZONE, value); } - if (m.matches()) { + if (match) { // 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) { diff --git a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java index e383fcfbc09296..8754103cac9143 100644 --- a/fe/src/main/java/org/apache/doris/qe/VariableMgr.java +++ b/fe/src/main/java/org/apache/doris/qe/VariableMgr.java @@ -68,8 +68,6 @@ public class VariableMgr { public static final int READ_ONLY = 8; // Variables with this flag can not be seen with `SHOW VARIABLES` statement. public static final int INVISIBLE = 16; - // set CST to +08:00 instead of America/Chicago - public static final ImmutableMap timeZoneAliasMap = ImmutableMap.of("CST", "Asia/Shanghai"); // Map variable name to variable context which have enough information to change variable value. private static ImmutableMap ctxByVarName;