tmp3
bool HasParquetRowExceeding(parquet::ParquetFileReader* reader, uint64_t limit = 0x3fffffffULL) {
auto meta = reader->metadata();
auto* schema = meta->schema();
uint64_t base = 128ULL + (schema->num_columns() + 7) / 8; // openGauss 元组头+对齐预留
std::vector<int> var_cols;
bool has_rep = false;
for (int c = 0; c < schema->num_columns(); ++c) {
auto* col = schema->Column(c);
has_rep |= (col->max_repetition_level() > 0);
if (col->physical_type() == parquet::Type::BYTE_ARRAY) var_cols.push_back(c);
else base += col->physical_type() == parquet::Type::FIXED_LEN_BYTE_ARRAY
? ((std::max(0, col->type_length()) + 7ULL) & ~7ULL) : 8ULL;
}
constexpr int64_t kBatch = 4096;
std::vector<int16_t> def_buf(kBatch), rep_buf(kBatch);
std::vector<parquet::ByteArray> val_buf(kBatch);
for (int rg = 0; rg < meta->num_row_groups(); ++rg) {
auto rg_meta = meta->RowGroup(rg);
int64_t n = rg_meta->num_rows();
if (n <= 0) continue;
if (base > limit) return true;
// 1. Footer 快速剪枝(常规 128MB~512MB 行组在此 O(1) 跳过)
uint64_t uncomp_sum = 0;
std::vector<std::pair<uint64_t, int>> cols;
for (int c : var_cols) {
int64_t u = rg_meta->ColumnChunk(c)->total_uncompressed_size();
uint64_t sz = u < 0 ? UINT64_MAX : static_cast<uint64_t>(u);
uncomp_sum = (sz == UINT64_MAX || uncomp_sum > UINT64_MAX - sz) ? UINT64_MAX : uncomp_sum + sz;
cols.emplace_back(sz, c);
}
uint64_t rg_upper = std::max(
uncomp_sum, rg_meta->total_byte_size() > 0 ? static_cast<uint64_t>(rg_meta->total_byte_size()) : 0ULL);
if (!has_rep && rg_upper < UINT64_MAX && base + rg_upper + var_cols.size() * 7ULL <= limit) continue;
// 2. 超限行组按列记分 + 双向早退
std::vector<uint64_t> row_bytes(static_cast<size_t>(n), base);
std::sort(cols.begin(), cols.end(), std::greater<>());
auto rg_reader = reader->RowGroup(rg);
uint64_t rem = uncomp_sum;
size_t rem_cols = cols.size();
for (auto [u, c] : cols) {
auto* col = schema->Column(c);
int16_t max_def = col->max_definition_level(), max_rep = col->max_repetition_level();
auto rdr = std::static_pointer_cast<parquet::ByteArrayReader>(rg_reader->Column(c));
int64_t row = -1;
uint64_t max_row = 0;
while (rdr->HasNext()) {
int64_t vals = 0;
int64_t lvls = rdr->ReadBatch(kBatch, max_def ? def_buf.data() : nullptr,
max_rep ? rep_buf.data() : nullptr, val_buf.data(), &vals);
for (int64_t i = 0, v = 0; i < lvls; ++i) {
if (max_rep == 0 || rep_buf[i] == 0) ++row;
if (row < 0 || row >= n) break;
if (max_def == 0 || def_buf[i] == max_def)
row_bytes[row] += (static_cast<uint64_t>(val_buf[v++].len) + 7ULL) & ~3ULL;
if (row_bytes[row] > limit) return true;
max_row = std::max(max_row, row_bytes[row]);
}
}
if (!has_rep && rem < UINT64_MAX && max_row + (rem -= u) + (--rem_cols) * 7ULL <= limit) break;
}
}
return false;
}

浙公网安备 33010602011771号