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

feat: support stride in array_slice, change indexes to be1 based #8829

Merged
merged 10 commits into from
Jan 21, 2024

Conversation

Weijun-H
Copy link
Member

@Weijun-H Weijun-H commented Jan 11, 2024

Which issue does this PR close?

Closes #8784

Rationale for this change

What changes are included in this PR?

support array_slice with stride argument

array_slice(make_array(1, 2, 3, 4, 5), 1, 5, 2) --> [1, 3, 5]

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Physical Expressions sqllogictest SQL Logic Tests (.slt) labels Jan 11, 2024
@Weijun-H Weijun-H changed the title support 'stride' in 'array_slice' support stride in array_slice Jan 11, 2024

query error Execution error: array_slice got invalid stride: 0, it cannot be 0
select array_slice(make_array(1, 2, 3, 4, 5), 1, 5, 0), array_slice(make_array('h', 'e', 'l', 'l', 'o'), 1, 5, 0);

Copy link
Contributor

Choose a reason for hiding this comment

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

SELECT([1, 2, 3, 4, 5])[4:2:-2]; are also needed. And other examples in duckdb, we should consider them too.

https://duckdb.org/docs/sql/functions/nested#slicing

Copy link
Member Author

@Weijun-H Weijun-H Jan 12, 2024

Choose a reason for hiding this comment

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

Yes, I think so. And we could do it in ticket #8830 .

@Weijun-H Weijun-H marked this pull request as draft January 11, 2024 13:52
@Weijun-H Weijun-H force-pushed the feat-array-slice-stride branch from 6b56383 to d41c3a2 Compare January 12, 2024 06:07
@Weijun-H Weijun-H marked this pull request as ready for review January 12, 2024 06:07
@Weijun-H Weijun-H requested a review from jayzhan211 January 12, 2024 08:44
@@ -680,6 +689,16 @@ where
let end = offset_window[1];
let len = end - start;

let stride = if let Some(stride) = stride {
Copy link
Contributor

Choose a reason for hiding this comment

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

this construction most likely can be replaced by .map/.and_then ?

(start + to + O::usize_as(1)).to_usize().unwrap(),
);
offsets.push(offsets[row_index] + (to - from + O::usize_as(1)));
if let Some(stride) = stride {
Copy link
Contributor

Choose a reason for hiding this comment

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

this check may be earlier?

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

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

Thanks @Weijun-H, just couple of minors, and we need to modify the user documentation?

@@ -731,7 +731,7 @@ scalar_expr!(
scalar_expr!(
ArraySlice,
array_slice,
array offset length,
array offset length stride,
Copy link
Contributor

@jayzhan211 jayzhan211 Jan 13, 2024

Choose a reason for hiding this comment

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

It seems offset length is incorrect, it should be begin end

Copy link
Contributor

@jayzhan211 jayzhan211 left a comment

Choose a reason for hiding this comment

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

LGTM

@Weijun-H Weijun-H changed the title support stride in array_slice feat: support stride in array_slice Jan 13, 2024
@Weijun-H Weijun-H force-pushed the feat-array-slice-stride branch from d11a0cc to 977f2e1 Compare January 13, 2024 06:02
@Weijun-H
Copy link
Member Author

stalled until #8847 fixed

@@ -700,17 +709,67 @@ where
};

if let (Some(from), Some(to)) = (from_index, to_index) {
let stride = stride.map(|s| s.value(row_index));
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

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

Thanks @Weijun-H for the fixes, its much nicer now. Waiting for the CI

@alamb alamb marked this pull request as draft January 15, 2024 10:49
@alamb
Copy link
Contributor

alamb commented Jan 15, 2024

stalled until #8847 fixed

Marking as draft to make it clear this PR is not waiting on review

@Weijun-H Weijun-H force-pushed the feat-array-slice-stride branch from 977f2e1 to 297ef22 Compare January 15, 2024 11:52
@Weijun-H Weijun-H marked this pull request as ready for review January 15, 2024 13:17
@alamb alamb changed the title feat: support stride in array_slice feat: support stride in array_slice, change indexes to be 1 based Jan 19, 2024
@alamb alamb changed the title feat: support stride in array_slice, change indexes to be 1 based feat: support stride in array_slice, change indexes to be0 based Jan 19, 2024
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looks good to me @Weijun-H -- thank you

Can you confirm the behavior change to be 0 based indexes rather than 1 based indexes is intentional? I updated the PR title but I wanted to double check

@Weijun-H
Copy link
Member Author

Looks good to me @Weijun-H -- thank you

Can you confirm the behavior change to be 0 based indexes rather than 1 based indexes is intentional? I updated the PR title but I wanted to double check

array_slice is 1-index, not 0-index, which aligns with PostgreSQL and DuckDB. @alamb

@alamb alamb changed the title feat: support stride in array_slice, change indexes to be0 based feat: support stride in array_slice, change indexes to be1 based Jan 21, 2024
@alamb
Copy link
Contributor

alamb commented Jan 21, 2024

Looks good to me @Weijun-H -- thank you
Can you confirm the behavior change to be 0 based indexes rather than 1 based indexes is intentional? I updated the PR title but I wanted to double check

array_slice is 1-index, not 0-index, which aligns with PostgreSQL and DuckDB. @alamb

Fixed

@alamb alamb merged commit 0116e2a into apache:main Jan 21, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions physical-expr Physical Expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support stride for array_slice
4 participants