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";
} }
...@@ -72,9 +72,9 @@ public final class VariableManager { ...@@ -72,9 +72,9 @@ public final class VariableManager {
* Registers a variable of sql under the given name. The sql variable name must be unique. * Registers a variable of sql under the given name. The sql variable name must be unique.
* *
* @param variableName name under which to register the given sql variable * @param variableName name under which to register the given sql variable
* @param variable a variable of sql to register * @param variable a variable of sql to register
* @throws CatalogException if the registration of the sql variable under the given name failed. * @throws CatalogException if the registration of the sql variable under the given name failed.
* But at the moment, with CatalogException, not SqlException * But at the moment, with CatalogException, not SqlException
*/ */
public void registerVariable(String variableName, String variable) { public void registerVariable(String variableName, String variable) {
checkArgument(!StringUtils.isNullOrWhitespaceOnly(variableName), "sql variable name cannot be null or empty."); checkArgument(!StringUtils.isNullOrWhitespaceOnly(variableName), "sql variable name cannot be null or empty.");
...@@ -92,7 +92,7 @@ public final class VariableManager { ...@@ -92,7 +92,7 @@ public final class VariableManager {
* *
* @param variableMap a variable map of sql to register * @param variableMap a variable map of sql to register
* @throws CatalogException if the registration of the sql variable under the given name failed. * @throws CatalogException if the registration of the sql variable under the given name failed.
* But at the moment, with CatalogException, not SqlException * But at the moment, with CatalogException, not SqlException
*/ */
public void registerVariable(Map<String, String> variableMap) { public void registerVariable(Map<String, String> variableMap) {
if (Asserts.isNotNull(variableMap)) { if (Asserts.isNotNull(variableMap)) {
...@@ -103,11 +103,11 @@ public final class VariableManager { ...@@ -103,11 +103,11 @@ public final class VariableManager {
/** /**
* Unregisters a variable of sql under the given name. The sql variable name must be existed. * Unregisters a variable of sql under the given name. The sql variable name must be existed.
* *
* @param variableName name under which to unregister the given sql variable. * @param variableName name under which to unregister the given sql variable.
* @param ignoreIfNotExists If false exception will be thrown if the variable of sql to be * @param ignoreIfNotExists If false exception will be thrown if the variable of sql to be
* altered does not exist. * altered does not exist.
* @throws CatalogException if the unregistration of the sql variable under the given name * @throws CatalogException if the unregistration of the sql variable under the given name
* failed. But at the moment, with CatalogException, not SqlException * failed. But at the moment, with CatalogException, not SqlException
*/ */
public void unregisterVariable(String variableName, boolean ignoreIfNotExists) { public void unregisterVariable(String variableName, boolean ignoreIfNotExists) {
checkArgument( checkArgument(
...@@ -125,7 +125,7 @@ public final class VariableManager { ...@@ -125,7 +125,7 @@ public final class VariableManager {
* *
* @param variableName name under which to unregister the given sql variable. * @param variableName name under which to unregister the given sql variable.
* @throws CatalogException if the unregistration of the sql variable under the given name * @throws CatalogException if the unregistration of the sql variable under the given name
* failed. But at the moment, with CatalogException, not SqlException * failed. But at the moment, with CatalogException, not SqlException
*/ */
public String getVariable(String variableName) { public String getVariable(String variableName) {
checkArgument( checkArgument(
...@@ -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));
...@@ -162,7 +164,7 @@ public final class VariableManager { ...@@ -162,7 +164,7 @@ public final class VariableManager {
* Get a variable of sql under the given name. The sql variable name must be existed. * Get a variable of sql under the given name. The sql variable name must be existed.
* *
* @throws CatalogException if the unregistration of the sql variable under the given name * @throws CatalogException if the unregistration of the sql variable under the given name
* failed. But at the moment, with CatalogException, not SqlException * failed. But at the moment, with CatalogException, not SqlException
*/ */
public Map<String, String> getVariable() { public Map<String, String> getVariable() {
return variables; return variables;
...@@ -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) {
value = parseDateVariable(key); if (isInnerDateVariable(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