Repository: duckdb/duckdb (language: python)

Issue:
duckdb run the tpcds query no.85 failed, and no.64 out of memory
#### What happens?
duckdb run one of the tpcds query failed

```
--64
Error: near line 1: Out of Memory Error: could not allocate block of 262144 bytes
Database is launched in in-memory mode and no temporary directory is specified.
Unused blocks cannot be offloaded to disk.

Launch the database with a persistent storage back-end

--85
Run Time: real 1147.346 user 1146.872552 sys 0.015600
Error: near line 2: INTERRUPT Error: Interrupted!
```

#### To Reproduce



I copied the sql from 

https://codeload.github.com/cwida/tpcds-result-reproduction/zip/refs/heads/master

extract to d:/tpcds/app

and copied the data from

https://github.com/Altinity/tpc-ds/tree/master/data

extract to d:/tpcds/data

the file customer_001.dat need to be converted to utf8

I wrote the import command in d:/import_tpcds.txt

then run .read d:/tpcds_sql_bat.txt

It stoped after the above output.

		

#### Environment (please complete the following information):
 - OS: [e.g. iOS]windows7 x64
 - DuckDB Version: [e.g. 22]0.30
 - DuckDB Client: [e.g. Python]CLI

#### Before Submitting

- [ ] **Have you tried this on the latest `master` branch?**
* **Python**: `pip install duckdb --upgrade --pre`
* **R**: `install.packages("https://github.com/duckdb/duckdb/releases/download/master-builds/duckdb_r_src.tar.gz", repos = NULL)`
* **Other Platforms**: You can find binaries [here](https://github.com/duckdb/duckdb/releases/tag/master-builds) or compile from source.

- [ ] **Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?**



Discussion / hints:
[import_tpcds.txt](https://github.com/duckdb/duckdb/files/7844040/import_tpcds.txt)
[tpcds_sql_bat.txt](https://github.com/duckdb/duckdb/files/7844042/tpcds_sql_bat.txt)

[64.sql.txt](https://github.com/duckdb/duckdb/files/7844538/64.sql.txt)
[85.sql.txt](https://github.com/duckdb/duckdb/files/7844537/85.sql.txt)

This has been fixed
@Mytherin Do you have a PR reference for the fix? I just saw something similar using `0.5.1`, and am wondering whether I should file a bug report or if this covers it.
The problem was a suboptimal join order leading to an exploding join - the fix was improving the join order (see https://github.com/duckdb/duckdb/pull/4274).

Fix patch:
diff --git a/extension/icu/icu-datefunc.cpp b/extension/icu/icu-datefunc.cpp
index aeab8379efbd..fd2f269c1003 100644
--- a/extension/icu/icu-datefunc.cpp
+++ b/extension/icu/icu-datefunc.cpp
@@ -73,6 +73,9 @@ unique_ptr<FunctionData> ICUDateFunc::Bind(ClientContext &context, ScalarFunctio
 
 void ICUDateFunc::SetTimeZone(icu::Calendar *calendar, const string_t &tz_id) {
 	auto tz = icu_66::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(icu::StringPiece(tz_id.GetString())));
+	if (*tz == icu::TimeZone::getUnknown()) {
+		throw NotImplementedException("Unknown TimeZone '%s'", tz_id.GetString());
+	}
 	calendar->adoptTimeZone(tz);
 }
 
@@ -83,7 +86,7 @@ timestamp_t ICUDateFunc::GetTimeUnsafe(icu::Calendar *calendar, uint64_t micros)
 	if (U_FAILURE(status)) {
 		throw InternalException("Unable to get ICU calendar time.");
 	}
-	return timestamp_t(millis * Interval::MICROS_PER_MSEC + micros);
+	return timestamp_t(millis * Interval::MICROS_PER_MSEC + int64_t(micros));
 }
 
 bool ICUDateFunc::TryGetTime(icu::Calendar *calendar, uint64_t micros, timestamp_t &result) {
@@ -98,7 +101,7 @@ bool ICUDateFunc::TryGetTime(icu::Calendar *calendar, uint64_t micros, timestamp
 	if (!TryMultiplyOperator::Operation<int64_t, int64_t, int64_t>(millis, Interval::MICROS_PER_MSEC, millis)) {
 		return false;
 	}
-	if (!TryAddOperator::Operation<int64_t, int64_t, int64_t>(millis, micros, millis)) {
+	if (!TryAddOperator::Operation<int64_t, int64_t, int64_t>(millis, int64_t(micros), millis)) {
 		return false;
 	}
 
diff --git a/extension/icu/icu-timezone.cpp b/extension/icu/icu-timezone.cpp
index bd0f9d213417..0a91ef759144 100644
--- a/extension/icu/icu-timezone.cpp
+++ b/extension/icu/icu-timezone.cpp
@@ -125,7 +125,7 @@ struct ICUFromNaiveTimestamp : public ICUDateFunc {
 		int32_t secs;
 		int32_t frac;
 		Time::Convert(local_time, hr, mn, secs, frac);
-		int32_t millis = frac / Interval::MICROS_PER_MSEC;
+		int32_t millis = frac / int32_t(Interval::MICROS_PER_MSEC);
 		uint64_t micros = frac % Interval::MICROS_PER_MSEC;
 
 		// Use them to set the time in the time zone
@@ -199,7 +199,7 @@ struct ICUToNaiveTimestamp : public ICUDateFunc {
 		}
 
 		// Extract the time zone parts
-		auto micros = SetTime(calendar, instant);
+		auto micros = int32_t(SetTime(calendar, instant));
 		const auto era = ExtractField(calendar, UCAL_ERA);
 		const auto year = ExtractField(calendar, UCAL_YEAR);
 		const auto mm = ExtractField(calendar, UCAL_MONTH) + 1;
@@ -216,7 +216,7 @@ struct ICUToNaiveTimestamp : public ICUDateFunc {
 		const auto secs = ExtractField(calendar, UCAL_SECOND);
 		const auto millis = ExtractField(calendar, UCAL_MILLISECOND);
 
-		micros += millis * Interval::MICROS_PER_MSEC;
+		micros += millis * int32_t(Interval::MICROS_PER_MSEC);
 		dtime_t local_time = Time::FromTime(hr, mn, secs, micros);
 
 		timestamp_t naive;
diff --git a/extension/icu/icu_extension.cpp b/extension/icu/icu_extension.cpp
index 707bd35a3b18..e5c038805eb0 100644
--- a/extension/icu/icu_extension.cpp
+++ b/extension/icu/icu_extension.cpp
@@ -42,7 +42,7 @@ struct IcuBindData : public FunctionData {
 	string country;
 	string tag;
 
-	IcuBindData(duckdb::unique_ptr<icu::Collator> collator_p) : collator(std::move(collator_p)) {
+	explicit IcuBindData(duckdb::unique_ptr<icu::Collator> collator_p) : collator(std::move(collator_p)) {
 	}
 
 	IcuBindData(string language_p, string country_p) : language(std::move(language_p)), country(std::move(country_p)) {
@@ -59,7 +59,7 @@ struct IcuBindData : public FunctionData {
 		}
 	}
 
-	IcuBindData(string tag_p) : tag(std::move(tag_p)) {
+	explicit IcuBindData(string tag_p) : tag(std::move(tag_p)) {
 		UErrorCode status = U_ZERO_ERROR;
 		UCollator *ucollator = ucol_open(tag.c_str(), &status);
 		if (U_FAILURE(status)) {
@@ -120,7 +120,7 @@ const string IcuBindData::FUNCTION_PREFIX = "icu_collate_";
 static int32_t ICUGetSortKey(icu::Collator &collator, string_t input, duckdb::unique_ptr<char[]> &buffer,
                              int32_t &buffer_size) {
 	icu::UnicodeString unicode_string =
-	    icu::UnicodeString::fromUTF8(icu::StringPiece(input.GetData(), input.GetSize()));
+	    icu::UnicodeString::fromUTF8(icu::StringPiece(input.GetData(), int32_t(input.GetSize())));
 	int32_t string_size = collator.getSortKey(unicode_string, reinterpret_cast<uint8_t *>(buffer.get()), buffer_size);
 	if (string_size > buffer_size) {
 		// have to resize the buffer
@@ -204,18 +204,19 @@ static ScalarFunction GetICUCollateFunction(const string &collation, const strin
 	string fname = IcuBindData::EncodeFunctionName(collation);
 	ScalarFunction result(fname, {LogicalType::VARCHAR}, LogicalType::VARCHAR, ICUCollateFunction, ICUCollateBind);
 	//! collation tag is added into the Function extra info
-	result.extra_info = std::move(tag);
+	result.extra_info = tag;
 	result.serialize = IcuBindData::Serialize;
 	result.deserialize = IcuBindData::Deserialize;
 	return result;
 }
 
 static void SetICUTimeZone(ClientContext &context, SetScope scope, Value &parameter) {
-	icu::StringPiece utf8(StringValue::Get(parameter));
+	auto str = StringValue::Get(parameter);
+	icu::StringPiece utf8(str);
 	const auto uid = icu::UnicodeString::fromUTF8(utf8);
 	duckdb::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createTimeZone(uid));
 	if (*tz == icu::TimeZone::getUnknown()) {
-		throw NotImplementedException("Unknown TimeZone setting");
+		throw NotImplementedException("Unknown TimeZone '%s'", str);
 	}
 }
 


Test patch:
diff --git a/test/sql/function/timestamp/test_icu_makedate.test b/test/sql/function/timestamp/test_icu_makedate.test
index 3d712cbc6274..dde6de64dd75 100644
--- a/test/sql/function/timestamp/test_icu_makedate.test
+++ b/test/sql/function/timestamp/test_icu_makedate.test
@@ -332,44 +332,12 @@ Canada/Newfoundland	2021-05-02 05:11:49.5-07	2021-05-02 00:41:49.5-07
 US/Hawaii	2021-12-01 05:54:48.123456-08	2021-12-01 07:54:48.123456-08
 NULL	NULL	NULL
 
-# Invalid zone - ICU defaults to GMT in this situation
-query II
+# Invalid zone - 
+statement error
 SELECT ts, make_timestamptz(yyyy, mm, dd, hr, mn, ss, 'Europe/Duck') mts
 FROM timeparts;
 ----
-1001-03-15 (BC) 12:45:42-07:52	1001-03-15 (BC) 04:52:44-07:52
-0044-03-15 (BC) 12:45:42-07:52	0044-03-15 (BC) 04:52:44-07:52
-1962-07-31 05:20:48.123456-07	1962-07-30 22:20:48.123456-07
-1968-12-31 17:03:20.45432-08	1968-12-31 09:03:20.45432-08
-1991-12-31 17:01:01.4-08	1991-12-31 09:01:01.4-08
-1991-12-31 17:01:02.2-08	1991-12-31 09:01:02.2-08
-1991-12-31 17:01:02.4-08	1991-12-31 09:01:02.4-08
-1993-08-14 01:22:33-07	1993-08-13 18:22:33-07
-1993-08-14 01:22:33.42-07	1993-08-13 18:22:33.42-07
-2001-04-20 07:42:11-07	2001-04-20 00:42:11-07
-2001-04-20 07:42:11.123-07	2001-04-20 00:42:11.123-07
-2004-01-31 04:00:00.00005-08	2004-01-30 20:00:00.00005-08
-2004-01-31 04:00:00.05-08	2004-01-30 20:00:00.05-08
-2004-02-01 04:00:00.00005-08	2004-01-31 20:00:00.00005-08
-2004-02-01 04:00:00.05-08	2004-01-31 20:00:00.05-08
-2004-02-29 05:05:47.123456-08	2004-02-28 21:05:47.123456-08
-2007-12-31 16:00:01.5-08	2007-12-31 08:00:01.5-08
-2007-12-31 16:00:01.594-08	2007-12-31 08:00:01.594-08
-2007-12-31 16:00:01.794-08	2007-12-31 08:00:01.794-08
-2007-12-31 16:00:01.88926-08	2007-12-31 08:00:01.88926-08
-2007-12-31 16:00:01.894-08	2007-12-31 08:00:01.894-08
-2007-12-31 16:00:01.98926-08	2007-12-31 08:00:01.98926-08
-2007-12-31 16:00:01.99926-08	2007-12-31 08:00:01.99926-08
-2007-12-31 16:00:11.1-08	2007-12-31 08:00:11.1-08
-2019-01-05 20:03:02.123456-08	2019-01-05 12:03:02.123456-08
-2019-01-05 20:03:02.5-08	2019-01-05 12:03:02.5-08
-2019-12-31 16:00:01.88926-08	2019-12-31 08:00:01.88926-08
-2020-12-31 13:25:58.745232-08	2020-12-31 05:25:58.745232-08
-2021-04-15 07:55:17.915-07	2021-04-15 00:55:17.915-07
-2021-04-15 07:55:17.915-07	2021-04-15 00:55:17.915-07
-2021-05-02 05:11:49.5-07	2021-05-01 22:11:49.5-07
-2021-12-01 05:54:48.123456-08	2021-11-30 21:54:48.123456-08
-NULL	NULL
+Unknown TimeZone 'Europe/Duck'
 
 # Cast to DATE
 query II
@@ -417,10 +385,12 @@ WITH all_types AS (
 )
 SELECT make_timestamptz(
 	CAST(century(CAST(a."interval" AS INTERVAL)) AS BIGINT), 
-	CAST(a."bigint" AS BIGINT), CAST(a."bigint" AS BIGINT), 
-	CAST(a."bigint" AS BIGINT), CAST(a."bigint" AS BIGINT), 
+	CAST(a."bigint" AS BIGINT), 
+	CAST(a."bigint" AS BIGINT), 
+	CAST(a."bigint" AS BIGINT), 
+	CAST(a."bigint" AS BIGINT), 
 	CAST(txid_current() AS BIGINT), 
-	CAST(CAST(COALESCE(a."bigint", a."bigint") AS BIGINT) AS VARCHAR))
+	'UTC')
 FROM all_types a;
 ----
 Overflow in subtraction
diff --git a/tools/pythonpkg/tests/fast/pandas/test_datetime_timestamp.py b/tools/pythonpkg/tests/fast/pandas/test_datetime_timestamp.py
index 57ccf565f56c..e3b265016c0a 100644
--- a/tools/pythonpkg/tests/fast/pandas/test_datetime_timestamp.py
+++ b/tools/pythonpkg/tests/fast/pandas/test_datetime_timestamp.py
@@ -105,18 +105,17 @@ def test_timestamp_timezone_negative_extreme(self, pandas, duckdb_cursor):
     def test_timestamp_timezone_positive_extreme(self, pandas, duckdb_cursor):
         duckdb_time = duckdb_cursor.sql(
             """
-            SELECT timestamp '2021-12-31 23:00:00' AT TIME ZONE 'kea_CV' as "0"
+            SELECT timestamp '2021-12-31 23:00:00' AT TIME ZONE 'Etc/GMT-14' as "0"
         """
         ).df()
 
-        # 'kea_CV' is 20 hours ahead of UTC
-        offset = datetime.timedelta(hours=20)
+        offset = datetime.timedelta(hours=14)
         timezone = datetime.timezone(offset)
 
         df_in = pandas.DataFrame(
             {
                 0: pandas.Series(
-                    data=[datetime.datetime(year=2022, month=1, day=1, hour=19, tzinfo=timezone)], dtype='object'
+                    data=[datetime.datetime(year=2021, month=12, day=31, hour=23, tzinfo=timezone)], dtype='object'
                 )
             }
         )