Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions datafusion/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let plan = plan?;

// The SELECT expressions, with wildcards expanded.
let select_exprs = self.prepare_select_exprs(&plan, &select.projection)?;
let select_exprs = self.prepare_select_exprs(&plan, select)?;

// having and group by clause may reference aliases defined in select projection
let projected_plan = self.project(plan.clone(), select_exprs.clone())?;
Expand Down Expand Up @@ -899,18 +899,26 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
fn prepare_select_exprs(
&self,
plan: &LogicalPlan,
projection: &[SelectItem],
select: &Select,
) -> Result<Vec<Expr>> {
let input_schema = plan.schema();

let projection = &select.projection;
projection
.iter()
.map(|expr| self.sql_select_to_rex(expr, input_schema))
.collect::<Result<Vec<Expr>>>()?
.into_iter()
.map(|expr| {
Ok(match expr {
Expr::Wildcard => expand_wildcard(input_schema, plan)?,
Expr::Wildcard => {
if select.from.is_empty() {
return Err(DataFusionError::Plan(
"SELECT * with no tables specified is not valid"
.to_string(),
));
}
expand_wildcard(input_schema, plan)?
}
_ => vec![normalize_col(expr, plan)?],
})
})
Expand Down
17 changes: 17 additions & 0 deletions datafusion/tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6422,3 +6422,20 @@ async fn test_specific_nulls_first_asc() -> Result<()> {
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn test_select_wildcard_without_table() -> Result<()> {
let mut ctx = ExecutionContext::new();
let sql = "SELECT * ";
let actual = ctx.sql(sql).await;
match actual {
Ok(_) => panic!("expect err"),
Err(e) => {
assert_contains!(
e.to_string(),
"Error during planning: SELECT * with no tables specified is not valid"
);
}
}
Ok(())
}