Skip to content

Commit 00ab344

Browse files
committed
remove the is_internal part and clean up
1 parent 3e0a37d commit 00ab344

25 files changed

Lines changed: 109 additions & 268 deletions

File tree

datafusion/core/src/dataframe/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,6 @@ impl DataFrame {
24822482
relation: None,
24832483
name: field.name().to_string(),
24842484
metadata: None,
2485-
is_internal: false,
24862485
}),
24872486
Err(_) => col(field.name()),
24882487
}

datafusion/core/src/physical_planner.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,10 @@ pub fn create_window_expr(
24722472
) -> Result<Arc<dyn WindowExpr>> {
24732473
// unpack aliased logical expressions, e.g. "sum(col) over () as total"
24742474
let (name, e) = match e {
2475-
Expr::Alias(alias) => (alias.name.clone(), e.clone().unalias_nested().data),
2475+
Expr::Alias(alias) => (
2476+
alias.name.clone(),
2477+
alias.expr.as_ref().clone().unalias_nested().data,
2478+
),
24762479
_ => (e.schema_name().to_string(), e.clone()),
24772480
};
24782481
create_window_expr_with_name(&e, name, logical_schema, execution_props)
@@ -3269,9 +3272,10 @@ mod tests {
32693272
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
32703273
use datafusion_expr::{
32713274
Accumulator, AggregateUDF, AggregateUDFImpl, ExprFunctionExt, LogicalPlanBuilder,
3272-
Signature, TableSource, UserDefinedLogicalNodeCore, Volatility, col, lit,
3275+
Signature, TableSource, UserDefinedLogicalNodeCore, Volatility,
3276+
WindowFunctionDefinition, col, lit,
32733277
};
3274-
use datafusion_functions_aggregate::count::count_all;
3278+
use datafusion_functions_aggregate::count::{count_all, count_udaf};
32753279
use datafusion_functions_aggregate::expr_fn::sum;
32763280
use datafusion_functions_aggregate::first_last::first_value_udaf;
32773281
use datafusion_physical_expr::EquivalenceProperties;
@@ -3303,6 +3307,35 @@ mod tests {
33033307
Ok(displayable(physical_plan.as_ref()).indent(true).to_string())
33043308
}
33053309

3310+
#[test]
3311+
fn test_create_window_expr_unwraps_alias_with_metadata() -> Result<()> {
3312+
use std::collections::HashMap;
3313+
3314+
use datafusion_common::metadata::FieldMetadata;
3315+
3316+
let schema = Arc::new(Schema::new(vec![Field::new(
3317+
"column1",
3318+
DataType::Int64,
3319+
true,
3320+
)]));
3321+
let logical_schema = schema.as_ref().clone().to_dfschema_ref()?;
3322+
let metadata = FieldMetadata::from(HashMap::from([(
3323+
"some_key".to_string(),
3324+
"some_value".to_string(),
3325+
)]));
3326+
let expr = Expr::from(WindowFunction::new(
3327+
WindowFunctionDefinition::AggregateUDF(count_udaf()),
3328+
vec![col("column1")],
3329+
))
3330+
.alias_with_metadata("window_alias", Some(metadata));
3331+
3332+
let window_expr =
3333+
create_window_expr(&expr, &logical_schema, &ExecutionProps::new())?;
3334+
3335+
assert_eq!(window_expr.name(), "window_alias");
3336+
Ok(())
3337+
}
3338+
33063339
#[derive(Debug, Default)]
33073340
struct NullAccumulator;
33083341

datafusion/expr/src/expr.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ pub struct Alias {
670670
pub relation: Option<TableReference>,
671671
pub name: String,
672672
pub metadata: Option<FieldMetadata>,
673-
pub is_internal: bool,
674673
}
675674

676675
impl Hash for Alias {
@@ -679,7 +678,6 @@ impl Hash for Alias {
679678
self.relation.hash(state);
680679
self.name.hash(state);
681680
self.metadata.hash(state);
682-
self.is_internal.hash(state);
683681
}
684682
}
685683

@@ -712,22 +710,6 @@ impl Alias {
712710
relation: relation.map(|r| r.into()),
713711
name: name.into(),
714712
metadata: None,
715-
is_internal: false,
716-
}
717-
}
718-
719-
#[doc(hidden)]
720-
pub fn new_internal(
721-
expr: Expr,
722-
relation: Option<impl Into<TableReference>>,
723-
name: impl Into<String>,
724-
) -> Self {
725-
Self {
726-
expr: Box::new(expr),
727-
relation: relation.map(|r| r.into()),
728-
name: name.into(),
729-
metadata: None,
730-
is_internal: true,
731713
}
732714
}
733715

@@ -749,14 +731,12 @@ impl Alias {
749731
relation,
750732
name,
751733
metadata,
752-
is_internal,
753734
} = self;
754735
Ok(Expr::Alias(Alias {
755736
expr: Box::new(f(*expr)?),
756737
relation,
757738
name,
758739
metadata,
759-
is_internal,
760740
}))
761741
}
762742
}
@@ -1896,11 +1876,6 @@ impl Expr {
18961876
Expr::Alias(Alias::new(self, None::<&str>, name.into()))
18971877
}
18981878

