You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org> on 2026/03/02 04:55:12 UTC
[PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
dmitry-chirkov-dremio opened a new pull request, #49421:
URL: https://github.com/apache/arrow/pull/49421
### Rationale for this change
The `castVARCHAR` functions in Gandiva have memory allocation inefficiencies and missing edge case handling. See GH-49420 for details.
### What changes are included in this PR?
**Functional fixes:**
- `bool`: Remove unused 5-byte arena allocation; return string literal directly
- `int32`/`int64`: Add handling for `len=0` (return empty string) and `len<0` (set error)
**Memory allocation optimizations:**
- `int32`/`int64`: Use stack buffer with digit-pair conversion, allocate only `min(len, actual_size)` bytes
- `date64`: Allocate only `min(len, 10)` bytes upfront (output is always "YYYY-MM-DD")
- `float32`/`float64`: Allocate only `min(len, 24)` bytes upfront (max output length)
**Code cleanup:**
- Extract common code into helper macros to reduce duplication
### Are these changes tested?
Yes. Added tests for `len=0` and `len<0` edge cases for int64, date64, float32, float64, and bool types. All existing Gandiva tests pass.
### Are there any user-facing changes?
No. Users will see reduced memory usage and proper error messages for invalid len parameter values.
Note: Error messages for negative `len` remain different between precompiled ("Output buffer length can't be negative") and interpreted ("Buffer length cannot be negative") code paths, preserving existing behavior.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "kou (via GitHub)" <gi...@apache.org>.
kou merged PR #49421:
URL: https://github.com/apache/arrow/pull/49421
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3994357817
@lriggs @akravchukdremio @xxlaykxx You may want to review this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on code in PR #49421:
URL: https://github.com/apache/arrow/pull/49421#discussion_r2928972455
##########
cpp/src/gandiva/gdv_string_function_stubs.cc:
##########
@@ -81,91 +83,125 @@ const char* gdv_fn_regexp_extract_utf8_utf8_int32(int64_t ptr, int64_t holder_pt
return (*holder)(context, data, data_len, extract_index, out_length);
}
-#define GDV_FN_CAST_VARLEN_TYPE_FROM_TYPE(IN_TYPE, CAST_NAME, ARROW_TYPE) \
- GANDIVA_EXPORT \
- const char* gdv_fn_cast##CAST_NAME##_##IN_TYPE##_int64( \
- int64_t context, gdv_##IN_TYPE value, int64_t len, int32_t * out_len) { \
- if (len < 0) { \
- gdv_fn_context_set_error_msg(context, "Buffer length cannot be negative"); \
- *out_len = 0; \
- return ""; \
- } \
- if (len == 0) { \
- *out_len = 0; \
- return ""; \
- } \
- arrow::internal::StringFormatter<arrow::ARROW_TYPE> formatter; \
- char* ret = reinterpret_cast<char*>( \
- gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len))); \
- if (ret == nullptr) { \
- gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
- *out_len = 0; \
- return ""; \
- } \
- arrow::Status status = formatter(value, [&](std::string_view v) { \
- int64_t size = static_cast<int64_t>(v.size()); \
- *out_len = static_cast<int32_t>(len < size ? len : size); \
- memcpy(ret, v.data(), *out_len); \
- return arrow::Status::OK(); \
- }); \
- if (!status.ok()) { \
- std::string err = "Could not cast " + std::to_string(value) + " to string"; \
- gdv_fn_context_set_error_msg(context, err.c_str()); \
- *out_len = 0; \
- return ""; \
- } \
- return ret; \
+// The following castVARCHAR macros are optimized to allocate only the actual
+// string size instead of the maximum buffer length (which can be 65536+ bytes).
+
+// Helper: arena allocation + null check
+#define GDV_FN_CAST_VARLEN_ALLOC(SIZE) \
+ char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, SIZE)); \
+ if (ret == nullptr) { \
+ gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
+ *out_len = 0; \
+ return ""; \
+ }
+
+// Helper: function signature + len validation
+#define GDV_FN_CAST_VARLEN_PREFIX(IN_TYPE, CAST_NAME) \
+ GANDIVA_EXPORT \
+ const char* gdv_fn_cast##CAST_NAME##_##IN_TYPE##_int64( \
+ int64_t context, gdv_##IN_TYPE value, int64_t len, int32_t * out_len) { \
+ if (len < 0) { \
+ gdv_fn_context_set_error_msg(context, "Buffer length cannot be negative"); \
+ *out_len = 0; \
+ return ""; \
+ } \
+ if (len == 0) { \
+ *out_len = 0; \
+ return ""; \
+ }
+
+// Macro for integer types (int32/int64). Uses optimized digit-pair conversion
+// directly into a small arena allocation (11 bytes for int32, 20 for int64).
+#define GDV_FN_CAST_VARLEN_TYPE_FROM_INTEGER(IN_TYPE, CAST_NAME, ARROW_TYPE) \
+ GDV_FN_CAST_VARLEN_PREFIX(IN_TYPE, CAST_NAME) \
+ constexpr int32_t max_len = std::numeric_limits<gdv_##IN_TYPE>::digits10 + 2; \
+ GDV_FN_CAST_VARLEN_ALLOC(max_len) \
+ char* end = ret + max_len; \
+ char* p = end; \
+ auto uval = value < 0 \
+ ? static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>( \
+ ~static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>(value) + 1) \
+ : static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>(value); \
+ const char* pairs = arrow::internal::detail::digit_pairs; \
+ while (uval >= 100) { \
+ auto idx = (uval % 100) * 2; \
+ uval /= 100; \
+ *--p = pairs[idx + 1]; \
+ *--p = pairs[idx]; \
+ } \
+ if (uval >= 10) { \
+ auto idx = uval * 2; \
+ *--p = pairs[idx + 1]; \
+ *--p = pairs[idx]; \
+ } else { \
+ *--p = '0' + static_cast<char>(uval); \
+ } \
+ if (value < 0) { \
+ *--p = '-'; \
+ } \
Review Comment:
@kou I had this done before however my local experiments showed performance regression - luckily, I was able to chase that out!
Addressed. Resolving. Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3986029261
Pushed [clang-format](https://github.com/apache/arrow/pull/49421/changes/61e5ec20dc4e71e67ddb4c3fdf4f04a99affc090) fixes
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #49421:
URL: https://github.com/apache/arrow/pull/49421#discussion_r2922797151
##########
cpp/src/gandiva/gdv_string_function_stubs.cc:
##########
@@ -81,91 +83,125 @@ const char* gdv_fn_regexp_extract_utf8_utf8_int32(int64_t ptr, int64_t holder_pt
return (*holder)(context, data, data_len, extract_index, out_length);
}
-#define GDV_FN_CAST_VARLEN_TYPE_FROM_TYPE(IN_TYPE, CAST_NAME, ARROW_TYPE) \
- GANDIVA_EXPORT \
- const char* gdv_fn_cast##CAST_NAME##_##IN_TYPE##_int64( \
- int64_t context, gdv_##IN_TYPE value, int64_t len, int32_t * out_len) { \
- if (len < 0) { \
- gdv_fn_context_set_error_msg(context, "Buffer length cannot be negative"); \
- *out_len = 0; \
- return ""; \
- } \
- if (len == 0) { \
- *out_len = 0; \
- return ""; \
- } \
- arrow::internal::StringFormatter<arrow::ARROW_TYPE> formatter; \
- char* ret = reinterpret_cast<char*>( \
- gdv_fn_context_arena_malloc(context, static_cast<int32_t>(len))); \
- if (ret == nullptr) { \
- gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
- *out_len = 0; \
- return ""; \
- } \
- arrow::Status status = formatter(value, [&](std::string_view v) { \
- int64_t size = static_cast<int64_t>(v.size()); \
- *out_len = static_cast<int32_t>(len < size ? len : size); \
- memcpy(ret, v.data(), *out_len); \
- return arrow::Status::OK(); \
- }); \
- if (!status.ok()) { \
- std::string err = "Could not cast " + std::to_string(value) + " to string"; \
- gdv_fn_context_set_error_msg(context, err.c_str()); \
- *out_len = 0; \
- return ""; \
- } \
- return ret; \
+// The following castVARCHAR macros are optimized to allocate only the actual
+// string size instead of the maximum buffer length (which can be 65536+ bytes).
+
+// Helper: arena allocation + null check
+#define GDV_FN_CAST_VARLEN_ALLOC(SIZE) \
+ char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, SIZE)); \
+ if (ret == nullptr) { \
+ gdv_fn_context_set_error_msg(context, "Could not allocate memory"); \
+ *out_len = 0; \
+ return ""; \
+ }
+
+// Helper: function signature + len validation
+#define GDV_FN_CAST_VARLEN_PREFIX(IN_TYPE, CAST_NAME) \
+ GANDIVA_EXPORT \
+ const char* gdv_fn_cast##CAST_NAME##_##IN_TYPE##_int64( \
+ int64_t context, gdv_##IN_TYPE value, int64_t len, int32_t * out_len) { \
+ if (len < 0) { \
+ gdv_fn_context_set_error_msg(context, "Buffer length cannot be negative"); \
+ *out_len = 0; \
+ return ""; \
+ } \
+ if (len == 0) { \
+ *out_len = 0; \
+ return ""; \
+ }
+
+// Macro for integer types (int32/int64). Uses optimized digit-pair conversion
+// directly into a small arena allocation (11 bytes for int32, 20 for int64).
+#define GDV_FN_CAST_VARLEN_TYPE_FROM_INTEGER(IN_TYPE, CAST_NAME, ARROW_TYPE) \
+ GDV_FN_CAST_VARLEN_PREFIX(IN_TYPE, CAST_NAME) \
+ constexpr int32_t max_len = std::numeric_limits<gdv_##IN_TYPE>::digits10 + 2; \
+ GDV_FN_CAST_VARLEN_ALLOC(max_len) \
+ char* end = ret + max_len; \
+ char* p = end; \
+ auto uval = value < 0 \
+ ? static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>( \
+ ~static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>(value) + 1) \
+ : static_cast<std::make_unsigned_t<gdv_##IN_TYPE>>(value); \
+ const char* pairs = arrow::internal::detail::digit_pairs; \
+ while (uval >= 100) { \
+ auto idx = (uval % 100) * 2; \
+ uval /= 100; \
+ *--p = pairs[idx + 1]; \
+ *--p = pairs[idx]; \
+ } \
+ if (uval >= 10) { \
+ auto idx = uval * 2; \
+ *--p = pairs[idx + 1]; \
+ *--p = pairs[idx]; \
+ } else { \
+ *--p = '0' + static_cast<char>(uval); \
+ } \
+ if (value < 0) { \
+ *--p = '-'; \
+ } \
Review Comment:
Why do we need to re-implement https://github.com/apache/arrow/blob/669830165a26eaa8169b3f5bef07137aec6f0711/cpp/src/arrow/util/formatting.h#L151-L176 ?
Can we use `FormatAllDigits()` instead?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3998091966
## Performance Benchmark Results
Benchmarks run on Apple M3, 5 repetitions each. Tests exercise `castVARCHAR` for Int32/Int64 with various `len` parameters.
**Test data:**
- `Int32` / `Int64`: Full-range random integers (1M rows)
- `Int32Small` / `Int64Small`: 2-digit integers 10-99 (1M rows)
### Int32 (Full-Range Random)
| len | Original (μs) | Optimized (μs) | Δ |
|-----|---------------|----------------|---|
| 1 | 17,200 | 16,856 | **-2.0%** ✅ |
| 11 | 18,856 | 18,977 | +0.6% |
| 100 | 19,088 | 18,819 | **-1.4%** ✅ |
| 65536 | 18,792 | 18,951 | +0.8% |
### Int32 (Small 2-Digit)
| len | Original (μs) | Optimized (μs) | Δ |
|-----|---------------|----------------|---|
| 1 | 11,791 | 11,062 | **-6.2%** ✅ |
| 11 | 12,461 | 11,756 | **-5.7%** ✅ |
| 100 | 12,606 | 11,878 | **-5.8%** ✅ |
| 65536 | 12,353 | 11,912 | **-3.6%** ✅ |
### Int64 (Full-Range Random)
| len | Original (μs) | Optimized (μs) | Δ |
|-----|---------------|----------------|---|
| 1 | 17,801 | 17,246 | **-3.1%** ✅ |
| 20 | 19,785 | 19,010 | **-3.9%** ✅ |
| 100 | 19,268 | 19,439 | +0.9% |
| 65536 | 19,688 | 18,950 | **-3.7%** ✅ |
### Int64 (Small 2-Digit)
| len | Original (μs) | Optimized (μs) | Δ |
|-----|---------------|----------------|---|
| 1 | 11,747 | 11,255 | **-4.2%** ✅ |
| 20 | 12,581 | 11,435 | **-9.1%** ✅ |
| 100 | 12,357 | 11,728 | **-5.1%** ✅ |
| 65536 | 12,586 | 11,796 | **-6.3%** ✅ |
**Memory savings:** Allocates 11-20 bytes per integer instead of up to 65,536 bytes — a **3,000x reduction** in typical VARCHAR(65536) usage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3994953692
> @lriggs @akravchukdremio @xxlaykxx You may want to review this.
https://github.com/telemenar is reviewing offline
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3988215210
Additional context from benchmarking expression evaluation for the scenario I was troubleshooting:
LPAD+castVARCHAR
- Before: LPAD(castVARCHAR(int)) = 1.85x overhead vs LPAD(string)
- After: LPAD(castVARCHAR(int)) = 1.59x overhead vs LPAD(string)
- Improvement: ~14% reduction in castVARCHAR overhead
3-column expression with nested LPAD/CASE/CONCAT:
- Before: ~1350 ms
- After: ~857 ms
- Improvement: ~37% faster
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-3984405445
Let me go through first-timer's hurdles (like pre-commit clang format failures).
And I'll look around for this error's explanation: 'aws/core/utils/pagination/Paginator.h' file not found
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-4054628975
After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit b9274bc9e043338e070f342503e392b935e5fbfa.
There were no benchmark performance regressions. 🎉
The [full Conbench report](https://github.com/apache/arrow/runs/66948616748) has more details. It also includes information about 30 possible false positives for unstable benchmarks that are known to sometimes produce them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-49420: [C++][Gandiva] Fix castVARCHAR memory allocation and len<=0 handling [arrow]
Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #49421:
URL: https://github.com/apache/arrow/pull/49421#issuecomment-4052643906
@telemenar
> now we always allocate at `max_len`.
I reverified - going with `out_len` allocation introduces a measurable performance penalty (5-10%!) and I don't want to chase that optimization for ~10 byte/value savings. Some other day! 😄
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org