Coverage Report

Created: 2026-07-14 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrfeaturequery.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  Implementation of simple SQL WHERE style attributes queries
5
 *           for OGRFeatures.
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com>
10
 * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
11
 *
12
 * SPDX-License-Identifier: MIT
13
 ****************************************************************************/
14
15
#include "cpl_port.h"
16
#include "ogr_feature.h"
17
#include "ogr_swq.h"
18
19
#include <cstddef>
20
#include <algorithm>
21
22
#include "cpl_conv.h"
23
#include "cpl_error.h"
24
#include "cpl_string.h"
25
#include "ogr_attrind.h"
26
#include "ogr_core.h"
27
#include "ogr_p.h"
28
#include "ogrsf_frmts.h"
29
30
//! @cond Doxygen_Suppress
31
32
/************************************************************************/
33
/*     Support for special attributes (feature query and selection)     */
34
/************************************************************************/
35
extern const swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT];
36
37
const char *const SpecialFieldNames[SPECIAL_FIELD_COUNT] = {
38
    "FID", "OGR_GEOMETRY", "OGR_STYLE", "OGR_GEOM_WKT", "OGR_GEOM_AREA"};
39
const swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT] = {
40
    SWQ_INTEGER, SWQ_STRING, SWQ_STRING, SWQ_STRING, SWQ_FLOAT};
41
42
/************************************************************************/
43
/*                          OGRFeatureQuery()                           */
44
/************************************************************************/
45
46
OGRFeatureQuery::OGRFeatureQuery()
47
0
    : poTargetDefn(nullptr), pSWQExpr(nullptr),
48
0
      m_psContext(new swq_evaluation_context())
49
0
{
50
0
}
51
52
/************************************************************************/
53
/*                          ~OGRFeatureQuery()                          */
54
/************************************************************************/
55
56
OGRFeatureQuery::~OGRFeatureQuery()
57
58
0
{
59
0
    delete m_psContext;
60
0
    delete static_cast<swq_expr_node *>(pSWQExpr);
61
0
}
62
63
/************************************************************************/
64
/*                              Compile()                               */
65
/************************************************************************/
66
67
OGRErr
68
OGRFeatureQuery::Compile(const OGRLayer *poLayer, const char *pszExpression,
69
                         int bCheck,
70
                         swq_custom_func_registrar *poCustomFuncRegistrar)
71
72
0
{
73
0
    if (poLayer->TestCapability(OLCStringsAsUTF8))
74
0
        m_psContext->bUTF8Strings = true;
75
0
    return Compile(poLayer, poLayer->GetLayerDefn(), pszExpression, bCheck,
76
0
                   poCustomFuncRegistrar);
77
0
}
78
79
/************************************************************************/
80
/*                              Compile()                               */
81
/************************************************************************/
82
83
OGRErr
84
OGRFeatureQuery::Compile(const OGRFeatureDefn *poDefn,
85
                         const char *pszExpression, int bCheck,
86
                         swq_custom_func_registrar *poCustomFuncRegistrar)
87
88
0
{
89
0
    return Compile(nullptr, poDefn, pszExpression, bCheck,
90
0
                   poCustomFuncRegistrar);
91
0
}
92
93
/************************************************************************/
94
/*                              Compile()                               */
95
/************************************************************************/
96
97
OGRErr
98
OGRFeatureQuery::Compile(const OGRLayer *poLayer, const OGRFeatureDefn *poDefn,
99
                         const char *pszExpression, int bCheck,
100
                         swq_custom_func_registrar *poCustomFuncRegistrar)
