pbordescnil commited on
Commit
3a7d2b8
·
1 Parent(s): 60ee538

Deduplicate Hub models and datasets before import

Browse files
database_refresh/refresh_database.py CHANGED
@@ -66,16 +66,36 @@ def create_views(
66
  connection.execute(
67
  f"""
68
  CREATE VIEW source_models AS
69
- SELECT *
70
- FROM read_parquet('{sql_path(models_path)}')
 
 
 
 
 
 
 
 
 
 
71
  {model_limit}
72
  """
73
  )
74
  connection.execute(
75
  f"""
76
  CREATE VIEW source_datasets AS
77
- SELECT *
78
- FROM read_parquet('{sql_path(datasets_path)}')
 
 
 
 
 
 
 
 
 
 
79
  {dataset_limit}
80
  """
81
  )
@@ -335,10 +355,14 @@ def write_metadata(
335
  return path
336
 
337
 
338
- def parquet_count(path: Path) -> int:
339
  connection = duckdb.connect()
340
  count = connection.execute(
341
- f"SELECT count(*) FROM read_parquet('{sql_path(path)}')"
 
 
 
 
342
  ).fetchone()[0]
343
  connection.close()
344
  return int(count)
@@ -428,8 +452,8 @@ def main() -> int:
428
  return 0
429
 
430
  dump_path = build_dump(files, args.work_dir, args.neo4j_admin)
431
- model_count = args.max_models or parquet_count(models_path)
432
- dataset_count = args.max_datasets or parquet_count(datasets_path)
433
  metadata_path = write_metadata(
434
  args.work_dir,
435
  source_revision,
 
66
  connection.execute(
67
  f"""
68
  CREATE VIEW source_models AS
69
+ SELECT * EXCLUDE (_dedupe_rank)
70
+ FROM (
71
+ SELECT
72
+ *,
73
+ row_number() OVER (
74
+ PARTITION BY id
75
+ ORDER BY lastModified DESC NULLS LAST, _id DESC NULLS LAST
76
+ ) AS _dedupe_rank
77
+ FROM read_parquet('{sql_path(models_path)}')
78
+ WHERE id IS NOT NULL AND trim(id) <> ''
79
+ )
80
+ WHERE _dedupe_rank = 1
81
  {model_limit}
82
  """
83
  )
84
  connection.execute(
85
  f"""
86
  CREATE VIEW source_datasets AS
87
+ SELECT * EXCLUDE (_dedupe_rank)
88
+ FROM (
89
+ SELECT
90
+ *,
91
+ row_number() OVER (
92
+ PARTITION BY id
93
+ ORDER BY lastModified DESC NULLS LAST, _id DESC NULLS LAST
94
+ ) AS _dedupe_rank
95
+ FROM read_parquet('{sql_path(datasets_path)}')
96
+ WHERE id IS NOT NULL AND trim(id) <> ''
97
+ )
98
+ WHERE _dedupe_rank = 1
99
  {dataset_limit}
100
  """
101
  )
 
355
  return path
356
 
357
 
358
+ def parquet_unique_id_count(path: Path) -> int:
359
  connection = duckdb.connect()
360
  count = connection.execute(
361
+ f"""
362
+ SELECT count(DISTINCT id)
363
+ FROM read_parquet('{sql_path(path)}')
364
+ WHERE id IS NOT NULL AND trim(id) <> ''
365
+ """
366
  ).fetchone()[0]
367
  connection.close()
368
  return int(count)
 
452
  return 0
453
 
454
  dump_path = build_dump(files, args.work_dir, args.neo4j_admin)
455
+ model_count = args.max_models or parquet_unique_id_count(models_path)
456
+ dataset_count = args.max_datasets or parquet_unique_id_count(datasets_path)
457
  metadata_path = write_metadata(
458
  args.work_dir,
459
  source_revision,