summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/qe/JoinedQueryExecutor.java
blob: 11a08fb8891307470b5f7b0352ca4e0568cee8fe (plain)
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
/*
 * Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado.qe;

import java.io.IOException;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.cojen.classfile.ClassFile;
import org.cojen.classfile.CodeBuilder;
import org.cojen.classfile.LocalVariable;
import org.cojen.classfile.MethodInfo;
import org.cojen.classfile.Modifiers;
import org.cojen.classfile.TypeDesc;

import org.cojen.util.ClassInjector;

import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Query;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.cursor.MultiTransformedCursor;

import com.amazon.carbonado.filter.Filter;
import com.amazon.carbonado.filter.FilterValues;
import com.amazon.carbonado.filter.RelOp;

import com.amazon.carbonado.info.ChainedProperty;
import com.amazon.carbonado.info.OrderedProperty;
import com.amazon.carbonado.info.StorableIndex;
import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.info.StorableProperty;

import com.amazon.carbonado.util.QuickConstructorGenerator;
import com.amazon.carbonado.util.SoftValuedCache;

import com.amazon.carbonado.gen.CodeBuilderUtil;

/**
 * QueryExecutor which joins a <i>source</i> and <i>target</i> executor,
 * producing results of target type. The source executor is called once per
 * fetch (outer loop), but the target executor is called once per source result
 * (inner loop).
 *
 * @author Brian S O'Neill
 * @param <S> source type
 * @param <T> target type
 */
