提交 d198e275 authored 作者: Andrei Tokar's avatar Andrei Tokar

Streamline Value comparison: eliminate redundant attempts for type conversions

上级 006fe1b0
...@@ -271,15 +271,19 @@ public class Comparison extends Condition { ...@@ -271,15 +271,19 @@ public class Comparison extends Condition {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
} }
int dataType = Value.getHigherOrder(left.getType(), right.getType()); int leftType = left.getType();
if (dataType == Value.ENUM) { int rightType = right.getType();
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r); if (leftType != rightType || leftType == Value.ENUM) {
l = l.convertToEnum(enumerators); int dataType = Value.getHigherOrder(leftType, rightType);
r = r.convertToEnum(enumerators); if (dataType == Value.ENUM) {
} else { String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r);
Mode mode = database.getMode(); l = l.convertToEnum(enumerators);
l = l.convertTo(dataType, -1, mode); r = r.convertToEnum(enumerators);
r = r.convertTo(dataType, -1, mode); } else {
Mode mode = database.getMode();
l = l.convertTo(dataType, -1, mode);
r = r.convertTo(dataType, -1, mode);
}
} }
boolean result = compareNotNull(database, l, r, compareType); boolean result = compareNotNull(database, l, r, compareType);
return ValueBoolean.get(result); return ValueBoolean.get(result);
......
...@@ -142,29 +142,23 @@ public class ValueDataType implements DataType { ...@@ -142,29 +142,23 @@ public class ValueDataType implements DataType {
if (a == b) { if (a == b) {
return 0; return 0;
} }
// null is never stored;
// comparison with null is used to retrieve all entries
// in which case null is always lower than all entries
// (even for descending ordered indexes)
if (a == null) {
return -1;
} else if (b == null) {
return 1;
}
boolean aNull = a == ValueNull.INSTANCE; boolean aNull = a == ValueNull.INSTANCE;
boolean bNull = b == ValueNull.INSTANCE; if (aNull || b == ValueNull.INSTANCE) {
if (aNull || bNull) {
return SortOrder.compareNull(aNull, sortType); return SortOrder.compareNull(aNull, sortType);
} }
int t2 = Value.getHigherOrder(a.getType(), b.getType()); int aType = a.getType();
if (t2 == Value.ENUM) { int bType = b.getType();
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b); if (aType != bType || aType == Value.ENUM) {
a = a.convertToEnum(enumerators); int t2 = Value.getHigherOrder(aType, bType);
b = b.convertToEnum(enumerators); if (t2 == Value.ENUM) {
} else { String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
a = a.convertTo(t2, -1, mode); a = a.convertToEnum(enumerators);
b = b.convertTo(t2, -1, mode); b = b.convertToEnum(enumerators);
} else {
a = a.convertTo(t2, -1, mode);
b = b.convertTo(t2, -1, mode);
}
} }
int comp = a.compareTypeSafe(b, compareMode); int comp = a.compareTypeSafe(b, compareMode);
......
...@@ -1213,15 +1213,19 @@ public abstract class Table extends SchemaObjectBase { ...@@ -1213,15 +1213,19 @@ public abstract class Table extends SchemaObjectBase {
if (a == b) { if (a == b) {
return 0; return 0;
} }
int dataType = Value.getHigherOrder(a.getType(), b.getType()); int aType = a.getType();
if (dataType == Value.ENUM) { int bType = b.getType();
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b); if (aType != bType || aType == Value.ENUM) {
a = a.convertToEnum(enumerators); int dataType = Value.getHigherOrder(aType, bType);
b = b.convertToEnum(enumerators); if (dataType == Value.ENUM) {
} else { String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
Mode mode = database.getMode(); a = a.convertToEnum(enumerators);
a = a.convertTo(dataType, -1, mode); b = b.convertToEnum(enumerators);
b = b.convertTo(dataType, -1, mode); } else {
Mode mode = database.getMode();
a = a.convertTo(dataType, -1, mode);
b = b.convertTo(dataType, -1, mode);
}
} }
return a.compareTypeSafe(b, compareMode); return a.compareTypeSafe(b, compareMode);
} }
......
...@@ -1161,13 +1161,6 @@ public abstract class Value { ...@@ -1161,13 +1161,6 @@ public abstract class Value {
* 1 otherwise * 1 otherwise
*/ */
public final int compareTypeSafe(Value v, CompareMode mode) { public final int compareTypeSafe(Value v, CompareMode mode) {
if (this == v) {
return 0;
} else if (this == ValueNull.INSTANCE) {
return -1;
} else if (v == ValueNull.INSTANCE) {
return 1;
}
return compareSecure(v, mode); return compareSecure(v, mode);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论