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

builtin: add cardinality builtin function #68263

Merged
merged 1 commit into from
Jul 31, 2021

Conversation

jlevesy
Copy link
Contributor

@jlevesy jlevesy commented Jul 30, 2021

To ensure PostgreSQL compatibility, this commit adds a new builtin
function called cardinality which returns the total number of elements
present in a given array.

Fixes: #67996

Release note (sql change): Added cardinality builtin function that
returns the total number of elements in a given array.

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Jul 30, 2021

CLA assistant check
All committers have signed the CLA.

@blathers-crl
Copy link

blathers-crl bot commented Jul 30, 2021

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.

I have added a few people who may be able to assist in reviewing:

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Jul 30, 2021
@blathers-crl blathers-crl bot requested a review from rafiss July 30, 2021 07:40
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss
Copy link
Collaborator

rafiss commented Jul 30, 2021

thanks for your contribution! it's not exactly equivalent to array_length, just similar. the important difference is the way it handles empty arrays -- if you are able to try it out on postgres you'll see that cardinality returns 0 while array_length returns null.

please address that and add a test case

@jlevesy jlevesy force-pushed the 67996/add-cardinality-builtin branch from da56e00 to 7cacbd9 Compare July 30, 2021 20:41
@blathers-crl
Copy link

blathers-crl bot commented Jul 30, 2021

Thank you for updating your pull request.

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 otan.

@jlevesy jlevesy force-pushed the 67996/add-cardinality-builtin branch 2 times, most recently from 3eb093a to a9fae64 Compare July 30, 2021 20:52
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 is getting close!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jlevesy)


pkg/sql/logictest/testdata/logic_test/builtin_function, line 1787 at r1 (raw file):

SELECT cardinality(ARRAY[]:::int[])
----
0

can you add one more test case for SELECT cardinality(NULL::int[]) -- it should return NULL


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

			Fn: func(_ *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
				arr := tree.MustBeDArray(args[0])
				return tree.NewDInt(tree.DInt(arr.Len())), nil

sorry to be nitpicky, but i'm worried that if we ever do support multi-dimensional arrays, this function will not work but we won't realize. can you add a recursive implementation? (here's what i'm thinking, borrowing from the arrayLength code)

func cardinality(arr *tree.DArray) tree.Datum {
	if arr.ParamTyp.Family() != types.ArrayFamily {
		return tree.NewDInt(tree.DInt(arr.Len()))
	}
	card := 0
	for _, a := range arr.Array {
		card += int(tree.MustBeDInt(cardinality(tree.MustBeDArray(a))))
	}
	return tree.NewDInt(tree.DInt(card))
}

@jlevesy
Copy link
Contributor Author

jlevesy commented Jul 31, 2021

Will do! Thanks for your reviews anyway 😄

To ensure PostgreSQL compatibility, this commit adds a new builtin
function called `cardinality` which returns the total number of elements
present in a given array.

Fixes: cockroachdb#67996

Release note (sql change): Added cardinality builtin function that
returns the total number of elements in a given array.
@jlevesy jlevesy force-pushed the 67996/add-cardinality-builtin branch from a9fae64 to 0b53fa8 Compare July 31, 2021 08:57
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!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 31, 2021

Build succeeded:

@craig craig bot merged commit ff20d1e into cockroachdb:master Jul 31, 2021
@jlevesy jlevesy deleted the 67996/add-cardinality-builtin branch August 2, 2021 07:52
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 X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: implement CARDINALITY builtin for arrays
3 participants