3030public class ColumnList {
3131 private final List <Object > values ;
3232 private final List <Integer > types ;
33- private final boolean Simba ;
33+ private final boolean simba ;
3434 public static final String SIMBA_JDBC = "sjdbc" ;
35+ private static final double MAX_DIFF_VALUE = 1.0E-6 ;
3536
3637 public ColumnList (List <Integer > types , List <Object > values ) {
3738 this .values = values ;
3839 this .types = types ;
3940 if (TestDriver .cmdParam .driverExt != null &&
4041 TestDriver .cmdParam .driverExt .equals (ColumnList .SIMBA_JDBC )) {
41- this .Simba = true ;
42+ this .simba = true ;
4243 }
4344 else {
44- this .Simba = false ;
45+ this .simba = false ;
4546 }
4647 }
4748
@@ -54,9 +55,15 @@ public List<Object> getValues() {
5455 * "loosened" logic to handle float, double and decimal types. The algorithm
5556 * used for the comparison follows:
5657 *
57- * Floats: f1 equals f2 iff |((f1-f2)/(average(f1,f2)))| < 0.000001
58+ * Floats: f1 and f2 are equal
59+ * if f1 == f2
60+ * if f1 > f2 && if |(f1-f2)/f1| < 0.000001
61+ * if f1 < f2 && if |(f1-f2)/f2| < 0.000001
5862 *
59- * Doubles: d1 equals d2 iff |((d1-d2)/(average(d1,d2)))| < 0.000000000001
63+ * Doubles: d1 and d2 are equal
64+ * if d1 == d2
65+ * if d1 > d2 && if |(d1-d2)/d1| < 0.000001
66+ * if d1 < d2 && if |(d1-d2)/d2| < 0.000001
6067 *
6168 * Decimals: dec1 equals dec2 iff value(dec1) == value(dec2) and scale(dec1)
6269 * == scale(dec2)
@@ -76,7 +83,7 @@ public int hashCode() {
7683 if (values .get (i ) == null ) {
7784 continue ;
7885 }
79- int type = ( Integer ) ( types .get (i ) );
86+ int type = types .get (i );
8087 switch (type ) {
8188 case Types .FLOAT :
8289 case Types .DOUBLE :
@@ -98,8 +105,8 @@ public int hashCode() {
98105 public String toString () {
99106 StringBuilder sb = new StringBuilder ();
100107 for (int i = 0 ; i < values .size () - 1 ; i ++) {
101- int type = ( Integer ) ( types .get (i ) );
102- if (Simba && (type == Types .VARCHAR )) {
108+ int type = types .get (i );
109+ if (simba && (type == Types .VARCHAR )) {
103110 String s1 = String .valueOf (values .get (i ));
104111 // if the field has a JSON string or list, then remove newlines so that
105112 // each record fits on a single line in the actual output file.
@@ -114,8 +121,8 @@ public String toString() {
114121 }
115122 sb .append (values .get (i ) + "\t " );
116123 }
117- int type = ( Integer ) ( types .get (values .size ()-1 ) );
118- if (Simba && (type == Types .VARCHAR )) {
124+ int type = types .get (values .size ()-1 );
125+ if (simba && (type == Types .VARCHAR )) {
119126 String s1 = String .valueOf (values .get (values .size ()-1 ));
120127 // if the field has a JSON string or list, then remove newlines so that
121128 // each record fits on a single line in the actual output file.
@@ -147,17 +154,23 @@ private boolean compare(ColumnList o1, ColumnList o2) {
147154 if (oneNull (list1 .get (i ), list2 .get (i ))) {
148155 return false ;
149156 }
150- int type = ( Integer ) ( types .get (i ) );
157+ int type = types .get (i );
151158 try {
152159 switch (type ) {
153160 case Types .FLOAT :
154161 case Types .REAL :
155162 float f1 = (Float ) list1 .get (i );
156163 float f2 = (Float ) list2 .get (i );
157- if ((f1 + f2 ) / 2 != 0 ) {
158- if (!(Math .abs ((f1 - f2 ) / ((f1 + f2 ) / 2 )) < 1.0E-6 )) return false ;
159- } else if (f1 != 0 ) {
160- return false ;
164+ if (f1 != f2 ) {
165+ double relativeError ;
166+ if (f1 > f2 ) {
167+ relativeError = Math .abs ((f1 -f2 )/f1 );
168+ } else {
169+ relativeError = Math .abs ((f1 -f2 )/f2 );
170+ }
171+ if (relativeError > MAX_DIFF_VALUE ) {
172+ return false ;
173+ }
161174 }
162175 break ;
163176 case Types .DOUBLE :
@@ -167,9 +180,13 @@ private boolean compare(ColumnList o1, ColumnList o2) {
167180 // especially for the cases when doubles are NaN / POSITIVE_INFINITY / NEGATIVE_INFINITY
168181 // otherwise proceed with "loosened" logic
169182 if (!d1 .equals (d2 )) {
170- if ((d1 + d2 ) / 2 != 0 ) {
171- if (!(Math .abs ((d1 - d2 ) / ((d1 + d2 ) / 2 )) < 1.0E-12 )) return false ;
172- } else if (d1 != 0 ) {
183+ double relativeError ;
184+ if (d1 > d2 ) {
185+ relativeError = Math .abs ((d1 -d2 )/d1 );
186+ } else {
187+ relativeError = Math .abs ((d1 -d2 )/d2 );
188+ }
189+ if (relativeError > MAX_DIFF_VALUE ) {
173190 return false ;
174191 }
175192 }
0 commit comments