#pragma once #include namespace DB { namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_ARGUMENT; } template class FunctionCustomWeekToDateOrDate32 : public IFunctionCustomWeek, WithContext { public: bool enable_extended_results_for_datetime_functions = false; static FunctionPtr create(ContextPtr context_) { return std::make_shared(context_); } explicit FunctionCustomWeekToDateOrDate32(ContextPtr context_) : WithContext(context_) , enable_extended_results_for_datetime_functions(context_->getSettingsRef().enable_extended_results_for_datetime_functions) { } DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { this->checkArguments(arguments, /*is_result_type_date_or_date32*/ true); const IDataType * from_type = arguments[0].type.get(); WhichDataType which(from_type); if ((which.isDate32() || which.isDateTime64()) && enable_extended_results_for_datetime_functions) return std::make_shared(); else return std::make_shared(); } ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override { const IDataType * from_type = arguments[0].type.get(); WhichDataType which(from_type); if (which.isDate()) return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, Transform{}); else if (which.isDate32()) if (enable_extended_results_for_datetime_functions) return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, Transform{}); else return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, Transform{}); else if (which.isDateTime()) return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, Transform{}); else if (which.isDateTime64()) { if (enable_extended_results_for_datetime_functions) return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, TransformDateTime64{assert_cast(from_type)->getScale()}); else return CustomWeekTransformImpl::execute( arguments, result_type, input_rows_count, TransformDateTime64{assert_cast(from_type)->getScale()}); } else throw Exception( "Illegal type " + arguments[0].type->getName() + " of argument of function " + this->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); } }; }