Skip to content

Commit db63b1c

Browse files
perf: only check for orphaned records on entities with at least one record
1 parent 3eca2f1 commit db63b1c

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

  • src/dve/core_engine/backends/implementations/duckdb

‎src/dve/core_engine/backends/implementations/duckdb/rules.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
duckdb_write_parquet,
3030
get_all_registered_udfs,
3131
get_duckdb_type_from_annotation,
32+
relation_is_empty,
3233
)
3334
from dve.core_engine.backends.implementations.duckdb.types import (
3435
DuckDBEntities,
@@ -396,6 +397,10 @@ def identify_orphans(
396397
target_rel: DuckDBPyRelation = entities[config.target_name]
397398
target_rel = target_rel.set_alias(config.target_name)
398399

400+
if relation_is_empty(source_rel):
401+
self.logger.info(f"{config.entity_name} is empty. Skipping orphan check.")
402+
return [], 0
403+
399404
match_name = f"matched_{uuid4().hex}"
400405
target_rel = target_rel.select(
401406
StarExpression(exclude=[]), ConstantExpression(1).alias(match_name)
@@ -420,16 +425,16 @@ def identify_orphans(
420425
_orph_records: tuple[int] = orphaned_rel.count(RECORD_INDEX_COLUMN_NAME).fetchone() # type: ignore # pylint: disable=C0301
421426
if _orph_records:
422427
_no_orphans = _orph_records[0]
428+
if entities.get(ORPHANED_RECORD_ENTITY_NAME) is not None:
429+
entities[ORPHANED_RECORD_ENTITY_NAME] = entities[ORPHANED_RECORD_ENTITY_NAME].union(
430+
orphaned_rel
431+
)
432+
else:
433+
entities[ORPHANED_RECORD_ENTITY_NAME] = orphaned_rel
423434
else:
424435
_no_orphans = 0
425436
self.logger.info(f"Found {_no_orphans} orphaned records in {config.entity_name}.")
426437

427-
if entities.get(ORPHANED_RECORD_ENTITY_NAME) is not None:
428-
entities[ORPHANED_RECORD_ENTITY_NAME] = entities[ORPHANED_RECORD_ENTITY_NAME].union(
429-
orphaned_rel
430-
)
431-
else:
432-
entities[ORPHANED_RECORD_ENTITY_NAME] = orphaned_rel
433438
return [], _no_orphans
434439

435440
def remove_orphans(self, entities: DuckDBEntities, *, config: OrphanRemoval) -> Iterator:

0 commit comments

Comments
 (0)