Veweew's picture
Create join.py
3ecb5ce verified
Raw History Blame Contribute Delete
5.13 kB
import duckdb
import os
def run_and_copy(query: str, output_path: str, memory_limit: str = "2GB"):
"""
Execute `query` in DuckDB and stream results directly to Parquet via COPY.
Also shows a small preview (LIMIT 5) and prints row count from the written file.
"""
con = duckdb.connect(database=":memory:", read_only=False)
print("DuckDB connection established.")
print("-" * 30)
# Ensure output folder exists
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
# Write results straight to Parquet (no Python materialization)
copy_stmt = f"""
COPY (
{query}
) TO '{output_path}'
(FORMAT 'parquet', COMPRESSION 'ZSTD');
"""
con.execute(copy_stmt)
print("Query finished and written via COPY.")
print("-" * 30)
# Preview a few rows without materializing full result
print("\n--- Preview (first 5 rows) ---")
preview_df = con.execute(query + " LIMIT 5").df()
print(preview_df)
# Verify row count from the written file
count = con.execute(f"SELECT COUNT(*) FROM read_parquet('{output_path}')").fetchone()[0]
print(f"\nRows written: {count}")
print(f"Final joined table saved to: {output_path}\n")
con.close()
def join_movie_info_with_title():
base_path = "."
output_path = os.path.join(base_path, "title_with_movie_info.parquet")
query = f"""
SELECT
mi.id AS movie_info_id,
mi.movie_id AS movie_info_movie_id,
mi.info_type_id AS movie_info_info_type_id,
mi.info AS movie_info_info,
mi.note AS movie_info_note,
t.id AS title_id,
t.title AS title_title,
t.imdb_index AS title_imdb_index,
t.kind_id AS title_kind_id,
t.production_year AS title_production_year,
t.imdb_id AS title_imdb_id,
t.phonetic_code AS title_phonetic_code,
t.episode_of_id AS title_episode_of_id,
t.season_nr AS title_season_nr,
t.episode_nr AS title_episode_nr,
t.series_years AS title_series_years,
t.md5sum AS title_md5sum
FROM
read_parquet('{base_path}/movie_info.parquet') AS mi
JOIN
read_parquet('{base_path}/title.parquet') AS t ON t.id = mi.movie_id
"""
run_and_copy(query, output_path)
def join_cast_info_with_title():
base_path = "."
output_path = os.path.join(base_path, "title_with_cast_info.parquet")
query = f"""
SELECT
ci.id AS cast_info_id,
ci.person_id AS cast_info_person_id,
ci.movie_id AS cast_info_movie_id,
ci.person_role_id AS cast_info_person_role_id,
ci.note AS cast_info_note,
ci.nr_order AS cast_info_nr_order,
ci.role_id AS cast_info_role_id,
t.id AS title_id,
t.title AS title_title,
t.imdb_index AS title_imdb_index,
t.kind_id AS title_kind_id,
t.production_year AS title_production_year,
t.imdb_id AS title_imdb_id,
t.phonetic_code AS title_phonetic_code,
t.episode_of_id AS title_episode_of_id,
t.season_nr AS title_season_nr,
t.episode_nr AS title_episode_nr,
t.series_years AS title_series_years,
t.md5sum AS title_md5sum
FROM
read_parquet('{base_path}/cast_info.parquet') AS ci
JOIN
read_parquet('{base_path}/title.parquet') AS t ON t.id = ci.movie_id
"""
run_and_copy(query, output_path)
def join_title_with_movie_companies_and_company_name():
base_path = "."
output_path = os.path.join(base_path, "title_with_movie_companies_and_company_name.parquet")
query = f"""
SELECT
t.id as title_id,
t.title as title_title,
t.imdb_index as title_imdb_index,
t.kind_id as title_kind_id,
t.production_year as title_production_year,
t.imdb_id as title_imdb_id,
t.phonetic_code as title_phonetic_code,
t.episode_of_id as title_episode_of_id,
t.season_nr as title_season_nr,
t.episode_nr as title_episode_nr,
t.series_years as title_series_years,
t.md5sum as title_md5sum,
mc.id as movie_companies_id,
mc.movie_id as movie_companies_movie_id,
mc.company_id as movie_companies_company_id,
mc.company_type_id as movie_companies_company_type_id,
mc.note as movie_companies_note,
cn.id as company_name_id,
cn.name as company_name_name,
cn.country_code as company_name_country_code,
cn.imdb_id as company_name_imdb_id,
cn.name_pcode_nf as company_name_name_pcode_nf,
cn.name_pcode_sf as company_name_name_pcode_sf,
cn.md5sum as company_name_md5sum
FROM
read_parquet('{base_path}/title.parquet') AS t
JOIN
read_parquet('{base_path}/movie_companies.parquet') AS mc ON t.id = mc.movie_id
JOIN
read_parquet('{base_path}/company_name.parquet') AS cn ON mc.company_id = cn.id
"""
run_and_copy(query, output_path)
if __name__ == "__main__":
join_movie_info_with_title()
join_cast_info_with_title()
join_title_with_movie_companies_and_company_name()