in core/src/main/java/com/jetbrains/youtrackdb/internal/core/sql/parser/SQLOrderByItem.java [91:199]
public int compare(Result a, Result b, CommandContext ctx) {
Object aVal = null;
Object bVal = null;
if (rid != null) {
throw new UnsupportedOperationException("ORDER BY " + rid + " is not supported yet");
}
var result = 0;
if (recordAttr != null) {
aVal = a.getProperty(recordAttr);
bVal = b.getProperty(recordAttr);
} else if (alias != null) {
if (isEdge) {
var aElement = (Vertex) a.asEntityOrNull();
var aIter =
aElement != null ? aElement.getVertices(Direction.OUT, alias).iterator() : null;
aVal = (aIter != null && aIter.hasNext()) ? aIter.next() : null;
var bElement = (Vertex) b.asEntityOrNull();
var bIter =
bElement != null ? bElement.getVertices(Direction.OUT, alias).iterator() : null;
bVal = (bIter != null && bIter.hasNext()) ? bIter.next() : null;
} else {
if (a.hasProperty(alias)) {
aVal = a.getProperty(alias);
}
if (b.hasProperty(alias)) {
bVal = b.getProperty(alias);
}
}
}
if (aVal == null && bVal == null) {
aVal = ((ResultInternal) a).getMetadata(alias);
bVal = ((ResultInternal) b).getMetadata(alias);
}
if (modifier != null) {
aVal = modifier.execute(a, aVal, ctx);
bVal = modifier.execute(b, bVal, ctx);
}
if (collate != null && collateStrategy == null) {
var collateVal = collate.execute(new ResultInternal(ctx.getDatabaseSession()), ctx);
if (collateVal == null) {
collateVal = collate.toString();
if (collateVal.equals("null")) {
collateVal = null;
}
}
if (collateVal != null) {
collateStrategy = SQLEngine.getCollate(String.valueOf(collateVal));
if (collateStrategy == null) {
collateStrategy =
SQLEngine.getCollate(String.valueOf(collateVal).toUpperCase(Locale.ENGLISH));
}
if (collateStrategy == null) {
collateStrategy =
SQLEngine.getCollate(String.valueOf(collateVal).toLowerCase(Locale.ENGLISH));
}
if (collateStrategy == null) {
throw new CommandExecutionException(ctx.getDatabaseSession(),
"Invalid collate for ORDER BY: " + collateVal);
}
}
}
if (collateStrategy != null) {
result = collateStrategy.compareForOrderBy(aVal, bVal);
} else {
if (aVal == null) {
if (bVal == null) {
result = 0;
} else {
result = -1;
}
} else if (bVal == null) {
result = 1;
} else if (aVal instanceof String && bVal instanceof String) {
var internal = ctx.getDatabaseSession();
if (stringCollator == null) {
var language = (String) internal.get(DatabaseSession.ATTRIBUTES.LOCALE_LANGUAGE);
var country = (String) internal.get(DatabaseSession.ATTRIBUTES.LOCALE_COUNTRY);
Locale locale;
if (language != null) {
if (country != null) {
locale = new Locale(language, country);
} else {
locale = new Locale(language);
}
} else {
locale = Locale.getDefault();
}
stringCollator = Collator.getInstance(locale);
}
result = stringCollator.compare(aVal, bVal);
} else if (aVal instanceof Comparable && bVal instanceof Comparable) {
try {
result = ((Comparable) aVal).compareTo(bVal);
} catch (Exception e) {
LogManager.instance().error(this, "Error during comparision", e);
result = 0;
}
}
}
if (type == DESC) {
result = -1 * result;
}
return result;
}