Skip to content
Merged
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
58 changes: 53 additions & 5 deletions datafusion-examples/examples/relation_planner/pivot_unpivot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ async fn run_examples(ctx: &SessionContext) -> Result<()> {
+--------+
");

// Example 7: PIVOT on a quoted mixed-case column
// Reuses the parsed column expression so quoted identifiers keep their case.
let results = run_example(
ctx,
"Example 7: PIVOT with quoted mixed-case column",
r#"SELECT * FROM point_stats
PIVOT (MAX(max_value) FOR "pointNumber" IN ('16951' AS p16951, '16952' AS p16952)) AS p
ORDER BY ts"#,
)
.await?;
assert_snapshot!(results, @r"
+----------------------+------+--------+--------+
| ts | port | p16951 | p16952 |
+----------------------+------+--------+--------+
| 2024-09-01T10:00:00Z | 2411 | 10 | 20 |
| 2024-09-01T10:01:00Z | 2411 | 30 | 40 |
+----------------------+------+--------+--------+
");

Ok(())
}

Expand Down Expand Up @@ -288,6 +307,34 @@ fn register_sample_data(ctx: &SessionContext) -> Result<()> {
])?,
)?;

// point_stats: grouped data with a quoted mixed-case pivot column.
ctx.register_batch(
"point_stats",
RecordBatch::try_from_iter(vec![
(
"ts",
Arc::new(StringArray::from(vec![
"2024-09-01T10:00:00Z",
"2024-09-01T10:00:00Z",
"2024-09-01T10:01:00Z",
"2024-09-01T10:01:00Z",
])) as ArrayRef,
),
(
"pointNumber",
Arc::new(StringArray::from(vec!["16951", "16952", "16951", "16952"])),
),
(
"port",
Arc::new(StringArray::from(vec!["2411", "2411", "2411", "2411"])),
),
(
"max_value",
Arc::new(Int64Array::from(vec![10, 20, 30, 40])),
),
])?,
)?;

Ok(())
}

Expand Down Expand Up @@ -415,11 +462,12 @@ fn plan_pivot(
.collect();

let group_by_cols: Vec<Expr> = schema
.fields()
.iter()
.map(|f| f.name().as_str())
.filter(|name| *name != pivot_col_name.as_str() && !agg_input_cols.contains(name))
.map(col)
.filter(|(_, field)| {
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.

is this change necessary? It seems like it is more verbose but does the same thing

let name = field.name();
name != pivot_col_name.as_str() && !agg_input_cols.contains(&name.as_str())
})
.map(Expr::from)
.collect();

// Build CASE expressions for each (aggregate, pivot_value) pair
Expand All @@ -434,7 +482,7 @@ fn plan_pivot(

for (value_alias, pivot_value) in &pivot_values {
// CASE pivot_col WHEN pivot_value THEN agg_input END
let case_expr = case(col(&pivot_col_name))
let case_expr = case(pivot_col.clone())
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.

this is the real fix, right?

.when(pivot_value.clone(), agg_input.clone())
.end()?;

Expand Down
Loading