-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Added composite identifiers to get field of struct. #628
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! get field of a struct array | ||
|
||
use std::{any::Any, sync::Arc}; | ||
|
||
use arrow::{ | ||
array::StructArray, | ||
datatypes::{DataType, Schema}, | ||
record_batch::RecordBatch, | ||
}; | ||
|
||
use crate::{ | ||
error::DataFusionError, | ||
error::Result, | ||
physical_plan::{ColumnarValue, PhysicalExpr}, | ||
utils::get_field as get_data_type_field, | ||
}; | ||
|
||
/// expression to get a field of a struct array. | ||
#[derive(Debug)] | ||
pub struct GetFieldExpr { | ||
arg: Arc<dyn PhysicalExpr>, | ||
name: String, | ||
} | ||
|
||
impl GetFieldExpr { | ||
/// Create new get field expression | ||
pub fn new(arg: Arc<dyn PhysicalExpr>, name: String) -> Self { | ||
Self { arg, name } | ||
} | ||
|
||
/// Get the input expression | ||
pub fn arg(&self) -> &Arc<dyn PhysicalExpr> { | ||
&self.arg | ||
} | ||
} | ||
|
||
impl std::fmt::Display for GetFieldExpr { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "({}).{}", self.arg, self.name) | ||
} | ||
} | ||
|
||
impl PhysicalExpr for GetFieldExpr { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn data_type(&self, input_schema: &Schema) -> Result<DataType> { | ||
let data_type = self.arg.data_type(input_schema)?; | ||
get_data_type_field(&data_type, &self.name).map(|f| f.data_type().clone()) | ||
} | ||
|
||
fn nullable(&self, input_schema: &Schema) -> Result<bool> { | ||
let data_type = self.arg.data_type(input_schema)?; | ||
get_data_type_field(&data_type, &self.name).map(|f| f.is_nullable()) | ||
} | ||
|
||
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { | ||
let arg = self.arg.evaluate(batch)?; | ||
match arg { | ||
ColumnarValue::Array(array) => Ok(ColumnarValue::Array( | ||
array | ||
.as_any() | ||
.downcast_ref::<StructArray>() | ||
.unwrap() | ||
.column_by_name(&self.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very cool -- I was wondering if |
||
.unwrap() | ||
.clone(), | ||
)), | ||
ColumnarValue::Scalar(_) => Err(DataFusionError::NotImplemented( | ||
"field is not yet implemented for scalar values".to_string(), | ||
)), | ||
} | ||
} | ||
} | ||
|
||
/// Create an `.field` expression | ||
pub fn get_field( | ||
arg: Arc<dyn PhysicalExpr>, | ||
name: String, | ||
) -> Result<Arc<dyn PhysicalExpr>> { | ||
Ok(Arc::new(GetFieldExpr::new(arg, name))) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,22 @@ pub struct SqlToRel<'a, S: ContextProvider> { | |
schema_provider: &'a S, | ||
} | ||
|
||
fn plan_compound(mut identifiers: Vec<String>) -> Expr { | ||
if &identifiers[0][0..1] == "@" { | ||
Expr::ScalarVariable(identifiers) | ||
} else if identifiers.len() == 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth considering a follow up PR to handle an edge case where user tries to access nested field with unqualified column like |
||
// "table.column" | ||
let name = identifiers.pop().unwrap(); | ||
let relation = Some(identifiers.pop().unwrap()); | ||
Expr::Column(Column { relation, name }) | ||
} else { | ||
// "table.column.field..." | ||
let name = identifiers.pop().unwrap(); | ||
let expr = Box::new(plan_compound(identifiers)); | ||
Expr::GetField { expr, name } | ||
} | ||
} | ||
|
||
impl<'a, S: ContextProvider> SqlToRel<'a, S> { | ||
/// Create a new query planner | ||
pub fn new(schema_provider: &'a S) -> Self { | ||
|
@@ -916,23 +932,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
} | ||
|
||
SQLExpr::CompoundIdentifier(ids) => { | ||
let mut var_names = vec![]; | ||
for id in ids { | ||
var_names.push(id.value.clone()); | ||
} | ||
if &var_names[0][0..1] == "@" { | ||
Ok(Expr::ScalarVariable(var_names)) | ||
} else if var_names.len() == 2 { | ||
// table.column identifier | ||
let name = var_names.pop().unwrap(); | ||
let relation = Some(var_names.pop().unwrap()); | ||
Ok(Expr::Column(Column { relation, name })) | ||
} else { | ||
Err(DataFusionError::NotImplemented(format!( | ||
"Unsupported compound identifier '{:?}'", | ||
var_names, | ||
))) | ||
} | ||
let var_names = ids.iter().map(|x| x.value.clone()).collect::<Vec<_>>(); | ||
Ok(plan_compound(var_names)) | ||
} | ||
|
||
SQLExpr::Wildcard => Ok(Expr::Wildcard), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::datatypes::{DataType, Field}; | ||
|
||
use crate::error::{DataFusionError, Result}; | ||
|
||
/// Returns the first field named `name` from the fields of a [`DataType::Struct`]. | ||
/// # Error | ||
/// Errors iff | ||
/// * the `data_type` is not a Struct or, | ||
/// * there is no field named `name` | ||
pub fn get_field<'a>(data_type: &'a DataType, name: &str) -> Result<&'a Field> { | ||
if let DataType::Struct(fields) = data_type { | ||
let maybe_field = fields.iter().find(|x| x.name() == name); | ||
if let Some(field) = maybe_field { | ||
Ok(field) | ||
} else { | ||
Err(DataFusionError::Plan(format!( | ||
"The `Struct` has no field named \"{}\"", | ||
name | ||
))) | ||
} | ||
} else { | ||
Err(DataFusionError::Plan( | ||
"The expression to get a field is only valid for `Struct`".to_string(), | ||
)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andygrove , this is not ideal; I have to admit I am not very familiar with proto and this part yet. Could be relevant to add a small guide in the readme like "how to add a new logical node to ballista"? It could reduce the barrier?