From 0df3d897cccb1c2e93042e041480c2b5972cc7cf Mon Sep 17 00:00:00 2001 From: kamille Date: Mon, 6 Feb 2023 15:56:50 +0800 Subject: [PATCH] add test. --- table_engine/src/provider.rs | 130 +++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/table_engine/src/provider.rs b/table_engine/src/provider.rs index 96a3a266f9..7b14b1ff2f 100644 --- a/table_engine/src/provider.rs +++ b/table_engine/src/provider.rs @@ -379,3 +379,133 @@ impl fmt::Debug for ScanTable { .finish() } } + +#[cfg(test)] +mod test { + use std::sync::Arc; + + use common_types::{ + column_schema, + datum::DatumKind, + schema::{Builder, Schema}, + }; + use datafusion::scalar::ScalarValue; + use datafusion_expr::{col, Expr}; + + use super::TableProviderAdapter; + use crate::{memory::MemoryTable, table::TableId}; + + fn build_user_defined_primary_key_schema() -> Schema { + Builder::new() + .auto_increment_column_id(true) + .add_key_column( + column_schema::Builder::new("key1".to_string(), DatumKind::String) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_key_column( + column_schema::Builder::new("timestamp".to_string(), DatumKind::Timestamp) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_normal_column( + column_schema::Builder::new("field1".to_string(), DatumKind::String) + .is_tag(true) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_normal_column( + column_schema::Builder::new("field2".to_string(), DatumKind::Double) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .build() + .unwrap() + } + + fn build_default_primary_key_schema() -> Schema { + Builder::new() + .auto_increment_column_id(true) + .add_key_column( + column_schema::Builder::new("timestamp".to_string(), DatumKind::Timestamp) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_key_column( + column_schema::Builder::new("tsid".to_string(), DatumKind::UInt64) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_normal_column( + column_schema::Builder::new("field1".to_string(), DatumKind::String) + .is_tag(true) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .add_normal_column( + column_schema::Builder::new("field2".to_string(), DatumKind::Double) + .build() + .expect("should succeed build column schema"), + ) + .unwrap() + .build() + .unwrap() + } + + fn build_filters() -> Vec { + let filter1 = col("timestamp").lt(Expr::Literal(ScalarValue::UInt64(Some(10086)))); + let filter2 = col("key1").eq(Expr::Literal(ScalarValue::Utf8(Some("10086".to_string())))); + let filter3 = col("field1").eq(Expr::Literal(ScalarValue::Utf8(Some("10087".to_string())))); + let filter4 = col("field2").eq(Expr::Literal(ScalarValue::Float64(Some(10088.0)))); + + vec![filter1, filter2, filter3, filter4] + } + + #[test] + pub fn test_push_down_in_user_defined_primary_key_case() { + let test_filters = build_filters(); + let user_defined_pk_schema = build_user_defined_primary_key_schema(); + + let table = MemoryTable::new( + "test_table".to_string(), + TableId::new(0), + user_defined_pk_schema, + "memory".to_string(), + ); + let provider = TableProviderAdapter::new(Arc::new(table), 1); + let predicate = provider.check_and_build_predicate_from_filters(&test_filters); + + let expected_filters = vec![test_filters[0].clone(), test_filters[1].clone()]; + assert_eq!(predicate.exprs(), &expected_filters); + } + + #[test] + pub fn test_push_down_in_default_primary_key_case() { + let test_filters = build_filters(); + let test_filters = vec![ + test_filters[0].clone(), + test_filters[2].clone(), + test_filters[3].clone(), + ]; + let default_pk_schema = build_default_primary_key_schema(); + + let table = MemoryTable::new( + "test_table".to_string(), + TableId::new(0), + default_pk_schema, + "memory".to_string(), + ); + let provider = TableProviderAdapter::new(Arc::new(table), 1); + let predicate = provider.check_and_build_predicate_from_filters(&test_filters); + + let expected_filters = vec![test_filters[0].clone(), test_filters[1].clone()]; + assert_eq!(predicate.exprs(), &expected_filters); + } +}