riverandeye opened a new issue, #2727:
URL: https://github.com/apache/iceberg-go/issues/2727

   `inclusiveMetricsEval` always returns "might match" for `!=` and `NOT IN`, 
so a scan reads a data file even when its stats prove that no row can match. 
That happens when a column's lower and upper bounds are equal and the file has 
no nulls (or NaNs): every row holds that one value, so `col != value` or `col 
NOT IN (value, ...)` can't match any row.
   
   Reproduced on main (b9a9bd9):
   
   ```go
   sc := iceberg.NewSchema(0,
        iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},
        iceberg.NestedField{ID: 2, Name: "region", Type: 
iceberg.PrimitiveTypes.String},
   )
   tbl, _ := cat.CreateTable(ctx, catalog.ToIdentifier("db", "events"), sc)
   // append rows (1, "us"), (2, "us"), (3, "us")
   tbl, _ = tbl.Append(ctx, rdr, nil)
   
   filter := iceberg.NotEqualTo(iceberg.Reference("region"), "us")
   tasks, _ := tbl.Scan(table.WithRowFilter(filter)).PlanFiles(ctx)
   res, _ := tbl.Scan(table.WithRowFilter(filter)).ToArrowTable(ctx)
   fmt.Println("files:", len(tasks), "rows:", res.NumRows())
   ```
   
   Output:
   
   ```
   files: 1 rows: 0
   ```
   
   The file has `lower = upper = "us"` and a null count of 0 for `region`, so 
it could be skipped. `NOT IN ("us")` gives the same result.
   
   Both visitors return `rowsMightMatch` without looking at the stats 
([`table/evaluators.go`](https://github.com/apache/iceberg-go/blob/b9a9bd92e4d89ef9b2619a65d018e9e9905d456c/table/evaluators.go#L1313-L1315),
 
[`table/evaluators.go`](https://github.com/apache/iceberg-go/blob/b9a9bd92e4d89ef9b2619a65d018e9e9905d456c/table/evaluators.go#L1402-L1407)):
   
   ```go
   func (m *inclusiveMetricsEval) VisitNotEqual(iceberg.BoundTerm, 
iceberg.Literal) bool {
        return rowsMightMatch
   }
   ```
   
   Java prunes in this case 
([`InclusiveEvalVisitor.java`](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/api/src/main/java/org/apache/iceberg/expressions/InclusiveEvalVisitor.java#L281-L294)):
   
   ```java
   public <T> Boolean notEq(Bound<T> term, Literal<T> lit) {
     // ...when min == max and the file has no nulls or NaN values, we can 
safely prune
     // if that value equals the literal.
     T value = uniqueValue(term);
   
     if (value != null && lit.comparator().compare(value, lit.value()) == 0) {
       return ROWS_CANNOT_MATCH;
     }
   
     return ROWS_MIGHT_MATCH;
   }
   ```
   
   `notIn` does the same with `literalSet.contains(value)`. `uniqueValue` 
([`InclusiveEvalVisitor.java`](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/api/src/main/java/org/apache/iceberg/expressions/InclusiveEvalVisitor.java#L437-L459))
 returns a value only when the column may not contain nulls, both bounds are 
present and not NaN, a float or double column may not contain NaN, and the 
lower bound equals the upper bound.
   
   **Proposal:** add the same single-value check to `VisitNotEqual` and 
`VisitNotIn` in `inclusiveMetricsEval`, with cases in 
`InclusiveMetricsTestSuite` next to `TestIntNeq` and `TestNotInMetrics`.
   


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