Skip to content

DX-101651: [C++][Gandiva] castVARCHAR(binary) validates UTF-8 - #158

Open
vitaliibrodetskyi-prog wants to merge 3 commits into
dremio_27.0_23_19from
DX-101651-gandiva-castvarchar-validate-utf8
Open

vitaliibrodetskyi-prog wants to merge 3 commits into
dremio_27.0_23_19from
DX-101651-gandiva-castvarchar-validate-utf8

Conversation

@vitaliibrodetskyi-prog

@vitaliibrodetskyi-prog vitaliibrodetskyi-prog commented Nov 12, 2026 •

Copy link
Copy Markdown

Problem

castVARCHAR(binary, int64) reinterpreted raw bytes as VARCHAR and only inspected them when it had to truncate. Invalid UTF-8 (e.g. UNBASE64('hello world!') = 85 E9 65) therefore became a VARCHAR, and later string functions failed:

SELECT LENGTH(CAST(UNBASE64('hello world!') AS VARCHAR))
-- unexpected byte \85 encountered while decoding utf8 string

Change

  • New castVARCHAR_binary_int64 replaces the shared macro instantiation for binary (the utf8 variant is unchanged: Arrow guarantees its input).
  • Validates the whole input before truncating, strictly (Unicode Table 3-7): invalid lead bytes, bad/missing continuation bytes, overlong forms, surrogates, code points above U+10FFFF. The check is Arrow's arrow::util::ValidateUTF8, exposed to the precompiled code as the external function gdv_fn_is_valid_utf8 (it relies on lookup tables, so it cannot be inlined into the bitcode). The stub calls InitializeUTF8() first: ValidateUTF8 accepts every input until Arrow's tables are initialized.
  • Error message: Cannot cast VARBINARY to VARCHAR: input is not valid UTF-8 (same text as the planned Dremio Java cast).
  • Registered with kCanReturnErrors, so Gandiva skips the call for NULL rows (their slots may hold arbitrary bytes).

Verification

  • gandiva-precompiled-test: 135/135, gandiva-projector-test: 263/263 (TZ=UTC), gandiva-internals-test: 197/197. New tests: TestStringOps.TestCastVarcharFromBinaryValidatesUtf8 (valid + 15 invalid shapes), updated TestCastVarchar.
  • gandiva-projector-test TestUtf8.TestCastVarCharFromBinary: end-to-end, a NULL row holding invalid bytes is not validated, invalid non-NULL input fails the batch.
  • Arrow's validator checked against Python's strict UTF-8 decoder: exhaustively for all 1-3 byte inputs (16.8M) and with 900k random/edge cases (long inputs, 4-byte forms, bytes after an ASCII run): 0 mismatches. It also agrees with Dremio's Java GuavaUtf8.isUtf8.
  • Cost: the cast used to return its input without reading it when no truncation was needed, so validation is extra work proportional to the value size, and the whole input is scanned even when the cast truncates. End-to-end Gandiva timings on this PR's head, M4 Max, as a multiple of the unvalidated baseline: ASCII 16-256 B 1.4-1.6x, ASCII 4 KB 1.9x; multi-byte text 64-96 B 2.0-3.0x (2.3x when half the rows are NULL); castVARCHAR(b, 10) on a 4 KB ASCII value 20x (see the alternative below); inside length(castVARCHAR(..)) 1.1-1.3x for ASCII and 1.6-1.7x for short multi-byte values. Re-measured after switching to Arrow's validator; tables and method in the performance comment. Not measured on x86-64/Linux.

Alternative considered

Validating only the returned bytes when the cast truncates (cheaper truncation, slightly different semantics) was built and measured; see the comment below. It is not part of this PR.

Behaviour change

Binary that is not valid UTF-8 now fails the cast instead of producing an invalid VARCHAR. Companion Dremio change adds the same check to the Java cast so both engines agree.

Part of DX-101651. Dremio-side PRs follow once a build of this is available.

🤖 Generated with Claude Code

Additional info: related PRs

All PRs are part of DX-101651 ("LENGTH can't handle unicode characters"):