public class JoinedQueryExecutor<S extends Storable, T extends Storable>
    extends AbstractQueryExecutor<T>
{
    /**
     * Builds and returns a complex joined excutor against a chained property,
     * supporting multi-way joins. Filtering and ordering may also be supplied,
     * in order to better distribute work throughout the join.
     *
     * @param repoAccess used to create query executors for outer and inner loops
     * @param targetToSourceProperty join property of <i>target</i> type which maps
     * to instances of <i>source</i> type
     * @param targetFilter optional filter for fetching <i>target</i> instances
     * @param targetOrdering optional ordering to apply to <i>target</i> executor
     & @param hints optional hints
     * @throws IllegalArgumentException if any parameter is null or if join
     * property is not a Storable type
     * @throws RepositoryException from RepositoryAccess
     */
    public static <T extends Storable> QueryExecutor<T>
        build(RepositoryAccess repoAccess,
              ChainedProperty<T> targetToSourceProperty,
              Filter<T> targetFilter,
              OrderingList<T> targetOrdering,
              QueryHints hints)
        throws RepositoryException
    {
        if (targetOrdering == null) {
            targetOrdering = OrderingList.emptyList();
        }

        QueryExecutor<T> executor =
            buildJoin(repoAccess, targetToSourceProperty, targetFilter, targetOrdering, hints);

        OrderingList<T> handledOrdering = executor.getOrdering();

        // Apply sort if any remaining ordering properties.
        int handledCount = commonOrderingCount(handledOrdering, targetOrdering);

        OrderingList<T> remainderOrdering =
            targetOrdering.subList(handledCount, targetOrdering.size());

        if (remainderOrdering.size() > 0) {
            SortedQueryExecutor.Support<T> support = repoAccess
                .storageAccessFor(targetToSourceProperty.getPrimeProperty().getEnclosingType());
            executor = new SortedQueryExecutor<T>
                (support, executor, handledOrdering, remainderOrdering);
        }

        return executor;
    }

    private static <T extends Storable> JoinedQueryExecutor<?, T>
        buildJoin(RepositoryAccess repoAccess,
                  ChainedProperty<T> targetToSourceProperty,
                  Filter<T> targetFilter,
                  OrderingList<T> targetOrdering,
                  QueryHints hints)
        throws RepositoryException
    {
        StorableProperty<T> primeTarget = targetToSourceProperty.getPrimeProperty();

        Filter tailFilter;
        if (targetFilter == null) {
            tailFilter = null;
        } else {
            Filter<T>.NotJoined nj = targetFilter.notJoinedFrom(ChainedProperty.get(primeTarget));
            tailFilter = nj.getNotJoinedFilter();
            targetFilter = nj.getRemainderFilter();
        }

        // Determine the most ordering properties the source (outer loop
        // executor) can provide. It may use less if its selected index does
        // not provide the ordering for free.
        final OrderingList outerLoopOrdering = mostOrdering(primeTarget, targetOrdering);

        QueryExecutor outerLoopExecutor;
        if (targetToSourceProperty.getChainCount() > 0) {
            ChainedProperty tailProperty = targetToSourceProperty.tail();
            outerLoopExecutor = buildJoin
                (repoAccess, tailProperty, tailFilter, outerLoopOrdering, hints);
        } else {
            Class sourceType = targetToSourceProperty.getType();

            if (!Storable.class.isAssignableFrom(sourceType)) {
                throw new IllegalArgumentException
                    ("Property type is not a Storable: " + targetToSourceProperty);
            }

            StorageAccess sourceAccess = repoAccess.storageAccessFor(sourceType);

            OrderingList expectedOrdering =
                expectedOrdering(sourceAccess, tailFilter, outerLoopOrdering);

            QueryExecutorFactory outerLoopExecutorFactory = sourceAccess.getQueryExecutorFactory();

            outerLoopExecutor = outerLoopExecutorFactory
                .executor(tailFilter, expectedOrdering, hints);
        }

        if (targetOrdering.size() > 0) {
            // If outer loop handles some of the ordering, then it can be
            // removed from the target ordering. This simplifies or eliminates
            // a final sort operation.

            int handledCount =
                commonOrderingCount(outerLoopExecutor.getOrdering(), outerLoopOrdering);

            targetOrdering = targetOrdering.subList(handledCount, targetOrdering.size());
        }

        Class<T> targetType = primeTarget.getEnclosingType();
        StorageAccess<T> targetAccess = repoAccess.storageAccessFor(targetType);
        
        QueryExecutorFactory<T> innerLoopExecutorFactory = targetAccess.getQueryExecutorFactory();

        return new JoinedQueryExecutor<Storable, T>(outerLoopExecutor,
                                                    innerLoopExecutorFactory,
                                                    primeTarget,
                                                    targetFilter,
                                                    targetOrdering,
                                                    targetAccess);
    }

    private static final String INNER_LOOP_EX_FIELD_NAME = "innerLoopExecutor";
    private static final String INNER_LOOP_FV_FIELD_NAME = "innerLoopFilterValues";
    private static final String INNER_LOOP_CONTROLLER_FIELD_NAME = "innerLoopController";
    private static final String ACTIVE_SOURCE_FIELD_NAME = "active";

    private static final SoftValuedCache<StorableProperty, Class> cJoinerCursorClassCache;

    static {
        cJoinerCursorClassCache = SoftValuedCache.newCache(11);
    }

    private static synchronized <S, T extends Storable> Joiner.Factory<S, T>
        getJoinerFactory(StorableProperty<T> targetToSourceProperty)
    {
        Class clazz = cJoinerCursorClassCache.get(targetToSourceProperty);

        if (clazz == null) {
            clazz = generateJoinerCursor(targetToSourceProperty);
            cJoinerCursorClassCache.put(targetToSourceProperty, clazz);
        }

        return (Joiner.Factory<S, T>) QuickConstructorGenerator
            .getInstance(clazz, Joiner.Factory.class);
    }

    private static <T extends Storable> Class<Cursor<T>>
        generateJoinerCursor(StorableProperty<T> targetToSourceProperty)
    {
        final Class<?> sourceType = targetToSourceProperty.getType();
        final Class<T> targetType = targetToSourceProperty.getEnclosingType();

        String packageName;
        {
            String name = targetType.getName();
            int index = name.lastIndexOf('.');
            if (index >= 0) {
                packageName = name.substring(0, index);
            } else {
                packageName = "";
            }
        }

        ClassLoader loader = targetType.getClassLoader();

        ClassInjector ci = ClassInjector.create(packageName + ".JoinedCursor", loader);
        ClassFile cf = new ClassFile(ci.getClassName(), MultiTransformedCursor.class);
        cf.markSynthetic();
        cf.setSourceFile(JoinedQueryExecutor.class.getName());
        cf.setTarget("1.5");

        final TypeDesc cursorType = TypeDesc.forClass(Cursor.class);
        final TypeDesc queryExecutorType = TypeDesc.forClass(QueryExecutor.class);
        final TypeDesc filterValuesType = TypeDesc.forClass(FilterValues.class);
        final TypeDesc controllerType = TypeDesc.forClass(Query.Controller.class);

        // Define fields for inner loop executor and filter values, which are
        // passed into the constructor.
        cf.addField(Modifiers.PRIVATE.toFinal(true), INNER_LOOP_EX_FIELD_NAME, queryExecutorType);
        cf.addField(Modifiers.PRIVATE.toFinal(true), INNER_LOOP_FV_FIELD_NAME, filterValuesType);
        cf.addField(Modifiers.PRIVATE.toFinal(true),
                    INNER_LOOP_CONTROLLER_FIELD_NAME, controllerType);

        // If target storable can set a reference to the joined source
        // storable, then stash a copy of it as we go. This way, when user of
        // target storable accesses the joined property, it costs nothing.
        boolean canSetSourceReference = targetToSourceProperty.getWriteMethod() != null;

        if (canSetSourceReference) {
            // Field to hold active source storable.
            cf.addField(Modifiers.PRIVATE, ACTIVE_SOURCE_FIELD_NAME,
                        TypeDesc.forClass(sourceType));
        }

        // Define constructor.
        {
            TypeDesc[] params = {cursorType, queryExecutorType, filterValuesType, controllerType};

            MethodInfo mi = cf.addConstructor(Modifiers.PUBLIC, params);
            CodeBuilder b = new CodeBuilder(mi);

            b.loadThis();
            b.loadLocal(b.getParameter(0)); // pass source cursor to superclass
            b.invokeSuperConstructor(new TypeDesc[] {cursorType});

            b.loadThis();
            b.loadLocal(b.getParameter(1));
            b.storeField(INNER_LOOP_EX_FIELD_NAME, queryExecutorType);

            b.loadThis();
            b.loadLocal(b.getParameter(2));
            b.storeField(INNER_LOOP_FV_FIELD_NAME, filterValuesType);

            b.loadThis();
            b.loadLocal(b.getParameter(3));
            b.storeField(INNER_LOOP_CONTROLLER_FIELD_NAME, controllerType);

            b.returnVoid();
        }

        // Implement the transform method.
        {
            MethodInfo mi = cf.addMethod(Modifiers.PROTECTED, "transform", cursorType,
                                         new TypeDesc[] {TypeDesc.OBJECT});
            mi.addException(TypeDesc.forClass(FetchException.class));
            CodeBuilder b = new CodeBuilder(mi);

            LocalVariable sourceVar = b.createLocalVariable(null, TypeDesc.forClass(sourceType));
            b.loadLocal(b.getParameter(0));
            b.checkCast(TypeDesc.forClass(sourceType));
            b.storeLocal(sourceVar);

            if (canSetSourceReference) {
                b.loadThis();
                b.loadLocal(sourceVar);
                b.storeField(ACTIVE_SOURCE_FIELD_NAME, TypeDesc.forClass(sourceType));
            }

            // Prepare to call fetch on innerLoopExecutor.
            b.loadThis();
            b.loadField(INNER_LOOP_EX_FIELD_NAME, queryExecutorType);

            // Fill in values for innerLoopFilterValues.
            b.loadThis();
            b.loadField(INNER_LOOP_FV_FIELD_NAME, filterValuesType);

            int propCount = targetToSourceProperty.getJoinElementCount();
            for (int i=0; i<propCount; i++) {
                StorableProperty<?> external = targetToSourceProperty.getExternalJoinElement(i);
                b.loadLocal(sourceVar);
                b.invoke(external.getReadMethod());

                TypeDesc bindType = CodeBuilderUtil.bindQueryParam(external.getType());
                CodeBuilderUtil.convertValue(b, external.getType(), bindType.toClass());
                b.invokeVirtual(filterValuesType, "with", filterValuesType,
                                new TypeDesc[] {bindType});
            }

            b.loadThis();
            b.loadField(INNER_LOOP_CONTROLLER_FIELD_NAME, controllerType);

            // Now fetch and return.
            b.invokeInterface(queryExecutorType, "fetch", cursorType,
                              new TypeDesc[] {filterValuesType, controllerType});
            b.returnValue(cursorType);
        }

        if (canSetSourceReference) {
            // Override the "next" method to set source object on target.
            MethodInfo mi = cf.addMethod(Modifiers.PUBLIC, "next", TypeDesc.OBJECT, null);
            mi.addException(TypeDesc.forClass(FetchException.class));
            CodeBuilder b = new CodeBuilder(mi);

            b.loadThis();
            b.invokeSuper(TypeDesc.forClass(MultiTransformedCursor.class),
                          "next", TypeDesc.OBJECT, null);
            b.checkCast(TypeDesc.forClass(targetType));
            b.dup();

            b.loadThis();
            b.loadField(ACTIVE_SOURCE_FIELD_NAME, TypeDesc.forClass(sourceType));
            b.invoke(targetToSourceProperty.getWriteMethod());

            b.returnValue(TypeDesc.OBJECT);
        }

        return (Class<Cursor<T>>) ci.defineClass(cf);
    }

    private static <S extends Storable, T extends Storable> OrderingList<T>
        transformOrdering(Class<T> targetType,
                          String targetToSourceProperty,
                          QueryExecutor<S> sourceExecutor)
    {
        OrderingList<T> targetOrdering = OrderingList.emptyList();
        StorableInfo<T> targetInfo = StorableIntrospector.examine(targetType);

        for (OrderedProperty<S> sourceProp : sourceExecutor.getOrdering()) {
            String targetName = targetToSourceProperty + '.' + sourceProp.getChainedProperty();
            OrderedProperty<T> targetProp = OrderedProperty
                .get(ChainedProperty.parse(targetInfo, targetName), sourceProp.getDirection());
            targetOrdering = targetOrdering.concat(targetProp);
        }

        return targetOrdering;
    }

    /**
     * Given a list of chained ordering properties, returns the properties
     * stripped of the matching chain prefix for the targetToSourceProperty. As
     * the target ordering is scanned left-to-right, if any property is found
     * which doesn't match the targetToSourceProperty, the building of the new
     * list stops. In other words, it returns a consecutive run of matching
     * properties.
     */
    private static <T extends Storable> OrderingList
        mostOrdering(StorableProperty<T> primeTarget, OrderingList<T> targetOrdering)
    {
        OrderingList handledOrdering = OrderingList.emptyList();
        for (OrderedProperty<T> targetProp : targetOrdering) {
            ChainedProperty<T> chainedProp = targetProp.getChainedProperty();
            if (chainedProp.getPrimeProperty().equals(primeTarget)) {
                handledOrdering = handledOrdering
                    // I hate Java generics. Note the stupid cast. I have finally
                    // realized the core problem: the wildcard model is broken.
                    .concat(OrderedProperty
                            .get((ChainedProperty) chainedProp.tail(),
                                 targetProp.getDirection()));
            } else {
                break;
            }
        }

        return handledOrdering;
    }

    /**
     * Examines the given ordering against available indexes, returning the
     * ordering that the best index can provide for free.
     */
    private static <T extends Storable> OrderingList<T>
        expectedOrdering(StorageAccess<T> access, Filter<T> filter, OrderingList<T> ordering)
    {
        List<Filter<T>> split;
        if (filter == null) {
            split = Filter.getOpenFilter(access.getStorableType()).disjunctiveNormalFormSplit();
        } else {
            split = filter.disjunctiveNormalFormSplit();
        }

        Comparator comparator = CompositeScore.fullComparator();

        CompositeScore bestScore = null;
        for (StorableIndex<T> index : access.getAllIndexes()) {
            for (Filter<T> sub : split) {
                CompositeScore candidateScore = CompositeScore.evaluate(index, sub, ordering);
                if (bestScore == null || comparator.compare(candidateScore, bestScore) < 0) {
                    bestScore = candidateScore;
                }
            }
        }

        // Reduce source ordering to that which can be handled for
        // free. Otherwise, a sort would be performed which is a waste of time
        // if some source results will later be filtered out.
        int handledCount = bestScore == null ? 0 : bestScore.getOrderingScore().getHandledCount();
        return ordering.subList(0, handledCount);
    }

    /**
     * Returns the count of exactly matching properties from the two
     * orderings. The match must be consecutive and start at the first
     * property.
     */
    private static <T extends Storable> int
        commonOrderingCount(OrderingList<T> orderingA, OrderingList<T> orderingB)
    {
        int commonCount = Math.min(orderingA.size(), orderingB.size());

        for (int i=0; i<commonCount; i++) {
            if (!orderingA.get(i).equals(orderingB.get(i))) {
                return i;
            }
        }

        return commonCount;
    }

    private final Filter<T> mTargetFilter;
    private final StorableProperty<T> mTargetToSourceProperty;

    private final QueryExecutor<S> mOuterLoopExecutor;
    private final FilterValues<S> mOuterLoopFilterValues;

    private final QueryExecutor<T> mInnerLoopExecutor;
    private final FilterValues<T> mInnerLoopFilterValues;

    private final Filter<T> mSourceFilterAsFromTarget;
    private final Filter<T> mCombinedFilter;
    private final OrderingList<T> mCombinedOrdering;

    private final Joiner.Factory<S, T> mJoinerFactory;

    /**
     * @param outerLoopExecutor executor for <i>source</i> instances
     * @param innerLoopExecutorFactory used to construct inner loop executor
     * @param targetToSourceProperty join property of <i>target</i> type which maps
     * to instances of <i>source</i> type
     * @param targetFilter optional initial filter for fetching <i>target</i> instances
     * @param targetOrdering optional desired ordering to apply to
     * <i>target</i> executor
     * @param targetAccess used with target ordering to determine actual
     * ordering which an index provides for free
     * @throws IllegalArgumentException if any parameter is null or if join
     * property is not of <i>source</i> type
     * @throws RepositoryException from innerLoopExecutorFactory
     */
    private JoinedQueryExecutor(QueryExecutor<S> outerLoopExecutor,
                                QueryExecutorFactory<T> innerLoopExecutorFactory,
                                StorableProperty<T> targetToSourceProperty,
                                Filter<T> targetFilter,
                                OrderingList<T> targetOrdering,
                                StorageAccess<T> targetAccess)
        throws RepositoryException
    {
        if (targetToSourceProperty == null || outerLoopExecutor == null) {
            throw new IllegalArgumentException("Null parameter");
        }

        Class<S> sourceType = outerLoopExecutor.getStorableType();
        if (targetToSourceProperty.getType() != sourceType) {
            throw new IllegalArgumentException
                ("Property is not of type \"" + sourceType.getName() + "\": " +
                 targetToSourceProperty);
        }

        if (!targetToSourceProperty.isJoin()) {
            throw new IllegalArgumentException
                ("Property is not a join: " + targetToSourceProperty);
        }

        if (targetFilter != null && !targetFilter.isBound()) {
            throw new IllegalArgumentException("Target filter must be bound");
        }

        if (!outerLoopExecutor.getFilter().isBound()) {
            throw new IllegalArgumentException("Outer loop executor filter must be bound");
        }

        if (targetFilter.isOpen()) {
            targetFilter = null;
        }

        mTargetFilter = targetFilter;
        mTargetToSourceProperty = targetToSourceProperty;
        mOuterLoopExecutor = outerLoopExecutor;
        mOuterLoopFilterValues = outerLoopExecutor.getFilter().initialFilterValues();

        Class<T> targetType = targetToSourceProperty.getEnclosingType();

        // Prepare inner loop filter which is and'd by the join property elements.
        Filter<T> innerLoopExecutorFilter = Filter.getOpenFilter(targetType);
        if (targetFilter != null) {
            innerLoopExecutorFilter = innerLoopExecutorFilter.and(targetFilter);
        }
        int count = targetToSourceProperty.getJoinElementCount();
        for (int i=0; i<count; i++) {
            innerLoopExecutorFilter = innerLoopExecutorFilter
                .and(targetToSourceProperty.getInternalJoinElement(i).getName(), RelOp.EQ);
        }
        innerLoopExecutorFilter = innerLoopExecutorFilter.bind();

        mInnerLoopFilterValues = innerLoopExecutorFilter.initialFilterValues();

        // Only perform requested ordering if inner loop index provides it for
        // free. This optimization is only valid if outer loop matches at most
        // one record.
        if (targetOrdering != null) {
            if (outerLoopExecutor instanceof KeyQueryExecutor) {
                targetOrdering =
                    expectedOrdering(targetAccess, innerLoopExecutorFilter, targetOrdering);
            } else {
                targetOrdering = null;
            }
        }

        mInnerLoopExecutor = innerLoopExecutorFactory
            .executor(innerLoopExecutorFilter, targetOrdering, null);

        Filter<T> filter = outerLoopExecutor.getFilter()
            .asJoinedFrom(ChainedProperty.get(targetToSourceProperty));

        mSourceFilterAsFromTarget = filter;

        if (targetFilter != null) {
            filter = filter.and(targetFilter);
        }

        mCombinedFilter = filter;

        // Prepare combined ordering.
        OrderingList<T> ordering = transformOrdering
            (targetType, targetToSourceProperty.getName(), outerLoopExecutor);

        if (targetOrdering != null) {
            ordering = ordering.concat(targetOrdering);
        }

        mCombinedOrdering = ordering;

        mJoinerFactory = getJoinerFactory(targetToSourceProperty);
    }

    public Cursor<T> fetch(FilterValues<T> values) throws FetchException {
        return fetch(values, null);
    }

    public Cursor<T> fetch(FilterValues<T> values, Query.Controller controller)
        throws FetchException
    {
        FilterValues<T> innerLoopFilterValues = mInnerLoopFilterValues;

        if (mTargetFilter != null) {
            // Prepare this before opening source cursor, in case an exception is thrown.
            innerLoopFilterValues = innerLoopFilterValues
                .withValues(values.getValuesFor(mTargetFilter));
        }

        if (controller != null) {
            controller.begin();
        }

        Cursor<S> outerLoopCursor = mOuterLoopExecutor.fetch(transferValues(values), controller);

        return mJoinerFactory.newJoinedCursor
            (outerLoopCursor, mInnerLoopExecutor, innerLoopFilterValues, controller);
    }

    public Filter<T> getFilter() {
        return mCombinedFilter;
    }

    public OrderingList<T> getOrdering() {
        return mCombinedOrdering;
    }

    public boolean printPlan(Appendable app, int indentLevel, FilterValues<T> values)
        throws IOException
    {
        indent(app, indentLevel);
        app.append("join: ");
        app.append(mTargetToSourceProperty.getEnclosingType().getName());
        newline(app);
        indent(app, indentLevel);
        app.append("...inner loop: ");
        app.append(mTargetToSourceProperty.getName());
        newline(app);
        mInnerLoopExecutor.printPlan(app, increaseIndent(indentLevel), values);
        indent(app, indentLevel);
        app.append("...outer loop");
        newline(app);
        mOuterLoopExecutor.printPlan(app, increaseIndent(indentLevel), transferValues(values));
        return true;
    }

    private FilterValues<S> transferValues(FilterValues<T> values) {
        if (values == null) {
            return null;
        }
        return mOuterLoopFilterValues
            .withValues(values.getSuppliedValuesFor(mSourceFilterAsFromTarget));
    }

    @SuppressWarnings("unused")
    private static interface Joiner {
        /**
         * Needs to be public for {@link QuickConstructorGenerator}, but hide
         * it inside a private interface.
         */
        public static interface Factory<S, T extends Storable> {
            Cursor<T> newJoinedCursor(Cursor<S> outerLoopCursor,
                                      QueryExecutor<T> innerLoopExecutor,
                                      FilterValues<T> innerLoopFilterValues,
                                      Query.Controller innerLoopController);
        }
    }
}