Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

CLI #40

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
!/.github
!/LICENSE.txt
!/.gitattributes
!/cli

__pycache__
*.so
Expand Down
118 changes: 108 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["core", "py", "json"]
members = ["core", "py", "json", "cli"]
resolver = "2"

[profile.bench.package.pgpq]
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHONY: init build test
PHONY: init build-develop build-cli test

.init:
rm -rf .venv
Expand All @@ -15,10 +15,17 @@ init: .clean .init
build-develop: .init
. ./.venv/bin/activate && maturin develop -m py/Cargo.toml
. ./.venv/bin/activate && maturin develop -m json/Cargo.toml
cargo build --package pgpq-cli

test: build-develop
cargo test
./.venv/bin/python -m pytest

lint: build-develop
./.venv/bin/pre-commit run --all-files

build-cli:
cargo build --package pgpq-cli --release

install-cli: build-cli
cp target/release/pgpq /usr/local/bin/
15 changes: 15 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pgpq-cli"
version = "0.1.0"
edition = "2021"

[dependencies]
pgpq = { path = "../core" }
clap = "3.0"
arrow = "46.0.0"
parquet = "46.0.0"
bytes = "1.7.1"

[[bin]]
name = "pgpq"
path = "src/main.rs"
72 changes: 72 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use arrow::record_batch::RecordBatch;
use bytes::BytesMut;
use clap::{App, Arg};
use parquet::arrow::arrow_reader::ParquetRecordBatchReader;
use pgpq::ArrowToPostgresBinaryEncoder;
use std::fs::File;
use std::io::{self, Read, Write};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let matches = App::new("pgpq")
.version("0.1.0")
.about("Converts Parquet files to PostgreSQL binary format")
.arg(
Arg::with_name("input")
.short('i')
.long("input")
.value_name("FILE")
.help("Input Parquet file (use '-' for stdin)")
.takes_value(true)
.required(true),
)
.get_matches();

let input = matches.value_of("input").unwrap();

let record_batches = if input == "-" {
read_parquet_from_stdin()?
} else {
read_parquet_from_file(input)?
};

let mut encoder = ArrowToPostgresBinaryEncoder::try_new(&record_batches[0].schema())?;
let mut buffer = BytesMut::new();

// Write header
encoder.write_header(&mut buffer);
io::stdout().write_all(&buffer)?;
buffer.clear();

// Write batches
for batch in record_batches {
encoder.write_batch(&batch, &mut buffer)?;
io::stdout().write_all(&buffer)?;
buffer.clear();
}

Ok(())
}

fn read_parquet_from_stdin() -> Result<Vec<RecordBatch>, Box<dyn std::error::Error>> {
let mut buffer = Vec::new();
io::stdin().read_to_end(&mut buffer)?;
// Implement Parquet reading from buffer
// This part needs to be implemented using the parquet crate
unimplemented!()
}

fn read_parquet_from_file(path: &str) -> Result<Vec<RecordBatch>, Box<dyn std::error::Error>> {
// Open the file
let file = File::open(path)?;

// Create a ParquetRecordBatchReader directly from the file
let record_batch_reader = ParquetRecordBatchReader::try_new(file, 8192)?;

// Read all record batches
let mut record_batches = Vec::new();
for batch in record_batch_reader {
record_batches.push(batch?);
}

Ok(record_batches)
}
Loading