PR Repo What
dremio/arrow#158 Arrow (Gandiva C++) castVARCHAR(binary) validates UTF-8
dremio/arrow#159 Arrow (Gandiva C++) unbase64 rejects invalid base64
dremio/dremio#31344 Dremio (Java), draft same UTF-8 validation in the Java CAST(VARBINARY AS VARCHAR)
dremio/dremio#31345 Dremio (Java/planner) LENGTH(VARBINARY) returns the size in bytes

Dremio counterpart: dremio/dremio#31344 adds the same check (same rule, same error message) to the Java cast, so Java and Gandiva agree on which inputs fail. Dremio only gets this change after arrow.version in oss/pom.xml is bumped to a build containing this PR; until then apache#31344 is a draft and its Gandiva invalid-input test fails on purpose.

Related, not dependent: dremio/arrow#159 changes a different function and can merge in any order. dremio/dremio#31345 uses Gandiva's existing octet_length(binary) and does not need this PR.

castVARCHAR(binary, int64) reinterpreted the bytes as VARCHAR and only
looked at them when it had to truncate, so invalid UTF-8 (for example the
output of UNBASE64('hello world!')) became a VARCHAR. Downstream string
functions such as length() then failed with "unexpected byte ... while
decoding utf8 string".

Validate the whole input before truncating, with a strict check (Unicode
Table 3-7: invalid lead bytes, bad or missing continuation bytes, overlong
forms, surrogates and code points above U+10FFFF) and raise
"Cannot cast VARBINARY to VARCHAR: input is not valid UTF-8". The function
is now flagged kCanReturnErrors so NULL rows, whose slots may hold arbitrary
bytes, are not validated.

The utf8 variant keeps the shared macro since Arrow guarantees its input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@vitaliibrodetskyi-prog

vitaliibrodetskyi-prog commented Nov 12, 2026 •

Copy link
Copy Markdown
Author

