#pragma once #include "column_reader.h" #include "common/data_chunk/data_chunk.h" #include "common/file_system/virtual_file_system.h" #include "common/types/types.h" #include "function/function.h" #include "function/table/scan_file_function.h" #include "parquet_types.h" #include "protocol/TCompactProtocol.h" #include "resizable_buffer.h" namespace lbug { namespace storage { class MemoryManager; } // namespace storage namespace processor { struct ParquetReaderPrefetchConfig { // Percentage of data in a row group span that should be scanned for enabling whole group // prefetch static constexpr double WHOLE_GROUP_PREFETCH_MINIMUM_SCAN = 0.95; }; struct ParquetReaderScanState { std::vector groupIdxList; int64_t currentGroup = -1; uint64_t groupOffset = UINT64_MAX; std::unique_ptr fileInfo; std::unique_ptr rootReader; std::unique_ptr thriftFileProto; bool finished = false; // Range-limited scan support: rows to skip at the start of the first scanned row // group, and the total number of rows to emit (UINT64_MAX means "scan to end"). uint64_t rowsToSkip = 0; uint64_t rowsRemaining = UINT64_MAX; ResizeableBuffer defineBuf; ResizeableBuffer repeatBuf; bool prefetchMode = true; bool currentGroupPrefetched = false; }; class ParquetReader { public: ParquetReader(std::string filePath, std::vector columnSkips, main::ClientContext* context); ~ParquetReader() = default; void initializeScan(ParquetReaderScanState& state, std::vector groups_to_read, common::VirtualFileSystem* vfs, uint64_t skipRows = 0, uint64_t numRows = UINT64_MAX); bool scanInternal(ParquetReaderScanState& state, common::DataChunk& result); void scan(ParquetReaderScanState& state, common::DataChunk& result); uint64_t getNumRowGroups() { return metadata->row_groups.size(); } uint32_t getNumColumns() const { return columnNames.size(); } std::string getColumnName(uint32_t idx) const { return columnNames[idx]; } const common::LogicalType& getColumnType(uint32_t idx) const { return columnTypes[idx]; } lbug_parquet::format::FileMetaData* getMetadata() const { return metadata.get(); } storage::MemoryManager* getMemoryManager() const; private: std::unique_ptr createThriftProtocol( common::FileInfo* fileInfo_, bool prefetch_mode) { return std::make_unique< lbug_apache::thrift::protocol::TCompactProtocolT>( std::make_shared(fileInfo_, prefetch_mode)); } const lbug_parquet::format::RowGroup& getGroup(ParquetReaderScanState& state) { DASSERT( state.currentGroup >= 0 && (uint64_t)state.currentGroup < state.groupIdxList.size()); DASSERT(state.groupIdxList[state.currentGroup] < metadata->row_groups.size()); return metadata->row_groups[state.groupIdxList[state.currentGroup]]; } static common::LogicalType deriveLogicalType(const lbug_parquet::format::SchemaElement& s_ele); void initMetadata(); std::unique_ptr createReader(); std::unique_ptr createReaderRecursive(uint64_t depth, uint64_t maxDefine, uint64_t maxRepeat, uint64_t& nextSchemaIdx, uint64_t& nextFileIdx); void prepareRowGroupBuffer(ParquetReaderScanState& state, uint64_t colIdx); // Group span is the distance between the min page offset and the max page offset plus the max // page compressed size uint64_t getGroupSpan(ParquetReaderScanState& state); uint64_t getGroupCompressedSize(ParquetReaderScanState& state); uint64_t getGroupOffset(ParquetReaderScanState& state); private: std::string filePath; std::vector columnSkips; std::vector columnNames; std::vector columnTypes; std::unique_ptr metadata; main::ClientContext* context; }; struct ParquetScanSharedState final : function::ScanFileWithProgressSharedState { explicit ParquetScanSharedState(common::FileScanInfo fileScanInfo, uint64_t numRows, main::ClientContext* context, std::vector columnSkips); void resetState() override { fileIdx.store(0); blockIdx.store(0); numBlocksReadByFiles.store(0); } std::vector> readers; std::vector columnSkips; uint64_t totalRowsGroups; std::atomic numBlocksReadByFiles; }; struct ParquetScanLocalState final : function::TableFuncLocalState { ParquetScanLocalState() : reader(nullptr) { state = std::make_unique(); } ParquetReader* reader; std::unique_ptr state; }; struct ParquetScanFunction { static constexpr const char* name = "READ_PARQUET"; static function::function_set getFunctionSet(); }; } // namespace processor } // namespace lbug