Skip to content

Commit 57bc131

Browse files
committed
fix generate query method
1 parent 9116ec8 commit 57bc131

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
@@ -182,6 +182,12 @@ String generateQuery(String partitionFromDate, String partitionToDate, String fi
182182
String datasetProject, String dataset, String table, String limit, String orderBy,
183183
Boolean isPartitionFilterRequired, StandardTableDefinition tableDefinition) {
184184

185+
if (Strings.isNullOrEmpty(filter) && Strings.isNullOrEmpty(orderBy) && Strings.isNullOrEmpty(
186+
limit)
187+
&& Strings.isNullOrEmpty(partitionFromDate) && Strings.isNullOrEmpty(partitionToDate)) {
188+
return null;
189+
}
190+
185191
RangePartitioning rangePartitioning = tableDefinition.getRangePartitioning();
186192
TimePartitioning timePartitioning = tableDefinition.getTimePartitioning();
187193
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
@@ -34,13 +34,15 @@
3434

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

127-
@Test
128-
public void testGenerateQuery_NoOptions() {
129-
String expectedQuery = String.format("select * from %s", TEST_TABLE_SPEC);
130-
131-
String generatedQuery = format.generateQuery(null, null,
132-
null, TEST_PROJECT, TEST_DATASET, TEST_TABLE,
133-
null, null,
134-
false, mockTableDefinition);
135-
Assert.assertEquals(expectedQuery, generatedQuery);
136-
}
137-
138129
@Test
139130
public void testGenerateQuery_AllOptions() {
140131
String expectedQuery = String.format("select * from %s where %s order by %s limit %s",
@@ -163,38 +154,6 @@ public void testGenerateQuery_TimePartitionWithDates() {
163154
Assert.assertEquals(expectedQuery, generatedQuery);
164155
}
165156

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

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

236-
String expectedQuery = String.format("select * from %s where %s",
237-
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION);
195+
String expectedQuery = String.format("select * from %s where (%s)",
196+
TEST_TABLE_SPEC,
197+
TEST_PARTITION_CONDITION);
238198

239-
String generatedQuery = format.generateQuery(null, null, null,
199+
String generatedQuery = format.generateQuery(TEST_FROM_DATE, TEST_TO_DATE, null,
240200
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
241201
null, null,
242-
true, mockTableDefinition);
202+
false, mockTableDefinition);
243203
Assert.assertEquals(expectedQuery, generatedQuery);
244204
}
245205

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

@@ -260,27 +253,77 @@ public void testGenerateQuery_TimePartitionFilterNotRequiredWithDates() {
260253
}
261254

262255
@Test
263-
public void testGenerateQuery_TimePartitionFilterNotRequiredNoDates() {
256+
public void testGenerateQuery_TimePartitionRequired_WithFilterOnly_ShouldAssertQuery() {
264257
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
258+
when(mockTimePartitioning.getField()).thenReturn(null);
265259

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

274285
@Test
275-
public void testGenerateQuery_RangePartitionFilterNotRequired() {
286+
public void testGenerateQuery_RangePartitionRequiredWithLimit() {
276287
when(mockTableDefinition.getRangePartitioning()).thenReturn(mockRangePartitioning);
288+
when(mockRangePartitioning.getField()).thenReturn("range_col");
277289

278-
String expectedQuery = String.format("select * from %s", TEST_TABLE_SPEC);
290+
String expectedQuery = String.format("select * from %s where %s limit %s",
291+
TEST_TABLE_SPEC, TEST_DEFAULT_RANGE_CONDITION, TEST_LIMIT);
279292

280293
String generatedQuery = format.generateQuery(null, null, null,
294+
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
295+
TEST_LIMIT, null,
296+
true, mockTableDefinition);
297+
Assert.assertEquals(expectedQuery, generatedQuery);
298+
}
299+
300+
@Test
301+
public void testGenerateQuery_TimeUnitPartitionRequiredAndFilter() {
302+
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
303+
when(mockTimePartitioning.getField()).thenReturn(TEST_TIME_UNIT_COL);
304+
305+
String expectedQuery = String.format("select * from %s where %s and (%s)",
306+
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION, TEST_FILTER);
307+
308+
String generatedQuery = format.generateQuery(null, null, TEST_FILTER,
281309
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
282310
null, null,
283-
false, mockTableDefinition);
311+
true, mockTableDefinition);
312+
Assert.assertEquals(expectedQuery, generatedQuery);
313+
}
314+
315+
@Test
316+
public void testGenerateQuery_TimeUnitPartitionRequiredWithLimit() {
317+
when(mockTableDefinition.getTimePartitioning()).thenReturn(mockTimePartitioning);
318+
when(mockTimePartitioning.getField()).thenReturn(TEST_TIME_UNIT_COL);
319+
320+
String expectedQuery = String.format("select * from %s where %s limit %s",
321+
TEST_TABLE_SPEC, TEST_DEFAULT_TIME_UNIT_CONDITION, TEST_LIMIT);
322+
323+
String generatedQuery = format.generateQuery(null, null, null,
324+
TEST_PROJECT, TEST_DATASET, TEST_TABLE,
325+
TEST_LIMIT, null,
326+
true, mockTableDefinition);
284327
Assert.assertEquals(expectedQuery, generatedQuery);
285328
}
286329
}

0 commit comments

Comments
 (0)