101
0
{
102
    // Clear any existing expression.
103
0
    if (pSWQExpr != nullptr)
104
0
    {
105
0
        delete static_cast<swq_expr_node *>(pSWQExpr);
106
0
        pSWQExpr = nullptr;
107
0
    }
108
109
0
    const char *pszFIDColumn = nullptr;
110
0
    bool bMustAddFID = false;
111
0
    if (poLayer != nullptr)
112
0
    {
113
0
        pszFIDColumn = const_cast<OGRLayer *>(poLayer)->GetFIDColumn();
114
0
        if (pszFIDColumn != nullptr)
115
0
        {
116
0
            if (!EQUAL(pszFIDColumn, "") && !EQUAL(pszFIDColumn, "FID"))
117
0
            {
118
0
                bMustAddFID = true;
119
0
            }
120
0
        }
121
0
    }
122
123
    // Build list of fields.
124
0
    const int nFieldCount = poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT +
125
0
                            poDefn->GetGeomFieldCount() + (bMustAddFID ? 1 : 0);
126
127
0
    char **papszFieldNames =
128
0
        static_cast<char **>(CPLMalloc(sizeof(char *) * nFieldCount));
129
0
    swq_field_type *paeFieldTypes = static_cast<swq_field_type *>(
130
0
        CPLMalloc(sizeof(swq_field_type) * nFieldCount));
131
132
0
    for (int iField = 0; iField < poDefn->GetFieldCount(); iField++)
133
0
    {
134
0
        const OGRFieldDefn *poField = poDefn->GetFieldDefn(iField);
135
0
        if (!poField)
136
0
        {
137
0
            CPLAssert(0);
138
0
            break;
139
0
        }
140
141
0
        papszFieldNames[iField] = const_cast<char *>(poField->GetNameRef());
142
143
0
        switch (poField->GetType())
144
0
        {
145
0
            case OFTInteger:
146
0
            {
147
0
                if (poField->GetSubType() == OFSTBoolean)
148
0
                    paeFieldTypes[iField] = SWQ_BOOLEAN;
149
0
                else
150
0
                    paeFieldTypes[iField] = SWQ_INTEGER;
151
0
                break;
152
0
            }
153
154
0
            case OFTInteger64:
155
0
            {
156
0
                if (poField->GetSubType() == OFSTBoolean)
157
0
                    paeFieldTypes[iField] = SWQ_BOOLEAN;
158
0
                else
159
0
                    paeFieldTypes[iField] = SWQ_INTEGER64;
160
0
                break;
161
0
            }
162
163
0
            case OFTReal:
164
0
                paeFieldTypes[iField] = SWQ_FLOAT;
165
0
                break;
166
167
0
            case OFTString:
168
0
                paeFieldTypes[iField] = SWQ_STRING;
169
0
                break;
170
171
0
            case OFTDate:
172
0
            case OFTTime:
173
0
            case OFTDateTime:
174
0
                paeFieldTypes[iField] = SWQ_TIMESTAMP;
175
0
                break;
176
177
0
            default:
178
0
                paeFieldTypes[iField] = SWQ_OTHER;
179
0
                break;
180
0
        }
181
0
    }
182
183
0
    int iField = 0;
184
0
    while (iField < SPECIAL_FIELD_COUNT)
185
0
    {
186
0
        papszFieldNames[poDefn->GetFieldCount() + iField] =
187
0
            const_cast<char *>(SpecialFieldNames[iField]);
188
0
        paeFieldTypes[poDefn->GetFieldCount() + iField] =
189
0
            (iField == SPF_FID) ? SWQ_INTEGER64 : SpecialFieldTypes[iField];
190
0
        ++iField;
191
0
    }
192
193
0
    for (iField = 0; iField < poDefn->GetGeomFieldCount(); iField++)
194
0
    {
195
0
        const OGRGeomFieldDefn *poField = poDefn->GetGeomFieldDefn(iField);
196
0
        const int iDstField =
197
0
            poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT + iField;
198
199
0
        papszFieldNames[iDstField] = const_cast<char *>(poField->GetNameRef());
200
0
        if (*papszFieldNames[iDstField] == '\0')
201
0
            papszFieldNames[iDstField] =
202
0
                const_cast<char *>(OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME);
203
0
        paeFieldTypes[iDstField] = SWQ_GEOMETRY;
204
0
    }
205
206
0
    if (bMustAddFID)
207
0
    {
208
0
        papszFieldNames[nFieldCount - 1] = const_cast<char *>(pszFIDColumn);
209
0
        paeFieldTypes[nFieldCount - 1] =
210
0
            (poLayer != nullptr &&
211
0
             const_cast<OGRLayer *>(poLayer)->GetMetadataItem(OLMD_FID64) !=
212
0
                 nullptr &&
213
0
             EQUAL(const_cast<OGRLayer *>(poLayer)->GetMetadataItem(OLMD_FID64),
214
0
                   "YES"))
215
0
                ? SWQ_INTEGER64
216
0
                : SWQ_INTEGER;
217
0
    }
218
219
    // Try to parse.
220
0
    poTargetDefn = poDefn;
221
0
    const CPLErr eCPLErr = swq_expr_compile(
222
0
        pszExpression, nFieldCount, papszFieldNames, paeFieldTypes, bCheck,
223
0
        poCustomFuncRegistrar, reinterpret_cast<swq_expr_node **>(&pSWQExpr));
224
225
0
    OGRErr eErr = OGRERR_NONE;
226
0
    if (eCPLErr != CE_None)
227
0
    {
228
0
        eErr = OGRERR_CORRUPT_DATA;
229
0
        pSWQExpr = nullptr;
230
0
    }
231
232
0
    CPLFree(papszFieldNames);
233
0
    CPLFree(paeFieldTypes);
234
235
0
    return eErr;
236
0
}
237
238
/************************************************************************/
239
/*                   OGRFeatureFetcherFixFieldIndex()                   */
240
/************************************************************************/
241
242
static int OGRFeatureFetcherFixFieldIndex(const OGRFeatureDefn *poFDefn,
243
                                          int nIdx)
