Skip to content

Commit 3bc5b5c

Browse files
ldanilekConvex, Inc.
authored andcommitted
allow Date.now in schema evaluation (#25223)
set Date.now to a fixed timestamp determined at the beginning of schema evaluation. We do not store this timestamp anywhere; in particular it's not the same timestamp as used for Analyze. But I think that's fine because evaluating the schema really shouldn't be making decisions that change the schema with `Date.now` calls. Basically it's the same as how we treat Math.random in schema evaluation. Note future plan is described in #18861 . GitOrigin-RevId: 1e8f02217b0383e329f097d036c5e7afa234288c
1 parent 49965fe commit 3bc5b5c

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

crates/application/src/application_function_runner/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,9 +1688,10 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
16881688
schema_bundle: ModuleSource,
16891689
source_map: Option<SourceMap>,
16901690
rng_seed: [u8; 32],
1691+
unix_timestamp: UnixTimestamp,
16911692
) -> anyhow::Result<DatabaseSchema> {
16921693
self.analyze_isolate
1693-
.evaluate_schema(schema_bundle, source_map, rng_seed)
1694+
.evaluate_schema(schema_bundle, source_map, rng_seed, unix_timestamp)
16941695
.await
16951696
}
16961697

crates/application/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,9 +1469,10 @@ impl<RT: Runtime> Application<RT> {
14691469

14701470
async fn _evaluate_schema(&self, schema: ModuleConfig) -> anyhow::Result<DatabaseSchema> {
14711471
let rng_seed = self.runtime().with_rng(|rng| rng.gen());
1472+
let unix_timestamp = self.runtime().unix_timestamp();
14721473
let mut schema = self
14731474
.runner()
1474-
.evaluate_schema(schema.source, schema.source_map, rng_seed)
1475+
.evaluate_schema(schema.source, schema.source_map, rng_seed, unix_timestamp)
14751476
.await?;
14761477

14771478
for table_schema in schema.tables.values_mut() {

crates/isolate/src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ pub enum RequestType<RT: Runtime> {
428428
schema_bundle: ModuleSource,
429429
source_map: Option<SourceMap>,
430430
rng_seed: [u8; 32],
431+
unix_timestamp: UnixTimestamp,
431432
response: oneshot::Sender<anyhow::Result<DatabaseSchema>>,
432433
},
433434
EvaluateAuthConfig {
@@ -834,12 +835,14 @@ impl<RT: Runtime> IsolateClient<RT> {
834835
schema_bundle: ModuleSource,
835836
source_map: Option<SourceMap>,
836837
rng_seed: [u8; 32],
838+
unix_timestamp: UnixTimestamp,
837839
) -> anyhow::Result<DatabaseSchema> {
838840
let (tx, rx) = oneshot::channel();
839841
let request = RequestType::EvaluateSchema {
840842
schema_bundle,
841843
source_map,
842844
rng_seed,
845+
unix_timestamp,
843846
response: tx,
844847
};
845848
self.send_request(Request::new(
@@ -1655,6 +1658,7 @@ impl<RT: Runtime> IsolateWorker<RT> for BackendIsolateWorker<RT> {
16551658
schema_bundle,
16561659
source_map,
16571660
rng_seed,
1661+
unix_timestamp,
16581662
response,
16591663
} => {
16601664
let r = SchemaEnvironment::evaluate_schema(
@@ -1663,6 +1667,7 @@ impl<RT: Runtime> IsolateWorker<RT> for BackendIsolateWorker<RT> {
16631667
schema_bundle,
16641668
source_map,
16651669
rng_seed,
1670+
unix_timestamp,
16661671
)
16671672
.await;
16681673

crates/isolate/src/environment/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct SchemaEnvironment {
6868
schema_bundle: ModuleSource,
6969
source_map: Option<SourceMap>,
7070
rng: ChaCha12Rng,
71+
unix_timestamp: UnixTimestamp,
7172
}
7273

7374
impl<RT: Runtime> IsolateEnvironment<RT> for SchemaEnvironment {
@@ -84,10 +85,7 @@ impl<RT: Runtime> IsolateEnvironment<RT> for SchemaEnvironment {
8485
}
8586

8687
fn unix_timestamp(&self) -> anyhow::Result<UnixTimestamp> {
87-
anyhow::bail!(ErrorMetadata::bad_request(
88-
"NoDateInSchema",
89-
"Date unsupported when evaluating schema"
90-
))
88+
Ok(self.unix_timestamp)
9189
}
9290

9391
fn get_environment_variable(
@@ -181,12 +179,14 @@ impl SchemaEnvironment {
181179
schema_bundle: ModuleSource,
182180
source_map: Option<SourceMap>,
183181
rng_seed: [u8; 32],
182+
unix_timestamp: UnixTimestamp,
184183
) -> anyhow::Result<DatabaseSchema> {
185184
let rng = ChaCha12Rng::from_seed(rng_seed);
186185
let environment = Self {
187186
schema_bundle,
188187
source_map,
189188
rng,
189+
unix_timestamp,
190190
};
191191
let client_id = Arc::new(client_id);
192192
let (handle, state) = isolate.start_request(client_id, environment).await?;

crates/isolate/src/tests/schema.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ async fn test_eval_schema(rt: TestRuntime) -> anyhow::Result<()> {
222222
"#;
223223

224224
let rng_seed = rt.with_rng(|rng| rng.gen());
225+
let unix_timestamp = rt.unix_timestamp();
225226
let t = UdfTest::default_with_modules(vec![], rt).await??;
226227

227228
let schema = t
228229
.isolate
229-
.evaluate_schema(source.to_string(), None, rng_seed)
230+
.evaluate_schema(source.to_string(), None, rng_seed, unix_timestamp)
230231
.await?;
231232

232233
let name1: TableName = "noIndexes".parse()?;

0 commit comments

Comments
 (0)