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

Feat/make dfschema wrap schemaref #8905

Closed
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
73 changes: 28 additions & 45 deletions datafusion/common/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ impl Column {
}

for schema in schemas {
let fields = schema.fields_with_unqualified_name(&self.name);
match fields.len() {
let columns = schema.columns_with_unqualified_name(&self.name);
match columns.len() {
0 => continue,
1 => {
return Ok(fields[0].qualified_column());
return Ok(columns[0].clone().into());
}
_ => {
// More than 1 fields in this schema have their names set to self.name.
Expand All @@ -199,13 +199,11 @@ impl Column {

// Compare matched fields with one USING JOIN clause at a time
for using_col in using_columns {
let all_matched = fields
.iter()
.all(|f| using_col.contains(&f.qualified_column()));
let all_matched = columns.iter().all(|f| using_col.contains(f));
// All matched fields belong to the same using column set, in orther words
// the same join clause. We simply pick the qualifer from the first match.
if all_matched {
return Ok(fields[0].qualified_column());
return Ok(columns[0].clone().into());
}
}
}
Expand All @@ -214,10 +212,7 @@ impl Column {

_schema_err!(SchemaError::FieldNotFound {
field: Box::new(Column::new(self.relation.clone(), self.name)),
valid_fields: schemas
.iter()
.flat_map(|s| s.fields().iter().map(|f| f.qualified_column()))
.collect(),
valid_fields: schemas.iter().flat_map(|s| s.columns()).collect(),
})
}

Expand Down Expand Up @@ -267,13 +262,14 @@ impl Column {
}

for schema_level in schemas {
let fields = schema_level
let columns = schema_level
.iter()
.flat_map(|s| s.fields_with_unqualified_name(&self.name))
.flat_map(|s| s.columns_with_unqualified_name(&self.name))
.collect::<Vec<_>>();
match fields.len() {
match columns.len() {
0 => continue,
1 => return Ok(fields[0].qualified_column()),
1 => return Ok(columns[0].clone()),

_ => {
// More than 1 fields in this schema have their names set to self.name.
//
Expand All @@ -289,13 +285,11 @@ impl Column {

// Compare matched fields with one USING JOIN clause at a time
for using_col in using_columns {
let all_matched = fields
.iter()
.all(|f| using_col.contains(&f.qualified_column()));
let all_matched = columns.iter().all(|c| using_col.contains(c));
// All matched fields belong to the same using column set, in orther words
// the same join clause. We simply pick the qualifer from the first match.
if all_matched {
return Ok(fields[0].qualified_column());
return Ok(columns[0].clone());
}
}

Expand All @@ -312,7 +306,7 @@ impl Column {
valid_fields: schemas
.iter()
.flat_map(|s| s.iter())
.flat_map(|s| s.fields().iter().map(|f| f.qualified_column()))
.flat_map(|s| s.columns())
.collect(),
})
}
Expand Down Expand Up @@ -355,36 +349,25 @@ impl fmt::Display for Column {
#[cfg(test)]
mod tests {
use super::*;
use crate::DFField;
use arrow::datatypes::DataType;
use std::collections::HashMap;

fn create_schema(names: &[(Option<&str>, &str)]) -> Result<DFSchema> {
let fields = names
.iter()
.map(|(qualifier, name)| {
DFField::new(
qualifier.to_owned().map(|s| s.to_string()),
name,
DataType::Boolean,
true,
)
})
.collect::<Vec<_>>();
DFSchema::new_with_metadata(fields, HashMap::new())
use arrow_schema::{Field, SchemaBuilder};

fn create_qualified_schema(qualifier: &str, names: &[&str]) -> Result<DFSchema> {
let mut schema_builder = SchemaBuilder::new();
schema_builder.extend(
names
.iter()
.map(|f| Field::new(f.clone(), DataType::Boolean, true)),
);
let schema = Arc::new(schema_builder.finish());
DFSchema::try_from_qualified_schema(qualifier, &schema)
}

#[test]
fn test_normalize_with_schemas_and_ambiguity_check() -> Result<()> {
let schema1 = create_schema(&[(Some("t1"), "a"), (Some("t1"), "b")])?;
let schema2 = create_schema(&[(Some("t2"), "c"), (Some("t2"), "d")])?;
let schema3 = create_schema(&[
(Some("t3"), "a"),
(Some("t3"), "b"),
(Some("t3"), "c"),
(Some("t3"), "d"),
(Some("t3"), "e"),
])?;
let schema1 = create_qualified_schema("t1", &["a", "b"])?;
let schema2 = create_qualified_schema("t2", &["c", "d"])?;
let schema3 = create_qualified_schema("t3", &["a", "b", "c", "d", "e"])?;

// already normalized
let col = Column::new(Some("t1"), "a");
Expand Down
Loading