|
16 | 16 |
|
17 | 17 | #include "paimon/core/operation/internal_read_context.h" |
18 | 18 |
|
| 19 | +#include <optional> |
19 | 20 | #include <utility> |
20 | 21 |
|
| 22 | +#include "paimon/common/predicate/compound_predicate_impl.h" |
| 23 | +#include "paimon/common/predicate/leaf_predicate_impl.h" |
21 | 24 | #include "paimon/common/predicate/predicate_validator.h" |
22 | 25 | #include "paimon/common/table/special_fields.h" |
23 | 26 | #include "paimon/common/types/data_field.h" |
24 | 27 | #include "paimon/core/schema/arrow_schema_validator.h" |
| 28 | +#include "paimon/predicate/function.h" |
25 | 29 | #include "paimon/status.h" |
26 | 30 |
|
27 | 31 | namespace arrow { |
28 | 32 | class Schema; |
29 | 33 | } // namespace arrow |
30 | 34 |
|
31 | 35 | namespace paimon { |
| 36 | +namespace { |
| 37 | +// Build a map from a field's position in `table_schema` (the latest table schema, the |
| 38 | +// index space upstream predicates are typically constructed against) to its position |
| 39 | +// in `read_data_fields` (the projected read schema). Field identity is the stable |
| 40 | +// field id, so the mapping survives column renames within the same schema. Read-only |
| 41 | +// special fields (RowId / SequenceNumber / etc.) have no analogue in the table |
| 42 | +// schema and are skipped — user-supplied predicates do not reference them. |
| 43 | +std::map<int32_t, int32_t> BuildLatestToReadIdxMapping( |
| 44 | + const TableSchema& table_schema, const std::vector<DataField>& read_data_fields) { |
| 45 | + std::map<int32_t, int32_t> id_to_latest_idx; |
| 46 | + const auto& table_fields = table_schema.Fields(); |
| 47 | + for (size_t latest_idx = 0; latest_idx < table_fields.size(); latest_idx++) { |
| 48 | + id_to_latest_idx[table_fields[latest_idx].Id()] = static_cast<int32_t>(latest_idx); |
| 49 | + } |
| 50 | + std::map<int32_t, int32_t> mapping; |
| 51 | + for (size_t read_idx = 0; read_idx < read_data_fields.size(); read_idx++) { |
| 52 | + auto iter = id_to_latest_idx.find(read_data_fields[read_idx].Id()); |
| 53 | + if (iter != id_to_latest_idx.end()) { |
| 54 | + mapping[iter->second] = static_cast<int32_t>(read_idx); |
| 55 | + } |
| 56 | + } |
| 57 | + return mapping; |
| 58 | +} |
| 59 | + |
| 60 | +// Project `predicate` onto the read schema, rewriting each leaf's field index via |
| 61 | +// `latest_to_read_idx` (predicate's source index space → read schema position). |
| 62 | +// |
| 63 | +// Inclusive semantics, matching paimon Java's PredicateProjectionConverter: |
| 64 | +// - Leaf whose field is not in the read schema: dropped (returns nullopt). |
| 65 | +// - AND: drop non-projectable children, keep the rest. Safe because if `A AND B` |
| 66 | +// holds for a row, A holds too; the projected predicate is a superset |
| 67 | +// (necessary, not sufficient). |
| 68 | +// - OR: every child must be projectable. If any child is dropped, drop the |
| 69 | +// whole OR — otherwise a row that only satisfied the dropped branch would |
| 70 | +// falsely pass. |
| 71 | +// - Predicates without a field index (e.g. full-text / vector search) flow |
| 72 | +// through unchanged. |
| 73 | +Result<std::optional<std::shared_ptr<Predicate>>> ProjectPredicate( |
| 74 | + const std::map<int32_t, int32_t>& latest_to_read_idx, |
| 75 | + const std::shared_ptr<Predicate>& predicate) { |
| 76 | + if (auto leaf = std::dynamic_pointer_cast<LeafPredicateImpl>(predicate)) { |
| 77 | + auto iter = latest_to_read_idx.find(leaf->FieldIndex()); |
| 78 | + if (iter == latest_to_read_idx.end()) { |
| 79 | + return std::optional<std::shared_ptr<Predicate>>{}; |
| 80 | + } |
| 81 | + if (iter->second == leaf->FieldIndex()) { |
| 82 | + return std::optional<std::shared_ptr<Predicate>>{predicate}; |
| 83 | + } |
| 84 | + return std::optional<std::shared_ptr<Predicate>>{ |
| 85 | + std::static_pointer_cast<Predicate>(leaf->NewLeafPredicate(iter->second))}; |
| 86 | + } |
| 87 | + if (auto compound = std::dynamic_pointer_cast<CompoundPredicateImpl>(predicate)) { |
| 88 | + const bool is_and = compound->GetFunction().GetType() == Function::Type::AND; |
| 89 | + std::vector<std::shared_ptr<Predicate>> projected_children; |
| 90 | + projected_children.reserve(compound->Children().size()); |
| 91 | + bool any_changed = false; |
| 92 | + for (const auto& child : compound->Children()) { |
| 93 | + PAIMON_ASSIGN_OR_RAISE(std::optional<std::shared_ptr<Predicate>> projected_child, |
| 94 | + ProjectPredicate(latest_to_read_idx, child)); |
| 95 | + if (!projected_child.has_value()) { |
| 96 | + if (!is_and) { |
| 97 | + return std::optional<std::shared_ptr<Predicate>>{}; |
| 98 | + } |
| 99 | + any_changed = true; |
| 100 | + continue; |
| 101 | + } |
| 102 | + if (projected_child.value() != child) { |
| 103 | + any_changed = true; |
| 104 | + } |
| 105 | + projected_children.push_back(std::move(projected_child.value())); |
| 106 | + } |
| 107 | + if (projected_children.empty()) { |
| 108 | + return std::optional<std::shared_ptr<Predicate>>{}; |
| 109 | + } |
| 110 | + if (projected_children.size() == 1) { |
| 111 | + return std::optional<std::shared_ptr<Predicate>>{std::move(projected_children[0])}; |
| 112 | + } |
| 113 | + if (!any_changed) { |
| 114 | + return std::optional<std::shared_ptr<Predicate>>{predicate}; |
| 115 | + } |
| 116 | + return std::optional<std::shared_ptr<Predicate>>{std::static_pointer_cast<Predicate>( |
| 117 | + compound->NewCompoundPredicate(projected_children))}; |
| 118 | + } |
| 119 | + return std::optional<std::shared_ptr<Predicate>>{predicate}; |
| 120 | +} |
| 121 | + |
| 122 | +Result<std::shared_ptr<Predicate>> ProjectAndValidatePredicate( |
| 123 | + const arrow::Schema& read_schema, const std::map<int32_t, int32_t>& latest_to_read_idx, |
| 124 | + const std::shared_ptr<Predicate>& predicate) { |
| 125 | + PAIMON_ASSIGN_OR_RAISE(std::optional<std::shared_ptr<Predicate>> projected, |
| 126 | + ProjectPredicate(latest_to_read_idx, predicate)); |
| 127 | + if (!projected.has_value()) { |
| 128 | + return std::shared_ptr<Predicate>{}; |
| 129 | + } |
| 130 | + PAIMON_RETURN_NOT_OK(PredicateValidator::ValidatePredicateWithSchema( |
| 131 | + read_schema, projected.value(), /*validate_field_idx=*/true)); |
| 132 | + PAIMON_RETURN_NOT_OK(PredicateValidator::ValidatePredicateWithLiterals(projected.value())); |
| 133 | + return projected.value(); |
| 134 | +} |
| 135 | +} // namespace |
| 136 | + |
32 | 137 | Result<std::unique_ptr<InternalReadContext>> InternalReadContext::Create( |
33 | 138 | const std::shared_ptr<ReadContext>& context, const std::shared_ptr<TableSchema>& table_schema, |
34 | 139 | const std::map<std::string, std::string>& options) { |
@@ -95,34 +200,51 @@ Result<std::unique_ptr<InternalReadContext>> InternalReadContext::Create( |
95 | 200 | auto read_schema = DataField::ConvertDataFieldsToArrowSchema(read_data_fields); |
96 | 201 | // validate read schema to avoid redundant fields |
97 | 202 | PAIMON_RETURN_NOT_OK(ArrowSchemaValidator::ValidateSchemaWithFieldId(*read_schema)); |
98 | | - // validate predicate |
| 203 | + // Project the upstream predicate onto `read_schema`. Predicates carry field indices |
| 204 | + // pointing into the latest table schema; rewrite them to positions in `read_schema_` |
| 205 | + // so downstream readers can apply them directly. |
| 206 | + std::shared_ptr<Predicate> projected_predicate; |
99 | 207 | if (context->GetPredicate()) { |
100 | | - PAIMON_RETURN_NOT_OK(PredicateValidator::ValidatePredicateWithSchema( |
101 | | - *read_schema, context->GetPredicate(), /*validate_field_idx=*/true)); |
102 | | - PAIMON_RETURN_NOT_OK( |
103 | | - PredicateValidator::ValidatePredicateWithLiterals(context->GetPredicate())); |
| 208 | + auto latest_to_read_idx = BuildLatestToReadIdxMapping(*table_schema, read_data_fields); |
| 209 | + PAIMON_ASSIGN_OR_RAISE( |
| 210 | + projected_predicate, |
| 211 | + ProjectAndValidatePredicate(*read_schema, latest_to_read_idx, context->GetPredicate())); |
104 | 212 | } |
105 | 213 |
|
106 | | - return std::unique_ptr<InternalReadContext>( |
107 | | - new InternalReadContext(context, table_schema, read_schema, core_options)); |
| 214 | + return std::unique_ptr<InternalReadContext>(new InternalReadContext( |
| 215 | + context, table_schema, read_schema, core_options, std::move(projected_predicate))); |
108 | 216 | } |
109 | 217 |
|
110 | 218 | InternalReadContext::InternalReadContext(const std::shared_ptr<ReadContext>& read_context, |
111 | 219 | const std::shared_ptr<TableSchema>& table_schema, |
112 | 220 | const std::shared_ptr<arrow::Schema>& read_schema, |
113 | | - const CoreOptions& options) |
| 221 | + const CoreOptions& options, |
| 222 | + std::shared_ptr<Predicate> projected_predicate) |
114 | 223 | : read_context_(read_context), |
115 | 224 | table_schema_(table_schema), |
116 | 225 | read_schema_(read_schema), |
117 | | - options_(options) {} |
| 226 | + options_(options), |
| 227 | + projected_predicate_(std::move(projected_predicate)) {} |
118 | 228 |
|
119 | 229 | Result<std::shared_ptr<InternalReadContext>> InternalReadContext::CreateWithSchema( |
120 | 230 | const std::shared_ptr<InternalReadContext>& original, |
121 | 231 | const std::shared_ptr<arrow::Schema>& new_read_schema) { |
122 | | - // Create a new InternalReadContext sharing all properties except read_schema. |
123 | | - // The new read_schema is the minimal column set for COUNT(*). |
124 | | - return std::shared_ptr<InternalReadContext>(new InternalReadContext( |
125 | | - original->read_context_, original->table_schema_, new_read_schema, original->options_)); |
| 232 | + // The wrapped read_context still holds the caller-supplied predicate (in the latest |
| 233 | + // table schema's index space). Re-project it against `new_read_schema` directly, |
| 234 | + // rather than re-projecting the already-projected predicate sitting on `original`. |
| 235 | + std::shared_ptr<Predicate> projected_predicate; |
| 236 | + if (original->read_context_->GetPredicate()) { |
| 237 | + PAIMON_ASSIGN_OR_RAISE(std::vector<DataField> new_read_data_fields, |
| 238 | + DataField::ConvertArrowSchemaToDataFields(new_read_schema)); |
| 239 | + auto latest_to_read_idx = |
| 240 | + BuildLatestToReadIdxMapping(*original->table_schema_, new_read_data_fields); |
| 241 | + PAIMON_ASSIGN_OR_RAISE(projected_predicate, ProjectAndValidatePredicate( |
| 242 | + *new_read_schema, latest_to_read_idx, |
| 243 | + original->read_context_->GetPredicate())); |
| 244 | + } |
| 245 | + return std::shared_ptr<InternalReadContext>( |
| 246 | + new InternalReadContext(original->read_context_, original->table_schema_, new_read_schema, |
| 247 | + original->options_, std::move(projected_predicate))); |
126 | 248 | } |
127 | 249 |
|
128 | 250 | } // namespace paimon |
0 commit comments