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
2bfa4e24
Unverified
Commit
2bfa4e24
authored
Nov 28, 2022
by
dzygcc
Committed by
GitHub
Nov 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Feature] flinkSQL 添加内置日期变量 (#1320)
* add inner date var. * fix check style. Co-authored-by:
duzhongyi
<
duzhongyi@itiger.com
>
parent
3fb9afe6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
6 deletions
+84
-6
FlinkSQLConstant.java
...or/src/main/java/com/dlink/constant/FlinkSQLConstant.java
+13
-0
SqlManager.java
...executor/src/main/java/com/dlink/executor/SqlManager.java
+71
-6
No files found.
dlink-executor/src/main/java/com/dlink/constant/FlinkSQLConstant.java
View file @
2bfa4e24
...
@@ -26,6 +26,7 @@ package com.dlink.constant;
...
@@ -26,6 +26,7 @@ package com.dlink.constant;
* @since 2021/5/25 15:51
* @since 2021/5/25 15:51
**/
**/
public
interface
FlinkSQLConstant
{
public
interface
FlinkSQLConstant
{
/**
/**
* 分隔符
* 分隔符
*/
*/
...
@@ -46,4 +47,16 @@ public interface FlinkSQLConstant {
...
@@ -46,4 +47,16 @@ public interface FlinkSQLConstant {
* 片段 Fragments 标识
* 片段 Fragments 标识
*/
*/
String
FRAGMENTS
=
":="
;
String
FRAGMENTS
=
":="
;
/**
* 内置日期变量前缀
*/
String
INNER_DATETIME_KEY
=
"_CURRENT_DATE_"
;
/**
* 内置日期变量格式
* 确定后不能修改
*/
String
INNER_DATETIME_FORMAT
=
"yyyyMMdd"
;
}
}
dlink-executor/src/main/java/com/dlink/executor/SqlManager.java
View file @
2bfa4e24
...
@@ -36,8 +36,11 @@ import org.apache.flink.table.catalog.exceptions.CatalogException;
...
@@ -36,8 +36,11 @@ import org.apache.flink.table.catalog.exceptions.CatalogException;
import
org.apache.flink.types.Row
;
import
org.apache.flink.types.Row
;
import
org.apache.flink.util.StringUtils
;
import
org.apache.flink.util.StringUtils
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
...
@@ -79,7 +82,8 @@ public final class SqlManager {
...
@@ -79,7 +82,8 @@ public final class SqlManager {
* But at the moment, with CatalogException, not SqlException
* But at the moment, with CatalogException, not SqlException
*/
*/
public
void
registerSqlFragment
(
String
sqlFragmentName
,
String
sqlFragment
)
{
public
void
registerSqlFragment
(
String
sqlFragmentName
,
String
sqlFragment
)
{
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragment name cannot be null or empty."
);
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragment name cannot be null or empty."
);
checkNotNull
(
sqlFragment
,
"sql fragment cannot be null"
);
checkNotNull
(
sqlFragment
,
"sql fragment cannot be null"
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
...
@@ -112,7 +116,8 @@ public final class SqlManager {
...
@@ -112,7 +116,8 @@ public final class SqlManager {
* failed. But at the moment, with CatalogException, not SqlException
* failed. But at the moment, with CatalogException, not SqlException
*/
*/
public
void
unregisterSqlFragment
(
String
sqlFragmentName
,
boolean
ignoreIfNotExists
)
{
public
void
unregisterSqlFragment
(
String
sqlFragmentName
,
boolean
ignoreIfNotExists
)
{
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
sqlFragments
.
remove
(
sqlFragmentName
);
sqlFragments
.
remove
(
sqlFragmentName
);
...
@@ -129,22 +134,30 @@ public final class SqlManager {
...
@@ -129,22 +134,30 @@ public final class SqlManager {
* failed. But at the moment, with CatalogException, not SqlException
* failed. But at the moment, with CatalogException, not SqlException
*/
*/
public
String
getSqlFragment
(
String
sqlFragmentName
)
{
public
String
getSqlFragment
(
String
sqlFragmentName
)
{
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
return
sqlFragments
.
get
(
sqlFragmentName
);
return
sqlFragments
.
get
(
sqlFragmentName
);
}
}
if
(
isInnerDateVar
(
sqlFragmentName
))
{
return
parseDateVar
(
sqlFragmentName
);
}
throw
new
CatalogException
(
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
throw
new
CatalogException
(
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
}
}
public
TableResult
getSqlFragmentResult
(
String
sqlFragmentName
)
{
public
TableResult
getSqlFragmentResult
(
String
sqlFragmentName
)
{
if
(
Asserts
.
isNullString
(
sqlFragmentName
))
{
if
(
Asserts
.
isNullString
(
sqlFragmentName
))
{
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
())),
new
ArrayList
<>());
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
())),
new
ArrayList
<>());
}
}
String
sqlFragment
=
getSqlFragment
(
sqlFragmentName
);
String
sqlFragment
=
getSqlFragment
(
sqlFragmentName
);
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
())),
Collections
.
singletonList
(
Row
.
of
(
sqlFragment
)));
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
())),
Collections
.
singletonList
(
Row
.
of
(
sqlFragment
)));
}
}
/**
/**
...
@@ -162,7 +175,8 @@ public final class SqlManager {
...
@@ -162,7 +175,8 @@ public final class SqlManager {
for
(
String
key
:
sqlFragments
.
keySet
())
{
for
(
String
key
:
sqlFragments
.
keySet
())
{
rows
.
add
(
Row
.
of
(
key
));
rows
.
add
(
Row
.
of
(
key
));
}
}
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
"fragmentName"
,
DataTypes
.
STRING
())),
rows
);
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
"fragmentName"
,
DataTypes
.
STRING
())),
rows
);
}
}
public
Iterator
getSqlFragmentsIterator
()
{
public
Iterator
getSqlFragmentsIterator
()
{
...
@@ -221,9 +235,60 @@ public final class SqlManager {
...
@@ -221,9 +235,60 @@ public final class SqlManager {
String
key
=
m
.
group
(
1
);
String
key
=
m
.
group
(
1
);
String
value
=
this
.
getSqlFragment
(
key
);
String
value
=
this
.
getSqlFragment
(
key
);
m
.
appendReplacement
(
sb
,
""
);
m
.
appendReplacement
(
sb
,
""
);
// 内置时间变量的情况
if
(
value
==
null
&&
isInnerDateVar
(
key
))
{
value
=
parseDateVar
(
key
);
}
sb
.
append
(
value
==
null
?
""
:
value
);
sb
.
append
(
value
==
null
?
""
:
value
);
}
}
m
.
appendTail
(
sb
);
m
.
appendTail
(
sb
);
return
sb
.
toString
();
return
sb
.
toString
();
}
}
/**
* verify if key is inner variable
* @param key
* @return
*/
private
boolean
isInnerDateVar
(
String
key
)
{
if
(
key
.
startsWith
(
FlinkSQLConstant
.
INNER_DATETIME_KEY
))
{
return
true
;
}
return
false
;
}
/**
* parse datetime var
* @param key
* @return
*/
private
String
parseDateVar
(
String
key
)
{
int
days
=
0
;
try
{
if
(
key
.
contains
(
"+"
))
{
int
s
=
key
.
indexOf
(
"+"
);
String
num
=
key
.
substring
(
s
).
trim
();
days
=
Integer
.
parseInt
(
num
);
}
else
if
(
key
.
contains
(
"-"
))
{
int
s
=
key
.
indexOf
(
"-"
);
String
num
=
key
.
substring
(
s
).
trim
();
days
=
Integer
.
parseInt
(
num
)
*
-
1
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
SimpleDateFormat
dtf
=
new
SimpleDateFormat
(
FlinkSQLConstant
.
INNER_DATETIME_FORMAT
);
Date
endDate
=
new
Date
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
endDate
);
calendar
.
add
(
Calendar
.
DAY_OF_YEAR
,
days
);
Date
startDate
=
calendar
.
getTime
();
return
dtf
.
format
(
startDate
);
}
}
}
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