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

evalEngine: Implement string INSERT #15201

Merged
merged 8 commits into from
Feb 12, 2024
Merged

Conversation

beingnoble03
Copy link
Member

@beingnoble03 beingnoble03 commented Feb 10, 2024

Description

This PR adds implementation of INSERT func in evalengine.

Related Issue(s)

Fixes part of #9647

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Copy link
Contributor

vitess-bot bot commented Feb 10, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Feb 10, 2024
@github-actions github-actions bot added this to the v20.0.0 milestone Feb 10, 2024
go/vt/vtgate/evalengine/fn_string.go Show resolved Hide resolved
"5",
"0",
"2",
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this list should also include a bunch of non ideal cases. Like NULL (see the earlier comment), decimals, floats etc. I think for example inputBitwise is a good existing list with mostly numerical values.

Copy link
Contributor

Choose a reason for hiding this comment

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

The way to think about this is "how would I break this" by throwing explicitly bad / unexpected data at the function to see how it behaves then to make sure it's consistent with MySQL (and doesn't panic or error unexpectedly etc).

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, I forgot to add overflow checks.

go/vt/vtgate/evalengine/fn_string.go Outdated Show resolved Hide resolved
switch {
case str.isTextual():
default:
c.asm.Convert_xce(4, str.Type, call.collate)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this convert to the collation given by call.collate or by the other argument? Or should we merge collations with the appropriate logic? See also #15195 for how collations are handled there.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we can merge the collations here, as MySQL returns the collations of the str, not newstr if it is textual.
This can be observed by running these 2 queries: SELECT COLLATION(INSERT(_latin1 0xFF, 1, 3, "asd")); and SELECT COLLATION(INSERT("asd", 2, 3, _latin1 0xFF));.

But, I think there was a mistake here, as it should be c.asm.Convert_xce(4, sqltypes.VarChar, c.collation)

We have handled a similar case in LPAD/RPAD.

Copy link
Contributor

Choose a reason for hiding this comment

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

In the pad functions, we convert to the collation of str, and looking at how this works, we should do the same here?

So should it be c.asm.Convert_xce(4, sqltypes.VarChar, col.collation) instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah wait, that's already the case, but that's missing checks to not emit that conversion if it's not needed.


front := charset.Slice(cs, str.bytes, 0, pos)
var back []byte
if pos <= math.MaxInt-l && pos+l < strLen {
Copy link
Member Author

Choose a reason for hiding this comment

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

pos <= math.MaxInt-l can be used here to check overflow, as both pos and l are positive (checks added above).

col = typedCoercionCollation(sqltypes.VarChar, c.collation)
}

c.asm.Convert_xce(1, sqltypes.VarChar, col.Collation)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this conversion should be conditional? This is only needed if it's not already a string?

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

c.asm.Fn_INSERT(col)
c.asm.jumpDestination(skip)

return ctype{Type: sqltypes.VarChar, Col: col, Flag: flagNullable}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there cases where this function can return NULL if all inputs are not NULL?

Copy link
Member Author

@beingnoble03 beingnoble03 Feb 11, 2024

Choose a reason for hiding this comment

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

Yes, if the resultant string size is greater than max_allowed_packet, I think I forgot to add the validation for that. Pushed now. 😅

@dbussink
Copy link
Contributor

@beingnoble03 So there's a failure in the planbuilder tests, but the new output is correct since now the insert function is supported in the evalengine and it doesn't have to send the query down to a tablet. You can update the expectations (it should tell you how if you run those tests).

Otherwise I can also push up that change if you want.

@beingnoble03
Copy link
Member Author

@dbussink done.

Copy link

codecov bot commented Feb 11, 2024

Codecov Report

Attention: 16 lines in your changes are missing coverage. Please review.

Comparison is base (ea62cee) 67.28% compared to head (04490f4) 67.33%.
Report is 1 commits behind head on main.

Files Patch % Lines
go/vt/vtgate/evalengine/fn_string.go 88.88% 8 Missing ⚠️
go/vt/vtgate/evalengine/compiler_asm.go 81.81% 4 Missing ⚠️
go/vt/vtgate/evalengine/translate_builtin.go 78.94% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15201      +/-   ##
==========================================
+ Coverage   67.28%   67.33%   +0.05%     
==========================================
  Files        1560     1560              
  Lines      192264   192395     +131     
==========================================
+ Hits       129366   129556     +190     
+ Misses      62898    62839      -59     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dbussink dbussink added Type: Enhancement Logical improvement (somewhere between a bug and feature) and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 11, 2024
@dbussink dbussink added the Component: Evalengine changes to the evaluation engine label Feb 11, 2024
"0123",
"0xAACC",
"3.1415926",
// MySQL has broken behavior for these inputs,
Copy link
Contributor

Choose a reason for hiding this comment

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

You can reference mysql/mysql-server#517 here so we know what this is about in the future too.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

@dbussink dbussink added Type: Enhancement Logical improvement (somewhere between a bug and feature) and removed Type: Enhancement Logical improvement (somewhere between a bug and feature) labels Feb 11, 2024
Signed-off-by: Noble Mittal <[email protected]>

copy(res[:len(front)], front)
copy(res[len(front):], newstr.bytes)
copy(res[len(front)+len(newstr.bytes):], back)
Copy link
Contributor

Choose a reason for hiding this comment

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

@vmg Do you think it's worth extending charset.Slice to allow passing in an existing output []byte or is that a bit premature just for this function?

Copy link
Collaborator

@vmg vmg Feb 12, 2024

Choose a reason for hiding this comment

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

I think it would make for a more awkward API, don't you think? Right now Slice doesn't allocate (it returns a sub-slice of the input), so if we add an out parameter, we'd have to make it always allocate or have two code paths in the function for when out is nil.

Furthermore, if the Slice API had an out parameter, it would allocate more than once when used for INSERT, since we wouldn't know the required capacity of the out buffer, so it'd have to grow it at least twice.

With the current non-allocating Slice, we just call the function twice and then we get to compose the final result outside of the function with a single allocation, since we know the final size. I quite dig the version of the code as it is written.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah, good point. It's already non-allocating so it doesn't really help in any way.

Copy link
Collaborator

@vmg vmg left a comment

Choose a reason for hiding this comment

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

Lookin' great as usual!


copy(res[:len(front)], front)
copy(res[len(front):], newstr.bytes)
copy(res[len(front)+len(newstr.bytes):], back)
Copy link
Collaborator

@vmg vmg Feb 12, 2024

Choose a reason for hiding this comment

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

I think it would make for a more awkward API, don't you think? Right now Slice doesn't allocate (it returns a sub-slice of the input), so if we add an out parameter, we'd have to make it always allocate or have two code paths in the function for when out is nil.

Furthermore, if the Slice API had an out parameter, it would allocate more than once when used for INSERT, since we wouldn't know the required capacity of the out buffer, so it'd have to grow it at least twice.

With the current non-allocating Slice, we just call the function twice and then we get to compose the final result outside of the function with a single allocation, since we know the final size. I quite dig the version of the code as it is written.

@vmg vmg merged commit 8586b6d into vitessio:main Feb 12, 2024
102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Evalengine changes to the evaluation engine Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants