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

Feat/add support for _varchar #487

Merged
Merged
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
42 changes: 42 additions & 0 deletions connectorx/src/destinations/arrow2/arrow_assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,45 @@ impl ArrowAssoc for Vec<u8> {
Field::new(header, ArrowDataType::LargeBinary, false)
}
}

impl ArrowAssoc for Option<Vec<String>> {
type Builder = MutableListArray<i64, MutableUtf8Array<i64>>;

fn builder(nrows: usize) -> Self::Builder {
MutableListArray::with_capacity(nrows)
}

fn push(builder: &mut Self::Builder, value: Self) {
let value = value.unwrap();
let mut string_array: Vec<Option<String>> = vec![];
for sub_value in value {
string_array.push(Some(sub_value))
}

builder.try_push(Some(string_array));
}

fn field(header: &str) -> Field {
Field::new(header, ArrowDataType::LargeUtf8, true)
}
}

impl ArrowAssoc for Vec<String> {
type Builder = MutableListArray<i64, MutableUtf8Array<i64>>;

fn builder(nrows: usize) -> Self::Builder {
MutableListArray::with_capacity(nrows)
}

fn push(builder: &mut Self::Builder, value: Self) {
let mut string_array: Vec<Option<String>> = vec![];
for sub_value in value {
string_array.push(Some(sub_value))
}
builder.try_push(Some(string_array));
}

fn field(header: &str) -> Field {
Field::new(header, ArrowDataType::LargeUtf8, false)
}
}
10 changes: 6 additions & 4 deletions connectorx/src/destinations/arrow2/typesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Arrow2TypeSystem {
UInt64Array(bool),
Float32Array(bool),
Float64Array(bool),
Utf8Array(bool),
}

impl_typesystem! {
Expand All @@ -42,9 +43,10 @@ impl_typesystem! {
{ DateTimeTz => DateTime<Utc> }
{ Int32Array => Vec<i32> }
{ Int64Array => Vec<i64> }
{ UInt32Array => Vec<u32> }
{ UInt64Array => Vec<u64> }
{ Float32Array => Vec<f32> }
{ Float64Array => Vec<f64> }
{ UInt32Array => Vec<u32> }
{ UInt64Array => Vec<u64> }
{ Float32Array => Vec<f32> }
{ Float64Array => Vec<f64> }
{ Utf8Array => Vec<String> }
}
}
6 changes: 4 additions & 2 deletions connectorx/src/sources/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ impl_produce!(
NaiveDate,
Uuid,
Value,
Vec<String>,
);

impl<'r, 'a> Produce<'r, HashMap<String, Option<String>>>
Expand Down Expand Up @@ -652,7 +653,7 @@ macro_rules! impl_csv_vec_produce {
};
}

impl_csv_vec_produce!(i8, i16, i32, i64, f32, f64, Decimal,);
impl_csv_vec_produce!(i8, i16, i32, i64, f32, f64, Decimal, String,);

impl<'r, 'a> Produce<'r, HashMap<String, Option<String>>> for PostgresCSVSourceParser<'a> {
type Error = PostgresSourceError;
Expand Down Expand Up @@ -1015,6 +1016,7 @@ impl_produce!(
Uuid,
Value,
HashMap<String, Option<String>>,
Vec<String>,
);

impl<C> SourcePartition for PostgresSourcePartition<SimpleProtocol, C>
Expand Down Expand Up @@ -1344,7 +1346,7 @@ macro_rules! impl_simple_vec_produce {
)+
};
}
impl_simple_vec_produce!(i16, i32, i64, f32, f64, Decimal,);
impl_simple_vec_produce!(i16, i32, i64, f32, f64, Decimal, String,);