1899-
#[doc(hidden)]
1900-
pub fn alias_internal(self, name: impl Into<String>) -> Expr {
1901-
Expr::Alias(Alias::new_internal(self, None::<&str>, name.into()))
1902-
}
1903-
19041879
/// Return `self AS name` alias expression with metadata
19051880
///
19061881
/// The metadata will be attached to the Arrow Schema field when the expression
@@ -1932,15 +1907,6 @@ impl Expr {
19321907
Expr::Alias(Alias::new(self, relation, name.into()))
19331908
}
19341909

1935-
#[doc(hidden)]
1936-
pub fn alias_qualified_internal(
1937-
self,
1938-
relation: Option<impl Into<TableReference>>,
1939-
name: impl Into<String>,
1940-
) -> Expr {
1941-
Expr::Alias(Alias::new_internal(self, relation, name.into()))
1942-
}
1943-
19441910
/// Return `self AS name` alias expression with a specific qualifier and metadata
19451911
///
19461912
/// The metadata will be attached to the Arrow Schema field when the expression
@@ -2461,20 +2427,17 @@ impl NormalizeEq for Expr {
24612427
relation: self_relation,
24622428
name: self_name,
24632429
metadata: self_metadata,
2464-
is_internal: self_is_internal,
24652430
}),
24662431
Expr::Alias(Alias {
24672432
expr: other_expr,
24682433
relation: other_relation,
24692434
name: other_name,
24702435
metadata: other_metadata,
2471-
is_internal: other_is_internal,
24722436
}),
24732437
) => {
24742438
self_name == other_name
24752439
&& self_relation == other_relation
24762440
&& self_metadata == other_metadata
2477-
&& self_is_internal == other_is_internal
24782441
&& self_expr.normalize_eq(other_expr)
24792442
}
24802443
(
@@ -2821,12 +2784,10 @@ impl HashNode for Expr {
28212784
relation,
28222785
name,
28232786
metadata,
2824-
is_internal,
28252787
}) => {
28262788
relation.hash(state);
28272789
name.hash(state);
28282790
metadata.hash(state);
2829-
is_internal.hash(state);
28302791
}
28312792
Expr::Column(column) => {
28322793
column.hash(state);
@@ -4297,24 +4258,6 @@ mod test {
42974258
);
42984259
}
42994260

