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 the ergonomics of getting list slice with stride #8946

Merged
merged 10 commits into from
Jan 29, 2024

Conversation

Weijun-H
Copy link
Member

Which issue does this PR close?

Closes #8830

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions physical-expr Physical Expressions core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Jan 22, 2024
@Weijun-H
Copy link
Member Author

sqlparser-rs does not support these syntax.

❯ select [1,2,3][-1:-3:-2];  🤔 Invalid statement: sql parser error: Expected a value, found: -
❯ select [1,2,3][-1:-3];  🤔 Invalid statement: sql parser error: Expected a value, found: -

@jayzhan211
Copy link
Contributor

sqlparser-rs does not support these syntax.

❯ select [1,2,3][-1:-3:-2];  🤔 Invalid statement: sql parser error: Expected a value, found: -
❯ select [1,2,3][-1:-3];  🤔 Invalid statement: sql parser error: Expected a value, found: -

This is not valid in duckdb too, try select ([1,2,3])[-1:-3]

@alamb
Copy link
Contributor

alamb commented Jan 23, 2024

Hi @Weijun-H -- I wonder if you know of another database that offers this method of accessing lists?

@Weijun-H
Copy link
Member Author

Weijun-H commented Jan 24, 2024

Hi @Weijun-H -- I wonder if you know of another database that offers this method of accessing lists?

I only know DuckDB: https://duckdb.org/docs/sql/functions/nested.html#slicing @alamb

@Weijun-H
Copy link
Member Author

sqlparser-rs does not support these syntax.

❯ select [1,2,3][-1:-3:-2];  🤔 Invalid statement: sql parser error: Expected a value, found: -
❯ select [1,2,3][-1:-3];  🤔 Invalid statement: sql parser error: Expected a value, found: -

This is not valid in duckdb too, try select ([1,2,3])[-1:-3]

This Is the problem of sqlparser-rs, apache/datafusion-sqlparser-rs#1108.

}

/// Create a new [`GetIndexedFieldExpr`] for accessing the stride
pub fn new_stride(
Copy link
Contributor

@jayzhan211 jayzhan211 Jan 29, 2024

Choose a reason for hiding this comment

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

It is unused in this PR, and I think we might not need it too, since new_range is only used in test, which may be removed in the future since we can test them in slt.

@@ -208,10 +208,15 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {
let key = create_physical_name(key, false)?;
format!("{expr}[{key}]")
}
GetFieldAccess::ListRange { start, stop } => {
GetFieldAccess::ListStride {
Copy link
Contributor

Choose a reason for hiding this comment

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

One small thing, I think stride is equivalent to step, IMO ListRange is more appropriate than ListStride

)?);
let stop = Box::new(self.sql_expr_to_logical_expr(
*right,
// the last value could represent stop or stride
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

                let (start, stop, stride) = if let SQLExpr::JsonAccess { left: l, operator: JsonOperator::Colon, right: r } = *left {
                    let start = Box::new(self.sql_expr_to_logical_expr(*l, schema, planner_context)?);
                    let stop = Box::new(self.sql_expr_to_logical_expr(*r, schema, planner_context)?);
                    let stride = Box::new(self.sql_expr_to_logical_expr(*right, schema, planner_context)?);
                    (start, stop, stride)
                } else {
                    let start = Box::new(self.sql_expr_to_logical_expr(*left, schema, planner_context)?);
                    let stop = Box::new(self.sql_expr_to_logical_expr(*right, schema, planner_context)?);
                    let stride = Box::new(Expr::Literal(ScalarValue::Int64(Some(1))));
                    (start, stop, stride)
                };
                GetFieldAccess {
                    start,
                    stop,
                    stride
                }

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, left some minor comments

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.

Thank you @Weijun-H and @jayzhan211 🚀

@@ -750,6 +751,8 @@ fn roundtrip_get_indexed_field_list_range() -> Result<()> {
GetFieldAccessExpr::ListRange {
start: col_start,
stop: col_stop,
stride: Arc::new(Literal::new(ScalarValue::Int64(Some(1))))
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@alamb alamb merged commit fffc8be into apache:main Jan 29, 2024
21 of 22 checks passed
@alamb
Copy link
Contributor

alamb commented Jan 29, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Physical Expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support the ergonomics of getting list slice with stride
3 participants