| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.result; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.Comparator; |
| 11 |
|
| 12 |
import org.h2.command.query.QueryOrderBy; |
| 13 |
import org.h2.engine.Database; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.expression.ExpressionColumn; |
| 17 |
import org.h2.mode.DefaultNullOrdering; |
| 18 |
import org.h2.table.Column; |
| 19 |
import org.h2.table.TableFilter; |
| 20 |
import org.h2.util.Utils; |
| 21 |
import org.h2.value.Value; |
| 22 |
import org.h2.value.ValueNull; |
| 23 |
import org.h2.value.ValueRow; |
| 24 |
|
| 25 |
/** |
| 26 |
* A sort order represents an ORDER BY clause in a query. |
| 27 |
*/ |
| 28 |
public final class SortOrder implements Comparator<Value[]> { |
| 29 |
|
| 30 |
/** |
| 31 |
* This bit mask means the values should be sorted in ascending order. |
| 32 |
*/ |
| 33 |
public static final int ASCENDING = 0; |
| 34 |
|
| 35 |
/** |
| 36 |
* This bit mask means the values should be sorted in descending order. |
| 37 |
*/ |
| 38 |
public static final int DESCENDING = 1; |
| 39 |
|
| 40 |
/** |
| 41 |
* This bit mask means NULLs should be sorted before other data, no matter |
| 42 |
* if ascending or descending order is used. |
| 43 |
*/ |
| 44 |
public static final int NULLS_FIRST = 2; |
| 45 |
|
| 46 |
/** |
| 47 |
* This bit mask means NULLs should be sorted after other data, no matter |
| 48 |
* if ascending or descending order is used. |
| 49 |
*/ |
| 50 |
public static final int NULLS_LAST = 4; |
| 51 |
|
| 52 |
private final SessionLocal session; |
| 53 |
|
| 54 |
/** |
| 55 |
* The column indexes of the order by expressions within the query. |
| 56 |
*/ |
| 57 |
private final int[] queryColumnIndexes; |
| 58 |
|
| 59 |
/** |
| 60 |
* The sort type bit mask (DESCENDING, NULLS_FIRST, NULLS_LAST). |
| 61 |
*/ |
| 62 |
private final int[] sortTypes; |
| 63 |
|
| 64 |
/** |
| 65 |
* The order list. |
| 66 |
*/ |
| 67 |
private final ArrayList<QueryOrderBy> orderList; |
| 68 |
|
| 69 |
/** |
| 70 |
* Construct a new sort order object with default sort directions. |
| 71 |
* |
| 72 |
* @param session the session |
| 73 |
* @param queryColumnIndexes the column index list |
| 74 |
*/ |
| 75 |
public SortOrder(SessionLocal session, int[] queryColumnIndexes) { |
| 76 |
this (session, queryColumnIndexes, new int[queryColumnIndexes.length], null); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Construct a new sort order object. |
| 81 |
* |
| 82 |
* @param session the session |
| 83 |
* @param queryColumnIndexes the column index list |
| 84 |
* @param sortType the sort order bit masks |
| 85 |
* @param orderList the original query order list (if this is a query) |
| 86 |
*/ |
| 87 |
public SortOrder(SessionLocal session, int[] queryColumnIndexes, int[] sortType, |
| 88 |
ArrayList<QueryOrderBy> orderList) { |
| 89 |
this.session = session; |
| 90 |
this.queryColumnIndexes = queryColumnIndexes; |
| 91 |
this.sortTypes = sortType; |
| 92 |
this.orderList = orderList; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Create the SQL snippet that describes this sort order. |
| 97 |
* This is the SQL snippet that usually appears after the ORDER BY clause. |
| 98 |
* |
| 99 |
* @param builder string builder to append to |
| 100 |
* @param list the expression list |
| 101 |
* @param visible the number of columns in the select list |
| 102 |
* @param sqlFlags formatting flags |
| 103 |
* @return the specified string builder |
| 104 |
*/ |
| 105 |
public StringBuilder getSQL(StringBuilder builder, Expression[] list, int visible, int sqlFlags) { |
| 106 |
int i = 0; |
| 107 |
for (int idx : queryColumnIndexes) { |
| 108 |
if (i > 0) { |
| 109 |
builder.append(", "); |
| 110 |
} |
| 111 |
if (idx < visible) { |
| 112 |
builder.append(idx + 1); |
| 113 |
} else { |
| 114 |
list[idx].getUnenclosedSQL(builder, sqlFlags); |
| 115 |
} |
| 116 |
typeToString(builder, sortTypes[i++]); |
| 117 |
} |
| 118 |
return builder; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Appends type information (DESC, NULLS FIRST, NULLS LAST) to the specified statement builder. |
| 123 |
* @param builder string builder |
| 124 |
* @param type sort type |
| 125 |
*/ |
| 126 |
public static void typeToString(StringBuilder builder, int type) { |
| 127 |
if ((type & DESCENDING) != 0) { |
| 128 |
builder.append(" DESC"); |
| 129 |
} |
| 130 |
if ((type & NULLS_FIRST) != 0) { |
| 131 |
builder.append(" NULLS FIRST"); |
| 132 |
} else if ((type & NULLS_LAST) != 0) { |
| 133 |
builder.append(" NULLS LAST"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Compare two expression lists. |
| 139 |
* |
| 140 |
* @param a the first expression list |
| 141 |
* @param b the second expression list |
| 142 |
* @return the result of the comparison |
| 143 |
*/ |
| 144 |
@Override |
| 145 |
public int compare(Value[] a, Value[] b) { |
| 146 |
for (int i = 0, len = queryColumnIndexes.length; i < len; i++) { |
| 147 |
int idx = queryColumnIndexes[i]; |
| 148 |
int type = sortTypes[i]; |
| 149 |
Value ao = a[idx]; |
| 150 |
Value bo = b[idx]; |
| 151 |
boolean aNull = ao == ValueNull.INSTANCE, bNull = bo == ValueNull.INSTANCE; |
| 152 |
if (aNull || bNull) { |
| 153 |
if (aNull == bNull) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
return session.getDatabase().getDefaultNullOrdering().compareNull(aNull, type); |
| 157 |
} |
| 158 |
int comp = session.compare(ao, bo); |
| 159 |
if (comp != 0) { |
| 160 |
return (type & DESCENDING) == 0 ? comp : -comp; |
| 161 |
} |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Sort a list of rows. |
| 168 |
* |
| 169 |
* @param rows the list of rows |
| 170 |
*/ |
| 171 |
public void sort(ArrayList<Value[]> rows) { |
| 172 |
rows.sort(this); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Sort a list of rows using offset and limit. |
| 177 |
* |
| 178 |
* @param rows the list of rows |
| 179 |
* @param fromInclusive the start index, inclusive |
| 180 |
* @param toExclusive the end index, exclusive |
| 181 |
*/ |
| 182 |
public void sort(ArrayList<Value[]> rows, int fromInclusive, int toExclusive) { |
| 183 |
if (toExclusive == 1 && fromInclusive == 0) { |
| 184 |
rows.set(0, Collections.min(rows, this)); |
| 185 |
return; |
| 186 |
} |
| 187 |
Value[][] arr = rows.toArray(new Value[0][]); |
| 188 |
Utils.sortTopN(arr, fromInclusive, toExclusive, this); |
| 189 |
for (int i = fromInclusive; i < toExclusive; i++) { |
| 190 |
rows.set(i, arr[i]); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get the column index list. This is the column indexes of the order by |
| 196 |
* expressions within the query. |
| 197 |
* <p> |
| 198 |
* For the query "select name, id from test order by id, name" this is {1, |
| 199 |
* 0} as the first order by expression (the column "id") is the second |
| 200 |
* column of the query, and the second order by expression ("name") is the |
| 201 |
* first column of the query. |
| 202 |
* |
| 203 |
* @return the list |
| 204 |
*/ |
| 205 |
public int[] getQueryColumnIndexes() { |
| 206 |
return queryColumnIndexes; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get the column for the given table filter, if the sort column is for this |
| 211 |
* filter. |
| 212 |
* |
| 213 |
* @param index the column index (0, 1,..) |
| 214 |
* @param filter the table filter |
| 215 |
* @return the column, or null |
| 216 |
*/ |
| 217 |
public Column getColumn(int index, TableFilter filter) { |
| 218 |
if (orderList == null) { |
| 219 |
return null; |
| 220 |
} |
| 221 |
QueryOrderBy order = orderList.get(index); |
| 222 |
Expression expr = order.expression; |
| 223 |
if (expr == null) { |
| 224 |
return null; |
| 225 |
} |
| 226 |
expr = expr.getNonAliasExpression(); |
| 227 |
if (expr.isConstant()) { |
| 228 |
return null; |
| 229 |
} |
| 230 |
if (!(expr instanceof ExpressionColumn)) { |
| 231 |
return null; |
| 232 |
} |
| 233 |
ExpressionColumn exprCol = (ExpressionColumn) expr; |
| 234 |
if (exprCol.getTableFilter() != filter) { |
| 235 |
return null; |
| 236 |
} |
| 237 |
return exprCol.getColumn(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get the sort order bit masks. |
| 242 |
* |
| 243 |
* @return the list |
| 244 |
*/ |
| 245 |
public int[] getSortTypes() { |
| 246 |
return sortTypes; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the original query order list. |
| 251 |
* |
| 252 |
* @return the original query order list |
| 253 |
*/ |
| 254 |
public ArrayList<QueryOrderBy> getOrderList() { |
| 255 |
return orderList; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Returns sort order bit masks with {@link SortOrder#NULLS_FIRST} or |
| 260 |
* {@link SortOrder#NULLS_LAST} explicitly set. |
| 261 |
* |
| 262 |
* @return bit masks with either {@link SortOrder#NULLS_FIRST} or {@link SortOrder#NULLS_LAST} |
| 263 |
* explicitly set. |
| 264 |
*/ |
| 265 |
public int[] getSortTypesWithNullOrdering() { |
| 266 |
return addNullOrdering(session.getDatabase(), sortTypes.clone()); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Add explicit {@link SortOrder#NULLS_FIRST} or {@link SortOrder#NULLS_LAST} where they |
| 271 |
* aren't already specified. |
| 272 |
* |
| 273 |
* @param database |
| 274 |
* the database |
| 275 |
* @param sortTypes |
| 276 |
* bit masks |
| 277 |
* @return the specified array with possibly modified bit masks |
| 278 |
*/ |
| 279 |
public static int[] addNullOrdering(Database database, int[] sortTypes) { |
| 280 |
DefaultNullOrdering defaultNullOrdering = database.getDefaultNullOrdering(); |
| 281 |
for (int i = 0, length = sortTypes.length; i < length; i++) { |
| 282 |
sortTypes[i] = defaultNullOrdering.addExplicitNullOrdering(sortTypes[i]); |
| 283 |
} |
| 284 |
return sortTypes; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Returns comparator for row values. |
| 289 |
* |
| 290 |
* @return comparator for row values. |
| 291 |
*/ |
| 292 |
public Comparator<Value> getRowValueComparator() { |
| 293 |
return (o1, o2) -> compare(((ValueRow) o1).getList(), ((ValueRow) o2).getList()); |
| 294 |
} |
| 295 |
|
| 296 |
} |