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

opt,sql: don't fold "- 0" for JSON #35617

Merged
merged 1 commit into from
Mar 11, 2019
Merged

Conversation

justinj
Copy link
Contributor

@justinj justinj commented Mar 11, 2019

Fixes #35612.

Prior to this commit, we would unconditionally fold x - 0 to x. This was
invalid in the case where x was JSON, since - 0 in that case means
"delete the 0th entry in the array".

This bug existed in both the HP and CBO.

This fix slightly abuses the existing "types.IsAdditiveType" function to
determine whether or not the identity applies.

Release note (bug fix): Subtracting 0 from a JSON array now correctly
removes its first element.

@justinj justinj requested review from knz and rytaft March 11, 2019 19:42
@justinj justinj requested a review from a team as a code owner March 11, 2019 19:42
@justinj justinj requested a review from a team March 11, 2019 19:42
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

thank you!

@knz
Copy link
Contributor

knz commented Mar 11, 2019

this may want a backport.

Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm:

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


pkg/sql/opt/norm/custom_funcs.go, line 1295 at r1 (raw file):

// ----------------------------------------------------------------------

// IsAdditive returned true if the type of the expression supports addition and

returned -> returns


pkg/sql/opt/norm/custom_funcs.go, line 1298 at r1 (raw file):

// subtraction in the natural way. This differs from "has a +/- Numeric
// implementation" because JSON has an implementation for "- INT" which doesn't
// obey x - 0 = x. This includes not just numeric types, but also DATE.

[nit] this last sentence is a bit confusing. I'd say something like "additive types include all numeric types as well as timestamps and dates"


pkg/sql/opt/norm/rules/numeric.opt, line 23 at r1 (raw file):

# FoldMinusZero folds $left - 0 for numeric types. This rule requires a check
# that $left is numeric because JSON - INT is valid and is not a no-op with a
# zero value.

Is subtraction the only operation that JSON performs differently?

Fixes cockroachdb#35612.

Prior to this commit, we would unconditionally fold x - 0 to x. This was
invalid in the case where x was JSON, since `- 0` in that case means
"delete the 0th entry in the array".

This bug existed in both the HP and CBO.

This fix slightly abuses the existing "types.IsAdditiveType" function to
determine whether or not the identity applies.

Release note (bug fix): Subtracting 0 from a JSON array now correctly
removes its first element.
Copy link
Contributor Author

@justinj justinj left a comment

Choose a reason for hiding this comment

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

Agreed, will backport. Thanks all!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rytaft)


pkg/sql/opt/norm/custom_funcs.go, line 1295 at r1 (raw file):

Previously, rytaft wrote…

returned -> returns

Done.


pkg/sql/opt/norm/custom_funcs.go, line 1298 at r1 (raw file):

Previously, rytaft wrote…

[nit] this last sentence is a bit confusing. I'd say something like "additive types include all numeric types as well as timestamps and dates"

Done.


pkg/sql/opt/norm/rules/numeric.opt, line 23 at r1 (raw file):

Previously, rytaft wrote…

Is subtraction the only operation that JSON performs differently?

It's the only one it has, there's no implementation for the other "arithmetic" operators.

craig bot pushed a commit that referenced this pull request Mar 11, 2019
35617: opt,sql: don't fold "- 0" for JSON r=justinj a=justinj

Fixes #35612.

Prior to this commit, we would unconditionally fold x - 0 to x. This was
invalid in the case where x was JSON, since `- 0` in that case means
"delete the 0th entry in the array".

This bug existed in both the HP and CBO.

This fix slightly abuses the existing "types.IsAdditiveType" function to
determine whether or not the identity applies.

Release note (bug fix): Subtracting 0 from a JSON array now correctly
removes its first element.

Co-authored-by: Justin Jaffray <[email protected]>
Copy link
Collaborator

@rytaft rytaft 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.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale)

@craig
Copy link
Contributor

craig bot commented Mar 11, 2019

Build succeeded

@craig craig bot merged commit 1aa4e56 into cockroachdb:master Mar 11, 2019
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: json minus (RemoveIndex) does not work properly
5 participants