1818
1919import static com .google .edwmigration .dumper .application .dumper .connector .snowflake .SnowflakeInput .SCHEMA_ONLY_SOURCE ;
2020import static com .google .edwmigration .dumper .application .dumper .utils .ArchiveNameUtil .getEntryFileNameWithTimestamp ;
21+ import static org .apache .commons .lang3 .StringUtils .isBlank ;
2122
2223import com .google .auto .service .AutoService ;
2324import com .google .common .base .CaseFormat ;
@@ -225,11 +226,7 @@ private String createQueryFromAccountUsage(ConnectorArguments arguments)
225226 + "FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY\n "
226227 + "WHERE end_time >= to_timestamp_ltz('%s')\n "
227228 + "AND end_time <= to_timestamp_ltz('%s')\n " );
228- if (!StringUtils .isBlank (arguments .getQueryLogEarliestTimestamp ()))
229- queryBuilder
230- .append ("AND start_time >= " )
231- .append (arguments .getQueryLogEarliestTimestamp ())
232- .append ("\n " );
229+ queryBuilder .append (extractEarliestTimestamp (arguments ));
233230 if (overrideWhere != null ) queryBuilder .append (" AND " ).append (overrideWhere );
234231 return queryBuilder .toString ().replace ('\n' , ' ' );
235232 }
@@ -242,8 +239,6 @@ private String createQueryFromInformationSchema(ConnectorArguments arguments)
242239 String overrideQuery = getOverrideQuery (arguments );
243240 if (overrideQuery != null ) return overrideQuery ;
244241
245- String overrideWhere = getOverrideWhere (arguments );
246-
247242 @ SuppressWarnings ("OrphanedFormatString" )
248243 StringBuilder queryBuilder =
249244 new StringBuilder (
@@ -268,20 +263,18 @@ private String createQueryFromInformationSchema(ConnectorArguments arguments)
268263 // maximum range of 7 trailing days.
269264 + ",end_time_range_start=>to_timestamp_ltz('%s')\n "
270265 + ",end_time_range_end=>to_timestamp_ltz('%s')\n "
271- + "))\n " );
266+ // It makes leter formatting easier if we always have a 'WHERE'.
267+ + ")) WHERE 1=1\n " );
272268 // if the user specifies an earliest start time there will be extraneous empty dump files
273269 // because we always iterate over the full 7 trailing days; maybe it's worth
274270 // preventing that in the future. To do that, we should require getQueryLogEarliestTimestamp()
275271 // to parse and return an ISO instant, not a database-server-specific format.
276- // TODO: Use ZonedIntervalIterableGenerator.forConnectorArguments()
277- boolean appendStartTime = !StringUtils .isBlank (arguments .getQueryLogEarliestTimestamp ());
278- if (appendStartTime )
279- queryBuilder
280- .append ("WHERE start_time >= " )
281- .append (arguments .getQueryLogEarliestTimestamp ())
282- .append ("\n " );
283- if (overrideWhere != null )
284- queryBuilder .append (appendStartTime ? " AND " : "WHERE" ).append (overrideWhere );
272+ queryBuilder .append (extractEarliestTimestamp (arguments ));
273+
274+ String overrideWhere = getOverrideWhere (arguments );
275+ if (overrideWhere != null ) {
276+ queryBuilder .append (" AND " + overrideWhere );
277+ }
285278 return queryBuilder .toString ().replace ('\n' , ' ' );
286279 }
287280
@@ -343,15 +336,21 @@ private String createExtendedQueryFromAccountUsage(ConnectorArguments arguments)
343336 + "AND end_time <= to_timestamp_ltz('%s')\n "
344337 + "AND is_client_generated_statement = FALSE\n " );
345338
346- if (!StringUtils .isBlank (arguments .getQueryLogEarliestTimestamp ()))
347- queryBuilder
348- .append ("AND start_time >= " )
349- .append (arguments .getQueryLogEarliestTimestamp ())
350- .append ("\n " );
339+ queryBuilder .append (extractEarliestTimestamp (arguments ));
351340 if (overrideWhere != null ) queryBuilder .append (" AND " ).append (overrideWhere );
352341 return queryBuilder .toString ().replace ('\n' , ' ' );
353342 }
354343
344+ @ Nonnull
345+ static String extractEarliestTimestamp (@ Nonnull ConnectorArguments arguments ) {
346+ String timestamp = arguments .getQueryLogEarliestTimestamp ();
347+ if (isBlank (timestamp )) {
348+ return "" ;
349+ } else {
350+ return String .format ("AND start_time >= %s\n " , timestamp );
351+ }
352+ }
353+
355354 @ CheckForNull
356355 private String getOverrideQuery (@ Nonnull ConnectorArguments arguments )
357356 throws MetadataDumperUsageException {
0 commit comments