Skip to content

Commit

Permalink
Cleanup and formatting (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger authored Nov 7, 2021
1 parent ada1e1e commit 03ae387
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 126 deletions.
2 changes: 1 addition & 1 deletion 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,6 +1,6 @@
[package]
name = "flaco"
version = "0.4.2"
version = "0.5.0"
edition = "2018"
license = "Unlicense/MIT"

Expand Down
49 changes: 0 additions & 49 deletions examples/memory_profile.rs

This file was deleted.

2 changes: 1 addition & 1 deletion flaco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.4.2"
__version__ = "0.5.0"
from .io import read_sql, Database
60 changes: 28 additions & 32 deletions flaco/io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,37 @@ cpdef dict read_sql(str stmt, Database db, int n_rows=-1, int size_hint=-1):
lib.RowDataArrayPtr row_data_ptr
lib.Data *data

while True:
if row_iterator == NULL:
break
else:

if row_idx == 0:
# Initialize arrays for output
# will resize at `n_increment` if `n_rows` is not set.
while row_iterator != NULL:
if row_idx == 0:
# Initialize arrays for output
# will resize at `n_increment` if `n_rows` is not set.
for i in range(0, n_columns):
data = lib.index_row(row_data_array_ptr, n_columns, i)
output.append(
array_init(deref(data), n_increment if n_rows == -1 else n_rows)
)

# grow arrays if next insert is passed current len
if _n_rows == -1 and current_array_len <= row_idx:
for i in range(0, n_columns):
data = lib.index_row(row_data_array_ptr, n_columns, i)
output.append(
array_init(deref(data), n_increment if n_rows == -1 else n_rows)
)
resize(output[i], current_array_len + n_increment)
current_array_len += n_increment

# grow arrays if next insert is passed current len
if _n_rows == -1 and current_array_len <= row_idx:
for i in range(0, n_columns):
resize(output[i], current_array_len + n_increment)
current_array_len += n_increment
for i in range(0, n_columns):
data = lib.index_row(row_data_array_ptr, n_columns, i)
output[i] = insert_data_into_array(deref(data), output[i], row_idx)

for i in range(0, n_columns):
data = lib.index_row(row_data_array_ptr, n_columns, i)
output[i] = insert_data_into_array(deref(data), output[i], row_idx)

row_idx += one

lib.next_row(
&row_iterator,
&row_data_array_ptr,
&n_columns,
&column_names,
&exc
)
if exc != NULL:
raise FlacoException(exc.decode())
row_idx += one

lib.next_row(
&row_iterator,
&row_data_array_ptr,
&n_columns,
&column_names,
&exc
)
if exc != NULL:
raise FlacoException(exc.decode())

# Ensure arrays are correct size; only if n_rows not set
if _n_rows == -1 and current_array_len != row_idx:
Expand Down
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
libraries.extend(["ntdll", "ws2_32", "bcrypt", "advapi32"])
flaco_lib_file = flaco_lib_dir.joinpath("flaco.lib")
extra_link_args = []
extra_compile_args = [
"/link",
"/SUBSYSTEM:WINDOWS",
]
extra_compile_args = ["/link", "/SUBSYSTEM:WINDOWS"]
else:
flaco_lib_file = flaco_lib_dir.joinpath("libflaco.a")
extra_link_args = ["-l:libflaco.a"]
extra_compile_args = ["-fopenmp", "-O3"]

assert flaco_lib_file.is_file(), "flaco lib not built; run 'cargo build --release'"


extension = Extension(
name="*",
sources=[str(pathlib.Path("flaco/*.pyx"))],
Expand All @@ -51,7 +47,7 @@

setup(
name="flaco",
version="0.4.2",
version="0.5.0",
author="Miles Granger",
author_email="miles59923@gmail.com",
description="Fast and Efficient PostgreSQL data into numpy/pandas",
Expand Down
52 changes: 16 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//#![warn(missing_docs)]
use postgres as pg;
use postgres::fallible_iterator::FallibleIterator;
use postgres::types::{FromSql, Type};
use postgres::RowIter;
use rust_decimal::prelude::{Decimal, ToPrimitive};
use std::error::Error;
use std::ffi::CString;
use std::net::IpAddr;
use std::os::raw::c_char;
use std::{ffi, mem};
use std::error::Error;
use postgres::types::{FromSql, Type};
use time;

type DatabasePtr = *mut u32;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl From<CString> for StringPtr {
#[repr(C)]
pub struct DateInfo {
/// The value represents the number of days since January 1st, 2000.
offset: i32
offset: i32,
}

impl FromSql<'_> for DateInfo {
Expand Down Expand Up @@ -189,7 +189,7 @@ impl FromSql<'_> for TimeInfo {
#[repr(C)]
pub struct DateTimeInfo {
/// The value represents the number of microseconds since midnight, January 1st, 2000.
offset: i64
offset: i64,
}
impl FromSql<'_> for DateTimeInfo {
fn from_sql(_: &Type, raw: &[u8]) -> std::result::Result<Self, Box<dyn Error + Sync + Send>> {
Expand Down Expand Up @@ -248,7 +248,6 @@ simple_from!(TimeInfo, Time);
simple_from!(BytesPtr, Bytes);
simple_from!(StringPtr, String);


#[no_mangle]
pub extern "C" fn free_db(ptr: DatabasePtr) {
unsafe { Box::from_raw(ptr as DatabasePtr) };
Expand Down Expand Up @@ -325,28 +324,13 @@ fn row_data(row: pg::Row, array_ptr: &mut RowDataArrayPtr) -> Result<()> {
// TODO: postgres-types: expose Inner enum which these variations
// and have postgres Row.type/(or something) expose the variant
let value: Data = match type_.name() {
"bytea" => row
.get::<_, Option<Vec<u8>>>(i)
.map(BytesPtr::from)
.into(),
"char" => row
.get::<_, Option<i8>>(i)
.into(),
"smallint" | "smallserial" | "int2" => row
.get::<_, Option<i16>>(i)
.into(),
"oid" => row
.get::<_, Option<u32>>(i)
.into(),
"int4" | "int" | "serial" => row
.get::<_, Option<i32>>(i)
.into(),
"bigint" | "int8" | "bigserial" => row
.get::<_, Option<i64>>(i)
.into(),
"bool" => row
.get::<_, Option<bool>>(i)
.into(),
"bytea" => row.get::<_, Option<Vec<u8>>>(i).map(BytesPtr::from).into(),
"char" => row.get::<_, Option<i8>>(i).into(),
"smallint" | "smallserial" | "int2" => row.get::<_, Option<i16>>(i).into(),
"oid" => row.get::<_, Option<u32>>(i).into(),
"int4" | "int" | "serial" => row.get::<_, Option<i32>>(i).into(),
"bigint" | "int8" | "bigserial" => row.get::<_, Option<i64>>(i).into(),
"bool" => row.get::<_, Option<bool>>(i).into(),
"double precision" | "float8" => row
.get::<_, Option<f64>>(i)
.or_else(|| Some(f64::NAN))
Expand All @@ -360,15 +344,11 @@ fn row_data(row: pg::Row, array_ptr: &mut RowDataArrayPtr) -> Result<()> {
.map(|v| CString::new(v).unwrap())
.map(StringPtr::from)
.into(),
"timestamp" | "timestamp with time zone" | "timestamptz" => row
.get::<_, Option<DateTimeInfo>>(i)
.into(),
"date" => row
.get::<_, Option<DateInfo>>(i)
.into(),
"time" => row
.get::<_, Option<TimeInfo>>(i)
.into(),
"timestamp" | "timestamp with time zone" | "timestamptz" => {
row.get::<_, Option<DateTimeInfo>>(i).into()
}
"date" => row.get::<_, Option<DateInfo>>(i).into(),
"time" => row.get::<_, Option<TimeInfo>>(i).into(),
"json" | "jsonb" => row
.get::<_, Option<serde_json::Value>>(i)
.map(|v| CString::new(v.to_string()).unwrap())
Expand Down

0 comments on commit 03ae387

Please sign in to comment.