47
47
@ RequiredArgsConstructor (access = AccessLevel .PACKAGE )
48
48
class SqlExecutor {
49
49
50
- private static final Pattern SET_STATEMENT_PATTERN =
50
+ private static final Pattern SET_PATTERN =
51
51
Pattern .compile ("SET\\ s+'(\\ S+)'\\ s*=\\ s*'(.+)';?" , Pattern .CASE_INSENSITIVE );
52
52
53
+ private static final Pattern STATEMENT_SET_PATTERN =
54
+ Pattern .compile ("EXECUTE\\ s+STATEMENT\\ s+SET" , Pattern .CASE_INSENSITIVE | Pattern .DOTALL );
55
+
53
56
private final TableEnvironment tEnv ;
54
57
55
58
SqlExecutor (Configuration config , @ Nullable String udfPath ) {
@@ -115,8 +118,11 @@ void setupSystemFunctions() {
115
118
TableResult executeScript (String script ) {
116
119
var statements = SqlUtils .parseStatements (script );
117
120
TableResult tableResult = null ;
118
- for (String statement : statements ) {
119
- tableResult = executeStatement (statement );
121
+
122
+ var it = statements .iterator ();
123
+ while (it .hasNext ()) {
124
+ var statement = it .next ();
125
+ tableResult = executeStatement (statement , it .hasNext ());
120
126
}
121
127
122
128
return tableResult ;
@@ -141,24 +147,29 @@ TableResult executeCompiledPlan(String planJson) {
141
147
}
142
148
}
143
149
144
- private TableResult executeStatement (String statement ) {
150
+ private TableResult executeStatement (String statement , boolean intermediate ) {
145
151
TableResult tableResult = null ;
146
152
try {
147
- var setMatcher = SET_STATEMENT_PATTERN .matcher (statement .trim ());
153
+ var setMatcher = SET_PATTERN .matcher (statement .trim ());
154
+ var statementSetMatcher = STATEMENT_SET_PATTERN .matcher (statement .trim ());
148
155
149
156
if (setMatcher .matches ()) {
150
157
// Handle SET statements
151
158
var key = setMatcher .group (1 );
152
159
var value = setMatcher .group (2 );
153
160
tEnv .getConfig ().getConfiguration ().setString (key , value );
154
161
log .info ("Set configuration: {} = {}" , key , value );
162
+
155
163
} else {
156
164
log .info ("Executing statement:\n {}" , statement );
157
165
tableResult = tEnv .executeSql (statement );
166
+ if (statementSetMatcher .find () && intermediate ) {
167
+ log .debug ("Make sure to wait intermediate statement set to finish..." );
168
+ tableResult .await ();
169
+ }
158
170
}
159
171
} catch (Exception e ) {
160
- e .addSuppressed (new RuntimeException ("Error while executing stmt: " + statement ));
161
- throw e ;
172
+ throw new RuntimeException ("Error while executing stmt: " + statement , e );
162
173
}
163
174
return tableResult ;
164
175
}
0 commit comments