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

Streamline Value comparison: eliminate redundant attempts for type conversions

上级 006fe1b0
...@@ -271,7 +271,10 @@ public class Comparison extends Condition { ...@@ -271,7 +271,10 @@ public class Comparison extends Condition {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
} }
int dataType = Value.getHigherOrder(left.getType(), right.getType()); int leftType = left.getType();
int rightType = right.getType();
if (leftType != rightType || leftType == Value.ENUM) {
int dataType = Value.getHigherOrder(leftType, rightType);
if (dataType == Value.ENUM) { if (dataType == Value.ENUM) {
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r); String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r);
l = l.convertToEnum(enumerators); l = l.convertToEnum(enumerators);
...@@ -281,6 +284,7 @@ public class Comparison extends Condition { ...@@ -281,6 +284,7 @@ public class Comparison extends Condition {
l = l.convertTo(dataType, -1, mode); l = l.convertTo(dataType, -1, mode);
r = r.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,22 +142,15 @@ public class ValueDataType implements DataType { ...@@ -142,22 +142,15 @@ 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();
int bType = b.getType();
if (aType != bType || aType == Value.ENUM) {
int t2 = Value.getHigherOrder(aType, bType);
if (t2 == Value.ENUM) { if (t2 == Value.ENUM) {
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b); String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
a = a.convertToEnum(enumerators); a = a.convertToEnum(enumerators);
...@@ -166,6 +159,7 @@ public class ValueDataType implements DataType { ...@@ -166,6 +159,7 @@ public class ValueDataType implements DataType {
a = a.convertTo(t2, -1, mode); a = a.convertTo(t2, -1, mode);
b = b.convertTo(t2, -1, mode); b = b.convertTo(t2, -1, mode);
} }
}
int comp = a.compareTypeSafe(b, compareMode); int comp = a.compareTypeSafe(b, compareMode);
if ((sortType & SortOrder.DESCENDING) != 0) { if ((sortType & SortOrder.DESCENDING) != 0) {
......
...@@ -1213,7 +1213,10 @@ public abstract class Table extends SchemaObjectBase { ...@@ -1213,7 +1213,10 @@ 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();
int bType = b.getType();
if (aType != bType || aType == Value.ENUM) {
int dataType = Value.getHigherOrder(aType, bType);
if (dataType == Value.ENUM) { if (dataType == Value.ENUM) {
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b); String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
a = a.convertToEnum(enumerators); a = a.convertToEnum(enumerators);
...@@ -1223,6 +1226,7 @@ public abstract class Table extends SchemaObjectBase { ...@@ -1223,6 +1226,7 @@ public abstract class Table extends SchemaObjectBase {
a = a.convertTo(dataType, -1, mode); a = a.convertTo(dataType, -1, mode);
b = b.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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论