Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 104 additions & 0 deletions connectorx-python/connectorx/tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,110 @@ def test_postgres_types_cursor(postgres_url: str) -> None:
assert_frame_equal(df, expected, check_names=True)


def test_types_simple(postgres_url: str) -> None:
query = "SELECT test_date, test_timestamp, test_timestamptz, test_int16, test_int64, test_float32, test_numeric, test_bpchar, test_char, test_varchar, test_uuid, test_time, test_json, test_jsonb, test_bytea, test_enum::text, test_f4array, test_f8array, test_narray, test_i2array, test_i4array, test_i8array FROM test_types"
df = read_sql(postgres_url, query, protocol="simple")
expected = pd.DataFrame(
index=range(4),
data={
"test_date": pd.Series(
["1970-01-01", "2000-02-28", "2038-01-18", None], dtype="datetime64[ns]"
),
"test_timestamp": pd.Series(
[
"1970-01-01 00:00:01",
"2000-02-28 12:00:10",
"2038-01-18 23:59:59",
None,
],
dtype="datetime64[ns]",
),
"test_timestamptz": pd.Series(
[
"1970-01-01 00:00:01",
"2000-02-28 16:00:10",
"2038-01-18 15:59:59",
None,
],
dtype="datetime64[ns]",
),
"test_int16": pd.Series([0, 1, 2, 3], dtype="Int64"),
"test_int64": pd.Series(
[-9223372036854775808, 0, 9223372036854775807, None], dtype="Int64"
),
"test_float32": pd.Series(
[None, 3.1415926535, 2.71, -1e-37], dtype="float64"
),
"test_numeric": pd.Series([None, 521.34, 999.99, 0.00], dtype="float64"),
"test_bpchar": pd.Series(["a ", "bb ", "ccc ", None], dtype="object"),
"test_char": pd.Series(["a", "b", None, "d"], dtype="object"),
"test_varchar": pd.Series([None, "bb", "c", "defghijklm"], dtype="object"),
"test_uuid": pd.Series(
[
"86b494cc-96b2-11eb-9298-3e22fbb9fe9d",
"86b49b84-96b2-11eb-9298-3e22fbb9fe9d",
"86b49c42-96b2-11eb-9298-3e22fbb9fe9d",
None,
],
dtype="object",
),
"test_time": pd.Series(
["08:12:40", None, "23:00:10", "18:30:00"], dtype="object"
),
"test_json": pd.Series(
[
'{"customer":"John Doe","items":{"product":"Beer","qty":6}}',
'{"customer":"Lily Bush","items":{"product":"Diaper","qty":24}}',
'{"customer":"Josh William","items":{"product":"Toy Car","qty":1}}',
None,
],
dtype="object",
),
"test_jsonb": pd.Series(
[
'{"qty":6,"product":"Beer"}',
'{"qty":24,"product":"Diaper"}',
'{"qty":1,"product":"Toy Car"}',
None,
],
dtype="object",
),
"test_bytea": pd.Series(
[
None,
b"\xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xcc\x81\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9\xd1\x82\xd0\xb5",
b"",
b"\xf0\x9f\x98\x9c",
],
dtype="object",
),
"test_enum": pd.Series(
["happy", "very happy", "ecstatic", None], dtype="object"
),
"test_f4array": pd.Series(
[[], None, [123.123], [-1e-37, 1e37]], dtype="object"
),
"test_f8array": pd.Series(
[[], None, [1e-307, 1e308], [0.000234, -12.987654321]], dtype="object"
),
"test_narray": pd.Series(
[[], None, [521.34], [0.12, 333.33, 22.22]], dtype="object"
),
"test_i2array": pd.Series(
[[-1, 0, 1], [], [-32768, 32767], None], dtype="object"
),
"test_i4array": pd.Series(
[[-1, 0, 1123], [], [-2147483648, 2147483647], None], dtype="object"
),
"test_i8array": pd.Series(
[[-9223372036854775808, 9223372036854775807], [], [0], None],
dtype="object",
),
},
)
assert_frame_equal(df, expected, check_names=True)


