arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[C++] Enable use of USE_OS_TZDB with conda, also on windows

Open h-vetinari opened this issue 10 months ago • 27 comments

Describe the enhancement requested

I've started looking into this because the tests on the arrow feedstock are failing with

%SRC_DIR%\cpp\src\arrow\compute\kernels\test_util_internal.cc(87): error: Failed
'_error_or_value26.status()' failed with Invalid: Cannot locate or parse timezone 'Australia/Broken_Hill': Timezone database not found at "C:\Users\VssAdministrator\Downloads\tzdata"

(these failures had been hidden by a bug in a batch file, apologies).

Conda-forge ships tzdata as a noarch package, and it also works on windows; for example, I've contributed basic support for using conda-forge's tzdata to orc: https://github.com/apache/orc/pull/1882

I've started looking at patching this into arrow as well, but the code is pretty involved. I can put up a draft PR with some very basic first steps that I have, if that helps.

It's a bit urgent for me to fix this, because I need to fix the CI on the feedstock, and - short of disabling the tzdb-related tests on windows - I can't currently.

Component(s)

C++

h-vetinari avatar Jan 16 '26 02:01 h-vetinari

I would like to work on this issue.Please assign this to me.

shashbha14 avatar Jan 16 '26 08:01 shashbha14

Done.

FYI: You can do it by adding "take" only comment.

kou avatar Jan 16 '26 08:01 kou

@h-vetinari I suppose this is a blocker for Arrow 23.0.0 on conda right?

raulcd avatar Jan 19 '26 12:01 raulcd

This is currently a blocker for anything on the feedstock, unfortunately. Well, we could continue to ignore the previously existing errors, but that's quite far from responsible maintainership. I'd consider it as a last resort maybe

h-vetinari avatar Jan 20 '26 08:01 h-vetinari

cc @rok in case you have some time to look into this issue and help with suggestions.

AlenkaF avatar Jan 20 '26 08:01 AlenkaF

We are working towards using stdlib provided tzdb on windows here. However we missed the 23.0.0 window and the change is too big to tack on now. I don't have much else to add.

rok avatar Jan 20 '26 13:01 rok

Thanks a lot! Conda-forge has the ability to backport patches where necessary. Since main had barely any time to move on, and we need this change to fix our builds, I'll definitely consider a backport. That said, looking at the description of #48601, you're IMO going quite a bit further than would be necessary. Not that I'm against using C++20 std facilities per se (though they're still not completely implemented in libc++), but strictly speaking we only need a way to use conda-forge's tzdata.