244
0
{
245
    /* Nastry trick: if we inserted the FID column as an extra column, it is */
246
    /* after regular fields, special fields and geometry fields */
247
0
    if (nIdx == poFDefn->GetFieldCount() + SPECIAL_FIELD_COUNT +
248
0
                    poFDefn->GetGeomFieldCount())
249
0
    {
250
0
        return poFDefn->GetFieldCount() + SPF_FID;
251
0
    }
252
0
    return nIdx;
253
0
}
254
255
/************************************************************************/
256
/*                         OGRFeatureFetcher()                          */
257
/************************************************************************/
258
259
static swq_expr_node *OGRFeatureFetcher(swq_expr_node *op,
260
                                        const void *pFeatureIn)
261
262
0
{
263
0
    const OGRFeature *poFeature = static_cast<const OGRFeature *>(pFeatureIn);
264
265
0
    if (op->field_type == SWQ_GEOMETRY)
266
0
    {
267
0
        const int iField = op->field_index -
268
0
                           (poFeature->GetFieldCount() + SPECIAL_FIELD_COUNT);
269
0
        swq_expr_node *poRetNode =
270
0
            new swq_expr_node(poFeature->GetGeomFieldRef(iField));
271
0
        return poRetNode;
272
0
    }
273
274
0
    const int idx = OGRFeatureFetcherFixFieldIndex(poFeature->GetDefnRef(),
275
0
                                                   op->field_index);
276
277
0
    swq_expr_node *poRetNode = nullptr;
278
0
    switch (op->field_type)
279
0
    {
280
0
        case SWQ_INTEGER:
281
0
        case SWQ_BOOLEAN:
282
0
            poRetNode = new swq_expr_node(poFeature->GetFieldAsInteger(idx));
283
0
            break;
284
285
0
        case SWQ_INTEGER64:
286
0
            poRetNode = new swq_expr_node(poFeature->GetFieldAsInteger64(idx));
287
0
            break;
288
289
0
        case SWQ_FLOAT:
290
0
            poRetNode = new swq_expr_node(poFeature->GetFieldAsDouble(idx));
291
0
            break;
292
293
0
        case SWQ_TIMESTAMP:
294
0
            poRetNode = new swq_expr_node(poFeature->GetFieldAsString(idx));
295
0
            poRetNode->MarkAsTimestamp();
296
0
            break;
297
298
0
        default:
299
0
            poRetNode = new swq_expr_node(poFeature->GetFieldAsString(idx));
300
0
            break;
301
0
    }
302
303
0
    poRetNode->is_null = !(poFeature->IsFieldSetAndNotNull(idx));
304
305
0
    return poRetNode;
306
0
}
307
308
/************************************************************************/
309
/*                              Evaluate()                              */
310
/************************************************************************/
311
312
int OGRFeatureQuery::Evaluate(const OGRFeature *poFeature)
313
314
0
{
315
0
    if (pSWQExpr == nullptr)
316
0
        return FALSE;
317
318
0
    swq_expr_node *poResult = static_cast<swq_expr_node *>(pSWQExpr)->Evaluate(
319
0
        OGRFeatureFetcher, poFeature, *m_psContext);
320
321
0
    if (poResult == nullptr)
322
0
        return FALSE;
323
324
0
    bool bLogicalResult = false;
325
0
    if (poResult->field_type == SWQ_INTEGER ||
326
0
        poResult->field_type == SWQ_INTEGER64 ||
327
0
        poResult->field_type == SWQ_BOOLEAN)
328
0
        bLogicalResult = CPL_TO_BOOL(static_cast<int>(poResult->int_value));
329
330
0
    delete poResult;
331
332
0
    return bLogicalResult;
333
0
}
334
335
/************************************************************************/
336
/*                            CanUseIndex()                             */
337
/************************************************************************/
338
339
int OGRFeatureQuery::CanUseIndex(OGRLayer *poLayer)
340
0
{
341
0
    swq_expr_node *psExpr = static_cast<swq_expr_node *>(pSWQExpr);
342
343
    // Do we have an index on the targeted layer?
344
0
    if (poLayer->GetIndex() == nullptr)
345
0
        return FALSE;
346
347
0
    return CanUseIndex(psExpr, poLayer);
348
0
}
349
350
int OGRFeatureQuery::CanUseIndex(const swq_expr_node *psExpr, OGRLayer *poLayer)
351
0
{
352
    // Does the expression meet our requirements?
353
0
    if (psExpr == nullptr || psExpr->eNodeType != SNT_OPERATION)
354
0
        return FALSE;
355
356
0
    if ((psExpr->nOperation == SWQ_OR || psExpr->nOperation == SWQ_AND) &&
357
0
        psExpr->nSubExprCount == 2)
358
0
    {
359
0
        return CanUseIndex(psExpr->papoSubExpr[0], poLayer) &&
360
0
               CanUseIndex(psExpr->papoSubExpr[1], poLayer);
361
0
    }
362
363
0
    if (!(psExpr->nOperation == SWQ_EQ || psExpr->nOperation == SWQ_IN) ||
364
0
        psExpr->nSubExprCount < 2)
365
0
        return FALSE;
366
367
0
    swq_expr_node *poColumn = psExpr->papoSubExpr[0];
368
0
    swq_expr_node *poValue = psExpr->papoSubExpr[1];
369
370
0
    if (poColumn->eNodeType != SNT_COLUMN || poValue->eNodeType != SNT_CONSTANT)
371
0
        return FALSE;
372
373
0
    OGRAttrIndex *poIndex =
374
0
        poLayer->GetIndex()->GetFieldIndex(OGRFeatureFetcherFixFieldIndex(
375
0
            poLayer->GetLayerDefn(), poColumn->field_index));
376
0
    if (poIndex == nullptr)
377
0
        return FALSE;
378
379
    // Have an index.
380
0
    return TRUE;
381
0
}
382
383
/************************************************************************/
384
/*                       EvaluateAgainstIndices()                       */
385
/*                                                                      */
386
/*      Attempt to return a list of FIDs matching the given             */
387
/*      attribute query conditions utilizing attribute indices.         */
388
/*      Returns NULL if the result cannot be computed from the          */
389
/*      available indices, or an "OGRNullFID" terminated list of        */
390
/*      FIDs if it can.                                                 */
391
/*                                                                      */
392
/*      For now we only support equality tests on a single indexed      */
393
/*      attribute field.  Eventually we should make this support        */
394
/*      multi-part queries with ranges.                                 */
395
/************************************************************************/
396
397
GIntBig *OGRFeatureQuery::EvaluateAgainstIndices(OGRLayer *poLayer,
398
                                                 OGRErr *peErr)
