File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -103,11 +103,7 @@ def visit_Arel_Nodes_SelectStatement_SQLServer_Lock collector, options = {}
103103 end
104104
105105 def visit_Orders_And_Let_Fetch_Happen o , collector
106- if ( o . limit || o . offset ) && o . orders . empty?
107- table = table_From_Statement o
108- column = primary_Key_From_Table ( table )
109- o . orders = [ column . asc ]
110- end
106+ make_Fetch_Possible_And_Deterministic o
111107 unless o . orders . empty?
112108 collector << SPACE
113109 collector << ORDER_BY
@@ -141,6 +137,17 @@ def select_statement_lock?
141137 @select_statement && @select_statement . lock
142138 end
143139
140+ def make_Fetch_Possible_And_Deterministic o
141+ return if o . limit . nil? && o . offset . nil?
142+ t = table_From_Statement o
143+ pk = primary_Key_From_Table t
144+ return unless pk
145+ if o . orders . empty?
146+ # Prefer deterministic vs a simple `(SELECT NULL)` expr.
147+ o . orders = [ pk . asc ]
148+ end
149+ end
150+
144151 def table_From_Statement o
145152 core = o . cores . first
146153 if Arel ::Table === core . from
@@ -153,6 +160,7 @@ def table_From_Statement o
153160 end
154161
155162 def primary_Key_From_Table t
163+ return unless t
156164 return t . primary_key if t . primary_key
157165 if engine_pk = t . engine . primary_key
158166 pk = t . engine . arel_table [ engine_pk ]
You can’t perform that action at this time.
0 commit comments