30 llvm::Value* operand_lv{
nullptr};
31 if (operand_as_const) {
32 const auto operand_lvs =
33 codegen(operand_as_const, ti.get_compression(), ti.get_comp_param(), co);
34 if (operand_lvs.size() == 3) {
37 operand_lv = operand_lvs.front();
40 operand_lv =
codegen(operand,
true, co).front();
46 if (operand_lv->getType()->isPointerTy() &&
47 operand_lv->getType()->getPointerElementType()->isStructTy()) {
49 operand_lv->getType()->getPointerElementType(), operand_lv);
56 const auto& operand_ti = operand->get_type_info();
57 return codegenCast(operand_lv, operand_ti, ti, operand_as_const, co);
72 const bool operand_is_const,
78 byte_array_type->getPointerTo());
80 if (operand_lv->getType()->isIntegerTy()) {
88 CHECK(operand_lv->getType()->isIntegerTy(1) ||
89 operand_lv->getType()->isIntegerTy(8));
90 if (operand_lv->getType()->isIntegerTy(1)) {
97 if (operand_ti.
is_integer() && operand_lv->getType()->isIntegerTy(8) &&
114 const auto operand_dimen =
135 const bool nullable) {
137 CHECK(ts_lv->getType()->isIntegerTy(64));
141 "DateTruncateHighPrecisionToDateNullable",
147 return cgen_state_->emitExternalCall(
148 "DateTruncateHighPrecisionToDate",
153 std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
156 std::make_unique<NullCheckCodegen>(cgen_state_,
160 "cast_timestamp_nullcheck");
162 auto ret = cgen_state_->emitExternalCall(
163 "datetrunc_day",
get_int_type(64, cgen_state_->context_), {ts_lv});
164 if (nullcheck_codegen) {
165 ret = nullcheck_codegen->finalize(
ll_int(
NULL_BIGINT, cgen_state_->context_), ret);
173 const bool nullable) {
177 if (operand_dimen == target_dimen) {
180 CHECK(ts_lv->getType()->isIntegerTy(64));
183 if (operand_dimen < target_dimen) {
205 const bool operand_is_const,
209 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
226 const int64_t source_string_proxy_handle =
227 reinterpret_cast<int64_t
>(
executor()->getStringDictionaryProxy(
230 const int64_t dest_string_proxy_handle =
231 reinterpret_cast<int64_t
>(
executor()->getStringDictionaryProxy(
234 auto source_string_proxy_handle_lv =
cgen_state_->
llInt(source_string_proxy_handle);
235 auto dest_string_proxy_handle_lv =
cgen_state_->
llInt(dest_string_proxy_handle);
237 std::vector<llvm::Value*> string_cast_lvs{
238 operand_lv, source_string_proxy_handle_lv, dest_string_proxy_handle_lv};
241 "intersect_translate_string_id_to_other_dict",
251 const std::vector<StringOps_Namespace::StringOpInfo> string_op_infos;
252 auto string_dictionary_translation_mgr =
253 std::make_unique<StringDictionaryTranslationMgr>(
262 &
executor()->getCatalog()->getDataMgr(),
267 ->
codegen(operand_lv, operand_ti,
true , co);
272 throw std::runtime_error(
273 "Cast from none-encoded string to dictionary-encoded not supported for "
274 "distributed queries");
278 CHECK(operand_lv->getType()->isIntegerTy(64));
289 CHECK(operand_lv->getType()->isIntegerTy(32));
292 throw std::runtime_error(
293 "Cast from dictionary-encoded string to none-encoded not "
294 "currently supported for distributed queries.");
302 const int64_t string_dictionary_ptr =
304 ?
reinterpret_cast<int64_t
>(
305 executor()->getRowSetMemoryOwner()->getLiteralStringDictProxy())
306 : reinterpret_cast<int64_t>(
executor()->getStringDictionaryProxy(
313 CHECK(operand_is_const);
328 const auto scale_lv =
350 const auto scale_lv =
353 const auto operand_width =
354 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
356 std::string method_name =
"scale_decimal_down_nullable";
358 method_name =
"scale_decimal_down_not_nullable";
361 CHECK(operand_width == 64);
371 const auto operand_width =
372 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
374 if (target_width == operand_width) {
379 target_width > operand_width ? llvm::Instruction::CastOps::SExt
380 : llvm::Instruction::CastOps::Trunc,
392 llvm::Value* operand_lv,
395 const int64_t scale) {
397 llvm::Value* chosen_max{
nullptr};
398 llvm::Value* chosen_min{
nullptr};
399 std::tie(chosen_max, chosen_min) =
403 auto cast_ok = llvm::BasicBlock::Create(
405 auto cast_fail = llvm::BasicBlock::Create(
407 auto operand_max =
static_cast<llvm::ConstantInt*
>(chosen_max)->getSExtValue() / scale;
408 auto operand_min =
static_cast<llvm::ConstantInt*
>(chosen_min)->getSExtValue() / scale;
409 const auto ti_llvm_type =
411 llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
412 llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
415 const auto operand_ti_llvm_type =
422 llvm::Value* over{
nullptr};
423 llvm::Value* under{
nullptr};
434 {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
437 {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
439 const auto detected = cgen_state_->ir_builder_.CreateOr(over, under,
"overflow");
440 cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
442 cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
443 cgen_state_->ir_builder_.CreateRet(
446 cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
454 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
457 llvm::Value* result_lv;
463 if (
auto const scale = static_cast<unsigned>(operand_ti.
get_scale())) {
466 result_lv, llvm::ConstantFP::get(result_lv->getType(), multiplier));
469 if (
auto const scale = static_cast<unsigned>(operand_ti.
get_scale())) {
479 llvm::ConstantFP::get(fp_type, multiplier)});
497 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
504 CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
514 auto* fp_type = operand_lv->getType()->isFloatTy()
517 auto* zero = llvm::ConstantFP::get(fp_type, 0.0);
518 auto* mhalf = llvm::ConstantFP::get(fp_type, -0.5);
519 auto* phalf = llvm::ConstantFP::get(fp_type, 0.5);
HOST DEVICE SQLTypes get_subtype() const
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
llvm::Value * codegenCastFromFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
HOST DEVICE int get_size() const
bool is_timestamp() const
HOST DEVICE int get_scale() const
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
llvm::IRBuilder ir_builder_
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
bool byte_array_cast(const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
HOST DEVICE SQLTypes get_type() const
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
llvm::Function * current_func_
int get_logical_size() const
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
bool is_dict_intersection() const
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
double power10inv(unsigned const x)
const SQLTypeInfo & get_type_info() const
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
HOST DEVICE EncodingType get_compression() const
const Expr * get_operand() const
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
HOST DEVICE int get_dimension() const
std::string get_type_name() const
llvm::Value * toBool(llvm::Value *)
HOST DEVICE int get_comp_param() const
llvm::Value * codegenCastBetweenTimestamps(llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_dimen, const bool nullable)
llvm::ConstantInt * llInt(const T v) const
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
llvm::Value * codegenCastFromString(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
const StringDictionaryTranslationMgr * moveStringDictionaryTranslationMgr(std::unique_ptr< const StringDictionaryTranslationMgr > &&str_dict_translation_mgr)
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
std::string numeric_type_name(const SQLTypeInfo &ti)
HOST DEVICE bool get_notnull() const
llvm::Value * codegen(llvm::Value *str_id_input, const SQLTypeInfo &input_ti, const bool add_nullcheck, const CompilationOptions &co) const
constexpr auto type_name() noexcept
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
SQLOps get_optype() const
static constexpr int32_t literalsDictId
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Executor * executor() const