Skip to content

Commit 320b510

Browse files
max-ibragimowMax Ibragimov
andauthored
[PMM-7951] Added NULLS LAST sorting for view-data mode (#52)
Co-authored-by: Max Ibragimov <maxim.ibragimov@tantorlabs.ru>
1 parent 78380e9 commit 320b510

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

pg_anon/common/db_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def get_fields_list(connection_params: ConnectionParams, table_schema: str
9898
db_conn = await create_connection(connection_params, server_settings=server_settings)
9999
fields_list = await db_conn.fetch(
100100
"""
101-
SELECT column_name, udt_name FROM information_schema.columns
101+
SELECT column_name, udt_name, is_nullable FROM information_schema.columns
102102
WHERE table_schema = '%s' AND table_name='%s'
103103
ORDER BY ordinal_position ASC
104104
"""

pg_anon/common/utils.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,20 @@ def get_dict_rule_for_table(dictionary_rules: List[Dict], schema: str, table: st
185185
return result
186186

187187

188-
async def get_dump_query(ctx, table_schema: str, table_name: str, table_rule,
189-
files: Dict, excluded_objs: List, included_objs: List):
188+
async def get_dump_query(ctx, table_schema: str, table_name: str, table_rule, files: Dict,
189+
excluded_objs: List, included_objs: List, nulls_last: bool = False):
190190

191191
table_name_full = f'"{table_schema}"."{table_name}"'
192192

193-
found_white_list = table_rule is not None
194-
195193
# dictionary_exclude has the highest priority
196-
if ctx.prepared_dictionary_obj.get("dictionary_exclude"):
194+
if "dictionary_exclude" in ctx.prepared_dictionary_obj:
197195
exclude_rule = get_dict_rule_for_table(
198196
dictionary_rules=ctx.prepared_dictionary_obj["dictionary_exclude"],
199197
schema=table_schema,
200198
table=table_name,
201199
)
202-
found = exclude_rule is not None
203-
if found and not found_white_list:
200+
201+
if exclude_rule is not None and table_rule is None:
204202
excluded_objs.append(
205203
[
206204
exclude_rule,
@@ -218,7 +216,7 @@ async def get_dump_query(ctx, table_schema: str, table_name: str, table_rule,
218216

219217
files[f"{hashed_name}.bin.gz"] = {"schema": table_schema, "table": table_name}
220218

221-
if not found_white_list:
219+
if table_rule is None:
222220
included_objs.append(
223221
[table_rule, table_schema, table_name, "if not found_white_list"]
224222
)
@@ -286,6 +284,13 @@ def _check_field(_field_name: str):
286284
or ctx.args.dbg_stage_3_validate_full):
287285
query += f" {ctx.validate_limit}"
288286

287+
if nulls_last:
288+
ordering = ", ".join([
289+
field["column_name"] + ' NULLS LAST' for field in fields_list
290+
if field["is_nullable"].lower() == "yes"
291+
])
292+
query += f" ORDER BY {ordering}"
293+
289294
return query
290295

291296

pg_anon/modes/view_data.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ async def _prepare_queries(self):
132132
table_rule=self.table_rule,
133133
files=files,
134134
included_objs=included_objs,
135-
excluded_objs=excluded_objs
135+
excluded_objs=excluded_objs,
136+
nulls_last=True
136137
)
137138
self.query = query_without_limit + f" LIMIT {self._limit} OFFSET {self._offset}"
138139

@@ -144,7 +145,8 @@ async def _prepare_queries(self):
144145
table_rule=None,
145146
files=files,
146147
included_objs=included_objs,
147-
excluded_objs=excluded_objs
148+
excluded_objs=excluded_objs,
149+
nulls_last=True
148150
)
149151
self.raw_query = query_without_limit + f" LIMIT {self._limit} OFFSET {self._offset}"
150152

0 commit comments

Comments
 (0)