Skip to content
Open
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
21 changes: 17 additions & 4 deletions datafusion/sql/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ use std::sync::Arc;
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};

use crate::stack::StackGuard;
use datafusion_common::{Constraints, DFSchema, Result, not_impl_err};
use datafusion_common::{Constraints, DFSchema, Result, not_impl_err, plan_err};
use datafusion_expr::expr::{Sort, WildcardOptions};

use datafusion_expr::select_expr::SelectExpr;
use datafusion_expr::{
CreateMemoryTable, DdlStatement, Distinct, Expr, LogicalPlan, LogicalPlanBuilder,
};
use sqlparser::ast::{
Expr as SQLExpr, ExprWithAliasAndOrderBy, Ident, LimitClause, Offset, OffsetRows,
OrderBy, OrderByExpr, OrderByKind, PipeOperator, Query, SelectInto, SetExpr,
SetOperator, SetQuantifier, TableAlias,
ExceptSelectItem, Expr as SQLExpr, ExprWithAliasAndOrderBy, Ident, LimitClause,
Offset, OffsetRows, OrderBy, OrderByExpr, OrderByKind, PipeOperator, Query,
SelectInto, SetExpr, SetOperator, SetQuantifier, TableAlias,
};
use sqlparser::tokenizer::Span;

Expand Down Expand Up @@ -215,6 +215,19 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
PipeOperator::Join(join) => {
self.parse_relation_join(plan, join, planner_context)
}
PipeOperator::Drop { columns } => {
let Some((first, rest)) = columns.split_first() else {
return plan_err!("DROP requires at least one column");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would be good to have a test case covering this

Copy link
Copy Markdown
Author

@jayendra13 jayendra13 Apr 22, 2026

Choose a reason for hiding this comment

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

I think the https://github.com/google/googlesql/blob/master/docs/pipe-syntax.md#drop-pipe-operator makes column_name required (|> DROP column_name [, ...]), which would make this a parse error rather than a planner error, and it feels like it belongs in sqlparser instead. Could you confirm I'm reading the spec right? If so, I'll need to fix it in the upstream crate first.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not familiar with the spec, just basing it off the code I see here (if we need to ensure columns aren't empty here then it means its possible to be empty? Or its just safety?)

};
let options = WildcardOptions {
except: Some(ExceptSelectItem {
first_element: first.clone(),
additional_elements: rest.to_vec(),
}),
..WildcardOptions::default()
};
self.project(plan, vec![SelectExpr::Wildcard(options)])
}

x => not_impl_err!("`{x}` pipe operator is not supported yet"),
}
Expand Down
26 changes: 26 additions & 0 deletions datafusion/sqllogictest/test_files/pipe_operator.slt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ query TII
apples 2 123
bananas 5 NULL

# DROP pipe - drop two columns
query RT
SELECT * FROM test |> DROP a, n
----
1.1 a
2.2 b
3.3 c

# DROP pipe - drop single column
query IRT
SELECT * FROM test |> DROP n
----
1 1.1 a
2 2.2 b
3 3.3 c

# DROP pipe - chained with other operators
query R
SELECT * FROM test |> WHERE a > 1 |> DROP a, c, n
----
2.2
3.3

# Error: DROP non-existent column
statement error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we validate the specific error that comes here too?

SELECT * FROM test |> DROP nonexistent
# Config reset
statement ok
RESET datafusion.sql_parser.dialect;
17 changes: 17 additions & 0 deletions docs/source/user-guide/sql/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ DataFusion currently supports the following pipe operators:
- [EXCEPT](#pipe_except)
- [AGGREGATE](#pipe_aggregate)
- [JOIN](#pipe_join)
- [DROP](#pipe_drop)

(pipe_where)=

Expand Down Expand Up @@ -670,3 +671,19 @@ select * from range(0,3)
| bananas| 5 | NULL |
+--------+-------+------+
```

(pipe_drop)=

### DROP

Removes columns from the output, equivalent to `SELECT * EXCEPT(columns)`.

```sql
select 1 as a, 2 as b, 3 as c
|> drop b;
+---+---+
| a | c |
+---+---+
| 1 | 3 |
+---+---+
```