399
400
0
{
401
0
    swq_expr_node *psExpr = static_cast<swq_expr_node *>(pSWQExpr);
402
403
0
    if (peErr != nullptr)
404
0
        *peErr = OGRERR_NONE;
405
406
    // Do we have an index on the targeted layer?
407
0
    if (poLayer->GetIndex() == nullptr)
408
0
        return nullptr;
409
410
0
    GIntBig nFIDCount = 0;
411
0
    return EvaluateAgainstIndices(psExpr, poLayer, nFIDCount);
412
0
}
413
414
// The input arrays must be sorted.
415
static GIntBig *OGRORGIntBigArray(GIntBig panFIDList1[], GIntBig nFIDCount1,
416
                                  GIntBig panFIDList2[], GIntBig nFIDCount2,
417
                                  GIntBig &nFIDCount)
418
0
{
419
0
    const GIntBig nMaxCount = nFIDCount1 + nFIDCount2;
420
0
    GIntBig *panFIDList = static_cast<GIntBig *>(
421
0
        CPLMalloc(static_cast<size_t>(nMaxCount + 1) * sizeof(GIntBig)));
422
0
    nFIDCount = 0;
423
424
0
    for (GIntBig i1 = 0, i2 = 0; i1 < nFIDCount1 || i2 < nFIDCount2;)
425
0
    {
426
0
        if (i1 < nFIDCount1 && i2 < nFIDCount2)
427
0
        {
428
0
            const GIntBig nVal1 = panFIDList1[i1];
429
0
            const GIntBig nVal2 = panFIDList2[i2];
430
0
            if (nVal1 < nVal2)
431
0
            {
432
0
                if (i1 + 1 < nFIDCount1 && panFIDList1[i1 + 1] <= nVal2)
433
0
                {
434
0
                    panFIDList[nFIDCount++] = nVal1;
435
0
                    i1++;
436
0
                }
437
0
                else
438
0
                {
439
0
                    panFIDList[nFIDCount++] = nVal1;
440
0
                    panFIDList[nFIDCount++] = nVal2;
441
0
                    i1++;
442
0
                    i2++;
443
0
                }
444
0
            }
445
0
            else if (nVal1 == nVal2)
446
0
            {
447
0
                panFIDList[nFIDCount++] = nVal1;
448
0
                i1++;
449
0
                i2++;
450
0
            }
451
0
            else
452
0
            {
453
0
                if (i2 + 1 < nFIDCount2 && panFIDList2[i2 + 1] <= nVal1)
454
0
                {
455
0
                    panFIDList[nFIDCount++] = nVal2;
456
0
                    i2++;
457
0
                }
458
0
                else
459
0
                {
460
0
                    panFIDList[nFIDCount++] = nVal2;
461
0
                    panFIDList[nFIDCount++] = nVal1;
462
0
                    i1++;
463
0
                    i2++;
464
0
                }
465
0
            }
466
0
        }
467
0
        else if (i1 < nFIDCount1)
468
0
        {
469
0
            const GIntBig nVal1 = panFIDList1[i1];
470
0
            panFIDList[nFIDCount++] = nVal1;
471
0
            i1++;
472
0
        }
473
0
        else if (i2 < nFIDCount2)
474
0
        {
475
0
            const GIntBig nVal2 = panFIDList2[i2];
476
0
            panFIDList[nFIDCount++] = nVal2;
477
0
            i2++;
478
0
        }
479
0
    }
480
481
0
    panFIDList[nFIDCount] = OGRNullFID;
482
483
0
    return panFIDList;
484
0
}
485
486
// The input arrays must be sorted.
487
static GIntBig *OGRANDGIntBigArray(GIntBig panFIDList1[], GIntBig nFIDCount1,
488
                                   GIntBig panFIDList2[], GIntBig nFIDCount2,
489
                                   GIntBig &nFIDCount)
