okumin commented on code in PR #6505:
URL: https://github.com/apache/hive/pull/6505#discussion_r4725219528


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/stats/FilterSelectivityEstimator.java:
##########
@@ -74,6 +74,7 @@ public class FilterSelectivityEstimator extends 
RexVisitorImpl<Double> {
   private final double  childCardinality;
   private final RelMetadataQuery mq;
   private final RexBuilder rexBuilder;
+  private boolean unknownSeen;

Review Comment:
   In this comment, I have two assumptions.
   - No degradation would happen if 
`HiveRelMdDistinctRowCount#getDistinctRowCount` returned 0.0 where the original 
implementation returned zero or a negative number
   - We don't have clear evidence why we should handle only ANTI JOIN 
differently
   
   I understand that inverting the fallback can produce very small estimates. 
My concern, however, is the asymmetry: the same AND/OR expression propagates 
unknown estimates only when used for a filter above an ANTI JOIN. The immutable 
flag removes the mutable state, but it does not resolve this inconsistency.
   
   Let me explain how I see this as a reviewer.
   1. The [earlier implementation returned 
zero](https://github.com/apache/hive/blob/eda2c8d93ef4ee6f22fdc0f77801c52d8c2d772b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/stats/HiveRelMdDistinctRowCount.java#L70)
 for unknown NDVs. Calcite uses null to represent unknown, so zero is not 
ideal. However, returning zero at least prevents negative NDVs from reaching 
Calcite, and some existing callers assume a non-null result. My question was 
whether switching to null would require substantial changes or break existing 
tests. > "What if this returns null? Does it require major surgery, or do 
existing tests go wrong?"
   2. The revised implementation returns null, but handles its propagation 
differently depending on the caller. "If it is already clear that only ANTI 
JOIN is a special case" has not been satisfied at this point. The partial and 
incomplete patch introduces some tech debt; we have to read why only ANTI JOIN 
is specially handled.
   
   If keeping that fallback does not introduce regressions relative to master, 
I would prefer to keep this PR focused on distinguishing unknown NDVs from 
verified zero. We could retain the zero fallback at the Calcite boundary 
temporarily and address consistent unknown propagation, including the ANTI JOIN 
behavior, in a separate ticket.
   
   Please feel free to ask me if you would like me to clarify my expectations 👍 



##########
ql/src/test/org/apache/hadoop/hive/ql/optimizer/calcite/stats/TestFilterSelectivityEstimator.java:
##########
@@ -507,6 +517,246 @@ public void 
testComputeRangePredicateSelectivityGreaterThanOrEqualWhenHigherThan
     Assert.assertEquals(0, estimator.estimateSelectivity(filter), DELTA);
   }
 
+  @Test
+  public void testNotWithUnknownNdvIsUnknown() {
+    doReturn(null).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode equality = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, 
inputRef0, int1);
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.NOT, 
equality);
+    Assert.assertNull(new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate));
+  }
+
+  @Test
+  public void testNotWithKnownNdv() {
+    doReturn(10.0).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode equality = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, 
inputRef0, int1);
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.NOT, 
equality);
+    Assert.assertEquals(0.9, new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate), DELTA);
+  }
+
+  @Test
+  public void testSearchPointWithUnknownNdvIsUnknown() {
+    
assertSearchSelectivity(ImmutableRangeSet.of(Range.singleton(BigDecimal.ONE)), 
null, null);
+  }
+
+  @Test
+  public void testSearchPointsWithUnknownNdvIsUnknown() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        
.add(Range.singleton(BigDecimal.ONE)).add(Range.singleton(BigDecimal.TEN)).build(),
 null, null);
+  }
+
+  @Test
+  public void testSearchRangeWithUnknownNdvIsUnknown() {
+    assertSearchSelectivity(ImmutableRangeSet.of(Range.closed(BigDecimal.ONE, 
BigDecimal.TEN)), null, null);
+  }
+
+  @Test
+  public void testSearchRangesWithUnknownNdvUseDisjunctionFallback() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        .add(Range.closed(BigDecimal.ONE, BigDecimal.valueOf(3)))
+        .add(Range.closed(BigDecimal.valueOf(5), BigDecimal.TEN)).build(), 
null, 0.9999);
+  }
+
+  @Test
+  public void testSearchRangeAndPointWithUnknownNdvUseDisjunctionFallback() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        .add(Range.singleton(BigDecimal.ONE))
+        .add(Range.closed(BigDecimal.valueOf(5), BigDecimal.TEN)).build(), 
null, 0.9999);
+  }
+
+  @Test
+  public void testSearchPointWithKnownNdv() {
+    
assertSearchSelectivity(ImmutableRangeSet.of(Range.singleton(BigDecimal.ONE)), 
10.0, 0.1);
+  }
+
+  @Test
+  public void testSearchPointsWithKnownNdv() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        
.add(Range.singleton(BigDecimal.ONE)).add(Range.singleton(BigDecimal.TEN)).build(),
 10.0, 0.2);
