@@ -125,23 +125,27 @@ public int hashCode() {
125125 private final List <String > whereConditions ;
126126 private final Optional <String > limitStr ;
127127 private final Optional <ImmutableSet <String >> excludedColumns ;
128+ private final Optional <String > splitColumn ;
128129
129130 private QueryBuilder (final QueryBase base ) {
130131 this .base = base ;
131132 this .limitStr = Optional .empty ();
132133 this .whereConditions = ImmutableList .of ();
133134 this .excludedColumns = Optional .empty ();
135+ this .splitColumn = Optional .empty ();
134136 }
135137
136138 private QueryBuilder (
137139 final QueryBase base ,
138140 final List <String > whereConditions ,
139141 final Optional <String > limitStr ,
140- final Optional <ImmutableSet <String >> excludedColumns ) {
142+ final Optional <ImmutableSet <String >> excludedColumns ,
143+ final Optional <String > splitColumn ) {
141144 this .base = base ;
142145 this .whereConditions = whereConditions ;
143146 this .limitStr = limitStr ;
144147 this .excludedColumns = excludedColumns ;
148+ this .splitColumn = splitColumn ;
145149 }
146150
147151 public static QueryBuilder fromTablename (final String tableName ) {
@@ -162,20 +166,29 @@ public QueryBuilder withPartitionCondition(
162166 createSqlPartitionCondition (partitionColumn , startPointIncl , endPointExcl )))
163167 .collect (Collectors .toList ()),
164168 this .limitStr ,
165- this .excludedColumns );
169+ this .excludedColumns ,
170+ this .splitColumn );
171+ }
172+
173+ public QueryBuilder withSplitColumn (final Optional <String > splitColumn ) {
174+ return new QueryBuilder (
175+ this .base , this .whereConditions , this .limitStr , this .excludedColumns , splitColumn );
166176 }
167177
168178 public QueryBuilder withExcludedColumns (final Optional <ImmutableSet <String >> excludedColumns ) {
169179 if (excludedColumns .isPresent () && this .base instanceof UserQueryBase ) {
170180 UserQueryBase userQueryBase = (UserQueryBase ) this .base ;
171- String newSqlQuery = rebuildSelectClause (userQueryBase .userSqlQuery , excludedColumns .get ());
181+ String newSqlQuery =
182+ rebuildSelectClause (userQueryBase .userSqlQuery , excludedColumns .get (), this .splitColumn );
172183 return new QueryBuilder (
173184 new UserQueryBase (newSqlQuery , userQueryBase .selectClause ),
174185 this .whereConditions ,
175186 this .limitStr ,
176- excludedColumns );
187+ excludedColumns ,
188+ this .splitColumn );
177189 } else {
178- return new QueryBuilder (this .base , this .whereConditions , this .limitStr , excludedColumns );
190+ return new QueryBuilder (
191+ this .base , this .whereConditions , this .limitStr , excludedColumns , this .splitColumn );
179192 }
180193 }
181194
@@ -200,7 +213,8 @@ public QueryBuilder withParallelizationCondition(
200213 partitionColumn , startPointIncl , endPoint , isEndPointExcl )))
201214 .collect (Collectors .toList ()),
202215 this .limitStr ,
203- this .excludedColumns );
216+ this .excludedColumns ,
217+ this .splitColumn );
204218 }
205219
206220 private static String createSqlSplitCondition (
@@ -235,7 +249,7 @@ private static String removeTrailingSymbols(String sqlQuery) {
235249 }
236250
237251 private static String rebuildSelectClause (
238- String sqlQuery , ImmutableSet <String > excludedColumns ) {
252+ String sqlQuery , ImmutableSet <String > excludedColumns , Optional < String > splitColumn ) {
239253 String lowerCaseQuery = sqlQuery .toLowerCase ();
240254 int selectIdx = lowerCaseQuery .indexOf ("select" );
241255 int fromIdx = lowerCaseQuery .indexOf ("from" );
@@ -247,12 +261,25 @@ private static String rebuildSelectClause(
247261
248262 String selectClause = sqlQuery .substring (selectIdx + "select" .length (), fromIdx ).trim ();
249263 String [] columns = selectClause .split ("," );
250- List <String > newColumns =
264+ List <String > newColumns =
251265 Stream .of (columns )
252266 .map (String ::trim )
253- .filter (column -> !excludedColumns .contains (column ))
267+ .filter (
268+ column -> {
269+ if (splitColumn .isPresent () && column .equals (splitColumn .get ())) {
270+ return true ;
271+ }
272+ return !excludedColumns .contains (column );
273+ })
254274 .collect (Collectors .toList ());
255275
276+ if (splitColumn .isPresent ()) {
277+ boolean exists = newColumns .stream ().anyMatch (c -> c .equals (splitColumn .get ()));
278+ if (!exists ) {
279+ newColumns .add (splitColumn .get ());
280+ }
281+ }
282+
256283 if (newColumns .isEmpty ()) {
257284 return "SELECT * " + sqlQuery .substring (fromIdx );
258285 } else {
@@ -265,7 +292,8 @@ public QueryBuilder withLimit(long limit) {
265292 this .base ,
266293 this .whereConditions ,
267294 Optional .of (String .format (" LIMIT %d" , limit )),
268- this .excludedColumns );
295+ this .excludedColumns ,
296+ this .splitColumn );
269297 }
270298
271299 @ Override
@@ -294,9 +322,10 @@ public QueryBuilder resolveSelect(final Connection connection) throws SQLExcepti
294322 if (this .excludedColumns .isPresent ()) {
295323 String queryToCheck = this .base .getBaseSql () + " AND 1=0" ;
296324 List <String > columns = getColumnsFromQuery (connection , queryToCheck );
297- List <String > filteredColumns = columns .stream ()
298- .filter (c -> !this .excludedColumns .get ().contains (c ))
299- .collect (Collectors .toList ());
325+ List <String > filteredColumns =
326+ columns .stream ()
327+ .filter (c -> !this .excludedColumns .get ().contains (c ))
328+ .collect (Collectors .toList ());
300329
301330 if (filteredColumns .isEmpty ()) {
302331 throw new SQLException ("All columns excluded for query: " + queryToCheck );
@@ -307,7 +336,8 @@ public QueryBuilder resolveSelect(final Connection connection) throws SQLExcepti
307336 this .base .withSelect (selectClause ),
308337 this .whereConditions ,
309338 this .limitStr ,
310- this .excludedColumns );
339+ this .excludedColumns ,
340+ this .splitColumn );
311341 }
312342 return this ;
313343 }
@@ -352,6 +382,7 @@ public QueryBuilder generateQueryToGetLimitsOfSplitColumn(
352382 base .withSelect (selectMinMax ),
353383 this .whereConditions ,
354384 this .limitStr ,
355- this .excludedColumns );
385+ this .excludedColumns ,
386+ this .splitColumn );
356387 }
357388}
0 commit comments