Repository: duckdb/duckdb (language: python)

Issue:
group by a timestamp column in a parquet file can cause the process to crash
### What happens?

Group by a timestamp column in a parquet file can cause the process to crash.
[demo.parquet.zip](https://github.com/user-attachments/files/17427101/demo.zip)


### To Reproduce

```sql
SELECT a FROM 'demo.parquet' GROUP BY a;
```

### OS:

windows 11, x86_64

### DuckDB Version:

1.1.2

### DuckDB Client:

CLI

### Hardware:

_No response_

### Full Name:

Jason Jia

### Affiliation:

personal

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

### Did you include all relevant data sets for reproducing the issue?

Yes

### 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/src/execution/perfect_aggregate_hashtable.cpp b/src/execution/perfect_aggregate_hashtable.cpp
index 4fa6f08f47b8..c378e61edafb 100644
--- a/src/execution/perfect_aggregate_hashtable.cpp
+++ b/src/execution/perfect_aggregate_hashtable.cpp
@@ -130,7 +130,12 @@ void PerfectAggregateHashTable::AddChunk(DataChunk &groups, DataChunk &payload)
 	// compute the actual pointer to the data by adding it to the base HT pointer and multiplying by the tuple size
 	for (idx_t i = 0; i < groups.size(); i++) {
 		const auto group = address_data[i];
-		D_ASSERT(group < total_groups);
+		if (group >= total_groups) {
+			throw InvalidInputException("Perfect hash aggregate: aggregate group %llu exceeded total groups %llu. This "
+			                            "likely means that the statistics in your data source are corrupt.\n* PRAGMA "
+			                            "disable_optimizer to disable optimizations that rely on correct statistics",
+			                            group, total_groups);
+		}
 		group_is_set[group] = true;
 		address_data[i] = uintptr_t(data) + group * tuple_size;
 	}


Test patch:
diff --git a/data/parquet-testing/corrupt_stats.parquet b/data/parquet-testing/corrupt_stats.parquet
new file mode 100644
index 000000000000..26f4bb0cac3d
Binary files /dev/null and b/data/parquet-testing/corrupt_stats.parquet differ
diff --git a/test/sql/copy/parquet/corrupt_stats.test b/test/sql/copy/parquet/corrupt_stats.test
new file mode 100644
index 000000000000..146c27f9d2e4
--- /dev/null
+++ b/test/sql/copy/parquet/corrupt_stats.test
@@ -0,0 +1,18 @@
+# name: test/sql/copy/parquet/corrupt_stats.test
+# description: Issue #14430: group by a timestamp column in a parquet file can cause the process to crash
+# group: [parquet]
+
+require parquet
+
+statement error
+SELECT a FROM 'data/parquet-testing/corrupt_stats.parquet' GROUP BY a;
+----
+This likely means that the statistics in your data source are corrupt
+
+statement ok
+PRAGMA disable_optimizer
+
+query I
+SELECT a FROM 'data/parquet-testing/corrupt_stats.parquet' GROUP BY a;
+----
+2021-01-01 12:00:00