Skip to content

Commit 2488dc2

Browse files
committed
fix #633
1 parent a30c13b commit 2488dc2

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

connectorx-python/connectorx/tests/test_postgres.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,4 +1175,67 @@ def test_postgres_semicolon_list_queries(postgres_url: str) -> None:
11751175
},
11761176
)
11771177
df.sort_values(by="test_int", inplace=True, ignore_index=True)
1178+
assert_frame_equal(df, expected, check_names=True)
1179+
1180+
def test_postgres_partition_with_orderby(postgres_url: str) -> None:
1181+
query = "select * from test_table order by test_int"
1182+
df = read_sql(postgres_url, query=query, partition_on="test_int", partition_num=2)
1183+
1184+
expected = pd.DataFrame(
1185+
index=range(6),
1186+
data={
1187+
"test_int": pd.Series([0, 1, 2, 3, 4, 1314], dtype="Int64"),
1188+
"test_nullint": pd.Series([5, 3, None, 7, 9, 2], dtype="Int64"),
1189+
"test_str": pd.Series(
1190+
["a", "str1", "str2", "b", "c", None], dtype="object"
1191+
),
1192+
"test_float": pd.Series([3.1, None, 2.2, 3, 7.8, -10], dtype="float64"),
1193+
"test_bool": pd.Series(
1194+
[None, True, False, False, None, True], dtype="boolean"
1195+
),
1196+
},
1197+
)
1198+
df.sort_values(by="test_int", inplace=True, ignore_index=True)
1199+
assert_frame_equal(df, expected, check_names=True)
1200+
1201+
def test_postgres_partition_with_orderby_limit_asc(postgres_url: str) -> None:
1202+
query = "select * from test_table order by test_int asc limit 2"
1203+
df = read_sql(postgres_url, query=query, partition_on="test_int", partition_num=2)
1204+
1205+
expected = pd.DataFrame(
1206+
index=range(2),
1207+
data={
1208+
"test_int": pd.Series([0, 1], dtype="Int64"),
1209+
"test_nullint": pd.Series([5, 3], dtype="Int64"),
1210+
"test_str": pd.Series(
1211+
["a", "str1"], dtype="object"
1212+
),
1213+
"test_float": pd.Series([3.1, None], dtype="float64"),
1214+
"test_bool": pd.Series(
1215+
[None, True], dtype="boolean"
1216+
),
1217+
},
1218+
)
1219+
df.sort_values(by="test_int", inplace=True, ignore_index=True)
1220+
assert_frame_equal(df, expected, check_names=True)
1221+
1222+
def test_postgres_partition_with_orderby_limit_desc(postgres_url: str) -> None:
1223+
query = "select * from test_table order by test_int desc limit 2"
1224+
df = read_sql(postgres_url, query=query, partition_on="test_int", partition_num=2)
1225+
1226+
expected = pd.DataFrame(
1227+
index=range(2),
1228+
data={
1229+
"test_int": pd.Series([4, 1314], dtype="Int64"),
1230+
"test_nullint": pd.Series([9, 2], dtype="Int64"),
1231+
"test_str": pd.Series(
1232+
["c", None], dtype="object"
1233+
),
1234+
"test_float": pd.Series([7.8, -10], dtype="float64"),
1235+
"test_bool": pd.Series(
1236+
[None, True], dtype="boolean"
1237+
),
1238+
},
1239+
)
1240+
df.sort_values(by="test_int", inplace=True, ignore_index=True)
11781241
assert_frame_equal(df, expected, check_names=True)

connectorx/src/sql.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,10 @@ pub fn get_partition_range_query<T: Dialect>(sql: &str, col: &str, dialect: &T)
462462
.ok_or_else(|| ConnectorXError::SqlQueryNotSupported(sql.to_string()))?
463463
.clone();
464464
let ast_range: Statement;
465-
query.order_by = vec![];
465+
466+
if query.limit.is_none() && query.offset.is_none() {
467+
query.order_by = vec![]; // only omit orderby when there is no limit and offset in the query
468+
}
466469
let projection = vec![
467470
SelectItem::UnnamedExpr(Expr::Function(Function {
468471
name: ObjectName(vec![Ident {

0 commit comments

Comments
 (0)