Skip to content

Commit

Permalink
feat(tianmu): impl bit type, add support to create/desc table with bi…
Browse files Browse the repository at this point in the history
…t type stoneatom#919

[summary]
1. add support to create table with bit type
2. add support to desc/show fields with bit type table
3. currently we design bit(M) with (1 <= M <= 63), in the future we'll expand prec to 64.
  • Loading branch information
hustjieke committed Dec 15, 2022
1 parent 5eff821 commit a016f83
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
30 changes: 28 additions & 2 deletions mysql-test/suite/tianmu/r/create_table.result
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,33 @@ t1 CREATE TABLE `t1` (
`fld2` datetime DEFAULT '1211-01-01 00:00:00'
) ENGINE=TIANMU DEFAULT CHARSET=latin1
# error of support type
create table t_unsupport_type (a2 bit DEFAULT NULL);
ERROR HY000: Unsupported data type[bit]
create table t_unsupport_type (a2 json DEFAULT NULL);
ERROR HY000: Unsupported data type[json]
drop table if exists bittypes;
Warnings:
Note 1051 Unknown table 'test.bittypes'
create table bittypes (
id int not null primary key,
bit1 bit(1),
bit2 bit(2),
bit4 bit(4),
bit8 bit(8),
bit16 bit(16),
bit32 bit(32),
bit63 bit(63)
) ENGINE=tianmu DEFAULT CHARSET=utf8mb4;
show create table bittypes;
Table Create Table
bittypes CREATE TABLE `bittypes` (
`id` int(11) NOT NULL,
`bit1` bit(1) DEFAULT NULL,
`bit2` bit(2) DEFAULT NULL,
`bit4` bit(4) DEFAULT NULL,
`bit8` bit(8) DEFAULT NULL,
`bit16` bit(16) DEFAULT NULL,
`bit32` bit(32) DEFAULT NULL,
`bit63` bit(63) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4
create table tc(a bit(64)) engine=tianmu;
ERROR HY000: The bit(M) type, M must be less than or equal to 63 in tianmu engine.
21 changes: 19 additions & 2 deletions mysql-test/suite/tianmu/t/create_table.test
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ show create table t1;

--echo # error of support type
--error 6
create table t_unsupport_type (a2 bit DEFAULT NULL);
--error 6
create table t_unsupport_type (a2 json DEFAULT NULL);

# test new type bit
drop table if exists bittypes;
create table bittypes (
id int not null primary key,

bit1 bit(1),
bit2 bit(2),
bit4 bit(4),
bit8 bit(8),
bit16 bit(16),
bit32 bit(32),
bit63 bit(63)

) ENGINE=tianmu DEFAULT CHARSET=utf8mb4;
show create table bittypes;

--error 6
create table tc(a bit(64)) engine=tianmu;
3 changes: 3 additions & 0 deletions storage/tianmu/common/common_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum class ColumnType : unsigned char {
MEDIUMINT,
BIGINT,
LONGTEXT,
BIT,
UNK = 255
};

Expand Down Expand Up @@ -137,6 +138,8 @@ constexpr int64_t TIANMU_BIGINT_MIN = NULL_VALUE_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.

#define NULL_VALUE_D (*(double *)("\x01\x00\x00\x00\x00\x00\x00\x80"))
#define TIANMU_INT_MIN (-2147483647)
#define TIANMU_MEDIUMINT_MAX ((1 << 23) - 1)
Expand Down
4 changes: 2 additions & 2 deletions storage/tianmu/core/aggregator_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ types::BString AggregatorListT::GetValueT(unsigned char *buf) {
if ((*p != static_cast<char>(AggregatedValue_Mark::AM_NOT_FILL)) &&
(*p != static_cast<char>(
AggregatedValue_Mark::AM_STRING_NULL))) // empty string indicator: len==0 and nontrivial character
return types::BString("", 0, true); // empty string
return types::BString(); // null value
return types::BString("", 0, true); // empty string
return types::BString(); // null value
}
types::BString res(p, len);
return res;
Expand Down
5 changes: 5 additions & 0 deletions storage/tianmu/core/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ DataType::DataType(common::ColumnType atype, int prec, int scale, DTCollation co
valtype = ValueType::VT_FIXED;
fixmax = MAX(TIANMU_TINYINT_MAX, -TIANMU_TINYINT_MIN);
break;
case common::ColumnType::BIT:
DEBUG_ASSERT((prec > 0) && (prec <= common::kTianmuBitMaxPrec));
valtype = ValueType::VT_FIXED;
fixmax = MAX(TIANMU_TINYINT_MAX, -TIANMU_TINYINT_MIN); // TODO(fix max value with common::kTianmuBitMaxPrec)
break;

case common::ColumnType::NUM:
DEBUG_ASSERT((prec > 0) && (prec <= 19) && (fixscale >= 0));
Expand Down
16 changes: 15 additions & 1 deletion storage/tianmu/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ void Engine::EncodeRecord(const std::string &table_path, int table_id, Field **f
*(int64_t *)ptr = v;
ptr += sizeof(int64_t);
} break;
case MYSQL_TYPE_BIT: {
int64_t v = f->val_int();
ASSERT(v < 0, "bit type data should never less than 0.");
if (v > common::TIANMU_BIGINT_MAX) // how can v > bigint max ??
v = common::TIANMU_BIGINT_MAX; // TODO(fix with bit prec)
*(int64_t *)ptr = v;
ptr += sizeof(int64_t);
} break;
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE: {
Expand Down Expand Up @@ -555,7 +563,6 @@ void Engine::EncodeRecord(const std::string &table_path, int table_id, Field **f
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_BIT:
default:
throw common::Exception("unsupported mysql type " + std::to_string(f->type()));
break;
Expand Down Expand Up @@ -710,6 +717,13 @@ AttributeTypeInfo Engine::GetAttrTypeInfo(const Field &field) {
}
throw common::UnsupportedDataTypeException();
}
case MYSQL_TYPE_BIT: {
const Field_bit_as_char *f_bit = ((const Field_bit_as_char *)&field);
if (/*f_bit->field_length > 0 && */ f_bit->field_length <= common::kTianmuBitMaxPrec)
return AttributeTypeInfo(common::ColumnType::NUM, notnull, f_bit->field_length);
throw common::UnsupportedDataTypeException(
"The bit(M) type, M must be less than or equal to 63 in tianmu engine.");
}
case MYSQL_TYPE_NEWDECIMAL: {
const Field_new_decimal *fnd = ((const Field_new_decimal *)&field);
if (/*fnd->precision > 0 && */ fnd->precision <= 18 /*&& fnd->dec >= 0*/
Expand Down
10 changes: 6 additions & 4 deletions storage/tianmu/core/tianmu_attr_typeinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ class ATI {
}
static bool IsFixedNumericType(common::ColumnType attr_type) {
return IsInteger32Type(attr_type) || attr_type == common::ColumnType::BIGINT ||
attr_type == common::ColumnType::NUM;
attr_type == common::ColumnType::NUM || attr_type == common::ColumnType::BIT;
}

static bool IsRealType(common::ColumnType attr_type) {
return attr_type == common::ColumnType::FLOAT || attr_type == common::ColumnType::REAL;
}

static bool IsBitType(common::ColumnType attr_type) { return attr_type == common::ColumnType::BIT; }

static bool IsNumericType(common::ColumnType attr_type) {
return IsInteger32Type(attr_type) || attr_type == common::ColumnType::BIGINT ||
attr_type == common::ColumnType::NUM || attr_type == common::ColumnType::FLOAT ||
attr_type == common::ColumnType::REAL;
return IsIntegerType(attr_type) || attr_type == common::ColumnType::NUM || attr_type == common::ColumnType::FLOAT ||
attr_type == common::ColumnType::REAL || attr_type == common::ColumnType::BIT;
}

static bool IsBinType(common::ColumnType attr_type) {
Expand Down

0 comments on commit a016f83

Please sign in to comment.