Skip to content
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

Wrap DataSchem create with metadata #473

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions common/datablocks/src/data_block_kernel_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ fn test_data_block_kernel_take() -> anyhow::Result<()> {

use crate::*;

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Utf8, false),
]));
]);

let raw = DataBlock::create(schema.clone(), vec![
Arc::new(Int64Array::from(vec![1, 2, 3])),
Expand Down Expand Up @@ -44,10 +44,10 @@ fn test_data_block_kernel_concat() -> anyhow::Result<()> {

use crate::*;

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Utf8, false),
]));
]);

let blocks = vec![
DataBlock::create(schema.clone(), vec![
Expand Down Expand Up @@ -95,10 +95,10 @@ fn test_data_block_sort() -> anyhow::Result<()> {
use crate::data_block_kernel::SortColumnDescription;
use crate::*;

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Utf8, false),
]));
]);

let raw = DataBlock::create(schema.clone(), vec![
Arc::new(Int64Array::from(vec![6, 4, 3, 2, 1, 7])),
Expand Down
7 changes: 2 additions & 5 deletions common/datablocks/src/data_block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ fn test_data_block() -> anyhow::Result<()> {

use crate::DataBlock;

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Int64,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Int64, false)]);

let block = DataBlock::create(schema.clone(), vec![Arc::new(Int64Array::from(vec![
1, 2, 3,
Expand Down
18 changes: 18 additions & 0 deletions common/datavalues/src/data_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
//
// SPDX-License-Identifier: Apache-2.0.

use std::collections::HashMap;
use std::sync::Arc;

use common_arrow::arrow;

use crate::DataField;

pub type DataSchema = arrow::datatypes::Schema;
pub type DataSchemaRef = arrow::datatypes::SchemaRef;

pub struct DataSchemaRefExt;
impl DataSchemaRefExt {
pub fn create_with_metadata(fields: Vec<DataField>) -> DataSchemaRef {
// Custom metadata is for deser_json, or it will returns error: value: Error("missing field `metadata`")
// https://github.com/apache/arrow-rs/issues/241
let metadata: HashMap<String, String> = [("k".to_string(), "v".to_string())]
.iter()
.cloned()
.collect();
Arc::new(DataSchema::new_with_metadata(fields, metadata))
}
}
1 change: 1 addition & 0 deletions common/datavalues/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub use crate::data_columnar_value::DataColumnarValue;
pub use crate::data_field::DataField;
pub use crate::data_schema::DataSchema;
pub use crate::data_schema::DataSchemaRef;
pub use crate::data_schema::DataSchemaRefExt;
pub use crate::data_type::DataType;
pub use crate::data_type::*;
pub use crate::data_value::DataValue;
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/aggregators/aggregator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn test_aggregator_function() -> Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Int64, false),
]));
]);
let block = DataBlock::create(schema.clone(), vec![
Arc::new(Int64Array::from(vec![4, 3, 2, 1])),
Arc::new(Int64Array::from(vec![1, 2, 3, 4])),
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/arithmetics/arithmetic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ fn test_arithmetic_function() -> Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Int64, false),
DataField::new("c", DataType::Int16, false),
]));
]);

let field_a = ColumnFunction::try_create("a")?;
let field_b = ColumnFunction::try_create("b")?;
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/comparisons/comparison_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ fn test_comparison_function() -> Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Int64, false),
DataField::new("b", DataType::Int64, false),
]));
]);

let field_a = ColumnFunction::try_create("a").unwrap();
let field_b = ColumnFunction::try_create("b").unwrap();
Expand Down
8 changes: 4 additions & 4 deletions common/functions/src/expressions/cast_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ fn test_cast_function() -> Result<()> {
display: "CAST(a)",
nullable: false,
block: DataBlock::create(
Arc::new(DataSchema::new(vec![DataField::new(
DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"a",
DataType::Int64,
false
)])),
)]),
vec![Arc::new(Int64Array::from(vec![4, 3, 2, 4]))]
),
func: CastFunction::create(field_a.clone(), DataType::Int8),
Expand All @@ -49,11 +49,11 @@ fn test_cast_function() -> Result<()> {
display: "CAST(a)",
nullable: false,
block: DataBlock::create(
Arc::new(DataSchema::new(vec![DataField::new(
DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"a",
DataType::Utf8,
false
)])),
)]),
vec![Arc::new(StringArray::from(vec!["20210305", "20211024"]))]
),
func: CastFunction::create(field_a.clone(), DataType::Int32),
Expand Down
7 changes: 2 additions & 5 deletions common/functions/src/function_column_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ fn test_column_function() -> anyhow::Result<()> {

use crate::*;

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Boolean,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Boolean, false)]);
let block = DataBlock::create(schema.clone(), vec![Arc::new(BooleanArray::from(vec![
true, true, true, false,
]))]);
Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/logics/logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ fn test_logic_function() -> anyhow::Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Boolean, false),
DataField::new("b", DataType::Boolean, false),
]));
]);

let field_a = ColumnFunction::try_create("a").unwrap();
let field_b = ColumnFunction::try_create("b").unwrap();
Expand Down
7 changes: 2 additions & 5 deletions common/functions/src/udfs/to_type_name_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ fn test_to_type_name_function() -> anyhow::Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Boolean,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Boolean, false)]);

let field_a = ColumnFunction::try_create("a")?;

Expand Down
4 changes: 2 additions & 2 deletions common/functions/src/udfs/udf_example_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fn test_udf_example_function() -> anyhow::Result<()> {
func: Box<dyn IFunction>
}

