| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you 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 "types/operations/unary_operations/UnaryOperationFactory.hpp" |
| |
| #include <string> |
| |
| #include "types/TypeFactory.hpp" |
| #include "types/operations/Operation.pb.h" |
| #include "types/operations/unary_operations/ArithmeticUnaryOperations.hpp" |
| #include "types/operations/unary_operations/NumericCastOperation.hpp" |
| #include "types/operations/unary_operations/DateExtractOperation.hpp" |
| #include "types/operations/unary_operations/SubstringOperation.hpp" |
| #include "types/operations/unary_operations/UnaryOperationID.hpp" |
| #include "utility/Macros.hpp" |
| |
| #include "glog/logging.h" |
| |
| namespace quickstep { |
| |
| const UnaryOperation& UnaryOperationFactory::GetUnaryOperation(const UnaryOperationID id) { |
| switch (id) { |
| case UnaryOperationID::kNegate: |
| return NegateUnaryOperation::Instance(); |
| case UnaryOperationID::kCast: |
| FATAL_ERROR("Getting a CastOperation through GetUnaryOperation is not supported"); |
| case UnaryOperationID::kDateExtract: |
| FATAL_ERROR("Getting a DateExtractOperation through GetUnaryOperation is not supported"); |
| case UnaryOperationID::kSubstring: |
| FATAL_ERROR("Getting a SubstringOperation through GetUnaryOperation is not supported"); |
| default: |
| FATAL_ERROR("Unknown UnaryOperationID"); |
| } |
| } |
| |
| bool UnaryOperationFactory::ProtoIsValid(const serialization::UnaryOperation &proto) { |
| // Check that UnaryOperation is fully initialized. |
| if (!proto.IsInitialized()) { |
| return false; |
| } |
| |
| // Check that the operation_id is a valid UnaryOperation. |
| if (!proto.UnaryOperationID_IsValid(proto.operation_id())) { |
| return false; |
| } |
| |
| switch (proto.operation_id()) { |
| case serialization::UnaryOperation::NEGATE: |
| return true; |
| case serialization::UnaryOperation::CAST: |
| return proto.HasExtension(serialization::CastOperation::target_type) |
| && TypeFactory::ProtoIsValid(proto.GetExtension(serialization::CastOperation::target_type)); |
| case serialization::UnaryOperation::DATE_EXTRACT: |
| return proto.HasExtension(serialization::DateExtractOperation::unit) |
| && DateExtractOperation_Unit_IsValid(proto.GetExtension(serialization::DateExtractOperation::unit)); |
| case serialization::UnaryOperation::SUBSTRING: |
| return proto.HasExtension(serialization::SubstringOperation::start_position) |
| && proto.HasExtension(serialization::SubstringOperation::substring_length); |
| default: |
| return false; |
| } |
| } |
| |
| const UnaryOperation& UnaryOperationFactory::ReconstructFromProto( |
| const serialization::UnaryOperation &proto) { |
| DCHECK(ProtoIsValid(proto)) |
| << "Attempted to create UnaryOperation from an invalid proto description:\n" |
| << proto.DebugString(); |
| |
| switch (proto.operation_id()) { |
| case serialization::UnaryOperation::NEGATE: |
| return GetUnaryOperation(UnaryOperationID::kNegate); |
| case serialization::UnaryOperation::CAST: |
| return NumericCastOperation::Instance( |
| TypeFactory::ReconstructFromProto( |
| proto.GetExtension( |
| serialization::CastOperation::target_type))); |
| case serialization::UnaryOperation::DATE_EXTRACT: |
| switch (proto.GetExtension(serialization::DateExtractOperation::unit)) { |
| case serialization::DateExtractOperation::YEAR: |
| return DateExtractOperation::Instance(DateExtractUnit::kYear); |
| case serialization::DateExtractOperation::MONTH: |
| return DateExtractOperation::Instance(DateExtractUnit::kMonth); |
| case serialization::DateExtractOperation::DAY: |
| return DateExtractOperation::Instance(DateExtractUnit::kDay); |
| case serialization::DateExtractOperation::HOUR: |
| return DateExtractOperation::Instance(DateExtractUnit::kHour); |
| case serialization::DateExtractOperation::MINUTE: |
| return DateExtractOperation::Instance(DateExtractUnit::kMinute); |
| case serialization::DateExtractOperation::SECOND: |
| return DateExtractOperation::Instance(DateExtractUnit::kSecond); |
| default: |
| FATAL_ERROR("Unrecognized DateExtractOperation unit in UnaryOperation::ReconstructFromProto"); |
| } |
| case serialization::UnaryOperation::SUBSTRING: |
| return SubstringOperation::Instance( |
| proto.GetExtension(serialization::SubstringOperation::start_position), |
| proto.GetExtension(serialization::SubstringOperation::substring_length)); |
| default: |
| FATAL_ERROR("Unrecognized UnaryOperationID in UnaryOperation::ReconstructFromProto"); |
| } |
| } |
| |
| } // namespace quickstep |