impl<'r> Produce<'r, NaiveDate> for PostgresSimpleSourceParser {
type Error = PostgresSourceError;
Expand Down
3 changes: 3 additions & 0 deletions connectorx/src/sources/postgres/typesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum PostgresTypeSystem {
Int2Array(bool),
Int4Array(bool),
Int8Array(bool),
VarcharArray(bool),
Date(bool),
Char(bool),
BpChar(bool),
Expand Down Expand Up @@ -51,6 +52,7 @@ impl_typesystem! {
{ Float4Array => Vec<f32> }
{ Float8Array => Vec<f64> }
{ NumericArray => Vec<Decimal> }
{ VarcharArray => Vec<String>}
{ Bool => bool }
{ Char => i8 }
{ Text | BpChar | VarChar | Enum => &'r str }
Expand Down Expand Up @@ -81,6 +83,7 @@ impl<'a> From<&'a Type> for PostgresTypeSystem {
"_float4" => Float4Array(true),
"_float8" => Float8Array(true),
"_numeric" => NumericArray(true),
"_varchar" => VarcharArray(true),
"bool" => Bool(true),
"char" => Char(true),
"text" | "citext" | "ltree" | "lquery" | "ltxtquery" => Text(true),
Expand Down
1 change: 1 addition & 0 deletions connectorx/src/transports/postgres_arrow2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ macro_rules! impl_postgres_transport {
{ Float4Array[Vec<f32>] => Float64Array[Vec<f64>] | conversion auto_vec }
{ Float8Array[Vec<f64>] => Float64Array[Vec<f64>] | conversion auto }
{ NumericArray[Vec<Decimal>] => Float64Array[Vec<f64>] | conversion option }
{ VarcharArray[Vec<String>] => Utf8Array[Vec<String>] | conversion auto_vec }
}
);
}
Expand Down
44 changes: 44 additions & 0 deletions connectorx/tests/test_polars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,47 @@ fn test_postgres_arrow() {

assert!(df.frame_equal_missing(&expected) || df.frame_equal_missing(&expected2));
}

#[test]

fn test_pg_pl_varchar_array() {
let _ = env_logger::builder().is_test(true).try_init();

let dburl = env::var("POSTGRES_URL").unwrap();

let queries = [CXQuery::naked("select test_varchararray from test_types")];
let url = Url::parse(dburl.as_str()).unwrap();
let (config, _tls) = rewrite_tls_args(&url).unwrap();
let builder = PostgresSource::<BinaryProtocol, NoTls>::new(config, NoTls, 2).unwrap();
let mut destination = Arrow2Destination::new();
let dispatcher = Dispatcher::<_, _, PostgresArrow2Transport<BinaryProtocol, NoTls>>::new(
builder,
&mut destination,
&queries,
Some(format!("select * from test_types")),
);

dispatcher.run().expect("run dispatcher");

let s1 = Series::new("a", ["str1", "str2"]);
let s2 = Series::new(
"b",
[
"0123456789",
"abcdefghijklmnopqrstuvwxyz",
"!@#$%^&*()_-+=~`:;<>?/",
],
);
let s3 = Series::new("c", ["", " "]);
let empty_vec: Vec<&str> = vec![];
let s4 = Series::new("d", empty_vec);

let df: DataFrame = destination.polars().unwrap();
let test_df: DataFrame = df!(
"test_varchararray" => &[s1,s2,s3,s4]
)
.unwrap();

println!("{:?}", df);
assert_eq!(df, test_df);
}
11 changes: 6 additions & 5 deletions scripts/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ CREATE TABLE IF NOT EXISTS test_types(
test_citext CITEXT,
test_ltree ltree,
test_lquery lquery,
test_ltxtquery ltxtquery
test_ltxtquery ltxtquery,
test_varchararray VARCHAR[]
);

INSERT INTO test_types VALUES ('1970-01-01', '1970-01-01 00:00:01', '1970-01-01 00:00:01-00', 0, -9223372036854775808, NULL, NULL, 'a', 'a', NULL, '86b494cc-96b2-11eb-9298-3e22fbb9fe9d', '08:12:40', '1 year 2 months 3 days', '{"customer": "John Doe", "items": {"product": "Beer","qty": 6}}', '{"product": "Beer","qty": 6}', NULL, 'happy','{}', '{}', '{}', '{-1, 0, 1}', '{-1, 0, 1123}', '{-9223372036854775808, 9223372036854775807}', 'str_citext', 'A.B.C.D', '*.B.*', 'A & B*');
INSERT INTO test_types VALUES ('2000-02-28', '2000-02-28 12:00:10', '2000-02-28 12:00:10-04', 1, 0, 3.1415926535, 521.34, 'bb', 'b', 'bb', '86b49b84-96b2-11eb-9298-3e22fbb9fe9d', NULL, '2 weeks ago', '{"customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}', '{"product": "Diaper","qty": 24}', 'Здра́вствуйте', 'very happy', NULL, NULL, NULL, '{}', '{}', '{}', '', 'A.B.E', 'A.*', 'A | B');
INSERT INTO test_types VALUES ('2038-01-18', '2038-01-18 23:59:59', '2038-01-18 23:59:59+08', 2, 9223372036854775807, 2.71, '1e-130', 'ccc', NULL, 'c', '86b49c42-96b2-11eb-9298-3e22fbb9fe9d', '23:00:10', '3 months 2 days ago', '{"customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}', '{"product": "Toy Car","qty": 1}', '', 'ecstatic', '{123.123}', '{-1e-307, 1e308}', '{521.34}', '{-32768, 32767}', '{-2147483648, 2147483647}', '{0}', 's', 'A', '*', 'A@');
INSERT INTO test_types VALUES (NULL, NULL, NULL, 3, NULL, 0.00, -1e-37, NULL, 'd', 'defghijklm', NULL, '18:30:00', '3 year', NULL, NULL, '😜', NULL, '{-1e-37, 1e37}', '{0.000234, -12.987654321}', '{0.12, 333.33, 22.22}', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO test_types VALUES ('1970-01-01', '1970-01-01 00:00:01', '1970-01-01 00:00:01-00', 0, -9223372036854775808, NULL, NULL, 'a', 'a', NULL, '86b494cc-96b2-11eb-9298-3e22fbb9fe9d', '08:12:40', '1 year 2 months 3 days', '{"customer": "John Doe", "items": {"product": "Beer","qty": 6}}', '{"product": "Beer","qty": 6}', NULL, 'happy','{}', '{}', '{}', '{-1, 0, 1}', '{-1, 0, 1123}', '{-9223372036854775808, 9223372036854775807}', 'str_citext', 'A.B.C.D', '*.B.*', 'A & B*',ARRAY['str1','str2']);
INSERT INTO test_types VALUES ('2000-02-28', '2000-02-28 12:00:10', '2000-02-28 12:00:10-04', 1, 0, 3.1415926535, 521.34, 'bb', 'b', 'bb', '86b49b84-96b2-11eb-9298-3e22fbb9fe9d', NULL, '2 weeks ago', '{"customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}', '{"product": "Diaper","qty": 24}', 'Здра́вствуйте', 'very happy', NULL, NULL, NULL, '{}', '{}', '{}', '', 'A.B.E', 'A.*', 'A | B','{"0123456789","abcdefghijklmnopqrstuvwxyz","!@#$%^&*()_-+=~`:;<>?/"}');
INSERT INTO test_types VALUES ('2038-01-18', '2038-01-18 23:59:59', '2038-01-18 23:59:59+08', 2, 9223372036854775807, 2.71, '1e-130', 'ccc', NULL, 'c', '86b49c42-96b2-11eb-9298-3e22fbb9fe9d', '23:00:10', '3 months 2 days ago', '{"customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}', '{"product": "Toy Car","qty": 1}', '', 'ecstatic', '{123.123}', '{-1e-307, 1e308}', '{521.34}', '{-32768, 32767}', '{-2147483648, 2147483647}', '{0}', 's', 'A', '*', 'A@',ARRAY['',' ']);
INSERT INTO test_types VALUES (NULL, NULL, NULL, 3, NULL, 0.00, -1e-37, NULL, 'd', 'defghijklm', NULL, '18:30:00', '3 year', NULL, NULL, '😜', NULL, '{-1e-37, 1e37}', '{0.000234, -12.987654321}', '{0.12, 333.33, 22.22}', NULL, NULL, NULL, NULL, NULL, NULL, NULL,'{}');

CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
BEGIN
Expand Down