@@ -1031,31 +1031,41 @@ def _reproject_to_table(self) -> OrderedIR:
1031
1031
1032
1032
def to_sql (
1033
1033
self ,
1034
- offset_column : typing .Optional [str ] = None ,
1035
1034
col_id_overrides : typing .Mapping [str , str ] = {},
1036
1035
sorted : bool = False ,
1037
1036
) -> str :
1038
- offsets_id = offset_column or ORDER_ID_COLUMN
1039
-
1040
1037
sql = ibis_bigquery .Backend ().compile (
1041
1038
self ._to_ibis_expr (
1042
- ordering_mode = "offset_col"
1043
- if (offset_column or sorted )
1044
- else "unordered" ,
1045
- order_col_name = offsets_id ,
1039
+ ordering_mode = "unordered" ,
1046
1040
col_id_overrides = col_id_overrides ,
1041
+ expose_hidden_cols = sorted ,
1047
1042
)
1048
1043
)
1049
1044
if sorted :
1045
+ ordering_columns = self ._ordering .all_ordering_columns
1046
+ except_cols = ", " .join (
1047
+ map (lambda ref : f"`{ ref .column_id } `" , ordering_columns )
1048
+ )
1049
+ order_by_clause = self ._ordering_clause (self ._ordering .all_ordering_columns )
1050
+
1050
1051
sql = textwrap .dedent (
1051
- f"SELECT * EXCEPT (` { offsets_id } ` )\n "
1052
+ f"SELECT * EXCEPT ({ except_cols } )\n "
1052
1053
"FROM (\n "
1053
1054
f"{ sql } \n "
1054
1055
")\n "
1055
- f"ORDER BY ` { offsets_id } ` \n "
1056
+ f"{ order_by_clause } \n "
1056
1057
)
1057
1058
return typing .cast (str , sql )
1058
1059
1060
+ def _ordering_clause (self , ordering : Iterable [OrderingColumnReference ]) -> str :
1061
+ parts = []
1062
+ for col_ref in ordering :
1063
+ asc_desc = "ASC" if col_ref .direction .is_ascending else "DESC"
1064
+ null_clause = "NULLS LAST" if col_ref .na_last else "NULLS FIRST"
1065
+ part = f"`{ col_ref .column_id } ` { asc_desc } { null_clause } "
1066
+ parts .append (part )
1067
+ return f"ORDER BY { ' ,' .join (parts )} "
1068
+
1059
1069
def _to_ibis_expr (
1060
1070
self ,
1061
1071
* ,
0 commit comments