From 3a9716829e7a1d64953c20bdf824ed02c767c629 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Wed, 9 Oct 2024 16:32:05 +0200 Subject: [PATCH] fix: simplify SQL expression Signed-off-by: Harald Hoyer --- ...03f9f912ef79afd559f8af530450649593fc6.json | 26 --------- ...97ae86e2513aca65199717dfd7359feddcd5e.json | 26 +++++++++ core/lib/dal/src/tee_proof_generation_dal.rs | 58 +++++++------------ 3 files changed, 48 insertions(+), 62 deletions(-) delete mode 100644 core/lib/dal/.sqlx/query-b2de2ea4a66ff596af40976b05c03f9f912ef79afd559f8af530450649593fc6.json create mode 100644 core/lib/dal/.sqlx/query-d825fcac2f7d32a31c159d5674497ae86e2513aca65199717dfd7359feddcd5e.json diff --git a/core/lib/dal/.sqlx/query-b2de2ea4a66ff596af40976b05c03f9f912ef79afd559f8af530450649593fc6.json b/core/lib/dal/.sqlx/query-b2de2ea4a66ff596af40976b05c03f9f912ef79afd559f8af530450649593fc6.json deleted file mode 100644 index c1238981bab1..000000000000 --- a/core/lib/dal/.sqlx/query-b2de2ea4a66ff596af40976b05c03f9f912ef79afd559f8af530450649593fc6.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH\n tee_proofs AS (\n SELECT\n *\n FROM\n tee_proof_generation_details\n WHERE\n tee_type = $1\n ),\n \n upsert AS (\n SELECT\n proof_generation_details.l1_batch_number\n FROM\n proof_generation_details\n LEFT JOIN\n l1_batches\n ON proof_generation_details.l1_batch_number = l1_batches.number\n LEFT JOIN\n tee_proofs\n ON proof_generation_details.l1_batch_number = tee_proofs.l1_batch_number\n WHERE\n (\n proof_generation_details.vm_run_data_blob_url IS NOT NULL\n AND proof_generation_details.proof_gen_data_blob_url IS NOT NULL\n AND l1_batches.hash IS NOT NULL\n AND l1_batches.aux_data_hash IS NOT NULL\n AND l1_batches.meta_parameters_hash IS NOT NULL\n )\n AND (\n (\n tee_proofs.tee_type IS NULL\n AND tee_proofs.status IS NULL\n AND tee_proofs.l1_batch_number IS NULL\n ) OR (\n (\n tee_proofs.status = $3\n OR (\n tee_proofs.status = $2\n AND tee_proofs.prover_taken_at < NOW() - $4::INTERVAL\n )\n )\n AND tee_proofs.l1_batch_number >= $5\n )\n )\n ORDER BY\n l1_batch_number ASC\n LIMIT\n 1\n )\n \n INSERT INTO\n tee_proof_generation_details (\n l1_batch_number, tee_type, status, created_at, updated_at, prover_taken_at\n )\n SELECT\n l1_batch_number,\n $1,\n $2,\n NOW(),\n NOW(),\n NOW()\n FROM\n upsert\n ON CONFLICT (l1_batch_number, tee_type) DO\n UPDATE\n SET\n status = $2,\n updated_at = NOW(),\n prover_taken_at = NOW()\n RETURNING\n l1_batch_number\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "l1_batch_number", - "type_info": "Int8" - } - ], - "parameters": { - "Left": [ - "Text", - "Text", - "Text", - "Interval", - "Int8" - ] - }, - "nullable": [ - false - ] - }, - "hash": "b2de2ea4a66ff596af40976b05c03f9f912ef79afd559f8af530450649593fc6" -} diff --git a/core/lib/dal/.sqlx/query-d825fcac2f7d32a31c159d5674497ae86e2513aca65199717dfd7359feddcd5e.json b/core/lib/dal/.sqlx/query-d825fcac2f7d32a31c159d5674497ae86e2513aca65199717dfd7359feddcd5e.json new file mode 100644 index 000000000000..2d17efc0fccf --- /dev/null +++ b/core/lib/dal/.sqlx/query-d825fcac2f7d32a31c159d5674497ae86e2513aca65199717dfd7359feddcd5e.json @@ -0,0 +1,26 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH upsert AS (\n SELECT\n p.l1_batch_number\n FROM\n proof_generation_details p\n LEFT JOIN\n l1_batches l1\n ON p.l1_batch_number = l1.number\n LEFT JOIN\n tee_proof_generation_details tee\n ON\n p.l1_batch_number = tee.l1_batch_number\n AND tee.tee_type = $1\n WHERE\n (\n p.l1_batch_number >= $5\n AND p.vm_run_data_blob_url IS NOT NULL\n AND p.proof_gen_data_blob_url IS NOT NULL\n AND l1.hash IS NOT NULL\n AND l1.aux_data_hash IS NOT NULL\n AND l1.meta_parameters_hash IS NOT NULL\n )\n AND (\n tee.l1_batch_number IS NULL\n OR (\n tee.status = $3\n OR (\n tee.status = $2\n AND tee.prover_taken_at < NOW() - $4::INTERVAL\n )\n )\n )\n ORDER BY\n l1_batch_number ASC\n FETCH FIRST ROW ONLY\n )\n \n INSERT INTO\n tee_proof_generation_details (\n l1_batch_number, tee_type, status, created_at, updated_at, prover_taken_at\n )\n SELECT\n l1_batch_number,\n $1,\n $2,\n NOW(),\n NOW(),\n NOW()\n FROM\n upsert\n ON CONFLICT (l1_batch_number, tee_type) DO\n UPDATE\n SET\n status = $2,\n updated_at = NOW(),\n prover_taken_at = NOW()\n RETURNING\n l1_batch_number\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "l1_batch_number", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Text", + "Text", + "Text", + "Interval", + "Int8" + ] + }, + "nullable": [ + false + ] + }, + "hash": "d825fcac2f7d32a31c159d5674497ae86e2513aca65199717dfd7359feddcd5e" +} diff --git a/core/lib/dal/src/tee_proof_generation_dal.rs b/core/lib/dal/src/tee_proof_generation_dal.rs index 71c7f1c62af4..c9e5989e6413 100644 --- a/core/lib/dal/src/tee_proof_generation_dal.rs +++ b/core/lib/dal/src/tee_proof_generation_dal.rs @@ -38,55 +38,41 @@ impl TeeProofGenerationDal<'_, '_> { let min_batch_number = min_batch_number.map_or(0, |num| i64::from(num.0)); let query = sqlx::query!( r#" - WITH - tee_proofs AS ( + WITH upsert AS ( SELECT - * + p.l1_batch_number FROM - tee_proof_generation_details - WHERE - tee_type = $1 - ), - - upsert AS ( - SELECT - proof_generation_details.l1_batch_number - FROM - proof_generation_details + proof_generation_details p LEFT JOIN - l1_batches - ON proof_generation_details.l1_batch_number = l1_batches.number + l1_batches l1 + ON p.l1_batch_number = l1.number LEFT JOIN - tee_proofs - ON proof_generation_details.l1_batch_number = tee_proofs.l1_batch_number + tee_proof_generation_details tee + ON + p.l1_batch_number = tee.l1_batch_number + AND tee.tee_type = $1 WHERE ( - proof_generation_details.vm_run_data_blob_url IS NOT NULL - AND proof_generation_details.proof_gen_data_blob_url IS NOT NULL - AND l1_batches.hash IS NOT NULL - AND l1_batches.aux_data_hash IS NOT NULL - AND l1_batches.meta_parameters_hash IS NOT NULL + p.l1_batch_number >= $5 + AND p.vm_run_data_blob_url IS NOT NULL + AND p.proof_gen_data_blob_url IS NOT NULL + AND l1.hash IS NOT NULL + AND l1.aux_data_hash IS NOT NULL + AND l1.meta_parameters_hash IS NOT NULL ) AND ( - ( - tee_proofs.tee_type IS NULL - AND tee_proofs.status IS NULL - AND tee_proofs.l1_batch_number IS NULL - ) OR ( - ( - tee_proofs.status = $3 - OR ( - tee_proofs.status = $2 - AND tee_proofs.prover_taken_at < NOW() - $4::INTERVAL - ) + tee.l1_batch_number IS NULL + OR ( + tee.status = $3 + OR ( + tee.status = $2 + AND tee.prover_taken_at < NOW() - $4::INTERVAL ) - AND tee_proofs.l1_batch_number >= $5 ) ) ORDER BY l1_batch_number ASC - LIMIT - 1 + FETCH FIRST ROW ONLY ) INSERT INTO