Repository: duckdb/duckdb (language: python)

Issue:
`histogram` table function throws conversion error
### What happens?

The `histogram` table function throws a conversion error on a decimal column.

### To Reproduce

```sql
WITH cte AS (FROM (VALUES (0.0), (9.9)) df(l_orderkey)) SELECT * FROM histogram(cte, l_orderkey)
```
```
Conversion Error: Could not cast value 100.000000 to DECIMAL(2,1)
```

### OS:

Linux

### DuckDB Version:

1.1.4

### DuckDB Client:

Python

### Hardware:

i5, AMD64

### Full Name:

Soeren Wolfers

### Affiliation:

G-Research

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a nightly build

### Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

### Did you include all code required to reproduce the issue?

- [X] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

- [X] Yes, I have


Discussion / hints:

Fix patch:
diff --git a/extension/core_functions/aggregate/nested/binned_histogram.cpp b/extension/core_functions/aggregate/nested/binned_histogram.cpp
index eda2a6530ca9..fc184b8bba92 100644
--- a/extension/core_functions/aggregate/nested/binned_histogram.cpp
+++ b/extension/core_functions/aggregate/nested/binned_histogram.cpp
@@ -342,6 +342,9 @@ static AggregateFunction GetHistogramBinFunction(const LogicalType &type) {
 
 template <class HIST>
 AggregateFunction GetHistogramBinFunction(const LogicalType &type) {
+	if (type.id() == LogicalTypeId::DECIMAL) {
+		return GetHistogramBinFunction<HIST>(LogicalType::DOUBLE);
+	}
 	switch (type.InternalType()) {
 #ifndef DUCKDB_SMALLER_BINARY
 	case PhysicalType::BOOL:
diff --git a/src/common/operator/cast_operators.cpp b/src/common/operator/cast_operators.cpp
index 5e1dc4423ac9..fe1ce26afaed 100644
--- a/src/common/operator/cast_operators.cpp
+++ b/src/common/operator/cast_operators.cpp
@@ -2298,7 +2298,7 @@ bool DoubleToDecimalCast(SRC input, DST &result, CastParameters &parameters, uin
 	double roundedValue = round(value);
 	if (roundedValue <= -NumericHelper::DOUBLE_POWERS_OF_TEN[width] ||
 	    roundedValue >= NumericHelper::DOUBLE_POWERS_OF_TEN[width]) {
-		string error = StringUtil::Format("Could not cast value %f to DECIMAL(%d,%d)", value, width, scale);
+		string error = StringUtil::Format("Could not cast value %f to DECIMAL(%d,%d)", input, width, scale);
 		HandleCastError::AssignError(error, parameters);
 		return false;
 	}


Test patch:
diff --git a/test/sql/aggregate/aggregates/histogram_tpch.test_slow b/test/sql/aggregate/aggregates/histogram_tpch.test_slow
index 378716388a4a..0fdc234cba1b 100644
--- a/test/sql/aggregate/aggregates/histogram_tpch.test_slow
+++ b/test/sql/aggregate/aggregates/histogram_tpch.test_slow
@@ -55,17 +55,17 @@ ORDER BY l_returnflag
 query II
 SELECT bin, count FROM histogram(lineitem, l_extendedprice)
 ----
-x <= 10000.00	773545
-10000.00 < x <= 20000.00	831988
-20000.00 < x <= 30000.00	833410
-30000.00 < x <= 40000.00	835155
-40000.00 < x <= 50000.00	829356
-50000.00 < x <= 60000.00	724657
-60000.00 < x <= 70000.00	528755
-70000.00 < x <= 80000.00	358127
-80000.00 < x <= 90000.00	207206
-90000.00 < x <= 100000.00	74894
-100000.00 < x <= 110000.00	4122
+x <= 10000.0	773545
+10000.0 < x <= 20000.0	831988
+20000.0 < x <= 30000.0	833410
+30000.0 < x <= 40000.0	835155
+40000.0 < x <= 50000.0	829356
+50000.0 < x <= 60000.0	724657
+60000.0 < x <= 70000.0	528755
+70000.0 < x <= 80000.0	358127
+80000.0 < x <= 90000.0	207206
+90000.0 < x <= 100000.0	74894
+100000.0 < x <= 110000.0	4122
 
 # dates
 query II
diff --git a/test/sql/function/list/aggregates/histogram_decimal.test b/test/sql/function/list/aggregates/histogram_decimal.test
new file mode 100644
index 000000000000..bba3a96f772c
--- /dev/null
+++ b/test/sql/function/list/aggregates/histogram_decimal.test
@@ -0,0 +1,20 @@
+# name: test/sql/function/list/aggregates/histogram_decimal.test
+# description: Test histogram for decimals
+# group: [aggregates]
+
+statement ok
+PRAGMA enable_verification
+
+query II
+WITH cte AS (FROM (VALUES (0.0), (9.9)) df(l_orderkey)) SELECT * FROM histogram_values(cte, l_orderkey)
+----
+1.0	1
+2.0	0
+3.0	0
+4.0	0
+5.0	0
+6.0	0
+7.0	0
+8.0	0
+9.0	0
+10.0	1