Repository navigation
minor: Validate batch_size configuration when setting it - #23054
Conversation
| pub const fn new(value: usize) -> Self { | ||
| match NonZeroUsize::new(value) { | ||
| Some(value) => Self(value), | ||
| None => panic!("value must be greater than 0"), |
There was a problem hiding this comment.
lets return config_err it would super confusing if client crashes
There was a problem hiding this comment.
The default constructor for configurations is wrapped inside config_namespace! macro, and it can't handle ? operator on result, so we have to panic here.
But I agree this unsafe API should not be exposed to the caller, so in b37fe74, a private helper is added for unsafe construction, its input is guaranteed to be always valid, since it's only for the hard-coded default value, outside caller can only use the safe try_new()
|
|
||
| /// Creates a [`ConfigNonZeroUsize`], returning a configuration error if | ||
| /// `value` is zero. | ||
| pub fn try_new(value: usize) -> Result<Self> { |
There was a problem hiding this comment.
maybe new with Result<Self> is more obvious choice?
comphead
left a comment
There was a problem hiding this comment.
Thanks @2010YOUY01 just some minors
Looks like it's a false positive due to the flaky Github CI runner. |
|
Thanks for the review. |
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> Follows up to apache#23054 - Part of apache#17498 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Similar to apache#23054, 0 value in `recursion_limit` is invalid, and carrying such value to the SQL parsing logic can cause bugs or panics. A better alternative would be to catch the invalid `0` at config value setting step. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> 1. Use a safe type `ConfigNonZeroUsize` instead of `usize` for this configuration 2. Propogate changes to the implementation 3. Add one `.slt` test ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 4. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> Follows up to apache#23054 - Part of apache#17498 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Similar to apache#23054, 0 value in `recursion_limit` is invalid, and carrying such value to the SQL parsing logic can cause bugs or panics. A better alternative would be to catch the invalid `0` at config value setting step. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> 1. Use a safe type `ConfigNonZeroUsize` instead of `usize` for this configuration 2. Propogate changes to the implementation 3. Add one `.slt` test ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 4. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
Which issue does this PR close?
Rationale for this change
In datafusion-cli:
A '0' batch_size is a invalid config value, and it's widely used in most operators to control behavior. If operators don't validate
batch_size, the 0 value might cause bugs/panics. This PR rejects it when setting the config.I triggered a bug on my first attempt:
What changes are included in this PR?
Implement a custom type for non-zero usize configuration value on
batch_size, and rejects '0' value on initialization. Rust nativeNonZero<u32>is not used, because custom type allows to provide better error message on errors, and the implementation complexity is similar.Note other invalid inputs like '-1' 'a' are already rejected and covered by existing tests.
Are these changes tested?
Yes, sqllogictest
Are there any user-facing changes?
No