+  }
+
+  @Test
+  public void testSearchRangeWithKnownNdv() {
+    doReturn(10.0).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode range = REX_BUILDER.makeSearchArgumentLiteral(
+        Sarg.of(RexUnknownAs.UNKNOWN, 
ImmutableRangeSet.of(Range.closed(BigDecimal.ONE, BigDecimal.TEN))),
+        inputRef0.getType());
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.SEARCH, 
inputRef0, range);
+    Assert.assertNotNull(new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate));

Review Comment:
   If I understand correctly, this can assert a floating point with delta



##########
ql/src/test/org/apache/hadoop/hive/ql/optimizer/calcite/stats/TestFilterSelectivityEstimator.java:
##########
@@ -507,6 +517,246 @@ public void 
testComputeRangePredicateSelectivityGreaterThanOrEqualWhenHigherThan
     Assert.assertEquals(0, estimator.estimateSelectivity(filter), DELTA);
   }
 
+  @Test
+  public void testNotWithUnknownNdvIsUnknown() {
+    doReturn(null).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode equality = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, 
inputRef0, int1);
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.NOT, 
equality);
+    Assert.assertNull(new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate));
+  }
+
+  @Test
+  public void testNotWithKnownNdv() {
+    doReturn(10.0).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode equality = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, 
inputRef0, int1);
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.NOT, 
equality);
+    Assert.assertEquals(0.9, new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate), DELTA);
+  }
+
+  @Test
+  public void testSearchPointWithUnknownNdvIsUnknown() {
+    
assertSearchSelectivity(ImmutableRangeSet.of(Range.singleton(BigDecimal.ONE)), 
null, null);
+  }
+
+  @Test
+  public void testSearchPointsWithUnknownNdvIsUnknown() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        
.add(Range.singleton(BigDecimal.ONE)).add(Range.singleton(BigDecimal.TEN)).build(),
 null, null);
+  }
+
+  @Test
+  public void testSearchRangeWithUnknownNdvIsUnknown() {
+    assertSearchSelectivity(ImmutableRangeSet.of(Range.closed(BigDecimal.ONE, 
BigDecimal.TEN)), null, null);
+  }
+
+  @Test
+  public void testSearchRangesWithUnknownNdvUseDisjunctionFallback() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        .add(Range.closed(BigDecimal.ONE, BigDecimal.valueOf(3)))
+        .add(Range.closed(BigDecimal.valueOf(5), BigDecimal.TEN)).build(), 
null, 0.9999);
+  }
+
+  @Test
+  public void testSearchRangeAndPointWithUnknownNdvUseDisjunctionFallback() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        .add(Range.singleton(BigDecimal.ONE))
+        .add(Range.closed(BigDecimal.valueOf(5), BigDecimal.TEN)).build(), 
null, 0.9999);
+  }
+
+  @Test
+  public void testSearchPointWithKnownNdv() {
+    
assertSearchSelectivity(ImmutableRangeSet.of(Range.singleton(BigDecimal.ONE)), 
10.0, 0.1);
+  }
+
+  @Test
+  public void testSearchPointsWithKnownNdv() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        
.add(Range.singleton(BigDecimal.ONE)).add(Range.singleton(BigDecimal.TEN)).build(),
 10.0, 0.2);
+  }
+
+  @Test
+  public void testSearchRangeWithKnownNdv() {
+    doReturn(10.0).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode range = REX_BUILDER.makeSearchArgumentLiteral(
+        Sarg.of(RexUnknownAs.UNKNOWN, 
ImmutableRangeSet.of(Range.closed(BigDecimal.ONE, BigDecimal.TEN))),
+        inputRef0.getType());
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.SEARCH, 
inputRef0, range);
+    Assert.assertNotNull(new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate));
+  }
+
+  @Test
+  public void testSearchRangeAndPointWithKnownNdv() {
+    assertSearchSelectivity(ImmutableRangeSet.<BigDecimal>builder()
+        .add(Range.singleton(BigDecimal.ONE))
+        .add(Range.closed(BigDecimal.valueOf(5), BigDecimal.TEN)).build(), 
10.0, 0.19);
+  }
+
+  @Test
+  public void testOrWithUnknownPartIsUnknownWhenPropagating() {
+    doReturn(null).when(mq).getDistinctRowCount(scan, ImmutableBitSet.of(0), 
REX_BUILDER.makeLiteral(true));
+    RexNode predicate = REX_BUILDER.makeCall(SqlStdOperatorTable.OR,
+        REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, inputRef0, int1),
+        REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, inputRef0, int2));
+    Assert.assertNull(new FilterSelectivityEstimator(scan, mq, 
true).estimateSelectivity(predicate));
+    Assert.assertNotNull(new FilterSelectivityEstimator(scan, 
mq).estimateSelectivity(predicate));

Review Comment:
   ditto. This file includes both `Assert.assertNotNull(...)` and 
`Assert.assertEquals(n, ..., DELTA)`. Please use the latter pattern for all so 
that we can ensure FilterSelectivityEstimator does not return undesirable values



-- 
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]

Reply via email to