|
| 1 | +// Copyright 2026 Arash Hatami |
| 2 | + |
| 3 | +#include "extract_path_segments.hpp" |
| 4 | + |
| 5 | +#include "../utils/url_helpers.hpp" |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace duckdb { |
| 11 | +namespace netquack { |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Pure logic: extract path from URL, then split into non-empty segments |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +std::vector<std::string> ExtractPathSegments(const std::string &input) { |
| 17 | + std::vector<std::string> segments; |
| 18 | + |
| 19 | + if (input.empty()) { |
| 20 | + return segments; |
| 21 | + } |
| 22 | + |
| 23 | + const char *data = input.data(); |
| 24 | + size_t size = input.size(); |
| 25 | + const char *pos = data; |
| 26 | + const char *end = pos + size; |
| 27 | + |
| 28 | + // Locate the path start (same logic as ExtractPath) |
| 29 | + pos = find_first_symbols<'/'>(pos, end); |
| 30 | + if (end == pos) { |
| 31 | + return segments; |
| 32 | + } |
| 33 | + |
| 34 | + bool has_subsequent_slash = pos + 1 < end && pos[1] == '/'; |
| 35 | + if (has_subsequent_slash) { |
| 36 | + pos = find_first_symbols<'/'>(pos + 2, end); |
| 37 | + if (end == pos) { |
| 38 | + return segments; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Path ends at '?' or '#' |
| 43 | + const char *path_end = find_first_symbols<'?', '#'>(pos, end); |
| 44 | + |
| 45 | + // Skip leading '/' |
| 46 | + if (pos < path_end && *pos == '/') { |
| 47 | + ++pos; |
| 48 | + } |
| 49 | + |
| 50 | + // Split on '/' |
| 51 | + const char *seg_start = pos; |
| 52 | + for (const char *cur = pos; cur <= path_end; ++cur) { |
| 53 | + if (cur == path_end || *cur == '/') { |
| 54 | + if (cur > seg_start) { |
| 55 | + segments.emplace_back(seg_start, cur - seg_start); |
| 56 | + } |
| 57 | + seg_start = cur + 1; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return segments; |
| 62 | +} |
| 63 | + |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | +// Table function callbacks |
| 66 | +// --------------------------------------------------------------------------- |
| 67 | +struct ExtractPathSegmentsData : public TableFunctionData {}; |
| 68 | + |
| 69 | +struct ExtractPathSegmentsLocalState : public LocalTableFunctionState { |
| 70 | + std::vector<std::string> segments; |
| 71 | + idx_t current_idx = 0; |
| 72 | + bool done = false; |
| 73 | +}; |
| 74 | + |
| 75 | +unique_ptr<FunctionData> ExtractPathSegmentsFunc::Bind(ClientContext &context, TableFunctionBindInput &input, |
| 76 | + vector<LogicalType> &return_types, vector<string> &names) { |
| 77 | + // Output columns: segment_index (1-based) and segment |
| 78 | + return_types.emplace_back(LogicalType(LogicalTypeId::INTEGER)); |
| 79 | + names.emplace_back("segment_index"); |
| 80 | + |
| 81 | + return_types.emplace_back(LogicalType(LogicalTypeId::VARCHAR)); |
| 82 | + names.emplace_back("segment"); |
| 83 | + |
| 84 | + return make_uniq<ExtractPathSegmentsData>(); |
| 85 | +} |
| 86 | + |
| 87 | +unique_ptr<LocalTableFunctionState> ExtractPathSegmentsFunc::InitLocal(ExecutionContext &context, |
| 88 | + TableFunctionInitInput &input, |
| 89 | + GlobalTableFunctionState *global_state_p) { |
| 90 | + return make_uniq<ExtractPathSegmentsLocalState>(); |
| 91 | +} |
| 92 | + |
| 93 | +OperatorResultType ExtractPathSegmentsFunc::Function(ExecutionContext &context, TableFunctionInput &data_p, |
| 94 | + DataChunk &input, DataChunk &output) { |
| 95 | + auto &local_state = data_p.local_state->Cast<ExtractPathSegmentsLocalState>(); |
| 96 | + |
| 97 | + // Already finished outputting for this input row — request next input |
| 98 | + if (local_state.done) { |
| 99 | + local_state.done = false; |
| 100 | + local_state.segments.clear(); |
| 101 | + local_state.current_idx = 0; |
| 102 | + return OperatorResultType::NEED_MORE_INPUT; |
| 103 | + } |
| 104 | + |
| 105 | + // Continue outputting remaining segments from a previous HAVE_MORE_OUTPUT |
| 106 | + if (!local_state.segments.empty() && local_state.current_idx < local_state.segments.size()) { |
| 107 | + idx_t count = 0; |
| 108 | + while (local_state.current_idx < local_state.segments.size() && count < STANDARD_VECTOR_SIZE) { |
| 109 | + output.data[0].SetValue(count, Value::INTEGER(static_cast<int32_t>(local_state.current_idx + 1))); |
| 110 | + output.data[1].SetValue(count, Value(local_state.segments[local_state.current_idx])); |
| 111 | + ++local_state.current_idx; |
| 112 | + ++count; |
| 113 | + } |
| 114 | + output.SetCardinality(count); |
| 115 | + |
| 116 | + if (local_state.current_idx >= local_state.segments.size()) { |
| 117 | + local_state.done = true; |
| 118 | + } |
| 119 | + return OperatorResultType::HAVE_MORE_OUTPUT; |
| 120 | + } |
| 121 | + |
| 122 | + // Parse the URL from the input chunk |
| 123 | + auto url_value = input.data[0].GetValue(0); |
| 124 | + if (url_value.IsNull()) { |
| 125 | + output.SetCardinality(0); |
| 126 | + return OperatorResultType::NEED_MORE_INPUT; |
| 127 | + } |
| 128 | + |
| 129 | + auto url = url_value.GetValue<string>(); |
| 130 | + local_state.segments = ExtractPathSegments(url); |
| 131 | + local_state.current_idx = 0; |
| 132 | + |
| 133 | + if (local_state.segments.empty()) { |
| 134 | + output.SetCardinality(0); |
| 135 | + return OperatorResultType::NEED_MORE_INPUT; |
| 136 | + } |
| 137 | + |
| 138 | + // Output as many segments as we can |
| 139 | + idx_t count = 0; |
| 140 | + while (local_state.current_idx < local_state.segments.size() && count < STANDARD_VECTOR_SIZE) { |
| 141 | + output.data[0].SetValue(count, Value::INTEGER(static_cast<int32_t>(local_state.current_idx + 1))); |
| 142 | + output.data[1].SetValue(count, Value(local_state.segments[local_state.current_idx])); |
| 143 | + ++local_state.current_idx; |
| 144 | + ++count; |
| 145 | + } |
| 146 | + output.SetCardinality(count); |
| 147 | + |
| 148 | + if (local_state.current_idx >= local_state.segments.size()) { |
| 149 | + local_state.done = true; |
| 150 | + } |
| 151 | + return OperatorResultType::HAVE_MORE_OUTPUT; |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace netquack |
| 155 | +} // namespace duckdb |
0 commit comments