@@ -402,6 +402,10 @@ func (t numberT) Compare(a interface{}, b interface{}) (int, error) {
402
402
func (t numberT ) String () string { return t .t .String () }
403
403
404
404
func compareFloats (a interface {}, b interface {}) (int , error ) {
405
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
406
+ return res , nil
407
+ }
408
+
405
409
ca , err := cast .ToFloat64E (a )
406
410
if err != nil {
407
411
return 0 , err
@@ -423,6 +427,10 @@ func compareFloats(a interface{}, b interface{}) (int, error) {
423
427
}
424
428
425
429
func compareSignedInts (a interface {}, b interface {}) (int , error ) {
430
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
431
+ return res , nil
432
+ }
433
+
426
434
ca , err := cast .ToInt64E (a )
427
435
if err != nil {
428
436
return 0 , err
@@ -444,6 +452,10 @@ func compareSignedInts(a interface{}, b interface{}) (int, error) {
444
452
}
445
453
446
454
func compareUnsignedInts (a interface {}, b interface {}) (int , error ) {
455
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
456
+ return res , nil
457
+ }
458
+
447
459
ca , err := cast .ToUint64E (a )
448
460
if err != nil {
449
461
return 0 , err
@@ -540,6 +552,10 @@ func (t timestampT) Convert(v interface{}) (interface{}, error) {
540
552
541
553
// Compare implements Type interface.
542
554
func (t timestampT ) Compare (a interface {}, b interface {}) (int , error ) {
555
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
556
+ return res , nil
557
+ }
558
+
543
559
av := a .(time.Time )
544
560
bv := b .(time.Time )
545
561
if av .Before (bv ) {
@@ -603,6 +619,10 @@ func (t dateT) Convert(v interface{}) (interface{}, error) {
603
619
}
604
620
605
621
func (t dateT ) Compare (a , b interface {}) (int , error ) {
622
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
623
+ return res , nil
624
+ }
625
+
606
626
av := truncateDate (a .(time.Time ))
607
627
bv := truncateDate (b .(time.Time ))
608
628
if av .Before (bv ) {
@@ -758,6 +778,9 @@ func (t varCharT) Convert(v interface{}) (interface{}, error) {
758
778
759
779
// Compare implements Type interface.
760
780
func (t varCharT ) Compare (a interface {}, b interface {}) (int , error ) {
781
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
782
+ return res , nil
783
+ }
761
784
return strings .Compare (a .(string ), b .(string )), nil
762
785
}
763
786
@@ -795,6 +818,9 @@ func (t textT) Convert(v interface{}) (interface{}, error) {
795
818
796
819
// Compare implements Type interface.
797
820
func (t textT ) Compare (a interface {}, b interface {}) (int , error ) {
821
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
822
+ return res , nil
823
+ }
798
824
return strings .Compare (a .(string ), b .(string )), nil
799
825
}
800
826
@@ -847,6 +873,10 @@ func (t booleanT) Convert(v interface{}) (interface{}, error) {
847
873
848
874
// Compare implements Type interface.
849
875
func (t booleanT ) Compare (a interface {}, b interface {}) (int , error ) {
876
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
877
+ return res , nil
878
+ }
879
+
850
880
if a == b {
851
881
return 0 , nil
852
882
}
@@ -899,6 +929,9 @@ func (t blobT) Convert(v interface{}) (interface{}, error) {
899
929
900
930
// Compare implements Type interface.
901
931
func (t blobT ) Compare (a interface {}, b interface {}) (int , error ) {
932
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
933
+ return res , nil
934
+ }
902
935
return bytes .Compare (a .([]byte ), b .([]byte )), nil
903
936
}
904
937
@@ -941,6 +974,9 @@ func (t jsonT) Convert(v interface{}) (interface{}, error) {
941
974
942
975
// Compare implements Type interface.
943
976
func (t jsonT ) Compare (a interface {}, b interface {}) (int , error ) {
977
+ if hasNulls , res := compareNulls (a , b ); hasNulls {
978
+ return res , nil
979
+ }
944
980
return bytes .Compare (a .([]byte ), b .([]byte )), nil
945
981
}
946
982
@@ -1302,3 +1338,19 @@ func convertArrayForJSON(t arrayT, v interface{}) (interface{}, error) {
1302
1338
return nil , ErrNotArray .New (v )
1303
1339
}
1304
1340
}
1341
+
1342
+ // compareNulls compares two values, and returns true if either is null.
1343
+ // The returned integer represents the ordering, with a rule that states nulls
1344
+ // as being ordered before non-nulls.
1345
+ func compareNulls (a interface {}, b interface {}) (bool , int ) {
1346
+ aIsNull := a == nil
1347
+ bIsNull := b == nil
1348
+ if aIsNull && bIsNull {
1349
+ return true , 0
1350
+ } else if aIsNull && ! bIsNull {
1351
+ return true , - 1
1352
+ } else if ! aIsNull && bIsNull {
1353
+ return true , 1
1354
+ }
1355
+ return false , 0
1356
+ }
0 commit comments