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 third argument for array_position() built-in #112161

Merged
merged 1 commit into from Dec 1, 2023

Conversation

charlespnh
Copy link
Contributor

Resolves: #109953

Release note (sql change): The array_position() built-in function with additional third argument was added.

@charlespnh charlespnh requested a review from a team as a code owner October 11, 2023 08:29
@blathers-crl
Copy link

blathers-crl bot commented Oct 11, 2023

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

Before a member of our team reviews your PR, I have some potential action items for you:

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

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

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

This change is Reviewable

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 @charlespnh)


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

					start := int(tree.MustBeDInt(args[2])) - 1
					darray := tree.MustBeDArray(args[0]).Array
					if start > len(darray) || start < 1 {

i don't think the start < 1 condition matches the PostgreSQL behavior:

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', 0);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', -1);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', -3);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', 1);
 array_position
----------------
              4

It looks like any start value less than 1 is implicitly treated as 1. Can you make that change (perhaps include a comment), and add tests for that?


pkg/sql/logictest/testdata/logic_test/array line 1140 at r1 (raw file):

SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', 0)
----
NULL

in postgres, this returns 4


pkg/sql/logictest/testdata/logic_test/array line 1146 at r1 (raw file):

----
NULL

could you add these tests to verify that the second occurrence of an element is found correctly:

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'mon', 7);
 array_position
----------------
              8
(1 row)

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'mon', 1);
 array_position
----------------
              2

Copy link

blathers-crl bot commented Nov 23, 2023

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

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

Copy link

blathers-crl bot commented Nov 25, 2023

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 dev-inf.

Copy link
Contributor Author

@charlespnh charlespnh left a comment

Choose a reason for hiding this comment

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

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


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

Previously, rafiss (Rafi Shamim) wrote…

i don't think the start < 1 condition matches the PostgreSQL behavior:

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', 0);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', -1);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', -3);
 array_position
----------------
              4

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'wed', 1);
 array_position
----------------
              4

It looks like any start value less than 1 is implicitly treated as 1. Can you make that change (perhaps include a comment), and add tests for that?

Change is applied and comment has been added!


pkg/sql/logictest/testdata/logic_test/array line 1140 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

in postgres, this returns 4

Change is applied!


pkg/sql/logictest/testdata/logic_test/array line 1146 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

could you add these tests to verify that the second occurrence of an element is found correctly:

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'mon', 7);
 array_position
----------------
              8
(1 row)

postgres=# SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat','mon'], 'mon', 1);
 array_position
----------------
              2

Tests added!

@charlespnh
Copy link
Contributor Author

@rafiss Thanks for the feedback! Please let me know if there are any other meaningful tests I should have added

@rafiss rafiss requested a review from a team as a code owner December 1, 2023 22:24
Release note (sql change): Added support for third argument in the
array_position() builtin. If provided, it gives the index from
which to begin searching in the array.
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.

i added one more test i just thought of, to check for null inputs, which required a change to null handling in the code. thanks for your contribution!

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 1, 2023

Build succeeded:

@craig craig bot merged commit a343889 into cockroachdb:master Dec 1, 2023
8 of 9 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.

sql: support third argument (startIndex) for function array_position
3 participants