Skip to content

Skip literal vs round tripped comparison for non-orderable types in UnwrapCastInComparison#16815

Merged
martint merged 1 commit intotrinodb:masterfrom
JunhyungSong:skip-unwrap-cast-in-comparison-for-unorderable-types
Apr 15, 2023
Merged

Skip literal vs round tripped comparison for non-orderable types in UnwrapCastInComparison#16815
martint merged 1 commit intotrinodb:masterfrom
JunhyungSong:skip-unwrap-cast-in-comparison-for-unorderable-types

Conversation

@JunhyungSong
Copy link
Copy Markdown
Member

@JunhyungSong JunhyungSong commented Mar 30, 2023

Skip literal vs round tripped comparison for non-orderable types in UnwrapCastInComparison

  • Non-orderable type allows only equality checks, so round tripped comparison cannot be applicable

Description

Additional context and related issues

Release notes

(v ) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:

# Section
* Fix some things. ({issue}`issuenumber`)

@cla-bot cla-bot bot added the cla-signed label Mar 30, 2023
@JunhyungSong JunhyungSong requested a review from martint March 30, 2023 20:28
@martint
Copy link
Copy Markdown
Member

martint commented Mar 30, 2023

The optimization works for non-orderable types, too, when they are “equatable” (they support equals/not-equals).

What problem are you trying to solve?

BTW, you can read more about the optimization here: https://trino.io/blog/2019/05/21/optimizing-the-casts-away.html

@JunhyungSong
Copy link
Copy Markdown
Member Author

JunhyungSong commented Mar 30, 2023

The optimization works for non-orderable types, too, when they are “equatable” (they support equals/not-equals).

What problem are you trying to solve?

BTW, you can read more about the optimization here: https://trino.io/blog/2019/05/21/optimizing-the-casts-away.html

The simple query below,

select * from (select cast(row(
    MAP(ARRAY['foo', 'bar'], ARRAY[1, 2])
  ) as row(a MAP(VARCHAR(3), BIGINT))) as row_column)
where row_column = row(
    MAP(ARRAY['foo', 'bar'], ARRAY[1, 2])
  )

fails because map type is not orderable. Because UnwrapCastInComparison calls getComparisonUnorderedLastOperator in the compare function.

@martint
Copy link
Copy Markdown
Member

martint commented Mar 30, 2023

The proper fix would be to check if the type is not orderable but equatable, skip the literalVsRoundtripped > 0 and literalVsRoundtripped < 0 branches and just perform the corresponding logic depending on whether the value is equal or not.

@JunhyungSong
Copy link
Copy Markdown
Member Author

The proper fix would be to check if the type is not orderable but equatable, skip the literalVsRoundtripped > 0 and literalVsRoundtripped < 0 branches and just perform the corresponding logic depending on whether the value is equal or not.

Will try.

@JunhyungSong JunhyungSong force-pushed the skip-unwrap-cast-in-comparison-for-unorderable-types branch from 86306fe to 6e64042 Compare March 31, 2023 01:16
@JunhyungSong JunhyungSong changed the title Skip UnwrapCastInComparison for non-orderable types Skip literal vs round tripped comparison for non-orderable types in UnwrapCastInComparison Mar 31, 2023
@JunhyungSong JunhyungSong force-pushed the skip-unwrap-cast-in-comparison-for-unorderable-types branch from 6e64042 to fd95845 Compare March 31, 2023 01:18
@JunhyungSong
Copy link
Copy Markdown
Member Author

@martint can you review, please? Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is unnecessary. It can be made to support row types by changing the query to do this, instead:

FROM (VALUES CAST(ROW(%s) AS row(%s))) t(v)

@JunhyungSong JunhyungSong force-pushed the skip-unwrap-cast-in-comparison-for-unorderable-types branch from fd95845 to dec4f5f Compare April 6, 2023 04:40
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should never happen. If the type of the right term of the comparison expression were not orderable or comparable, the ComparisonExpression would be invalid.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right. This is an additional check arguments like other check arguments as safeguards since this should not add any overheads. Also, I think this is helpful to understand the codes. I can change it to throw instead of silently return. Or do you think it's totally superfluous?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, it's superfluous. There's an assumption everywhere in the optimizer that the plan that is provided to each rule is well-formed. It's unreasonable to expect that every rule has to validate that structure. Otherwise, we'll end up with these types of checks sprinkled all over the code and we'll end up with code that's much harder to read and follow.

Comment on lines 369 to 371
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is unnecessary, too. It's effectively validating that the ComparisonExpression that was passed to this method is valid, but that's an assumption we don't need to check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is also a safeguard. In case there is a bug in parser/checker, it would be easier to understand what the cause is. And this also improves the code readability. But, if you strongly believe that this is not beneficial at all, I'm fine with deleting it.

@JunhyungSong JunhyungSong force-pushed the skip-unwrap-cast-in-comparison-for-unorderable-types branch from dec4f5f to c913919 Compare April 6, 2023 20:43
…nwrapCastInComparison

 - Non-orderable type allows only equality checks, so round tripped comparison cannot be applicable.
@JunhyungSong JunhyungSong force-pushed the skip-unwrap-cast-in-comparison-for-unorderable-types branch from c913919 to ad8151f Compare April 13, 2023 02:58
@JunhyungSong
Copy link
Copy Markdown
Member Author

The test failures seem irrelevant. Can you merge this if you don't have more comments, please?

@martint martint merged commit d277013 into trinodb:master Apr 15, 2023
@github-actions github-actions bot added this to the 414 milestone Apr 15, 2023
@JunhyungSong JunhyungSong deleted the skip-unwrap-cast-in-comparison-for-unorderable-types branch April 26, 2023 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants