Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLINK-25187][table-planner] Apply padding when CASTing to BINARY(<length>) #18162

Closed
wants to merge 2 commits into from

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Dec 21, 2021

What is the purpose of the change

Similarly to CHAR(<length>) when casting to a BINARY(<length>)
apply padding with 0 bytes to the right so that the resulting byte[]
matches exaxctly the specified length.

Verifying this change

This change added tests and can be verified as follows:

  • Added tests in CastRulesTest
  • Added tests in CastFunctionITCase
  • Added tests in CastFunctionMiscITCase

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): no
  • The public API, i.e., is any changed class annotated with @Public(Evolving): no
  • The serializers: no
  • The runtime per-record code paths (performance sensitive): no
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
  • The S3 file system connector: no

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

@matriv matriv changed the title Flink 25187 [FLINK-25187][table-planner] Apply padding when CASTing to BINARY(<length>) Dec 21, 2021
@flinkbot
Copy link
Collaborator

Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.

Automated Checks

Last check on commit 68c1818 (Tue Dec 21 14:53:50 UTC 2021)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!
  • Invalid pull request title: No valid Jira ID provided

Mention the bot in a comment to re-run the automated checks.

Review Progress

  • ❓ 1. The [description] looks good.
  • ❓ 2. There is [consensus] that the contribution should go into to Flink.
  • ❓ 3. Needs [attention] from.
  • ❓ 4. The change fits into the overall [architecture].
  • ❓ 5. Overall code [quality] is good.

Please see the Pull Request Review Guide for a full explanation of the review process.


The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commands
The @flinkbot bot supports the following commands:

  • @flinkbot approve description to approve one or more aspects (aspects: description, consensus, architecture and quality)
  • @flinkbot approve all to approve all aspects
  • @flinkbot approve-until architecture to approve everything until architecture
  • @flinkbot attention @username1 [@username2 ..] to require somebody's attention
  • @flinkbot disapprove architecture to remove an approval you gave earlier

@flinkbot
Copy link
Collaborator

flinkbot commented Dec 21, 2021

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

Copy link
Contributor

@twalthr twalthr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @matriv. I only had minor comments to improve readability.

|| !(couldTrim(length) || couldPad(targetLogicalType, length))) {
|| ((!couldTrim(targetLength)
// Assume input length is respected by the source
|| (inputLength != null && inputLength <= targetLength))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition is already quite complex. maybe introduce another method that takes input from above and target length?

.fromCaseLegacy(CHAR(2), fromString("Apache"), fromString("Apache"))
.fromCase(VARCHAR(2), fromString("Apache"), fromString("Apache"))
.fromCaseLegacy(VARCHAR(2), fromString("Apache"), fromString("Apache"))
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty comments are not very useful. fill them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used it to mark the end of that section, since there are more tests for this target type.

if (context.legacyBehaviour()
|| ((!couldTrim(targetLength)
// Assume input length is respected by the source
|| (inputLength <= targetLength))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify the condition maybe with local variables? or separate branches for early out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (condition) {
  return inputTerm;
}

if (condition) {
  return inputTerm;
}


remaining code without nesting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot do that apart from the check for the legacyBehaviour, the other condition cannot be splitted, it has to be evaluated as a whole.

Copy link
Contributor Author

@matriv matriv Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could invert it maybe to avoid the wrapping not but imho it's more readable to exit early or then apply the logic for trimming/padding.

@@ -656,13 +656,6 @@
.fromCaseLegacy(CHAR(6), fromString("Apache"), fromString("Apache"))
.fromCase(VARCHAR(5), fromString("Flink"), fromString("Flink "))
.fromCaseLegacy(VARCHAR(5), fromString("Flink"), fromString("Flink"))
// We assume that the input length is respected, therefore, no trimming is
// applied
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the hotfix commit if those tests can be removed? They are invalid anyways so +1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I will do.

…d for CAST

When casting to CHAR/VARCHAR/BINARY/VARBINARY, we assume that
the length of the source type CHAR/VARCHAR/BINARY/VARBINARY is
respected, to avoid performance overhead by applying checks and trimming
at runtime. i.e. if input type is `VARCHAR(3)`, input value is 'foobar' and target
type is `VARCHAR(4)`, no trimming is applied and the result value remains:
`foobar`.
…ngth>)

Similarly to `CHAR(<length>)` when casting to a `BINARY(<length>)`
apply padding with 0 bytes to the right so that the resulting `byte[]`
matches exaxctly the specified length.
@matriv
Copy link
Contributor Author

matriv commented Dec 28, 2021

@flinkbot run azure

Copy link
Contributor

@twalthr twalthr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@twalthr twalthr closed this in 49acb27 Dec 31, 2021
@matriv matriv deleted the FLINK-25187 branch January 17, 2022 08:14
niklassemmler pushed a commit to niklassemmler/flink that referenced this pull request Feb 3, 2022
…ngth>)

Similarly to `CHAR(<length>)` when casting to a `BINARY(<length>)`
apply padding with 0 bytes to the right so that the resulting `byte[]`
matches exaxctly the specified length.

This closes apache#18162.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants