|
| 1 | +// Copyright (C) 2017-2018 Dremio Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "gandiva/filter.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <utility> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "codegen/bitmap_accumulator.h" |
| 22 | +#include "codegen/expr_validator.h" |
| 23 | +#include "codegen/llvm_generator.h" |
| 24 | +#include "gandiva/condition.h" |
| 25 | +#include "gandiva/status.h" |
| 26 | + |
| 27 | +namespace gandiva { |
| 28 | + |
| 29 | +Filter::Filter(std::unique_ptr<LLVMGenerator> llvm_generator, SchemaPtr schema, |
| 30 | + std::shared_ptr<Configuration> configuration) |
| 31 | + : llvm_generator_(std::move(llvm_generator)), |
| 32 | + schema_(schema), |
| 33 | + configuration_(configuration) {} |
| 34 | + |
| 35 | +Status Filter::Make(SchemaPtr schema, ConditionPtr condition, |
| 36 | + std::shared_ptr<Configuration> configuration, |
| 37 | + std::shared_ptr<Filter> *filter) { |
| 38 | + GANDIVA_RETURN_FAILURE_IF_FALSE(schema != nullptr, |
| 39 | + Status::Invalid("schema cannot be null")); |
| 40 | + GANDIVA_RETURN_FAILURE_IF_FALSE(condition != nullptr, |
| 41 | + Status::Invalid("condition cannot be null")); |
| 42 | + GANDIVA_RETURN_FAILURE_IF_FALSE(configuration != nullptr, |
| 43 | + Status::Invalid("configuration cannot be null")); |
| 44 | + // Build LLVM generator, and generate code for the specified expression |
| 45 | + std::unique_ptr<LLVMGenerator> llvm_gen; |
| 46 | + Status status = LLVMGenerator::Make(configuration, &llvm_gen); |
| 47 | + GANDIVA_RETURN_NOT_OK(status); |
| 48 | + |
| 49 | + // Run the validation on the expression. |
| 50 | + // Return if the expression is invalid since we will not be able to process further. |
| 51 | + ExprValidator expr_validator(llvm_gen->types(), schema); |
| 52 | + status = expr_validator.Validate(condition); |
| 53 | + GANDIVA_RETURN_NOT_OK(status); |
| 54 | + |
| 55 | + status = llvm_gen->Build({condition}); |
| 56 | + GANDIVA_RETURN_NOT_OK(status); |
| 57 | + |
| 58 | + // Instantiate the filter with the completely built llvm generator |
| 59 | + *filter = std::make_shared<Filter>(std::move(llvm_gen), schema, configuration); |
| 60 | + return Status::OK(); |
| 61 | +} |
| 62 | + |
| 63 | +Status Filter::Evaluate(const arrow::RecordBatch &batch, |
| 64 | + std::shared_ptr<SelectionVector> out_selection) { |
| 65 | + if (!batch.schema()->Equals(*schema_)) { |
| 66 | + return Status::Invalid("Schema in RecordBatch must match the schema in Make()"); |
| 67 | + } |
| 68 | + if (batch.num_rows() == 0) { |
| 69 | + return Status::Invalid("RecordBatch must be non-empty."); |
| 70 | + } |
| 71 | + if (out_selection == nullptr) { |
| 72 | + return Status::Invalid("out_selection must be non-null."); |
| 73 | + } |
| 74 | + if (out_selection->GetMaxSlots() < batch.num_rows()) { |
| 75 | + std::stringstream ss; |
| 76 | + ss << "out_selection has " << out_selection->GetMaxSlots() |
| 77 | + << " slots, which is less than the batch size " << batch.num_rows(); |
| 78 | + return Status::Invalid(ss.str()); |
| 79 | + } |
| 80 | + |
| 81 | + // Allocate three local_bitmaps (one for output, one for validity, one to compute the |
| 82 | + // intersection). |
| 83 | + LocalBitMapsHolder bitmaps(batch.num_rows(), 3 /*local_bitmaps*/); |
| 84 | + int bitmap_size = bitmaps.GetLocalBitMapSize(); |
| 85 | + |
| 86 | + auto validity = std::make_shared<arrow::Buffer>(bitmaps.GetLocalBitMap(0), bitmap_size); |
| 87 | + auto value = std::make_shared<arrow::Buffer>(bitmaps.GetLocalBitMap(1), bitmap_size); |
| 88 | + auto array_data = |
| 89 | + arrow::ArrayData::Make(arrow::boolean(), batch.num_rows(), {validity, value}); |
| 90 | + |
| 91 | + // Execute the expression(s). |
| 92 | + auto status = llvm_generator_->Execute(batch, {array_data}); |
| 93 | + GANDIVA_RETURN_NOT_OK(status); |
| 94 | + |
| 95 | + // Compute the intersection of the value and validity. |
| 96 | + auto result = bitmaps.GetLocalBitMap(2); |
| 97 | + BitMapAccumulator::IntersectBitMaps( |
| 98 | + result, {bitmaps.GetLocalBitMap(0), bitmaps.GetLocalBitMap((1))}, bitmap_size); |
| 99 | + |
| 100 | + return out_selection->PopulateFromBitMap(result, bitmap_size, batch.num_rows() - 1); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace gandiva |
0 commit comments