Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
9f994585
Unverified
Commit
9f994585
authored
Oct 18, 2023
by
cainiao-cnj
Committed by
GitHub
Oct 18, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Optimize][Flink]Add Flink Table global variable (#2395)
* 添加内置全局变量时间戳 _CURRENT_TIMESTAMP_ * fix code style --------- Co-authored-by:
shuhang.chang
<
shuhang.chang@mtime
>
parent
d4f0a00f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
11 deletions
+55
-11
FlinkSQLConstant.java
...or/src/main/java/org/dinky/constant/FlinkSQLConstant.java
+3
-1
VariableManager.java
...tor/src/main/java/org/dinky/executor/VariableManager.java
+52
-10
No files found.
dinky-executor/src/main/java/org/dinky/constant/FlinkSQLConstant.java
View file @
9f994585
...
...
@@ -25,7 +25,6 @@ package org.dinky.constant;
* @since 2021/5/25 15:51
*/
public
interface
FlinkSQLConstant
{
/** 分隔符 */
String
SEPARATOR
=
";\n"
;
/** DDL 类型 */
...
...
@@ -41,6 +40,9 @@ public interface FlinkSQLConstant {
/** The define identifier of FlinkSQL Date Variable */
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"
;
}
dinky-executor/src/main/java/org/dinky/executor/VariableManager.java
View file @
9f994585
...
...
@@ -72,9 +72,9 @@ public final class VariableManager {
* 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 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.
* But at the moment, with CatalogException, not SqlException
*
But at the moment, with CatalogException, not SqlException
*/
public
void
registerVariable
(
String
variableName
,
String
variable
)
{
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
variableName
),
"sql variable name cannot be null or empty."
);
...
...
@@ -92,7 +92,7 @@ public final class VariableManager {
*
* @param variableMap a variable map of sql to register
* @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
)
{
if
(
Asserts
.
isNotNull
(
variableMap
))
{
...
...
@@ -103,11 +103,11 @@ public final class VariableManager {
/**
* 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
* altered does not exist.
*
altered does not exist.
* @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
)
{
checkArgument
(
...
...
@@ -125,7 +125,7 @@ public final class VariableManager {
*
* @param variableName name under which to unregister the given sql variable.
* @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
)
{
checkArgument
(
...
...
@@ -137,6 +137,8 @@ public final class VariableManager {
if
(
isInnerDateVariable
(
variableName
))
{
return
parseDateVariable
(
variableName
);
}
else
if
(
isInnerTimestampVariable
(
variableName
))
{
return
parseTimestampVar
(
variableName
);
}
throw
new
CatalogException
(
format
(
"The variable of sql %s does not exist."
,
variableName
));
...
...
@@ -162,7 +164,7 @@ public final class VariableManager {
* 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
* failed. But at the moment, with CatalogException, not SqlException
*
failed. But at the moment, with CatalogException, not SqlException
*/
public
Map
<
String
,
String
>
getVariable
()
{
return
variables
;
...
...
@@ -231,8 +233,12 @@ public final class VariableManager {
m
.
appendReplacement
(
sb
,
""
);
// if value is null, parse inner date variable
if
(
value
==
null
&&
isInnerDateVariable
(
key
))
{
value
=
parseDateVariable
(
key
);
if
(
value
==
null
)
{
if
(
isInnerDateVariable
(
key
))
{
value
=
parseDateVariable
(
key
);
}
else
if
(
isInnerTimestampVariable
(
key
))
{
value
=
parseTimestampVar
(
key
);
}
}
sb
.
append
(
value
==
null
?
""
:
value
);
...
...
@@ -251,6 +257,16 @@ public final class VariableManager {
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
*
...
...
@@ -278,4 +294,30 @@ public final class VariableManager {
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment