|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <ctime> |
| 19 | +#include <iostream> |
| 20 | +#include <memory> |
| 21 | +#include <list> |
| 22 | + |
| 23 | +#include "parquet/api/reader.h" |
| 24 | + |
| 25 | +int main(int argc, char** argv) { |
| 26 | + if (argc > 4 || argc < 1) { |
| 27 | + std::cerr << "Usage: parquet-scan [--batch-size=] [--columns=...] <file>" |
| 28 | + << std::endl; |
| 29 | + return -1; |
| 30 | + } |
| 31 | + |
| 32 | + std::string filename; |
| 33 | + |
| 34 | + // Read command-line options |
| 35 | + int batch_size = 256; |
| 36 | + const std::string COLUMNS_PREFIX = "--columns="; |
| 37 | + const std::string BATCH_SIZE_PREFIX = "--batch-size="; |
| 38 | + std::vector<int> columns; |
| 39 | + int num_columns = 0; |
| 40 | + |
| 41 | + char *param, *value; |
| 42 | + for (int i = 1; i < argc; i++) { |
| 43 | + if ((param = std::strstr(argv[i], COLUMNS_PREFIX.c_str()))) { |
| 44 | + value = std::strtok(param + COLUMNS_PREFIX.length(), ","); |
| 45 | + while (value) { |
| 46 | + columns.push_back(std::atoi(value)); |
| 47 | + value = std::strtok(nullptr, ","); |
| 48 | + num_columns++; |
| 49 | + } |
| 50 | + } else if ((param = std::strstr(argv[i], BATCH_SIZE_PREFIX.c_str()))) { |
| 51 | + value = std::strtok(param + BATCH_SIZE_PREFIX.length(), " "); |
| 52 | + if (value) { batch_size = std::atoi(value); } |
| 53 | + } else { |
| 54 | + filename = argv[i]; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + std::vector<int16_t> rep_levels(batch_size); |
| 59 | + std::vector<int16_t> def_levels(batch_size); |
| 60 | + try { |
| 61 | + double total_time; |
| 62 | + std::clock_t start_time = std::clock(); |
| 63 | + std::unique_ptr<parquet::ParquetFileReader> reader = |
| 64 | + parquet::ParquetFileReader::OpenFile(filename); |
| 65 | + // columns are not specified explicitly. Add all columns |
| 66 | + if (num_columns == 0) { |
| 67 | + num_columns = reader->metadata()->num_columns(); |
| 68 | + columns.resize(num_columns); |
| 69 | + for (int i = 0; i < num_columns; i++) { |
| 70 | + columns[i] = i; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + int64_t total_rows[num_columns]; |
| 75 | + |
| 76 | + for (int r = 0; r < reader->metadata()->num_row_groups(); ++r) { |
| 77 | + auto group_reader = reader->RowGroup(r); |
| 78 | + int col = 0; |
| 79 | + for (auto i : columns) { |
| 80 | + total_rows[col] = 0; |
| 81 | + std::shared_ptr<parquet::ColumnReader> col_reader = group_reader->Column(i); |
| 82 | + size_t value_byte_size = GetTypeByteSize(col_reader->descr()->physical_type()); |
| 83 | + std::vector<uint8_t> values(batch_size * value_byte_size); |
| 84 | + |
| 85 | + int64_t values_read = 0; |
| 86 | + while (col_reader->HasNext()) { |
| 87 | + total_rows[col] += ScanAllValues(batch_size, def_levels.data(), |
| 88 | + rep_levels.data(), values.data(), &values_read, col_reader.get()); |
| 89 | + } |
| 90 | + col++; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + total_time = (std::clock() - start_time) / static_cast<double>(CLOCKS_PER_SEC); |
| 95 | + for (int ct = 1; ct < num_columns; ++ct) { |
| 96 | + if (total_rows[0] != total_rows[ct]) { |
| 97 | + std::cerr << "Parquet error: Total rows among columns do not match" << std::endl; |
| 98 | + } |
| 99 | + } |
| 100 | + std::cout << total_rows[0] << " rows scanned in " << total_time << " seconds." |
| 101 | + << std::endl; |
| 102 | + } catch (const std::exception& e) { |
| 103 | + std::cerr << "Parquet error: " << e.what() << std::endl; |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
0 commit comments