forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdbScanOperation.cpp
More file actions
4084 lines (3574 loc) · 116 KB
/
NdbScanOperation.cpp
File metadata and controls
4084 lines (3574 loc) · 116 KB
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
/*
Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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 "API.hpp"
#include <NdbSqlUtil.hpp>
#include <AttributeHeader.hpp>
#include <signaldata/ScanTab.hpp>
#include <signaldata/KeyInfo.hpp>
#include <signaldata/AttrInfo.hpp>
#include <signaldata/TcKeyReq.hpp>
#define DEBUG_NEXT_RESULT 0
NdbScanOperation::NdbScanOperation(Ndb* aNdb, NdbOperation::Type aType) :
NdbOperation(aNdb, aType),
m_transConnection(NULL)
{
theParallelism = 0;
m_allocated_receivers = 0;
m_prepared_receivers = 0;
m_api_receivers = 0;
m_conf_receivers = 0;
m_sent_receivers = 0;
m_receivers = 0;
m_array = new Uint32[1]; // skip if on delete in fix_receivers
theSCAN_TABREQ = 0;
m_executed = false;
m_scan_buffer= NULL;
m_scanUsingOldApi= true;
m_readTuplesCalled= false;
m_interpretedCodeOldApi= NULL;
}
NdbScanOperation::~NdbScanOperation()
{
for(Uint32 i = 0; i<m_allocated_receivers; i++){
m_receivers[i]->release();
theNdb->releaseNdbScanRec(m_receivers[i]);
}
delete[] m_array;
}
void
NdbScanOperation::setErrorCode(int aErrorCode) const
{
NdbScanOperation *pnonConstThis=const_cast<NdbScanOperation *>(this);
NdbTransaction* tmp = theNdbCon;
pnonConstThis->theNdbCon = m_transConnection;
NdbOperation::setErrorCode(aErrorCode);
pnonConstThis->theNdbCon = tmp;
}
void
NdbScanOperation::setErrorCodeAbort(int aErrorCode) const
{
NdbScanOperation *pnonConstThis=const_cast<NdbScanOperation *>(this);
NdbTransaction* tmp = theNdbCon;
pnonConstThis->theNdbCon = m_transConnection;
NdbOperation::setErrorCodeAbort(aErrorCode);
pnonConstThis->theNdbCon = tmp;
}
/*****************************************************************************
* int init();
*
* Return Value: Return 0 : init was successful.
* Return -1: In all other case.
* Remark: Initiates operation record after allocation.
*****************************************************************************/
int
NdbScanOperation::init(const NdbTableImpl* tab, NdbTransaction* myConnection)
{
m_transConnection = myConnection;
if (NdbOperation::init(tab, myConnection, false) != 0)
return -1;
theNdb->theRemainingStartTransactions++; // will be checked in hupp...
NdbTransaction* aScanConnection = theNdb->hupp(myConnection);
if (!aScanConnection){
theNdb->theRemainingStartTransactions--;
setErrorCodeAbort(theNdb->getNdbError().code);
return -1;
}
// NOTE! The hupped trans becomes the owner of the operation
theNdbCon= aScanConnection;
initInterpreter();
theStatus = GetValue;
theOperationType = OpenScanRequest;
theNdbCon->theMagicNumber = 0xFE11DF;
theNoOfTupKeyLeft = tab->m_noOfDistributionKeys;
m_ordered= false;
m_descending= false;
m_read_range_no = 0;
m_executed = false;
m_scanUsingOldApi= true;
m_readTuplesCalled= false;
m_interpretedCodeOldApi= NULL;
m_pruneState= SPS_UNKNOWN;
m_api_receivers_count = 0;
m_current_api_receiver = 0;
m_sent_receivers_count = 0;
m_conf_receivers_count = 0;
return 0;
}
int
NdbScanOperation::handleScanGetValuesOldApi()
{
/* Handle old API-defined scan getValue(s) */
assert(m_scanUsingOldApi);
if (theReceiver.theFirstRecAttr != NULL)
{
/* theReceiver has a list of RecAttrs which the user
* wants to read. Traverse it, adding signals to the
* request to read them, *similar* to extra GetValue
* handling, except that we want to use the RecAttrs we've
* already got.
* Once these are added to the signal train, all other handling
* is exactly the same as for normal NdbRecord 'extra GetValues'
*/
const NdbRecAttr* recAttrToRead = theReceiver.theFirstRecAttr;
while(recAttrToRead != NULL)
{
int res;
res= insertATTRINFOHdr_NdbRecord(recAttrToRead->theAttrId, 0);
if (unlikely(res == -1))
return -1;
recAttrToRead= recAttrToRead->next();
}
theInitialReadSize= theTotalCurrAI_Len - AttrInfo::SectionSizeInfoLength;
}
return 0;
}
/* Method for adding interpreted code signals to a
* scan operation request.
* Both main program words and subroutine words can
* be added in one method as scans do not use
* the final update or final read sections.
*/
int
NdbScanOperation::addInterpretedCode()
{
Uint32 mainProgramWords= 0;
Uint32 subroutineWords= 0;
const NdbInterpretedCode* code= m_interpreted_code;
/* Any disk access? */
if (code->m_flags & NdbInterpretedCode::UsesDisk)
{
m_flags &= ~Uint8(OF_NO_DISK);
}
/* Main program size depends on whether there's subroutines */
mainProgramWords= code->m_first_sub_instruction_pos ?
code->m_first_sub_instruction_pos :
code->m_instructions_length;
int res = insertATTRINFOData_NdbRecord((const char*)code->m_buffer,
mainProgramWords << 2);
if (res == 0)
{
/* Add subroutines, if we have any */
if (code->m_number_of_subs > 0)
{
assert(mainProgramWords > 0);
assert(code->m_first_sub_instruction_pos > 0);
Uint32 *subroutineStart=
&code->m_buffer[ code->m_first_sub_instruction_pos ];
subroutineWords=
code->m_instructions_length -
code->m_first_sub_instruction_pos;
res = insertATTRINFOData_NdbRecord((const char*) subroutineStart,
subroutineWords << 2);
}
/* Update signal section lengths */
theInterpretedSize= mainProgramWords;
theSubroutineSize= subroutineWords;
}
return res;
}
/* Method for handling scanoptions passed into
* NdbTransaction::scanTable or scanIndex
*/
int
NdbScanOperation::handleScanOptions(const ScanOptions *options)
{
/* Options size has already been checked.
* scan_flags, parallel and batch have been handled
* already (see NdbTransaction::scanTable and scanIndex)
*/
if ((options->optionsPresent & ScanOptions::SO_GETVALUE) &&
(options->numExtraGetValues > 0))
{
if (options->extraGetValues == NULL)
{
setErrorCodeAbort(4299);
/* Incorrect combination of ScanOption flags,
* extraGetValues ptr and numExtraGetValues */
return -1;
}
/* Add extra getValue()s */
for (unsigned int i=0; i < options->numExtraGetValues; i++)
{
NdbOperation::GetValueSpec *pvalSpec = &(options->extraGetValues[i]);
pvalSpec->recAttr=NULL;
if (pvalSpec->column == NULL)
{
setErrorCodeAbort(4295);
// Column is NULL in Get/SetValueSpec structure
return -1;
}
/* Call internal NdbRecord specific getValue() method
* Same method handles table scans and index scans
*/
NdbRecAttr *pra=
getValue_NdbRecord_scan(&NdbColumnImpl::getImpl(*pvalSpec->column),
(char *) pvalSpec->appStorage);
if (pra == NULL)
{
return -1;
}
pvalSpec->recAttr = pra;
}
}
if (options->optionsPresent & ScanOptions::SO_PARTITION_ID)
{
/* Should not have any blobs defined at this stage */
assert(theBlobList == NULL);
assert(m_pruneState == SPS_UNKNOWN);
/* Only allowed to set partition id for PK ops on UserDefined
* partitioned tables
*/
if(unlikely(! (m_attribute_record->flags &
NdbRecord::RecHasUserDefinedPartitioning)))
{
/* Explicit partitioning info not allowed for table and operation*/
setErrorCodeAbort(4546);
return -1;
}
m_pruneState= SPS_FIXED;
m_pruningKey= options->partitionId;
/* And set the vars in the operation now too */
theDistributionKey = options->partitionId;
theDistrKeyIndicator_ = 1;
assert((m_attribute_record->flags & NdbRecord::RecHasUserDefinedPartitioning) != 0);
DBUG_PRINT("info", ("NdbScanOperation::handleScanOptions(dist key): %u",
theDistributionKey));
}
if (options->optionsPresent & ScanOptions::SO_INTERPRETED)
{
/* Check the program's for the same table as the
* operation, within a major version number
* Perhaps NdbInterpretedCode should not contain the table
*/
const NdbDictionary::Table* codeTable=
options->interpretedCode->getTable();
if (codeTable != NULL)
{
NdbTableImpl* impl= &NdbTableImpl::getImpl(*codeTable);
if ((impl->m_id != (int) m_attribute_record->tableId) ||
(table_version_major(impl->m_version) !=
table_version_major(m_attribute_record->tableVersion)))
return 4524; // NdbInterpretedCode is for different table`
}
if ((options->interpretedCode->m_flags &
NdbInterpretedCode::Finalised) == 0)
{
setErrorCodeAbort(4519);
return -1; // NdbInterpretedCode::finalise() not called.
}
m_interpreted_code= options->interpretedCode;
}
/* User's operation 'tag' data. */
if (options->optionsPresent & ScanOptions::SO_CUSTOMDATA)
{
m_customData = options->customData;
}
/* Preferred form of partitioning information */
if (options->optionsPresent & ScanOptions::SO_PART_INFO)
{
Uint32 partValue;
Ndb::PartitionSpec tmpSpec;
const Ndb::PartitionSpec* pSpec= options->partitionInfo;
if (unlikely(validatePartInfoPtr(pSpec,
options->sizeOfPartInfo,
tmpSpec) ||
getPartValueFromInfo(pSpec,
m_currentTable,
&partValue)))
return -1;
assert(m_pruneState == SPS_UNKNOWN);
m_pruneState= SPS_FIXED;
m_pruningKey= partValue;
theDistributionKey= partValue;
theDistrKeyIndicator_= 1;
DBUG_PRINT("info", ("Set distribution key from partition spec to %u",
partValue));
}
return 0;
}
/**
* generatePackedReadAIs
* This method is adds AttrInfos to the current signal train to perform
* a packed read of the requested columns.
* It is used by table scan and index scan.
*/
int
NdbScanOperation::generatePackedReadAIs(const NdbRecord *result_record,
bool& haveBlob,
const Uint32 * m_read_mask)
{
Bitmask<MAXNROFATTRIBUTESINWORDS> readMask;
Uint32 columnCount= 0;
Uint32 maxAttrId= 0;
haveBlob= false;
for (Uint32 i= 0; i<result_record->noOfColumns; i++)
{
const NdbRecord::Attr *col= &result_record->columns[i];
Uint32 attrId= col->attrId;
assert(!(attrId & AttributeHeader::PSEUDO));
/* Skip column if result_mask says so and we don't need
* to read it
*/
if (!BitmaskImpl::get(MAXNROFATTRIBUTESINWORDS, m_read_mask, attrId))
continue;
/* Blob reads are handled with a getValue() in NdbBlob.cpp. */
if (unlikely(col->flags & NdbRecord::IsBlob))
{
m_keyInfo= 1; // Need keyinfo for blob scan
haveBlob= true;
continue;
}
if (col->flags & NdbRecord::IsDisk)
m_flags &= ~Uint8(OF_NO_DISK);
if (attrId > maxAttrId)
maxAttrId= attrId;
readMask.set(attrId);
columnCount++;
}
int result= 0;
/* Are there any columns to read via NdbRecord?
* Old Api scans, and new Api scans which only read via extra getvalues
* may have no 'NdbRecord reads'
*/
if (columnCount > 0)
{
bool all= (columnCount == m_currentTable->m_columns.size());
if (all)
result= insertATTRINFOHdr_NdbRecord(AttributeHeader::READ_ALL,
columnCount);
else
{
/* How many bitmask words are significant? */
Uint32 sigBitmaskWords= (maxAttrId>>5) + 1;
result= insertATTRINFOHdr_NdbRecord(AttributeHeader::READ_PACKED,
sigBitmaskWords << 2);
if (result != -1)
result= insertATTRINFOData_NdbRecord((const char*) &readMask.rep.data[0],
sigBitmaskWords << 2); // Bitmask
}
}
return result;
}
/**
* scanImpl
* This method is called by scanTableImpl() and scanIndexImpl() and
* performs most of the signal building tasks that both scan
* types share
*/
inline int
NdbScanOperation::scanImpl(const NdbScanOperation::ScanOptions *options,
const Uint32 * readMask)
{
bool haveBlob= false;
/* Add AttrInfos for packed read of cols in result_record */
if (generatePackedReadAIs(m_attribute_record, haveBlob, readMask) != 0)
return -1;
theInitialReadSize= theTotalCurrAI_Len - AttrInfo::SectionSizeInfoLength;
/* Handle any getValue() calls made against the old API. */
if (m_scanUsingOldApi)
{
if (handleScanGetValuesOldApi() !=0)
return -1;
}
/* Handle scan options - always for old style scan API */
if (options != NULL)
{
if (handleScanOptions(options) != 0)
return -1;
}
/* Get Blob handles unless this is an old Api scan op
* For old Api Scan ops, the Blob handles are already
* set up by the call to getBlobHandle()
*/
if (unlikely(haveBlob) && !m_scanUsingOldApi)
{
if (getBlobHandlesNdbRecord(m_transConnection, readMask) == -1)
return -1;
}
/* Add interpreted code words to ATTRINFO signal
* chain as necessary
*/
if (m_interpreted_code != NULL)
{
if (addInterpretedCode() == -1)
return -1;
}
/* Scan is now fully defined, so let's start preparing
* signals.
*/
if (prepareSendScan(theNdbCon->theTCConPtr,
theNdbCon->theTransactionId) == -1)
/* Error code should be set */
return -1;
return 0;
}
int
NdbScanOperation::handleScanOptionsVersion(const ScanOptions*& optionsPtr,
Uint32 sizeOfOptions,
ScanOptions& currOptions)
{
/* Handle different sized ScanOptions */
if (unlikely((sizeOfOptions !=0) &&
(sizeOfOptions != sizeof(ScanOptions))))
{
/* Different size passed, perhaps it's an old client */
if (sizeOfOptions == sizeof(ScanOptions_v1))
{
const ScanOptions_v1* oldOptions=
(const ScanOptions_v1*) optionsPtr;
/* v1 of ScanOptions, copy into current version
* structure and update options ptr
*/
currOptions.optionsPresent= oldOptions->optionsPresent;
currOptions.scan_flags= oldOptions->scan_flags;
currOptions.parallel= oldOptions->parallel;
currOptions.batch= oldOptions->batch;
currOptions.extraGetValues= oldOptions->extraGetValues;
currOptions.numExtraGetValues= oldOptions->numExtraGetValues;
currOptions.partitionId= oldOptions->partitionId;
currOptions.interpretedCode= oldOptions->interpretedCode;
currOptions.customData= oldOptions->customData;
/* New fields */
currOptions.partitionInfo= NULL;
currOptions.sizeOfPartInfo= 0;
optionsPtr= &currOptions;
}
else
{
/* No other versions supported currently */
setErrorCodeAbort(4298);
/* Invalid or unsupported ScanOptions structure */
return -1;
}
}
return 0;
}
int
NdbScanOperation::scanTableImpl(const NdbRecord *result_record,
NdbOperation::LockMode lock_mode,
const unsigned char *result_mask,
const NdbScanOperation::ScanOptions *options,
Uint32 sizeOfOptions)
{
int res;
Uint32 scan_flags = 0;
Uint32 parallel = 0;
Uint32 batch = 0;
ScanOptions currentOptions;
if (options != NULL)
{
if (handleScanOptionsVersion(options, sizeOfOptions, currentOptions))
return -1;
/* Process some initial ScanOptions - most are
* handled later
*/
if (options->optionsPresent & ScanOptions::SO_SCANFLAGS)
scan_flags = options->scan_flags;
if (options->optionsPresent & ScanOptions::SO_PARALLEL)
parallel = options->parallel;
if (options->optionsPresent & ScanOptions::SO_BATCH)
batch = options->batch;
}
#if 0 // ToDo: this breaks optimize index, but maybe there is a better solution
if (result_record->flags & NdbRecord::RecIsIndex)
{
setErrorCodeAbort(4340);
return -1;
}
#endif
m_attribute_record= result_record;
AttributeMask readMask;
m_attribute_record->copyMask(readMask.rep.data, result_mask);
/* Process scan definition info */
res= processTableScanDefs(lock_mode, scan_flags, parallel, batch);
if (res == -1)
return -1;
theStatus= NdbOperation::UseNdbRecord;
/* Call generic scan code */
return scanImpl(options, readMask.rep.data);
}
int
NdbScanOperation::getPartValueFromInfo(const Ndb::PartitionSpec* partInfo,
const NdbTableImpl* table,
Uint32* partValue)
{
switch(partInfo->type)
{
case Ndb::PartitionSpec::PS_USER_DEFINED:
{
assert(table->m_fragmentType == NdbDictionary::Object::UserDefined);
*partValue= partInfo->UserDefined.partitionId;
return 0;
}
case Ndb::PartitionSpec::PS_DISTR_KEY_PART_PTR:
{
assert(table->m_fragmentType != NdbDictionary::Object::UserDefined);
Uint32 hashVal;
int ret= Ndb::computeHash(&hashVal, table,
partInfo->KeyPartPtr.tableKeyParts,
partInfo->KeyPartPtr.xfrmbuf,
partInfo->KeyPartPtr.xfrmbuflen);
if (ret == 0)
{
/* We send the hash result here (rather than the partitionId
* generated by doing some function on the hash)
* Note that KEY and LINEAR KEY native partitioning hash->partitionId
* mapping functions are idempotent so that they can be
* applied multiple times to their result without changing it.
* DIH will apply them, so there's no need to also do it here in API,
* unless we want to see which physical partition we *think* will
* hold the values.
* Only possible advantage is that we could identify some locality
* not shown in the hash result. This is only *safe* for schemes
* which cannot change the hash->partitionId mapping function
* online.
* Can add as an optimisation if necessary.
*/
*partValue= hashVal;
return 0;
}
else
{
setErrorCodeAbort(ret);
return -1;
}
}
case Ndb::PartitionSpec::PS_DISTR_KEY_RECORD:
{
assert(table->m_fragmentType != NdbDictionary::Object::UserDefined);
Uint32 hashVal;
int ret= Ndb::computeHash(&hashVal,
partInfo->KeyRecord.keyRecord,
partInfo->KeyRecord.keyRow,
partInfo->KeyRecord.xfrmbuf,
partInfo->KeyRecord.xfrmbuflen);
if (ret == 0)
{
/* See comments above about sending hashResult rather than
* partitionId
*/
*partValue= hashVal;
return 0;
}
else
{
setErrorCodeAbort(ret);
return -1;
}
}
}
/* 4542 : Unknown partition information type */
setErrorCodeAbort(4542);
return -1;
}
/*
Compare two rows on some prefix of the index.
This is used to see if we can determine that all rows in an index range scan
will come from a single fragment (if the two rows bound a single distribution
key).
*/
static int
compare_index_row_prefix(const NdbRecord *rec,
const char *row1,
const char *row2,
Uint32 prefix_length)
{
Uint32 i;
if (row1 == row2) // Easy case with same ptrs
return 0;
for (i= 0; i<prefix_length; i++)
{
const NdbRecord::Attr *col= &rec->columns[rec->key_indexes[i]];
bool is_null1= col->is_null(row1);
bool is_null2= col->is_null(row2);
if (is_null1)
{
if (!is_null2)
return -1;
/* Fall-through to compare next one. */
}
else
{
if (is_null2)
return 1;
Uint32 offset= col->offset;
Uint32 maxSize= col->maxSize;
const char *ptr1= row1 + offset;
const char *ptr2= row2 + offset;
/* bug#56853 */
char buf1[NdbRecord::Attr::SHRINK_VARCHAR_BUFFSIZE];
char buf2[NdbRecord::Attr::SHRINK_VARCHAR_BUFFSIZE];
if (col->flags & NdbRecord::IsMysqldShrinkVarchar)
{
Uint32 len1;
bool ok1 = col->shrink_varchar(row1, len1, buf1);
assert(ok1);
ptr1 = buf1;
Uint32 len2;
bool ok2 = col->shrink_varchar(row2, len2, buf2);
assert(ok2);
ptr2 = buf2;
}
void *info= col->charset_info;
int res=
(*col->compare_function)(info, ptr1, maxSize, ptr2, maxSize);
if (res)
{
return res;
}
}
}
return 0;
}
int
NdbIndexScanOperation::getDistKeyFromRange(const NdbRecord *key_record,
const NdbRecord *result_record,
const char *row,
Uint32* distKey)
{
const Uint32 MaxKeySizeInLongWords= (NDB_MAX_KEY_SIZE + 7) / 8;
Uint64 tmp[ MaxKeySizeInLongWords ];
char* tmpshrink = (char*)tmp;
Uint32 tmplen = (Uint32)sizeof(tmp);
/* This can't work for User Defined partitioning */
assert(key_record->table->m_fragmentType !=
NdbDictionary::Object::UserDefined);
Ndb::Key_part_ptr ptrs[NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY+1];
Uint32 i;
for (i = 0; i<key_record->distkey_index_length; i++)
{
const NdbRecord::Attr *col =
&key_record->columns[key_record->distkey_indexes[i]];
if (col->flags & NdbRecord::IsMysqldShrinkVarchar)
{
if (tmplen >= 256)
{
Uint32 len;
bool len_ok = col->shrink_varchar(row, len, tmpshrink);
if (!len_ok)
{
/* 4209 : Length parameter in equal/setValue is incorrect */
setErrorCodeAbort(4209);
return -1;
}
ptrs[i].ptr = tmpshrink;
tmpshrink += len;
tmplen -= len;
}
else
{
/* 4207 : Key size is limited to 4092 bytes */
setErrorCodeAbort(4207);
return -1;
}
}
else
{
ptrs[i].ptr = row + col->offset;
}
ptrs[i].len = col->maxSize;
}
ptrs[i].ptr = 0;
Uint32 hashValue;
int ret = Ndb::computeHash(&hashValue, result_record->table,
ptrs, tmpshrink, tmplen);
if (ret == 0)
{
*distKey = hashValue;
return 0;
}
else
{
#ifdef VM_TRACE
ndbout << "err: " << ret << endl;
#endif
setErrorCodeAbort(ret);
return -1;
}
}
int
NdbScanOperation::validatePartInfoPtr(const Ndb::PartitionSpec*& partInfo,
Uint32 sizeOfPartInfo,
Ndb::PartitionSpec& tmpSpec)
{
if (unlikely(sizeOfPartInfo != sizeof(Ndb::PartitionSpec)))
{
if (sizeOfPartInfo == sizeof(Ndb::PartitionSpec_v1))
{
const Ndb::PartitionSpec_v1* oldPSpec=
(const Ndb::PartitionSpec_v1*) partInfo;
/* Let's upgrade to the latest variant */
tmpSpec.type= oldPSpec->type;
if (tmpSpec.type == Ndb::PartitionSpec_v1::PS_USER_DEFINED)
{
tmpSpec.UserDefined.partitionId= oldPSpec->UserDefined.partitionId;
}
else
{
tmpSpec.KeyPartPtr.tableKeyParts= oldPSpec->KeyPartPtr.tableKeyParts;
tmpSpec.KeyPartPtr.xfrmbuf= oldPSpec->KeyPartPtr.xfrmbuf;
tmpSpec.KeyPartPtr.xfrmbuflen= oldPSpec->KeyPartPtr.xfrmbuflen;
}
partInfo= &tmpSpec;
}
else
{
/* 4545 : Invalid or Unsupported PartitionInfo structure */
setErrorCodeAbort(4545);
return -1;
}
}
if (partInfo->type != Ndb::PartitionSpec::PS_NONE)
{
if (m_pruneState == SPS_FIXED)
{
/* 4543 : Duplicate partitioning information supplied */
setErrorCodeAbort(4543);
return -1;
}
if ((partInfo->type == Ndb::PartitionSpec::PS_USER_DEFINED) !=
((m_currentTable->m_fragmentType == NdbDictionary::Object::UserDefined)))
{
/* Mismatch between type of partitioning info supplied, and table's
* partitioning type
*/
/* 4544 : Wrong partitionInfo type for table */
setErrorCodeAbort(4544);
return -1;
}
}
else
{
/* PartInfo supplied, but set to NONE */
partInfo= NULL;
}
return 0;
}
int
NdbIndexScanOperation::setBound(const NdbRecord* key_record,
const IndexBound& bound)
{
return setBound(key_record, bound, NULL, 0);
}
/**
* setBound()
*
* This method is called from scanIndex() and setBound().
* It adds a bound to an Index Scan.
* It can be passed extra partitioning information.
*/
int
NdbIndexScanOperation::setBound(const NdbRecord *key_record,
const IndexBound& bound,
const Ndb::PartitionSpec* partInfo,
Uint32 sizeOfPartInfo)
{
if (unlikely((theStatus != NdbOperation::UseNdbRecord)))
{
setErrorCodeAbort(4284);
/* Cannot mix NdbRecAttr and NdbRecord methods in one operation */
return -1;
}
if (unlikely(key_record == NULL))
{
setErrorCodeAbort(4285);
/* NULL NdbRecord pointer */
return -1;
}
/* Has the user supplied an open range (no bounds)? */
const bool openRange= (((bound.low_key == NULL) &&
(bound.high_key == NULL)) ||
((bound.low_key_count == 0) &&
(bound.high_key_count == 0)));
/* Check the base table's partitioning scheme
* (Ordered index itself has 'undefined' fragmentation)
*/
bool tabHasUserDefPartitioning= (m_currentTable->m_fragmentType ==
NdbDictionary::Object::UserDefined);
/* Validate explicit partitioning info if it's supplied */
Ndb::PartitionSpec tmpSpec;
if (partInfo)
{
/* May update the PartInfo ptr */
if (validatePartInfoPtr(partInfo,
sizeOfPartInfo,
tmpSpec))
return -1;
}
m_num_bounds++;
if (unlikely((m_num_bounds > 1) &&
(m_multi_range == 0)))
{
/* > 1 IndexBound, but not MRR */
setErrorCodeAbort(4509);
/* Non SF_MultiRange scan cannot have more than one bound */
return -1;
}
Uint32 j;
Uint32 key_count, common_key_count;
Uint32 range_no;
Uint32 bound_head;
range_no= bound.range_no;
if (unlikely(range_no > MaxRangeNo))
{
setErrorCodeAbort(4286);
return -1;
}
/* Check valid ordering of supplied range numbers */
if ( m_read_range_no && m_ordered )
{
if (unlikely((m_num_bounds > 1) &&
(range_no <= m_previous_range_num)))
{
setErrorCodeAbort(4282);
/* range_no not strictly increasing in ordered multi-range index scan */
return -1;
}
m_previous_range_num= range_no;
}
key_count= bound.low_key_count;
common_key_count= key_count;
if (key_count < bound.high_key_count)
key_count= bound.high_key_count;
else
common_key_count= bound.high_key_count;
if (unlikely(key_count > key_record->key_index_length))
{
/* Too many keys specified for key bound. */
setErrorCodeAbort(4281);
return -1;
}
/* We need to get a ptr to the first word of this
* range so that we can set the total length of the range
* (and range num) at the end of writing out the range.
*/
Uint32* firstRangeWord= NULL;
const Uint32 keyLenBeforeRange= theTupKeyLen;
if (likely(!openRange))
{
/* If low and high key pointers are the same and key counts are
* the same, we send as an Eq bound to save bandwidth.
* This will not send an EQ bound if :
* - Different numbers of high and low keys are EQ
* - High and low keys are EQ, but use different ptrs
* This could be improved in future with another setBound() variant.
*/
const bool isEqRange=
(bound.low_key == bound.high_key) &&
(bound.low_key_count == bound.high_key_count) &&
(bound.low_inclusive && bound.high_inclusive); // Does this matter?
if (isEqRange)