Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/optimizer/count_rel_table_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::visitOperator(

static LogicalOperator* skipProjections(LogicalOperator* op);
static bool containsOp(const LogicalOperator* root, const LogicalOperator* target);
static bool isNativeRelGroupEntry(const RelGroupCatalogEntry* entry);
static bool isNativeNodeEntry(const NodeTableCatalogEntry* entry);

std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteAntiEdgeChainCount(
std::shared_ptr<LogicalOperator> op) {
Expand Down Expand Up @@ -234,7 +236,8 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteAntiEdgeChain
continue;
}
auto* relGroupEntry = rel->getEntry(0)->ptrCast<RelGroupCatalogEntry>();
if (relGroupEntry->getScanFunction().has_value()) {
if (relGroupEntry->getScanFunction().has_value() ||
!isNativeRelGroupEntry(relGroupEntry)) {
continue;
}
auto* extendChild = skipProjections((ext->getChild(0).get()));
Expand Down Expand Up @@ -405,7 +408,8 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteAntiEdgeChain
return op;
}
auto* relGroupEntry = rel->getEntry(0)->ptrCast<RelGroupCatalogEntry>();
if (relGroupEntry->getScanFunction().has_value()) {
if (relGroupEntry->getScanFunction().has_value() ||
!isNativeRelGroupEntry(relGroupEntry)) {
return op;
}
if (ext->getDirection() == ExtendDirection::BOTH) {
Expand Down Expand Up @@ -449,6 +453,14 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteAntiEdgeChain
toTableID != toNode->getTableIDs()[0]) {
return op;
}
// Parity with the extend-chain path, which gates every node it touches. The
// binder refuses to mix icebug-disk tables with native ones, but there is no
// equivalent guard for Arrow-backed tables, so the rel group's format alone
// cannot vouch for its endpoints.
if (!isNativeNodeEntry(fromNode->getEntry(0)->ptrCast<NodeTableCatalogEntry>()) ||
!isNativeNodeEntry(toNode->getEntry(0)->ptrCast<NodeTableCatalogEntry>())) {
return op;
}
hop.relScans.push_back({relGroupEntry->getRelEntryInfos()[0].oid, scanDirection,
fromTableID, toTableID, relGroupEntry->getName()});
matched = true;
Expand All @@ -468,6 +480,11 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteAntiEdgeChain
if (antiNodeA->getTableIDs()[0] != midTableID || antiNodeB->getTableIDs()[0] != midTableID) {
return op;
}
// n0, n1 and n2 all bind midTableID, so gating that one entry covers every node the
// operator's node-group arithmetic touches.
if (!isNativeNodeEntry(midNode->getEntry(0)->ptrCast<NodeTableCatalogEntry>())) {
return op;
}

std::vector<common::table_id_t> antiRelTableIDs;
antiRelTableIDs.push_back(antiRelEntry->getRelEntryInfos()[0].oid);
Expand Down Expand Up @@ -570,15 +587,6 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::tryRewriteExtendChainCo

// Validate the extends form a simple path over single-table nodes with single-entry,
// storage-backed rel groups.
// The fast path below iterates the committed node-group grid of native node tables and
// reads the CSR of native rel tables. Arrow-backed and icebug-disk tables have neither,
// so they must be left to the regular plan.
auto isNativeRelGroupEntry = [](const RelGroupCatalogEntry* entry) {
return entry->getStorage().empty() && entry->getStorageFormat() == StorageFormat::NONE;
};
auto isNativeNodeEntry = [](const NodeTableCatalogEntry* entry) {
return entry->getStorage().empty() && entry->getStorageFormat() == StorageFormat::NONE;
};
struct ChainEdge {
std::string u;
std::string v;
Expand Down Expand Up @@ -1143,6 +1151,17 @@ std::shared_ptr<LogicalOperator> CountRelTableOptimizer::visitAggregateReplace(
return projection;
}

// The count-chain fast paths iterate the committed node-group grid of native node tables and
// read the CSR of native rel tables. Arrow-backed and icebug-disk tables have neither, so they
// must be left to the regular plan.
static bool isNativeRelGroupEntry(const RelGroupCatalogEntry* entry) {
return entry->getStorage().empty() && entry->getStorageFormat() == StorageFormat::NONE;
}

static bool isNativeNodeEntry(const NodeTableCatalogEntry* entry) {
return entry->getStorage().empty() && entry->getStorageFormat() == StorageFormat::NONE;
}

static LogicalOperator* skipProjections(LogicalOperator* op) {
while (op->getOperatorType() == LogicalOperatorType::PROJECTION) {
op = op->getChild(0).get();
Expand Down
135 changes: 135 additions & 0 deletions test/optimizer/optimizer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include <functional>
#include <stdexcept>
#include <string>
#include <vector>

#include "catalog/catalog.h"
#include "catalog/catalog_entry/node_table_catalog_entry.h"
#include "catalog/catalog_entry/rel_group_catalog_entry.h"
#include "common/enums/storage_format.h"
#include "graph_test/private_graph_test.h"
#include "planner/join_order/cost_model.h"
#include "planner/operator/logical_filter.h"
#include "planner/operator/logical_plan_util.h"
#include "planner/operator/scan/logical_count_rel_table.h"
#include "planner/operator/scan/logical_dummy_scan.h"
#include "test_runner/test_runner.h"
#include "transaction/transaction.h"
#include <format>

namespace lbug {
Expand Down Expand Up @@ -901,5 +907,134 @@ TEST_F(OptimizerTest, CountReachableDistinctNodes) {
ASSERT_EQ(resultNoCsr->getNext()->getValue(0)->getValue<int64_t>(), 2);
}

// The COUNT_ANTI_EDGE_CHAIN fast path walks the committed node-group grid and reads
// the CSR of native tables, so it must decline tables backed by another storage
// format.
//
// What is asserted here, and why: the rewrite only fires when the planner produces an
// INNER-over-MARK plan with the prefix chain rooted at the middle-node scan (see
// tryRewriteAntiEdgeChainCount). On a tiny fixture the join orderer never produces that
// shape — everything plans at cardinality 1 — so operator presence cannot be asserted
// deterministically here; firing itself was validated by hand on an LSQB-scale database.
// Instead these tests pin both sides of the storage gate directly: native tables satisfy
// the nativeness condition the fast path requires (and answer the query correctly),
// while icebug-disk tables fail it (and keep the regular plan).
class AntiEdgeChainStorageGateTest : public StatsOptimizerTest {
public:
// The LSQB q9 shape the rewrite targets: two undirected hops either side of a middle
// node, an anti-edge between the outer two, and a directed suffix hop into another
// table. Mirrors test_files/lsqb/lsqb_queries.test with the demo-db schema.
static constexpr const char* kAntiEdgeChainQuery =
"EXPLAIN LOGICAL MATCH (u1:user)-[:follows]-(u2:user)-[:follows]-(u3:user)"
"-[:livesin]->(c:city) "
"WHERE NOT EXISTS {MATCH (u1)-[:follows]-(u3)} AND id(u1) <> id(u3) "
"RETURN count(*);";
// Same shape without the EXPLAIN prefix, for executing the query.
static constexpr const char* kAntiEdgeChainCountQuery =
"MATCH (u1:user)-[:follows]-(u2:user)-[:follows]-(u3:user)"
"-[:livesin]->(c:city) "
"WHERE NOT EXISTS {MATCH (u1)-[:follows]-(u3)} AND id(u1) <> id(u3) "
"RETURN count(*);";

void createIcebugDiskTables() {
// The demo-db icebug-disk dataset carries exactly this schema.
const std::string storage = TestHelper::appendLbugRootPath("dataset/demo-db/icebug-disk/");
const std::string backing = " WITH (storage = '" + storage + "', format = 'icebug-disk');";
const std::vector<std::string> ddl{
"CREATE NODE TABLE city(id INT32, name STRING, population INT64, "
"PRIMARY KEY(id))" +
backing,
"CREATE NODE TABLE user(id INT32, name STRING, age INT64, PRIMARY KEY(id))" + backing,
"CREATE REL TABLE follows(FROM user TO user, since INT32)" + backing,
"CREATE REL TABLE livesin(FROM user TO city)" + backing};
for (const auto& statement : ddl) {
auto result = conn->query(statement);
ASSERT_TRUE(result->isSuccess()) << result->getErrorMessage();
}
}

void createNativeTables() {
const std::vector<std::string> ddl{
"CREATE NODE TABLE city(id INT32, name STRING, population INT64, "
"PRIMARY KEY(id));",
"CREATE NODE TABLE user(id INT32, name STRING, age INT64, PRIMARY KEY(id));",
"CREATE REL TABLE follows(FROM user TO user, since INT32);",
"CREATE REL TABLE livesin(FROM user TO city);",
"UNWIND range(0, 5) AS i CREATE (:user {id: i, name: 'u', age: i});",
"UNWIND range(0, 2) AS i CREATE (:city {id: i, name: 'c', population: i});",
"MATCH (x:user), (y:user) WHERE x.id + 1 = y.id "
"CREATE (x)-[:follows {since: 2020}]->(y);",
"MATCH (u:user), (c:city) WHERE u.id % 3 = c.id CREATE (u)-[:livesin]->(c);"};
for (const auto& statement : ddl) {
auto result = conn->query(statement);
ASSERT_TRUE(result->isSuccess()) << result->getErrorMessage();
}
}

// Pins the exact branch condition of isNativeNodeEntry / isNativeRelGroupEntry in
// count_rel_table_optimizer.cpp for the tables under test.
void assertNativeEntries() {
auto* catalog = database->getCatalog();
for (const char* table : {"user", "city"}) {
const auto* entry =
catalog->getTableCatalogEntry(&transaction::DUMMY_CHECKPOINT_TRANSACTION, table)
->ptrCast<catalog::NodeTableCatalogEntry>();
ASSERT_TRUE(entry->getStorage().empty()) << table;
ASSERT_EQ(entry->getStorageFormat(), common::StorageFormat::NONE) << table;
}
for (const char* table : {"follows", "livesin"}) {
const auto* entry =
catalog->getTableCatalogEntry(&transaction::DUMMY_CHECKPOINT_TRANSACTION, table)
->ptrCast<catalog::RelGroupCatalogEntry>();
ASSERT_TRUE(entry->getStorage().empty()) << table;
ASSERT_EQ(entry->getStorageFormat(), common::StorageFormat::NONE) << table;
}
}

void assertNonNativeEntries() {
auto* catalog = database->getCatalog();
for (const char* table : {"user", "city"}) {
const auto* entry =
catalog->getTableCatalogEntry(&transaction::DUMMY_CHECKPOINT_TRANSACTION, table)
->ptrCast<catalog::NodeTableCatalogEntry>();
ASSERT_FALSE(entry->getStorage().empty() &&
entry->getStorageFormat() == common::StorageFormat::NONE)
<< table;
}
for (const char* table : {"follows", "livesin"}) {
const auto* entry =
catalog->getTableCatalogEntry(&transaction::DUMMY_CHECKPOINT_TRANSACTION, table)
->ptrCast<catalog::RelGroupCatalogEntry>();
ASSERT_FALSE(entry->getStorage().empty() &&
entry->getStorageFormat() == common::StorageFormat::NONE)
<< table;
}
}
};

TEST_F(AntiEdgeChainStorageGateTest, NativeTablesPassStorageGate) {
createNativeTables();
// Positive side of the gate: native tables satisfy the nativeness condition the fast
// path requires, so the gate lets them through.
assertNativeEntries();
// The query over the fixture graph (user chain 0-1-...-5, one livesin city per user)
// counts 8 undirected length-2 follows paths: no follows chord closes any of them, and
// each contributes one suffix row.
auto result = conn->query(kAntiEdgeChainCountQuery);
ASSERT_TRUE(result->isSuccess()) << result->getErrorMessage();
ASSERT_EQ(result->getNext()->getValue(0)->getValue<int64_t>(), 8);
}

TEST_F(AntiEdgeChainStorageGateTest, SkipsIcebugDiskTables) {
createIcebugDiskTables();
// Negative side of the gate: these tables fail the nativeness condition, so the fast
// path must decline them. Asserting the precondition alongside the plan keeps this
// test honest: it cannot pass by running against native tables by mistake.
assertNonNativeEntries();
auto plan = getRoot(kAntiEdgeChainQuery);
ASSERT_FALSE(OptimizerTest::hasOperatorType(plan->getLastOperator().get(),
planner::LogicalOperatorType::COUNT_ANTI_EDGE_CHAIN));
}

} // namespace testing
} // namespace lbug
Loading