@@ -52,8 +52,7 @@ use datafusion_common::tree_node::{Transformed, TreeNode};
5252use datafusion_common:: { Column , DFSchema , ExprSchema , ScalarValue , TableReference } ;
5353use datafusion_expr:: { col, conditional_expressions:: CaseBuilder , lit, when, Expr , JoinType } ;
5454use datafusion_expr:: {
55- ExprSchemable , Extension , LogicalPlan , LogicalPlanBuilder , UserDefinedLogicalNode ,
56- UNNAMED_TABLE ,
55+ Extension , LogicalPlan , LogicalPlanBuilder , UserDefinedLogicalNode , UNNAMED_TABLE ,
5756} ;
5857
5958use delta_kernel:: schema:: { ColumnMetadataKey , StructType } ;
@@ -78,16 +77,18 @@ use crate::delta_datafusion::{
7877 DeltaSessionConfig , DeltaTableProvider ,
7978} ;
8079
81- use crate :: kernel:: { Action , DataCheck , Metadata , StructTypeExt } ;
80+ use crate :: kernel:: { Action , Metadata , StructTypeExt } ;
8281use crate :: logstore:: LogStoreRef ;
8382use crate :: operations:: cast:: merge_schema:: { merge_arrow_field, merge_arrow_schema} ;
8483use crate :: operations:: cdc:: * ;
8584use crate :: operations:: merge:: barrier:: find_node;
8685use crate :: operations:: transaction:: CommitBuilder ;
86+ use crate :: operations:: write:: generated_columns:: {
87+ add_generated_columns, add_missing_generated_columns,
88+ } ;
8789use crate :: operations:: write:: WriterStatsConfig ;
8890use crate :: protocol:: { DeltaOperation , MergePredicate } ;
8991use crate :: table:: state:: DeltaTableState ;
90- use crate :: table:: GeneratedColumn ;
9192use crate :: { DeltaResult , DeltaTable , DeltaTableError } ;
9293use writer:: write_execution_plan_v2;
9394
@@ -776,72 +777,6 @@ async fn execute(
776777 None => TableReference :: bare ( UNNAMED_TABLE ) ,
777778 } ;
778779
779- /// Add generated column expressions to a dataframe
780- fn add_missing_generated_columns (
781- mut df : DataFrame ,
782- generated_cols : & Vec < GeneratedColumn > ,
783- ) -> DeltaResult < ( DataFrame , Vec < String > ) > {
784- let mut missing_cols = vec ! [ ] ;
785- for generated_col in generated_cols {
786- let col_name = generated_col. get_name ( ) ;
787-
788- if df
789- . clone ( )
790- . schema ( )
791- . field_with_unqualified_name ( col_name)
792- . is_err ( )
793- // implies it doesn't exist
794- {
795- debug ! (
796- "Adding missing generated column {} in source as placeholder" ,
797- col_name
798- ) ;
799- // If column doesn't exist, we add a null column, later we will generate the values after
800- // all the merge is projected.
801- // Other generated columns that were provided upon the start we only validate during write
802- missing_cols. push ( col_name. to_string ( ) ) ;
803- df = df
804- . clone ( )
805- . with_column ( col_name, Expr :: Literal ( ScalarValue :: Null ) ) ?;
806- }
807- }
808- Ok ( ( df, missing_cols) )
809- }
810-
811- /// Add generated column expressions to a dataframe
812- fn add_generated_columns (
813- mut df : DataFrame ,
814- generated_cols : & Vec < GeneratedColumn > ,
815- generated_cols_missing_in_source : & [ String ] ,
816- state : & SessionState ,
817- ) -> DeltaResult < DataFrame > {
818- debug ! ( "Generating columns in dataframe" ) ;
819- for generated_col in generated_cols {
820- // We only validate columns that were missing from the start. We don't update
821- // update generated columns that were provided during runtime
822- if !generated_cols_missing_in_source. contains ( & generated_col. name ) {
823- continue ;
824- }
825-
826- let generation_expr = state. create_logical_expr (
827- generated_col. get_generation_expression ( ) ,
828- df. clone ( ) . schema ( ) ,
829- ) ?;
830- let col_name = generated_col. get_name ( ) ;
831-
832- df = df. clone ( ) . with_column (
833- generated_col. get_name ( ) ,
834- when ( col ( col_name) . is_null ( ) , generation_expr)
835- . otherwise ( col ( col_name) ) ?
836- . cast_to (
837- & arrow_schema:: DataType :: try_from ( & generated_col. data_type ) ?,
838- df. schema ( ) ,
839- ) ?,
840- ) ?
841- }
842- Ok ( df)
843- }
844-
845780 let generated_col_expressions = snapshot
846781 . schema ( )
847782 . get_generated_columns ( )
0 commit comments