Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::CopyToFunctionGlobalState Class Reference
Inheritance diagram for duckdb::CopyToFunctionGlobalState:
Collaboration diagram for duckdb::CopyToFunctionGlobalState:

Public Member Functions

 CopyToFunctionGlobalState (ClientContext &context_p)
void Initialize (ClientContext &context, const PhysicalCopyToFile &op)
void CreateDir (const string &dir_path, FileSystem &fs)
string GetOrCreateDirectory (const vector< idx_t > &cols, bool hive_file_pattern, const vector< string > &names, const vector< Value > &values, string path, FileSystem &fs)
optional_ptr< CopyToFileInfo > AddFile (const StorageLockKey &l, const string &file_name, CopyFunctionReturnType return_type)
void FinalizePartition (ClientContext &context, const PhysicalCopyToFile &op, PartitionWriteInfo &info)
void FinalizePartitions (ClientContext &context, const PhysicalCopyToFile &op)
PartitionWriteInfo & GetPartitionWriteInfo (ExecutionContext &context, const PhysicalCopyToFile &op, const vector< Value > &values)
void FinishPartitionWrite (PartitionWriteInfo &info)
Public Member Functions inherited from duckdb::GlobalSinkState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
virtual idx_t MaxThreads (idx_t source_max_threads)
Public Member Functions inherited from duckdb::StateWithBlockableTasks
unique_lock< mutex > Lock ()
void PreventBlocking (const unique_lock< mutex > &guard)
bool BlockTask (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
 Add a task to 'blocked_tasks' before returning SourceResultType::BLOCKED (must hold the lock).
bool CanBlock (const unique_lock< mutex > &guard) const
bool UnblockTasks (const unique_lock< mutex > &guard)
 Unblock all tasks (must hold the lock).
SinkResultType BlockSink (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
SourceResultType BlockSource (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
void VerifyLock (const unique_lock< mutex > &guard) const

Public Attributes

ClientContext & context
bool finalized
 Whether the copy was successfully finalized.
vector< string > created_files
 The list of files created by this operator.
StorageLock lock
atomic< bool > initialized
atomic< idx_t > rows_copied
atomic< idx_t > last_file_offset
unique_ptr< GlobalFunctionData > global_state
unordered_set< string > created_directories
 Created directories.
shared_ptr< GlobalHivePartitionState > partition_state
 shared state for HivePartitionedColumnData
vector< unique_ptr< CopyToFileInfo > > written_files
 Written file info and stats.
idx_t max_open_files
 Max open files.
unique_ptr< StorageLock > file_write_lock_if_rotating
 If rotate is true, this lock is used.
Public Attributes inherited from duckdb::GlobalSinkState
SinkFinalizeType state

Private Attributes

vector_of_value_map_t< unique_ptr< PartitionWriteInfo > > active_partitioned_writes
 The active writes per partition (for partitioned write).
vector_of_value_map_t< idx_t > previous_partitions
idx_t global_offset = 0

Constructor & Destructor Documentation

◆ CopyToFunctionGlobalState()

duckdb::CopyToFunctionGlobalState::CopyToFunctionGlobalState ( ClientContext & context_p)
inlineexplicit
193969 : context(context_p), finalized(false), initialized(false), rows_copied(0), last_file_offset(0),
193970 file_write_lock_if_rotating(make_uniq<StorageLock>()) {
193971 max_open_files = Settings::Get<PartitionedWriteMaxOpenFilesSetting>(context);
193972 }
unique_ptr< StorageLock > file_write_lock_if_rotating
If rotate is true, this lock is used.
Definition duckdb.cpp:193995
bool finalized
Whether the copy was successfully finalized.
Definition duckdb.cpp:193977
idx_t max_open_files
Max open files.
Definition duckdb.cpp:193993

◆ ~CopyToFunctionGlobalState()

duckdb::CopyToFunctionGlobalState::~CopyToFunctionGlobalState ( )
override
194177 {
194178 if (!initialized || finalized || created_files.empty()) {
194179 return;
194180 }
194181 // If we reach here, the query failed before Finalize was called
194182 auto &fs = FileSystem::GetFileSystem(context);
194183 for (auto &file : created_files) {
194184 try {
194185 fs.TryRemoveFile(file);
194186 } catch (...) {
194187 // TryRemoveFile migth fail for a varieaty of reasons, but we can't really propagate error codes here, so
194188 // best effort cleanup
194189 }
194190 }
194191}
vector< string > created_files
The list of files created by this operator.
Definition duckdb.cpp:193979

Member Function Documentation

◆ Initialize()

void duckdb::CopyToFunctionGlobalState::Initialize ( ClientContext & context,
const PhysicalCopyToFile & op )
inline
193997 {
193998 if (initialized) {
193999 return;
194000 }
194001 auto write_lock = lock.GetExclusiveLock();
194002 if (initialized) {
194003 return;
194004 }
194005 // initialize writing to the file
194006 created_files.push_back(op.file_path);
194007 global_state = op.function.copy_to_initialize_global(context, *op.bind_data, op.file_path);
194008 if (op.function.initialize_operator) {
194009 op.function.initialize_operator(*global_state, op);
194010 }
194011 auto written_file_info = AddFile(*write_lock, op.file_path, op.return_type);
194012 if (written_file_info) {
194013 op.function.copy_to_get_written_statistics(context, *op.bind_data, *global_state,
194014 *written_file_info->file_stats);
194015 }
194016 initialized = true;
194017 }

◆ CreateDir()

void duckdb::CopyToFunctionGlobalState::CreateDir ( const string & dir_path,
FileSystem & fs )
inline
194019 {
194020 if (created_directories.find(dir_path) != created_directories.end()) {
194021 // already attempted to create this directory
194022 return;
194023 }
194024 if (!fs.DirectoryExists(dir_path)) {
194025 fs.CreateDirectory(dir_path);
194026 }
194027 created_directories.insert(dir_path);
194028 }
unordered_set< string > created_directories
Created directories.
Definition duckdb.cpp:193987

◆ GetOrCreateDirectory()

string duckdb::CopyToFunctionGlobalState::GetOrCreateDirectory ( const vector< idx_t > & cols,
bool hive_file_pattern,
const vector< string > & names,
const vector< Value > & values,
string path,
FileSystem & fs )
inline
194031 {
194032 CreateDir(path, fs);
194033 if (hive_file_pattern) {
194034 for (idx_t i = 0; i < cols.size(); i++) {
194035 const auto &partition_col_name = names[cols[i]];
194036 const auto &partition_value = values[i];
194037 string p_dir;
194038 p_dir += HivePartitioning::Escape(partition_col_name);
194039 p_dir += "=";
194040 if (partition_value.IsNull()) {
194041 p_dir += "__HIVE_DEFAULT_PARTITION__";
194042 } else {
194043 p_dir += HivePartitioning::Escape(partition_value.ToString());
194044 }
194045 path = fs.JoinPath(path, p_dir);
194046 CreateDir(path, fs);
194047 }
194048 }
194049 return path;
194050 }
static DUCKDB_API string Escape(const string &input)
Escape a hive partition key or value using URL encoding.
Definition duckdb.cpp:77845
int cols
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ AddFile()

optional_ptr< CopyToFileInfo > duckdb::CopyToFunctionGlobalState::AddFile ( const StorageLockKey & l,
const string & file_name,
CopyFunctionReturnType return_type )
inline
194053 {
194054 D_ASSERT(l.GetType() == StorageLockType::EXCLUSIVE);
194055 auto file_info = make_uniq<CopyToFileInfo>(file_name);
194056 optional_ptr<CopyToFileInfo> result;
194057 if (return_type == CopyFunctionReturnType::WRITTEN_FILE_STATISTICS) {
194058 file_info->file_stats = make_uniq<CopyFunctionFileStatistics>();
194059 result = file_info.get();
194060 }
194061 written_files.push_back(std::move(file_info));
194062 return result;
194063 }
vector< unique_ptr< CopyToFileInfo > > written_files
Written file info and stats.
Definition duckdb.cpp:193991

◆ FinalizePartition()

void duckdb::CopyToFunctionGlobalState::FinalizePartition ( ClientContext & context,
const PhysicalCopyToFile & op,
PartitionWriteInfo & info )
inline
194065 {
194066 if (!info.global_state) {
194067 // already finalized
194068 return;
194069 }
194070 // finalize the partition
194071 op.function.copy_to_finalize(context, *op.bind_data, *info.global_state);
194072 info.global_state.reset();
194073 }

◆ FinalizePartitions()

void duckdb::CopyToFunctionGlobalState::FinalizePartitions ( ClientContext & context,
const PhysicalCopyToFile & op )
inline
194075 {
194076 // finalize any remaining partitions
194077 for (auto &entry : active_partitioned_writes) {
194078 FinalizePartition(context, op, *entry.second);
194079 }
194080 }
vector_of_value_map_t< unique_ptr< PartitionWriteInfo > > active_partitioned_writes
The active writes per partition (for partitioned write).
Definition duckdb.cpp:194172

◆ GetPartitionWriteInfo()

PartitionWriteInfo & duckdb::CopyToFunctionGlobalState::GetPartitionWriteInfo ( ExecutionContext & context,
const PhysicalCopyToFile & op,
const vector< Value > & values )
inline
194083 {
194084 auto global_lock = lock.GetExclusiveLock();
194085 // check if we have already started writing this partition
194086 auto active_write_entry = active_partitioned_writes.find(values);
194087 if (active_write_entry != active_partitioned_writes.end()) {
194088 // we have - continue writing in this partition
194089 active_write_entry->second->active_writes++;
194090 return *active_write_entry->second;
194091 }
194092 // check if we need to close any writers before we can continue
194094 // we need to! try to close writers
194095 for (auto &entry : active_partitioned_writes) {
194096 if (entry.second->active_writes == 0) {
194097 // we can evict this entry - evict the partition
194098 FinalizePartition(context.client, op, *entry.second);
194099 ++previous_partitions[entry.first];
194100 active_partitioned_writes.erase(entry.first);
194101 break;
194102 }
194103 }
194104 }
194105 idx_t offset = 0;
194106 if (op.hive_file_pattern) {
194107 auto prev_offset = previous_partitions.find(values);
194108 if (prev_offset != previous_partitions.end()) {
194109 offset = prev_offset->second;
194110 }
194111 } else {
194112 offset = global_offset++;
194113 }
194114 auto &fs = FileSystem::GetFileSystem(context.client);
194115 // Create a writer for the current file
194116 auto trimmed_path = op.GetTrimmedPath(context.client);
194117 string hive_path =
194118 GetOrCreateDirectory(op.partition_columns, op.hive_file_pattern, op.names, values, trimmed_path, fs);
194119 string full_path(op.filename_pattern.CreateFilename(fs, hive_path, op.file_extension, offset));
194120 if (op.overwrite_mode == CopyOverwriteMode::COPY_APPEND) {
194121 // when appending, we first check if the file exists
194122 while (fs.FileExists(full_path)) {
194123 // file already exists - re-generate name
194124 if (!op.filename_pattern.HasUUID()) {
194125 throw InternalException("CopyOverwriteMode::COPY_APPEND without {uuid} - and file exists");
194126 }
194127 full_path = op.filename_pattern.CreateFilename(fs, hive_path, op.file_extension, offset);
194128 }
194129 }
194130 created_files.push_back(full_path);
194131 optional_ptr<CopyToFileInfo> written_file_info;
194132 if (op.return_type != CopyFunctionReturnType::CHANGED_ROWS) {
194133 written_file_info = AddFile(*global_lock, full_path, op.return_type);
194134 }
194135 // initialize writes
194136 auto info = make_uniq<PartitionWriteInfo>();
194137 info->global_state = op.function.copy_to_initialize_global(context.client, *op.bind_data, full_path);
194138 if (written_file_info) {
194139 // set up the file stats for the copy
194140 op.function.copy_to_get_written_statistics(context.client, *op.bind_data, *info->global_state,
194141 *written_file_info->file_stats);
194142
194143 // set the partition info
194144 vector<Value> partition_keys;
194145 vector<Value> partition_values;
194146 for (idx_t i = 0; i < op.partition_columns.size(); i++) {
194147 const auto &partition_col_name = op.names[op.partition_columns[i]];
194148 const auto &partition_value = values[i];
194149 partition_keys.emplace_back(partition_col_name);
194150 partition_values.push_back(partition_value.DefaultCastAs(LogicalType::VARCHAR));
194151 }
194152 written_file_info->partition_keys = Value::MAP(LogicalType::VARCHAR, LogicalType::VARCHAR,
194153 std::move(partition_keys), std::move(partition_values));
194154 }
194155 if (op.function.initialize_operator) {
194156 op.function.initialize_operator(*info->global_state, op);
194157 }
194158 auto &result = *info;
194159 info->active_writes = 1;
194160 // store in active write map
194161 active_partitioned_writes.insert(make_pair(values, std::move(info)));
194162 return result;
194163 }
static DUCKDB_API Value MAP(const LogicalType &child_type, vector< Value > values)
Create a map value with the given entries.
Definition duckdb.cpp:132466

◆ FinishPartitionWrite()

void duckdb::CopyToFunctionGlobalState::FinishPartitionWrite ( PartitionWriteInfo & info)
inline
194165 {
194166 auto global_lock = lock.GetExclusiveLock();
194167 info.active_writes--;
194168 }

The documentation for this class was generated from the following file: