[SPARK-55978][SQL] Add TABLESAMPLE SYSTEM block sampling with DSv2 pushdown#54972
Open
stanyao wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-55978][SQL] Add TABLESAMPLE SYSTEM block sampling with DSv2 pushdown#54972stanyao wants to merge 1 commit intoapache:masterfrom
stanyao wants to merge 1 commit intoapache:masterfrom
Conversation
34548e4 to
9aa99d8
Compare
…shdown ### What changes were proposed in this pull request? This PR adds support for ANSI SQL `TABLESAMPLE SYSTEM` (block-level sampling) alongside the existing `TABLESAMPLE BERNOULLI` (row-level sampling). Key changes: - **SQL grammar**: Extended `TABLESAMPLE` to accept an optional `SYSTEM` or `BERNOULLI` qualifier before the sample method. Added both as non-reserved keywords. - **Logical plan**: Introduced `SampleMethod` sealed trait (`Bernoulli`/`System`) and added it to the `Sample` node. Default is `Bernoulli` for backward compatibility. - **Parser**: `TABLESAMPLE SYSTEM` only supports `PERCENT` sampling and does not support `REPEATABLE`. Other methods (ROWS, BUCKET, BYTES) are rejected with clear error messages. - **DSv2 pushdown**: `TABLESAMPLE SYSTEM` is pushed down to data sources via an extended `SupportsPushDownTableSample.pushTableSample()` overload with `isSystemSampling` flag. Sources that don't override the new method reject SYSTEM sampling by default. - **Physical planning**: SYSTEM samples that aren't pushed down to a DSv2 source raise an `AnalysisException` — there is no row-level fallback since block sampling is data-source dependent. ### Why are the changes needed? ANSI SQL defines two sampling methods: `BERNOULLI` (row-level) and `SYSTEM` (implementation-dependent, typically block/split-level). Block sampling is significantly faster for large tables since it avoids per-row evaluation, making it useful for approximate queries and data exploration. Many databases (PostgreSQL, Hive, Trino) support this distinction. ### Does this PR introduce _any_ user-facing change? Yes. New SQL syntax `TABLESAMPLE SYSTEM (x PERCENT)` and `TABLESAMPLE BERNOULLI (x PERCENT)`. `BERNOULLI` and `SYSTEM` are added as non-reserved keywords. Existing queries without these keywords behave identically to before. ### How was this patch tested? - 9 new test cases in `PlanParserSuite` covering: basic parsing, case insensitivity, boundary fractions, unsupported methods (ROWS/BUCKET with SYSTEM), REPEATABLE rejection, fraction validation, identifier preservation, and subquery contexts. - Existing `SQLQuerySuite` tests pass. - Scalastyle passes with 0 errors/warnings.
9aa99d8 to
bd6f9cf
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR adds support for ANSI SQL
TABLESAMPLE SYSTEM(block-level sampling) alongside the existingTABLESAMPLE BERNOULLI(row-level sampling). Key changes:TABLESAMPLEto accept an optionalSYSTEMorBERNOULLIqualifier before the sample method. Added both as non-reserved keywords.SampleMethodsealed trait (Bernoulli/System) and added it to theSamplenode. Default isBernoullifor backward compatibility.TABLESAMPLE SYSTEMonly supportsPERCENTsampling and does not supportREPEATABLE. Other methods (ROWS, BUCKET, BYTES) are rejected with clear error messages.TABLESAMPLE SYSTEMis pushed down to data sources via an extendedSupportsPushDownTableSample.pushTableSample()overload withisSystemSamplingflag. Sources that don't override the new method reject SYSTEM sampling by default.AnalysisException— there is no row-level fallback since block sampling is data-source dependent.Why are the changes needed?
ANSI SQL defines two sampling methods:
BERNOULLI(row-level) andSYSTEM(implementation-dependent, typically block/split-level). Block sampling is significantly faster for large tables since it avoids per-row evaluation, making it useful for approximate queries and data exploration. Many databases (PostgreSQL, Hive, Trino) support this distinction.Does this PR introduce any user-facing change?
Yes. New SQL syntax
TABLESAMPLE SYSTEM (x PERCENT)andTABLESAMPLE BERNOULLI (x PERCENT).BERNOULLIandSYSTEMare added as non-reserved keywords. Existing queries without these keywords behave identically to before.How was this patch tested?
PlanParserSuitecovering: basic parsing, case insensitivity, boundary fractions, unsupported methods (ROWS/BUCKET with SYSTEM), REPEATABLE rejection, fraction validation, identifier preservation, and subquery contexts.SQLQuerySuitetests pass.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (thoroughly refined, reviewed, and tested by human)