Skip to content

Commit 79a68a3

Browse files
committed
Add test for earlies timestamp
1 parent e301ca9 commit 79a68a3

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/snowflake/SnowflakeLogsConnector.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.edwmigration.dumper.application.dumper.connector.snowflake.SnowflakeInput.SCHEMA_ONLY_SOURCE;
2020
import static com.google.edwmigration.dumper.application.dumper.utils.ArchiveNameUtil.getEntryFileNameWithTimestamp;
21+
import static org.apache.commons.lang3.StringUtils.isBlank;
2122

2223
import com.google.auto.service.AutoService;
2324
import 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 {

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/connector/snowflake/SnowflakeLogsConnectorTest.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
1818

19-
import com.google.common.base.Predicates;
19+
import static com.google.common.base.Predicates.alwaysTrue;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
22+
2023
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
2124
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
22-
import com.google.edwmigration.dumper.plugin.lib.dumper.spi.SnowflakeLogsDumpFormat;
2325
import com.google.edwmigration.dumper.test.TestUtils;
2426
import java.io.File;
2527
import java.io.IOException;
@@ -30,20 +32,48 @@
3032

3133
/** @author shevek */
3234
@RunWith(JUnit4.class)
33-
public class SnowflakeLogsConnectorTest extends AbstractSnowflakeConnectorExecutionTest {
34-
35-
private final SnowflakeLogsConnector connector = new SnowflakeLogsConnector();
35+
public class SnowflakeLogsConnectorTest {
3636

3737
@Test
3838
public void testExecution() throws Exception {
39+
class ExecutionTest extends AbstractSnowflakeConnectorExecutionTest {
40+
void test(File output) {
41+
if (run(ARGS(new SnowflakeLogsConnector(), output))) {
42+
new ZipValidator()
43+
.withFormat("snowflake.logs.zip")
44+
.withAllowedEntries(alwaysTrue())
45+
.run(output);
46+
}
47+
}
48+
}
3949
File outputFile = TestUtils.newOutputFile("compilerworks-snowflake-logs-au.zip");
40-
if (!run(ARGS(connector, outputFile))) return;
4150

42-
ZipValidator validator = new ZipValidator().withFormat(SnowflakeLogsDumpFormat.FORMAT_NAME);
51+
new ExecutionTest().test(outputFile);
52+
}
53+
54+
@Test
55+
public void extractEarliestTimestamp_notProvided_emptyResult() throws IOException {
56+
ConnectorArguments arguments = new ConnectorArguments("--connector", "snowflake-logs");
57+
58+
String result = SnowflakeLogsConnector.extractEarliestTimestamp(arguments);
59+
60+
assertEquals("", result);
61+
}
62+
63+
@Test
64+
public void extractEarliestTimestamp_provided_resultMatches() throws IOException {
65+
ConnectorArguments arguments =
66+
new ConnectorArguments(
67+
"--connector",
68+
"snowflake-logs",
69+
"--" + ConnectorArguments.OPT_QUERY_LOG_EARLIEST_TIMESTAMP,
70+
"2024-03-21");
4371

44-
validator.withAllowedEntries(Predicates.alwaysTrue()); // Permit any files.
72+
String result = SnowflakeLogsConnector.extractEarliestTimestamp(arguments);
4573

46-
validator.run(outputFile);
74+
assertTrue(result, result.contains("2024-03-21"));
75+
assertTrue(result, result.contains("start_time"));
76+
assertTrue(result, result.endsWith("\n"));
4777
}
4878

4979
@Test

0 commit comments

Comments
 (0)