Repository: duckdb/duckdb (language: python)

Issue:
Strange macro results
### What happens?

I have this macro to calculate year quarter:

```sql
create or replace macro yearqtr(x) as 
(SELECT (((date_part('year', CAST(x AS DATE)) * 10) + ((date_part('month', CAST(x AS DATE)) - 1) // 3)) + 1));
```

Now, when i try to use this i get some very weird results for a particular date. Here's the query in question:

```sql
with df as (select unnest(generate_series('2000-01-01'::date,'2099-12-31'::date,interval 1 day)::date[]) as dt) 
select dt,yearqtr(dt) as yq,yearqtr('2025-12-31') as yq1,yearqtr(dt)//10 as yq2,yearqtr(dt)//3 as yq3,yearqtr(dt)%3 as yq4 from df where dt='2050-12-31';
```

What is evey weirder is that if you run this query a few times i get different values for `yq`, `yq2`, `yq3`! I suspect that this has got something to do with the `//` operator.

I'm unsure if this strangeness is only for the date '2050-12-31' or i was unlucky enough to stumble upon this edge case.

### To Reproduce

```sql
create or replace macro yearqtr(x) as 
(SELECT (((date_part('year', CAST(x AS DATE)) * 10) + ((date_part('month', CAST(x AS DATE)) - 1) // 3)) + 1));
```

Now run the following query a few times:

```sql
with df as (select unnest(generate_series('2000-01-01'::date,'2099-12-31'::date,interval 1 day)::date[]) as dt) 
select dt,yearqtr(dt) as yq,yearqtr('2025-12-31') as yq1,yearqtr(dt)//10 as yq2,yearqtr(dt)//3 as yq3,yearqtr(dt)%3 as yq4 from df where dt='2050-12-31';
```

### OS:

macos arm64 (23.4.0 Darwin Kernel Version 23.4.0)

### DuckDB Version:

v1.1.3 19864453f7

### DuckDB Client:

CLI

### Hardware:

_No response_

### Full Name:

Vijay Lulla

### Affiliation:

Sweetwater

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

I have not tested with any 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:
Thanks for the report! 

This is a fencepost error checking the date part cache limits. The end of the cache is `2050-12-31`, which should be excluded from the test but is included. In a release build, this will produce random garbage, but in debug it crashes with an address sanitiser error.

Fix patch:
diff --git a/src/include/duckdb/common/types/date_lookup_cache.hpp b/src/include/duckdb/common/types/date_lookup_cache.hpp
index 044493ef1089..b276bbe32d1c 100644
--- a/src/include/duckdb/common/types/date_lookup_cache.hpp
+++ b/src/include/duckdb/common/types/date_lookup_cache.hpp
@@ -30,7 +30,7 @@ class DateLookupCache {
 
 	//! Extracts the component, or sets the validity mask to NULL if the date is infinite
 	int64_t ExtractElement(date_t date, ValidityMask &mask, idx_t idx) const {
-		if (DUCKDB_UNLIKELY(date.days < CACHE_MIN_DATE || date.days > CACHE_MAX_DATE)) {
+		if (DUCKDB_UNLIKELY(date.days < CACHE_MIN_DATE || date.days >= CACHE_MAX_DATE)) {
 			if (DUCKDB_UNLIKELY(!Value::IsFinite(date))) {
 				mask.SetInvalid(idx);
 				return 0;


Test patch:
diff --git a/test/sql/function/date/test_date_part.test b/test/sql/function/date/test_date_part.test
index d02538c86a44..7a3607ab1fc0 100644
--- a/test/sql/function/date/test_date_part.test
+++ b/test/sql/function/date/test_date_part.test
@@ -264,6 +264,14 @@ NULL
 NULL
 NULL
 
+# Cache upper bound
+query I
+select date_part('year', dt::DATE) * 10,
+from generate_series('2050-01-01'::date,'2051-12-31'::date,interval 1 day) t(dt)
+where dt = '2050-12-31';
+----
+20500
+
 #
 # Two-argument timezone
 #