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):
|
| 5 |
+
"""
|
| 6 |
+
Execute `query` in DuckDB and stream results directly to Parquet via COPY.
|
| 7 |
+
Also shows a small preview (LIMIT 5) and prints row count from the written file.
|
| 8 |
+
"""
|
| 9 |
+
con = duckdb.connect(database=":memory:", read_only=False)
|
| 10 |
+
print("DuckDB connection established.")
|
| 11 |
+
print("-" * 30)
|
| 12 |
+
|
| 13 |
+
# Ensure output folder exists
|
| 14 |
+
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 15 |
+
|
| 16 |
+
# Write results straight to Parquet (no Python materialization)
|
| 17 |
+
copy_stmt = f"""
|
| 18 |
+
COPY (
|
| 19 |
+
{query}
|
| 20 |
+
) TO '{output_path}'
|
| 21 |
+
(FORMAT 'parquet', COMPRESSION 'ZSTD');
|
| 22 |
+
"""
|
| 23 |
+
con.execute(copy_stmt)
|
| 24 |
+
print("Query finished and written via COPY.")
|
| 25 |
+
print("-" * 30)
|
| 26 |
+
|
| 27 |
+
# Preview a few rows without materializing full result
|
| 28 |
+
print("\n--- Preview (first 5 rows) ---")
|
| 29 |
+
preview_df = con.execute(query + " LIMIT 5").df()
|
| 30 |
+
print(preview_df)
|
| 31 |
+
|
| 32 |
+
# Verify row count from the written file
|
| 33 |
+
count = con.execute(f"SELECT COUNT(*) FROM read_parquet('{output_path}')").fetchone()[0]
|
| 34 |
+
print(f"\nRows written: {count}")
|
| 35 |
+
print(f"Final joined table saved to: {output_path}\n")
|
| 36 |
+
|
| 37 |
+
con.close()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def join_movie_info_with_title():
|
| 41 |
+
base_path = "."
|
| 42 |
+
output_path = os.path.join(base_path, "title_with_movie_info.parquet")
|
| 43 |
+
|
| 44 |
+
query = f"""
|
| 45 |
+
SELECT
|
| 46 |
+
mi.id AS movie_info_id,
|
| 47 |
+
mi.movie_id AS movie_info_movie_id,
|
| 48 |
+
mi.info_type_id AS movie_info_info_type_id,
|
| 49 |
+
mi.info AS movie_info_info,
|
| 50 |
+
mi.note AS movie_info_note,
|
| 51 |
+
t.id AS title_id,
|
| 52 |
+
t.title AS title_title,
|
| 53 |
+
t.imdb_index AS title_imdb_index,
|
| 54 |
+
t.kind_id AS title_kind_id,
|
| 55 |
+
t.production_year AS title_production_year,
|
| 56 |
+
t.imdb_id AS title_imdb_id,
|
| 57 |
+
t.phonetic_code AS title_phonetic_code,
|
| 58 |
+
t.episode_of_id AS title_episode_of_id,
|
| 59 |
+
t.season_nr AS title_season_nr,
|
| 60 |
+
t.episode_nr AS title_episode_nr,
|
| 61 |
+
t.series_years AS title_series_years,
|
| 62 |
+
t.md5sum AS title_md5sum
|
| 63 |
+
FROM
|
| 64 |
+
read_parquet('{base_path}/movie_info.parquet') AS mi
|
| 65 |
+
JOIN
|
| 66 |
+
read_parquet('{base_path}/title.parquet') AS t ON t.id = mi.movie_id
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
run_and_copy(query, output_path)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def join_cast_info_with_title():
|
| 73 |
+
base_path = "."
|
| 74 |
+
output_path = os.path.join(base_path, "title_with_cast_info.parquet")
|
| 75 |
+
|
| 76 |
+
query = f"""
|
| 77 |
+
SELECT
|
| 78 |
+
ci.id AS cast_info_id,
|
| 79 |
+
ci.person_id AS cast_info_person_id,
|
| 80 |
+
ci.movie_id AS cast_info_movie_id,
|
| 81 |
+
ci.person_role_id AS cast_info_person_role_id,
|
| 82 |
+
ci.note AS cast_info_note,
|
| 83 |
+
ci.nr_order AS cast_info_nr_order,
|
| 84 |
+
ci.role_id AS cast_info_role_id,
|
| 85 |
+
t.id AS title_id,
|
| 86 |
+
t.title AS title_title,
|
| 87 |
+
t.imdb_index AS title_imdb_index,
|
| 88 |
+
t.kind_id AS title_kind_id,
|
| 89 |
+
t.production_year AS title_production_year,
|
| 90 |
+
t.imdb_id AS title_imdb_id,
|
| 91 |
+
t.phonetic_code AS title_phonetic_code,
|
| 92 |
+
t.episode_of_id AS title_episode_of_id,
|
| 93 |
+
t.season_nr AS title_season_nr,
|
| 94 |
+
t.episode_nr AS title_episode_nr,
|
| 95 |
+
t.series_years AS title_series_years,
|
| 96 |
+
t.md5sum AS title_md5sum
|
| 97 |
+
FROM
|
| 98 |
+
read_parquet('{base_path}/cast_info.parquet') AS ci
|
| 99 |
+
JOIN
|
| 100 |
+
read_parquet('{base_path}/title.parquet') AS t ON t.id = ci.movie_id
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
run_and_copy(query, output_path)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def join_title_with_movie_companies_and_company_name():
|
| 107 |
+
base_path = "."
|
| 108 |
+
output_path = os.path.join(base_path, "title_with_movie_companies_and_company_name.parquet")
|
| 109 |
+
|
| 110 |
+
query = f"""
|
| 111 |
+
SELECT
|
| 112 |
+
t.id as title_id,
|
| 113 |
+
t.title as title_title,
|
| 114 |
+
t.imdb_index as title_imdb_index,
|
| 115 |
+
t.kind_id as title_kind_id,
|
| 116 |
+
t.production_year as title_production_year,
|
| 117 |
+
t.imdb_id as title_imdb_id,
|
| 118 |
+
t.phonetic_code as title_phonetic_code,
|
| 119 |
+
t.episode_of_id as title_episode_of_id,
|
| 120 |
+
t.season_nr as title_season_nr,
|
| 121 |
+
t.episode_nr as title_episode_nr,
|
| 122 |
+
t.series_years as title_series_years,
|
| 123 |
+
t.md5sum as title_md5sum,
|
| 124 |
+
mc.id as movie_companies_id,
|
| 125 |
+
mc.movie_id as movie_companies_movie_id,
|
| 126 |
+
mc.company_id as movie_companies_company_id,
|
| 127 |
+
mc.company_type_id as movie_companies_company_type_id,
|
| 128 |
+
mc.note as movie_companies_note,
|
| 129 |
+
cn.id as company_name_id,
|
| 130 |
+
cn.name as company_name_name,
|
| 131 |
+
cn.country_code as company_name_country_code,
|
| 132 |
+
cn.imdb_id as company_name_imdb_id,
|
| 133 |
+
cn.name_pcode_nf as company_name_name_pcode_nf,
|
| 134 |
+
cn.name_pcode_sf as company_name_name_pcode_sf,
|
| 135 |
+
cn.md5sum as company_name_md5sum
|
| 136 |
+
FROM
|
| 137 |
+
read_parquet('{base_path}/title.parquet') AS t
|
| 138 |
+
JOIN
|
| 139 |
+
read_parquet('{base_path}/movie_companies.parquet') AS mc ON t.id = mc.movie_id
|
| 140 |
+
JOIN
|
| 141 |
+
read_parquet('{base_path}/company_name.parquet') AS cn ON mc.company_id = cn.id
|
| 142 |
+
"""
|
| 143 |
+
|
| 144 |
+
run_and_copy(query, output_path)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
join_movie_info_with_title()
|
| 149 |
+
join_cast_info_with_title()
|
| 150 |
+
join_title_with_movie_companies_and_company_name()
|