lbug 0.22.0

An in-process property graph database management system built for query speed and scalability
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
#include "planner/join_order/cardinality_estimator.h"

#include "binder/expression/literal_expression.h"
#include "binder/expression/property_expression.h"
#include "binder/expression/scalar_function_expression.h"
#include "catalog/catalog.h"
#include "catalog/catalog_entry/node_table_catalog_entry.h"
#include "catalog/catalog_entry/rel_group_catalog_entry.h"
#include "catalog/catalog_entry/table_catalog_entry.h"
#include "common/enums/extend_direction_util.h"
#include "function/list/vector_list_functions.h"
#include "main/client_context.h"
#include "main/database_manager.h"
#include "planner/join_order/join_order_util.h"
#include "planner/operator/logical_aggregate.h"
#include "planner/operator/logical_hash_join.h"
#include "planner/operator/scan/logical_scan_node_table.h"
#include "storage/partition_storage_registry.h"
#include "storage/stats/planner_stats.h"
#include "storage/storage_manager.h"
#include "storage/table/node_table.h"

using namespace lbug::binder;
using namespace lbug::common;
using namespace lbug::transaction;

namespace lbug {
namespace planner {

static cardinality_t atLeastOne(uint64_t x) {
    return x == 0 ? 1 : x;
}

static PlannerTableStats getPlannerStats(main::ClientContext* context,
    storage::StorageManager& storageManager, catalog::Catalog& catalog,
    catalog::TableCatalogEntry& tableEntry, table_id_t physicalTableID = INVALID_TABLE_ID) {
    const auto tableID =
        physicalTableID == INVALID_TABLE_ID ? tableEntry.getTableID() : physicalTableID;
    auto* table = storageManager.containsTable(tableID) ?
                      storageManager.getTable(tableID) :
                      // Partition children resolve through their own StorageManager.
                      storage::PartitionStorageRegistry::resolveNodeTableByID(context, tableID);
    if (auto cachedStats = storageManager.getCachedPlannerTableStats(tableID);
        cachedStats.has_value() && cachedStats->tableChangeEpoch == table->getChangeEpoch()) {
        return std::move(cachedStats.value());
    }
    // No fresh cache entry: build full ANALYZE stats (real CSR degree distributions for
    // rel tables) so one-hop extend fan-out estimation sees true degrees instead of
    // schema-only row counts, and cache them for subsequent plannings on this epoch.
    auto stats = storage::buildPlannerTableStats(storageManager, catalog,
        transaction::Transaction::Get(*context), tableEntry, physicalTableID,
        storage::PlannerStatsMode::ANALYZE);
    storageManager.setCachedPlannerTableStats(stats.copy());
    return stats;
}

void CardinalityEstimator::init(const QueryGraph& queryGraph) {
    for (auto i = 0u; i < queryGraph.getNumQueryNodes(); ++i) {
        init(*queryGraph.getQueryNode(i));
    }
    for (uint64_t i = 0u; i < queryGraph.getNumQueryRels(); ++i) {
        auto rel = queryGraph.getQueryRel(i);
        init(*rel);
        if (QueryRelTypeUtils::isRecursive(rel->getRelType())) {
            auto recursiveInfo = rel->getRecursiveInfo();
            init(*recursiveInfo->node);
            init(*recursiveInfo->rel);
        }
    }
}

void CardinalityEstimator::init(const NodeExpression& node) {
    auto key = node.getInternalID()->getUniqueName();
    cardinality_t numNodes = 0u;
    for (auto entry : node.getEntries()) {
        // Skip foreign tables and other scan-function-backed entries (e.g. remotely routed
        // partition substitutes) - they don't have storage in the local database.
        if (entry->getType() == catalog::CatalogEntryType::FOREIGN_TABLE_ENTRY ||
            (entry->getType() == catalog::CatalogEntryType::NODE_TABLE_ENTRY &&
                entry->ptrCast<catalog::NodeTableCatalogEntry>()->getScanFunction().has_value())) {
            continue;
        }
        auto tableID = entry->getTableID();
        auto dbName = node.getDbName(entry);
        if (!dbName.empty()) {
            // Entries from non-lbug attached databases (DuckDB, Postgres,
            // Iceberg, ...) have no local storage stats. Skipping them also
            // avoids downcasting a foreign attached database to
            // AttachedLbugDatabase, which is undefined behaviour.
            auto* attachedDB = main::DatabaseManager::Get(*context)->getAttachedDatabase(dbName);
            if (attachedDB == nullptr || attachedDB->getDBType() != common::ATTACHED_LBUG_DB_TYPE) {
                continue;
            }
        }
        storage::StorageManager* storageManager;
        catalog::Catalog* cat;
        if (!dbName.empty()) {
            auto* attachedDB = main::DatabaseManager::Get(*context)->getAttachedDatabase(dbName);
            auto* attachedLbugDB = static_cast<main::AttachedLbugDatabase*>(attachedDB);
            storageManager = attachedLbugDB->getStorageManager();
            cat = attachedDB->getCatalog();
        } else {
            storageManager = storage::StorageManager::Get(*context);
            cat = catalog::Catalog::Get(*context);
        }
        auto stats = getPlannerStats(context, *storageManager, *cat, *entry);
        DASSERT(stats.storageStats.has_value());
        numNodes += stats.storageStats->getTableCard();
        if (!tableStats.contains(tableID)) {
            tableStats.insert({tableID, std::move(stats)});
        }
    }
    if (!nodeIDName2dom.contains(key)) {
        nodeIDName2dom.insert({key, numNodes});
    }
}

void CardinalityEstimator::init(const RelExpression& rel) {
    for (auto entry : rel.getEntries()) {
        if (entry->getType() == catalog::CatalogEntryType::FOREIGN_TABLE_ENTRY) {
            continue;
        }
        auto dbName = rel.getDbName(entry);
        if (!dbName.empty()) {
            // See the node overload above: foreign attached tables carry no
            // local stats and must not be downcast to AttachedLbugDatabase.
            auto* attachedDB = main::DatabaseManager::Get(*context)->getAttachedDatabase(dbName);
            if (attachedDB == nullptr || attachedDB->getDBType() != common::ATTACHED_LBUG_DB_TYPE) {
                continue;
            }
        }
        storage::StorageManager* storageManager;
        catalog::Catalog* cat;
        if (!dbName.empty()) {
            auto* attachedDB = main::DatabaseManager::Get(*context)->getAttachedDatabase(dbName);
            auto* attachedLbugDB = static_cast<main::AttachedLbugDatabase*>(attachedDB);
            storageManager = attachedLbugDB->getStorageManager();
            cat = attachedDB->getCatalog();
        } else {
            storageManager = storage::StorageManager::Get(*context);
            cat = catalog::Catalog::Get(*context);
        }
        auto& relGroupEntry = entry->cast<catalog::RelGroupCatalogEntry>();
        for (const auto& relInfo : relGroupEntry.getRelEntryInfos()) {
            const auto tableID = relInfo.oid;
            if (tableStats.contains(tableID)) {
                continue;
            }
            tableStats.insert(
                {tableID, getPlannerStats(context, *storageManager, *cat, *entry, tableID)});
        }
    }
}

void CardinalityEstimator::rectifyCardinality(const Expression& nodeID, cardinality_t card) {
    DASSERT(nodeIDName2dom.contains(nodeID.getUniqueName()));
    auto newCard = std::min(nodeIDName2dom.at(nodeID.getUniqueName()), card);
    nodeIDName2dom[nodeID.getUniqueName()] = newCard;
}

cardinality_t CardinalityEstimator::getNodeIDDom(const std::string& nodeIDName) const {
    DASSERT(nodeIDName2dom.contains(nodeIDName));
    return nodeIDName2dom.at(nodeIDName);
}

uint64_t CardinalityEstimator::estimateScanNode(const LogicalOperator& op) const {
    const auto& scan = op.constCast<const LogicalScanNodeTable&>();
    switch (scan.getScanType()) {
    case LogicalScanNodeTableType::PRIMARY_KEY_SCAN: {
        auto& primaryKeyScanInfo = scan.getExtraInfo()->constCast<PrimaryKeyScanInfo>();
        return primaryKeyScanInfo.isRange ?
                   atLeastOne(getNodeIDDom(scan.getNodeID()->getUniqueName())) :
                   1;
    }
    case LogicalScanNodeTableType::SECONDARY_INDEX_SCAN:
        return atLeastOne(getNodeIDDom(scan.getNodeID()->getUniqueName()));
    default:
        return atLeastOne(getNodeIDDom(scan.getNodeID()->getUniqueName()));
    }
}

uint64_t CardinalityEstimator::estimateAggregate(const LogicalAggregate& op) const {
    if (op.getKeys().empty()) {
        return 1;
    }
    // Output rows = number of distinct key combinations, bounded above by both the input
    // rows and the product of per-key domains. Any key with an unknown domain falls back
    // to the input cardinality (previous behavior).
    const auto childCard = static_cast<double>(op.getChild(0)->getCardinality());
    auto domainProduct = 1.0;
    for (auto& key : op.getKeys()) {
        auto domain = getGroupKeyDomain(*key);
        if (!domain.has_value()) {
            return op.getChild(0)->getCardinality();
        }
        domainProduct *= static_cast<double>(domain.value());
        if (domainProduct >= childCard) {
            return op.getChild(0)->getCardinality();
        }
    }
    return atLeastOne(static_cast<cardinality_t>(std::min(domainProduct, childCard)));
}

cardinality_t CardinalityEstimator::multiply(double extensionRate, cardinality_t card) const {
    return atLeastOne(extensionRate * card);
}

uint64_t CardinalityEstimator::estimateHashJoin(
    const std::vector<binder::expression_pair>& joinConditions, const LogicalOperator& probeOp,
    const LogicalOperator& buildOp) const {
    if (LogicalHashJoin::isNodeIDOnlyJoin(joinConditions)) {
        cardinality_t denominator = 1u;
        auto joinKeys = LogicalHashJoin::getJoinNodeIDs(joinConditions);
        for (auto& joinKey : joinKeys) {
            if (nodeIDName2dom.contains(joinKey->getUniqueName())) {
                denominator *= getNodeIDDom(joinKey->getUniqueName());
            }
        }
        return atLeastOne(probeOp.getCardinality() *
                          JoinOrderUtil::getJoinKeysFlatCardinality(joinKeys, buildOp) /
                          atLeastOne(denominator));
    } else {
        // Naively estimate the cardinality if the join is non-ID based
        cardinality_t estCardinality = probeOp.getCardinality() * buildOp.getCardinality();
        for (size_t i = 0; i < joinConditions.size(); ++i) {
            estCardinality *= PlannerKnobs::EQUALITY_PREDICATE_SELECTIVITY;
        }
        return atLeastOne(estCardinality);
    }
}

uint64_t CardinalityEstimator::estimateHashJoin(const expression_vector& joinNodeIDs,
    const LogicalOperator& probeOp, const LogicalOperator& buildOp) const {
    std::vector<expression_pair> joinConditions;
    joinConditions.reserve(joinNodeIDs.size());
    for (auto& joinNodeID : joinNodeIDs) {
        joinConditions.emplace_back(joinNodeID, joinNodeID);
    }
    return estimateHashJoin(joinConditions, probeOp, buildOp);
}

uint64_t CardinalityEstimator::estimateCrossProduct(const LogicalOperator& probeOp,
    const LogicalOperator& buildOp) const {
    return atLeastOne(probeOp.getCardinality() * buildOp.getCardinality());
}

uint64_t CardinalityEstimator::estimateIntersect(const expression_vector& joinNodeIDs,
    const LogicalOperator& probeOp, const std::vector<LogicalOperator*>& buildOps) const {
    // Formula 1: treat intersect as a Filter on probe side.
    uint64_t estCardinality1 =
        probeOp.getCardinality() * PlannerKnobs::NON_EQUALITY_PREDICATE_SELECTIVITY;
    // Formula 2: assume independence on join conditions.
    cardinality_t denominator = 1u;
    for (auto& joinNodeID : joinNodeIDs) {
        denominator *= getNodeIDDom(joinNodeID->getUniqueName());
    }
    auto numerator = probeOp.getCardinality();
    for (auto& buildOp : buildOps) {
        numerator *= buildOp->getCardinality();
    }
    auto estCardinality2 = numerator / atLeastOne(denominator);
    // Pick minimum between the two formulas.
    return atLeastOne(std::min<uint64_t>(estCardinality1, estCardinality2));
}

uint64_t CardinalityEstimator::estimateFlatten(const LogicalOperator& childOp,
    f_group_pos groupPosToFlatten) const {
    auto group = childOp.getSchema()->getGroup(groupPosToFlatten);
    return atLeastOne(childOp.getCardinality() * group->cardinalityMultiplier);
}

static bool isPrimaryKey(const Expression& expression) {
    if (expression.expressionType != ExpressionType::PROPERTY) {
        return false;
    }
    return ((PropertyExpression&)expression).isPrimaryKey();
}

static bool isSingleLabelledProperty(const Expression& expression) {
    if (expression.expressionType != ExpressionType::PROPERTY) {
        return false;
    }
    return expression.constCast<PropertyExpression>().isSingleLabel();
}

static std::optional<cardinality_t> getPropertyNumDistinct(main::ClientContext* context,
    const Expression& propertyExpr,
    const std::unordered_map<common::table_id_t, PlannerTableStats>& tableStats) {
    if (!isSingleLabelledProperty(propertyExpr)) {
        return {};
    }
    auto& prop = propertyExpr.constCast<PropertyExpression>();
    auto tableID = prop.getSingleTableID();
    if (tableStats.contains(tableID) && tableStats.at(tableID).storageStats.has_value() &&
        prop.hasProperty(tableID)) {
        auto transaction = Transaction::Get(*context);
        auto [cat, sm] = main::DatabaseManager::resolveTableStorage(*context, tableID);
        auto entry = cat->getTableCatalogEntry(transaction, tableID);
        if (!entry->containsProperty(prop.getPropertyName())) {
            return {};
        }
        auto columnID = entry->getColumnID(prop.getPropertyName());
        if (columnID != INVALID_COLUMN_ID && columnID != ROW_IDX_COLUMN_ID) {
            auto& stats = tableStats.at(tableID).storageStats.value();
            return atLeastOne(stats.getNumDistinctValues(columnID));
        }
    }
    return {};
}

static std::optional<cardinality_t> getTableStatsIfPossible(main::ClientContext* context,
    const Expression& predicate,
    const std::unordered_map<common::table_id_t, PlannerTableStats>& tableStats) {
    DASSERT(predicate.getNumChildren() >= 1);
    return getPropertyNumDistinct(context, *predicate.getChild(0), tableStats);
}

// Observed [min, max] of a single-label property's column, for range-selectivity
// estimation. Empty when stats are unavailable or the type is not min/max-tracked.
static std::optional<std::pair<double, double>> getPropertyMinMax(main::ClientContext* context,
    const Expression& propertyExpr,
    const std::unordered_map<common::table_id_t, PlannerTableStats>& tableStats) {
    if (!isSingleLabelledProperty(propertyExpr)) {
        return {};
    }
    auto& prop = propertyExpr.constCast<PropertyExpression>();
    auto tableID = prop.getSingleTableID();
    if (!tableStats.contains(tableID) || !tableStats.at(tableID).storageStats.has_value() ||
        !prop.hasProperty(tableID)) {
        return {};
    }
    auto transaction = Transaction::Get(*context);
    auto [cat, sm] = main::DatabaseManager::resolveTableStorage(*context, tableID);
    auto entry = cat->getTableCatalogEntry(transaction, tableID);
    if (!entry->containsProperty(prop.getPropertyName())) {
        return {};
    }
    auto columnID = entry->getColumnID(prop.getPropertyName());
    if (columnID == INVALID_COLUMN_ID || columnID == ROW_IDX_COLUMN_ID) {
        return {};
    }
    return tableStats.at(tableID).storageStats.value().getColumnMinMax(columnID);
}

// Converts a folded literal to double in the column's physical domain (integers, DATE
// days, TIMESTAMP micros), matching ColumnStats::updateMinMax. Parameters and other
// non-constant expressions stay on the fixed-selectivity path: their values are
// unknown at plan time.
static std::optional<double> literalToDouble(const Expression& expr) {
    if (expr.expressionType != ExpressionType::LITERAL) {
        return {};
    }
    const auto& value = expr.constCast<LiteralExpression>().getValue();
    if (value.isNull()) {
        return {};
    }
    switch (value.getDataType().getLogicalTypeID()) {
    case LogicalTypeID::INT8:
        return value.getValue<int8_t>();
    case LogicalTypeID::INT16:
        return value.getValue<int16_t>();
    case LogicalTypeID::INT32:
        return value.getValue<int32_t>();
    case LogicalTypeID::INT64:
        return static_cast<double>(value.getValue<int64_t>());
    case LogicalTypeID::UINT8:
        return value.getValue<uint8_t>();
    case LogicalTypeID::UINT16:
        return value.getValue<uint16_t>();
    case LogicalTypeID::UINT32:
        return value.getValue<uint32_t>();
    case LogicalTypeID::UINT64:
        return static_cast<double>(value.getValue<uint64_t>());
    case LogicalTypeID::FLOAT:
        return value.getValue<float>();
    case LogicalTypeID::DOUBLE:
        return value.getValue<double>();
    case LogicalTypeID::DATE:
        return value.getValue<date_t>().days;
    case LogicalTypeID::TIMESTAMP:
    case LogicalTypeID::TIMESTAMP_TZ:
    case LogicalTypeID::TIMESTAMP_MS:
    case LogicalTypeID::TIMESTAMP_NS:
    case LogicalTypeID::TIMESTAMP_SEC:
        return static_cast<double>(value.getValue<timestamp_t>().value);
    default:
        return {};
    }
}

// Strips binder-inserted CASTs (e.g. an INT32 column vs an INT64 literal compares as
// CAST(prop) OP literal) to the underlying property. Value-preserving numeric and
// temporal casts keep the range fraction valid.
static const Expression* unwrapCast(const Expression* expr) {
    while (expr->expressionType == ExpressionType::FUNCTION && expr->getNumChildren() == 1) {
        const auto& name = expr->constCast<ScalarFunctionExpression>().getFunction().name;
        if (std::string(name).rfind("CAST", 0) != 0) {
            break;
        }
        expr = expr->getChild(0).get();
    }
    return expr;
}

// Selectivity of `property OP constant` for ordered comparisons, as the fraction of the
// observed [min, max] range satisfying OP under uniformity, clamped to [0, 1]. A filter
// keeping every row (e.g. `c.length >= 0` when min is 0) estimates 1.0 instead of the
// fixed 0.1 below, so the DP comparison sees true cardinalities; a filter outside the
// range estimates 0 (exactly one row via atLeastOne at the call site). Anything else
// (parameters, unresolvable columns, non-literal sides) stays empty for the fallback.
static std::optional<double> getRangeSelectivity(main::ClientContext* context,
    const Expression& predicate,
    const std::unordered_map<common::table_id_t, PlannerTableStats>& tableStats) {
    const auto op = predicate.expressionType;
    if (op != ExpressionType::GREATER_THAN && op != ExpressionType::GREATER_THAN_EQUALS &&
        op != ExpressionType::LESS_THAN && op != ExpressionType::LESS_THAN_EQUALS) {
        return {};
    }
    DASSERT(predicate.getNumChildren() == 2);
    auto lhs = unwrapCast(predicate.getChild(0).get());
    auto rhs = unwrapCast(predicate.getChild(1).get());
    const Expression* propSide = nullptr;
    const Expression* constSide = nullptr;
    auto effectiveOp = op;
    if (isSingleLabelledProperty(*lhs) && rhs->expressionType == ExpressionType::LITERAL) {
        propSide = lhs;
        constSide = rhs;
    } else if (isSingleLabelledProperty(*rhs) && lhs->expressionType == ExpressionType::LITERAL) {
        propSide = rhs;
        constSide = lhs;
        switch (op) {
        case ExpressionType::GREATER_THAN:
            effectiveOp = ExpressionType::LESS_THAN;
            break;
        case ExpressionType::GREATER_THAN_EQUALS:
            effectiveOp = ExpressionType::LESS_THAN_EQUALS;
            break;
        case ExpressionType::LESS_THAN:
            effectiveOp = ExpressionType::GREATER_THAN;
            break;
        default:
            effectiveOp = ExpressionType::GREATER_THAN_EQUALS;
            break;
        }
    } else {
        return {};
    }
    const auto constant = literalToDouble(*constSide);
    const auto minMax = getPropertyMinMax(context, *propSide, tableStats);
    if (!constant.has_value() || !minMax.has_value()) {
        return {};
    }
    const auto [minValue, maxValue] = *minMax;
    const auto c = *constant;
    double selectivity;
    if (minValue >= maxValue) {
        // Single-valued column: the predicate is decided.
        bool keeps = false;
        switch (effectiveOp) {
        case ExpressionType::GREATER_THAN:
            keeps = minValue > c;
            break;
        case ExpressionType::GREATER_THAN_EQUALS:
            keeps = minValue >= c;
            break;
        case ExpressionType::LESS_THAN:
            keeps = minValue < c;
            break;
        default:
            keeps = minValue <= c;
            break;
        }
        selectivity = keeps ? 1.0 : 0.0;
    } else if (effectiveOp == ExpressionType::GREATER_THAN ||
               effectiveOp == ExpressionType::GREATER_THAN_EQUALS) {
        selectivity = (maxValue - c) / (maxValue - minValue);
    } else {
        selectivity = (c - minValue) / (maxValue - minValue);
    }
    return std::min(1.0, std::max(0.0, selectivity));
}

// Upper bound on the number of distinct values of a GROUP BY key: node internal IDs are
// bounded by the node count, single-label properties by the column NDV, and LIST_CREATION
// packs (e.g. Q14's personIdsInPath) by the product of their children's bounds.
std::optional<cardinality_t> CardinalityEstimator::getGroupKeyDomain(const Expression& key) const {
    if (nodeIDName2dom.contains(key.getUniqueName())) {
        return getNodeIDDom(key.getUniqueName());
    }
    if (key.expressionType == ExpressionType::PROPERTY) {
        return getPropertyNumDistinct(context, key, tableStats);
    }
    if (key.expressionType == ExpressionType::FUNCTION &&
        key.constCast<ScalarFunctionExpression>().getFunction().name ==
            function::ListCreationFunction::name) {
        auto product = 1.0;
        for (auto& child : key.getChildren()) {
            auto childDomain = getGroupKeyDomain(*child);
            if (!childDomain.has_value()) {
                return {};
            }
            product *= static_cast<double>(childDomain.value());
            if (product >= static_cast<double>(std::numeric_limits<cardinality_t>::max())) {
                return std::numeric_limits<cardinality_t>::max();
            }
        }
        return static_cast<cardinality_t>(product);
    }
    return {};
}

uint64_t CardinalityEstimator::estimateFilter(const LogicalOperator& childPlan,
    const Expression& predicate) const {
    if (predicate.expressionType == ExpressionType::EQUALS) {
        if (isPrimaryKey(*predicate.getChild(0)) || isPrimaryKey(*predicate.getChild(1))) {
            return 1;
        } else {
            const auto numDistinctValues = getTableStatsIfPossible(context, predicate, tableStats);
            if (numDistinctValues.has_value()) {
                return atLeastOne(childPlan.getCardinality() / numDistinctValues.value());
            }
            return atLeastOne(
                childPlan.getCardinality() * PlannerKnobs::EQUALITY_PREDICATE_SELECTIVITY);
        }
    } else {
        // Ordered comparison against a folded constant uses the observed [min, max]
        // range; everything else (parameters, unresolvable sides) keeps the fixed
        // selectivity.
        if (const auto selectivity = getRangeSelectivity(context, predicate, tableStats)) {
            return atLeastOne(childPlan.getCardinality() * *selectivity);
        }
        return atLeastOne(
            childPlan.getCardinality() * PlannerKnobs::NON_EQUALITY_PREDICATE_SELECTIVITY);
    }
}

uint64_t CardinalityEstimator::getNumNodes(const Transaction*,
    const std::vector<table_id_t>& tableIDs) const {
    cardinality_t numNodes = 0u;
    for (auto& tableID : tableIDs) {
        // Skip foreign tables - they won't be in tableStats.
        if (!tableStats.contains(tableID) || !tableStats.at(tableID).storageStats.has_value()) {
            continue;
        }
        numNodes += tableStats.at(tableID).storageStats.value().getTableCard();
    }
    return atLeastOne(numNodes);
}

static storage::Table* getTableFromAnyStorageManager(main::ClientContext* context,
    common::table_id_t tableID) {
    auto [cat, sm] = main::DatabaseManager::resolveTableStorage(*context, tableID);
    return sm->getTable(tableID);
}

uint64_t CardinalityEstimator::getNumRels(const Transaction* transaction,
    const std::vector<table_id_t>& tableIDs) const {
    cardinality_t numRels = 0u;
    for (auto tableID : tableIDs) {
        numRels += getTableFromAnyStorageManager(context, tableID)->getNumTotalRows(transaction);
    }
    return atLeastOne(numRels);
}

double CardinalityEstimator::getOneHopExtensionRate(const std::vector<table_id_t>& tableIDs,
    const std::vector<table_id_t>& boundTableIDs, RelDataDirection direction) const {
    // Expected fan-out of a one-hop extend: total incident edges over ALL bound-table rows.
    // (Dividing by only the edge-active bound nodes overestimates full-scan extends: bound
    // rows without edges still produce no output, and the zeros are part of the mean.)
    // Measured ANALYZE degrees feed the numerator exactly; schema-only stats fall back to
    // the same ratio from row counts.
    cardinality_t numBoundNodes = 0;
    cardinality_t numRels = 0;
    bool sawRelStats = false;
    bool allBoundKeysUnique = true;
    for (auto tableID : boundTableIDs) {
        if (tableStats.contains(tableID) && tableStats.at(tableID).storageStats.has_value()) {
            numBoundNodes += tableStats.at(tableID).storageStats.value().getTableCard();
        }
    }
    for (auto tableID : tableIDs) {
        if (!tableStats.contains(tableID)) {
            continue;
        }
        const auto& stats = tableStats.at(tableID);
        const auto directionKey = RelDirectionUtils::relDirectionToKeyIdx(direction);
        if (!stats.relDirectionStats[directionKey].has_value()) {
            continue;
        }
        sawRelStats = true;
        const auto& relStats = stats.relDirectionStats[directionKey].value();
        allBoundKeysUnique &= relStats.boundKeysUnique;
        numRels += relStats.numRows;
    }
    auto rate = static_cast<double>(numRels) / atLeastOne(numBoundNodes);
    return sawRelStats && allBoundKeysUnique ? std::min<double>(rate, 1) : rate;
}

double CardinalityEstimator::getExtensionRate(const RelExpression& rel,
    const NodeExpression& boundNode, ExtendDirection direction,
    const Transaction* transaction) const {
    double oneHopExtensionRate = 0;
    switch (direction) {
    case ExtendDirection::FWD:
    case ExtendDirection::BWD: {
        oneHopExtensionRate = getOneHopExtensionRate(rel.getInnerRelTableIDs(),
            boundNode.getTableIDs(), ExtendDirectionUtil::getRelDataDirection(direction));
    } break;
    case ExtendDirection::BOTH: {
        oneHopExtensionRate = getOneHopExtensionRate(rel.getInnerRelTableIDs(),
                                  boundNode.getTableIDs(), RelDataDirection::FWD) +
                              getOneHopExtensionRate(rel.getInnerRelTableIDs(),
                                  boundNode.getTableIDs(), RelDataDirection::BWD);
    } break;
    default:
        UNREACHABLE_CODE;
    }
    const auto numRels = static_cast<double>(getNumRels(transaction, rel.getInnerRelTableIDs()));
    switch (rel.getRelType()) {
    case QueryRelType::NON_RECURSIVE: {
        return oneHopExtensionRate;
    }
    case QueryRelType::VARIABLE_LENGTH_WALK:
    case QueryRelType::VARIABLE_LENGTH_TRAIL:
    case QueryRelType::VARIABLE_LENGTH_ACYCLIC: {
        auto rate = oneHopExtensionRate *
                    std::max<uint16_t>(rel.getRecursiveInfo()->bindData->upperBound, 1);
        rate = std::min(rate, numRels);
        return rate * context->getClientConfig()->recursivePatternCardinalityScaleFactor;
    }
    case QueryRelType::SHORTEST:
    case QueryRelType::ALL_SHORTEST:
    case QueryRelType::WEIGHTED_SHORTEST:
    case QueryRelType::ALL_WEIGHTED_SHORTEST: {
        auto rate = std::min<double>(
            oneHopExtensionRate *
                std::max<uint16_t>(rel.getRecursiveInfo()->bindData->upperBound, 1),
            numRels);
        return rate * context->getClientConfig()->recursivePatternCardinalityScaleFactor;
    }
    default:
        UNREACHABLE_CODE;
    }
}

} // namespace planner
} // namespace lbug