-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tianmu): support add integer/decimal column DDL with default value. (#1187) #1208
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
DROP DATABASE IF EXISTS issuse1187_test; | ||
CREATE DATABASE issuse1187_test; | ||
USE issuse1187_test; | ||
create table t1(id int,name varchar(5)) ; | ||
insert into t1 values(1,'AAA'),(2,'BBB'); | ||
alter table t1 add column age int null; | ||
select * from t1; | ||
id name age | ||
1 AAA NULL | ||
2 BBB NULL | ||
create table t2(id int,name varchar(5)) ; | ||
insert into t2 values(1,'AAA'),(2,'BBB'); | ||
alter table t2 add column age int not null; | ||
select * from t2; | ||
id name age | ||
1 AAA 0 | ||
2 BBB 0 | ||
create table t3(id int,name varchar(5)) ; | ||
insert into t3 values(1,'AAA'),(2,'BBB'); | ||
alter table t3 add column age int not null default 66; | ||
select * from t3; | ||
id name age | ||
1 AAA 66 | ||
2 BBB 66 | ||
insert into t3(id,name) values (3,'CCC'); | ||
select * from t3; | ||
id name age | ||
1 AAA 66 | ||
2 BBB 66 | ||
3 CCC 66 | ||
alter table t3 alter column age set default 77; | ||
insert into t3(id,name) values (4,'DDD'); | ||
select * from t3; | ||
id name age | ||
1 AAA 66 | ||
2 BBB 66 | ||
3 CCC 66 | ||
4 DDD 77 | ||
create table t4(id int,name varchar(5)) ; | ||
insert into t4 values(1,'AAA'),(2,'BBB'); | ||
alter table t4 add column age DECIMAL(17,9); | ||
select * from t4; | ||
id name age | ||
1 AAA NULL | ||
2 BBB NULL | ||
create table t5(id int,name varchar(5)) ; | ||
insert into t5 values(1,'AAA'),(2,'BBB'); | ||
alter table t5 add column age DECIMAL(17,9) not null; | ||
select * from t5; | ||
id name age | ||
1 AAA 0.000000000 | ||
2 BBB 0.000000000 | ||
create table t6(id int,name varchar(5)) ; | ||
insert into t6 values(1,'AAA'),(2,'BBB'); | ||
alter table t6 add column age DECIMAL(17,9) not null default '800.0024'; | ||
select * from t6; | ||
id name age | ||
1 AAA 800.002400000 | ||
2 BBB 800.002400000 | ||
DROP DATABASE issuse1187_test; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--source include/have_tianmu.inc | ||
|
||
--disable_warnings | ||
DROP DATABASE IF EXISTS issuse1187_test; | ||
--enable_warnings | ||
|
||
CREATE DATABASE issuse1187_test; | ||
USE issuse1187_test; | ||
|
||
create table t1(id int,name varchar(5)) ; | ||
insert into t1 values(1,'AAA'),(2,'BBB'); | ||
alter table t1 add column age int null; | ||
select * from t1; | ||
|
||
create table t2(id int,name varchar(5)) ; | ||
insert into t2 values(1,'AAA'),(2,'BBB'); | ||
alter table t2 add column age int not null; | ||
select * from t2; | ||
|
||
create table t3(id int,name varchar(5)) ; | ||
insert into t3 values(1,'AAA'),(2,'BBB'); | ||
alter table t3 add column age int not null default 66; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls, add some case for errors. such as using wrong default value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F.I.Y: Just only change the default value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
select * from t3; | ||
insert into t3(id,name) values (3,'CCC'); | ||
select * from t3; | ||
alter table t3 alter column age set default 77; | ||
insert into t3(id,name) values (4,'DDD'); | ||
select * from t3; | ||
|
||
create table t4(id int,name varchar(5)) ; | ||
insert into t4 values(1,'AAA'),(2,'BBB'); | ||
alter table t4 add column age DECIMAL(17,9); | ||
select * from t4; | ||
|
||
create table t5(id int,name varchar(5)) ; | ||
insert into t5 values(1,'AAA'),(2,'BBB'); | ||
alter table t5 add column age DECIMAL(17,9) not null; | ||
select * from t5; | ||
|
||
create table t6(id int,name varchar(5)) ; | ||
insert into t6 values(1,'AAA'),(2,'BBB'); | ||
alter table t6 add column age DECIMAL(17,9) not null default '800.0024'; | ||
select * from t6; | ||
|
||
DROP DATABASE issuse1187_test; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,19 +87,42 @@ void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8 | |
fmeta.WriteExact(&meta, sizeof(meta)); | ||
fmeta.Flush(); | ||
|
||
size_t no_nulls{no_rows}; | ||
int64_t min{0}; | ||
int64_t max{0}; | ||
bool int_not_null{false}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use AttributeTypeInfo::NotNull() to check whether the field is NULL? I guess int_not_null means has_default_value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.not null flag check is also a solution. in the scenario the alter table defined not null or null(same with tianmu), the |
||
bool is_real{false}; | ||
core::Value value = ati.GetDefaultValue(); | ||
if (ati.GetPackType() == common::PackType::INT && value.HasValue()) { | ||
int_not_null = true; | ||
} | ||
if (ATI::IsRealType(ati.Type())) { | ||
is_real = true; | ||
} | ||
|
||
if (int_not_null) { | ||
no_nulls = 0; | ||
if (is_real) { | ||
common::double_int_t t(value.GetDouble()); | ||
min = max = t.i; | ||
} else { | ||
min = max = value.GetInt(); | ||
} | ||
} | ||
|
||
COL_VER_HDR hdr{ | ||
no_rows, // no_obj | ||
no_rows, // no_nulls | ||
no_pack, // no of packs | ||
0, // no of deleted | ||
0, // auto_inc next | ||
0, // min | ||
0, // max | ||
0, // dict file version name. 0 means n/a | ||
0, // is unique? | ||
0, // is unique_updated? | ||
0, // natural size | ||
0, // compressed size | ||
no_rows, // no_obj | ||
no_nulls, // no_nulls | ||
no_pack, // no of packs | ||
0, // no of deleted | ||
0, // auto_inc next | ||
min, // min | ||
max, // max | ||
0, // dict file version name. 0 means n/a | ||
0, // is unique? | ||
0, // is unique_updated? | ||
0, // natural size | ||
0, // compressed size | ||
}; | ||
|
||
if (ati.Lookup()) { | ||
|
@@ -128,7 +151,20 @@ void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8 | |
DPN dpn; | ||
dpn.reset(); | ||
dpn.used = 1; | ||
dpn.numOfNulls = 1 << pss; | ||
if (int_not_null) { | ||
dpn.numOfNulls = 0; | ||
if (is_real) { | ||
dpn.max_d = value.GetDouble(); | ||
dpn.min_d = value.GetDouble(); | ||
dpn.sum_d = value.GetDouble() * (1 << pss); | ||
} else { | ||
dpn.max_i = value.GetInt(); | ||
dpn.min_i = value.GetInt(); | ||
dpn.sum_i = value.GetInt() * (1 << pss); | ||
} | ||
} else { | ||
dpn.numOfNulls = 1 << pss; | ||
} | ||
dpn.numOfRecords = 1 << pss; | ||
dpn.xmax = common::MAX_XID; | ||
dpn.dataAddress = DPN_INVALID_ADDR; | ||
|
@@ -141,7 +177,21 @@ void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8 | |
auto left = no_rows % (1 << pss); | ||
if (left != 0) { | ||
dpn.numOfRecords = left; | ||
dpn.numOfNulls = left; | ||
|
||
if (int_not_null) { | ||
dpn.numOfNulls = 0; | ||
if (is_real) { | ||
dpn.max_d = value.GetDouble(); | ||
dpn.min_d = value.GetDouble(); | ||
dpn.sum_d = value.GetDouble() * left; | ||
} else { | ||
dpn.max_i = value.GetInt(); | ||
dpn.min_i = value.GetInt(); | ||
dpn.sum_i = value.GetInt() * left; | ||
} | ||
} else { | ||
dpn.numOfNulls = left; | ||
} | ||
} | ||
fdn.WriteExact(&dpn, sizeof(dpn)); | ||
fdn.Flush(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when the type is varchar not null,what will happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it is convenient,please fix it together when the type is varchar not null
as below case: