blob: d7b3e6a9a2ae2f31c7938de1054421f877b326b9 [file]
/**
* Copyright 2011-2015 Quickstep Technologies LLC.
* Copyright 2015-2016 Pivotal Software, Inc.
* Copyright 2016, Quickstep Research Group, Computer Sciences Department,
* University of Wisconsin—Madison.
*
* 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.
**/
#ifndef QUICKSTEP_STORAGE_WINDOW_AGGREGATION_OPERATION_STATE_HPP_
#define QUICKSTEP_STORAGE_WINDOW_AGGREGATION_OPERATION_STATE_HPP_
#include <cstddef>
#include <memory>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#include "expressions/aggregation/AggregationHandle.hpp"
#include "expressions/scalar/Scalar.hpp"
#include "expressions/scalar/ScalarAttribute.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "storage/WindowAggregationOperationState.pb.h"
#include "utility/Macros.hpp"
namespace quickstep {
class AggregateFunction;
class CatalogDatabaseLite;
class CatalogRelationSchema;
class InsertDestination;
class StorageManager;
/** \addtogroup Storage
* @{
*/
/**
* @brief Helper class for maintaining the state of window aggregation.
**/
class WindowAggregationOperationState {
public:
/**
* @brief Constructor for window aggregation operation state.
*
* @param input_relation Input relation on which window aggregation is computed.
* @param window_aggregate_functions The window aggregate function to be
* computed.
* @param arguments A list of argument expressions to that aggregate.
* @param partition_by_attributes A list of window partition key.
* @param is_row True if the window frame is calculated by ROW, false if it is
* calculated by RANGE.
* @param num_preceding The number of rows/range for the tuples preceding the
* current row. -1 means UNBOUNDED PRECEDING.
* @param num_following The number of rows/range for the tuples following the
* current row. -1 means UNBOUNDED FOLLOWING.
* @param storage_manager The StorageManager to use for allocating hash
* tables.
*/
WindowAggregationOperationState(const CatalogRelationSchema &input_relation,
const AggregateFunction *window_aggregate_function,
std::vector<std::unique_ptr<const Scalar>> &&arguments,
std::vector<std::unique_ptr<const Scalar>> &&partition_by_attributes,
const bool is_row,
const std::int64_t num_preceding,
const std::int64_t num_following,
StorageManager *storage_manager);
~WindowAggregationOperationState() {}
/**
* @brief Generate the window aggregation operation state from the serialized
* Protocol Buffer representation.
*
* @param proto A serialized protocol buffer representation of a
* WindowAggregationOperationState, originally generated by the
* optimizer.
* @param database The database for resolving relation and attribute
* references.
* @param storage_manager The StorageManager to use.
**/
static WindowAggregationOperationState* ReconstructFromProto(
const serialization::WindowAggregationOperationState &proto,
const CatalogDatabaseLite &database,
StorageManager *storage_manager);
/**
* @brief Check whether a serialization::AggregationOperationState is
* fully-formed and all parts are valid.
*
* @param proto A serialized Protocol Buffer representation of an
* AggregationOperationState, originally generated by the optimizer.
* @param database The Database to resolve relation and attribute references
* in.
* @return Whether proto is fully-formed and valid.
**/
static bool ProtoIsValid(const serialization::WindowAggregationOperationState &proto,
const CatalogDatabaseLite &database);
/**
* @brief Get the is_row info.
* @note This is a quickfix for "unused variable". After the window aggregate
* functions are built, these methods might be dropped.
*
* @return True if the frame mode is ROW, false if it is RANGE.
**/
const bool is_row() const { return is_row_; }
/**
* @brief Get the num_preceding info.
* @note This is a quickfix for "unused variable". After the window aggregate
* functions are built, these methods might be dropped.
*
* @return The number of rows/range that precedes the current row.
**/
const std::int64_t num_preceding() const { return num_preceding_; }
/**
* @brief Get the num_following info.
* @note This is a quickfix for "unused variable". After the window aggregate
* functions are built, these methods might be dropped.
*
* @return The number of rows/range that follows the current row.
**/
const std::int64_t num_following() const { return num_following_; }
/**
* @brief Get the pointer to StorageManager.
* @note This is a quickfix for "unused variable". After the window aggregate
* functions are built, these methods might be dropped.
*
* @return A pointer to the storage manager.
**/
StorageManager *storage_manager() { return storage_manager_; }
private:
const CatalogRelationSchema &input_relation_;
// TODO(Shixuan): Handle and State for window aggregation will be needed for
// actual calculation.
std::unique_ptr<AggregationHandle> window_aggregation_handle_;
std::unique_ptr<AggregationState> window_aggregation_state_;
std::vector<std::unique_ptr<const Scalar>> arguments_;
// We don't add order_by_attributes here since it is not needed after sorting.
std::vector<std::unique_ptr<const Scalar>> partition_by_attributes_;
// Window framing information.
const bool is_row_;
const std::int64_t num_preceding_;
const std::int64_t num_following_;
StorageManager *storage_manager_;
#ifdef QUICKSTEP_ENABLE_VECTOR_COPY_ELISION_SELECTION
// If all an aggregate's argument expressions are simply attributes in
// 'input_relation_', then this caches the attribute IDs of those arguments.
std::vector<attribute_id> arguments_as_attributes_;
#endif
DISALLOW_COPY_AND_ASSIGN(WindowAggregationOperationState);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_STORAGE_WINDOW_AGGREGATION_OPERATION_STATE_HPP_