gengliangwang opened a new pull request, #60314:
URL: https://github.com/apache/spark/pull/60314

   ### What changes were proposed in this pull request?
   
   This PR is stacked on #59520, the Rust SDK of native data sources: only its 
last commit, 89cfc029ce7, is new here.
   
   SPARK-61200 (#59929) added `push_domains` to version 1 of the C ABI: Spark 
derives the domains of the columns that the filters of a query constrain, from 
comparisons with literals, `IN`, `IS NULL`, `IS NOT NULL` and `STARTS_WITH` 
under `AND`, `OR` and `NOT`, and a reader can skip the data that has no value 
in them, such as a row group whose statistics exclude a domain. Spark evaluates 
all the filters after reading anyway. #59520 binds the function but leaves it 
unset. This PR installs it for every source with batch reads, and passes the 
domains to a new method of `ScanBuilder`, called before `push_limit` and 
`prune_columns`:
   
   ```rust
   fn push_domains(&mut self, domains: &[Domain]) -> Result<Vec<bool>> {
       Ok(vec![false; domains.len()])
   }
   ```
   
   - `Domain { path, null_allowed, nan_allowed, ranges }` mirrors 
`SparkDsDomain`. `path` names the column, and its field in each struct, in 
`ScanInfo::schema`: the SDK resolves the indices of the ABI in the schema that 
`create_reader` got, which it now keeps with each scan, because Spark prunes 
the columns after it pushes the domains, so their indices in the schema to read 
of the readers may differ. `ranges` is `None` when every other value is in the 
domain, or sorted ranges without overlaps, `ValueRange { low, high }` with 
`std::ops::Bound<Value>` limits.
   - `Value` holds the values of the types that version 1 passes domains for: 
`Boolean`, `Integer` (tinyint to bigint), `Float` (float and double, never 
NaN), `Decimal { unscaled, precision, scale }`, `String`, `Binary`, `Date`, 
`Timestamp`, and `TimestampNtz`. The SDK converts the Arrow struct array of the 
ranges, which it moves to take ownership of it, as the header allows, and skips 
as unused a domain whose values have another type, which a later version of 
Spark may pass. `Value` and `Domain` are `#[non_exhaustive]`, so types and 
fields can be added; `Domain::new` and `ValueRange::single` build them in tests.
   - `Domain`, `ValueRange`, and `Value` derive `Serialize` and `Deserialize`, 
so a reader factory can carry the domains to the executors, where a file source 
reads the statistics of its files.
   - The method returns whether the scan uses each domain, which Spark shows as 
pushed filters in `EXPLAIN FORMATTED`. The SDK checks the number of results. 
The default uses none, so an existing connector builds and behaves as before.
   
   The trait documentation and the README describe the contract: the order of 
the calls, what a domain holds, that a scan may skip data or rows outside a 
domain but never a row whose values are all in their domains, and the order of 
Spark SQL in which values compare (floats with `<` and `==`, under which -0.0 
equals 0.0, strings by their UTF-8 bytes, binary values as unsigned bytes). The 
README section on pushdown replaces the paragraph that announced this change, 
and the Range example notes that it uses no domain.
   
   ### Why are the changes needed?
   
   Without `push_domains`, a Rust connector cannot skip data by the filters of 
a query: Spark pushes nothing to a library whose function table leaves it 
unset, and reads every row group of every file. Sources of formats with 
statistics, such as Parquet, need it: #59692, a Parquet source on this SDK, 
uses the domains to skip row groups and pages by their statistics, and to drop 
the rows outside them, as its predicates did on the ABI before version 1.
   
   Names rather than indices, typed values rather than Arrow arrays, and 
serializable domains keep the common case simple: a connector compares a domain 
with its own statistics or values, and ships it to its readers, without 
handling Arrow C structs or the pruning of the schema.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the unreleased, experimental Rust SDK of #59520: 
`ScanBuilder::push_domains` and the types `Domain`, `ValueRange`, and `Value`. 
Since the SDK now installs `push_domains`, Spark derives the domains of the 
filtered columns of each batch scan of a Rust source and pushes them; a source 
that does not override the method uses none, so its plans and results do not 
change. There is no change to Spark itself.
   
   ### How was this patch tested?
   
   From `sql/native/rust`:
   
   ```sh
   cargo test --workspace --locked
   cargo clippy --workspace --all-targets --locked -- -D warnings
   cargo fmt --all -- --check
   RUSTDOCFLAGS='-D warnings' cargo doc --workspace --no-deps --locked
   ```
   
   All 82 tests passed, 6 of them new:
   
   - unit tests of the conversion: the values of each type of version 1, 
unbounded, included and excluded bounds, ranges without rows, the names of the 
fields of structs, a type without a `Value`, invalid paths, and a MessagePack 
round trip of domains, with 128-bit decimals and every kind of bound, as the 
SDK encodes reader factories;
   - `tests/domains.rs`, which calls `push_domains` through the exported C 
function table with ranges that arrow-rs exports as Spark exports them: the 
domains that the scan gets, the used flags at the positions of the domains, the 
moved ranges, negative and out-of-range paths, and a wrong number of results;
   - `tests/batch_write.rs` checks that a write-only library leaves 
`push_domains` null.
   
   With Spark, on a distribution built from #59520 (master at 8aac2b7d3e3, 
which has SPARK-61200) with Java 25: the smoke script of the Range example 
passed, whose filtered reads now go through `push_domains`, which the Range 
source leaves at its default, so Spark still evaluates their filters after 
reading; and so did the smoke script of #59692, a Parquet source that uses the 
domains, whose 45 queries over three layouts of files return what Spark's own 
reader returns, and whose formatted plans show the filters of the used domains 
as pushed.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


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