let schema = Arc::new(DataSchema::new(vec![
let schema = DataSchemaRefExt::create_with_metadata(vec![
DataField::new("a", DataType::Boolean, false),
DataField::new("b", DataType::Boolean, false),
]));
]);

let field_a = ColumnFunction::try_create("a").unwrap();
let field_b = ColumnFunction::try_create("b").unwrap();
Expand Down
13 changes: 9 additions & 4 deletions common/planners/src/plan_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
use common_datavalues::DataField;
use common_datavalues::DataSchema;
use common_datavalues::DataSchemaRef;
use common_datavalues::DataSchemaRefExt;
use common_exception::Result;

use crate::col;
Expand Down Expand Up @@ -94,7 +95,7 @@ impl PlanBuilder {
Ok(Self::from(&PlanNode::Projection(ProjectionPlan {
input: Arc::new(self.plan.clone()),
expr: projection_exprs,
schema: Arc::new(DataSchema::new(fields))
schema: DataSchemaRefExt::create_with_metadata(fields)
})))
}

Expand All @@ -119,7 +120,7 @@ impl PlanBuilder {
input: Arc::new(self.plan.clone()),
aggr_expr,
group_expr,
schema: Arc::new(DataSchema::new(aggr_projection_fields))
schema: DataSchemaRefExt::create_with_metadata(aggr_projection_fields)
}))
})
}
Expand Down Expand Up @@ -152,9 +153,13 @@ impl PlanBuilder {
limit: Option<usize>
) -> Result<Self> {
let table_schema = DataSchemaRef::new(table_schema.clone());
let projected_schema = projection
let projected_schema = projection.clone().map(|p| {
DataSchemaRefExt::create_with_metadata(
p.iter().map(|i| table_schema.field(*i).clone()).collect()
)
.as_ref()
.clone()
.map(|p| DataSchema::new(p.iter().map(|i| table_schema.field(*i).clone()).collect()));
});
let projected_schema = match projected_schema {
None => table_schema.clone(),
Some(v) => Arc::new(v)
Expand Down
10 changes: 3 additions & 7 deletions common/planners/src/plan_display_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// SPDX-License-Identifier: Apache-2.0.

use std::collections::HashMap;
use std::sync::Arc;

use common_datavalues::DataField;
use common_datavalues::DataSchema;
use common_datavalues::DataSchemaRefExt;
use common_datavalues::DataType;
use pretty_assertions::assert_eq;

Expand All @@ -18,11 +17,8 @@ use crate::TableEngineType;
fn test_plan_display_indent() -> anyhow::Result<()> {
// TODO test other plan type

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Int64,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Int64, false)]);

let mut options = HashMap::new();
options.insert("opt_foo".to_string(), "opt_bar".to_string());
Expand Down
4 changes: 2 additions & 2 deletions common/planners/src/plan_projection_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ fn test_projection_plan() -> anyhow::Result<()> {

let projection = PlanNode::Projection(ProjectionPlan {
expr: vec![col("a")],
schema: Arc::new(DataSchema::new(vec![DataField::new(
schema: DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"a",
DataType::Utf8,
false
)])),
)]),
input: Arc::from(PlanBuilder::empty().build()?)
});
let _ = projection.schema();
Expand Down
10 changes: 4 additions & 6 deletions common/planners/src/plan_scan_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

#[test]
fn test_scan_plan() -> anyhow::Result<()> {
use std::sync::Arc;

use common_datavalues::*;
use pretty_assertions::assert_eq;

use crate::*;

let scan = PlanNode::Scan(ScanPlan {
schema_name: "scan_test".to_string(),
table_schema: Arc::new(DataSchema::new(vec![DataField::new(
table_schema: DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"a",
DataType::Utf8,
false
)])),
)]),
table_args: None,
projection: None,
projected_schema: Arc::new(DataSchema::new(vec![DataField::new(
projected_schema: DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"a",
DataType::Utf8,
false
)])),
)]),
filters: vec![],
limit: None
});
Expand Down
7 changes: 2 additions & 5 deletions common/planners/src/plan_select_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ fn test_select_wildcard_plan() -> anyhow::Result<()> {

use crate::*;

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Utf8,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Utf8, false)]);
let plan = PlanBuilder::create(schema)
.project(vec![col("a")])?
.build()?;
Expand Down
8 changes: 3 additions & 5 deletions common/planners/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
//
// SPDX-License-Identifier: Apache-2.0.

use std::sync::Arc;

use anyhow::Result;
use common_datavalues::DataField;
use common_datavalues::DataSchema;
use common_datavalues::DataSchemaRefExt;
use common_datavalues::DataType;

use crate::plan_partition::Partition;
Expand All @@ -23,11 +21,11 @@ impl Test {
}

pub fn generate_source_plan_for_test(&self, total: usize) -> Result<PlanNode> {
let schema = Arc::new(DataSchema::new(vec![DataField::new(
let schema = DataSchemaRefExt::create_with_metadata(vec![DataField::new(
"number",
DataType::UInt64,
false
)]));
)]);

let statistics = Statistics {
read_rows: total,
Expand Down
7 changes: 2 additions & 5 deletions common/streams/src/stream_progress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ async fn test_progress_stream() -> anyhow::Result<()> {

use crate::*;

let schema = Arc::new(DataSchema::new(vec![DataField::new(
"a",
DataType::Int64,
false
)]));
let schema =
DataSchemaRefExt::create_with_metadata(vec![DataField::new("a", DataType::Int64, false)]);

let block = DataBlock::create(schema.clone(), vec![Arc::new(Int64Array::from(vec![
1, 2, 3,
Expand Down
Loading