1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
/* Copyright (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/rpl_write_set_handler.h"
#include <inttypes.h>
#include <stddef.h>
#include <sys/types.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "extra/xxhash/my_xxhash.h" // IWYU pragma: keep
#include "lex_string.h"
#include "m_ctype.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_murmur3.h" // murmur3_32
#include "mysql_com.h"
#include "sql-common/json_binary.h"
#include "sql-common/json_dom.h"
#include "sql/field.h" // Field
#include "sql/key.h"
#include "sql/query_options.h"
#include "sql/rpl_transaction_write_set_ctx.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/system_variables.h"
#include "sql/table.h" // TABLE
#include "sql/transaction_info.h"
#include "template_utils.h"
#define HASH_STRING_SEPARATOR "½"
const char *transaction_write_set_hashing_algorithms[] = {"OFF", "MURMUR32",
"XXHASH64", nullptr};
const char *get_write_set_algorithm_string(unsigned int algorithm) {
switch (algorithm) {
case HASH_ALGORITHM_OFF:
return "OFF";
case HASH_ALGORITHM_MURMUR32:
return "MURMUR32";
case HASH_ALGORITHM_XXHASH64:
return "XXHASH64";
default:
return "UNKNOWN ALGORITHM";
}
}
template <class type>
uint64 calc_hash(ulong algorithm, type T, size_t len) {
if (algorithm == HASH_ALGORITHM_MURMUR32)
return (murmur3_32((const uchar *)T, len, 0));
else
return (MY_XXH64((const uchar *)T, len, 0));
}
#ifndef NDEBUG
static void debug_check_for_write_sets(
std::vector<std::string> &key_list_to_hash,
std::vector<uint64> &hash_list) {
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_key_generated_insert",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 340395741608101502ULL););
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_key_generated_update",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1" ||
key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 8563267070232261320ULL ||
hash_list[0] == 340395741608101502ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_primary_key_generated_insert",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 13655149628280894901ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_primary_key_generated_update",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1" ||
key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 16833405476607614310ULL ||
hash_list[0] == 13655149628280894901ULL););
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_unique_key_generated_insert",
assert(key_list_to_hash.size() == 3);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c3" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 3);
assert(hash_list[0] == 340395741608101502ULL &&
hash_list[1] == 12796928550717161120ULL &&
hash_list[2] == 11199547876733116431ULL););
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_unique_key_generated_update",
assert(key_list_to_hash.size() == 3);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c3" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c3" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 3);
assert((hash_list[0] == 7803002059431370747ULL &&
hash_list[1] == 12796928550717161120ULL &&
hash_list[2] == 11199547876733116431ULL) ||
(hash_list[0] == 340395741608101502ULL &&
hash_list[1] == 12796928550717161120ULL &&
hash_list[2] == 11199547876733116431ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_multi_primary_unique_key_generated_insert",
assert(key_list_to_hash.size() == 3);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"b" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 3);
assert(hash_list[0] == 13655149628280894901ULL &&
hash_list[1] == 8954424140835647185ULL &&
hash_list[2] == 3520344117573337805ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_primary_unique_key_generated_update",
assert(key_list_to_hash.size() == 3);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"b" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"b" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 3);
assert((hash_list[0] == 13655149628280894901ULL &&
hash_list[1] == 8954424140835647185ULL &&
hash_list[2] == 3520344117573337805ULL) ||
(hash_list[0] == 17122769277112661326ULL &&
hash_list[1] == 8954424140835647185ULL &&
hash_list[2] == 3520344117573337805ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_multi_foreign_key_generated_insert",
assert(key_list_to_hash.size() == 4);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"15" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[3] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 4);
assert(hash_list[0] == 3283233640848908273ULL &&
hash_list[1] == 17221725733811443497ULL &&
hash_list[2] == 340395741608101502ULL &&
hash_list[3] == 14682037479339770823ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_foreign_key_generated_update",
assert(key_list_to_hash.size() == 4);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"15" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[3] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR
"15" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t3" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "23" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[3] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 4);
assert((hash_list[0] == 3283233640848908273ULL &&
hash_list[1] == 17221725733811443497ULL &&
hash_list[2] == 340395741608101502ULL &&
hash_list[3] == 14682037479339770823ULL) ||
(hash_list[0] == 12666755939597291234ULL &&
hash_list[1] == 17221725733811443497ULL &&
hash_list[2] == 8563267070232261320ULL &&
hash_list[3] == 14682037479339770823ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_unique_key_parent_generated_insert",
assert(key_list_to_hash.size() == 2);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 2);
assert(hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 12796928550717161120ULL););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_unique_key_generated_insert",
assert(key_list_to_hash.size() == 2);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 2);
assert(hash_list[0] == 10002085147685770725ULL &&
hash_list[1] == 8692935619695688993ULL););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_unique_key_generated_update",
assert(key_list_to_hash.size() == 2);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"c2" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 2);
assert((hash_list[0] == 10002085147685770725ULL &&
hash_list[1] == 12796928550717161120ULL) ||
(hash_list[0] == 10002085147685770725ULL &&
hash_list[1] == 8692935619695688993ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_non_unique_key_parent_generated_"
"insert",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 16097759999475440183ULL););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_non_unique_key_generated_insert",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 10002085147685770725ULL););
DBUG_EXECUTE_IF(
"PKE_assert_foreign_key_on_referenced_non_unique_key_generated_update",
assert(key_list_to_hash.size() == 1);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 1);
assert(hash_list[0] == 10002085147685770725ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_column_foreign_key_on_multiple_column_primary_key_"
"insert",
assert(key_list_to_hash.size() == 2);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 2);
assert(hash_list[0] == 15066957522449671266ULL &&
hash_list[1] == 9647156720027801592ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_column_foreign_key_on_multiple_column_primary_key_"
"update",
assert(key_list_to_hash.size() == 2);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR
"12" HASH_STRING_SEPARATOR "13" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 2);
assert((hash_list[0] == 12726729333133305663ULL &&
hash_list[1] == 17273381564889724595ULL) ||
(hash_list[0] == 15066957522449671266ULL &&
hash_list[1] == 9647156720027801592ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_multiple_column_unique_key_insert",
assert(key_list_to_hash.size() == 2);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"key_b_c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR
"13" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 2);
assert(hash_list[0] == 340395741608101502ULL &&
hash_list[1] == 14341778092818779177ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_column_foreign_key_on_multiple_column_unique_key_"
"insert",
assert(key_list_to_hash.size() == 3);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"key_e_f" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR
"13" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"key_b_c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR
"13" HASH_STRING_SEPARATOR "1");
assert(hash_list.size() == 3);
assert(hash_list[0] == 10002085147685770725ULL &&
hash_list[1] == 7781576503154198764ULL &&
hash_list[2] == 14341778092818779177ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multi_column_foreign_key_on_multiple_column_unique_key_"
"update",
assert(key_list_to_hash.size() == 3);
assert((key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "24" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"key_e_f" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR
"16" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"key_b_c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "25" HASH_STRING_SEPARATOR
"16" HASH_STRING_SEPARATOR "1") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "21" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"key_e_f" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t2" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR
"13" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[2] ==
"key_b_c" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR
"13" HASH_STRING_SEPARATOR "1"));
assert(hash_list.size() == 3);
assert((hash_list[0] == 7572125940027161025ULL &&
hash_list[1] == 12139583969308846244ULL &&
hash_list[2] == 3682008013696622692ULL) ||
(hash_list[0] == 10002085147685770725ULL &&
hash_list[1] == 7781576503154198764ULL &&
hash_list[2] == 14341778092818779177ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_key_part_insert",
assert(key_list_to_hash.size() == 2);
assert(hash_list.size() == 2); assert(
key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR "16");
assert(hash_list[0] == 16317411346116498013ULL &&
hash_list[1] == 18003062610192037758ULL););
DBUG_EXECUTE_IF(
"PKE_assert_single_primary_key_part_update",
assert(key_list_to_hash.size() == 2);
assert(hash_list.size() == 2); assert(
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034`\034`\034`\034`" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "2\034`\034`\034`\034`"
"\036\377\036\377\036\377\036\377" HASH_STRING_SEPARATOR "16") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR "16"));
assert((hash_list[0] == 12854286396201953359ULL &&
hash_list[1] == 7801970996840350074ULL) ||
(hash_list[0] == 16317411346116498013ULL &&
hash_list[1] == 18003062610192037758ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_multiple_primary_key_part_insert",
assert(key_list_to_hash.size() == 2);
assert(hash_list.size() == 2);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034z\034z\034z\034z" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] == "PRIMARY" HASH_STRING_SEPARATOR
"test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR
"16\034z\034z\034z\034z\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR "16");
assert(hash_list[0] == 11952739978637581076ULL &&
hash_list[1] == 5625842696515157153ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multiple_primary_key_part_update",
assert(key_list_to_hash.size() == 2);
assert(hash_list.size() == 2); assert(
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034\217\034\217\034\217\034\217" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR
"16\034\217\034\217\034\217\034\217\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR "16") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034z\034z\034z\034z" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[1] == "PRIMARY" HASH_STRING_SEPARATOR
"test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR
"16\034z\034z\034z\034z\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR "16"));
assert((hash_list[0] == 321901661932738829ULL &&
hash_list[1] == 13548511452318291546ULL) ||
(hash_list[0] == 11952739978637581076ULL &&
hash_list[1] == 5625842696515157153ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_single_unique_key_part_insert",
assert(key_list_to_hash.size() == 3);
assert(hash_list.size() == 3);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034`\034`\034`\034`" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "2\034`\034`\034`\034`"
"\036\377\036\377\036\377\036\377" HASH_STRING_SEPARATOR "16");
assert(hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 7725531530140183570ULL &&
hash_list[2] == 14701666498086136515ULL););
DBUG_EXECUTE_IF(
"PKE_assert_single_unique_key_part_update",
assert(key_list_to_hash.size() == 3);
assert(hash_list.size() == 3); assert(
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034\217\034\217\034\217\034\217\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR "16") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034`\034`\034`\034`" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "2\034`\034`\034`\034`"
"\036\377\036\377\036\377\036\377" HASH_STRING_SEPARATOR "16"));
assert((hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 5363251102313010695ULL &&
hash_list[2] == 14835022138864790192ULL) ||
(hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 7725531530140183570ULL &&
hash_list[2] == 14701666498086136515ULL)););
DBUG_EXECUTE_IF(
"PKE_assert_multiple_unique_key_part_insert",
assert(key_list_to_hash.size() == 3);
assert(hash_list.size() == 3);
assert(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034z\034z\034z\034z" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] == "a" HASH_STRING_SEPARATOR
"test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR
"16\034z\034z\034z\034z\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR "16");
assert(hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 1438169577983365775ULL &&
hash_list[2] == 312264863968578629ULL););
DBUG_EXECUTE_IF(
"PKE_assert_multiple_unique_key_part_update",
assert(key_list_to_hash.size() == 3);
assert(hash_list.size() == 3); assert(
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034\217\034\217\034\217\034\217" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036\377\036"
"\377" HASH_STRING_SEPARATOR
"16\034\217\034\217\034\217\034\217\036\377\036\377\036\377"
"\036\377" HASH_STRING_SEPARATOR "16") ||
(key_list_to_hash[0] ==
"PRIMARY" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR "22" HASH_STRING_SEPARATOR "1" &&
key_list_to_hash[1] ==
"a" HASH_STRING_SEPARATOR "test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G" HASH_STRING_SEPARATOR
"8\034z\034z\034z\034z" HASH_STRING_SEPARATOR "8" &&
key_list_to_hash[2] == "a" HASH_STRING_SEPARATOR
"test" HASH_STRING_SEPARATOR
"4t1" HASH_STRING_SEPARATOR
"2\034G\034G\034G\034G\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR
"16\034z\034z\034z\034z\036\377\036\377\036"
"\377\036\377" HASH_STRING_SEPARATOR "16"));
assert((hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 7001586410307804499ULL &&
hash_list[2] == 888558645316043973ULL) ||
(hash_list[0] == 16097759999475440183ULL &&
hash_list[1] == 1438169577983365775ULL &&
hash_list[2] == 312264863968578629ULL)););
}
#endif
/**
Function to generate the hash of the string passed to this function.
@param[in] pke - the string to be hashed.
@param[in] thd - THD object pointing to current thread.
@return true if a problem occurred on generation or write set tracking.
*/
#ifndef NDEBUG
/**
@param[in] write_sets - list of all write sets
@param[in] hash_list - list of all hashes
*/
#endif
static bool generate_hash_pke(const std::string &pke, THD *thd
#ifndef NDEBUG
,
std::vector<std::string> &write_sets,
std::vector<uint64> &hash_list
#endif
) {
DBUG_TRACE;
assert(thd->variables.transaction_write_set_extraction != HASH_ALGORITHM_OFF);
uint64 hash = calc_hash<const char *>(
thd->variables.transaction_write_set_extraction, pke.c_str(), pke.size());
if (thd->get_transaction()->get_transaction_write_set_ctx()->add_write_set(
hash))
return true;
#ifndef NDEBUG
write_sets.push_back(pke);
hash_list.push_back(hash);
#endif
DBUG_PRINT("info", ("pke: %s; hash: %" PRIu64, pke.c_str(), hash));
return false;
}
/**
Function to generate set of hashes for a multi-valued key
@param[in] prefix_pke - stringified non-multi-valued prefix of key
@param[in] thd - THD object pointing to current thread.
@param[in] fld - multi-valued keypart's field
@return true if a problem occurred on generation or write set tracking.
*/
#ifndef NDEBUG
/**
@param[in] write_sets - DEBUG ONLY, vector of added PKEs
@param[in] hash_list - DEBUG ONLY, list of all hashes
*/
#endif
static bool generate_mv_hash_pke(const std::string &prefix_pke, THD *thd,
Field *fld
#ifndef NDEBUG
,
std::vector<std::string> &write_sets,
std::vector<uint64> &hash_list
#endif
) {
Field_typed_array *field = down_cast<Field_typed_array *>(fld);
json_binary::Value v(
json_binary::parse_binary(field->get_binary(), field->data_length()));
uint elems = v.element_count();
if (!elems || field->is_null()) {
// Multi-valued key part doesn't contain actual values.
// No need to hash prefix pke as it won't cause conflicts.
} else {
const CHARSET_INFO *cs = field->charset();
size_t max_length = cs->coll->strnxfrmlen(cs, field->key_length());
std::unique_ptr<uchar[]> pk_value(new uchar[max_length + 1]());
assert(v.type() == json_binary::Value::ARRAY);
for (uint i = 0; i < elems; i++) {
std::string pke = prefix_pke;
json_binary::Value elt = v.element(i);
Json_wrapper wr(elt);
/*
convert to normalized string and store so that it can be
sorted using binary comparison functions like memcmp.
*/
size_t length = field->make_sort_key(&wr, pk_value.get(), max_length);
pk_value[length] = 0;
pke.append(pointer_cast<char *>(pk_value.get()), length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(std::to_string(length));
if (generate_hash_pke(pke, thd
#ifndef NDEBUG
,
write_sets, hash_list
#endif
))
return true;
}
}
return false;
}
bool add_pke(TABLE *table, THD *thd, const uchar *record) {
DBUG_TRACE;
assert(record == table->record[0] || record == table->record[1]);
/*
The next section extracts the primary key equivalent of the rows that are
changing during the current transaction.
1. The primary key field is always stored in the key_part[0] so we can
simply read the value from the table->s->keys.
2. Along with primary key we also need to extract the unique key values to
look for the places where we are breaking the unique key constraints.
These keys (primary/unique) are prefixed with their index names.
In MySQL, the name of a PRIMARY KEY is PRIMARY. For other indexes, if
you do not assign a name, the index is assigned the same name as the
first indexed column, with an optional suffix (_2, _3, ...) to make it
unique.
example :
CREATE TABLE db1.t1 (i INT NOT NULL PRIMARY KEY, j INT UNIQUE KEY, k INT
UNIQUE KEY);
INSERT INTO db1.t1 VALUES(1, 2, 3);
Here the write set string will have three values and the prepared value
before hash function is used will be :
i -> PRIMARYdb13t1211 => PRIMARY is the index name (for primary key)
j -> jdb13t1221 => 'j' is the index name (for first unique key)
k -> kdb13t1231 => 'k' is the index name (for second unique key)
Finally these value are hashed using the murmur hash function to prevent
sending more for certification algorithm.
*/
Rpl_transaction_write_set_ctx *ws_ctx =
thd->get_transaction()->get_transaction_write_set_ctx();
bool writeset_hashes_added = false;
if (table->key_info && (table->s->primary_key < MAX_KEY)) {
const ptrdiff_t ptrdiff = record - table->record[0];
std::string pke_schema_table;
pke_schema_table.reserve(NAME_LEN * 3);
pke_schema_table.append(HASH_STRING_SEPARATOR);
pke_schema_table.append(table->s->db.str, table->s->db.length);
pke_schema_table.append(HASH_STRING_SEPARATOR);
pke_schema_table.append(std::to_string(table->s->db.length));
pke_schema_table.append(table->s->table_name.str,
table->s->table_name.length);
pke_schema_table.append(HASH_STRING_SEPARATOR);
pke_schema_table.append(std::to_string(table->s->table_name.length));
std::string pke;
pke.reserve(NAME_LEN * 5);
#ifndef NDEBUG
std::vector<std::string> write_sets;
std::vector<uint64> hash_list;
#endif
for (uint key_number = 0; key_number < table->s->keys; key_number++) {
// Skip non unique.
if (!((table->key_info[key_number].flags & (HA_NOSAME)) == HA_NOSAME))
continue;
enum pke_mode_e { STANDARD_PKE = 1, NO_PARTIAL_KEYS_PKE = 2, END = 3 };
bool old_pke_needed = false;
uint pke_mode = pke_mode_e::STANDARD_PKE;
DBUG_EXECUTE_IF("do_not_add_pke_key_part",
{ pke_mode = pke_mode_e::NO_PARTIAL_KEYS_PKE; });
while (pke_mode != pke_mode_e::END) {
pke.clear();
pke.append(table->key_info[key_number].name);
pke.append(pke_schema_table);
uint i = 0;
// Whether the key has mv keypart which have to be handled separately
Field *mv_field = nullptr;
for (/*empty*/; i < table->key_info[key_number].user_defined_key_parts;
i++) {
/* Get the primary key field index. */
int index = table->key_info[key_number].key_part[i].fieldnr;
Field *field = table->field[index - 1];
/* Ignore if the value is NULL. */
if (field->is_null(ptrdiff)) break;
if (field->is_array()) {
// There can be only one multi-valued key part per key
assert(!mv_field);
mv_field = field;
// Skip it for now
continue;
}
/*
Update the field offset as we may be working on table->record[0]
or table->record[1], depending on the "record" parameter.
*/
field->move_field_offset(ptrdiff);
const CHARSET_INFO *cs = field->charset();
size_t key_length = table->key_info[key_number].key_part[i].length;
size_t max_length = cs->coll->strnxfrmlen(cs, key_length);
if (pke_mode == pke_mode_e::STANDARD_PKE) {
std::unique_ptr<uchar[]> pk_value(new uchar[max_length + 1]());
/*
convert to normalized string and store so that it can be
sorted using binary comparison functions like memcmp.
*/
size_t length = field->make_sort_key(pk_value.get(), max_length,
key_length / cs->mbmaxlen);
pk_value[length] = 0;
pke.append(pointer_cast<char *>(pk_value.get()), length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(std::to_string(length));
}
/*
When we upgrade a group, old members might be using the old PKEs and
so they identify a row A with key OLD_PKE. This is the value that is
in the certification table. When all members are upgrade and are now
in the latest version, all members now identify row A with key
NEW_PKE. How do you detect conflicts in row A if the transactions in
the certification database have the old PKE?
So the decision taken was to always send the old PKE and the new PKE
when keys have only a partial value of a column. In 9.0 we remove
this code for the old PKE.
*/
size_t pack_length = cs->coll->strnxfrmlen(cs, field->pack_length());
if (pke_mode == pke_mode_e::NO_PARTIAL_KEYS_PKE) {
std::unique_ptr<uchar[]> pk_value(new uchar[pack_length + 1]());
/*
convert to normalized string and store so that it can be
sorted using binary comparison functions like memcmp.
*/
size_t length = field->make_sort_key(pk_value.get(), pack_length);
pk_value[length] = 0;
pke.append(pointer_cast<char *>(pk_value.get()), length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(std::to_string(length));
}
if (pke_mode == pke_mode_e::STANDARD_PKE &&
max_length != pack_length) {
old_pke_needed = true;
}
field->move_field_offset(-ptrdiff);
}
/*
If any part of the key is NULL, ignore adding it to hash keys.
NULL cannot conflict with any value.
Eg: create table t1(i int primary key not null, j int, k int,
unique key (j, k));
insert into t1 values (1, 2, NULL);
insert into t1 values (2, 2, NULL); => this is allowed.
*/
if (i == table->key_info[key_number].user_defined_key_parts) {
if (mv_field) {
mv_field->move_field_offset(ptrdiff);
if (generate_mv_hash_pke(pke, thd, mv_field
#ifndef NDEBUG
,
write_sets, hash_list
#endif
))
return true;
mv_field->move_field_offset(-ptrdiff);
} else {
if (generate_hash_pke(pke, thd
#ifndef NDEBUG
,
write_sets, hash_list
#endif
))
return true;
}
writeset_hashes_added = true;
} else {
/* This is impossible to happen in case of primary keys */
assert(key_number != 0);
}
if (pke_mode == pke_mode_e::STANDARD_PKE && old_pke_needed == true) {
pke_mode = pke_mode_e::NO_PARTIAL_KEYS_PKE;
} else {
pke_mode = pke_mode_e::END;
}
}
}
/*
Foreign keys handling.
OPTION_NO_FOREIGN_KEY_CHECKS bit in options_bits is set at two places
1) If the user executed 'SET foreign_key_checks= 0' on the local session
before executing the query.
or
2) We are applying a RBR event (i.e., the event is from a remote server)
and logic in Rows_log_event::do_apply_event found out that the event is
generated from a remote server session that disabled foreign_key_checks
(using 'SET foreign_key_checks=0').
In either of the above cases (i.e., the foreign key check is disabled for
the current query/current event), we should ignore generating
the foreign key information as they should not participate
in the conflicts detecting algorithm.
*/
if (!(thd->variables.option_bits & OPTION_NO_FOREIGN_KEY_CHECKS) &&
table->s->foreign_keys > 0) {
TABLE_SHARE_FOREIGN_KEY_INFO *fk = table->s->foreign_key;
for (uint fk_number = 0; fk_number < table->s->foreign_keys;
fk_number++) {
/*
There are two situations on which there is no
unique_constraint_name, which means that the foreign key
must be skipped.
1) The referenced table was dropped using
foreign_key_checks= 0, on that case we cannot check
foreign key and need to skip it.
2) The foreign key does reference a non unique key, thence
it must be skipped since it cannot be used to check
conflicts/dependencies.
Example:
CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT, KEY(c2));
CREATE TABLE t2 (x1 INT PRIMARY KEY, x2 INT,
FOREIGN KEY (x2) REFERENCES t1(c2));
DELETE FROM t1 WHERE c1=1;
does generate the PKEs:
PRIMARY½test½4t1½21½1
INSERT INTO t2 VALUES (1,1);
does generate the PKEs:
PRIMARY½test½4t2½21½1
which does not contain PKE for the non unique key c2.
*/
if (0 == fk[fk_number].unique_constraint_name.length) continue;
const std::string referenced_schema_name_length =
std::to_string(fk[fk_number].referenced_table_db.length);
const std::string referenced_table_name_length =
std::to_string(fk[fk_number].referenced_table_name.length);
/*
Prefix the hash keys with the referenced index name.
*/
pke.clear();
pke.append(fk[fk_number].unique_constraint_name.str,
fk[fk_number].unique_constraint_name.length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(fk[fk_number].referenced_table_db.str,
fk[fk_number].referenced_table_db.length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(referenced_schema_name_length);
pke.append(fk[fk_number].referenced_table_name.str,
fk[fk_number].referenced_table_name.length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(referenced_table_name_length);
/*
Foreign key must not have a empty column list.
*/
assert(fk[fk_number].columns > 0);
for (uint c = 0; c < fk[fk_number].columns; c++) {
for (uint field_number = 0; field_number < table->s->fields;
field_number++) {
Field *field = table->field[field_number];
if (field->is_null(ptrdiff)) continue;
if (!my_strcasecmp(system_charset_info, field->field_name,
fk[fk_number].column_name[c].str)) {
/*
Update the field offset, since we may be operating on
table->record[0] or table->record[1] and both have
different offsets.
*/
field->move_field_offset(ptrdiff);
const CHARSET_INFO *cs = field->charset();
size_t max_length =
cs->coll->strnxfrmlen(cs, field->pack_length());
std::unique_ptr<uchar[]> pk_value(new uchar[max_length + 1]());
/*
convert to normalized string and store so that it can be
sorted using binary comparison functions like memcmp.
*/
size_t length = field->make_sort_key(pk_value.get(), max_length);
pk_value[length] = 0;
pke.append(pointer_cast<char *>(pk_value.get()), length);
pke.append(HASH_STRING_SEPARATOR);
pke.append(std::to_string(length));
/* revert the field object record offset back */
field->move_field_offset(-ptrdiff);
}
}
}
if (generate_hash_pke(pke, thd
#ifndef NDEBUG
,
write_sets, hash_list
#endif
))
return true;
writeset_hashes_added = true;
}
}
if (table->s->foreign_key_parents > 0)
ws_ctx->set_has_related_foreign_keys();
#ifndef NDEBUG
debug_check_for_write_sets(write_sets, hash_list);
#endif
}
if (!writeset_hashes_added) ws_ctx->set_has_missing_keys();
return false;
}
|