Skip to content

Commit

Permalink
Merge pull request #9813 from Mytherin/issue8596
Browse files Browse the repository at this point in the history
Fix #8596 - use ConstructConstantFromExpression for PIVOT IN list
  • Loading branch information
Mytherin committed Nov 27, 2023
2 parents 08f07ab + b33de79 commit 664e13d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/include/duckdb/parser/transformer.hpp
Expand Up @@ -37,6 +37,7 @@ class OnConflictInfo;
class UpdateSetInfo;
struct ParserOptions;
struct PivotColumn;
struct PivotColumnEntry;

//! The transformer class is responsible for transforming the internal Postgres
//! parser representation into the DuckDB representation
Expand Down Expand Up @@ -176,6 +177,8 @@ class Transformer {
unique_ptr<SQLStatement> CreatePivotStatement(unique_ptr<SQLStatement> statement);
PivotColumn TransformPivotColumn(duckdb_libpgquery::PGPivot &pivot);
vector<PivotColumn> TransformPivotList(duckdb_libpgquery::PGList &list);
static void TransformPivotInList(unique_ptr<ParsedExpression> &expr, PivotColumnEntry &entry,
bool root_entry = true);

//===--------------------------------------------------------------------===//
// SetStatement Transform
Expand Down
11 changes: 6 additions & 5 deletions src/parser/transform/tableref/transform_pivot.cpp
Expand Up @@ -7,16 +7,13 @@

namespace duckdb {

static void TransformPivotInList(unique_ptr<ParsedExpression> &expr, PivotColumnEntry &entry, bool root_entry = true) {
void Transformer::TransformPivotInList(unique_ptr<ParsedExpression> &expr, PivotColumnEntry &entry, bool root_entry) {
if (expr->type == ExpressionType::COLUMN_REF) {
auto &colref = expr->Cast<ColumnRefExpression>();
if (colref.IsQualified()) {
throw ParserException("PIVOT IN list cannot contain qualified column references");
}
entry.values.emplace_back(colref.GetColumnName());
} else if (expr->type == ExpressionType::VALUE_CONSTANT) {
auto &constant_expr = expr->Cast<ConstantExpression>();
entry.values.push_back(std::move(constant_expr.value));
} else if (root_entry && expr->type == ExpressionType::FUNCTION) {
auto &function = expr->Cast<FunctionExpression>();
if (function.function_name != "row") {
Expand All @@ -28,7 +25,11 @@ static void TransformPivotInList(unique_ptr<ParsedExpression> &expr, PivotColumn
} else if (root_entry && expr->type == ExpressionType::STAR) {
entry.star_expr = std::move(expr);
} else {
throw ParserException("PIVOT IN list must contain columns or lists of columns");
Value val;
if (!Transformer::ConstructConstantFromExpression(*expr, val)) {
throw ParserException("PIVOT IN list must contain columns or lists of columns");
}
entry.values.push_back(std::move(val));
}
}

Expand Down
50 changes: 50 additions & 0 deletions test/sql/pivot/pivot_in_boolean.test
@@ -0,0 +1,50 @@
# name: test/sql/pivot/pivot_in_boolean.test
# description: Issue #8596 - Pivot with IN clause doesn't work for a boolean column
# group: [pivot]

statement ok
PRAGMA enable_verification

statement ok
CREATE TABLE Cities(Country VARCHAR, Name VARCHAR, Year INT, Population INT);

statement ok
INSERT INTO Cities VALUES ('NL', 'Amsterdam', 2000, 1005);

statement ok
INSERT INTO Cities VALUES ('NL', 'Amsterdam', 2010, 1065);

statement ok
INSERT INTO Cities VALUES ('NL', 'Amsterdam', 2020, 1158);

statement ok
INSERT INTO Cities VALUES ('US', 'Seattle', 2000, 564);

statement ok
INSERT INTO Cities VALUES ('US', 'Seattle', 2010, 608);

statement ok
INSERT INTO Cities VALUES ('US', 'Seattle', 2020, 738);

statement ok
INSERT INTO Cities VALUES ('US', 'New York City', 2000, 8015);

statement ok
INSERT INTO Cities VALUES ('US', 'New York City', 2010, 8175);

statement ok
INSERT INTO Cities VALUES ('US', 'New York City', 2020, 8772);

query III rowsort
pivot cities on (Country='NL') using avg(Population) group by name;
----
Amsterdam NULL 1076.0
New York City 8320.666666666666 NULL
Seattle 636.6666666666666 NULL

query III rowsort
pivot cities on (Country='NL') in (false, true) using avg(Population) group by name;
----
Amsterdam NULL 1076.0
New York City 8320.666666666666 NULL
Seattle 636.6666666666666 NULL

0 comments on commit 664e13d

Please sign in to comment.