490
0
{
491
0
    GIntBig nMaxCount = std::max(nFIDCount1, nFIDCount2);
492
0
    GIntBig *panFIDList = static_cast<GIntBig *>(
493
0
        CPLMalloc(static_cast<size_t>(nMaxCount + 1) * sizeof(GIntBig)));
494
0
    nFIDCount = 0;
495
496
0
    for (GIntBig i1 = 0, i2 = 0; i1 < nFIDCount1 && i2 < nFIDCount2;)
497
0
    {
498
0
        const GIntBig nVal1 = panFIDList1[i1];
499
0
        const GIntBig nVal2 = panFIDList2[i2];
500
0
        if (nVal1 < nVal2)
501
0
        {
502
0
            if (i1 + 1 < nFIDCount1 && panFIDList1[i1 + 1] <= nVal2)
503
0
            {
504
0
                i1++;
505
0
            }
506
0
            else
507
0
            {
508
0
                i1++;
509
0
                i2++;
510
0
            }
511
0
        }
512
0
        else if (nVal1 == nVal2)
513
0
        {
514
0
            panFIDList[nFIDCount++] = nVal1;
515
0
            i1++;
516
0
            i2++;
517
0
        }
518
0
        else
519
0
        {
520
0
            if (i2 + 1 < nFIDCount2 && panFIDList2[i2 + 1] <= nVal1)
521
0
            {
522
0
                i2++;
523
0
            }
524
0
            else
525
0
            {
526
0
                i1++;
527
0
                i2++;
528
0
            }
529
0
        }
530
0
    }
531
532
0
    panFIDList[nFIDCount] = OGRNullFID;
533
534
0
    return panFIDList;
535
0
}
536
537
GIntBig *OGRFeatureQuery::EvaluateAgainstIndices(const swq_expr_node *psExpr,
538
                                                 OGRLayer *poLayer,
539
                                                 GIntBig &nFIDCount)
