@@ -44,6 +44,7 @@ use vegafusion_common::data::table::VegaFusionTable;
4444use vegafusion_common:: datafusion_common:: DFSchema ;
4545use vegafusion_common:: datatypes:: cast_to;
4646use vegafusion_common:: error:: { Result , ResultWithContext , VegaFusionError } ;
47+ use vegafusion_core:: data:: dataset:: VegaFusionDataset ;
4748use vegafusion_core:: proto:: gen:: expression:: {
4849 expression, literal, CallExpression , Expression , Literal ,
4950} ;
@@ -96,15 +97,15 @@ pub enum VegaFusionCallable {
9697 Scale ,
9798}
9899
99- pub fn compile_scalar_arguments (
100+ pub async fn compile_scalar_arguments (
100101 node : & CallExpression ,
101102 config : & CompilationConfig ,
102103 schema : & DFSchema ,
103104 cast : & Option < DataType > ,
104105) -> Result < Vec < Expr > > {
105106 let mut args: Vec < Expr > = Vec :: new ( ) ;
106107 for arg in & node. arguments {
107- let compiled_arg = compile ( arg, config, Some ( schema) ) ?;
108+ let compiled_arg = compile ( arg, config, Some ( schema) ) . await ?;
108109 let arg_expr = match cast {
109110 None => compiled_arg,
110111 Some ( dtype) => cast_to ( compiled_arg, dtype, schema) ?,
@@ -114,7 +115,7 @@ pub fn compile_scalar_arguments(
114115 Ok ( args)
115116}
116117
117- pub fn compile_call (
118+ pub async fn compile_call (
118119 node : & CallExpression ,
119120 config : & CompilationConfig ,
120121 schema : & DFSchema ,
@@ -127,10 +128,10 @@ pub fn compile_call(
127128 VegaFusionCallable :: Macro ( callable) => {
128129 // Apply macro then recursively compile
129130 let new_expr = callable ( & node. arguments ) ?;
130- compile ( & new_expr, config, Some ( schema) )
131+ compile ( & new_expr, config, Some ( schema) ) . await
131132 }
132133 VegaFusionCallable :: ScalarUDF { udf, cast } => {
133- let args = compile_scalar_arguments ( node, config, schema, cast) ?;
134+ let args = compile_scalar_arguments ( node, config, schema, cast) . await ?;
134135 Ok ( Expr :: ScalarFunction ( expr:: ScalarFunction {
135136 func : udf. clone ( ) ,
136137 args,
@@ -144,18 +145,19 @@ pub fn compile_call(
144145 ..
145146 } ) => {
146147 if let Some ( dataset) = config. data_scope . get ( name) {
147- if let Some ( table) = dataset. as_table ( ) {
148- let tz_config = config. tz_config . with_context ( || {
149- "No local timezone info provided" . to_string ( )
150- } ) ?;
151- callee ( table, & node. arguments [ 1 ..] , schema, & tz_config)
152- } else {
153- // TODO: If a plan executor is available in config, try to materialize here.
154- Err ( VegaFusionError :: compilation ( format ! (
155- "Dataset {} is not materialized as table" ,
156- name
157- ) ) )
158- }
148+ let table = match dataset {
149+ VegaFusionDataset :: Table { table, .. } => table. clone ( ) ,
150+ // Materialize plans on-demand when needed for expression evaluation.
151+ // This allows lazy evaluation of data transformations - plans are only
152+ // executed when the data is actually needed by an expression.
153+ VegaFusionDataset :: Plan { plan } => {
154+ config. plan_executor . execute_plan ( plan. clone ( ) ) . await ?
155+ }
156+ } ;
157+ let tz_config = config
158+ . tz_config
159+ . with_context ( || "No local timezone info provided" . to_string ( ) ) ?;
160+ callee ( & table, & node. arguments [ 1 ..] , schema, & tz_config)
159161 } else {
160162 Err ( VegaFusionError :: internal ( format ! (
161163 "No dataset named {}. Available: {:?}" ,
@@ -179,11 +181,11 @@ pub fn compile_call(
179181 }
180182 }
181183 VegaFusionCallable :: Transform ( callable) => {
182- let args = compile_scalar_arguments ( node, config, schema, & None ) ?;
184+ let args = compile_scalar_arguments ( node, config, schema, & None ) . await ?;
183185 callable ( & args, schema)
184186 }
185187 VegaFusionCallable :: UnaryTransform ( callable) => {
186- let mut args = compile_scalar_arguments ( node, config, schema, & None ) ?;
188+ let mut args = compile_scalar_arguments ( node, config, schema, & None ) . await ?;
187189 if args. len ( ) != 1 {
188190 Err ( VegaFusionError :: internal ( format ! (
189191 "The {} function requires 1 argument. Received {}" ,
@@ -195,14 +197,14 @@ pub fn compile_call(
195197 }
196198 }
197199 VegaFusionCallable :: LocalTransform ( callable) => {
198- let args = compile_scalar_arguments ( node, config, schema, & None ) ?;
200+ let args = compile_scalar_arguments ( node, config, schema, & None ) . await ?;
199201 let tz_config = config
200202 . tz_config
201203 . with_context ( || "No local timezone info provided" . to_string ( ) ) ?;
202204 callable ( & tz_config, & args, schema)
203205 }
204206 VegaFusionCallable :: UtcTransform ( callable) => {
205- let args = compile_scalar_arguments ( node, config, schema, & None ) ?;
207+ let args = compile_scalar_arguments ( node, config, schema, & None ) . await ?;
206208 let tz_config = RuntimeTzConfig {
207209 local_tz : chrono_tz:: UTC ,
208210 default_input_tz : chrono_tz:: UTC ,
0 commit comments