When I started down this rabbit hole, I had some first trivial changes that looked as follows (before realizing that it's connected pretty deeply with the vendored stuff and probably not so easy to pipe through; from that POV, I can understand going with C++20 😅):

diff --git a/cpp/src/arrow/vendored/datetime/tz.cpp b/cpp/src/arrow/vendored/datetime/tz.cpp
index 2cf6c62a84..27a64701a4 100644
--- a/cpp/src/arrow/vendored/datetime/tz.cpp
+++ b/cpp/src/arrow/vendored/datetime/tz.cpp
@@ -476,16 +475,19 @@ CONSTDATA auto max_day = date::December/31;

 CONSTCD14 const sys_seconds min_seconds = sys_days(min_year/min_day);

-#endif  // USE_OS_TZDB
-
-#ifndef _WIN32
-
 static
 std::string
 discover_tz_dir()
 {
     struct stat sb;
     using namespace std;
+    // this is present if we're in an activated conda environment
+    const char* condaPrefix = getenv("CONDA_PREFIX");
+    if (condaPrefix) {
+      string condaDir(condaPrefix);
+      condaDir += "/share/zoneinfo";
+      return condaDir;
+    }
 #  if defined(ANDROID) || defined(__ANDROID__)
     CONSTDATA auto tz_dir_default = "/apex/com.android.tzdata/etc/tz";
     CONSTDATA auto tz_dir_fallback = "/system/usr/share/zoneinfo";
diff --git a/cpp/src/arrow/vendored/datetime/tz.h b/cpp/src/arrow/vendored/datetime/tz.h
index 61ab3df106..cd5fa70d29 100644
--- a/cpp/src/arrow/vendored/datetime/tz.h
+++ b/cpp/src/arrow/vendored/datetime/tz.h
@@ -82,12 +82,6 @@ static_assert(HAS_REMOTE_API == 0 ? AUTO_DOWNLOAD == 0 : true,
 #  define USE_SHELL_API 1
 #endif

-#if USE_OS_TZDB
-#  ifdef _WIN32
-#    error "USE_OS_TZDB can not be used on Windows"
-#  endif
-#endif
-
 #ifndef HAS_DEDUCTION_GUIDES
 #  if __cplusplus >= 201703
 #    define HAS_DEDUCTION_GUIDES 1
diff --git a/cpp/src/arrow/vendored/datetime/visibility.h b/cpp/src/arrow/vendored/datetime/visibility.h
index 780c00d70b..cd182bdf70 100644
--- a/cpp/src/arrow/vendored/datetime/visibility.h
+++ b/cpp/src/arrow/vendored/datetime/visibility.h
@@ -17,9 +17,7 @@

 #pragma once

-#ifndef _WIN32
-#  define USE_OS_TZDB 1
-#endif
+#define USE_OS_TZDB 1

 #if defined(ARROW_STATIC)
 // intentially empty

h-vetinari avatar Jan 20 '26 13:01 h-vetinari

There's one thing that a more minimal patch would be very useful for though: backporting, as we're still supporting v19-v22 (which are running into the same failures currently).

h-vetinari avatar Jan 20 '26 13:01 h-vetinari

@h-vetinari very much agreed! And a much more minimal patch can be cut out of the https://github.com/apache/arrow/pull/48601. Which compilers are you supporting?

rok avatar Jan 20 '26 13:01 rok

Which compilers are you supporting?

Our default compilers are currently GCC 14 on linux, Clang 19 on osx (will move to GCC 15 & Clang 21 in ~May/June) and VS 2022 on windows (where we have no control over the exact version, it's whatever comes along in https://github.com/actions/runner-images). Both clang and GCC are also usable on the other platforms if need be. Note: GCC-on-win is not mingw, but rather GCC compiling against the windows ucrt.

Note also that we do hook up libstdcxx to use our own tzdata. For libc++ we cannot do that due to security reasons as long as chrono is part of the static libc++experimental.a, rather than in the dylib. However, even with the system-tzdb, the partially implemented std::chrono in libc++ works according to some smoke tests, with the exception of leap seconds. Xref https://github.com/llvm/llvm-project/issues/117451

h-vetinari avatar Jan 20 '26 13:01 h-vetinari

If you don't use mingw and have GCC>=13 you should be able to avoid downloading tzdata on windows (I think). Sorry I can't spend time right now, but changes in https://github.com/apache/arrow/pull/48601 would probably be a good starting point. It might make sense for it to merge first and then backport the non-CI part.

rok avatar Jan 20 '26 13:01 rok

Hi, I've analyzed this issue and found it requires significant changes to the vendored datetime library. I see @rok is working on PR #48601 with a more comprehensive solution.

Should I: a) Wait for #48601 to be merged and help with backports? b) Try to implement a minimal patch based on @h-vetinari's suggestions?

Please advise on the best approach.

shashbha14 avatar Jan 20 '26 15:01 shashbha14

I am not an expert here but the PR @rok is pointing out will be complex to backport to pre 23.0.0 conda feedstocks. That PR is also under current development and is not clear when will be finished. In my opinion we should try and work on a minimal patch for the feedstocks to work.

From my understanding those timezones where removed for the official tzdata but on some operating systems are still part of the tzdata-legacy packages. Is this the problem?

cc @pitrou who might have ideas about possible solutions.

raulcd avatar Jan 20 '26 16:01 raulcd

I've started looking into this because the tests on the arrow feedstock are failing with

With which Arrow version, and when have they started failing?

pitrou avatar Jan 20 '26 16:01 pitrou

With which Arrow version, and when have they started failing?

Unfortunately, the errors were hidden due to an issue with batchfiles (%ERRORLEVEL% doesn't work in an if-branch). However, the failures are still visible in the logs; I went back and found that the issue appeared (exactly) with arrow v22; v21 and below are fine.

h-vetinari avatar Jan 20 '26 21:01 h-vetinari

I went back and found that the issue appeared (exactly) with arrow v22; v21 and below are fine.

Hmm, I see. I'm not sure what changed that made it appear, but you could try to go with the patch in https://github.com/apache/arrow/issues/48874#issuecomment-3772831757

pitrou avatar Jan 21 '26 07:01 pitrou

I doubt that this patch is complete or even functional. The tzdb-handling in cpp/src/arrow/vendored/datetime/tz.cpp is all over the place, and I opened this issue before pouring hours down this rabbit hole. I can give it a try, but it'll be a long & messy process, and I don't know that code very well or at all...

h-vetinari avatar Jan 21 '26 07:01 h-vetinari

Is Australia/Broken_Hill the only timezone that's not found? If so, you could just disable the corresponding tests.

pitrou avatar Jan 21 '26 08:01 pitrou

Unfortunately not; everything tzdb-related fails

h-vetinari avatar Jan 21 '26 11:01 h-vetinari

The environment variable ARROW_TIMEZONE_DATABASE should be set to somewhere where the timezone database resides, is that not the case in your test script?

pitrou avatar Jan 21 '26 12:01 pitrou