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-15381] [table-planner-blink] correct collation derive logic on RelSubset in RelMdCollation #10694

Merged
merged 3 commits into from
Jan 2, 2020

Conversation

godfreyhe
Copy link
Contributor

@godfreyhe godfreyhe commented Dec 26, 2019

What is the purpose of the change

sql: select cast(a as int), cast(b as varchar) from (values (3, 'c')) T(a,b) will fail,
the reason is: the original LogicalProject has collation trait (see the picture in FLINK-15381: [1] which means the second field is ordered and its direction is ascending), but when LogicalProject converts to LogicalCalc in ProjectToCalcRule, the collation info of new Calc is empty. The root cause is the collation derive logic on RelSubset in RelMdCollation in not collect.
This PR aim to fix the bug

Brief change log

  • Implement Collation handler in Flink (most logic except the part about RelSubset is same with RelMdCollation)

Verifying this change

This change added tests and can be verified as follows:

  • Added FlinkRelMdCollationTest to verify the collation derive logic on each RelNode
  • Added test that validates the plan of select cast(a as int), cast(b as varchar) from (values (3, 'c')) T(a,b)

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

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

Documentation

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

* {@link org.apache.calcite.rel.metadata.RelMetadataQuery#collations}
* for the standard logical algebra.
*/
public class FlinkRelMdCollation implements MetadataHandler<BuiltInMetadata.Collation> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most logic (except the part about RelSebset) is same with RelMdCollation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add the comment in The code. It's really a long class.

@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 e9b4471 (Thu Dec 26 07:17:26 UTC 2019)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!

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 26, 2019

CI report:

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

@godfreyhe
Copy link
Contributor Author

@flinkbot run travis

Copy link
Contributor

@beyond1920 beyond1920 left a comment

Choose a reason for hiding this comment

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

@godfreyhe ,the fix makes sense.
However, it's better to fix the bug in Calcite.
Or Mark "The class should be removed after CALCITE-{JIARANUMBER} is fixed." in the header comment of the class.
What do you think?

return mq.collations(rel.getOriginal());
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

The fix makes sense.

* {@link org.apache.calcite.rel.metadata.RelMetadataQuery#collations}
* for the standard logical algebra.
*/
public class FlinkRelMdCollation implements MetadataHandler<BuiltInMetadata.Collation> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add the comment in The code. It's really a long class.

@godfreyhe
Copy link
Contributor Author

@godfreyhe ,the fix makes sense.
However, it's better to fix the bug in Calcite.
Or Mark "The class should be removed after CALCITE-{JIARANUMBER} is fixed." in the header comment of the class.
What do you think?

thanks for the suggestion @beyond1920 . Flink had rewritten most metadata handlers defined in Calcite. I think we should keep this class in order to support more RelNodes (many RelNodes can derive collation, like Rank). So I would like to add some comments in public ImmutableList<RelCollation> collations(RelSubset subset, RelMetadataQuery mq) method instead of in the header of the class.

Copy link
Contributor

@beyond1920 beyond1920 left a comment

Choose a reason for hiding this comment

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

I'm agreed with you.
However it's unsafe to throw exception in the else branch.

RelNode rel = Util.first(subset.getBest(), subset.getOriginal());
return mq.collations(rel);
} else {
throw new RuntimeException("CALCITE_1048 is fixed, so check this method again!");
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unsafe to throw exception here.

@godfreyhe
Copy link
Contributor Author

godfreyhe commented Jan 2, 2020

I'm agreed with you.
However it's unsafe to throw exception in the else branch.

FlinkRelMdUniqueKeys and FlinkRelMdUniqueGroups also throw exception for this case and there are many test cases will fail once CALCITE_1048 is fixed

@beyond1920
Copy link
Contributor

LGTM

@wuchong
Copy link
Member

wuchong commented Jan 2, 2020

@godfreyhe could you remove the cast string on this line?

tableEnv.sqlUpdate("insert overwrite db1.dest values (3,cast('c' as varchar))");

It should work now if the bug is fixed.

Copy link
Member

@wuchong wuchong left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the effort @godfreyhe .

@wuchong wuchong merged commit 6b6dfd0 into apache:master Jan 2, 2020
wuchong pushed a commit that referenced this pull request Jan 2, 2020
@godfreyhe godfreyhe deleted the FLINK-15381 branch January 3, 2020 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants