diff --git a/datafusion-cli/tests/cli_integration.rs b/datafusion-cli/tests/cli_integration.rs index c1395aa4f562..7177d097e4c4 100644 --- a/datafusion-cli/tests/cli_integration.rs +++ b/datafusion-cli/tests/cli_integration.rs @@ -157,6 +157,10 @@ fn init() { "change_format_version", ["--file", "tests/sql/types_format.sql", "-q"], )] +#[case::fetch_not_supported( + "fetch_not_supported", + ["--command", "SELECT 1 FETCH NEXT 1 ROW ONLY", "-q"], +)] #[test] fn cli_quick_test<'a>( #[case] snapshot_name: &'a str, diff --git a/datafusion-cli/tests/snapshots/cli_quick_test@fetch_not_supported.snap b/datafusion-cli/tests/snapshots/cli_quick_test@fetch_not_supported.snap new file mode 100644 index 000000000000..1e38a48cfc85 --- /dev/null +++ b/datafusion-cli/tests/snapshots/cli_quick_test@fetch_not_supported.snap @@ -0,0 +1,16 @@ +--- +source: datafusion-cli/tests/cli_integration.rs +assertion_line: 176 +info: + program: datafusion-cli + args: + - "--command" + - SELECT 1 FETCH NEXT 1 ROW ONLY + - "-q" +--- +success: false +exit_code: 1 +----- stdout ----- +Error: This feature is not implemented: FETCH clause is not supported yet + +----- stderr ----- diff --git a/datafusion/sql/src/query.rs b/datafusion/sql/src/query.rs index d316550f4dd2..a759f0f29fdd 100644 --- a/datafusion/sql/src/query.rs +++ b/datafusion/sql/src/query.rs @@ -46,17 +46,31 @@ impl SqlToRel<'_, S> { let mut query_plan_context = outer_planner_context.clone(); let planner_context = &mut query_plan_context; - if let Some(with) = query.with { + let Query { + with, + body, + order_by, + limit_clause, + pipe_operators, + fetch, + .. + } = query; + + if fetch.is_some() { + return not_impl_err!("FETCH clause is not supported yet"); + } + + if let Some(with) = with { self.plan_with_clause(with, planner_context)?; } - let set_expr = *query.body; + let set_expr = *body; let plan = match set_expr { SetExpr::Select(mut select) => { let select_into = select.into.take(); let plan = - self.select_to_plan(*select, query.order_by, planner_context)?; - let plan = self.limit(plan, query.limit_clause, planner_context)?; + self.select_to_plan(*select, order_by.clone(), planner_context)?; + let plan = self.limit(plan, limit_clause.clone(), planner_context)?; // Process the `SELECT INTO` after `LIMIT`. self.select_into(plan, select_into) } @@ -69,7 +83,7 @@ impl SqlToRel<'_, S> { let _guard = StackGuard::new(256 * 1024); self.set_expr_to_plan(other, planner_context) }?; - let oby_exprs = to_order_by_exprs(query.order_by)?; + let oby_exprs = to_order_by_exprs(order_by)?; let order_by_rex = self.order_by_to_sort_expr( oby_exprs, plan.schema(), @@ -78,11 +92,11 @@ impl SqlToRel<'_, S> { None, )?; let plan = self.order_by(plan, order_by_rex)?; - self.limit(plan, query.limit_clause, planner_context) + self.limit(plan, limit_clause, planner_context) } }?; - self.pipe_operators(plan, query.pipe_operators, planner_context) + self.pipe_operators(plan, pipe_operators, planner_context) } /// Apply pipe operators to a plan diff --git a/datafusion/sql/tests/sql_integration.rs b/datafusion/sql/tests/sql_integration.rs index 96d9f23522f1..f4656134c61a 100644 --- a/datafusion/sql/tests/sql_integration.rs +++ b/datafusion/sql/tests/sql_integration.rs @@ -3901,6 +3901,13 @@ Limit: skip=3, fetch=5 ); } +#[test] +fn fetch_clause_is_not_supported() { + let sql = "SELECT 1 FETCH NEXT 1 ROW ONLY"; + let err = logical_plan(sql).unwrap_err(); + assert_contains!(err.to_string(), "FETCH clause is not supported yet"); +} + #[test] fn test_offset_before_limit() { let sql = "select id from person where person.id > 100 OFFSET 3 LIMIT 5;"; diff --git a/docs/source/user-guide/sql/select.md b/docs/source/user-guide/sql/select.md index baacf432f5fd..3e78681ac2a4 100644 --- a/docs/source/user-guide/sql/select.md +++ b/docs/source/user-guide/sql/select.md @@ -306,6 +306,8 @@ SELECT age, person FROM table ORDER BY age, person DESC; Limits the number of rows to be a maximum of `count` rows. `count` should be a non-negative integer. +DataFusion does not currently support the SQL `FETCH` clause. Use `LIMIT` to constrain the number of returned rows instead. + Example: ```sql