PostgreSQL Source Code: src/backend/utils/adt/multirangetypes_selfuncs.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

19

20#include <math.h>

21

26#include "utils/fmgrprotos.h"

32

40 Oid operator);

72

73

74

75

76

77static double

79{

80 switch (operator)

81 {

85 return 0.01;

86

93 return 0.005;

94

97

98

99

100

101

103

120

122

123 default:

124

125

126

127

128

129 return 0.01;

130 }

131}

132

133

134

135

138{

150

151

152

153

154

158

159

160

161

163 {

166 }

167

168

169

170

171

173 {

176 }

177

178

179

180

181

183 {

184

186 if (!operator)

187 {

188

191 }

192 }

193

194

195

196

197

198

199

200

201

202

203

204

206 {

208

210 {

213

216 lower.infinite = false;

217 lower.lower = true;

218 upper.inclusive = true;

220 upper.infinite = false;

221 upper.lower = false;

223 false, NULL);

226 }

227 }

235 {

236

237

238

239

242 {

246 }

247 }

256 {

257

258

259

260

261

262

263 }

265 {

266

268

270 }

271

272

273

274

275

276

277

280 else

282

284

286

288}

289

290static double

293{

298

299

300

301

302

304 {

307

310

311

316 {

317 if (sslot.nnumbers != 1)

318 elog(ERROR, "invalid empty fraction statistic");

321 }

322 else

323 {

324

326 }

327 }

328 else

329 {

330

331

332

333

334

335

338 }

339

341 {

342

343

344

345

346 switch (operator)

347 {

348

359

362 break;

363

364

365

366

367

370

373 break;

374

375

378

381 break;

382

383

386 break;

387

388

390

391

400

401 default:

402 elog(ERROR, "unexpected operator %u", operator);

403 selec = 0.0;

404 break;

405 }

406 }

407 else

408 {

409

410

411

412

413

414

415

416

418 operator);

421

422

423

424

425

426

429 {

430

432 }

433 else

434 {

435

437 }

438 }

439

440

442

443

445

447}

448

449

450

451

452

453

454

455static double

458{

465 int i;

470

471

474 return -1;

478 return -1;

479

480

485 return -1.0;

486

487

488 if (hslot.nvalues < 2)

489 {

491 return -1.0;

492 }

493

494

495

496

497

502 {

503 bool empty;

504

507

508 if (empty)

509 elog(ERROR, "bounds histogram contains an empty range");

510 }

511

512

517 {

523 {

525 return -1.0;

526 }

527

528

529 if (lslot.nvalues < 2)

530 {

533 return -1.0;

534 }

535 }

536 else

538

539

545

546

547

548

549

550 switch (operator)

551 {

553

554

555

556

557

558

559

560

561

565 break;

566

571 break;

572

577 break;

578

583 break;

584

587

591 break;

592

595

599 break;

600

603

607 break;

608

611

615 break;

616

620

621

622

623

624

625

626

627

628

629

630

631

632

642 break;

643

650 break;

651

655 {

656

657

658

659

663 }

665 {

669 }

670 else

671 {

676 }

677 break;

678

679

688

689 default:

690 elog(ERROR, "unknown multirange operator %u", operator);

691 hist_selec = -1.0;

692 break;

693 }

694

697

699}

700

701

702

703

704

705

706static double

709{

712

713

714

715

716

719

720

724

726}

727

728

729

730

731

732

733

734

735

736

737

738static int

741{

746

748 {

751

754 else

756 }

758}

759

760

761

762

763

764

765

766

767static int

770{

774

776 {

778

780

784 else

786 }

788}

789

790

791

792

796{

799

800 if (hist1->infinite && hist2->infinite)

801 {

803

804

805

806

807

808

809

810 if (value->infinite)

811 return 0.5;

812

813

815 return 0.5;

816

817

823 return 0.5;

824

830

831 if (isnan(position))

832 return 0.5;

833

834

835 position = Max(position, 0.0);

836 position = Min(position, 1.0);

837 return position;

838 }

839 else if (hist1->infinite && hist2->infinite)

840 {

841

842

843

844

845

846

847 return ((value->infinite && value->lower) ? 0.0 : 1.0);

848 }

849 else if (hist1->infinite && hist2->infinite)

850 {

851

852 return ((value->infinite && value->lower) ? 1.0 : 0.0);

853 }

854 else

855 {

856

857

858

859

860

861

862

863

864 return 0.5;

865 }

866}

867

868

869

870

871

872static double

874{

876 {

877

878

879

880

881

883 return 0.5;

884

886 }

888 {

889

890

891

892

893 return 1.0;

894 }

896 {

897

898 return 0.0;

899 }

900 else

901 {

902

903

904

905

906

907

908

909

910 return 0.5;

911 }

912}

913

914

915

916

919{

921

923 {

924

925

926

927

929 {

931

936

937 if (isnan(res) || res < 0.0)

938 return 1.0;

939 else

940 return res;

941 }

942 else

943 return 1.0;

944 }

945 else if (bound1->infinite && bound2->infinite)

946 {

947

949 return 0.0;

950 else

952 }

953 else

954 {

955

957 }

958}

959

960

961

962

963

964

965static double

968{

970 double A,

971 B,

974 double pos;

975 int i;

976 double area;

977

979

981 return 0.0;

982

983

985 return 1.0;

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1028 return 1.0;

1029

1030 if (i < 0)

1031 {

1032 i = 0;

1033 pos = 0.0;

1034 }

1035 else

1036 {

1037

1041 }

1044

1045

1046

1047

1048

1049

1051 return PB;

1052

1053

1054

1055

1056

1057

1058 area = 0.0;

1060 {

1062

1063

1065 break;

1066

1067

1068 A = B;

1070

1073

1074

1075

1076

1077

1078

1079

1080 if (PA > 0 || PB > 0)

1081 area += 0.5 * (PB + PA) * (B - A);

1082 }

1083

1084

1085 A = B;

1087

1088 B = length2;

1090 pos = 0.0;

1091 else

1092 {

1094 pos = 0.0;

1095 else

1099 }

1101

1102 if (PA > 0 || PB > 0)

1103 area += 0.5 * (PB + PA) * (B - A);

1104

1105

1106

1107

1108

1109

1110

1111

1112

1115 else

1117

1118 return frac;

1119}

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130static double

1135{

1136 int i,

1142

1143

1144

1145

1146

1147

1148 upper->inclusive = upper->inclusive;

1149 upper->lower = true;

1151 false);

1152

1153

1154

1155

1156

1158 return 0.0;

1159

1160

1161

1162

1163

1164

1165

1166

1167

1169

1170

1171

1172

1173

1174

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1190

1193 {

1194 double dist;

1197

1198

1199

1200

1201

1202

1203

1205 {

1207

1208

1209

1210

1211

1217 }

1218 else

1220

1221

1222

1223

1224

1228

1229

1230

1231

1232

1234

1236 break;

1237

1240 }

1241

1243}

1244

1245

1246

1247

1248

1249

1250

1251static double

1256{

1257 int i,

1263

1264

1266 true);

1267

1268

1269

1270

1271

1273 return 0.0;

1274

1275

1276

1277

1278

1279

1280

1281

1282

1284

1285

1286

1287

1288

1289

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1309 {

1312

1313

1314

1315

1316

1317

1319

1320

1321

1322

1323

1328

1330

1333 }

1334

1336}

#define Assert(condition)

#define OidIsValid(objectId)

bool equal(const void *a, const void *b)

#define palloc_array(type, count)

static float8 get_float8_infinity(void)

Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)

#define PG_RETURN_FLOAT8(x)

#define PG_GETARG_POINTER(n)

#define PG_GETARG_INT32(n)

#define HeapTupleIsValid(tuple)

static void * GETSTRUCT(const HeapTupleData *tuple)

void free_attstatsslot(AttStatsSlot *sslot)

bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)

Oid get_commutator(Oid opno)

#define ATTSTATSSLOT_NUMBERS

#define ATTSTATSSLOT_VALUES

MultirangeType * make_multirange(Oid mltrngtypoid, TypeCacheEntry *rangetyp, int32 range_count, RangeType **ranges)

void multirange_get_bounds(TypeCacheEntry *rangetyp, const MultirangeType *multirange, uint32 i, RangeBound *lower, RangeBound *upper)

TypeCacheEntry * multirange_get_typcache(FunctionCallInfo fcinfo, Oid mltrngtypid)

#define MultirangeIsEmpty(mr)

static MultirangeType * DatumGetMultirangeTypeP(Datum X)

static double calc_hist_selectivity_contains(TypeCacheEntry *typcache, const RangeBound *lower, const RangeBound *upper, const RangeBound *hist_lower, int hist_nvalues, const Datum *length_hist_values, int length_hist_nvalues)

static float8 get_position(TypeCacheEntry *typcache, const RangeBound *value, const RangeBound *hist1, const RangeBound *hist2)

static double calc_hist_selectivity_scalar(TypeCacheEntry *typcache, const RangeBound *constbound, const RangeBound *hist, int hist_nvalues, bool equal)

static double calc_multirangesel(TypeCacheEntry *typcache, VariableStatData *vardata, const MultirangeType *constval, Oid operator)

static int length_hist_bsearch(const Datum *length_hist_values, int length_hist_nvalues, double value, bool equal)

static int rbound_bsearch(TypeCacheEntry *typcache, const RangeBound *value, const RangeBound *hist, int hist_length, bool equal)

static double calc_hist_selectivity(TypeCacheEntry *typcache, VariableStatData *vardata, const MultirangeType *constval, Oid operator)

static double default_multirange_selectivity(Oid operator)

static float8 get_len_position(double value, double hist1, double hist2)

static double calc_length_hist_frac(const Datum *length_hist_values, int length_hist_nvalues, double length1, double length2, bool equal)

Datum multirangesel(PG_FUNCTION_ARGS)

static double calc_hist_selectivity_contained(TypeCacheEntry *typcache, const RangeBound *lower, RangeBound *upper, const RangeBound *hist_lower, int hist_nvalues, const Datum *length_hist_values, int length_hist_nvalues)

static float8 get_distance(TypeCacheEntry *typcache, const RangeBound *bound1, const RangeBound *bound2)

#define IsA(nodeptr, _type_)

Datum lower(PG_FUNCTION_ARGS)

Datum upper(PG_FUNCTION_ARGS)

FormData_pg_statistic * Form_pg_statistic

static float8 DatumGetFloat8(Datum X)

int range_cmp_bounds(TypeCacheEntry *typcache, const RangeBound *b1, const RangeBound *b2)

RangeType * range_serialize(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty, struct Node *escontext)

void range_deserialize(TypeCacheEntry *typcache, const RangeType *range, RangeBound *lower, RangeBound *upper, bool *empty)

static RangeType * DatumGetRangeTypeP(Datum X)

static int cmp(const chr *x, const chr *y, size_t len)

bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)

bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)

#define ReleaseVariableStats(vardata)

#define CLAMP_PROBABILITY(p)

#define DEFAULT_MULTIRANGE_INEQ_SEL

struct TypeCacheEntry * rngelemtype

struct TypeCacheEntry * rngtype

FmgrInfo rng_subdiff_finfo