def test_empty_result(postgres_url: str) -> None:
query = "SELECT * FROM test_table where test_int < -100"
df = read_sql(postgres_url, query)
Expand Down
5 changes: 4 additions & 1 deletion connectorx-python/src/pandas/transports/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use connectorx::{
impl_transport,
sources::postgres::{
BinaryProtocol, CSVProtocol, CursorProtocol, PostgresSource, PostgresTypeSystem,
BinaryProtocol, CSVProtocol, CursorProtocol, SimpleProtocol,
PostgresSource, PostgresTypeSystem,
},
typesystem::TypeConversion,
};
Expand Down Expand Up @@ -64,6 +65,8 @@ impl_postgres_transport!(CSVProtocol, NoTls);
impl_postgres_transport!(CSVProtocol, MakeTlsConnector);
impl_postgres_transport!(CursorProtocol, NoTls);
impl_postgres_transport!(CursorProtocol, MakeTlsConnector);
impl_postgres_transport!(SimpleProtocol, NoTls);
impl_postgres_transport!(SimpleProtocol, MakeTlsConnector);

impl<'py, P, C> TypeConversion<HashMap<String, Option<String>>, String>
for PostgresPandasTransport<'py, P, C>
Expand Down
30 changes: 30 additions & 0 deletions connectorx/src/get_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::sources::mysql::{BinaryProtocol as MySQLBinaryProtocol, TextProtocol}
#[cfg(feature = "src_postgres")]
use crate::sources::postgres::{
rewrite_tls_args, BinaryProtocol as PgBinaryProtocol, CSVProtocol, CursorProtocol,
SimpleProtocol,
};
use crate::{prelude::*, sql::CXQuery};
use fehler::{throw, throws};
Expand Down Expand Up @@ -114,6 +115,35 @@ pub fn get_arrow(
);
dispatcher.run()?;
}
("simple", Some(tls_conn)) => {
let sb = PostgresSource::<SimpleProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let dispatcher = Dispatcher::<
_,
_,
PostgresArrowTransport<SimpleProtocol, MakeTlsConnector>,
>::new(
sb, &mut destination, queries, origin_query
);
debug!("Running dispatcher");
dispatcher.run()?;
}
("simple", None) => {
let sb =
PostgresSource::<SimpleProtocol, NoTls>::new(config, NoTls, queries.len())?;
let dispatcher = Dispatcher::<
_,
_,
PostgresArrowTransport<SimpleProtocol, NoTls>,
>::new(
sb, &mut destination, queries, origin_query
);
debug!("Running dispatcher");
dispatcher.run()?;
}
_ => unimplemented!("{} protocol not supported", protocol),
}
}
Expand Down
31 changes: 31 additions & 0 deletions connectorx/src/get_arrow2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::sources::mysql::{BinaryProtocol as MySQLBinaryProtocol, TextProtocol}
#[cfg(feature = "src_postgres")]
use crate::sources::postgres::{
rewrite_tls_args, BinaryProtocol as PgBinaryProtocol, CSVProtocol, CursorProtocol,
SimpleProtocol,
};
use crate::{prelude::*, sql::CXQuery};
use fehler::{throw, throws};
Expand Down Expand Up @@ -114,6 +115,36 @@ pub fn get_arrow2(
);
dispatcher.run()?;
}
("simple", Some(tls_conn)) => {
let sb = PostgresSource::<SimpleProtocol, MakeTlsConnector>::new(
config,
tls_conn,
queries.len(),
)?;
let dispatcher = Dispatcher::<
_,
_,
PostgresArrowTransport<SimpleProtocol, MakeTlsConnector>,
Copy link
Contributor

@wangxiaoying wangxiaoying Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transport should be PostgresArrow2Transport, the same applies to the below one. This is the reason for the ci failure.

Copy link
Contributor Author

@alswang18 alswang18 Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it. code compiles now

>::new(
sb, &mut destination, queries, origin_query
);
debug!("Running dispatcher");
dispatcher.run()?;
}
("simple", None) => {
let sb =
PostgresSource::<SimpleProtocol, NoTls>::new(config, NoTls, queries.len())?;
let dispatcher = Dispatcher::<
_,
_,
PostgresArrowTransport<SimpleProtocol, NoTls>,
>::new(
sb, &mut destination, queries, origin_query
);
debug!("Running dispatcher");
dispatcher.run()?;
}

_ => unimplemented!("{} protocol not supported", protocol),
}
}
Expand Down
Loading