@@ -402,6 +402,10 @@ func (t numberT) Compare(a interface{}, b interface{}) (int, error) {
402402func (t numberT ) String () string { return t .t .String () }
403403
404404func compareFloats (a interface {}, b interface {}) (int , error ) {
405+ if hasNulls , res := compareNulls (a , b ); hasNulls {
406+ return res , nil
407+ }
408+
405409 ca , err := cast .ToFloat64E (a )
406410 if err != nil {
407411 return 0 , err
@@ -423,6 +427,10 @@ func compareFloats(a interface{}, b interface{}) (int, error) {
423427}
424428
425429func compareSignedInts (a interface {}, b interface {}) (int , error ) {
430+ if hasNulls , res := compareNulls (a , b ); hasNulls {
431+ return res , nil
432+ }
433+
426434 ca , err := cast .ToInt64E (a )
427435 if err != nil {
428436 return 0 , err
@@ -444,6 +452,10 @@ func compareSignedInts(a interface{}, b interface{}) (int, error) {
444452}
445453
446454func compareUnsignedInts (a interface {}, b interface {}) (int , error ) {
455+ if hasNulls , res := compareNulls (a , b ); hasNulls {
456+ return res , nil
457+ }
458+
447459 ca , err := cast .ToUint64E (a )
448460 if err != nil {
449461 return 0 , err
@@ -540,6 +552,10 @@ func (t timestampT) Convert(v interface{}) (interface{}, error) {
540552
541553// Compare implements Type interface.
542554func (t timestampT ) Compare (a interface {}, b interface {}) (int , error ) {
555+ if hasNulls , res := compareNulls (a , b ); hasNulls {
556+ return res , nil
557+ }
558+
543559 av := a .(time.Time )
544560 bv := b .(time.Time )
545561 if av .Before (bv ) {
@@ -603,6 +619,10 @@ func (t dateT) Convert(v interface{}) (interface{}, error) {
603619}
604620
605621func (t dateT ) Compare (a , b interface {}) (int , error ) {
622+ if hasNulls , res := compareNulls (a , b ); hasNulls {
623+ return res , nil
624+ }
625+
606626 av := truncateDate (a .(time.Time ))
607627 bv := truncateDate (b .(time.Time ))
608628 if av .Before (bv ) {
@@ -758,6 +778,9 @@ func (t varCharT) Convert(v interface{}) (interface{}, error) {
758778
759779// Compare implements Type interface.
760780func (t varCharT ) Compare (a interface {}, b interface {}) (int , error ) {
781+ if hasNulls , res := compareNulls (a , b ); hasNulls {
782+ return res , nil
783+ }
761784 return strings .Compare (a .(string ), b .(string )), nil
762785}
763786
@@ -795,6 +818,9 @@ func (t textT) Convert(v interface{}) (interface{}, error) {
795818
796819// Compare implements Type interface.
797820func (t textT ) Compare (a interface {}, b interface {}) (int , error ) {
821+ if hasNulls , res := compareNulls (a , b ); hasNulls {
822+ return res , nil
823+ }
798824 return strings .Compare (a .(string ), b .(string )), nil
799825}
800826
@@ -847,6 +873,10 @@ func (t booleanT) Convert(v interface{}) (interface{}, error) {
847873
848874// Compare implements Type interface.
849875func (t booleanT ) Compare (a interface {}, b interface {}) (int , error ) {
876+ if hasNulls , res := compareNulls (a , b ); hasNulls {
877+ return res , nil
878+ }
879+
850880 if a == b {
851881 return 0 , nil
852882 }
@@ -899,6 +929,9 @@ func (t blobT) Convert(v interface{}) (interface{}, error) {
899929
900930// Compare implements Type interface.
901931func (t blobT ) Compare (a interface {}, b interface {}) (int , error ) {
932+ if hasNulls , res := compareNulls (a , b ); hasNulls {
933+ return res , nil
934+ }
902935 return bytes .Compare (a .([]byte ), b .([]byte )), nil
903936}
904937
@@ -941,6 +974,9 @@ func (t jsonT) Convert(v interface{}) (interface{}, error) {
941974
942975// Compare implements Type interface.
943976func (t jsonT ) Compare (a interface {}, b interface {}) (int , error ) {
977+ if hasNulls , res := compareNulls (a , b ); hasNulls {
978+ return res , nil
979+ }
944980 return bytes .Compare (a .([]byte ), b .([]byte )), nil
945981}
946982
@@ -1302,3 +1338,19 @@ func convertArrayForJSON(t arrayT, v interface{}) (interface{}, error) {
13021338 return nil , ErrNotArray .New (v )
13031339 }
13041340}
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