Performance results (end-to-end Gandiva, baseline vs this PR's current head)

Setup. Real Gandiva Projector evaluating the expression over a 4096-row batch (a typical batch size); every row holds the same value unless noted. Three libgandiva builds from the exact branch heads: baseline f21f62c, #158 at 8465dc1, #159 at 01e2360 (Release, LLVM 21). Numbers are wall-clock ns per row for Evaluate (output allocation included, JIT compile excluded): median of 9 timed runs, averaged over 2 interleaved rounds (base, #158, #159, base, ...), single thread, Apple M4 Max. Each build's behaviour was verified (baseline accepts invalid bytes and invalid base64, #158 rejects the bytes, #159 rejects the base64).

Noise. The two Arrow PRs touch different functions, so each is the other's control and its column should read 1.00x. It was 0.97x-1.03x everywhere except one sub-20 ns cell (1.12x); the two rounds differed by at most 16%, usually under 5%. Treat differences below ~10% as noise.

castVARCHAR(b, 65536) (no truncation)

Input baseline ns/row #158 ns/row ratio control (#159)
ASCII 16 B 6.4 8.7 1.36x 1.01x
ASCII 256 B 12.9 20.8 1.62x 1.00x
ASCII 4 KB 128.3 240.0 1.87x 1.02x
Latin text 64 B (é, ö) 7.7 23.1 3.01x 1.01x
CJK 96 B (3-byte chars) 9.7 28.0 2.90x 1.01x
Emoji 64 B (4-byte chars) 9.1 18.4 2.02x 1.03x
Latin 64 B, every other row NULL 7.9 17.8 2.26x 1.00x

castVARCHAR(b, 10) (truncates to 10 chars)

Input baseline ns/row #158 ns/row ratio control (#159)
ASCII 16 B 6.9 9.3 1.35x 1.01x
ASCII 256 B 6.9 17.3 2.50x 1.02x
ASCII 4 KB 7.2 143.4 19.89x 1.01x
Latin text 64 B (é, ö) 12.2 28.0 2.28x 1.02x
CJK 96 B (3-byte chars) 18.8 38.5 2.05x 1.03x
Emoji 64 B (4-byte chars) 17.6 26.7 1.52x 1.02x

length(castVARCHAR(b, 65536)) (the query from DX-101651)

Input baseline ns/row #158 ns/row ratio control (#159)
ASCII 16 B 6.0 7.9 1.32x 0.98x
ASCII 256 B 99.7 107.5 1.08x 1.03x
ASCII 4 KB 1316.4 1475.8 1.12x 1.01x
Latin text 64 B (é, ö) 20.8 34.7 1.67x 1.00x
CJK 96 B (3-byte chars) 32.6 53.1 1.63x 0.98x
Emoji 64 B (4-byte chars) 16.9 26.9 1.59x 1.12x

What the numbers mean

  • The baseline cast is free. With no truncation it returns the input pointer without reading a byte, so any validation adds work proportional to the value size.
  • ASCII: short values cost +2 to +8 ns/row (the call into Arrow's validator is included); 4 KB values cost about 2x of a very cheap operation (+112 ns/row, roughly 36 GB/s, L1-resident data).
  • Multi-byte text: +9 to +18 ns/row for 64-96 byte values (Latin and CJK validate at about 4-5 GB/s, derived from these rows), 2.0-3.0x of the free baseline. Using Arrow's ValidateUTF8 instead of the first, hand-written validator cut this from 3.3-5.8x.
  • NULL rows are skipped (kCanReturnErrors): with half the rows NULL the Latin case costs 2.3x instead of 3.0x.
  • With truncation (castVARCHAR(b, 10)) the baseline stops after 10 characters, but this PR validates the whole input (deliberately, so invalid bytes after the cut are rejected). So cost grows with the value size: 20x for 4 KB ASCII (7 -> 143 ns/row). A variant that validates only the returned bytes is described in a separate comment on this PR.
  • Inside the DX-101651 pipeline (length(castVARCHAR(...))), length() already scans the data, so the added cost is smaller: 1.1-1.3x for ASCII and 1.6-1.7x for short multi-byte values (+10 to +20 ns/row).

Not covered

x86-64/Linux (what production mostly runs on) was not measured; the ratios should be similar but absolute numbers will differ. Not measured: mixed value sizes within one batch, and values larger than 4 KB.

Benchmark source (gbench.cc)
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <memory>
#include <random>
#include <string>
#include <vector>

#include "arrow/array/builder_binary.h"
#include "arrow/memory_pool.h"
#include "arrow/record_batch.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "gandiva/projector.h"
#include "gandiva/tree_expr_builder.h"

using gandiva::Projector;
using gandiva::TreeExprBuilder;

static constexpr int kRows = 4096;

static std::string Repeat(const std::string& unit, size_t bytes) {
  std::string s;
  while (s.size() < bytes) s += unit;
  s.resize(bytes);
  // do not cut a multi-byte character in half
  while (!s.empty() && (static_cast<unsigned char>(s.back()) & 0xC0) == 0x80) s.pop_back();
  if (!s.empty() && static_cast<unsigned char>(s.back()) >= 0xC0) s.pop_back();
  return s;
}

static const char* kB64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static std::string RandomBase64(std::mt19937& rng, int raw_bytes) {
  std::string out;
  int i = 0;
  for (; i + 3 <= raw_bytes; i += 3)
    for (int j = 0; j < 4; ++j) out += kB64[rng() % 64];
  int rem = raw_bytes - i;
  if (rem == 1) { out += kB64[rng() % 64]; out += kB64[(rng() % 16) * 4]; out += "=="; }
  if (rem == 2) { out += kB64[rng() % 64]; out += kB64[rng() % 64]; out += kB64[(rng() % 16) * 4]; out += '='; }
  return out;
}

template <typename Builder>
static std::shared_ptr<arrow::Array> Column(const std::vector<std::string>& values) {
  Builder b;
  for (const auto& v : values) (void)b.Append(v);
  std::shared_ptr<arrow::Array> a;
  (void)b.Finish(&a);
  return a;
}

template <typename Builder>
static std::shared_ptr<arrow::Array> ColumnHalfNull(const std::vector<std::string>& values) {
  Builder b;
  for (size_t i = 0; i < values.size(); ++i) {
    if (i % 2 == 1) (void)b.AppendNull(); else (void)b.Append(values[i]);
  }
  std::shared_ptr<arrow::Array> a;
  (void)b.Finish(&a);
  return a;
}

static std::string Unpadded(std::string s) { while (!s.empty() && s.back() == '=') s.pop_back(); return s; }

struct Case { std::string name; std::vector<std::string> values; };

static double MedianNsPerRow(const std::shared_ptr<Projector>& p,
                             const std::shared_ptr<arrow::RecordBatch>& batch) {
  auto pool = arrow::default_memory_pool();
  arrow::ArrayVector out;
  for (int i = 0; i < 30; ++i) { out.clear(); (void)p->Evaluate(*batch, pool, &out); }
  // calibrate repetitions to ~150 ms
  auto t0 = std::chrono::steady_clock::now();
  int calib = 20;
  for (int i = 0; i < calib; ++i) { out.clear(); (void)p->Evaluate(*batch, pool, &out); }
  double per = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count() / calib;
  int reps = std::max(5, static_cast<int>(0.15 / per));
  std::vector<double> samples;
  for (int r = 0; r < 9; ++r) {
    auto s = std::chrono::steady_clock::now();
    for (int i = 0; i < reps; ++i) { out.clear(); (void)p->Evaluate(*batch, pool, &out); }
    double sec = std::chrono::duration<double>(std::chrono::steady_clock::now() - s).count();
    samples.push_back(sec * 1e9 / (static_cast<double>(reps) * kRows));
  }
  std::sort(samples.begin(), samples.end());
  return samples[samples.size() / 2];
}

int main(int argc, char** argv) {
  std::string variant = argc > 1 ? argv[1] : "?";
  std::mt19937 rng(42);

  // ---- castVARCHAR(binary) ----
  std::vector<Case> cast_cases = {
      {"ascii_16B", {}}, {"ascii_256B", {}}, {"ascii_4096B", {}},
      {"latin_64B", {}}, {"cjk_96B", {}},    {"emoji_64B", {}},
  };
  std::vector<std::pair<std::string, size_t>> units = {
      {"abcdefgh", 16}, {"abcdefgh", 256}, {"abcdefgh", 4096},
      {"h\xc3\xa9llo w\xc3\xb6rld ", 64}, {"\xe4\xb8\xad\xe6\x96\x87", 96}, {"\xf0\x9f\x98\x80", 64}};
  for (size_t c = 0; c < cast_cases.size(); ++c) {
    std::string v = Repeat(units[c].first, units[c].second);
    cast_cases[c].values.assign(kRows, v);
  }
  auto bin_field = arrow::field("b", arrow::binary());
  auto bin_schema = arrow::schema({bin_field});
  auto node_b = TreeExprBuilder::MakeField(bin_field);
  auto res_utf8 = arrow::field("r", arrow::utf8());
  auto res_i32 = arrow::field("r", arrow::int32());

  struct Expr { std::string name; gandiva::ExpressionPtr expr; };
  auto len65536 = TreeExprBuilder::MakeLiteral(static_cast<int64_t>(65536));
  auto len10 = TreeExprBuilder::MakeLiteral(static_cast<int64_t>(10));
  auto cast_full = TreeExprBuilder::MakeFunction("castVARCHAR", {node_b, len65536}, arrow::utf8());
  auto cast_trunc = TreeExprBuilder::MakeFunction("castVARCHAR", {node_b, len10}, arrow::utf8());
  auto cast_len = TreeExprBuilder::MakeFunction("length", {cast_full}, arrow::int32());
  std::vector<Expr> exprs = {
      {"castVARCHAR(b,65536)", TreeExprBuilder::MakeExpression(cast_full, res_utf8)},
      {"castVARCHAR(b,10)", TreeExprBuilder::MakeExpression(cast_trunc, res_utf8)},
      {"length(castVARCHAR(b,65536))", TreeExprBuilder::MakeExpression(cast_len, res_i32)},
  };
  for (const auto& e : exprs) {
    std::shared_ptr<Projector> p;
    auto st = Projector::Make(bin_schema, {e.expr}, &p);
    if (!st.ok()) { fprintf(stderr, "make failed: %s\n", st.ToString().c_str()); return 1; }
    for (const auto& c : cast_cases) {
      auto batch = arrow::RecordBatch::Make(bin_schema, kRows, {Column<arrow::BinaryBuilder>(c.values)});
      printf("RESULT|%s|%s|%s|%s|%.2f\n", variant.c_str(), "cast", e.name.c_str(), c.name.c_str(),
             MedianNsPerRow(p, batch));
      fflush(stdout);
    }
  }

  {
    std::shared_ptr<Projector> p;
    (void)Projector::Make(bin_schema, {exprs[0].expr}, &p);
    auto batch = arrow::RecordBatch::Make(bin_schema, kRows, {ColumnHalfNull<arrow::BinaryBuilder>(cast_cases[4].values)});
    printf("RESULT|%s|cast|castVARCHAR(b,65536)|latin_64B_half_NULL|%.2f\n", variant.c_str(), MedianNsPerRow(p, batch));
    fflush(stdout);
  }

  // sanity: what does each build do with invalid bytes?
  {
    std::shared_ptr<Projector> p;
    (void)Projector::Make(bin_schema, {exprs[0].expr}, &p);
    auto batch = arrow::RecordBatch::Make(bin_schema, 2, {Column<arrow::BinaryBuilder>({"ok", "\x85\xe9\xa5"})});
    arrow::ArrayVector out;
    auto st = p->Evaluate(*batch, arrow::default_memory_pool(), &out);
    printf("SANITY|%s|castVARCHAR_invalid_bytes|%s\n", variant.c_str(), st.ok() ? "accepted" : "rejected");
  }

  // ---- unbase64(utf8) ----
  auto txt_field = arrow::field("s", arrow::utf8());
  auto txt_schema = arrow::schema({txt_field});
  auto node_s = TreeExprBuilder::MakeField(txt_field);
  auto res_bin = arrow::field("r", arrow::binary());
  auto unb = TreeExprBuilder::MakeFunction("unbase64", {node_s}, arrow::binary());
  auto unb_expr = TreeExprBuilder::MakeExpression(unb, res_bin);
  std::shared_ptr<Projector> up;
  if (!Projector::Make(txt_schema, {unb_expr}, &up).ok()) { fprintf(stderr, "unbase64 make failed\n"); return 1; }
  for (int raw : {8, 64, 1024, 8192}) {
    std::vector<std::string> vals;
    for (int i = 0; i < kRows; ++i) vals.push_back(RandomBase64(rng, raw));
    auto batch = arrow::RecordBatch::Make(txt_schema, kRows, {Column<arrow::StringBuilder>(vals)});
    printf("RESULT|%s|unbase64|unbase64(s)|b64_%dB_decoded|%.2f\n", variant.c_str(), raw, MedianNsPerRow(up, batch));
    fflush(stdout);
  }
  {
    std::vector<std::string> vals, unp;
    for (int i = 0; i < kRows; ++i) { vals.push_back(RandomBase64(rng, 64)); unp.push_back(Unpadded(vals.back())); }
    auto b1 = arrow::RecordBatch::Make(txt_schema, kRows, {Column<arrow::StringBuilder>(unp)});
    printf("RESULT|%s|unbase64|unbase64(s)|b64_64B_unpadded|%.2f\n", variant.c_str(), MedianNsPerRow(up, b1));
    auto b2 = arrow::RecordBatch::Make(txt_schema, kRows, {ColumnHalfNull<arrow::StringBuilder>(vals)});
    printf("RESULT|%s|unbase64|unbase64(s)|b64_64B_half_NULL|%.2f\n", variant.c_str(), MedianNsPerRow(up, b2));
    fflush(stdout);
  }
  {
    auto batch = arrow::RecordBatch::Make(txt_schema, 2, {Column<arrow::StringBuilder>({"aGVsbG8=", "hello world!"})});
    arrow::ArrayVector out;
    auto st = up->Evaluate(*batch, arrow::default_memory_pool(), &out);
    printf("SANITY|%s|unbase64_invalid_text|%s\n", variant.c_str(), st.ok() ? "accepted" : "rejected");
  }
  return 0;
}

…alidateUTF8

The hand-written validator walked multi-byte text about 2x slower than
arrow::util::ValidateUTF8. Expose Arrow's validator as the external function
gdv_fn_is_valid_utf8 and call it from castVARCHAR_binary_int64.

Semantics are unchanged: the whole input is still validated, strictly
(Unicode Table 3-7), before truncating. The stub initializes Arrow's UTF-8
tables first, because ValidateUTF8 accepts every input until they are.

Measured end to end in a Gandiva projector, multi-byte values without
truncation go from 3.3-5.8x to 2.0-3.0x of the unvalidated baseline.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@vitaliibrodetskyi-prog

vitaliibrodetskyi-prog commented Nov 12, 2026 •

Copy link
Copy Markdown
Author

Alternative (not in this PR): validate only the bytes that are returned when the cast truncates

When the cast truncates (castVARCHAR(b, n) with n smaller than the value), this PR still validates the whole input, so invalid bytes after the cut are rejected. That matches the design on the ticket ("validate, then truncate"), but it makes truncation cost proportional to the full value size. An alternative (call it v3, built and measured on top of this PR) validates only the bytes that end up in the result:

  • When 0 < n < data_len, find the byte length of the first n characters (ASCII runs skipped 8 bytes at a time; an invalid lead byte or a cut-off character is an error), then run gdv_fn_is_valid_utf8 on just that prefix. When there is no truncation the whole input is validated, exactly as here.
  • The result is always valid UTF-8, so the VARCHAR invariant holds. Characters that are cut off are not part of the result.

Measured (same end-to-end setup, multiple of the unvalidated baseline; this PR / alternative):

Case this PR alternative
castVARCHAR(b, 10), ASCII 4 KB 19.4x 1.5x
castVARCHAR(b, 10), ASCII 256 B 2.5x 1.5x
castVARCHAR(b, 10), Latin 64 B 2.3x 1.5x
castVARCHAR(b, 10), CJK 96 B 2.1x 1.6x
no truncation (all inputs) same same

Note on the figures: the alternative's column was measured in an earlier run of the same benchmark; the 'this PR' column there was 19.4x / 2.5x / 2.3x / 2.1x. A later re-run on this PR's pushed head (see the performance comment) gives 19.9x / 2.5x / 2.3x / 2.05x, i.e. within noise, so the comparison still holds. The alternative was not re-run.

Trade-offs:

  • Behaviour differs only for input that is truncated and whose invalid bytes are all past the cut: CAST(X'6161C3' AS VARCHAR(2)) returns aa instead of an error. This is what the cast does today, so it removes some new failures.
  • The Java cast in dremio/dremio#31344 validates the whole input; it would need the same change (validate while walking to the cut) to keep both engines identical.
  • One existing assertion in TestStringOps.TestCastVarchar (aa\xC3 truncated to 2 characters) would flip from "error" to aa; a test for "invalid byte inside the prefix is rejected" would be added.
  • Not adopted here because it deviates from the agreed design and truncating a long binary value to a short VARCHAR is rare. It is a small, separate change if reviewers prefer it. Tested on Gandiva only (Arrow suites pass except the one assertion above).
Diff of the alternative relative to this PR (precompiled/string_ops.cc)
--- /private/tmp/claude-501/-Users-vitaliibrodetskyi-dev-dremio/b75652bd-3b98-4825-9225-9278650a17b4/scratchpad/v2_string_ops.cc	2026-11-12 17:24:57
+++ /private/tmp/claude-501/-Users-vitaliibrodetskyi-dev-dremio/b75652bd-3b98-4825-9225-9278650a17b4/scratchpad/v3_string_ops.cc	2026-11-12 17:24:57
@@ -694,19 +694,60 @@
 
 #undef CAST_VARCHAR_FROM_VARLEN_TYPE
 
-// Unlike utf8, binary input is not guaranteed to be valid UTF-8, so it is validated
-// before being reinterpreted as varchar.
+// Returns the length in bytes of the first char_count characters of data (all of data if it
+// has fewer), or -1 if one of them starts with an invalid byte or is cut off.
 FORCE_INLINE
+gdv_int32 utf8_prefix_byte_length(const char* data, gdv_int32 data_len,
+                                  gdv_int32 char_count) {
+  gdv_int32 index = 0;
+  gdv_int32 chars = 0;
+  while (chars < char_count && index < data_len) {
+    // skip runs of ascii 8 bytes at a time
+    while (chars + 8 <= char_count && index + 8 <= data_len) {
+      uint64_t chunk;
+      memcpy(&chunk, data + index, sizeof(chunk));
+      if ((chunk & 0x8080808080808080ULL) != 0) break;
+      index += 8;
+      chars += 8;
+    }
+    if (chars >= char_count || index >= data_len) break;
+    gdv_int32 char_len = utf8_char_length(data[index]);
+    if (char_len == 0 || index + char_len > data_len) return -1;
+    index += char_len;
+    ++chars;
+  }
+  return index;
+}
+
+// Unlike utf8, binary input is not guaranteed to be valid UTF-8, so the bytes that are
+// returned are validated. Characters cut off by the length are not part of the result and
+// are not validated.
+FORCE_INLINE
 const char* castVARCHAR_binary_int64(gdv_int64 context, const char* data,
                                      gdv_int32 data_len, int64_t out_len,
                                      int32_t* out_length) {
-  if (!gdv_fn_is_valid_utf8(data, data_len)) {
+  int32_t len = static_cast<int32_t>(out_len);
+  if (len < 0) {
+    char err_msg[96];
+    snprintf(err_msg, sizeof(err_msg),
+             "CAST_VARCHAR: Output buffer length can't be negative, got %d", len);
+    gdv_fn_context_set_error_msg(context, err_msg);
+    *out_length = 0;
+    return "";
+  }
+
+  gdv_int32 result_len = data_len;
+  if (len != 0 && len < data_len) {
+    result_len = utf8_prefix_byte_length(data, data_len, len);
+  }
+  if (result_len < 0 || !gdv_fn_is_valid_utf8(data, result_len)) {
     gdv_fn_context_set_error_msg(
         context, "Cannot cast VARBINARY to VARCHAR: input is not valid UTF-8");
     *out_length = 0;
     return "";
   }
-  return castVARCHAR_utf8_int64(context, data, data_len, out_len, out_length);
+  *out_length = result_len;
+  return data;
 }
 
 // Add functions for castVARBINARY

… in tests

UNBASE64('hello world!') decodes "hell" and stops at the space, which gives
85 E9 65, not 85 E9 A5. Both are invalid UTF-8 so the tests were valid, but the
comments named the wrong bytes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@vitaliibrodetskyi-prog

Copy link
Copy Markdown
Author

@akravchukdremio @lriggs if you get a chance, could you please review current changes?

@akravchukdremio akravchukdremio left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Verdict: No issues found. This implements the castVARCHAR fix agreed on DX-101651 (validate the whole input, then truncate, error on invalid UTF-8). Using Arrow's strict ValidateUTF8 also lines up with GuavaUtf8.isUtf8 in the Java cast, so both engines reject the same inputs.

Inspect first:

  • cpp/src/gandiva/gdv_function_stubs.cc: new external function gdv_fn_is_valid_utf8, which calls InitializeUTF8() once before validating, plus its i1 mapping
  • cpp/src/gandiva/function_registry_string.cc: kCanReturnErrors keeps NULL rows from being validated
  • cpp/src/gandiva/precompiled/string_ops_test.cc: intended behaviour change, aa\xC3 truncated to 2 characters now fails instead of returning aa
Change walkthrough (6 files)
File Change
cpp/src/gandiva/function_registry_string.cc castVARCHAR(binary, int64) flagged kCanReturnErrors
cpp/src/gandiva/gdv_function_stubs.cc New gdv_fn_is_valid_utf8 wrapping Arrow's ValidateUTF8, registered with the engine
cpp/src/gandiva/gdv_function_stubs.h Declaration of gdv_fn_is_valid_utf8
cpp/src/gandiva/precompiled/string_ops.cc Standalone castVARCHAR_binary_int64: validates, then delegates to the utf8 truncation path
cpp/src/gandiva/precompiled/string_ops_test.cc Valid and invalid UTF-8 cases; updated truncation/error assertions
cpp/src/gandiva/tests/utf8_test.cc End-to-end projector test: NULL row with invalid bytes is skipped, invalid non-NULL input fails

The failing CI jobs I checked failed on infrastructure (a missing Homebrew pkg-config@0.29.2 formula, a MinGW package-mirror timeout), not on this change.


AI-assisted analysis

@lriggs

lriggs commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Is this worth fixing giving the perf increases?

What happens when you run this query with your changes? SELECT LENGTH(CAST(UNBASE64('hello world!') AS VARCHAR))

Is it possible to change the length function to return a better error message instead of having to check first?

@vitaliibrodetskyi-prog

vitaliibrodetskyi-prog commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

Thanks @lriggs, good questions.

1. Is this worth fixing given the perf increase?

The cost is real, but only for castVARCHAR(binary); varchar to varchar casts are unchanged. Measured end to end through a Gandiva Projector (4096 rows, M4 Max, not x86; details in the performance comment above), as a multiple of the current time:

  • the cast alone takes 1.4-3x as long, because it used to return the bytes without reading them;
  • inside length(castVARCHAR(..)) it takes about 1.1-1.8x as long;
  • the worst case is about 20x: truncating a 4 KB value (castVARCHAR(b, 10)), because the whole input is validated before it is cut.

The validation itself is what the ticket decided on: the feedback on DX-101651 says CAST(VARBINARY AS VARCHAR) should fail on invalid UTF-8, so a VARCHAR always holds valid UTF-8 and no string function sees invalid bytes. The ticket also assumes this cast is not hot-path. I couldn't find usage data to confirm that, and note the planner also inserts this cast implicitly (that is how LENGTH(varbinary) ends up here), so counting explicit casts would undercount it.

I also compared this PR with the implementation sketched on the ticket (utf8_length to validate, then utf8_byte_pos to truncate), built on the same baseline and measured the same way:

Case this PR ticket sketch
castVARCHAR(b, 65536), ASCII 256 B 1.6x 6.5x
castVARCHAR(b, 65536), ASCII 4 KB 1.9x 9.2x
castVARCHAR(b, 65536), Latin 64 B 3.0x 3.9x
castVARCHAR(b, 10), ASCII 4 KB 19.7x 147x
length(castVARCHAR(..)), ASCII 256 B 1.1x 1.7x

It is also less strict: it accepts overlong forms (C0 AF), surrogates (ED A0 80) and code points above U+10FFFF, which the Java GuavaUtf8.isUtf8 check rejects, so Java and Gandiva would disagree. This PR rejects all of those.

Most of the time in this PR goes into Arrow's ValidateUTF8, which handles non-ASCII text one byte at a time. I tried two ways to reduce the cost:

  • Validate only the returned prefix when truncating (the alternative described in the second comment): the truncating case goes from about 20x to about 1.5x. It deviates from "validate, then truncate" on the ticket.
  • simdutf for values of 32 bytes or more (Arrow's validator below that, where simdutf is slower). I built this for real, vendored and trimmed to UTF-8 validation only, statically linked, with libgandiva growing by about 23 KB:
Case this PR with simdutf
castVARCHAR(b, 65536), ASCII 256 B 1.60x 1.16x
castVARCHAR(b, 65536), ASCII 4 KB 1.87x 1.15x
castVARCHAR(b, 65536), Latin 64 B 3.02x 1.68x
castVARCHAR(b, 65536), CJK 96 B 3.03x 1.64x
castVARCHAR(b, 65536), emoji 64 B 2.05x 1.82x
castVARCHAR(b, 10), ASCII 4 KB 19.7x 6.4x
length(castVARCHAR(..)), Latin 64 B 1.80x 1.35x

Values under 32 bytes are unchanged (about 1.4x for ASCII 16 B). It accepts and rejects exactly the same inputs as this PR (15 byte sequences run through the cast, plus 18.8M inputs compared directly against Arrow's validator), but it vendors a roughly 3 MB source file, so it's a dependency decision and I haven't measured it on x86.

Happy to do either (or both) if you think the cost matters.

2. What happens with SELECT LENGTH(CAST(UNBASE64('hello world!') AS VARCHAR))?

It still fails, because 'hello world!' is not valid base64, so there is no correct length to return. What changes is the error:

So #159 alone already gives this query a clear error, and it also makes unbase64 about 2.5-2.9x faster.

3. Can LENGTH return a better error instead of checking first?

Yes, that's possible, and it was considered on the ticket (LENGTH returning NULL) and judged not needed: with the cast validated, and LENGTH(VARBINARY) returning the byte count (dremio/dremio#31345), invalid bytes can't reach LENGTH from these queries. A better message in the two helpers (set_error_for_invalid_utf in precompiled/string_ops.cc and gdv_fn_set_error_for_invalid_utf8 in gdv_string_function_stubs.cc) would cost nothing, but it would only improve functions that already decode the string. Functions that just copy bytes (concat, for example) would still pass invalid VARCHAR through silently, and Java and Gandiva would still disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants