File: ogrfeaturequery.cpp

package info (click to toggle)
gdal 2.4.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 106,252 kB
  • sloc: cpp: 1,034,818; ansic: 177,912; python: 23,610; perl: 7,420; sh: 6,283; java: 5,382; xml: 3,100; cs: 2,343; yacc: 1,198; makefile: 519; sql: 112
file content (826 lines) | stat: -rw-r--r-- 27,350 bytes parent folder | download | duplicates (5)
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
/******************************************************************************
 *
 * Project:  OpenGIS Simple Features Reference Implementation
 * Purpose:  Implementation of simple SQL WHERE style attributes queries
 *           for OGRFeatures.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com>
 * Copyright (c) 2008-2014, Even Rouault <even dot rouault at mines-paris dot org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "cpl_port.h"
#include "ogr_feature.h"
#include "swq.h"

#include <cstddef>
#include <cstdlib>
#include <algorithm>

#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_string.h"
#include "ogr_attrind.h"
#include "ogr_core.h"
#include "ogr_p.h"
#include "ogrsf_frmts.h"

//! @cond Doxygen_Suppress

CPL_CVSID("$Id: ogrfeaturequery.cpp 7e07230bbff24eb333608de4dbd460b7312839d0 2017-12-11 19:08:47Z Even Rouault $")

/************************************************************************/
/*     Support for special attributes (feature query and selection)     */
/************************************************************************/

const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT] = {
    "FID", "OGR_GEOMETRY", "OGR_STYLE", "OGR_GEOM_WKT", "OGR_GEOM_AREA"};
const swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT] = {
    SWQ_INTEGER, SWQ_STRING, SWQ_STRING, SWQ_STRING, SWQ_FLOAT};

/************************************************************************/
/*                          OGRFeatureQuery()                           */
/************************************************************************/

OGRFeatureQuery::OGRFeatureQuery() :
    poTargetDefn(nullptr),
    pSWQExpr(nullptr)
{}

/************************************************************************/
/*                          ~OGRFeatureQuery()                          */
/************************************************************************/

OGRFeatureQuery::~OGRFeatureQuery()

{
    delete static_cast<swq_expr_node *>(pSWQExpr);
}

/************************************************************************/
/*                             Compile()                                */
/************************************************************************/

OGRErr
OGRFeatureQuery::Compile( OGRLayer *poLayer,
                          const char * pszExpression,
                          int bCheck,
                          swq_custom_func_registrar *poCustomFuncRegistrar )

{
    return Compile(poLayer, poLayer->GetLayerDefn(),
                   pszExpression, bCheck, poCustomFuncRegistrar);
}

/************************************************************************/
/*                             Compile()                                */
/************************************************************************/

OGRErr
OGRFeatureQuery::Compile( OGRFeatureDefn *poDefn,
                          const char * pszExpression,
                          int bCheck,
                          swq_custom_func_registrar *poCustomFuncRegistrar )

{
    return Compile(nullptr, poDefn, pszExpression, bCheck, poCustomFuncRegistrar);
}

/************************************************************************/
/*                             Compile()                                */
/************************************************************************/

OGRErr
OGRFeatureQuery::Compile( OGRLayer *poLayer,
                          OGRFeatureDefn *poDefn,
                          const char * pszExpression,
                          int bCheck,
                          swq_custom_func_registrar *poCustomFuncRegistrar )
{
    // Clear any existing expression.
    if( pSWQExpr != nullptr )
    {
        delete static_cast<swq_expr_node *>(pSWQExpr);
        pSWQExpr = nullptr;
    }

    const char* pszFIDColumn = nullptr;
    bool bMustAddFID = false;
    if( poLayer != nullptr )
    {
        pszFIDColumn = poLayer->GetFIDColumn();
        if( pszFIDColumn != nullptr )
        {
            if( !EQUAL(pszFIDColumn, "") && !EQUAL(pszFIDColumn, "FID") )
            {
                bMustAddFID = true;
            }
        }
    }

    // Build list of fields.
    const int nFieldCount =
        poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT +
        poDefn->GetGeomFieldCount() + (bMustAddFID ? 1 : 0);

    char **papszFieldNames = static_cast<char **>(
        CPLMalloc(sizeof(char *) * nFieldCount ));
    swq_field_type *paeFieldTypes = static_cast<swq_field_type *>(
        CPLMalloc(sizeof(swq_field_type) * nFieldCount));

    for( int iField = 0; iField < poDefn->GetFieldCount(); iField++ )
    {
        OGRFieldDefn *poField = poDefn->GetFieldDefn(iField);

        papszFieldNames[iField] = const_cast<char *>(poField->GetNameRef());

        switch( poField->GetType() )
        {
          case OFTInteger:
          {
              if( poField->GetSubType() == OFSTBoolean )
                  paeFieldTypes[iField] = SWQ_BOOLEAN;
              else
                  paeFieldTypes[iField] = SWQ_INTEGER;
              break;
          }

          case OFTInteger64:
          {
              if( poField->GetSubType() == OFSTBoolean )
                  paeFieldTypes[iField] = SWQ_BOOLEAN;
              else
                  paeFieldTypes[iField] = SWQ_INTEGER64;
              break;
          }

          case OFTReal:
            paeFieldTypes[iField] = SWQ_FLOAT;
            break;

          case OFTString:
            paeFieldTypes[iField] = SWQ_STRING;
            break;

          case OFTDate:
          case OFTTime:
          case OFTDateTime:
            paeFieldTypes[iField] = SWQ_TIMESTAMP;
            break;

          default:
            paeFieldTypes[iField] = SWQ_OTHER;
            break;
        }
    }

    int iField = 0;
    while( iField < SPECIAL_FIELD_COUNT )
    {
        papszFieldNames[poDefn->GetFieldCount() + iField] =
            const_cast<char *>(SpecialFieldNames[iField]);
        paeFieldTypes[poDefn->GetFieldCount() + iField] =
            (iField == SPF_FID) ? SWQ_INTEGER64 : SpecialFieldTypes[iField];
        ++iField;
    }

    for( iField = 0; iField < poDefn->GetGeomFieldCount(); iField++ )
    {
        OGRGeomFieldDefn *poField = poDefn->GetGeomFieldDefn(iField);
        const int iDstField =
            poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT + iField;

        papszFieldNames[iDstField] = const_cast<char *>(poField->GetNameRef());
        if( *papszFieldNames[iDstField] == '\0' )
            papszFieldNames[iDstField] =
                const_cast<char *>(OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME);
        paeFieldTypes[iDstField] = SWQ_GEOMETRY;
    }

    if( bMustAddFID )
    {
        papszFieldNames[nFieldCount-1] =
            const_cast<char*>(pszFIDColumn);
        paeFieldTypes[nFieldCount-1] =
            (poLayer != nullptr &&
             poLayer->GetMetadataItem(OLMD_FID64) != nullptr &&
             EQUAL(poLayer->GetMetadataItem(OLMD_FID64), "YES")) ?
            SWQ_INTEGER64 : SWQ_INTEGER;
    }

    // Try to parse.
    poTargetDefn = poDefn;
    const CPLErr eCPLErr =
        swq_expr_compile(pszExpression, nFieldCount,
                         papszFieldNames, paeFieldTypes,
                         bCheck,
                         poCustomFuncRegistrar,
                         reinterpret_cast<swq_expr_node **>(&pSWQExpr));

    OGRErr eErr = OGRERR_NONE;
    if( eCPLErr != CE_None )
    {
        eErr = OGRERR_CORRUPT_DATA;
        pSWQExpr = nullptr;
    }

    CPLFree(papszFieldNames);
    CPLFree(paeFieldTypes);

    return eErr;
}

/************************************************************************/
/*                    OGRFeatureFetcherFixFieldIndex()                  */
/************************************************************************/

static int OGRFeatureFetcherFixFieldIndex( OGRFeatureDefn* poFDefn, int nIdx )
{
    /* Nastry trick: if we inserted the FID column as an extra column, it is */
    /* after regular fields, special fields and geometry fields */
    if( nIdx == poFDefn->GetFieldCount() + SPECIAL_FIELD_COUNT +
                poFDefn->GetGeomFieldCount() )
    {
        return poFDefn->GetFieldCount() + SPF_FID;
    }
    return nIdx;
}

/************************************************************************/
/*                         OGRFeatureFetcher()                          */
/************************************************************************/

static swq_expr_node *OGRFeatureFetcher( swq_expr_node *op, void *pFeatureIn )

{
    OGRFeature *poFeature = static_cast<OGRFeature *>(pFeatureIn);

    if( op->field_type == SWQ_GEOMETRY )
    {
        const int iField =
            op->field_index -
            (poFeature->GetFieldCount() + SPECIAL_FIELD_COUNT);
        swq_expr_node *poRetNode =
            new swq_expr_node(poFeature->GetGeomFieldRef(iField));
        return poRetNode;
    }

    const int idx = OGRFeatureFetcherFixFieldIndex(poFeature->GetDefnRef(),
                                                   op->field_index);

    swq_expr_node *poRetNode = nullptr;
    switch( op->field_type )
    {
      case SWQ_INTEGER:
      case SWQ_BOOLEAN:
        poRetNode = new swq_expr_node(
            poFeature->GetFieldAsInteger(idx) );
        break;

      case SWQ_INTEGER64:
        poRetNode = new swq_expr_node(
            poFeature->GetFieldAsInteger64(idx) );
        break;

      case SWQ_FLOAT:
        poRetNode = new swq_expr_node(
            poFeature->GetFieldAsDouble(idx) );
        break;

      case SWQ_TIMESTAMP:
        poRetNode = new swq_expr_node(
            poFeature->GetFieldAsString(idx) );
        poRetNode->MarkAsTimestamp();
        break;

      default:
        poRetNode = new swq_expr_node(
            poFeature->GetFieldAsString(idx) );
        break;
    }

    poRetNode->is_null = !(poFeature->IsFieldSetAndNotNull(idx));

    return poRetNode;
}

/************************************************************************/
/*                              Evaluate()                              */
/************************************************************************/

int OGRFeatureQuery::Evaluate( OGRFeature *poFeature )

{
    if( pSWQExpr == nullptr )
        return FALSE;

    swq_expr_node *poResult =
        static_cast<swq_expr_node *>(pSWQExpr)->
            Evaluate(OGRFeatureFetcher, poFeature);

    if( poResult == nullptr )
        return FALSE;

    bool bLogicalResult = false;
    if( poResult->field_type == SWQ_INTEGER ||
        poResult->field_type == SWQ_INTEGER64 ||
        poResult->field_type == SWQ_BOOLEAN )
        bLogicalResult = CPL_TO_BOOL(static_cast<int>(poResult->int_value));

    delete poResult;

    return bLogicalResult;
}

/************************************************************************/
/*                            CanUseIndex()                             */
/************************************************************************/

int OGRFeatureQuery::CanUseIndex( OGRLayer *poLayer )
{
    swq_expr_node *psExpr = static_cast<swq_expr_node *>(pSWQExpr);

    // Do we have an index on the targeted layer?
    if( poLayer->GetIndex() == nullptr )
        return FALSE;

    return CanUseIndex(psExpr, poLayer);
}

int OGRFeatureQuery::CanUseIndex( swq_expr_node *psExpr,
                                  OGRLayer *poLayer )
{
    // Does the expression meet our requirements?
    if( psExpr == nullptr || psExpr->eNodeType != SNT_OPERATION )
        return FALSE;

    if( (psExpr->nOperation == SWQ_OR || psExpr->nOperation == SWQ_AND) &&
         psExpr->nSubExprCount == 2 )
    {
        return CanUseIndex(psExpr->papoSubExpr[0], poLayer) &&
               CanUseIndex(psExpr->papoSubExpr[1], poLayer);
    }

    if( !(psExpr->nOperation == SWQ_EQ || psExpr->nOperation == SWQ_IN)
        || psExpr->nSubExprCount < 2 )
        return FALSE;

    swq_expr_node *poColumn = psExpr->papoSubExpr[0];
    swq_expr_node *poValue = psExpr->papoSubExpr[1];

    if( poColumn->eNodeType != SNT_COLUMN
        || poValue->eNodeType != SNT_CONSTANT )
        return FALSE;

    OGRAttrIndex *poIndex =
        poLayer->GetIndex()->GetFieldIndex(
            OGRFeatureFetcherFixFieldIndex(poLayer->GetLayerDefn(),
                                           poColumn->field_index));
    if( poIndex == nullptr )
        return FALSE;

    // Have an index.
    return TRUE;
}

/************************************************************************/
/*                       EvaluateAgainstIndices()                       */
/*                                                                      */
/*      Attempt to return a list of FIDs matching the given             */
/*      attribute query conditions utilizing attribute indices.         */
/*      Returns NULL if the result cannot be computed from the          */
/*      available indices, or an "OGRNullFID" terminated list of        */
/*      FIDs if it can.                                                 */
/*                                                                      */
/*      For now we only support equality tests on a single indexed      */
/*      attribute field.  Eventually we should make this support        */
/*      multi-part queries with ranges.                                 */
/************************************************************************/

static int CompareGIntBig( const void *pa, const void *pb )
{
    const GIntBig a = *(reinterpret_cast<const GIntBig *>(pa));
    const GIntBig b = *(reinterpret_cast<const GIntBig *>(pb));
    if( a < b )
        return -1;
    else if( a > b )
        return 1;
    else
        return 0;
}

GIntBig *OGRFeatureQuery::EvaluateAgainstIndices( OGRLayer *poLayer,
                                                  OGRErr *peErr )

{
    swq_expr_node *psExpr = static_cast<swq_expr_node *>(pSWQExpr);

    if( peErr != nullptr )
        *peErr = OGRERR_NONE;

    // Do we have an index on the targeted layer?
    if( poLayer->GetIndex() == nullptr )
        return nullptr;

    GIntBig nFIDCount = 0;
    return EvaluateAgainstIndices(psExpr, poLayer, nFIDCount);
}

// The input arrays must be sorted.
static
GIntBig* OGRORGIntBigArray( GIntBig panFIDList1[], GIntBig nFIDCount1,
                            GIntBig panFIDList2[], GIntBig nFIDCount2,
                            GIntBig& nFIDCount )
{
    const GIntBig nMaxCount = nFIDCount1 + nFIDCount2;
    GIntBig* panFIDList = static_cast<GIntBig *>(
        CPLMalloc(static_cast<size_t>(nMaxCount + 1) * sizeof(GIntBig)));
    nFIDCount = 0;

    for( GIntBig i1 = 0, i2 = 0; i1<nFIDCount1 || i2<nFIDCount2; )
    {
        if( i1 < nFIDCount1 && i2 < nFIDCount2 )
        {
            const GIntBig nVal1 = panFIDList1[i1];
            const GIntBig nVal2 = panFIDList2[i2];
            if( nVal1 < nVal2 )
            {
                if( i1 + 1 < nFIDCount1 && panFIDList1[i1+1] <= nVal2 )
                {
                    panFIDList[nFIDCount++] = nVal1;
                    i1++;
                }
                else
                {
                    panFIDList[nFIDCount++] = nVal1;
                    panFIDList[nFIDCount++] = nVal2;
                    i1++;
                    i2++;
                }
            }
            else if( nVal1 == nVal2 )
            {
                panFIDList[nFIDCount++] = nVal1;
                i1++;
                i2++;
            }
            else
            {
                if( i2 + 1 < nFIDCount2 && panFIDList2[i2+1] <= nVal1 )
                {
                    panFIDList[nFIDCount++] = nVal2;
                    i2++;
                }
                else
                {
                    panFIDList[nFIDCount++] = nVal2;
                    panFIDList[nFIDCount++] = nVal1;
                    i1++;
                    i2++;
                }
            }
        }
        else if( i1 < nFIDCount1 )
        {
            const GIntBig nVal1 = panFIDList1[i1];
            panFIDList[nFIDCount++] = nVal1;
            i1++;
        }
        else if( i2 < nFIDCount2 )
        {
            const GIntBig nVal2 = panFIDList2[i2];
            panFIDList[nFIDCount++] = nVal2;
            i2++;
        }
    }

    panFIDList[nFIDCount] = OGRNullFID;

    return panFIDList;
}

// The input arrays must be sorted.
static
GIntBig* OGRANDGIntBigArray( GIntBig panFIDList1[], GIntBig nFIDCount1,
                             GIntBig panFIDList2[], GIntBig nFIDCount2,
                             GIntBig& nFIDCount )
{
    GIntBig nMaxCount = std::max(nFIDCount1, nFIDCount2);
    GIntBig* panFIDList = static_cast<GIntBig *>(
        CPLMalloc(static_cast<size_t>(nMaxCount + 1) * sizeof(GIntBig)));
    nFIDCount = 0;

    for( GIntBig i1 = 0, i2 = 0; i1 < nFIDCount1 && i2 < nFIDCount2; )
    {
        const GIntBig nVal1 = panFIDList1[i1];
        const GIntBig nVal2 = panFIDList2[i2];
        if( nVal1 < nVal2 )
        {
            if( i1+1 < nFIDCount1 && panFIDList1[i1+1] <= nVal2 )
            {
                i1++;
            }
            else
            {
                i1++;
                i2++;
            }
        }
        else if( nVal1 == nVal2 )
        {
            panFIDList[nFIDCount++] = nVal1;
            i1++;
            i2++;
        }
        else
        {
            if( i2 + 1 < nFIDCount2 && panFIDList2[i2+1] <= nVal1 )
            {
                i2++;
            }
            else
            {
                i1++;
                i2++;
            }
        }
    }

    panFIDList[nFIDCount] = OGRNullFID;

    return panFIDList;
}

GIntBig *OGRFeatureQuery::EvaluateAgainstIndices( swq_expr_node *psExpr,
                                                  OGRLayer *poLayer,
                                                  GIntBig& nFIDCount )
{
    // Does the expression meet our requirements?
    if( psExpr == nullptr ||
        psExpr->eNodeType != SNT_OPERATION )
        return nullptr;

    if( (psExpr->nOperation == SWQ_OR || psExpr->nOperation == SWQ_AND) &&
         psExpr->nSubExprCount == 2 )
    {
        GIntBig nFIDCount1 = 0;
        GIntBig nFIDCount2 = 0;
        GIntBig* panFIDList1 =
            EvaluateAgainstIndices(psExpr->papoSubExpr[0], poLayer, nFIDCount1);
        GIntBig* panFIDList2 =
            panFIDList1 == nullptr ? nullptr :
            EvaluateAgainstIndices(psExpr->papoSubExpr[1], poLayer, nFIDCount2);
        GIntBig* panFIDList = nullptr;
        if( panFIDList1 != nullptr && panFIDList2 != nullptr )
        {
            if( psExpr->nOperation == SWQ_OR )
                panFIDList = OGRORGIntBigArray(panFIDList1, nFIDCount1,
                                            panFIDList2, nFIDCount2, nFIDCount);
            else if( psExpr->nOperation == SWQ_AND )
                panFIDList = OGRANDGIntBigArray(panFIDList1, nFIDCount1,
                                            panFIDList2, nFIDCount2, nFIDCount);
        }
        CPLFree(panFIDList1);
        CPLFree(panFIDList2);
        return panFIDList;
    }

    if( !(psExpr->nOperation == SWQ_EQ || psExpr->nOperation == SWQ_IN)
        || psExpr->nSubExprCount < 2 )
        return nullptr;

    swq_expr_node *poColumn = psExpr->papoSubExpr[0];
    swq_expr_node *poValue = psExpr->papoSubExpr[1];

    if( poColumn->eNodeType != SNT_COLUMN
        || poValue->eNodeType != SNT_CONSTANT )
        return nullptr;

    const int nIdx = OGRFeatureFetcherFixFieldIndex(
        poLayer->GetLayerDefn(), poColumn->field_index);

    OGRAttrIndex *poIndex =
        poLayer->GetIndex()->GetFieldIndex(nIdx);
    if( poIndex == nullptr )
        return nullptr;

    // Have an index, now we need to query it.
    OGRField sValue;
    OGRFieldDefn *poFieldDefn =
        poLayer->GetLayerDefn()->GetFieldDefn(nIdx);

    // Handle the case of an IN operation.
    if( psExpr->nOperation == SWQ_IN )
    {
        int nLength = 0;
        GIntBig *panFIDs = nullptr;
        nFIDCount = 0;

        for( int iIN = 1; iIN < psExpr->nSubExprCount; iIN++ )
        {
            switch( poFieldDefn->GetType() )
            {
              case OFTInteger:
                if( psExpr->papoSubExpr[iIN]->field_type == SWQ_FLOAT )
                    sValue.Integer =
                        static_cast<int>(psExpr->papoSubExpr[iIN]->float_value);
                else
                    sValue.Integer =
                        static_cast<int>(psExpr->papoSubExpr[iIN]->int_value);
                break;

              case OFTInteger64:
                if( psExpr->papoSubExpr[iIN]->field_type == SWQ_FLOAT )
                    sValue.Integer64 = static_cast<GIntBig>(
                        psExpr->papoSubExpr[iIN]->float_value);
                else
                    sValue.Integer64 = psExpr->papoSubExpr[iIN]->int_value;
                break;

              case OFTReal:
                sValue.Real = psExpr->papoSubExpr[iIN]->float_value;
                break;

              case OFTString:
                sValue.String = psExpr->papoSubExpr[iIN]->string_value;
                break;

              default:
                CPLAssert(false);
                return nullptr;
            }

            int nFIDCount32 = static_cast<int>(nFIDCount);
            panFIDs = poIndex->GetAllMatches(&sValue, panFIDs,
                                             &nFIDCount32, &nLength);
            nFIDCount = nFIDCount32;
        }

        if( nFIDCount > 1 )
        {
            // The returned FIDs are expected to be in sorted order.
            qsort(panFIDs, static_cast<size_t>(nFIDCount),
                  sizeof(GIntBig), CompareGIntBig);
        }
        return panFIDs;
    }

    // Handle equality test.
    switch( poFieldDefn->GetType() )
    {
      case OFTInteger:
        if( poValue->field_type == SWQ_FLOAT )
            sValue.Integer = static_cast<int>(poValue->float_value);
        else
            sValue.Integer = static_cast<int>(poValue->int_value);
        break;

      case OFTInteger64:
        if( poValue->field_type == SWQ_FLOAT )
            sValue.Integer64 = static_cast<GIntBig>(poValue->float_value);
        else
            sValue.Integer64 = poValue->int_value;
        break;

      case OFTReal:
        sValue.Real = poValue->float_value;
        break;

      case OFTString:
        sValue.String = poValue->string_value;
        break;

      default:
        CPLAssert(false);
        return nullptr;
    }

    int nLength = 0;
    int nFIDCount32 = 0;
    GIntBig* panFIDs =
        poIndex->GetAllMatches(&sValue, nullptr, &nFIDCount32, &nLength);
    nFIDCount = nFIDCount32;
    if( nFIDCount > 1 )
    {
        // The returned FIDs are expected to be sorted.
        // TODO(schwehr): Use std::sort.
        qsort(panFIDs, static_cast<size_t>(nFIDCount),
              sizeof(GIntBig), CompareGIntBig);
    }
    return panFIDs;
}

/************************************************************************/
/*                         OGRFieldCollector()                          */
/*                                                                      */
/*      Helper function for recursing through tree to satisfy           */
/*      GetUsedFields().                                                */
/************************************************************************/

char **OGRFeatureQuery::FieldCollector( void *pBareOp,
                                        char **papszList )

{
    swq_expr_node *op = static_cast<swq_expr_node *>(pBareOp);

    // References to tables other than the primarily are currently unsupported.
    // Error out.
    if( op->eNodeType == SNT_COLUMN )
    {
        if( op->table_index != 0 )
        {
            CSLDestroy( papszList );
            return nullptr;
        }

        // Add the field name into our list if it is not already there.
        const char *pszFieldName = nullptr;
        const int nIdx = OGRFeatureFetcherFixFieldIndex(poTargetDefn,
                                                        op->field_index);

        if( nIdx >= poTargetDefn->GetFieldCount()
            && nIdx <
            poTargetDefn->GetFieldCount() + SPECIAL_FIELD_COUNT )
        {
            pszFieldName =
                SpecialFieldNames[nIdx -
                                  poTargetDefn->GetFieldCount()];
        }
        else if( nIdx >= 0
                 && nIdx < poTargetDefn->GetFieldCount() )
        {
            pszFieldName =
                poTargetDefn->GetFieldDefn(nIdx)->GetNameRef();
        }
        else
        {
            CSLDestroy(papszList);
            return nullptr;
        }

        if( CSLFindString(papszList, pszFieldName) == -1 )
            papszList = CSLAddString(papszList, pszFieldName);
    }

    // Add in fields from subexpressions.
    if( op->eNodeType == SNT_OPERATION )
    {
        for( int iSubExpr = 0; iSubExpr < op->nSubExprCount; iSubExpr++ )
        {
            papszList = FieldCollector(op->papoSubExpr[iSubExpr], papszList);
        }
    }

    return papszList;
}

/************************************************************************/
/*                           GetUsedFields()                            */
/************************************************************************/

/**
 * Returns lists of fields in expression.
 *
 * All attribute fields are used in the expression of this feature
 * query are returned as a StringList of field names.  This function would
 * primarily be used within drivers to recognise special case conditions
 * depending only on attribute fields that can be very efficiently
 * fetched.
 *
 * NOTE: If any fields in the expression are from tables other than the
 * primary table then NULL is returned indicating an error.  In successful
 * use, no non-empty expression should return an empty list.
 *
 * @return list of field names.  Free list with CSLDestroy() when no longer
 * required.
 */

char **OGRFeatureQuery::GetUsedFields( )

{
    if( pSWQExpr == nullptr )
        return nullptr;

    return FieldCollector( pSWQExpr, nullptr );
}

//! @endcond