Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0490567

Browse files
author
Nikita Glukhov
committed
Extract jsonb_sel_internal() from jsonb_sel()
1 parent ea33032 commit 0490567

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

src/backend/utils/adt/jsonb_selfuncs.c

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,50 +1307,16 @@ jsonSelectivityExists(JsonStats stats, Datum key)
13071307
return sel;
13081308
}
13091309

1310-
/*
1311-
* jsonb_sel
1312-
* The main procedure estimating selectivity for all JSONB operators.
1313-
*/
1314-
Datum
1315-
jsonb_sel(PG_FUNCTION_ARGS)
1310+
static Selectivity
1311+
jsonb_sel_internal(JsonStats stats, Oid operator, Const *cnst, bool varonleft)
13161312
{
1317-
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1318-
Oid operator = PG_GETARG_OID(1);
1319-
List *args = (List *) PG_GETARG_POINTER(2);
1320-
int varRelid = PG_GETARG_INT32(3);
1321-
double sel = DEFAULT_JSON_CONTAINS_SEL;
1322-
Node *other;
1323-
Const *cnst;
1324-
bool varonleft;
1325-
JsonStatData stats;
1326-
VariableStatData vardata;
1327-
1328-
if (!get_restriction_variable(root, args, varRelid,
1329-
&vardata, &other, &varonleft))
1330-
PG_RETURN_FLOAT8(sel);
1331-
1332-
if (!IsA(other, Const))
1333-
goto out;
1334-
1335-
cnst = (Const *) other;
1336-
1337-
if (cnst->constisnull)
1338-
{
1339-
sel = 0.0;
1340-
goto out;
1341-
}
1342-
1343-
if (!jsonStatsInit(&stats, &vardata))
1344-
goto out;
1345-
13461313
switch (operator)
13471314
{
13481315
case JsonbExistsOperator:
13491316
if (!varonleft || cnst->consttype != TEXTOID)
1350-
goto out;
1317+
break;
13511318

1352-
sel = jsonSelectivityExists(&stats, cnst->constvalue);
1353-
break;
1319+
return jsonSelectivityExists(stats, cnst->constvalue);
13541320

13551321
case JsonbExistsAnyOperator:
13561322
case JsonbExistsAllOperator:
@@ -1363,15 +1329,15 @@ jsonb_sel(PG_FUNCTION_ARGS)
13631329
bool all = operator == JsonbExistsAllOperator;
13641330

13651331
if (!varonleft || cnst->consttype != TEXTARRAYOID)
1366-
goto out;
1332+
break;
13671333

13681334
deconstruct_array(DatumGetArrayTypeP(cnst->constvalue), TEXTOID,
13691335
-1, false, 'i', &keys, &nulls, &nkeys);
13701336

13711337
for (i = 0; i < nkeys; i++)
13721338
if (!nulls[i])
13731339
{
1374-
Selectivity pathfreq = jsonSelectivityExists(&stats,
1340+
Selectivity pathfreq = jsonSelectivityExists(stats,
13751341
keys[i]);
13761342
freq *= all ? pathfreq : (1.0 - pathfreq);
13771343
}
@@ -1382,30 +1348,69 @@ jsonb_sel(PG_FUNCTION_ARGS)
13821348
if (!all)
13831349
freq = 1.0 - freq;
13841350

1385-
sel = freq;
1386-
break;
1351+
return freq;
13871352
}
13881353

13891354
case JsonbContainedOperator:
13901355
if (varonleft || cnst->consttype != JSONBOID)
1391-
goto out;
1356+
break;
13921357

1393-
sel = jsonSelectivityContains(&stats,
1394-
DatumGetJsonbP(cnst->constvalue));
1395-
break;
1358+
return jsonSelectivityContains(stats,
1359+
DatumGetJsonbP(cnst->constvalue));
13961360

13971361
case JsonbContainsOperator:
13981362
if (!varonleft || cnst->consttype != JSONBOID)
1399-
goto out;
1363+
break;
14001364

1401-
sel = jsonSelectivityContains(&stats,
1402-
DatumGetJsonbP(cnst->constvalue));
1365+
return jsonSelectivityContains(stats,
1366+
DatumGetJsonbP(cnst->constvalue));
1367+
1368+
default:
14031369
break;
14041370
}
14051371

1406-
out:
1407-
jsonStatsRelease(&stats);
1408-
ReleaseVariableStats(vardata);
1372+
return DEFAULT_JSON_CONTAINS_SEL;
1373+
}
1374+
1375+
/*
1376+
* jsonb_sel
1377+
* The main procedure estimating selectivity for all JSONB operators.
1378+
*/
1379+
Datum
1380+
jsonb_sel(PG_FUNCTION_ARGS)
1381+
{
1382+
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
1383+
Oid operator = PG_GETARG_OID(1);
1384+
List *args = (List *) PG_GETARG_POINTER(2);
1385+
int varRelid = PG_GETARG_INT32(3);
1386+
double sel = DEFAULT_JSON_CONTAINS_SEL;
1387+
Node *other;
1388+
bool varonleft;
1389+
VariableStatData vardata;
1390+
1391+
if (get_restriction_variable(root, args, varRelid,
1392+
&vardata, &other, &varonleft))
1393+
{
1394+
if (IsA(other, Const))
1395+
{
1396+
Const *cnst = (Const *) other;
1397+
1398+
if (cnst->constisnull)
1399+
sel = 0.0;
1400+
else
1401+
{
1402+
JsonStatData stats;
1403+
1404+
if (jsonStatsInit(&stats, &vardata))
1405+
{
1406+
sel = jsonb_sel_internal(&stats, operator, cnst, varonleft);
1407+
jsonStatsRelease(&stats);
1408+
}
1409+
}
1410+
}
1411+
1412+
ReleaseVariableStats(vardata);
1413+
}
14091414

14101415
PG_RETURN_FLOAT8((float8) sel);
14111416
}

0 commit comments

Comments
 (0)