1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-29 08:21:15 +03:00

MCOL-4464 Bitwise operations not like in MariaDB

This commit is contained in:
Alexander Barkov
2020-12-19 16:06:58 +04:00
parent 0e6778378d
commit 4abbe90302
10 changed files with 751 additions and 423 deletions

View File

@ -38,6 +38,8 @@ using namespace rowgroup;
#include "errorids.h"
using namespace logging;
#include "mcs_int64.h"
#include "mcs_decimal.h"
#include "dataconvert.h"
using namespace dataconvert;
@ -45,23 +47,84 @@ namespace
{
using namespace funcexp;
void bitWiseExceptionHandler(const std::string& funcName,
const CalpontSystemCatalog::ColType& colType)
{
std::ostringstream oss;
oss << funcName << ": datatype of " << execplan::colDataTypeToString(colType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
bool validateBitOperandTypeOrError(execplan::FunctionColumn &col,
const Func & func,
uint argno)
{
auto & type = col.functionParms()[argno]->data()->resultType();
if (type.canReturnXInt64())
return false;
bitWiseExceptionHandler(func.funcName(), type);
return true;
}
template<typename T>
datatypes::TUInt64Null ConvertToBitOperand(const T &val)
{
if (val > static_cast<T>(UINT64_MAX))
return datatypes::TUInt64Null(UINT64_MAX);
if (val >= 0)
return datatypes::TUInt64Null(static_cast<uint64_t>(val));
if (val < static_cast<T>(INT64_MIN))
return datatypes::TUInt64Null(static_cast<uint64_t>(INT64_MAX)+1);
return datatypes::TUInt64Null((uint64_t) (int64_t) val);
}
static
datatypes::TUInt64Null DecimalToBitOperand(Row& row,
const execplan::SPTP& parm,
const funcexp::Func& thisFunc)
{
bool tmpIsNull = false;
datatypes::Decimal d = parm->data()->getDecimalVal(row, tmpIsNull);
if (tmpIsNull)
return datatypes::TUInt64Null();
if (parm->data()->resultType().colWidth == datatypes::MAXDECIMALWIDTH)
{
int128_t val = d.getPosNegRoundedIntegralPart(0).getValue();
return ConvertToBitOperand<int128_t>(val);
}
return datatypes::TUInt64Null((uint64_t) d.narrowRound());
}
// Functions TypeHolderStd::canReturnXInt64() and GenericToBitOperand()
// should be splitted eventually to virtual methods in TypeHandler.
//
// However, TypeHandler::getBitOperand() would seem to be too specific.
// It would be nice to have a more generic functionality in TypeHandler.
//
// Let's consider having something similar to MariaDB Longlong_hybrid,
// which holds a signed/unsigned 64bit value together with a sign flag.
// Having TypeHandler::getXInt64Hybrid() would be more useful:
// it can be reused for other purposes, not only for bitwise operations.
// @bug 4703 - the actual bug was only in the DATETIME case
// part of this statement below, but instead of leaving 5 identical
// copies of this code, extracted into a single utility function
// here. This same method is potentially useful in other methods
// and could be extracted into a utility class with its own header
// if that is the case - this is left as future exercise
bool getUIntValFromParm(
datatypes::TUInt64Null GenericToBitOperand(
Row& row,
const execplan::SPTP& parm,
uint64_t& value,
bool& isNull,
const funcexp::Func& thisFunc,
bool& isBigVal,
int128_t& bigval)
bool temporalRounding)
{
isBigVal = false;
switch (parm->data()->resultType().colDataType)
{
case execplan::CalpontSystemCatalog::BIGINT:
@ -69,200 +132,258 @@ bool getUIntValFromParm(
case execplan::CalpontSystemCatalog::MEDINT:
case execplan::CalpontSystemCatalog::TINYINT:
case execplan::CalpontSystemCatalog::SMALLINT:
{
datatypes::TSInt64Null tmp= parm->data()->toTSInt64Null(row);
return tmp.isNull() ? datatypes::TUInt64Null() :
datatypes::TUInt64Null((uint64_t) (int64_t) tmp);
}
case execplan::CalpontSystemCatalog::DOUBLE:
case execplan::CalpontSystemCatalog::FLOAT:
case execplan::CalpontSystemCatalog::UDOUBLE:
case execplan::CalpontSystemCatalog::UFLOAT:
{
value = parm->data()->getIntVal(row, isNull);
bool tmpIsNull;
double val = parm->data()->getDoubleVal(row, tmpIsNull);
return tmpIsNull ? datatypes::TUInt64Null() :
ConvertToBitOperand<double>(round(val));
}
break;
case execplan::CalpontSystemCatalog::UBIGINT:
case execplan::CalpontSystemCatalog::UINT:
case execplan::CalpontSystemCatalog::UMEDINT:
case execplan::CalpontSystemCatalog::UTINYINT:
case execplan::CalpontSystemCatalog::USMALLINT:
{
value = parm->data()->getUintVal(row, isNull);
}
break;
return parm->data()->toTUInt64Null(row);
case execplan::CalpontSystemCatalog::VARCHAR:
case execplan::CalpontSystemCatalog::CHAR:
case execplan::CalpontSystemCatalog::TEXT:
{
value = parm->data()->getIntVal(row, isNull);
if (isNull)
{
isNull = true;
}
bool tmpIsNull;
const string& str = parm->data()->getStrVal(row, tmpIsNull);
if (tmpIsNull)
return datatypes::TUInt64Null();
static const datatypes::SystemCatalog::TypeAttributesStd
attr(datatypes::MAXDECIMALWIDTH, 6, datatypes::INT128MAXPRECISION);
int128_t val = attr.decimal128FromString(str);
datatypes::Decimal d(0, attr.scale, attr.precision, &val);
val = d.getPosNegRoundedIntegralPart(0).getValue();
return ConvertToBitOperand<int128_t>(val);
}
break;
case execplan::CalpontSystemCatalog::DECIMAL:
case execplan::CalpontSystemCatalog::UDECIMAL:
{
IDB_Decimal d = parm->data()->getDecimalVal(row, isNull);
if (parm->data()->resultType().colWidth == datatypes::MAXDECIMALWIDTH)
{
isBigVal = true;
if (parm->data()->resultType().colDataType == execplan::CalpontSystemCatalog::UDECIMAL &&
d.value < 0)
{
bigval = 0;
break;
}
int128_t scaleDivisor, scaleDivisor2;
datatypes::getScaleDivisor(scaleDivisor, d.scale);
scaleDivisor2 = (scaleDivisor <= 10) ? 1 : (scaleDivisor / 10);
int128_t tmpval = d.s128Value / scaleDivisor;
int128_t lefto = (d.s128Value - tmpval * scaleDivisor) / scaleDivisor2;
if (tmpval >= 0 && lefto > 4)
tmpval++;
if (tmpval < 0 && lefto < -4)
tmpval--;
bigval = tmpval;
}
else
{
if (parm->data()->resultType().colDataType == execplan::CalpontSystemCatalog::UDECIMAL &&
d.value < 0)
{
d.value = 0;
}
double dscale = d.scale;
int64_t tmpval = d.value / pow(10.0, dscale);
int lefto = (d.value - tmpval * pow(10.0, dscale)) / pow(10.0, dscale - 1);
if (tmpval >= 0 && lefto > 4)
tmpval++;
if (tmpval < 0 && lefto < -4)
tmpval--;
value = tmpval;
}
}
break;
return DecimalToBitOperand(row, parm, thisFunc);
case execplan::CalpontSystemCatalog::DATE:
{
int32_t time = parm->data()->getDateIntVal(row, isNull);
bool tmpIsNull;
int32_t time = parm->data()->getDateIntVal(row, tmpIsNull);
if (tmpIsNull)
return datatypes::TUInt64Null();
Date d(time);
value = d.convertToMySQLint();
int64_t value = Date(time).convertToMySQLint();
return datatypes::TUInt64Null((uint64_t) value);
}
break;
case execplan::CalpontSystemCatalog::DATETIME:
{
int64_t time = parm->data()->getDatetimeIntVal(row, isNull);
bool tmpIsNull;
int64_t time = parm->data()->getDatetimeIntVal(row, tmpIsNull);
if (tmpIsNull)
return datatypes::TUInt64Null();
// @bug 4703 - missing year when convering to int
DateTime dt(time);
value = dt.convertToMySQLint();
int64_t value = dt.convertToMySQLint();
if (temporalRounding && dt.msecond >= 500000)
value++;
return datatypes::TUInt64Null((uint64_t) value);
}
break;
case execplan::CalpontSystemCatalog::TIMESTAMP:
{
int64_t time = parm->data()->getTimestampIntVal(row, isNull);
bool tmpIsNull;
int64_t time = parm->data()->getTimestampIntVal(row, tmpIsNull);
if (tmpIsNull)
return datatypes::TUInt64Null();
TimeStamp dt(time);
value = dt.convertToMySQLint(thisFunc.timeZone());
int64_t value = dt.convertToMySQLint(thisFunc.timeZone());
if (temporalRounding && dt.msecond >= 500000)
value++;
return datatypes::TUInt64Null((uint64_t) value);
}
break;
case execplan::CalpontSystemCatalog::TIME:
{
int64_t time = parm->data()->getTimeIntVal(row, isNull);
bool tmpIsNull;
int64_t time = parm->data()->getTimeIntVal(row, tmpIsNull);
Time dt(time);
value = dt.convertToMySQLint();
int64_t value = dt.convertToMySQLint();
if (temporalRounding && dt.msecond >= 500000)
value < 0 ? value-- : value++;
return datatypes::TUInt64Null((uint64_t) value);
}
break;
default:
{
return false;
}
idbassert(0); // Not possible: checked during the preparation stage.
break;
}
return true;
return datatypes::TUInt64Null();
}
}
namespace funcexp
{
class BitOperandGeneric: public datatypes::TUInt64Null
{
public:
BitOperandGeneric() { }
BitOperandGeneric(Row& row,
const execplan::SPTP& parm,
const funcexp::Func& thisFunc)
:TUInt64Null(GenericToBitOperand(row, parm, thisFunc, true))
{ }
};
// The shift amount operand in MariaDB does not round temporal values
// when sql_mode=TIME_FRAC_ROUND is not set.
class BitOperandGenericShiftAmount: public datatypes::TUInt64Null
{
public:
BitOperandGenericShiftAmount() { }
BitOperandGenericShiftAmount(Row& row,
const execplan::SPTP& parm,
const funcexp::Func& thisFunc)
:TUInt64Null(GenericToBitOperand(row, parm, thisFunc, false))
{ }
};
// A functor to return NULL as a bitwise operation result.
// Used when an unexpected argument count
// is encounteded during the preparation step.
class Func_bitwise_null: public Func_BitOp
{
public:
Func_bitwise_null(): Func_BitOp("bitwise") { }
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
isNull = true;
return 0;
}
};
bool Func_BitOp::validateArgCount(execplan::FunctionColumn &col, uint expected) const
{
static Func_bitwise_null return_null;
if (col.functionParms().size() == expected)
return false;
col.setFunctor(&return_null);
return true;
}
void Func_BitOp::setFunctorByParm(execplan::FunctionColumn &col,
const execplan::SPTP& parm,
Func_Int & return_uint64_from_uint64,
Func_Int & return_uint64_from_sint64,
Func_Int & return_uint64_generic) const
{
if (parm->data()->resultType().isUnsignedInteger())
col.setFunctor(&return_uint64_from_uint64);
else if (parm->data()->resultType().isSignedInteger())
col.setFunctor(&return_uint64_from_sint64);
else
col.setFunctor(&return_uint64_generic);
}
bool Func_BitOp::fixForBitShift(execplan::FunctionColumn &col,
Func_Int & return_uint64_from_uint64,
Func_Int & return_uint64_from_sint64,
Func_Int & return_uint64_generic) const
{
if (validateArgCount(col, 2))
return false;
// The functor detection is done using functionParms()[0] only.
// This is how MariaDB performs it.
setFunctorByParm(col, col.functionParms()[0],
return_uint64_from_uint64,
return_uint64_from_sint64,
return_uint64_generic);
return validateBitOperandTypeOrError(col, *this, 0) ||
validateBitOperandTypeOrError(col, *this, 1);
}
bool Func_BitOp::fixForBitOp2(execplan::FunctionColumn &col,
Func_Int & return_uint64_from_uint64_uint64,
Func_Int & return_uint64_from_sint64_sint64,
Func_Int & return_uint64_generic) const
{
if (validateArgCount(col, 2))
return false;
if (col.functionParms()[0]->data()->resultType().isUnsignedInteger() &&
col.functionParms()[1]->data()->resultType().isUnsignedInteger())
{
col.setFunctor(&return_uint64_from_uint64_uint64);
return false;
}
if (col.functionParms()[0]->data()->resultType().isSignedInteger() &&
col.functionParms()[1]->data()->resultType().isSignedInteger())
{
col.setFunctor(&return_uint64_from_sint64_sint64);
return false;
}
col.setFunctor(&return_uint64_generic);
return validateBitOperandTypeOrError(col, *this, 0) ||
validateBitOperandTypeOrError(col, *this, 1);
}
//
// BITAND
//
CalpontSystemCatalog::ColType Func_bitand::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
template<class TA, class TB>
class Func_bitand_return_uint64: public Func_bitand
{
return resultType;
}
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
idbassert(parm.size() == 2);
Arg2Lazy<TA, TB> args(row, parm, *this);
return (int64_t) (args.a & args.b).nullSafeValue(isNull);
}
};
int64_t Func_bitand::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
bool Func_bitand::fix(execplan::FunctionColumn &col) const
{
if ( parm.size() < 2 )
{
isNull = true;
return 0;
}
uint64_t val1 = 0;
uint64_t val2 = 0;
int128_t bigval1 = 0;
int128_t bigval2 = 0;
bool isBigVal1;
bool isBigVal2;
if (!getUIntValFromParm(row, parm[0], val1, isNull, *this, isBigVal1, bigval1) ||
!getUIntValFromParm(row, parm[1], val2, isNull, *this, isBigVal2, bigval2))
{
std::ostringstream oss;
oss << "bitand: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal1 && !isBigVal2))
{
return val1 & val2;
}
// Type promotion to int128_t
if (!isBigVal1)
bigval1 = val1;
if (!isBigVal2)
bigval2 = val2;
int128_t res = bigval1 & bigval2;
if (res > static_cast<int128_t>(UINT64_MAX))
res = UINT64_MAX;
else if (res < static_cast<int128_t>(INT64_MIN))
res = INT64_MIN;
return (int64_t) res;
static Func_bitand_return_uint64<ParmTUInt64, ParmTUInt64> return_uint64_from_uint64_uint64;
static Func_bitand_return_uint64<ParmTSInt64, ParmTSInt64> return_uint64_from_sint64_sint64;
static Func_bitand_return_uint64<BitOperandGeneric, BitOperandGeneric> return_uint64_generic;
return fixForBitOp2(col, return_uint64_from_uint64_uint64,
return_uint64_from_sint64_sint64,
return_uint64_generic);
}
@ -271,58 +392,30 @@ int64_t Func_bitand::getIntVal(Row& row,
//
CalpontSystemCatalog::ColType Func_leftshift::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
template<class TA>
class Func_leftshift_return_uint64: public Func_leftshift
{
return resultType;
}
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
idbassert(parm.size() == 2);
Arg2Eager<TA, BitOperandGenericShiftAmount> args(row, parm, *this);
return (int64_t) args.a.MariaDBShiftLeft(args.b).nullSafeValue(isNull);
}
};
int64_t Func_leftshift::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
bool Func_leftshift::fix(execplan::FunctionColumn &col) const
{
if ( parm.size() < 2 )
{
isNull = true;
return 0;
}
uint64_t val1 = 0;
uint64_t val2 = 0;
int128_t bigval1 = 0;
int128_t bigval2 = 0;
bool isBigVal1;
bool isBigVal2;
if (!getUIntValFromParm(row, parm[0], val1, isNull, *this, isBigVal1, bigval1) ||
!getUIntValFromParm(row, parm[1], val2, isNull, *this, isBigVal2, bigval2))
{
std::ostringstream oss;
oss << "leftshift: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal1 && !isBigVal2))
{
return val1 << val2;
}
// Type promotion to int128_t
if (!isBigVal1)
bigval1 = val1;
if (!isBigVal2)
bigval2 = val2;
int128_t res = bigval1 << bigval2;
if (res > static_cast<int128_t>(UINT64_MAX))
res = UINT64_MAX;
else if (res < static_cast<int128_t>(INT64_MIN))
res = INT64_MIN;
return (int64_t) res;
static Func_leftshift_return_uint64<ParmTUInt64> return_uint64_from_uint64;
static Func_leftshift_return_uint64<ParmTSInt64> return_uint64_from_sint64;
static Func_leftshift_return_uint64<BitOperandGeneric> return_uint64_generic;
return fixForBitShift(col, return_uint64_from_uint64,
return_uint64_from_sint64,
return_uint64_generic);
}
@ -331,58 +424,30 @@ int64_t Func_leftshift::getIntVal(Row& row,
//
CalpontSystemCatalog::ColType Func_rightshift::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
template<class TA>
class Func_rightshift_return_uint64: public Func_rightshift
{
return resultType;
}
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
idbassert(parm.size() == 2);
Arg2Eager<TA, BitOperandGenericShiftAmount> args(row, parm, *this);
return (int64_t) args.a.MariaDBShiftRight(args.b).nullSafeValue(isNull);
}
};
int64_t Func_rightshift::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
bool Func_rightshift::fix(execplan::FunctionColumn &col) const
{
if ( parm.size() < 2 )
{
isNull = true;
return 0;
}
uint64_t val1 = 0;
uint64_t val2 = 0;
int128_t bigval1 = 0;
int128_t bigval2 = 0;
bool isBigVal1;
bool isBigVal2;
if (!getUIntValFromParm(row, parm[0], val1, isNull, *this, isBigVal1, bigval1) ||
!getUIntValFromParm(row, parm[1], val2, isNull, *this, isBigVal2, bigval2))
{
std::ostringstream oss;
oss << "rightshift: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal1 && !isBigVal2))
{
return val1 >> val2;
}
// Type promotion to int128_t
if (!isBigVal1)
bigval1 = val1;
if (!isBigVal2)
bigval2 = val2;
int128_t res = bigval1 >> bigval2;
if (res > static_cast<int128_t>(UINT64_MAX))
res = UINT64_MAX;
else if (res < static_cast<int128_t>(INT64_MIN))
res = INT64_MIN;
return (int64_t) res;
static Func_rightshift_return_uint64<ParmTUInt64> return_uint64_from_uint64;
static Func_rightshift_return_uint64<ParmTSInt64> return_uint64_from_sint64;
static Func_rightshift_return_uint64<BitOperandGeneric> return_uint64_generic;
return fixForBitShift(col, return_uint64_from_uint64,
return_uint64_from_sint64,
return_uint64_generic);
}
@ -391,60 +456,6 @@ int64_t Func_rightshift::getIntVal(Row& row,
//
CalpontSystemCatalog::ColType Func_bitor::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
{
return resultType;
}
int64_t Func_bitor::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
{
if ( parm.size() < 2 )
{
isNull = true;
return 0;
}
uint64_t val1 = 0;
uint64_t val2 = 0;
int128_t bigval1 = 0;
int128_t bigval2 = 0;
bool isBigVal1;
bool isBigVal2;
if (!getUIntValFromParm(row, parm[0], val1, isNull, *this, isBigVal1, bigval1) ||
!getUIntValFromParm(row, parm[1], val2, isNull, *this, isBigVal2, bigval2))
{
std::ostringstream oss;
oss << "bitor: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal1 && !isBigVal2))
{
return val1 | val2;
}
// Type promotion to int128_t
if (!isBigVal1)
bigval1 = val1;
if (!isBigVal2)
bigval2 = val2;
int128_t res = bigval1 | bigval2;
if (res > static_cast<int128_t>(UINT64_MAX))
res = UINT64_MAX;
else if (res < static_cast<int128_t>(INT64_MIN))
res = INT64_MIN;
return (int64_t) res;
}
uint64_t Func_bitor::getUintVal(rowgroup::Row& row,
FunctionParm& fp,
bool& isNull,
@ -454,63 +465,62 @@ uint64_t Func_bitor::getUintVal(rowgroup::Row& row,
}
template<class TA, class TB>
class Func_bitor_return_uint64: public Func_bitor
{
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
idbassert(parm.size() == 2);
Arg2Lazy<TA, TB> args(row, parm, *this);
return (int64_t) (args.a | args.b).nullSafeValue(isNull);
}
};
bool Func_bitor::fix(execplan::FunctionColumn &col) const
{
static Func_bitor_return_uint64<ParmTUInt64, ParmTUInt64> return_uint64_from_uint64_uint64;
static Func_bitor_return_uint64<ParmTSInt64, ParmTSInt64> return_uint64_from_sint64_sint64;
static Func_bitor_return_uint64<BitOperandGeneric, BitOperandGeneric> return_uint64_generic;
return fixForBitOp2(col, return_uint64_from_uint64_uint64,
return_uint64_from_sint64_sint64,
return_uint64_generic);
}
//
// BIT XOR
//
CalpontSystemCatalog::ColType Func_bitxor::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
template<class TA, class TB>
class Func_bitxor_return_uint64: public Func_bitxor
{
return resultType;
}
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
idbassert(parm.size() == 2);
Arg2Eager<TA, TB> args(row, parm, *this);
return (int64_t) (args.a ^ args.b).nullSafeValue(isNull);
}
};
int64_t Func_bitxor::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
bool Func_bitxor::fix(execplan::FunctionColumn &col) const
{
if ( parm.size() < 2 )
{
isNull = true;
return 0;
}
uint64_t val1 = 0;
uint64_t val2 = 0;
int128_t bigval1 = 0;
int128_t bigval2 = 0;
bool isBigVal1;
bool isBigVal2;
if (!getUIntValFromParm(row, parm[0], val1, isNull, *this, isBigVal1, bigval1) ||
!getUIntValFromParm(row, parm[1], val2, isNull, *this, isBigVal2, bigval2))
{
std::ostringstream oss;
oss << "bitxor: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal1 && !isBigVal2))
{
return val1 ^ val2;
}
// Type promotion to int128_t
if (!isBigVal1)
bigval1 = val1;
if (!isBigVal2)
bigval2 = val2;
int128_t res = bigval1 ^ bigval2;
if (res > static_cast<int128_t>(UINT64_MAX))
res = UINT64_MAX;
else if (res < static_cast<int128_t>(INT64_MIN))
res = INT64_MIN;
return (int64_t) res;
static Func_bitxor_return_uint64<ParmTUInt64, ParmTUInt64> return_uint64_from_uint64_uint64;
static Func_bitxor_return_uint64<ParmTSInt64, ParmTSInt64> return_uint64_from_sint64_sint64;
static Func_bitxor_return_uint64<BitOperandGeneric, BitOperandGeneric> return_uint64_generic;
return fixForBitOp2(col, return_uint64_from_uint64_uint64,
return_uint64_from_sint64_sint64,
return_uint64_generic);
}
@ -519,11 +529,6 @@ int64_t Func_bitxor::getIntVal(Row& row,
//
CalpontSystemCatalog::ColType Func_bit_count::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
{
return resultType;
}
inline int64_t bitCount(uint64_t val)
{
// Refer to Hacker's Delight Chapter 5
@ -538,38 +543,34 @@ inline int64_t bitCount(uint64_t val)
return (int64_t)(val & 0x000000000000007F);
}
int64_t Func_bit_count::getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType)
template<class TA>
class Func_bit_count_return_uint64: public Func_bit_count
{
if ( parm.size() != 1 )
public:
int64_t getIntVal(Row& row,
FunctionParm& parm,
bool& isNull,
CalpontSystemCatalog::ColType& operationColType) override
{
isNull = true;
return 0;
idbassert(parm.size() == 1);
return bitCount((uint64_t) TA(row, parm[0], *this).nullSafeValue(isNull));
}
};
uint64_t val = 0;
int128_t bigval = 0;
bool isBigVal;
if (!getUIntValFromParm(row, parm[0], val, isNull, *this, isBigVal, bigval))
{
std::ostringstream oss;
oss << "bit_count: datatype of " << execplan::colDataTypeToString(operationColType.colDataType);
throw logging::IDBExcept(oss.str(), ERR_DATATYPE_NOT_SUPPORT);
}
if (LIKELY(!isBigVal))
{
return bitCount(val);
}
else
{
return (bitCount(*reinterpret_cast<uint64_t*>(&bigval)) +
bitCount(*(reinterpret_cast<uint64_t*>(&bigval) + 1)));
}
bool Func_bit_count::fix(execplan::FunctionColumn &col) const
{
static Func_bit_count_return_uint64<ParmTUInt64> return_uint64_from_uint64;
static Func_bit_count_return_uint64<ParmTSInt64> return_uint64_from_sint64;
static Func_bit_count_return_uint64<BitOperandGeneric> return_uint64_generic;
if (validateArgCount(col, 1))
return false;
setFunctorByParm(col, col.functionParms()[0],
return_uint64_from_uint64,
return_uint64_from_sint64,
return_uint64_generic);
return validateBitOperandTypeOrError(col, *this, 0);
}