29 llvm::Value* operand_lv{
nullptr};
30 if (operand_as_const) {
31 const auto operand_lvs =
32 codegen(operand_as_const, ti.get_compression(), ti.get_comp_param(), co);
33 if (operand_lvs.size() == 3) {
36 operand_lv = operand_lvs.front();
39 operand_lv =
codegen(operand,
true, co).front();
41 const auto& operand_ti = operand->get_type_info();
42 return codegenCast(operand_lv, operand_ti, ti, operand_as_const, co);
57 const bool operand_is_const,
63 byte_array_type->getPointerTo());
65 if (operand_lv->getType()->isIntegerTy()) {
73 CHECK(operand_lv->getType()->isIntegerTy(1) ||
74 operand_lv->getType()->isIntegerTy(8));
75 if (operand_lv->getType()->isIntegerTy(1)) {
82 if (operand_ti.
is_integer() && operand_lv->getType()->isIntegerTy(8) &&
99 const auto operand_dimen =
120 const bool nullable) {
122 CHECK(ts_lv->getType()->isIntegerTy(64));
126 "DateTruncateHighPrecisionToDateNullable",
132 return cgen_state_->emitExternalCall(
133 "DateTruncateHighPrecisionToDate",
138 std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
141 std::make_unique<NullCheckCodegen>(cgen_state_,
145 "cast_timestamp_nullcheck");
147 auto ret = cgen_state_->emitExternalCall(
148 "datetrunc_day",
get_int_type(64, cgen_state_->context_), {ts_lv});
149 if (nullcheck_codegen) {
150 ret = nullcheck_codegen->finalize(
ll_int(
NULL_BIGINT, cgen_state_->context_), ret);
158 const bool nullable) {
162 if (operand_dimen == target_dimen) {
165 CHECK(ts_lv->getType()->isIntegerTy(64));
168 if (operand_dimen < target_dimen) {
190 const bool operand_is_const,
194 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
204 throw std::runtime_error(
205 "Cast from none-encoded string to dictionary-encoded not supported for "
206 "distributed queries");
210 "Cast from none-encoded string to dictionary-encoded would be slow");
214 CHECK(operand_lv->getType()->isIntegerTy(64));
225 CHECK(operand_lv->getType()->isIntegerTy(32));
228 throw std::runtime_error(
229 "Cast from dictionary-encoded string to none-encoded not supported for "
230 "distributed queries");
234 "Cast from dictionary-encoded string to none-encoded would be slow");
240 const int64_t string_dictionary_ptr =
242 ?
reinterpret_cast<int64_t
>(
243 executor()->getRowSetMemoryOwner()->getLiteralStringDictProxy())
244 : reinterpret_cast<int64_t>(
executor()->getStringDictionaryProxy(
251 CHECK(operand_is_const);
266 const auto scale_lv =
288 const auto scale_lv =
291 const auto operand_width =
292 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
294 std::string method_name =
"scale_decimal_down_nullable";
296 method_name =
"scale_decimal_down_not_nullable";
299 CHECK(operand_width == 64);
309 const auto operand_width =
310 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
312 if (target_width == operand_width) {
317 target_width > operand_width ? llvm::Instruction::CastOps::SExt
318 : llvm::Instruction::CastOps::Trunc,
330 llvm::Value* operand_lv,
333 const int64_t scale) {
335 llvm::Value* chosen_max{
nullptr};
336 llvm::Value* chosen_min{
nullptr};
337 std::tie(chosen_max, chosen_min) =
341 auto cast_ok = llvm::BasicBlock::Create(
343 auto cast_fail = llvm::BasicBlock::Create(
345 auto operand_max =
static_cast<llvm::ConstantInt*
>(chosen_max)->getSExtValue() / scale;
346 auto operand_min =
static_cast<llvm::ConstantInt*
>(chosen_min)->getSExtValue() / scale;
347 const auto ti_llvm_type =
349 llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
350 llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
353 const auto operand_ti_llvm_type =
360 llvm::Value* over{
nullptr};
361 llvm::Value* under{
nullptr};
372 {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
375 {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
377 const auto detected = cgen_state_->ir_builder_.CreateOr(over, under,
"overflow");
378 cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
380 cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
381 cgen_state_->ir_builder_.CreateRet(
384 cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
392 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
396 llvm::Value* result_lv{
nullptr};
413 llvm::ConstantFP::get(result_lv->getType(),
424 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
431 CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
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)
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
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)
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
std::string numeric_type_name(const SQLTypeInfo &ti)
HOST DEVICE bool get_notnull() 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
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Executor * executor() const