Skip to content

Commit

Permalink
test: add overflow.slt & select.slt
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Apr 28, 2024
1 parent 48cab9c commit e2b4a82
Show file tree
Hide file tree
Showing 8 changed files with 593 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/execution/volcano/dql/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl SumAccumulator {
assert!(ty.is_numeric());

Ok(Self {
result: DataValue::init(ty),
result: DataValue::none(ty),
evaluator: EvaluatorFactory::binary_create(*ty, BinaryOperator::Plus)?,
})
}
Expand All @@ -27,7 +27,11 @@ impl SumAccumulator {
impl Accumulator for SumAccumulator {
fn update_value(&mut self, value: &ValueRef) -> Result<(), DatabaseError> {
if !value.is_null() {
self.result = self.evaluator.0.binary_eval(&self.result, value);
if self.result.is_null() {
self.result = DataValue::clone(value);
} else {
self.result = self.evaluator.0.binary_eval(&self.result, value);
}
}

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ pub trait Transaction: Sync + Send + 'static + Sized {
columns: Vec<ColumnCatalog>,
if_not_exists: bool,
) -> Result<TableName, DatabaseError> {
let mut table_catalog = TableCatalog::new(table_name.clone(), columns)?;
let (_, column) = table_catalog.primary_key()?;

TableCodec::check_primary_key_type(column.datatype())?;

let (table_key, value) =
TableCodec::encode_root_table(&TableMeta::empty(table_name.clone()))?;
if self.get(&table_key)?.is_some() {
Expand All @@ -284,11 +289,8 @@ pub trait Transaction: Sync + Send + 'static + Sized {
}
return Err(DatabaseError::TableExists);
}
self.set(table_key, value)?;

let mut table_catalog = TableCatalog::new(table_name.clone(), columns)?;

self.create_index_meta_for_table(&mut table_catalog)?;
self.set(table_key, value)?;

for column in table_catalog.columns() {
let (key, value) = TableCodec::encode_column(&table_name, column)?;
Expand Down
35 changes: 21 additions & 14 deletions src/storage/table_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ enum CodecType {
}

impl TableCodec {
pub fn check_primary_key_type(ty: &LogicalType) -> Result<(), DatabaseError> {
if !matches!(
ty,
LogicalType::Tinyint
| LogicalType::Smallint
| LogicalType::Integer
| LogicalType::Bigint
| LogicalType::UTinyint
| LogicalType::USmallint
| LogicalType::UInteger
| LogicalType::UBigint
| LogicalType::Char(..)
| LogicalType::Varchar(..)
) {
return Err(DatabaseError::InvalidType);
}
Ok(())
}

/// TableName + Type
///
/// Tips: Root full key = key_prefix
Expand Down Expand Up @@ -159,23 +178,11 @@ impl TableCodec {
table_name: &str,
tuple_id: &TupleId,
) -> Result<Vec<u8>, DatabaseError> {
Self::check_primary_key_type(&tuple_id.logical_type())?;

let mut key_prefix = Self::key_prefix(CodecType::Tuple, table_name);
key_prefix.push(BOUND_MIN_TAG);

if !matches!(
tuple_id.logical_type(),
LogicalType::Tinyint
| LogicalType::Smallint
| LogicalType::Integer
| LogicalType::Bigint
| LogicalType::UTinyint
| LogicalType::USmallint
| LogicalType::UInteger
| LogicalType::UBigint
| LogicalType::Varchar(..)
) {
return Err(DatabaseError::InvalidType);
}
tuple_id.memcomparable_encode(&mut key_prefix)?;

Ok(key_prefix)
Expand Down
24 changes: 24 additions & 0 deletions tests/slt/crdb/overflow.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
statement ok
drop table if exists large_numbers

statement ok
CREATE TABLE large_numbers (a TINYINT PRIMARY KEY)

statement error
INSERT INTO large_numbers VALUES (9223372036854775807),(1)

query I
SELECT sum(a) FROM large_numbers
----
null

statement ok
DELETE FROM large_numbers

statement error
INSERT INTO large_numbers VALUES (-9223372036854775808),(-1)

query I
SELECT sum(a) FROM large_numbers
----
null
Loading

0 comments on commit e2b4a82

Please sign in to comment.