Unverified Commit 9f994585 authored by cainiao-cnj's avatar cainiao-cnj Committed by GitHub

[Optimize][Flink]Add Flink Table global variable (#2395)

* 添加内置全局变量时间戳 _CURRENT_TIMESTAMP_ * fix code style --------- Co-authored-by: 's avatarshuhang.chang <shuhang.chang@mtime>
parent d4f0a00f
...@@ -25,7 +25,6 @@ package org.dinky.constant; ...@@ -25,7 +25,6 @@ package org.dinky.constant;
* @since 2021/5/25 15:51 * @since 2021/5/25 15:51
*/ */
public interface FlinkSQLConstant { public interface FlinkSQLConstant {
/** 分隔符 */ /** 分隔符 */
String SEPARATOR = ";\n"; String SEPARATOR = ";\n";
/** DDL 类型 */ /** DDL 类型 */
...@@ -41,6 +40,9 @@ public interface FlinkSQLConstant { ...@@ -41,6 +40,9 @@ public interface FlinkSQLConstant {
/** The define identifier of FlinkSQL Date Variable */ /** The define identifier of FlinkSQL Date Variable */
String INNER_DATE_KEY = "_CURRENT_DATE_"; String INNER_DATE_KEY = "_CURRENT_DATE_";
/** The define identifier of FlinkSQL Timestamp Variable */
String INNER_TIMESTAMP_KEY = "_CURRENT_TIMESTAMP_";
/** 内置日期变量格式 确定后不能修改 */ /** 内置日期变量格式 确定后不能修改 */
String INNER_DATE_FORMAT = "yyyy-MM-dd"; String INNER_DATE_FORMAT = "yyyy-MM-dd";
} }
...@@ -137,6 +137,8 @@ public final class VariableManager { ...@@ -137,6 +137,8 @@ public final class VariableManager {
if (isInnerDateVariable(variableName)) { if (isInnerDateVariable(variableName)) {
return parseDateVariable(variableName); return parseDateVariable(variableName);
} else if (isInnerTimestampVariable(variableName)) {
return parseTimestampVar(variableName);
} }
throw new CatalogException(format("The variable of sql %s does not exist.", variableName)); throw new CatalogException(format("The variable of sql %s does not exist.", variableName));
...@@ -231,8 +233,12 @@ public final class VariableManager { ...@@ -231,8 +233,12 @@ public final class VariableManager {
m.appendReplacement(sb, ""); m.appendReplacement(sb, "");
// if value is null, parse inner date variable // if value is null, parse inner date variable
if (value == null && isInnerDateVariable(key)) { if (value == null) {
if (isInnerDateVariable(key)) {
value = parseDateVariable(key); value = parseDateVariable(key);
} else if (isInnerTimestampVariable(key)) {
value = parseTimestampVar(key);
}
} }
sb.append(value == null ? "" : value); sb.append(value == null ? "" : value);
...@@ -251,6 +257,16 @@ public final class VariableManager { ...@@ -251,6 +257,16 @@ public final class VariableManager {
return key.startsWith(FlinkSQLConstant.INNER_DATE_KEY); return key.startsWith(FlinkSQLConstant.INNER_DATE_KEY);
} }
/**
* verify if key is inner variable, such as _CURRENT_TIMESTAMP_ - 100
*
* @param key
* @return
*/
private boolean isInnerTimestampVariable(String key) {
return key.startsWith(FlinkSQLConstant.INNER_TIMESTAMP_KEY);
}
/** /**
* parse date variable * parse date variable
* *
...@@ -278,4 +294,30 @@ public final class VariableManager { ...@@ -278,4 +294,30 @@ public final class VariableManager {
return dtf.format(startDate); return dtf.format(startDate);
} }
/**
* parse timestamp variable
*
* @param key
* @return
*/
private String parseTimestampVar(String key) {
long millisecond = 0;
try {
if (key.contains("+")) {
int s = key.indexOf("+") + 1;
String num = key.substring(s).trim();
millisecond = Long.parseLong(num);
} else if (key.contains("-")) {
int s = key.indexOf("-") + 1;
String num = key.substring(s).trim();
millisecond = Long.parseLong(num) * -1;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return String.valueOf(System.currentTimeMillis() + millisecond);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment