28 llvm::Value* operand_lv{
nullptr};
29 if (operand_as_const) {
30 const auto operand_lvs =
31 codegen(operand_as_const, ti.get_compression(), ti.get_comp_param(), co);
32 if (operand_lvs.size() == 3) {
35 operand_lv = operand_lvs.front();
38 operand_lv =
codegen(operand,
true, co).front();
40 const auto& operand_ti = operand->get_type_info();
41 return codegenCast(operand_lv, operand_ti, ti, operand_as_const, co);
56 const bool operand_is_const,
61 byte_array_type->getPointerTo());
63 if (operand_lv->getType()->isIntegerTy()) {
70 CHECK(operand_lv->getType()->isIntegerTy(1) ||
71 operand_lv->getType()->isIntegerTy(8));
72 if (operand_lv->getType()->isIntegerTy(1)) {
88 const auto operand_dimen =
109 const bool nullable) {
110 CHECK(ts_lv->getType()->isIntegerTy(64));
111 std::vector<llvm::Value*> datetrunc_args{ts_lv};
113 static const std::string hptodate_fname =
"DateTruncateHighPrecisionToDate";
114 static const std::string hptodate_null_fname =
115 "DateTruncateHighPrecisionToDateNullable";
116 datetrunc_args.push_back(
125 std::string datetrunc_fname{
"DateTruncate"};
126 datetrunc_args.insert(datetrunc_args.begin(),
130 datetrunc_fname +=
"Nullable";
137 const int operand_dimen,
138 const int target_dimen,
139 const bool nullable) {
140 if (operand_dimen == target_dimen) {
143 CHECK(ts_lv->getType()->isIntegerTy(64));
144 static const std::string sup_fname{
"DateTruncateAlterPrecisionScaleUp"};
145 static const std::string sdn_fname{
"DateTruncateAlterPrecisionScaleDown"};
146 static const std::string sup_null_fname{
"DateTruncateAlterPrecisionScaleUpNullable"};
147 static const std::string sdn_null_fname{
"DateTruncateAlterPrecisionScaleDownNullable"};
148 std::vector<llvm::Value*> f_args{ts_lv,
151 abs(operand_dimen - target_dimen))))};
155 return operand_dimen < target_dimen
167 const bool operand_is_const,
170 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
180 throw std::runtime_error(
181 "Cast from none-encoded string to dictionary-encoded not supported for "
182 "distributed queries");
186 "Cast from none-encoded string to dictionary-encoded would be slow");
190 CHECK(operand_lv->getType()->isIntegerTy(64));
201 CHECK(operand_lv->getType()->isIntegerTy(32));
204 throw std::runtime_error(
205 "Cast from dictionary-encoded string to none-encoded not supported for "
206 "distributed queries");
210 "Cast from dictionary-encoded string to none-encoded would be slow");
223 CHECK(operand_is_const);
237 const auto scale_lv =
259 const auto scale_lv =
262 const auto operand_width =
263 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
265 std::string method_name =
"scale_decimal_down_nullable";
267 method_name =
"scale_decimal_down_not_nullable";
270 CHECK(operand_width == 64);
280 const auto operand_width =
281 static_cast<llvm::IntegerType*
>(operand_lv->getType())->getBitWidth();
283 if (target_width == operand_width) {
288 target_width > operand_width ? llvm::Instruction::CastOps::SExt
289 : llvm::Instruction::CastOps::Trunc,
301 llvm::Value* operand_lv,
304 const int64_t scale) {
305 llvm::Value* chosen_max{
nullptr};
306 llvm::Value* chosen_min{
nullptr};
307 std::tie(chosen_max, chosen_min) =
313 auto cast_fail = llvm::BasicBlock::Create(
315 auto operand_max =
static_cast<llvm::ConstantInt*
>(chosen_max)->getSExtValue() / scale;
316 auto operand_min =
static_cast<llvm::ConstantInt*
>(chosen_min)->getSExtValue() / scale;
317 const auto ti_llvm_type =
319 llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
320 llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
323 const auto operand_ti_llvm_type =
330 llvm::Value* over{
nullptr};
331 llvm::Value* under{
nullptr};
336 const auto type_name =
341 "gt_" + type_name +
"_nullable_lhs",
342 {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
344 "le_" + type_name +
"_nullable_lhs",
345 {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
347 const auto detected = cgen_state_->ir_builder_.CreateOr(over, under,
"overflow");
348 cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
350 cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
351 cgen_state_->ir_builder_.CreateRet(
354 cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
361 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
365 llvm::Value* result_lv{
nullptr};
382 llvm::ConstantFP::get(result_lv->getType(),
392 throw std::runtime_error(
"Cast from " + operand_ti.
get_type_name() +
" to " +
399 CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
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 EncodingType get_compression() const
HOST DEVICE int get_comp_param() const
llvm::IRBuilder ir_builder_
HOST DEVICE int get_scale() const
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
HOST DEVICE int get_size() const
bool byte_array_cast(const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::Function * row_func_
std::string get_type_name() const
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
int get_logical_size() const
HOST DEVICE SQLTypes get_type() 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)
HOST DEVICE bool get_notnull() const
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
const SQLTypeInfo & get_type_info() const
ExecutorDeviceType device_type_
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
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 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
llvm::Value * toBool(llvm::Value *)
llvm::ConstantInt * llInt(const T v) const
HOST DEVICE SQLTypes get_subtype() 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)
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::Value * codegenCastBetweenTimestamps(llvm::Value *ts_lv, const int operand_dimen, const int target_dimen, const bool nullable)
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Executor * executor() const