perf(s3select-query): short-circuit projection aliasing and UDF lookups (#9756)

This commit is contained in:
Ramakrishna Chilaka
2026-11-29 08:15:58 +05:30
committed by GitHub
parent eea2be63a3
commit d3569f6360
2 changed files with 36 additions and 28 deletions
@@ -136,8 +136,8 @@ pub struct SimpleFunctionMetadataManager {
impl Default for SimpleFunctionMetadataManager {
fn default() -> Self {
let mut func_meta_manager = Self {
scalar_functions: Default::default(),
aggregate_functions: Default::default(),
scalar_functions: HashMap::with_capacity(ALLOWED_SCALAR_FUNCTIONS.len() + 16),
aggregate_functions: HashMap::with_capacity(ALLOWED_AGGREGATE_FUNCTIONS.len()),
window_functions: Default::default(),
};
for udf in SessionStateDefaults::default_scalar_functions() {
@@ -179,6 +179,9 @@ impl FunctionMetadataManager for SimpleFunctionMetadataManager {
}
fn udf(&self, name: &str) -> QueryResult<Arc<ScalarUDF>> {
if self.scalar_functions.is_empty() {
return Err(QueryError::FunctionExists { name: name.to_string() });
}
let result = self.scalar_functions.get(&name.to_uppercase());
result
@@ -187,6 +190,9 @@ impl FunctionMetadataManager for SimpleFunctionMetadataManager {
}
fn udaf(&self, name: &str) -> QueryResult<Arc<AggregateUDF>> {
if self.aggregate_functions.is_empty() {
return Err(QueryError::FunctionNotExists { name: name.to_string() });
}
let result = self.aggregate_functions.get(&name.to_uppercase());
result
@@ -195,6 +201,9 @@ impl FunctionMetadataManager for SimpleFunctionMetadataManager {
}
fn udwf(&self, name: &str) -> QueryResult<Arc<WindowUDF>> {
if self.window_functions.is_empty() {
return Err(QueryError::FunctionNotExists { name: name.to_string() });
}
let result = self.window_functions.get(&name.to_uppercase());
result
+25 -26
View File
@@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{collections::HashMap, convert::Infallible, ops::ControlFlow};
use std::{
collections::{HashMap, HashSet},
convert::Infallible,
ops::ControlFlow,
};
use async_trait::async_trait;
use datafusion::{
@@ -206,30 +210,22 @@ fn alias_path_projections(select: &mut Select, order_by: Option<&OrderBy>) {
return;
};
let normalizer = IdentNormalizer::default();
let names: Vec<Option<String>> = select
.projection
.iter()
.map(|item| {
let ident = match item {
SelectItem::UnnamedExpr(Expr::Nested(_)) => None,
SelectItem::UnnamedExpr(expr) => column_name(expr),
SelectItem::ExprWithAlias { alias, .. } => Some(alias),
_ => None,
};
ident.map(|ident| normalizer.normalize(ident.clone()))
})
.collect();
if names.iter().any(Option::is_none) {
return;
}
let mut counts = HashMap::<String, usize>::new();
for name in names.iter().flatten() {
*counts.entry(name.clone()).or_default() += 1;
}
// Retained paths have generated names that cannot be inferred from their last key.
// Keep the whole projection unchanged so a sibling alias cannot collide with them.
if names.iter().flatten().any(|name| counts.get(name) != Some(&1)) {
return;
let mut seen = HashSet::<String>::with_capacity(select.projection.len());
for item in &select.projection {
let ident = match item {
SelectItem::UnnamedExpr(Expr::Nested(_)) => None,
SelectItem::UnnamedExpr(expr) => column_name(expr),
SelectItem::ExprWithAlias { alias, .. } => Some(alias),
_ => None,
};
let Some(ident) = ident else {
return;
};
if !seen.insert(normalizer.normalize(ident.clone())) {
// Retained paths have generated names that cannot be inferred from their last key.
// Keep the whole projection unchanged so a sibling alias cannot collide with them.
return;
}
}
for item in &mut select.projection {
let SelectItem::UnnamedExpr(expr) = item else {
@@ -347,9 +343,12 @@ fn name_path_projections(plan: LogicalPlan, keys: &[Option<String>]) -> DFResult
.zip(schema.fields())
.map(|(key, field)| key.as_deref().filter(|key| key != field.name()))
.collect();
if renames.iter().all(Option::is_none) {
return Ok(plan);
}
// Dropping a rename brings back the generated name, which can collide with another rename.
loop {
let mut counts = HashMap::<&str, usize>::new();
let mut counts = HashMap::<&str, usize>::with_capacity(schema.fields().len());
for (rename, field) in renames.iter().zip(schema.fields()) {
*counts.entry(rename.unwrap_or(field.name())).or_default() += 1;
}