540
0
{
541
    // Does the expression meet our requirements?
542
0
    if (psExpr == nullptr || psExpr->eNodeType != SNT_OPERATION)
543
0
        return nullptr;
544
545
0
    if ((psExpr->nOperation == SWQ_OR || psExpr->nOperation == SWQ_AND) &&
546
0
        psExpr->nSubExprCount == 2)
547
0
    {
548
0
        GIntBig nFIDCount1 = 0;
549
0
        GIntBig nFIDCount2 = 0;
550
0
        GIntBig *panFIDList1 =
551
0
            EvaluateAgainstIndices(psExpr->papoSubExpr[0], poLayer, nFIDCount1);
552
0
        GIntBig *panFIDList2 =
553
0
            panFIDList1 == nullptr
554
0
                ? nullptr
555
0
                : EvaluateAgainstIndices(psExpr->papoSubExpr[1], poLayer,
556
0
                                         nFIDCount2);
557
0
        GIntBig *panFIDList = nullptr;
558
0
        if (panFIDList1 != nullptr && panFIDList2 != nullptr)
559
0
        {
560
0
            if (psExpr->nOperation == SWQ_OR)
561
0
                panFIDList =
562
0
                    OGRORGIntBigArray(panFIDList1, nFIDCount1, panFIDList2,
563
0
                                      nFIDCount2, nFIDCount);
564
0
            else if (psExpr->nOperation == SWQ_AND)
565
0
                panFIDList =
566
0
                    OGRANDGIntBigArray(panFIDList1, nFIDCount1, panFIDList2,
567
0
                                       nFIDCount2, nFIDCount);
568
0
        }
569
0
        CPLFree(panFIDList1);
570
0
        CPLFree(panFIDList2);
571
0
        return panFIDList;
572
0
    }
573
574
0
    if (!(psExpr->nOperation == SWQ_EQ || psExpr->nOperation == SWQ_IN) ||
575
0
        psExpr->nSubExprCount < 2)
576
0
        return nullptr;
577
578
0
    const swq_expr_node *poColumn = psExpr->papoSubExpr[0];
579
0
    const swq_expr_node *poValue = psExpr->papoSubExpr[1];
580
581
0
    if (poColumn->eNodeType != SNT_COLUMN || poValue->eNodeType != SNT_CONSTANT)
582
0
        return nullptr;
583
584
0
    const int nIdx = OGRFeatureFetcherFixFieldIndex(poLayer->GetLayerDefn(),
585
0
                                                    poColumn->field_index);
586
587
0
    OGRAttrIndex *poIndex = poLayer->GetIndex()->GetFieldIndex(nIdx);
588
0
    if (poIndex == nullptr)
589
0
        return nullptr;
590
591
    // Have an index, now we need to query it.
592
0
    OGRField sValue;
593
0
    const OGRFieldDefn *poFieldDefn =
594
0
        poLayer->GetLayerDefn()->GetFieldDefn(nIdx);
595
596
    // Handle the case of an IN operation.
597
0
    if (psExpr->nOperation == SWQ_IN)
598
0
    {
599
0
        int nLength = 0;
600
0
        GIntBig *panFIDs = nullptr;
601
0
        nFIDCount = 0;
602
603
0
        for (int iIN = 1; iIN < psExpr->nSubExprCount; iIN++)
604
0
        {
605
0
            switch (poFieldDefn->GetType())
606
0
            {
607
0
                case OFTInteger:
608
0
                    if (psExpr->papoSubExpr[iIN]->field_type == SWQ_FLOAT)
609
0
                        sValue.Integer = static_cast<int>(
610
0
                            psExpr->papoSubExpr[iIN]->float_value);
611
0
                    else
612
0
                        sValue.Integer = static_cast<int>(
613
0
                            psExpr->papoSubExpr[iIN]->int_value);
614
0
                    break;
615
616
0
                case OFTInteger64:
617
0
                    if (psExpr->papoSubExpr[iIN]->field_type == SWQ_FLOAT)
618
0
                        sValue.Integer64 = static_cast<GIntBig>(
619
0
                            psExpr->papoSubExpr[iIN]->float_value);
620
0
                    else
621
0
                        sValue.Integer64 = psExpr->papoSubExpr[iIN]->int_value;
622
0
                    break;
623
624
0
                case OFTReal:
625
0
                    sValue.Real = psExpr->papoSubExpr[iIN]->float_value;
626
0
                    break;
627
628
0
                case OFTString:
629
0
                    sValue.String = psExpr->papoSubExpr[iIN]->string_value;
630
0
                    break;
631
632
0
                default:
633
0
                    CPLAssert(false);
634
0
                    return nullptr;
635
0
            }
636
637
0
            int nFIDCount32 = static_cast<int>(nFIDCount);
638
0
            panFIDs = poIndex->GetAllMatches(&sValue, panFIDs, &nFIDCount32,
639
0
                                             &nLength);
640
0
            nFIDCount = nFIDCount32;
641
0
        }
642
643
0
        if (nFIDCount > 1)
644
0
        {
645
            // The returned FIDs are expected to be in sorted order.
646
0
            std::sort(panFIDs, panFIDs + nFIDCount);
647
0
        }
648
0
        return panFIDs;
649
0
    }
650
651
    // Handle equality test.
652
0
    switch (poFieldDefn->GetType())
653
0
    {
654
0
        case OFTInteger:
655
0
            if (poValue->field_type == SWQ_FLOAT)
656
0
                sValue.Integer = static_cast<int>(poValue->float_value);
657
0
            else
658
0
                sValue.Integer = static_cast<int>(poValue->int_value);
659
0
            break;
660
661
0
        case OFTInteger64:
662
0
            if (poValue->field_type == SWQ_FLOAT)
663
0
                sValue.Integer64 = static_cast<GIntBig>(poValue->float_value);
664
0
            else
665
0
                sValue.Integer64 = poValue->int_value;
666
0
            break;
667
668
0
        case OFTReal:
669
0
            sValue.Real = poValue->float_value;
670
0
            break;
671
672
0
        case OFTString:
673
0
            sValue.String = poValue->string_value;
674
0
            break;
675
676
0
        default:
677
0
            CPLAssert(false);
678
0
            return nullptr;
679
0
    }
680
681
0
    int nLength = 0;
682
0
    int nFIDCount32 = 0;
683
0
    GIntBig *panFIDs =
684
0
        poIndex->GetAllMatches(&sValue, nullptr, &nFIDCount32, &nLength);
685
0
    nFIDCount = nFIDCount32;
686
0
    if (nFIDCount > 1)
687
0
    {
688
        // The returned FIDs are expected to be sorted.
689
0
        std::sort(panFIDs, panFIDs + nFIDCount);
690
0
    }
691
0
    return panFIDs;
692
0
}
693
694
/************************************************************************/
695
/*                         OGRFieldCollector()                          */
696
/*                                                                      */
697
/*      Helper function for recursing through tree to satisfy           */
698
/*      GetUsedFields().                                                */
699
/************************************************************************/
700
701
char **OGRFeatureQuery::FieldCollector(void *pBareOp, char **papszList)
702
703
0
{
704
0
    swq_expr_node *op = static_cast<swq_expr_node *>(pBareOp);
705
706
    // References to tables other than the primarily are currently unsupported.
707
    // Error out.
708
0
    if (op->eNodeType == SNT_COLUMN)
709
0
    {
710
0
        if (op->table_index != 0)
711
0
        {
712
0
            CSLDestroy(papszList);
713
0
            return nullptr;
714
0
        }
715
716
        // Add the field name into our list if it is not already there.
717
0
        const char *pszFieldName = nullptr;
718
0
        const int nIdx =
719
0
            OGRFeatureFetcherFixFieldIndex(poTargetDefn, op->field_index);
720
721
0
        if (nIdx >= poTargetDefn->GetFieldCount() &&
722
0
            nIdx < poTargetDefn->GetFieldCount() + SPECIAL_FIELD_COUNT)
723
0
        {
724
0
            pszFieldName =
725
0
                SpecialFieldNames[nIdx - poTargetDefn->GetFieldCount()];
726
0
        }
727
0
        else if (nIdx >= 0 && nIdx < poTargetDefn->GetFieldCount())
728
0
        {
729
0
            auto poFieldDefn = poTargetDefn->GetFieldDefn(nIdx);
730
0
            if (!poFieldDefn)
731
0
            {
732
0
                CPLAssert(false);
733
0
                CSLDestroy(papszList);
734
0
                return nullptr;
735
0
            }
736
0
            pszFieldName = poFieldDefn->GetNameRef();
737
0
        }
738
0
        else
739
0
        {
740
0
            CSLDestroy(papszList);
741
0
            return nullptr;
742
0
        }
743
744
0
        if (CSLFindString(papszList, pszFieldName) == -1)
745
0
            papszList = CSLAddString(papszList, pszFieldName);
746
0
    }
747
748
    // Add in fields from subexpressions.
749
0
    if (op->eNodeType == SNT_OPERATION)
750
0
    {
751
0
        for (int iSubExpr = 0; iSubExpr < op->nSubExprCount; iSubExpr++)
752
0
        {
753
0
            papszList = FieldCollector(op->papoSubExpr[iSubExpr], papszList);
754
0
        }
755
0
    }
756
757
0
    return papszList;
758
0
}
759
760
/************************************************************************/
761
/*                           GetUsedFields()                            */
762
/************************************************************************/
763
764
/**
765
 * Returns lists of fields in expression.
766
 *
767
 * All attribute fields are used in the expression of this feature
768
 * query are returned as a StringList of field names.  This function would
769
 * primarily be used within drivers to recognise special case conditions
770
 * depending only on attribute fields that can be very efficiently
771
 * fetched.
772
 *
773
 * NOTE: If any fields in the expression are from tables other than the
774
 * primary table then NULL is returned indicating an error.  In successful
775
 * use, no non-empty expression should return an empty list.
776
 *
777
 * @return list of field names.  Free list with CSLDestroy() when no longer
778
 * required.
779
 */
780
781
char **OGRFeatureQuery::GetUsedFields()
782
783
0
{
784
0
    if (pSWQExpr == nullptr)
785
0
        return nullptr;
786
787
0
    return FieldCollector(pSWQExpr, nullptr);
788
0
}
789
790
//! @endcond