|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::{ |
| 19 | + fs::create_dir_all, |
| 20 | + path::{Path, PathBuf}, |
| 21 | + sync::Arc, |
| 22 | +}; |
| 23 | + |
| 24 | +use color_eyre::{eyre, Result}; |
| 25 | +use datafusion::arrow::record_batch::RecordBatch; |
| 26 | +use log::info; |
| 27 | +use parquet::arrow::ArrowWriter; |
| 28 | +use tpchgen::generators::{ |
| 29 | + CustomerGenerator, LineItemGenerator, NationGenerator, OrderGenerator, PartGenerator, |
| 30 | + PartSuppGenerator, RegionGenerator, SupplierGenerator, |
| 31 | +}; |
| 32 | +use tpchgen_arrow::{ |
| 33 | + CustomerArrow, LineItemArrow, NationArrow, OrderArrow, PartArrow, PartSuppArrow, RegionArrow, |
| 34 | + SupplierArrow, |
| 35 | +}; |
| 36 | + |
| 37 | +use crate::config::AppConfig; |
| 38 | + |
| 39 | +enum GeneratorType { |
| 40 | + Customer, |
| 41 | + Order, |
| 42 | + LineItem, |
| 43 | + Nation, |
| 44 | + Part, |
| 45 | + PartSupp, |
| 46 | + Region, |
| 47 | + Supplier, |
| 48 | +} |
| 49 | + |
| 50 | +impl TryFrom<&str> for GeneratorType { |
| 51 | + type Error = color_eyre::Report; |
| 52 | + |
| 53 | + fn try_from(value: &str) -> std::result::Result<Self, Self::Error> { |
| 54 | + match value { |
| 55 | + "customers" => Ok(Self::Customer), |
| 56 | + "orders" => Ok(Self::Order), |
| 57 | + "line_items" => Ok(Self::LineItem), |
| 58 | + "nations" => Ok(Self::Nation), |
| 59 | + "parts" => Ok(Self::Part), |
| 60 | + "part_supps" => Ok(Self::PartSupp), |
| 61 | + "regions" => Ok(Self::Region), |
| 62 | + "suppliers" => Ok(Self::Supplier), |
| 63 | + _ => Err(eyre::Report::msg(format!("Unknown generator type {value}"))), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn create_tpch_dirs(config: &AppConfig) -> Result<Vec<(GeneratorType, PathBuf)>> { |
| 69 | + info!("...configured DB directory is {:?}", config.db.path); |
| 70 | + if config.db.path.is_file() { |
| 71 | + eyre::bail!("config DB directory is a file and it must be a directory") |
| 72 | + } |
| 73 | + |
| 74 | + if !config.db.path.exists() { |
| 75 | + info!("...DB directory does not exist, creating"); |
| 76 | + std::fs::create_dir_all(config.db.path.clone())?; |
| 77 | + } else { |
| 78 | + info!("...DB directory exists"); |
| 79 | + } |
| 80 | + let tpch_dir = config.db.path.join("tables").join("tpch"); |
| 81 | + if !tpch_dir.exists() { |
| 82 | + info!( |
| 83 | + "...TPC-H table directory ({:?}) does not exist, creating", |
| 84 | + config.db.path |
| 85 | + ); |
| 86 | + create_dir_all(&tpch_dir)?; |
| 87 | + } else { |
| 88 | + info!("...TPC-H table directory ({tpch_dir:?}) exists"); |
| 89 | + }; |
| 90 | + let needed_dirs = [ |
| 91 | + "customers", |
| 92 | + "orders", |
| 93 | + "line_items", |
| 94 | + "nations", |
| 95 | + "parts", |
| 96 | + "part_supps", |
| 97 | + "regions", |
| 98 | + "suppliers", |
| 99 | + ]; |
| 100 | + let mut table_paths = Vec::new(); |
| 101 | + for dir in needed_dirs { |
| 102 | + let table_path = tpch_dir.join(dir); |
| 103 | + create_dir_all(&table_path)?; |
| 104 | + table_paths.push((GeneratorType::try_from(dir)?, table_path)) |
| 105 | + } |
| 106 | + Ok(table_paths) |
| 107 | +} |
| 108 | + |
| 109 | +fn write_batches_to_parquet<I>( |
| 110 | + mut batches: std::iter::Peekable<I>, |
| 111 | + table_path: &Path, |
| 112 | + table_type: &str, |
| 113 | +) -> Result<()> |
| 114 | +where |
| 115 | + I: Iterator<Item = RecordBatch>, |
| 116 | +{ |
| 117 | + let first = batches.peek().ok_or(eyre::Error::msg(format!( |
| 118 | + "unable to generate {table_type} TPC-H data" |
| 119 | + )))?; |
| 120 | + |
| 121 | + let file_path = table_path.join("data.parquet"); |
| 122 | + let file = std::fs::File::create(file_path)?; |
| 123 | + let mut writer = ArrowWriter::try_new(file, Arc::clone(first.schema_ref()), None)?; |
| 124 | + info!("...writing {table_type} batches"); |
| 125 | + for batch in batches { |
| 126 | + writer.write(&batch)?; |
| 127 | + } |
| 128 | + writer.finish()?; |
| 129 | + Ok(()) |
| 130 | +} |
| 131 | + |
| 132 | +pub fn generate(config: AppConfig, scale_factor: f64) -> Result<()> { |
| 133 | + info!("Generating TPC-H data"); |
| 134 | + let table_paths = create_tpch_dirs(&config)?; |
| 135 | + for (table, table_path) in table_paths { |
| 136 | + if table_path.is_dir() { |
| 137 | + match table { |
| 138 | + GeneratorType::Customer => { |
| 139 | + info!("...generating customers"); |
| 140 | + let arrow_generator = |
| 141 | + CustomerArrow::new(CustomerGenerator::new(scale_factor, 1, 1)); |
| 142 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Customer")?; |
| 143 | + } |
| 144 | + GeneratorType::Order => { |
| 145 | + info!("...generating orders"); |
| 146 | + let arrow_generator = OrderArrow::new(OrderGenerator::new(scale_factor, 1, 1)); |
| 147 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Order")?; |
| 148 | + } |
| 149 | + GeneratorType::LineItem => { |
| 150 | + info!("...generating LineItems"); |
| 151 | + let arrow_generator = |
| 152 | + LineItemArrow::new(LineItemGenerator::new(scale_factor, 1, 1)); |
| 153 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "LineItem")?; |
| 154 | + } |
| 155 | + GeneratorType::Nation => { |
| 156 | + info!("...generating Nations"); |
| 157 | + let arrow_generator = |
| 158 | + NationArrow::new(NationGenerator::new(scale_factor, 1, 1)); |
| 159 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Nation")?; |
| 160 | + } |
| 161 | + GeneratorType::Part => { |
| 162 | + info!("...generating Parts"); |
| 163 | + let arrow_generator = PartArrow::new(PartGenerator::new(scale_factor, 1, 1)); |
| 164 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Part")?; |
| 165 | + } |
| 166 | + GeneratorType::PartSupp => { |
| 167 | + info!("...generating PartSupps"); |
| 168 | + let arrow_generator = |
| 169 | + PartSuppArrow::new(PartSuppGenerator::new(scale_factor, 1, 1)); |
| 170 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "PartSupp")?; |
| 171 | + } |
| 172 | + GeneratorType::Region => { |
| 173 | + info!("...generating Regions"); |
| 174 | + let arrow_generator = |
| 175 | + RegionArrow::new(RegionGenerator::new(scale_factor, 1, 1)); |
| 176 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Region")?; |
| 177 | + } |
| 178 | + GeneratorType::Supplier => { |
| 179 | + info!("...generating Suppliers"); |
| 180 | + let arrow_generator = |
| 181 | + SupplierArrow::new(SupplierGenerator::new(scale_factor, 1, 1)); |
| 182 | + write_batches_to_parquet(arrow_generator.peekable(), &table_path, "Supplier")?; |
| 183 | + } |
| 184 | + }; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + Ok(()) |
| 189 | +} |
0 commit comments