forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathha_ndbcluster_push.cc
1711 lines (1525 loc) · 58.3 KB
/
ha_ndbcluster_push.cc
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) 2011, 2014, 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
*/
/**
@file
@brief
This file defines various classes and methods used for pushing queries
to the ndb data node (for execution by the SPJ block).
*/
#include "ha_ndbcluster_glue.h"
#include "ha_ndbcluster.h"
#include "ha_ndbcluster_push.h"
#include "ha_ndbcluster_binlog.h"
#include "ha_ndbcluster_cond.h"
#include "abstract_query_plan.h"
#include <ndbapi/NdbApi.hpp>
#include <ndbapi/NdbInterpretedCode.hpp>
#include "../storage/ndb/src/ndbapi/NdbQueryBuilder.hpp"
#include "../storage/ndb/src/ndbapi/NdbQueryOperation.hpp"
#include <ndb_version.h>
/*
Explain why an operation could not be pushed
@param[in] msgfmt printf style format string.
*/
#define EXPLAIN_NO_PUSH(msgfmt, ...) \
do \
{ \
if (unlikely(current_thd->lex->describe)) \
{ \
push_warning_printf(current_thd, \
Sql_condition::SL_NOTE, ER_YES, \
(msgfmt), __VA_ARGS__); \
} \
} \
while(0)
static inline const char* get_referred_field_name(const Item_field* field_item)
{
DBUG_ASSERT(field_item->type() == Item::FIELD_ITEM);
return field_item->field->field_name;
}
static const char* get_referred_table_access_name(const Item_field* field_item)
{
DBUG_ASSERT(field_item->type() == Item::FIELD_ITEM);
return field_item->field->table->alias;
}
static bool ndbcluster_is_lookup_operation(AQP::enum_access_type accessType)
{
return accessType == AQP::AT_PRIMARY_KEY ||
accessType == AQP::AT_MULTI_PRIMARY_KEY ||
accessType == AQP::AT_UNIQUE_KEY;
}
uint
ndb_table_access_map::first_table(uint start) const
{
for (uint table_no= start; table_no<length(); table_no++)
{
if (contain(table_no))
return table_no;
}
return length();
}
uint
ndb_table_access_map::last_table(uint start) const
{
uint table_no= start;
while(true)
{
if (contain(table_no))
return table_no;
else if (table_no == 0)
return length();
table_no--;
}
}
ndb_pushed_join::ndb_pushed_join(
const ndb_pushed_builder_ctx& builder,
const NdbQueryDef* query_def)
:
m_query_def(query_def),
m_operation_count(0),
m_field_count(builder.m_fld_refs)
{
DBUG_ASSERT(query_def != NULL);
DBUG_ASSERT(builder.m_fld_refs <= MAX_REFERRED_FIELDS);
ndb_table_access_map searched;
for (uint tab_no= 0; !(searched==builder.m_join_scope); tab_no++)
{
const AQP::Table_access* const join_tab= builder.m_plan.get_table_access(tab_no);
if (builder.m_join_scope.contain(tab_no))
{
DBUG_ASSERT(m_operation_count < MAX_PUSHED_OPERATIONS);
m_tables[m_operation_count++] = join_tab->get_table();
searched.add(tab_no);
}
}
for (uint i= 0; i < builder.m_fld_refs; i++)
{
m_referred_fields[i] = builder.m_referred_fields[i];
}
}
ndb_pushed_join::~ndb_pushed_join()
{
if (m_query_def)
m_query_def->destroy();
}
bool ndb_pushed_join::match_definition(
int type, //NdbQueryOperationDef::Type,
const NDB_INDEX_DATA* idx) const
{
const NdbQueryOperationDef* const root_operation=
m_query_def->getQueryOperation((uint)0);
const NdbQueryOperationDef::Type def_type=
root_operation->getType();
if (def_type != type)
{
DBUG_PRINT("info",
("Cannot execute push join. Root operation prepared as %s "
"not executable as %s",
NdbQueryOperationDef::getTypeName(def_type),
NdbQueryOperationDef::getTypeName((NdbQueryOperationDef::Type)type)));
return FALSE;
}
const NdbDictionary::Index* const expected_index= root_operation->getIndex();
// Check that we still use the same index as when the query was prepared.
switch (def_type)
{
case NdbQueryOperationDef::PrimaryKeyAccess:
DBUG_ASSERT(idx!=NULL);
DBUG_ASSERT(idx->unique_index == expected_index);
break;
case NdbQueryOperationDef::UniqueIndexAccess:
DBUG_ASSERT(idx!=NULL);
// DBUG_ASSERT(idx->unique_index == expected_index);
if (idx->unique_index != expected_index)
{
DBUG_PRINT("info", ("Actual index %s differs from expected index %s."
"Therefore, join cannot be pushed.",
idx->unique_index->getName(),
expected_index->getName()));
return FALSE;
}
break;
case NdbQueryOperationDef::TableScan:
DBUG_ASSERT (idx==NULL && expected_index==NULL);
break;
case NdbQueryOperationDef::OrderedIndexScan:
DBUG_ASSERT(idx!=NULL);
// DBUG_ASSERT(idx->index == expected_index);
if (idx->index != expected_index)
{
DBUG_PRINT("info", ("Actual index %s differs from expected index %s. "
"Therefore, join cannot be pushed.",
idx->index->getName(),
expected_index->getName()));
return FALSE;
}
break;
default:
DBUG_ASSERT(false);
break;
}
/**
* There may be referrences to Field values from tables outside the scope of
* our pushed join which are supplied as paramValues().
* If any of these are NULL values, join can't be pushed.
*/
for (uint i= 0; i < get_field_referrences_count(); i++)
{
Field* field= m_referred_fields[i];
if (field->is_real_null())
{
DBUG_PRINT("info",
("paramValue is NULL, can not execute as pushed join"));
return FALSE;
}
}
return TRUE;
}
NdbQuery* ndb_pushed_join::make_query_instance(
NdbTransaction* trans,
const NdbQueryParamValue* keyFieldParams,
uint paramCnt) const
{
DBUG_ENTER("make_query_instance");
DBUG_PRINT("info",
("executing chain of %d pushed joins."
" First table is %s, accessed as %s.",
get_operation_count(),
get_table(0)->alias,
NdbQueryOperationDef::getTypeName(
m_query_def->getQueryOperation((uint)0)->getType())
));
const NdbQueryParamValue* paramValues = keyFieldParams;
/**
* There may be referrences to Field values from tables outside the scope of
* our pushed join: These are expected to be supplied as paramValues()
* after the keyFieldParams[].
*/
uint outer_fields= get_field_referrences_count();
NdbQueryParamValue* extendedParams = NULL;
if (unlikely(outer_fields > 0))
{
uint size= sizeof(NdbQueryParamValue) * (paramCnt+outer_fields);
extendedParams = reinterpret_cast<NdbQueryParamValue*>(my_alloca(size));
// Copy specified keyFieldParams[] first
for (uint i= 0; i < paramCnt; i++)
{
new (extendedParams + i) NdbQueryParamValue(keyFieldParams[i]);
}
// There may be referrences to Field values from tables outside the scope of
// our pushed join: These are expected to be supplied as paramValues()
for (uint i= 0; i < outer_fields; i++)
{
Field* field= m_referred_fields[i];
DBUG_ASSERT(!field->is_real_null()); // Checked by ::check_if_pushable()
new (extendedParams + paramCnt + i) NdbQueryParamValue(field->ptr, false);
}
paramValues= extendedParams;
}
NdbQuery* query= trans->createQuery(&get_query_def(), paramValues);
if (unlikely(extendedParams != NULL))
{
for (uint i = 0; i < paramCnt + outer_fields; i++)
{
extendedParams[i].~NdbQueryParamValue();
}
}
DBUG_RETURN(query);
}
/////////////////////////////////////////
ndb_pushed_builder_ctx::ndb_pushed_builder_ctx(const AQP::Join_plan& plan)
:
m_plan(plan),
m_join_root(),
m_join_scope(),
m_const_scope(),
m_firstmatch_skipped(),
m_internal_op_count(0),
m_fld_refs(0),
m_builder(NULL)
{
const uint count= m_plan.get_access_count();
(void)ha_ndb_ext; // Prevents compiler warning.
DBUG_ASSERT(count <= MAX_TABLES);
if (count > 1)
{
for (uint i= 0; i < count; i++)
{
m_tables[i].m_maybe_pushable= 0;
const AQP::Table_access* const table = m_plan.get_table_access(i);
if (table->get_table()->s->db_type()->db_type != DB_TYPE_NDBCLUSTER)
{
DBUG_PRINT("info", ("Table '%s' not in ndb engine, not pushable",
table->get_table()->alias));
continue;
}
switch (table->get_access_type())
{
case AQP::AT_VOID:
DBUG_ASSERT(false);
break;
case AQP::AT_FIXED:
EXPLAIN_NO_PUSH("Table '%s' was optimized away, or const'ified by optimizer",
table->get_table()->alias);
break;
case AQP::AT_OTHER:
EXPLAIN_NO_PUSH("Table '%s' is not pushable: %s",
table->get_table()->alias,
table->get_other_access_reason());
break;
case AQP::AT_UNDECIDED:
EXPLAIN_NO_PUSH("Table '%s' is not pushable: "
"Access type was not chosen at 'prepare' time",
table->get_table()->alias);
break;
default:
const char* reason= NULL;
const ha_ndbcluster* handler=
static_cast<ha_ndbcluster*>(table->get_table()->file);
if (handler->maybe_pushable_join(reason))
{
m_tables[i].m_maybe_pushable= PUSHABLE_AS_CHILD | PUSHABLE_AS_PARENT;
}
else if (reason != NULL)
{
EXPLAIN_NO_PUSH("Table '%s' is not pushable: %s",
table->get_table()->alias, reason);
}
break;
} //switch
/**
* FirstMatch algorithm may skip further nested-loop evaluation
* if this, and possible a number of previous tables.
* Aggregate into the bitmap 'm_firstmatch_skipped' those tables
* which 'FirstMatch' usage may possible skip.
*/
const AQP::Table_access* const firstmatch_last_skipped=
table->get_firstmatch_last_skipped();
if (firstmatch_last_skipped)
{
const uint last_skipped_tab= firstmatch_last_skipped->get_access_no();
DBUG_ASSERT(last_skipped_tab <= i);
for (uint skip_tab= last_skipped_tab; skip_tab <= i; skip_tab++)
{
m_firstmatch_skipped.add(skip_tab);
}
}
} //for 'all tables'
m_tables[0].m_maybe_pushable &= ~PUSHABLE_AS_CHILD;
m_tables[count-1].m_maybe_pushable &= ~PUSHABLE_AS_PARENT;
// Fill in table for maping internal <-> external table enumeration
for (uint i= 0; i < count; i++)
{
const AQP::Table_access* const table = m_plan.get_table_access(i);
uint external= table->get_table()->pos_in_table_list->tableno();
DBUG_ASSERT(external <= MAX_TABLES);
m_remap[i].to_external= external;
m_remap[external].to_internal= i;
}
}
} // ndb_pushed_builder_ctx::ndb_pushed_builder_ctx()
ndb_pushed_builder_ctx::~ndb_pushed_builder_ctx()
{
if (m_builder)
{
m_builder->destroy();
}
}
const NdbError& ndb_pushed_builder_ctx::getNdbError() const
{
DBUG_ASSERT(m_builder);
return m_builder->getNdbError();
}
/**
* Get *internal* table_no of table referred by 'key_item'
*/
uint
ndb_pushed_builder_ctx::get_table_no(const Item* key_item) const
{
DBUG_ASSERT(key_item->type() == Item::FIELD_ITEM);
table_map bitmap= key_item->used_tables();
for (uint i= 0; i<MAX_TABLES && bitmap!=0; i++, bitmap>>=1)
{
if (bitmap & 1)
{
DBUG_ASSERT(bitmap == 0x01); // Only a single table in 'bitmap'
return m_remap[i].to_internal;
}
}
return MAX_TABLES;
}
/**
* Main entry point to build a pushed join having 'join_root'
* as it first operation.
*
* If the root operation is pushable, we append as many 'child'
* operations as possible to the pushed join.
*
* This currently is implemented as a 3 pass algorithm:
*
* 1) Analyze each child and add it to 'm_join_scope' as
* 'pushable' if it qualifies as such. Part of this phase
* is also calculations of possible parents for each table.
*
* 2) Determine the parent to be used among the set of possible
* parents. This is decided based on simple heuristic where
* the goal is to employ filters as soon as possible,
* reduce the fanout of intermediate results, and utilize
* the parallelism of the SPJ block whenever considdered optimal.
*
* 3) Build the pushed query.
*
*/
int
ndb_pushed_builder_ctx::make_pushed_join(
const AQP::Table_access* join_root,
const ndb_pushed_join* &pushed_join)
{
DBUG_ENTER("make_pushed_join");
pushed_join= NULL;
if (is_pushable_with_root(join_root))
{
int error;
error= optimize_query_plan();
if (unlikely(error))
DBUG_RETURN(error);
error= build_query();
if (unlikely(error))
DBUG_RETURN(error);
const NdbQueryDef* const query_def= m_builder->prepare();
if (unlikely(query_def == NULL))
DBUG_RETURN(-1); // Get error with ::getNdbError()
pushed_join= new ndb_pushed_join(*this, query_def);
if (unlikely (pushed_join == NULL))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
DBUG_PRINT("info", ("Created pushed join with %d child operations",
pushed_join->get_operation_count()-1));
}
DBUG_RETURN(0);
} // ndb_pushed_builder_ctx::make_pushed_join()
/**
* Find the number SPJ operations needed to execute a given access type.
* (Unique index lookups are translated to two single table lookups internally.)
*/
uint internal_operation_count(AQP::enum_access_type accessType)
{
switch (accessType)
{
case AQP::AT_PRIMARY_KEY:
case AQP::AT_ORDERED_INDEX_SCAN:
case AQP::AT_MULTI_PRIMARY_KEY:
case AQP::AT_MULTI_MIXED:
case AQP::AT_TABLE_SCAN:
return 1;
// Unique key lookups is mapped to two primary key lookups internally.
case AQP::AT_UNIQUE_KEY:
case AQP::AT_MULTI_UNIQUE_KEY:
return 2;
default:
// Other access types are not pushable, so seeing them here is an error.
DBUG_ASSERT(false);
return 2;
}
}
/**
* If there is a pushable query starting with 'root'; add as many
* child operations as possible to this 'ndb_pushed_builder_ctx' starting
* with that join_root.
*/
bool
ndb_pushed_builder_ctx::is_pushable_with_root(const AQP::Table_access* root)
{
DBUG_ENTER("is_pushable_with_root");
const uint root_no= root->get_access_no();
if ((m_tables[root_no].m_maybe_pushable & PUSHABLE_AS_PARENT) != PUSHABLE_AS_PARENT)
{
DBUG_PRINT("info", ("Table %d already reported 'not pushable_as_parent'", root_no));
DBUG_RETURN(false);
}
const AQP::enum_access_type access_type= root->get_access_type();
DBUG_ASSERT(access_type != AQP::AT_VOID);
if (access_type == AQP::AT_MULTI_UNIQUE_KEY)
{
EXPLAIN_NO_PUSH("Table '%s' is not pushable, "
"access type 'MULTI_UNIQUE_KEY' not implemented",
root->get_table()->alias);
m_tables[root_no].m_maybe_pushable &= ~PUSHABLE_AS_PARENT;
DBUG_RETURN(false);
}
if (root->filesort_before_join())
{
EXPLAIN_NO_PUSH("Table '%s' is not pushable, "
"need filesort before joining child tables",
root->get_table()->alias);
DBUG_RETURN(false);
}
/**
* Past this point we know at least root to be pushable as parent
* operation. Search remaining tables appendable if '::is_pushable_as_child()'
*/
DBUG_PRINT("info", ("Table %d is pushable as root", root->get_access_no()));
DBUG_EXECUTE("info", root->dbug_print(););
m_fld_refs= 0;
m_join_root= root;
m_const_scope.set_prefix(root_no);
m_join_scope= ndb_table_access_map(root_no);
m_internal_op_count = internal_operation_count(access_type);
uint push_cnt= 0;
for (uint tab_no= root->get_access_no()+1; tab_no<m_plan.get_access_count(); tab_no++)
{
const AQP::Table_access* const table= m_plan.get_table_access(tab_no);
if (is_pushable_as_child(table))
{
push_cnt++;
}
}
DBUG_RETURN(push_cnt>0);
} // ndb_pushed_builder_ctx::is_pushable_with_root()
/***************************************************************
* is_pushable_as_child()
*
* Determines if the specified child ('table') can be appended to
* an existing chain of previously pushed join operations.
*
* To be considdered pushable the child operation should:
*
* 1) Have an REF to the previous parent operations.
* 2) Refer only a single parent, or a grandparent reachable through
* a single parent common to all key fields in the 'REF'
*
* In order to increase pushability we use the COND_EQUAL sets
* to resolve cases (2) above) where multiple parents are refered.
* If needed too make a child pushable, we replace parent
* references with another from the COND_EQUAL sets which make
* it pushable .
****************************************************************/
bool
ndb_pushed_builder_ctx::is_pushable_as_child(
const AQP::Table_access* table)
{
DBUG_ENTER("is_pushable_as_child");
const uint root_no= m_join_root->get_access_no();
const uint tab_no= table->get_access_no();
DBUG_ASSERT(tab_no > root_no);
if ((m_tables[tab_no].m_maybe_pushable & PUSHABLE_AS_CHILD) != PUSHABLE_AS_CHILD)
{
DBUG_PRINT("info", ("Table %s already known 'not is_pushable_as_child'", table->get_table()->alias));
DBUG_RETURN(false);
}
const AQP::enum_access_type root_type= m_join_root->get_access_type();
const AQP::enum_access_type access_type= table->get_access_type();
if (!(ndbcluster_is_lookup_operation(access_type) ||
access_type==AQP::AT_ORDERED_INDEX_SCAN))
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child, 'type' must be a 'ref' access",
table->get_table()->alias);
m_tables[tab_no].m_maybe_pushable &= ~PUSHABLE_AS_CHILD;
DBUG_RETURN(false);
}
// Currently there is a limitation in not allowing LOOKUP - (index)SCAN operations
if (access_type==AQP::AT_ORDERED_INDEX_SCAN &&
ndbcluster_is_lookup_operation(root_type))
{
EXPLAIN_NO_PUSH("Push of table '%s' as scan-child "
"with lookup-root '%s' not implemented",
table->get_table()->alias,
m_join_root->get_table()->alias);
// 'table' may still be PUSHABLE_AS_CHILD with another parent
DBUG_RETURN(false);
}
if (table->get_no_of_key_fields() > ndb_pushed_join::MAX_LINKED_KEYS)
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child, "
"too many ref'ed parent fields",
table->get_table()->alias);
m_tables[tab_no].m_maybe_pushable &= ~PUSHABLE_AS_CHILD; // Permanently dissable
DBUG_RETURN(false);
}
for (uint i = tab_no; i > root_no; i--)
{
if (m_plan.get_table_access(i)->uses_join_cache())
{
EXPLAIN_NO_PUSH("Cannot push table '%s' as child of table '%s'. Doing so "
"would prevent using join buffer for table '%s'.",
table->get_table()->alias,
m_join_root->get_table()->alias,
m_plan.get_table_access(i)->get_table()->alias);
DBUG_RETURN(false);
}
}
// Check that we do not exceed the max number of pushable operations.
const uint internal_ops_needed = internal_operation_count(access_type);
if (unlikely(m_internal_op_count + internal_ops_needed
> NDB_SPJ_MAX_TREE_NODES))
{
EXPLAIN_NO_PUSH("Cannot push table '%s' as child of '%s'. Max number"
" of pushable tables exceeded.",
table->get_table()->alias,
m_join_root->get_table()->alias);
DBUG_RETURN(false);
}
m_internal_op_count += internal_ops_needed;
DBUG_PRINT("info", ("Table:%d, Checking %d REF keys", tab_no,
table->get_no_of_key_fields()));
/*****
* Calculate the set of possible parents for table, where:
* - 'current' are those currently being referred by the
* FIELD_ITEMs as set up by the MySQL optimizer.
* - 'common' are those we may refer (possibly through the EQ-sets)
* such that all FIELD_ITEMs are from the same parent.
* - 'extended' are those parents refered from some of the
* FIELD_ITEMs, and having the rest of the referred FIELD_ITEM
* tables available as 'grandparent refs'
* (The SPJ block can handle field references to any ancestor
* operation, not just the (direct) parent).
*
* In addition there are firm dependencies between some parents
* such that all 'depend_parents' must be referred as an ancestors
* of the table. By default 'depend_parents' will at least contain
* the most 'grandparent' of the extended parents.
*
****/
ndb_table_access_map current_parents;
ndb_table_access_map common_parents(m_join_scope);
ndb_table_access_map extend_parents;
ndb_table_access_map depend_parents;
for (uint key_part_no= 0;
key_part_no < table->get_no_of_key_fields();
key_part_no++)
{
const Item* const key_item= table->get_key_field(key_part_no);
const KEY_PART_INFO* key_part= table->get_key_part_info(key_part_no);
if (key_item->const_item()) // REF is a litteral or field from const-table
{
DBUG_PRINT("info", (" Item type:%d is 'const_item'", key_item->type()));
if (!is_const_item_pushable(key_item, key_part))
{
DBUG_RETURN(false);
}
}
else if (key_item->type() == Item::FIELD_ITEM)
{
/**
* Calculate all parents FIELD_ITEM may refer - Including those
* available through usage of equality sets.
*/
ndb_table_access_map field_parents;
if (!is_field_item_pushable(table, key_item, key_part, field_parents))
{
DBUG_RETURN(false);
}
/**
* Calculate 'current_parents' as the set of tables
* currently being referred by some 'key_item'.
*/
DBUG_ASSERT(key_item == table->get_key_field(key_part_no));
DBUG_ASSERT(key_item->type() == Item::FIELD_ITEM);
current_parents.add(get_table_no(key_item));
/**
* Calculate 'common_parents' as the set of possible 'field_parents'
* available from all 'key_part'.
*/
common_parents.intersect(field_parents);
/**
* 'Extended' parents are refered from some 'FIELD_ITEM', and contain
* all parents directly referred, or available as 'depend_parents'.
* The later excludes those before the first (grand-)parent
* available from all 'field_parents' (first_grandparent).
* However, it also introduce a dependency of those
* tables to really be available as grand parents.
*/
extend_parents.add(field_parents);
const uint first= field_parents.first_table(root_no);
depend_parents.add(first);
}
else
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child, "
"column '%s' does neither 'ref' a column nor a constant",
table->get_table()->alias,
key_part->field->field_name);
m_tables[tab_no].m_maybe_pushable &= ~PUSHABLE_AS_CHILD; // Permanently disable as child
DBUG_RETURN(false);
}
} // for (uint key_part_no= 0 ...
if (m_const_scope.contain(current_parents))
{
// NOTE: This is a constant table wrt. this instance of the pushed join.
// It should be relatively simple to extend the SPJ block to
// allow such tables to be included in the pushed join.
EXPLAIN_NO_PUSH("Can't push table '%s' as child of '%s', "
"their dependency is 'const'",
table->get_table()->alias,
m_join_root->get_table()->alias);
DBUG_RETURN(false);
}
else if (extend_parents.is_clear_all())
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child of '%s', "
"no parents found within scope",
table->get_table()->alias,
m_join_root->get_table()->alias);
DBUG_RETURN(false);
}
if (!ndbcluster_is_lookup_operation(table->get_access_type()))
{
/**
* Outer joining scan-scan is not supported, due to the following problem:
* Consider the query:
*
* select * from t1 left join t2
* on t1.attr=t2.ordered_index
* where predicate(t1.row, t2. row);
*
* Where 'predicate' cannot be pushed to the ndb. The ndb api may then
* return:
* +---------+---------+
* | t1.row1 | t2.row1 | (First batch)
* | t1.row2 | t2.row1 |
* ..... (NextReq).....
* | t1.row1 | t2.row2 | (Next batch)
* +---------+---------+
* Now assume that all rows but [t1.row1, t2.row1] satisfies 'predicate'.
* mysqld would be confused since the rows are not grouped on t1 values.
* It would therefor generate a NULL row such that it returns:
* +---------+---------+
* | t1.row1 | NULL | -> Error!
* | t1.row2 | t2.row1 |
* | t1.row1 | t2.row2 |
* +---------+---------+
*
* (Outer joining with scan may be indirect through lookup operations
* inbetween)
*/
if (table->get_join_type(m_join_root) == AQP::JT_OUTER_JOIN)
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child of '%s', "
"outer join of scan-child not implemented",
table->get_table()->alias,
m_join_root->get_table()->alias);
DBUG_RETURN(false);
}
/**
* 'FirstMatch' is not allowed to skip over a scan-child.
* The reason is similar to the outer joined scan-scan above:
*
* Scan-scan result may return the same ancestor-scan rowset
* multiple times when rowset from child scan has to be fetched
* in multiple batches (as above).
*
* When a 'FirstMatch' skip remaining rows in a scan-child,
* the Nested Loop (NL) will also advance to the next ancestor
* row. However, due to child scan requiring multiple batches
* the same ancestor row will reappear in the next batch!
*/
if (m_firstmatch_skipped.contain(tab_no))
{
EXPLAIN_NO_PUSH("Can't push table '%s' as child of '%s', "
"'FirstMatch' not allowed to contain scan-child",
table->get_table()->alias,
m_join_root->get_table()->alias);
m_tables[tab_no].m_maybe_pushable &= ~PUSHABLE_AS_CHILD; // Permanently dissable
DBUG_RETURN(false);
}
/**
* Note, for both 'outer join' and 'FirstMatch' restriction above:
* The restriction could have been lifted if we could
* somehow ensure that all rows from a child scan are fetched
* before we move to the next ancestor row.
*/
} // scan operation
/**
* Check outer join restrictions if multiple 'depend_parents':
*
* If this table has multiple dependencies, it can only be added to
* the set of pushed tables if the dependent tables themself
* depends, or could be make dependent, on each other.
*
* Such new dependencies can only be added iff all 'depend_parents'
* are in the same 'inner join nest', i.e. we can not add *new*
* dependencies on outer joined tables.
* Any existing explicit specified outer joins are allowed though.
* Another way to view this is that the explained query plan
* should have no outer joins inbetween the table and the tables
* it joins with.
*
* Algorithm:
* 1. Calculate a single 'common ancestor' for all dependent tables
* which is the closest ancestor which they all depends on.
* (directly or indirectly through other dependencies.)
*
* 2. For all ancestors in the 'depend_parents' set:
* If none of the children of this ancestor are already 'joined'
* with this ancestor, (eiter directly or indirectly) it need
* to be in an inner join relation with our common ancestor.
*/
DBUG_ASSERT(!depend_parents.is_clear_all());
DBUG_ASSERT(!depend_parents.contain(tab_no)); // Circular dependency!
ndb_table_access_map dependencies(depend_parents);
/**
* Calculate the single 'common ancestor' of all 'depend_parents'.
* Iterating all directly, and indirectly, 'depend_parents'
* until a single dependent ancestor remains.
*/
uint common_ancestor_no= tab_no;
while (true)
{
common_ancestor_no= dependencies.last_table(common_ancestor_no-1);
dependencies.clear_bit(common_ancestor_no);
if (dependencies.is_clear_all())
break;
const ndb_table_access_map &ancestor_dependencies=
m_tables[common_ancestor_no].m_depend_parents;
const uint first_ancestor=
ancestor_dependencies.last_table(common_ancestor_no-1);
dependencies.add(first_ancestor);
} //while
const AQP::Table_access* const common_ancestor=
m_plan.get_table_access(common_ancestor_no);
/**
* Check that no dependencies on outer joined 'common ancestor'
* need to be added in order to allow this new child to be joined.
*/
ndb_table_access_map child_dependencies;
dependencies= depend_parents;
for (uint ancestor_no= dependencies.last_table(tab_no-1);
ancestor_no!= common_ancestor_no;
ancestor_no= dependencies.last_table(ancestor_no-1))
{
const AQP::Table_access* const ancestor=
m_plan.get_table_access(ancestor_no);
/**
* If there are children of this ancestor which depends on it,
* (i.e 'joins with it') then this ancestor can only be added to our
* 'join nest' if it is inner joined with our common_ancestor.
*/
if (depend_parents.contain(ancestor_no) &&
ancestor->get_join_type(common_ancestor) == AQP::JT_OUTER_JOIN)
{
/**
* Found an outer joined ancestor which none of my parents
* can depend / join with:
*/
if (child_dependencies.is_clear_all())
{
/**
* As this was the last (outer joined) 'depend_parents',
* with no other mandatory dependencies, this table may still
* be added to the pushed join.
* However, it may contain 'common' & 'extend' parent candidates
* which may now be joined with this outer joined ancestor.
* These are removed below.
*/
DBUG_ASSERT(extend_parents.contain(common_parents));
for (uint parent_no= extend_parents.last_table(tab_no-1);
parent_no > ancestor_no;
parent_no= extend_parents.last_table(parent_no-1))
{
if (!m_tables[parent_no].m_depend_parents.contain(ancestor_no))
{
common_parents.clear_bit(parent_no);
extend_parents.clear_bit(parent_no);
}
}
DBUG_ASSERT(!extend_parents.is_clear_all());
}
else if (!child_dependencies.contain(ancestor_no))
{
/**
* No child of this ancestor depends (joins) with this ancestor,
* and adding it as a 'depend_parent' would introduce new
* dependencies on outer joined grandparents.
* We will not be allowed to add this table to the pushed join.
*/
EXPLAIN_NO_PUSH("Can't push table '%s' as child of '%s', "
"as it would introduce a dependency on "
"outer joined grandparent '%s'",
table->get_table()->alias,
m_join_root->get_table()->alias,
ancestor->get_table()->alias);
DBUG_RETURN(false);
}
}
// Aggregate dependency sets
child_dependencies.add(m_tables[ancestor_no].m_depend_parents);
dependencies.add(m_tables[ancestor_no].m_depend_parents);
} //for
DBUG_ASSERT(m_join_scope.contain(common_parents));
DBUG_ASSERT(m_join_scope.contain(extend_parents));
DBUG_ASSERT(extend_parents.is_clear_all() ||
extend_parents.contain(common_parents));
/**
* Register calculated parent sets - ::optimize() choose from these.
*/
m_tables[tab_no].m_common_parents= common_parents;
m_tables[tab_no].m_extend_parents= extend_parents;
m_tables[tab_no].m_depend_parents= depend_parents;
m_tables[tab_no].m_parent= MAX_TABLES;
m_tables[tab_no].m_maybe_pushable= 0; // Exclude from further pushing
m_join_scope.add(tab_no);
DBUG_RETURN(true);
} // ndb_pushed_builder_ctx::is_pushable_as_child
/*********************
* This method examines a key item (could be part of a lookup key or a scan
* bound) for a table access operation and calculates the set of possible
* parents. (These are possible parent table access operations in the query
* tree that will be pushed to the ndb.)
*
* @param[in] table The table access operation to which the key item belongs.
* @param[in] key_item The key_item to examine
* @param[in] key_part Metatdata about the key item.
* @param[out] field_parents The set of possible parents for 'key_item'
* ('join_root' if keys are constant).
* @return True if at least one possible parent was found. (False means that
* operation cannot be pushed).
*/
bool ndb_pushed_builder_ctx::is_field_item_pushable(
const AQP::Table_access* table,
const Item* key_item,
const KEY_PART_INFO* key_part,
ndb_table_access_map& field_parents)
{
DBUG_ENTER("is_field_item_pushable()");
const uint tab_no = table->get_access_no();