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
36 changes: 31 additions & 5 deletions include/main/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,38 @@ class Database {
const std::vector<FieldUpdate> &fields,
UpdateType update_type, UpdateResult &result);

/**
* Build an alias->schema mapping from a Query's FROM + TRAVERSE clauses.
* Only declarations ("alias:Schema") are recorded; bare references ("alias")
* are skipped. Returns an error if the same alias is bound to two different
* schemas.
/** Initialize QueryState from query: temporal context, FROM table, prepare.
*/
[[nodiscard]] arrow::Status init_query_state(const Query &query,
QueryState &query_state) const;

/** Inline WHERE clauses applicable to the FROM alias. */
[[nodiscard]] arrow::Status inline_from_where(const Query &query,
QueryState &query_state,
QueryResult &result) const;

/** Process all clauses (WHERE + TRAVERSE) and collect deferred expressions.
*/
[[nodiscard]] arrow::Result<std::vector<std::shared_ptr<WhereExpr>>>
execute_clauses(const Query &query, QueryState &query_state,
QueryResult &result) const;

/** Execute a single TRAVERSE clause, updating query_state in-place. */
[[nodiscard]] arrow::Status execute_traverse(
const std::shared_ptr<Traverse> &traverse, QueryState &query_state,
const Query &query, size_t clause_index, QueryResult &result) const;

/** Apply a single-variable WHERE filter, or defer to post_where. */
[[nodiscard]] arrow::Status apply_where_filter(
const std::shared_ptr<WhereExpr> &where, QueryState &query_state,
std::vector<std::shared_ptr<WhereExpr>> &post_where) const;

/** Build the final output table: denormalize, populate rows, apply
* deferred WHERE filters, and project via SELECT. */
[[nodiscard]] arrow::Result<std::shared_ptr<arrow::Table>> build_result_table(
const Query &query, QueryState &query_state,
const std::vector<std::shared_ptr<WhereExpr>> &post_where,
QueryResult &result) const;
};

} // namespace tundradb
64 changes: 64 additions & 0 deletions include/query/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,37 @@ struct QueryState {
std::string ToString() const;
};

/**
* @brief Results of an expand traverse hop operation in a graph traversal
*
* Holds the sets of source and target node IDs identified during a single
* traversal hop, categorized by their connection statuses.
*/
struct ExpandTraverseHopResult {
/**
* @brief Source node IDs with matched connections discovered during traversal
*
* Represents the set of source node identifiers that have at least one valid
* connection to matching target nodes, as determined by traversal criteria.
*/
llvm::DenseSet<int64_t> matched_source_ids;
/**
* @brief Target node IDs that were successfully matched during traversal
*
* Represents the set of target node identifiers that satisfy the traversal
* criteria and have a valid connection from at least one source node.
*/
llvm::DenseSet<int64_t> matched_target_ids;

/**
* @brief Source node IDs without any accepted edges
*
* Represents the set of node IDs that were part of the traversal but did not
* have any connecting edges or target nodes meeting the required criteria.
*/
llvm::DenseSet<int64_t> unmatched_source_ids;
};

/**
* @brief Recursively collects all paths from a node in a connection graph
* (debug).
Expand Down Expand Up @@ -705,6 +736,39 @@ arrow::Result<std::shared_ptr<arrow::Table>> inline_where(
*/
arrow::Status prepare_query(const Query& query, QueryState& query_state);

/**
* @brief Executes one graph-pattern hop for a @c TRAVERSE clause.
*
* Walks the current source frontier in @p query_state (IDs under
* @c traverse.source().value()), loads each node's outgoing edges of type
* @c traverse.edge_type(), and keeps edges whose target resolves to a node in
* @p target_schema. Optional pruning applies when @p query_state already
* holds IDs for the target alias: targets not in that set are skipped.
*
* For each surviving edge, @p node_filters are evaluated on the target node and
* @p edge_filters on the edge (typically inlined WHERE expressions). Matches
* append a @c Connection to @p query_state (per-source connection lists and
* @c incoming on the target id) for later row materialization.
*
* The three output sets support @c JoinStrategy: sources with at least one
* matching edge, distinct matched target ids, and sources with no matching
* edge. Callers should clear these sets before calling if they are reused.
*
* @param traverse Parsed TRAVERSE (source/target aliases, edge type, optional
* edge alias).
* @param target_schema Resolved concrete schema name for the target endpoint.
* @param query_state Execution state; must have @c node_manager, @c edge_store,
* populated @c ids for the source alias, and (when used) target ids.
* @param node_filters WHERE expressions applied to each candidate target node.
* @param edge_filters WHERE expressions applied to each candidate edge.
* @return @c ExpandTraverseHopResult
*/
arrow::Result<ExpandTraverseHopResult> expand_traverse_hop(
const Traverse& traverse, const std::string& target_schema,
QueryState& query_state,
const std::vector<std::shared_ptr<WhereExpr>>& node_filters,
const std::vector<std::shared_ptr<WhereExpr>>& edge_filters);

} // namespace tundradb

#endif // QUERY_EXECUTION_HPP
Loading
Loading