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

builtins: support bit_count for bytea and varbit #115273

Merged
merged 1 commit into from
Dec 7, 2023

Conversation

andrew-delph
Copy link
Contributor

@andrew-delph andrew-delph commented Nov 29, 2023

Fixes: #114817

Release note (sql change): Added the bit_count builtin function
for BIT and BYTEA types.

@andrew-delph andrew-delph requested a review from a team as a code owner November 29, 2023 18:48
Copy link

blathers-crl bot commented Nov 29, 2023

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Nov 29, 2023

CLA assistant check
All committers have signed the CLA.

@blathers-crl blathers-crl bot added the O-community Originated from the community label Nov 29, 2023
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@andrew-delph andrew-delph requested a review from a team November 29, 2023 20:07
@andrew-delph andrew-delph requested review from a team as code owners November 29, 2023 20:07
@andrew-delph andrew-delph requested a review from a team November 29, 2023 20:07
@andrew-delph andrew-delph requested review from a team as code owners November 29, 2023 20:07
@andrew-delph andrew-delph requested a review from a team November 29, 2023 20:07
@andrew-delph andrew-delph requested review from a team as code owners November 29, 2023 20:07
@andrew-delph andrew-delph requested review from a team, abarganier, srosenberg, renatolabs and itsbilal and removed request for a team November 29, 2023 20:07
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

thanks for your contribution!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andrew-delph and @michae2)


pkg/sql/sem/builtins/builtins.go line 243 at r1 (raw file):

	"bit_count": makeBuiltin(tree.FunctionProperties{Category: builtinconstants.CategoryString},
		stringOverload1(

could you say more about why you added a string overload? it does not appear that postgres has one. i don't understand how it's expected to work on an input like: bit_count('abc')


pkg/sql/sem/builtins/builtins.go line 259 at r1 (raw file):

				total := 0
				for _, b := range s {
					total += bits.OnesCount(uint(b))

nit: should this be bits.OnesCount32(uint32(b)), since iterating over a string always returns int32s (which are aliased to runes)?


pkg/sql/sem/eval/testdata/eval/builtins line 102 at r1 (raw file):


eval
bit_count('1111111111'::bit(10))

can you also add testing for the bytea overload?

@andrew-delph andrew-delph force-pushed the bit_count branch 2 times, most recently from 66aea14 to 450a394 Compare December 1, 2023 22:15
@andrew-delph
Copy link
Contributor Author

@rafiss Thank you for your comments.

  1. I did remove the string overload as it doesn't make sense for the use case hence postgres doesn't include it.
  2. You are right. It is now revised to convert it to a []byte and then use bits.OnesCount8 for counting
  3. Tests from postgres are included :)

Fixes: cockroachdb#114817

Release note (sql change): Added the bit_count builtin function
for BIT and BYTEA types.
@rafiss rafiss changed the title builtins: support bit_count for strings,bytes,bits builtins: support bit_count for bytea and varbit Dec 7, 2023
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

this lgtm! thank you for your contribution

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 7, 2023

Build succeeded:

@craig craig bot merged commit 43b0dd3 into cockroachdb:master Dec 7, 2023
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-community Originated from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bit_count function
3 participants