From a4b7935a0eb9c81f3f8a3599dd7df6a349b114c8 Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Thu, 12 Mar 2020 14:43:27 -0700 Subject: [PATCH] Make sort stable (#416) Sort reverses records that compare as equal, making it unstable. Fix that by using the proper comparison in expr.(*RecordSlice).Less. This reverts #37. --- expr/sort.go | 2 +- tests/suite/sort/sort-4-1.yaml | 2 +- tests/suite/sort/sort-4-2.yaml | 14 +++++++------- tests/suite/sort/sort-4-3.yaml | 2 +- tests/suite/sort/sort-4-4.yaml | 14 +++++++------- zql/docs/processors/README.md | 14 +++++++------- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/expr/sort.go b/expr/sort.go index ca565cf67c..5db903db12 100644 --- a/expr/sort.go +++ b/expr/sort.go @@ -103,7 +103,7 @@ func (r *RecordSlice) Swap(i, j int) { r.records[i], r.records[j] = r.records[j] // Less implements sort.Interface for *Record slices. func (r *RecordSlice) Less(i, j int) bool { - return r.sorter(r.records[i], r.records[j]) <= 0 + return r.sorter(r.records[i], r.records[j]) < 0 } // Push adds x as element Len(). Implements heap.Interface. diff --git a/tests/suite/sort/sort-4-1.yaml b/tests/suite/sort/sort-4-1.yaml index 3c5db15b5b..270b2f5e9a 100644 --- a/tests/suite/sort/sort-4-1.yaml +++ b/tests/suite/sort/sort-4-1.yaml @@ -14,6 +14,6 @@ output: | 0:[a;] 0:[b;] 0:[c;] + 0:[-;] #1:record[notS:string] 1:[bleah;] - 0:[-;] diff --git a/tests/suite/sort/sort-4-2.yaml b/tests/suite/sort/sort-4-2.yaml index 2ac7dcf677..fa11299546 100644 --- a/tests/suite/sort/sort-4-2.yaml +++ b/tests/suite/sort/sort-4-2.yaml @@ -10,10 +10,10 @@ input: | 1:[bleah;] output: | - #0:record[notS:string] - 0:[bleah;] - #1:record[s:string] - 1:[-;] - 1:[a;] - 1:[b;] - 1:[c;] + #0:record[s:string] + 0:[-;] + #1:record[notS:string] + 1:[bleah;] + 0:[a;] + 0:[b;] + 0:[c;] diff --git a/tests/suite/sort/sort-4-3.yaml b/tests/suite/sort/sort-4-3.yaml index 829c3d131c..2aaf99abff 100644 --- a/tests/suite/sort/sort-4-3.yaml +++ b/tests/suite/sort/sort-4-3.yaml @@ -14,6 +14,6 @@ output: | 0:[c;] 0:[b;] 0:[a;] + 0:[-;] #1:record[notS:string] 1:[bleah;] - 0:[-;] diff --git a/tests/suite/sort/sort-4-4.yaml b/tests/suite/sort/sort-4-4.yaml index ba7d878e7f..045bedaf36 100644 --- a/tests/suite/sort/sort-4-4.yaml +++ b/tests/suite/sort/sort-4-4.yaml @@ -10,10 +10,10 @@ input: | 1:[bleah;] output: | - #0:record[notS:string] - 0:[bleah;] - #1:record[s:string] - 1:[-;] - 1:[c;] - 1:[b;] - 1:[a;] + #0:record[s:string] + 0:[-;] + #1:record[notS:string] + 1:[bleah;] + 0:[c;] + 0:[b;] + 0:[a;] diff --git a/zql/docs/processors/README.md b/zql/docs/processors/README.md index e86ff5a731..5af0262c36 100644 --- a/zql/docs/processors/README.md +++ b/zql/docs/processors/README.md @@ -152,14 +152,14 @@ zq -f table 'sort certificate.subject' x509.log.gz ```zq-output head:10 _PATH TS ID CERTIFICATE.VERSION CERTIFICATE.SERIAL CERTIFICATE.SUBJECT CERTIFICATE.ISSUER CERTIFICATE.NOT_VALID_BEFORE CERTIFICATE.NOT_VALID_AFTER CERTIFICATE.KEY_ALG CERTIFICATE.SIG_ALG CERTIFICATE.KEY_TYPE CERTIFICATE.KEY_LENGTH CERTIFICATE.EXPONENT CERTIFICATE.CURVE SAN.DNS SAN.URI SAN.EMAIL SAN.IP BASIC_CONSTRAINTS.CA BASIC_CONSTRAINTS.PATH_LEN x509 1521912578.233315 Fn2Gkp2Qd434JylJX9 3 CB11D05B561B4BB1 C=/C=US/ST=HI/O=Goldner and Sons/OU=1080p/CN=goldner.sons.net/emailAddress=1080p@goldner.sons.net C=/C=US/ST=HI/O=Goldner and Sons/OU=1080p/CN=goldner.sons.net/emailAddress=1080p@goldner.sons.net 1462788542.000000 1525860542.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - - - - - T - -x509 1521911928.524679 F6WWPk3ajsHLrmNFdb 3 031489479BCD9C116EA7B6162E5E68E6 CN=*.adnxs.com,O=AppNexus\\, Inc.,L=New York,ST=New York,C=US CN=DigiCert ECC Secure Server CA,O=DigiCert Inc,C=US 1516867200.000000 1548446400.000000 id-ecPublicKey ecdsa-with-SHA256 ecdsa 256 - prime256v1 *.adnxs.com,adnxs.com - - - F - x509 1521911928.524223 Fxq7P31K2FS3v7CBSh 3 031489479BCD9C116EA7B6162E5E68E6 CN=*.adnxs.com,O=AppNexus\\, Inc.,L=New York,ST=New York,C=US CN=DigiCert ECC Secure Server CA,O=DigiCert Inc,C=US 1516867200.000000 1548446400.000000 id-ecPublicKey ecdsa-with-SHA256 ecdsa 256 - prime256v1 *.adnxs.com,adnxs.com - - - F - -x509 1521912591.670418 F7QTmz23i9Wb9PxCec 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - -x509 1521912591.670293 F0hybM3L5RvvQnB0Af 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - -x509 1521912591.322345 FPETq2xQiKkghuuhj 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - -x509 1521912591.317347 FMITm2OyLT3OYnfq3 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - -x509 1521912590.367386 FAquaM1YmnRYGrPM0j 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - -x509 1521912590.364985 FXXOyHz8ZK5Fqyoi7 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521911928.524679 F6WWPk3ajsHLrmNFdb 3 031489479BCD9C116EA7B6162E5E68E6 CN=*.adnxs.com,O=AppNexus\\, Inc.,L=New York,ST=New York,C=US CN=DigiCert ECC Secure Server CA,O=DigiCert Inc,C=US 1516867200.000000 1548446400.000000 id-ecPublicKey ecdsa-with-SHA256 ecdsa 256 - prime256v1 *.adnxs.com,adnxs.com - - - F - +x509 1521912580.661204 FEMo0JLdFfaiP3cCj 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521912580.664443 Fx9w2e3ZeGeRVzB7wa 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521912580.971149 Fs71N02K3C48z0W8Rl 3 08C2D95B922842FCD7EEC9C4AF3BB3C1 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521912580.972007 FNfnZ84jkUdb1ELG4e 3 08C2D95B922842FCD7EEC9C4AF3BB3C1 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521912581.350977 FE774oxbdOCDlPx0i 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - +x509 1521912581.351155 FQNOg4tbfGapYl4A7 3 068D4086AEB3472996E5DFA2EC521A41 CN=*.adobe.com,OU=IS,O=Adobe Systems Incorporated,L=San Jose,ST=California,C=US CN=DigiCert SHA2 Secure Server CA,O=DigiCert Inc,C=US 1515139200.000000 1546718400.000000 rsaEncryption sha256WithRSAEncryption rsa 2048 65537 - *.adobe.com - - - F - ... ```