Skip to content

Commit

Permalink
fix: Tuple::serialize_to & Tuple::deserialize_from for `LogicalTy…
Browse files Browse the repository at this point in the history
…pe::Char`
  • Loading branch information
KKould committed Mar 21, 2024
1 parent 91632ec commit 06aac47
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/execution/volcano/dql/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Describe {
datatype
.raw_len()
.map(|len| len.to_string())
.unwrap_or_default(),
.unwrap_or_else(|| "DYNAMIC".to_string()),
))),
Arc::new(DataValue::Utf8(Some(column.nullable.to_string()))),
key_fn(column),
Expand Down
9 changes: 7 additions & 2 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ impl Tuple {
} else {
let mut value_bytes = value.to_raw();

if types[i].raw_len().is_none() {
if let Some(len) = types[i].raw_len() {
let difference = len.saturating_sub(value_bytes.len());

bytes.append(&mut value_bytes);
bytes.append(&mut vec![0; difference]);
} else {
bytes.append(&mut (value_bytes.len() as u32).encode_fixed_vec());
bytes.append(&mut value_bytes);
}
bytes.append(&mut value_bytes);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,18 @@ impl DataValue {
buf.copy_from_slice(bytes);
f64::from_ne_bytes(buf)
})),
LogicalType::Char(_) | LogicalType::Varchar(_) => DataValue::Utf8(
LogicalType::Char(_) => {
// https://dev.mysql.com/doc/refman/8.0/en/char.html#:~:text=If%20a%20given%20value%20is%20stored%20into%20the%20CHAR(4)%20and%20VARCHAR(4)%20columns%2C%20the%20values%20retrieved%20from%20the%20columns%20are%20not%20always%20the%20same%20because%20trailing%20spaces%20are%20removed%20from%20CHAR%20columns%20upon%20retrieval.%20The%20following%20example%20illustrates%20this%20difference%3A
let value = (!bytes.is_empty()).then(|| {
let last_non_zero_index = match bytes.iter().rposition(|&x| x != 0) {
Some(index) => index + 1,
None => 0,
};
String::from_utf8(bytes[0..last_non_zero_index].to_owned()).unwrap()
});
DataValue::Utf8(value)
}
LogicalType::Varchar(_) => DataValue::Utf8(
(!bytes.is_empty()).then(|| String::from_utf8(bytes.to_owned()).unwrap()),
),
LogicalType::Date => {
Expand Down
6 changes: 3 additions & 3 deletions tests/slt/describe.slt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ create table t9 (c1 int primary key, c2 int default 0, c3 varchar unique);
query TTTTI
describe t9;
----
c1 INTEGER false PRIMARY null
c2 INTEGER true EMPTY 0
c3 VARCHAR true UNIQUE null
c1 INTEGER 4 false PRIMARY null
c2 INTEGER 4 true EMPTY 0
c3 VARCHAR DYNAMIC true UNIQUE null

statement ok
drop table t9;
29 changes: 28 additions & 1 deletion tests/slt/insert.slt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ select * from t
7 10 1 null
8 null null null

statement ok
drop table t;

statement ok
create table t1(id int primary key, v1 bigint default 233)

Expand Down Expand Up @@ -73,4 +76,28 @@ select * from t1
2 233
3 233
4 0
5 233
5 233

statement ok
drop table t1;

statement ok
create table t2(id int primary key, v1 char(10), v2 varchar);

statement ok
insert into t2 (id, v1, v2) values (0, 'foo', 'foo');

query ITT
select * from t2;
----
0 foo foo

query B
select v1 = v2 from t2;
----
true

statement ok
drop table t2;


16 changes: 8 additions & 8 deletions tests/slt/sql_2016/E021_10.slt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# E021-10: Implicit casting among the fixed-length and variable-length character string types

# TODO: CHARACTER and CHARACTER VARYING
statement ok
CREATE TABLE TABLE_E021_10_01_01 ( ID INT PRIMARY KEY, A CHARACTER ( 10 ) , B CHARACTER VARYING ( 15 ) );

# statement ok
# CREATE TABLE TABLE_E021_10_01_01 ( A CHARACTER ( 10 ) , B CHARACTER VARYING ( 15 ) );
statement ok
INSERT INTO TABLE_E021_10_01_01 ( ID, A, B ) VALUES ( 0, 'foo' , 'bar' );

# statement ok
# INSERT INTO TABLE_E021_10_01_01 ( A, B ) VALUES ( 'foo' , 'bar' );

# query B
# SELECT A = B FROM TABLE_E021_10_01_01
query B
SELECT A = B FROM TABLE_E021_10_01_01
----
false

0 comments on commit 06aac47

Please sign in to comment.