imply-cheddar commented on code in PR #13934:
URL: https://github.com/apache/druid/pull/13934#discussion_r1139410417
##########
processing/src/test/java/org/apache/druid/segment/UnnestStorageAdapterTest.java:
##########
@@ -245,4 +260,107 @@ private static void assertColumnReadsIdentifier(final
VirtualColumn column, fina
MatcherAssert.assertThat(column,
CoreMatchers.instanceOf(ExpressionVirtualColumn.class));
Assert.assertEquals("\"" + identifier + "\"", ((ExpressionVirtualColumn)
column).getExpression());
}
+
+ @Test
+ public void test_unnest_adapters_with_no_base_filter_active_unnest_filter()
+ {
+
+ Sequence<Cursor> cursorSequence = UNNEST_STORAGE_ADAPTER2.makeCursors(
+ null,
+ UNNEST_STORAGE_ADAPTER2.getInterval(),
+ VirtualColumns.EMPTY,
+ Granularities.ALL,
+ false,
+ null
+ );
+
+ cursorSequence.accumulate(null, (accumulated, cursor) -> {
+ ColumnSelectorFactory factory = cursor.getColumnSelectorFactory();
+
+ DimensionSelector dimSelector =
factory.makeDimensionSelector(DefaultDimensionSpec.of(OUTPUT_COLUMN_NAME));
+ int count = 0;
+ while (!cursor.isDone()) {
+ Object dimSelectorVal = dimSelector.getObject();
+ if (dimSelectorVal == null) {
+ Assert.assertNull(dimSelectorVal);
+ }
+ cursor.advance();
+ count++;
+ }
+ Assert.assertEquals(1, count);
+ Filter unnestFilter = new SelectorDimFilter(OUTPUT_COLUMN_NAME, "1",
null).toFilter();
+ VirtualColumn vc = new ExpressionVirtualColumn(
+ OUTPUT_COLUMN_NAME,
+ "\"" + COLUMNNAME + "\"",
+ null,
+ ExprMacroTable.nil()
+ );
+ final String inputColumn =
UNNEST_STORAGE_ADAPTER2.getUnnestInputIfDirectAccess(vc);
+ Pair<Filter, Filter> filterPair =
UNNEST_STORAGE_ADAPTER2.computeBaseAndPostUnnestFilters(
+ null,
+ unnestFilter,
+ VirtualColumns.EMPTY,
+ inputColumn,
+ INCREMENTAL_INDEX_STORAGE_ADAPTER.getColumnCapabilities(inputColumn)
+ );
+ SelectorFilter left = ((SelectorFilter) filterPair.lhs);
+ SelectorFilter right = ((SelectorFilter) filterPair.rhs);
+ Assert.assertEquals(inputColumn, left.getDimension());
+ Assert.assertEquals(OUTPUT_COLUMN_NAME, right.getDimension());
+ Assert.assertEquals(right.getValue(), left.getValue());
+ return null;
+ });
+ }
+
Review Comment:
Yeah, this test is calling the methods that the implementation should call.
Essentially trying to mimic the actual implementation and making sure that it
does the right thing. That's a fine test, if you assume that the base
implementation will never change outside of these methods. But as soon as
someone changes the implementation outside of one of these methods, the tests
will no longer validate what we want to validate.
It's better to focus on testing the contract of the object. The contract in
this case is that it is given some filters on `makeCursors` and some of those
should be rewritten and passed down (to a delegate `makeCursors` call) and some
of them should be placed on the return cursor for filtering. If you write the
test to validate that contract, then it will be a cleaner test that is more
helpful in the face of code changes. Fwiw, there are two reasons to have tests
1. To give you confidence that the changes you are making work
2. To give everybody on the project confidence that their changes work
Goal #2 is infinitely more important than number 1 and is best served by
testing to contract and not implementation as much as possible.
##########
processing/src/test/java/org/apache/druid/segment/UnnestStorageAdapterTest.java:
##########
@@ -245,4 +260,107 @@ private static void assertColumnReadsIdentifier(final
VirtualColumn column, fina
MatcherAssert.assertThat(column,
CoreMatchers.instanceOf(ExpressionVirtualColumn.class));
Assert.assertEquals("\"" + identifier + "\"", ((ExpressionVirtualColumn)
column).getExpression());
}
+
+ @Test
+ public void test_unnest_adapters_with_no_base_filter_active_unnest_filter()
+ {
+
+ Sequence<Cursor> cursorSequence = UNNEST_STORAGE_ADAPTER2.makeCursors(
+ null,
+ UNNEST_STORAGE_ADAPTER2.getInterval(),
+ VirtualColumns.EMPTY,
+ Granularities.ALL,
+ false,
+ null
+ );
+
+ cursorSequence.accumulate(null, (accumulated, cursor) -> {
+ ColumnSelectorFactory factory = cursor.getColumnSelectorFactory();
+
+ DimensionSelector dimSelector =
factory.makeDimensionSelector(DefaultDimensionSpec.of(OUTPUT_COLUMN_NAME));
+ int count = 0;
+ while (!cursor.isDone()) {
+ Object dimSelectorVal = dimSelector.getObject();
+ if (dimSelectorVal == null) {
+ Assert.assertNull(dimSelectorVal);
+ }
+ cursor.advance();
+ count++;
+ }
+ Assert.assertEquals(1, count);
+ Filter unnestFilter = new SelectorDimFilter(OUTPUT_COLUMN_NAME, "1",
null).toFilter();
+ VirtualColumn vc = new ExpressionVirtualColumn(
+ OUTPUT_COLUMN_NAME,
+ "\"" + COLUMNNAME + "\"",
+ null,
+ ExprMacroTable.nil()
+ );
+ final String inputColumn =
UNNEST_STORAGE_ADAPTER2.getUnnestInputIfDirectAccess(vc);
+ Pair<Filter, Filter> filterPair =
UNNEST_STORAGE_ADAPTER2.computeBaseAndPostUnnestFilters(
+ null,
+ unnestFilter,
+ VirtualColumns.EMPTY,
+ inputColumn,
+ INCREMENTAL_INDEX_STORAGE_ADAPTER.getColumnCapabilities(inputColumn)
+ );
+ SelectorFilter left = ((SelectorFilter) filterPair.lhs);
+ SelectorFilter right = ((SelectorFilter) filterPair.rhs);
+ Assert.assertEquals(inputColumn, left.getDimension());
+ Assert.assertEquals(OUTPUT_COLUMN_NAME, right.getDimension());
+ Assert.assertEquals(right.getValue(), left.getValue());
+ return null;
+ });
+ }
+
Review Comment:
Yeah, this test is calling the methods that the implementation should call.
Essentially trying to mimic the actual implementation and making sure that it
does the right thing. That's a fine test, if you assume that the base
implementation will never change outside of these methods. But as soon as
someone changes the implementation outside of one of these methods, the tests
will no longer validate what we want to validate.
It's better to focus on testing the contract of the object. The contract in
this case is that it is given some filters on `makeCursors` and some of those
should be rewritten and passed down (to a delegate `makeCursors` call) and some
of them should be placed on the return cursor for filtering. If you write the
test to validate that contract, then it will be a cleaner test that is more
helpful in the face of code changes. Fwiw, there are two reasons to have tests
1. To give you confidence that the changes you are making work
2. To give everybody on the project confidence that their changes work
Goal 2 is infinitely more important than number 1 and is best served by
testing to contract and not implementation as much as possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]