4300-
#[test]
4301-
fn test_internal_alias_changes_equality() {
4302-
let user_alias = col("id").alias("count(*)");
4303-
let internal_alias = col("id").alias_internal("count(*)");
4304-
4305-
assert_ne!(user_alias, internal_alias);
4306-
assert_ne!(internal_alias, user_alias);
4307-
}
4308-
4309-
#[test]
4310-
fn test_internal_alias_changes_normalized_identity() {
4311-
let user_alias = col("id").alias("count(*)");
4312-
let internal_alias = col("id").alias_internal("count(*)");
4313-
4314-
assert!(!user_alias.normalize_eq(&internal_alias));
4315-
assert!(!internal_alias.normalize_eq(&user_alias));
4316-
}
4317-
43184261
#[test]
43194262
fn test_unalias_nested_respects_user_metadata() {
43204263
use std::collections::HashMap;
@@ -4332,9 +4275,6 @@ mod test {
43324275
);
43334276
assert_eq!(empty_metadata_alias.unalias_nested().data, base_expr);
43344277

4335-
let internal_alias = base_expr.clone().alias_internal("alias");
4336-
assert_eq!(internal_alias.unalias_nested().data, base_expr);
4337-
43384278
let user_metadata = FieldMetadata::from(HashMap::from([(
43394279
"some_key".to_string(),
43404280
"some_value".to_string(),
@@ -4346,14 +4286,6 @@ mod test {
43464286
let user_alias =
43474287
Expr::Alias(user_alias.with_metadata(Some(user_metadata.clone())));
43484288
assert_eq!(user_alias.clone().unalias_nested().data, user_alias);
4349-
4350-
let Expr::Alias(internal_alias) = base_expr.clone().alias_internal("alias")
4351-
else {
4352-
unreachable!();
4353-
};
4354-
let internal_alias =
4355-
Expr::Alias(internal_alias.with_metadata(Some(user_metadata.clone())));
4356-
assert_eq!(internal_alias.clone().unalias_nested().data, internal_alias);
43574289
}
43584290

43594291
fn wildcard_options(

datafusion/expr/src/expr_rewriter/mod.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ pub enum SavedName {
309309
Saved {
310310
relation: Option<TableReference>,
311311
name: String,
312-
is_internal_generated: bool,
313312
},
314313
/// Name is not preserved
315314
None,
@@ -345,17 +344,10 @@ impl NamePreserver {
345344
Expr::Alias(alias) => SavedName::Saved {
346345
relation: alias.relation.clone(),
347346
name: alias.name.clone(),
348-
is_internal_generated: alias.is_internal,
349347
},
350348
_ => {
351-
// Rewrites preserve this output name for schema stability, but the
352-
// alias itself is planner-generated rather than user-written.
353349
let (relation, name) = expr.qualified_name();
354-
SavedName::Saved {
355-
relation,
356-
name,
357-
is_internal_generated: true,
358-
}
350+
SavedName::Saved { relation, name }
359351
}
360352
}
361353
} else {
@@ -368,18 +360,10 @@ impl SavedName {
368360
/// Ensures the qualified name of the rewritten expression is preserved
369361
pub fn restore(self, expr: Expr) -> Expr {
370362
match self {
371-
SavedName::Saved {
372-
relation,
373-
name,
374-
is_internal_generated,
375-
} => {
363+
SavedName::Saved { relation, name } => {
376364
let (new_relation, new_name) = expr.qualified_name();
377365
if new_relation != relation || new_name != name {
378-
if is_internal_generated {
379-
expr.alias_qualified_internal(relation, name)
380-
} else {
381-
expr.alias_qualified(relation, name)
382-
}
366+
expr.alias_qualified(relation, name)
383367
} else {
384368
expr
385369
}
@@ -567,20 +551,6 @@ mod test {
567551
);
568552
}
569553

570-
#[test]
571-
fn test_name_preserver_marks_non_alias_restore_as_internal() {
572-
let saved_name = NamePreserver::new_for_projection().save(&col("a"));
573-
let restored = saved_name.restore(col("b"));
574-
575-
let Expr::Alias(alias) = restored else {
576-
panic!("expected alias");
577-
};
578-
579-
assert!(alias.is_internal);
580-
assert_eq!(alias.name, "a");
581-
assert_eq!(*alias.expr, col("b"));
582-
}
583-
584554
/// rewrites `expr_from` to `rewrite_to` while preserving the original qualified name
585555
/// by using the `NamePreserver`
586556
fn test_rewrite(expr_from: Expr, rewrite_to: Expr) {

datafusion/expr/src/expr_schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,14 +1070,14 @@ mod tests {
10701070
}
10711071

10721072
#[test]
1073-
fn test_internal_alias_metadata_is_preserved_in_field_metadata() {
1073+
fn test_alias_metadata_is_preserved_in_field_metadata() {
10741074
let schema = MockExprSchema::new().with_data_type(DataType::Int32);
10751075
let alias_metadata = FieldMetadata::from(HashMap::from([(
10761076
"some_key".to_string(),
10771077
"some_value".to_string(),
10781078
)]));
10791079

1080-
let Expr::Alias(alias) = col("foo").alias_internal("alias") else {
1080+
let Expr::Alias(alias) = col("foo").alias("alias") else {
10811081
unreachable!();
10821082
};
10831083
let expr = Expr::Alias(alias.with_metadata(Some(alias_metadata.clone())));

datafusion/expr/src/tree_node.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,12 @@ impl TreeNode for Expr {
158158
relation,
159159
name,
160160
metadata,
161-
is_internal,
162161
}) => expr.map_elements(f)?.update_data(|expr| {
163162
Expr::Alias(Alias {
164163
expr,
165164
relation,
166165
name,
167166
metadata,
168-
is_internal,
169167
})
170168
}),
171169
Expr::InSubquery(InSubquery {

datafusion/functions-aggregate/src/count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn count_distinct(expr: Expr) -> Expr {
102102
/// let expr = col(expr.schema_name().to_string());
103103
/// ```
104104
pub fn count_all() -> Expr {
105-
count(Expr::Literal(COUNT_STAR_EXPANSION, None)).alias_internal("count(*)")
105+
count(Expr::Literal(COUNT_STAR_EXPANSION, None)).alias("count(*)")
106106
}
107107

108108
/// Creates window aggregation to count all rows.

datafusion/optimizer/src/analyzer/resolve_grouping_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn replace_grouping_exprs(
111111
&group_expr_to_bitmap_index,
112112
grouping_id_type,
113113
)?;
114-
projection_exprs.push(Expr::Alias(Alias::new_internal(
114+
projection_exprs.push(Expr::Alias(Alias::new(
115115
grouping_expr,
116116
column.relation,
117117
column.name,

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,21 +1275,19 @@ fn project_with_column_index(
12751275
.enumerate()
12761276
.map(|(i, e)| match e {
12771277
Expr::Alias(Alias { ref name, .. }) if name != schema.field(i).name() => {
1278-
Ok(e.unalias().alias_internal(schema.field(i).name()))
1278+
Ok(e.unalias().alias(schema.field(i).name()))
12791279
}
12801280
Expr::Column(Column {
12811281
relation: _,
12821282
ref name,
12831283
spans: _,
1284-
}) if name != schema.field(i).name() => {
1285-
Ok(e.alias_internal(schema.field(i).name()))
1286-
}
1284+
}) if name != schema.field(i).name() => Ok(e.alias(schema.field(i).name())),
12871285
Expr::Alias { .. } | Expr::Column { .. } => Ok(e),
12881286
#[expect(deprecated)]
12891287
Expr::Wildcard { .. } => {
12901288
plan_err!("Wildcard should be expanded before type coercion")
12911289
}
1292-
_ => Ok(e.alias_internal(schema.field(i).name())),
1290+
_ => Ok(e.alias(schema.field(i).name())),
12931291
})
12941292
.collect::<Result<Vec<_>>>()?;
12951293

0 commit comments

Comments
 (0)