-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Add support for evaluating Iceberg partition transforms #15440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
PingLiuPing
wants to merge
1
commit into
facebookincubator:main
from
PingLiuPing:lp_iceberg_transform_eval
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "velox/connectors/hive/iceberg/TransformEvaluator.h" | ||
|
|
||
| #include "velox/expression/Expr.h" | ||
|
|
||
| namespace facebook::velox::connector::hive::iceberg { | ||
|
|
||
| TransformEvaluator::TransformEvaluator( | ||
| const std::vector<core::TypedExprPtr>& expressions, | ||
| const ConnectorQueryCtx* connectorQueryCtx) | ||
| : connectorQueryCtx_(connectorQueryCtx) { | ||
| VELOX_CHECK_NOT_NULL(connectorQueryCtx_); | ||
| exprSet_ = connectorQueryCtx_->expressionEvaluator()->compile(expressions); | ||
| VELOX_CHECK_NOT_NULL(exprSet_); | ||
| } | ||
|
|
||
| std::vector<VectorPtr> TransformEvaluator::evaluate( | ||
| const RowVectorPtr& input) const { | ||
| const auto numRows = input->size(); | ||
| const auto numExpressions = exprSet_->exprs().size(); | ||
|
|
||
| std::vector<VectorPtr> results(numExpressions); | ||
| SelectivityVector rows(numRows); | ||
|
|
||
| // Evaluate all expressions in one pass. | ||
| connectorQueryCtx_->expressionEvaluator()->evaluate( | ||
| exprSet_.get(), rows, *input, results); | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| } // namespace facebook::velox::connector::hive::iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include "velox/connectors/Connector.h" | ||
| #include "velox/core/QueryCtx.h" | ||
| #include "velox/expression/Expr.h" | ||
|
|
||
| namespace facebook::velox::connector::hive::iceberg { | ||
|
|
||
| /// Evaluates multiple expressions efficiently using batch evaluation. | ||
| /// Expressions are compiled once in the constructor and reused across multiple | ||
| /// input batches. | ||
| class TransformEvaluator { | ||
| public: | ||
| /// Creates an evaluator with the given expressions and connector query | ||
| /// context. Compiles the expressions once for reuse across multiple | ||
| /// evaluations. | ||
| /// | ||
| /// @param expressions Vector of typed expressions to evaluate. These are | ||
| /// typically built using TransformExprBuilder::toExpressions() for Iceberg | ||
| /// partition transforms, but can be any valid Velox expressions. The | ||
| /// expressions are compiled once during construction. | ||
| /// @param connectorQueryCtx Connector query context providing access to the | ||
| /// expression evaluator (for compilation and evaluation) and memory pool. | ||
| /// Must remain valid for the lifetime of this TransformEvaluator. | ||
| TransformEvaluator( | ||
| const std::vector<core::TypedExprPtr>& expressions, | ||
| const ConnectorQueryCtx* connectorQueryCtx); | ||
|
|
||
| /// Evaluates all expressions on the input data in a single pass. | ||
| /// Uses the pre-compiled ExprSet from the constructor for efficiency. | ||
| /// | ||
| /// The input RowType must match the RowType used when building the | ||
| /// expressions (passed to TransformExprBuilder::toExpressions). The column | ||
| /// positions, names and types must align. Create new TransformEvaluator for | ||
| /// input that has different RowType with the one when building the | ||
| /// expressions. | ||
| /// | ||
| /// @param input Input row vector containing the source data. Must have the | ||
| /// same RowType (column positions, names and types) as used when building the | ||
| /// expressions in the constructor. | ||
| /// @return Vector of result columns, one for each expression, in the same | ||
| /// order as the expressions provided to the constructor. | ||
| std::vector<VectorPtr> evaluate(const RowVectorPtr& input) const; | ||
|
|
||
| private: | ||
| const ConnectorQueryCtx* connectorQueryCtx_; | ||
| std::unique_ptr<exec::ExprSet> exprSet_; | ||
| }; | ||
|
|
||
| } // namespace facebook::velox::connector::hive::iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #include "velox/connectors/hive/iceberg/TransformExprBuilder.h" | ||
| #include "velox/core/Expressions.h" | ||
|
|
||
| namespace facebook::velox::connector::hive::iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| /// Converts a single partition field to a typed expression. | ||
| /// | ||
| /// Builds an expression tree for one partition transform. Identity transforms | ||
| /// become FieldAccessTypedExpr, while other transforms (bucket, truncate, | ||
| /// year, month, day, hour) become CallTypedExpr with appropriate function | ||
| /// names and parameters. | ||
| /// | ||
| /// @param field Partition field containing transform type, source column | ||
| /// type, and optional parameter (e.g., bucket count, truncate width). | ||
| /// @param inputFieldName Name of the source column in the input RowVector. | ||
| /// @return Typed expression representing the transform. | ||
| core::TypedExprPtr toExpression( | ||
| const IcebergPartitionSpec::Field& field, | ||
| const std::string& inputFieldName) { | ||
| // For identity transform, just return a field access expression. | ||
| if (field.transformType == TransformType::kIdentity) { | ||
| return std::make_shared<core::FieldAccessTypedExpr>( | ||
| field.type, inputFieldName); | ||
| } | ||
|
|
||
| // For other transforms, build a CallTypedExpr with the appropriate function. | ||
| std::string functionName; | ||
| switch (field.transformType) { | ||
| case TransformType::kBucket: | ||
| functionName = kBucketFunction; | ||
| break; | ||
| case TransformType::kTruncate: | ||
| functionName = kTruncateFunction; | ||
| break; | ||
| case TransformType::kYear: | ||
| functionName = kYearFunction; | ||
| break; | ||
| case TransformType::kMonth: | ||
| functionName = kMonthFunction; | ||
| break; | ||
| case TransformType::kDay: | ||
| functionName = kDayFunction; | ||
| break; | ||
| case TransformType::kHour: | ||
| functionName = kHourFunction; | ||
| break; | ||
| case TransformType::kIdentity: | ||
| break; | ||
| } | ||
|
|
||
| // Build the expression arguments. | ||
| std::vector<core::TypedExprPtr> exprArgs; | ||
| if (field.parameter.has_value()) { | ||
| exprArgs.emplace_back( | ||
| std::make_shared<core::ConstantTypedExpr>( | ||
| INTEGER(), Variant(field.parameter.value()))); | ||
| } | ||
| exprArgs.emplace_back( | ||
| std::make_shared<core::FieldAccessTypedExpr>(field.type, inputFieldName)); | ||
|
|
||
| return std::make_shared<core::CallTypedExpr>( | ||
| field.resultType(), std::move(exprArgs), functionName); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::vector<core::TypedExprPtr> TransformExprBuilder::toExpressions( | ||
| const IcebergPartitionSpecPtr& partitionSpec, | ||
| const std::vector<column_index_t>& partitionChannels, | ||
| const RowTypePtr& inputType) { | ||
| VELOX_CHECK_EQ( | ||
| partitionSpec->fields.size(), | ||
| partitionChannels.size(), | ||
| "Number of partition fields must match number of partition channels"); | ||
|
|
||
| const auto numTransforms = partitionChannels.size(); | ||
| std::vector<core::TypedExprPtr> transformExprs; | ||
| transformExprs.reserve(numTransforms); | ||
|
|
||
| for (auto i = 0; i < numTransforms; i++) { | ||
| const auto channel = partitionChannels[i]; | ||
| transformExprs.emplace_back( | ||
| toExpression(partitionSpec->fields.at(i), inputType->nameOf(channel))); | ||
| } | ||
|
|
||
| return transformExprs; | ||
| } | ||
|
|
||
| } // namespace facebook::velox::connector::hive::iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "velox/connectors/hive/iceberg/PartitionSpec.h" | ||
| #include "velox/expression/Expr.h" | ||
|
|
||
| namespace facebook::velox::connector::hive::iceberg { | ||
|
|
||
| /// Converts Iceberg partition specification to Velox expressions. | ||
| class TransformExprBuilder { | ||
| public: | ||
| /// Converts partition specification to a list of typed expressions. | ||
| /// | ||
| /// @param partitionSpec Iceberg partition specification containing transform | ||
| /// definitions for each partition field. | ||
| /// @param partitionChannels Column indices (0-based) in the input RowVector | ||
| /// that correspond to each partition field. Must have the same size as | ||
| /// partitionSpec->fields. Provides the positional mapping from partition spec | ||
| /// fields to input RowVector columns. | ||
| /// @param inputType The row type of the input data. This is necessary for | ||
| /// building expressions because the column names in partitionSpec reference | ||
| /// table schema names, which might not match the column names in inputType | ||
| /// (e.g., inputType may use generated names like c0, c1, c2). The | ||
| /// FieldAccessTypedExpr must be built using the actual column names from | ||
| /// inputType that will be present at runtime. The partitionChannels provide | ||
| /// the positional mapping to locate the correct columns. | ||
| /// @return Vector of typed expressions, one for each partition field. | ||
| static std::vector<core::TypedExprPtr> toExpressions( | ||
| const IcebergPartitionSpecPtr& partitionSpec, | ||
| const std::vector<column_index_t>& partitionChannels, | ||
| const RowTypePtr& inputType); | ||
| }; | ||
|
|
||
| } // namespace facebook::velox::connector::hive::iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mbasmanova Have to add this to fix the building break on Linux. Macos is fine without this change.