Skip to content

Commit a536287

Browse files
committed
fix generate query method
1 parent 266ac5b commit a536287

File tree

2 files changed

+107
-58
lines changed

2 files changed

+107
-58
lines changed

src/main/java/io/cdap/plugin/gcp/bigquery/source/PartitionedBigQueryInputFormat.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ String generateQuery(String partitionFromDate, String partitionToDate, String fi
175175
String datasetProject, String dataset, String table, String limit, String orderBy,
176176
Boolean isPartitionFilterRequired, StandardTableDefinition tableDefinition) {
177177

178+
if (Strings.isNullOrEmpty(filter) && Strings.isNullOrEmpty(orderBy) && Strings.isNullOrEmpty(
179+
limit)
180+
&& Strings.isNullOrEmpty(partitionFromDate) && Strings.isNullOrEmpty(partitionToDate)) {
181+
return null;
182+
}
183+
178184
RangePartitioning rangePartitioning = tableDefinition.getRangePartitioning();
179185
TimePartitioning timePartitioning = tableDefinition.getTimePartitioning();
180186
StringBuilder condition = new StringBuilder();

src/test/java/io/cdap/plugin/gcp/bigquery/source/PartitionedBigQueryInputFormatTest.java

Lines changed: 101 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333

3434
@RunWith(MockitoJUnitRunner.class)
3535
public class PartitionedBigQueryInputFormatTest {
36+
3637
private static final String TEST_PROJECT = "test-project";
3738
private static final String TEST_DATASET = "test-dataset";
3839
private static final String TEST_TABLE = "test-table";
3940
private static final String TEST_FILTER = "age > 10";
4041
private static final String TEST_LIMIT = "100";
4142
private static final String TEST_ORDER_BY = "name asc";
42-
private static final String TEST_TABLE_SPEC = String.format("%s.%s.%s", TEST_PROJECT, TEST_DATASET, TEST_TABLE);
43+
private static final String TEST_TABLE_SPEC = String.format("%s.%s.%s", TEST_PROJECT,
44+
TEST_DATASET, TEST_TABLE);
4345
private static final String TEST_FROM_DATE = "2025-01-01";
4446
private static final String TEST_TO_DATE = "2025-01-02";
4547
private static final String TEST_PARTITION_CONDITION =
@@ -122,17 +124,6 @@ public void testGenerateQuery_WithFilterOnly() {
122124
Assert.assertEquals(expectedQuery, generatedQuery);
123125
}
124126

125-
@Test
126-
public void testGenerateQuery_NoOptions() {
127-
String expectedQuery = String.format("select * from %s", TEST_TABLE_SPEC);
128-
129-
String generatedQuery = format.generateQuery(null, null,
130-
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
131-
null, null,
132-
false, mockTableDefinition);
133-
Assert.assertEquals(expectedQuery, generatedQuery);
134-
}
135-
136127
@Test
137128
public void testGenerateQuery_AllOptions() {
138129
String expectedQuery = String.format("select * from %s where %s order by %s limit %s",
@@ -161,38 +152,6 @@ public void testGenerateQuery_TimePartitionWithDates() {
161152
Assert.assertEquals(expectedQuery, generatedQuery);
162153
}
163154

164-
165-
@Test
166-
public void testGenerateQuery_TimePartitionRequiredNoDates() {
167-
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
168-
when(mockTimePartitioning.getField()).thenReturn(null);
169-
170-
String expectedQuery = String.format("select * from %s where %s",
171-
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_CONDITION);
172-
173-
String generatedQuery = format.generateQuery(null, null,
174-
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
175-
null, null,
176-
true, mockTableDefinition);
177-
Assert.assertEquals(expectedQuery, generatedQuery);
178-
}
179-
180-
@Test
181-
public void testGenerateQuery_RangePartitionRequiredNoDates() {
182-
when(mockTableDefinition.getTimePartitioning()).thenReturn(null);
183-
when(mockTableDefinition.getRangePartitioning()).thenReturn(mockRangePartitioning);
184-
when(mockRangePartitioning.getField()).thenReturn("range_col");
185-
186-
String expectedQuery = String.format("select * from %s where %s",
187-
TEST_TABLE_SPEC, TEST_DEFAULT_RANGE_CONDITION);
188-
189-
String generatedQuery = format.generateQuery(null, null, null,
190-
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
191-
null, null,
192-
true, mockTableDefinition);
193-
Assert.assertEquals(expectedQuery, generatedQuery);
194-
}
195-
196155
@Test
197156
public void testGenerateQuery_TimePartitionRequiredAndFilter() {
198157
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
@@ -227,22 +186,56 @@ public void testGenerateQuery_TimeUnitPartitionWithDates() {
227186
}
228187

229188
@Test
230-
public void testGenerateQuery_TimeUnitPartitionRequiredNoDates() {
189+
public void testGenerateQuery_TimePartitionFilterNotRequiredWithDates() {
231190
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
232-
when(mockTimePartitioning.getField()).thenReturn(TEST_TIME_UNIT_COL);
191+
when(mockTimePartitioning.getField()).thenReturn(null);
233192

234-
String expectedQuery = String.format("select * from %s where %s",
235-
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION);
193+
String expectedQuery = String.format("select * from %s where (%s)",
194+
TEST_TABLE_SPEC,
195+
TEST_PARTITION_CONDITION);
236196

237-
String generatedQuery = format.generateQuery(null, null, null,
197+
String generatedQuery = format.generateQuery(TEST_FROM_DATE, TEST_TO_DATE, null,
238198
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
239199
null, null,
240-
true, mockTableDefinition);
200+
false, mockTableDefinition);
241201
Assert.assertEquals(expectedQuery, generatedQuery);
242202
}
243203

244204
@Test
245-
public void testGenerateQuery_TimePartitionFilterNotRequiredWithDates() {
205+
public void testGenerateQuery_NoOptions_ShouldReturnNull() {
206+
String generatedQuery = format.generateQuery(null, null,
207+
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
208+
null, null,
209+
false, mockTableDefinition);
210+
Assert.assertNull("Query should be null if no filters or options are set.", generatedQuery);
211+
}
212+
213+
@Test
214+
public void testGenerateQuery_WithLimitOnly_ShouldAssertQuery() {
215+
String expectedQuery = String.format("select * from %s limit %s", TEST_TABLE_SPEC,
216+
TEST_LIMIT);
217+
218+
String generatedQuery = format.generateQuery(null, null,
219+
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
220+
TEST_LIMIT, null,
221+
false, mockTableDefinition);
222+
Assert.assertEquals(expectedQuery, generatedQuery);
223+
}
224+
225+
@Test
226+
public void testGenerateQuery_WithOrderByOnly_ShouldAssertQuery() {
227+
String expectedQuery = String.format("select * from %s order by %s", TEST_TABLE_SPEC,
228+
TEST_ORDER_BY);
229+
230+
String generatedQuery = format.generateQuery(null, null,
231+
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
232+
null, TEST_ORDER_BY,
233+
false, mockTableDefinition);
234+
Assert.assertEquals(expectedQuery, generatedQuery);
235+
}
236+
237+
@Test
238+
public void testGenerateQuery_TimePartitionNotRequired_WithDates_ShouldAssertQuery() {
246239
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
247240
when(mockTimePartitioning.getField()).thenReturn(null);
248241

@@ -258,27 +251,77 @@ public void testGenerateQuery_TimePartitionFilterNotRequiredWithDates() {
258251
}
259252

260253
@Test
261-
public void testGenerateQuery_TimePartitionFilterNotRequiredNoDates() {
254+
public void testGenerateQuery_TimePartitionRequired_WithFilterOnly_ShouldAssertQuery() {
262255
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
256+
when(mockTimePartitioning.getField()).thenReturn(null);
263257

264-
String expectedQuery = String.format("select * from %s", TEST_TABLE_SPEC);
265-
String generatedQuery = format.generateQuery(null, null, null,
258+
String expectedQuery = String.format("select * from %s where %s and (%s)",
259+
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_CONDITION, TEST_FILTER);
260+
261+
String generatedQuery = format.generateQuery(null, null,
262+
TEST_FILTER, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
263+
null, null,
264+
true, mockTableDefinition);
265+
Assert.assertEquals(expectedQuery, generatedQuery);
266+
}
267+
268+
@Test
269+
public void testGenerateQuery_RangePartitionRequiredAndFilter() {
270+
when(mockTableDefinition.getRangePartitioning()).thenReturn(mockRangePartitioning);
271+
when(mockRangePartitioning.getField()).thenReturn("range_col");
272+
273+
String expectedQuery = String.format("select * from %s where %s and (%s)",
274+
TEST_TABLE_SPEC, TEST_DEFAULT_RANGE_CONDITION, TEST_FILTER);
275+
276+
String generatedQuery = format.generateQuery(null, null, TEST_FILTER,
266277
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
267278
null, null,
268-
false, mockTableDefinition);
279+
true, mockTableDefinition);
269280
Assert.assertEquals(expectedQuery, generatedQuery);
270281
}
271282

272283
@Test
273-
public void testGenerateQuery_RangePartitionFilterNotRequired() {
284+
public void testGenerateQuery_RangePartitionRequiredWithLimit() {
274285
when(mockTableDefinition.getRangePartitioning()).thenReturn(mockRangePartitioning);
286+
when(mockRangePartitioning.getField()).thenReturn("range_col");
275287

276-
String expectedQuery = String.format("select * from %s", TEST_TABLE_SPEC);
288+
String expectedQuery = String.format("select * from %s where %s limit %s",
289+
TEST_TABLE_SPEC, TEST_DEFAULT_RANGE_CONDITION, TEST_LIMIT);
277290

278291
String generatedQuery = format.generateQuery(null, null, null,
292+
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
293+
TEST_LIMIT, null,
294+
true, mockTableDefinition);
295+
Assert.assertEquals(expectedQuery, generatedQuery);
296+
}
297+
298+
@Test
299+
public void testGenerateQuery_TimeUnitPartitionRequiredAndFilter() {
300+
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
301+
when(mockTimePartitioning.getField()).thenReturn(TEST_TIME_UNIT_COL);
302+
303+
String expectedQuery = String.format("select * from %s where %s and (%s)",
304+
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION, TEST_FILTER);
305+
306+
String generatedQuery = format.generateQuery(null, null, TEST_FILTER,
279307
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
280308
null, null,
281-
false, mockTableDefinition);
309+
true, mockTableDefinition);
310+
Assert.assertEquals(expectedQuery, generatedQuery);
311+
}
312+
313+
@Test
314+
public void testGenerateQuery_TimeUnitPartitionRequiredWithLimit() {
315+
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
316+
when(mockTimePartitioning.getField()).thenReturn(TEST_TIME_UNIT_COL);
317+
318+
String expectedQuery = String.format("select * from %s where %s limit %s",
319+
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION, TEST_LIMIT);
320+
321+
String generatedQuery = format.generateQuery(null, null, null,
322+
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
323+
TEST_LIMIT, null,
324+
true, mockTableDefinition);
282325
Assert.assertEquals(expectedQuery, generatedQuery);
283326
}
284327
}

0 commit comments

Comments
 (0)