Skip to content

optbuilder: remove spurious casts in binary ops#85108

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
jordanlewis:fix-trigram-mod-op
Jul 28, 2022
Merged

optbuilder: remove spurious casts in binary ops#85108
craig[bot] merged 1 commit intocockroachdb:masterfrom
jordanlewis:fix-trigram-mod-op

Conversation

@jordanlewis
Copy link
Copy Markdown
Member

@jordanlewis jordanlewis commented Jul 27, 2022

Fixes #85106

Previously, the optbuilder would insert spurious casts in binary
operators while building a plan to avoid a tricky type checking case.
This got in the way of checking for inverted filter support for
trigrams. Now, the code only inserts the cast if necessary (if the input
expression is NULL).

Release note: None

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@jordanlewis jordanlewis marked this pull request as ready for review July 27, 2022 12:38
@jordanlewis jordanlewis requested a review from a team as a code owner July 27, 2022 12:38
@jordanlewis jordanlewis requested a review from mgartner July 27, 2022 12:38
@jordanlewis
Copy link
Copy Markdown
Member Author

Hi @mgartner, tagging you because you've dealt with these auto-casts a bunch. I think that all of the casts here are spurious, but obviously I'm not sure and would be concerned I'm missing something.

If this is an unpalatable approach I can scope it down quite a bit, just thought I'd check to see if we can remove these excess casts while we're at it.

Copy link
Copy Markdown
Contributor

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

:lgtm_strong:

The fact that we add these casts at all is questionable - we can potentially add casts which are supposed to only be allowed when appearing explicitly in a query. Notice this comment in eval.ReType which is called by the reType in this commit:

// TODO(#75103): For legacy reasons, we check for a valid cast in the most
// permissive context, ContextExplicit. To be consistent with Postgres,
// we should check for a valid cast in the most restrictive context,
// ContextImplicit.
if !cast.ValidCast(resolvedType, wantedType, cast.ContextExplicit) {
return nil, false
}

So this seems like a step in the right direction, regardless of the fact that it fixes trigrams.

Reviewed 19 of 19 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/opt/optbuilder/scalar.go line 221 at r1 (raw file):

		// issues for the execbuilder, which doesn't have enough information to
		// select the right overload. The solution is to wrap any mismatched
		// arguments with a CastExpr that preserves the static type.

It sounds like we could avoid these casts entirely if we attached t.ResolvedBinOp().[Left|Right]Type to the NullExpr constructed in b.buildScalar below. If the Null was typed, then execbuilder should be able to determine the correct overload. I created an issue to track this: #85135

@jordanlewis
Copy link
Copy Markdown
Member Author

Thanks for the review! @yuzefovich could you briefly look at the changed vectorized execution plans and let me know if you see any potential issues too - i know we've had some trouble in the past with integer width casts getting messed up in vectorized and want to make sure this doesn't regress us.

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

LGTM - we also run all of the queries in vectorize_overloads file as a logic test. One thing that'd be good to do (to get more confidence in this PR) is to include fakedist config into pkg/sql/logictest/testdata/logic_test/vectorize_overloads. (I think originally we had both the query execution and the EXPLAIN (VEC) in the same file, so we couldn't use fakedist config, but we have since split those two up, so it should be possible to run with more configs.) Actually, maybe just remove the config specification in that file completely so that we run with all default configs.

Reviewed 2 of 19 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @jordanlewis)

Copy link
Copy Markdown
Contributor

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @jordanlewis)

@jordanlewis
Copy link
Copy Markdown
Member Author

Good suggestion, I added all the configs to that file (and a few missing rowsort declarations).

bors r+

@yuzefovich
Copy link
Copy Markdown
Member

I think this will come into a conflict with the updated logic test framework, so I'll unbors it for now.

bors r-

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 27, 2022

Canceled.

Previously, the optbuilder would insert spurious casts in binary
operators while building a plan to avoid a tricky type checking case.
This got in the way of checking for inverted filter support for
trigrams. Now, the code only inserts the cast if necessary (if the input
expression is NULL).

Release note: None
@yuzefovich yuzefovich force-pushed the fix-trigram-mod-op branch from 1ccbcae to 5a03f03 Compare July 28, 2022 03:18
@yuzefovich
Copy link
Copy Markdown
Member

Courtesy fixup :)

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 28, 2022

Build failed:

@jordanlewis
Copy link
Copy Markdown
Member Author

backupccl package level flake

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 28, 2022

Build succeeded:

@craig craig bot merged commit a7124fd into cockroachdb:master Jul 28, 2022
@jordanlewis jordanlewis deleted the fix-trigram-mod-op branch July 30, 2022 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: trigram inverted index acceleration for % operator doesn't work if indexed column is VARCHAR

4 participants