Skip to content

Commit

Permalink
feat(tianmu): revert and adjust codes on bit stoneatom#919
Browse files Browse the repository at this point in the history
[summary]
1. revert some vars renaming code to keep old style.
2. fix type cast using reinterpret_cast to avoid c-style casting.
3. add more test cases and fix some errors.
4. format code.
  • Loading branch information
hustjieke authored and konghaiya committed Mar 7, 2023
1 parent 1870d88 commit a8460fe
Show file tree
Hide file tree
Showing 48 changed files with 428 additions and 236 deletions.
34 changes: 34 additions & 0 deletions mysql-test/suite/tianmu/r/bit_type.result
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ select charset(binary a), collation(binary b) from bit2;
charset(binary a) collation(binary b)
binary binary
binary binary
SELECT _latin1 b'1000001' from bit2;
_latin1 b'1000001'
A
A
SELECT _utf8mb4 0b1000001 COLLATE utf8mb4_danish_ci from bit2;
_utf8mb4 0b1000001 COLLATE utf8mb4_danish_ci
A
A
SELECT _utf8mb4 0B1000001 COLLATE utf8mb4_danish_ci from bit_test;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0B1000001 COLLATE utf8mb4_danish_ci from bit_test' at line 1
SELECT BIT_AND(a), BIT_OR(a), BIT_XOR(a), BIT_COUNT(a), bin(a >> 1), bin(a << 1), bin(~a), bin(a & b'1111'), a ^ b, a | b, a & b from bit2 group by a,b;
BIT_AND(a) BIT_OR(a) BIT_XOR(a) BIT_COUNT(a) bin(a >> 1) bin(a << 1) bin(~a) bin(a & b'1111') a ^ b a | b a & b
23 23 23 4 1011 101110 1111111111111111111111111111111111111111111111111111111111101000 111 28 31 3
Expand Down Expand Up @@ -139,10 +149,34 @@ bit8 = CAST(CONV(MID(@var3, 3, LENGTH(@var3)-3), 2, 10) AS UNSIGNED),
bit16 = CAST(CONV(MID(@var4, 3, LENGTH(@var4)-3), 2, 10) AS UNSIGNED),
bit32 = CAST(CONV(MID(@var5, 3, LENGTH(@var5)-3), 2, 10) AS UNSIGNED),
bit63 = CAST(CONV(MID(@var6, 3, LENGTH(@var6)-3), 2, 10) AS UNSIGNED);
Warnings:
Warning 1406 Data too long for column 'bit1' at row 3
select bit1+0, bit2+0, bit8+0, bit16+0, bit32+0, bit63+0 from bit_test;
bit1+0 bit2+0 bit8+0 bit16+0 bit32+0 bit63+0
NULL NULL NULL NULL NULL NULL
0 0 0 0 0 0
1 2 7 47 3071 268435455
drop table bit_test;
CREATE TABLE `bit_test` (
`bit1` bit(1) DEFAULT NULL,
`bit2` bit(2) DEFAULT NULL,
`bit8` bit(8) DEFAULT NULL,
`bit16` bit(16) DEFAULT NULL,
`bit32` bit(32) DEFAULT NULL,
`bit63` bit(63) DEFAULT NULL
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4;
insert into bit_test values(b'1', b'11',b'111',b'1111',b'11',b'111');
insert into bit_test values(b'11', b'11',b'111',b'1111',b'11',b'111');
ERROR 22001: Data too long for column 'bit1' at row 1
insert into bit_test values(b'1', b'111',b'111',b'1111',b'11',b'111');
ERROR 22001: Data too long for column 'bit2' at row 1
insert into bit_test values(b'1', b'1',b'111111111',b'1111',b'11',b'111');
ERROR 22001: Data too long for column 'bit8' at row 1
insert into bit_test values(b'1', b'1',b'1',b'11111111111111111',b'11',b'111');
ERROR 22001: Data too long for column 'bit16' at row 1
insert into bit_test values(b'1', b'1',b'1',b'1', b'111111111111111111111111111111111',b'111');
ERROR 22001: Data too long for column 'bit32' at row 1
insert into bit_test values(b'1', b'1',b'1', b'1', b'1', b'11111111111111111111111111111111111111111111111111111111111111111');
ERROR 22001: Data too long for column 'bit63' at row 1
drop table bit_test;
drop database test_bit;
35 changes: 35 additions & 0 deletions mysql-test/suite/tianmu/t/bit_type.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ SELECT DATE_ADD('2018-05-01',INTERVAL a DAY) from bit2;
select a like "10111", b not like "1011" from bit2;
select strcmp(a,b), strcmp(b,a),strcmp(a,a) from bit2;
select charset(binary a), collation(binary b) from bit2;
SELECT _latin1 b'1000001' from bit2;
SELECT _utf8mb4 0b1000001 COLLATE utf8mb4_danish_ci from bit2;
--error 1064
SELECT _utf8mb4 0B1000001 COLLATE utf8mb4_danish_ci from bit_test;

# test bit function & operators: https://dev.mysql.com/doc/refman/8.0/en/bit-functions.html
SELECT BIT_AND(a), BIT_OR(a), BIT_XOR(a), BIT_COUNT(a), bin(a >> 1), bin(a << 1), bin(~a), bin(a & b'1111'), a ^ b, a | b, a & b from bit2 group by a,b;
Expand Down Expand Up @@ -120,4 +124,35 @@ select bit1+0, bit2+0, bit8+0, bit16+0, bit32+0, bit63+0 from bit_test;

drop table bit_test;

# boundary test
CREATE TABLE `bit_test` (
`bit1` bit(1) DEFAULT NULL,
`bit2` bit(2) DEFAULT NULL,
`bit8` bit(8) DEFAULT NULL,
`bit16` bit(16) DEFAULT NULL,
`bit32` bit(32) DEFAULT NULL,
`bit63` bit(63) DEFAULT NULL
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4;

insert into bit_test values(b'1', b'11',b'111',b'1111',b'11',b'111');
# Data too long for column 'bit1' at row 1
--error 1406
insert into bit_test values(b'11', b'11',b'111',b'1111',b'11',b'111');
# Data too long for column 'bit2' at row 1
--error 1406
insert into bit_test values(b'1', b'111',b'111',b'1111',b'11',b'111');
# Data too long for column 'bit8' at row 1
--error 1406
insert into bit_test values(b'1', b'1',b'111111111',b'1111',b'11',b'111');
# Data too long for column 'bit16' at row 1
--error 1406
insert into bit_test values(b'1', b'1',b'1',b'11111111111111111',b'11',b'111');
# Data too long for column 'bit32' at row 1
--error 1406
insert into bit_test values(b'1', b'1',b'1',b'1', b'111111111111111111111111111111111',b'111');
# Data too long for column 'bit63' at row 1
--error 1406
insert into bit_test values(b'1', b'1',b'1', b'1', b'1', b'11111111111111111111111111111111111111111111111111111111111111111');
drop table bit_test;

drop database test_bit;
2 changes: 1 addition & 1 deletion storage/tianmu/common/common_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ constexpr uint64_t TIANMU_BIGINT_UNSIGNED_MAX = 0xFFFFFFFFFFFFFFFFULL; // 2^64
constexpr int32_t TIANMU_MAX_INDEX_COL_LEN_LARGE = 3072;
constexpr int32_t TIANMU_MAX_INDEX_COL_LEN_SMALL = 767;

constexpr uint32_t kTianmuBitMaxPrec = 63; // in the future we'll expand to 64.
constexpr uint32_t TIANMU_BIT_MAX_PREC = 63; // in the future we'll expand to 64.

#define NULL_VALUE_D (*(double *)("\x01\x00\x00\x00\x00\x00\x00\x80"))
#define TIANMU_INT_MAX (2147483647)
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/common/txt_data_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ uint TxtDataFormat::StaticExtrnalSize(ColumnType attrt, uint precision, int scal
return 10;
else if (attrt == ColumnType::INT)
return 11; // -2,147,483,648 ~ 2,147,483,647, 1(signed) + 10(max digits)
else if (attrt == ColumnType::BIGINT || attrt == ColumnType::BIT)
else if (attrt == ColumnType::BIGINT)
return 20; // -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807, 1(signed) + 19(max digits)
else if (attrt == ColumnType::BYTEINT || attrt == ColumnType::YEAR)
return 4;
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/aggregation_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t

if (cur_a.term.vc) {
if (!has_lookup)
has_lookup = cur_a.term.vc->Type().IsLookup();
has_lookup = cur_a.term.vc->Type().Lookup();
max_size = cur_a.term.vc->MaxStringSize();
min_v = cur_a.term.vc->RoughMin();
max_v = cur_a.term.vc->RoughMax();
Expand Down
6 changes: 3 additions & 3 deletions storage/tianmu/core/bloom_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ class BloomFilterPolicy : public FilterPolicy {
}
}

bool KeyMayMatch(const Slice &key, const Slice &bloom_filter) const override {
const size_t len = bloom_filter.size();
bool KeyMayMatch(const Slice &key, const Slice &filter) const override {
const size_t len = filter.size();
if (len < 2)
return false;

const char *array = bloom_filter.data();
const char *array = filter.data();
const size_t bits = (len - 1) * 8;

// Use the encoded k so that we can read filters generated by
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace Tianmu {
namespace core {
enum class PackOntologicalStatus { kNullsOnly = 0, kUniform, kUniformAndNulls, kSequential, kNormal };
enum class PackOntologicalStatus { NULLS_ONLY = 0, UNIFORM, UNIFORM_AND_NULLS, SEQUENTIAL, NORMAL };

/*! \brief Base class for columns.
*
Expand Down
6 changes: 3 additions & 3 deletions storage/tianmu/core/column_bin_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ bool ColumnBinEncoder::PrepareEncoder(vcolumn::VirtualColumn *_vc, vcolumn::Virt
my_encoder.reset(new ColumnBinEncoder::EncoderDate(vc, decodable, nulls_possible, descending));
} else if (vct.GetTypeName() == common::ColumnType::YEAR) {
my_encoder.reset(new ColumnBinEncoder::EncoderYear(vc, decodable, nulls_possible, descending));
} else if (!monotonic_encoding && vct.IsLookup() && _vc2 == nullptr &&
} else if (!monotonic_encoding && vct.Lookup() && _vc2 == nullptr &&
!types::RequiresUTFConversions(vc->GetCollation())) { // Lookup encoding: only non-UTF
my_encoder.reset(new ColumnBinEncoder::EncoderLookup(vc, decodable, nulls_possible, descending));
lookup_encoder = true;
} else if (!monotonic_encoding && vct.IsLookup() && _vc2 != nullptr &&
vct2.IsLookup()) { // Lookup in joining - may be UTF
} else if (!monotonic_encoding && vct.Lookup() && _vc2 != nullptr &&
vct2.Lookup()) { // Lookup in joining - may be UTF
my_encoder.reset(new ColumnBinEncoder::EncoderLookup(vc, decodable, nulls_possible, descending));
lookup_encoder = true;
} else if (vct.IsString() || vct2.IsString()) {
Expand Down
2 changes: 1 addition & 1 deletion storage/tianmu/core/column_share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void ColumnShare::read_meta() {
ct.SetScale(meta.scale);

auto type = ct.GetTypeName();
if (ct.IsLookup() || ATI::IsNumericType(type) || ATI::IsDateTimeType(type))
if (ct.Lookup() || ATI::IsNumericType(type) || ATI::IsDateTimeType(type))
pt = common::PackType::INT;
else
pt = common::PackType::STR;
Expand Down
4 changes: 2 additions & 2 deletions storage/tianmu/core/column_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ColumnType::ColumnType(const DataType &dt) {
}

bool ColumnType::operator==(const ColumnType &ct2) const {
if (type == ct2.type && IsLookup() == ct2.IsLookup() &&
if (type == ct2.type && Lookup() == ct2.Lookup() &&
std::strcmp(collation.collation->csname, ct2.collation.collation->csname) == 0 &&
(type != common::ColumnType::NUM ||
(type == common::ColumnType::NUM && precision == ct2.precision && scale == ct2.scale)))
Expand All @@ -51,7 +51,7 @@ bool ColumnType::operator==(const ColumnType &ct2) const {
}

uint ColumnType::InternalSize() {
if (IsLookup())
if (Lookup())
return 4;
else if (ATI::IsStringType(type))
return precision;
Expand Down
22 changes: 11 additions & 11 deletions storage/tianmu/core/column_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ struct DataType;

struct ColumnType {
enum class enumCT {
kNotNull = 0,
kAutoInc,
kBloomFilter,
NOT_NULL = 0,
AUTO_INC,
BLOOM_FILTER,
};

public:
Expand All @@ -42,14 +42,14 @@ struct ColumnType {
display_size(ATI::TextSize(t, prec, sc, collation)),
collation(collation),
fmt(fmt) {
flag[static_cast<int>(enumCT::kNotNull)] = notnull;
flag[static_cast<int>(enumCT::NOT_NULL)] = notnull;
internal_size = InternalSize();
}

void Initialize(common::ColumnType t, bool notnull, common::PackFmt f, uint prec, int sc,
DTCollation collation = DTCollation()) {
type = t;
flag[static_cast<int>(enumCT::kNotNull)] = notnull;
flag[static_cast<int>(enumCT::NOT_NULL)] = notnull;
fmt = f;
precision = prec;
scale = sc;
Expand Down Expand Up @@ -95,7 +95,7 @@ struct ColumnType {
// materialization of Attr
void OverrideInternalSize(uint size) { internal_size = size; };
int GetDisplaySize() const { return display_size; }
bool IsLookup() const { return fmt == common::PackFmt::LOOKUP; }
bool Lookup() const { return fmt == common::PackFmt::LOOKUP; }
ColumnType RemovedLookup() const;

bool IsNumeric() const {
Expand All @@ -121,7 +121,7 @@ struct ColumnType {
const DTCollation &GetCollation() const { return collation; }
void SetCollation(DTCollation _collation) { collation = _collation; }
bool IsNumComparable(const ColumnType &sec) const {
if (IsLookup() || sec.IsLookup() || IsString() || sec.IsString())
if (Lookup() || sec.Lookup() || IsString() || sec.IsString())
return false;
if (scale != sec.scale)
return false;
Expand All @@ -137,11 +137,11 @@ struct ColumnType {
void SetFmt(common::PackFmt f) { fmt = f; }
unsigned char GetFlag() const { return flag.to_ulong(); }
void SetFlag(unsigned char v) { flag = std::bitset<std::numeric_limits<unsigned char>::digits>(v); }
bool NotNull() const { return flag[static_cast<int>(enumCT::kNotNull)]; }
bool NotNull() const { return flag[static_cast<int>(enumCT::NOT_NULL)]; }
bool IsNullable() const { return !NotNull(); }
bool GetAutoInc() const { return flag[static_cast<int>(enumCT::kAutoInc)]; }
void SetAutoInc(bool inc) { flag[static_cast<int>(enumCT::kAutoInc)] = inc; }
bool HasFilter() const { return flag[static_cast<int>(enumCT::kBloomFilter)]; }
bool GetAutoInc() const { return flag[static_cast<int>(enumCT::AUTO_INC)]; }
void SetAutoInc(bool inc) { flag[static_cast<int>(enumCT::AUTO_INC)] = inc; }
bool HasFilter() const { return flag[static_cast<int>(enumCT::BLOOM_FILTER)]; }
bool GetUnsigned() const { return is_unsigned; }
void SetUnsigned(bool unsigned_) { is_unsigned = unsigned_; }

Expand Down
18 changes: 9 additions & 9 deletions storage/tianmu/core/condition_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void ConditionEncoder::EncodeConditionOnStringColumn() {

if (desc->op == common::Operator::O_IN || desc->op == common::Operator::O_NOT_IN)
TransformINs();
else if (!attr->Type().IsLookup())
else if (!attr->Type().Lookup())
TransformOtherThanINsOnNotLookup();
else
TransformOtherThanINsOnNumerics();
Expand Down Expand Up @@ -413,15 +413,15 @@ void ConditionEncoder::TransformLIKEsPattern() {
desc->op = common::Operator::O_FALSE;
else
desc->op = common::Operator::O_NOT_NULL;
} else if (attr->Type().IsLookup())
} else if (attr->Type().Lookup())
TransformLIKEsIntoINsOnLookup();
else
encoding_done = true;
}

void ConditionEncoder::TransformLIKEsIntoINsOnLookup() {
MEASURE_FET("ConditionEncoder::TransformLIKEsIntoINsOnLookup(...)");
DEBUG_ASSERT(attr->Type().IsLookup());
DEBUG_ASSERT(attr->Type().Lookup());

if (desc->op == common::Operator::O_LIKE)
desc->op = common::Operator::O_IN;
Expand Down Expand Up @@ -460,7 +460,7 @@ void ConditionEncoder::TransformLIKEs() {
if (!IsTransformationNeeded())
return;

if (attr->Type().IsLookup() && (desc->op == common::Operator::O_LIKE || desc->op == common::Operator::O_NOT_LIKE))
if (attr->Type().Lookup() && (desc->op == common::Operator::O_LIKE || desc->op == common::Operator::O_NOT_LIKE))
TransformLIKEsIntoINsOnLookup();
}

Expand Down Expand Up @@ -579,7 +579,7 @@ void ConditionEncoder::TextTransformation() {
}
if (desc->op == common::Operator::O_LIKE || desc->op == common::Operator::O_NOT_LIKE) {
TransformLIKEs();
} else if (attr->Type().IsLookup()) { // lookup - transform into IN (numbers)
} else if (attr->Type().Lookup()) { // lookup - transform into IN (numbers)
if (desc->op == common::Operator::O_IN || desc->op == common::Operator::O_NOT_IN)
TransformINsOnLookup();
else
Expand Down Expand Up @@ -609,7 +609,7 @@ void ConditionEncoder::TransformINs() {
desc->op = common::Operator::O_FALSE;
} else {
if (no_dis_values == 1 && !mvc.ContainsNull(mit)) {
if (attr->GetPackType() == common::PackType::INT && !attr->Type().IsLookup()) {
if (attr->GetPackType() == common::PackType::INT && !attr->Type().Lookup()) {
desc->val2 = CQTerm();
desc->val2.vc =
new vcolumn::ConstColumn(ValueOrNull(types::TianmuNum(attr->EncodeValue64(mvc.GetSetMin(mit), sharp),
Expand Down Expand Up @@ -638,7 +638,7 @@ void ConditionEncoder::TransformINs() {
}
} else if (attr->GetPackType() == common::PackType::INT && !mvc.ContainsNull(mit)) {
int64_t val_min, val_max;
if (attr->Type().IsLookup()) {
if (attr->Type().Lookup()) {
val_min = int64_t((types::TianmuNum &)mvc.GetSetMin(mit));
val_max = int64_t((types::TianmuNum &)mvc.GetSetMax(mit));
} else {
Expand Down Expand Up @@ -774,8 +774,8 @@ void ConditionEncoder::EncodeIfPossible(Descriptor &desc, bool for_rough_query,
if (vcsc2 == nullptr || vcsc->GetVarMap()[0].GetTabPtr()->TableType() != TType::TABLE ||
vcsc2->GetVarMap()[0].GetTabPtr()->TableType() != TType::TABLE)
return;
if (vcsc->Type().IsString() || vcsc->Type().IsLookup() || vcsc2->Type().IsString() ||
vcsc2->Type().IsLookup()) // excluding strings
if (vcsc->Type().IsString() || vcsc->Type().Lookup() || vcsc2->Type().IsString() ||
vcsc2->Type().Lookup()) // excluding strings
return;
bool is_timestamp1 = (vcsc->Type().GetTypeName() == common::ColumnType::TIMESTAMP);
bool is_timestamp2 = (vcsc2->Type().GetTypeName() == common::ColumnType::TIMESTAMP);
Expand Down
5 changes: 3 additions & 2 deletions storage/tianmu/core/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ DataType::DataType(common::ColumnType atype, int prec, int scale, DTCollation co
fixmax = MAX(TIANMU_TINYINT_MAX, -TIANMU_TINYINT_MIN);
break;
case common::ColumnType::BIT:
DEBUG_ASSERT((prec > 0) && (prec <= common::kTianmuBitMaxPrec));
DEBUG_ASSERT((prec > 0) && (prec <= common::TIANMU_BIT_MAX_PREC));
valtype = ValueType::VT_FIXED;
fixmax = MAX(TIANMU_TINYINT_MAX, -TIANMU_TINYINT_MIN); // TODO(fix max value with common::kTianmuBitMaxPrec)
fixmax = MAX(common::TIANMU_BIGINT_MAX,
-common::TIANMU_BIGINT_MIN); // TODO(fix max value with common::TIANMU_BIT_MAX_PREC)
break;

case common::ColumnType::NUM:
Expand Down
Loading

0 comments on commit a8460fe

Please sign in to comment.