Optimize id()/start_id()/end_id() with direct column access by jrgemignani · Pull Request #2295 · apache/age

@jrgemignani

Optimize id()/start_id()/end_id() with direct graphid column access.

NOTE: This PR is created with AI tools and a human.

Add optimization to avoid rebuilding full vertex/edge objects when only
the id, start_id, or end_id is needed. Instead of calling age_id() on a
reconstructed _agtype_build_vertex/_agtype_build_edge, directly access
the underlying graphid column. This enables PostgreSQL to use B-tree
indexes on graphid columns for significantly improved query performance.

Implementation:
- Export hidden columns as raw graphid type (not agtype-wrapped) when
  entities pass between clauses via export_entity_hidden_columns()
- Store Var references (id_var, start_id_var, end_id_var) in
  transform_entity
- try_optimize_id_funcs() returns raw graphid Var for optimizable
  patterns
- Add cross-type comparison operators (graphid vs int8) with btree
  support
- Update graphid_ops operator class to include cross-type operators,
  enabling Index Cond scans instead of post-scan Filter evaluation
- Add graphid support to agtype_volatile_wrapper() for SET/MERGE
  contexts

Optimized patterns:
  MATCH (p) WHERE id(p) > 0 RETURN p
  MATCH ()-[e]->() WHERE id(e) > 0 RETURN e
  MATCH ()-[e]->() WHERE start_id(e) > 0 RETURN e
  MATCH ()-[e]->() WHERE end_id(e) > 0 RETURN e

All regression tests passed.
Added regression tests for the new graphid operators.

Files changed:
- src/include/parser/cypher_transform_entity.h: Add id_var, start_id_var,
  end_id_var fields to transform_entity struct
- src/backend/parser/cypher_clause.c: Export raw graphid columns
- src/backend/parser/cypher_expr.c: Add try_optimize_id_funcs(),
  extract_id_var_from_entity_expr(); return raw graphid Var
- src/backend/utils/adt/graphid.c: Add 12 cross-type comparison functions
  (graphid_eq_int8, etc.) and 2 btree comparison functions
  (graphid_btree_cmp_int8, int8_btree_cmp_graphid)
- src/backend/utils/adt/agtype.c: Add GRAPHIDOID to
  agtype_volatile_wrapper()
- sql/age_main.sql: Add cross-type operators, functions, and update
  graphid_ops operator class with cross-type btree support
- age--1.6.0--y.y.y.sql: Add upgrade definitions for new functions,
  operators, and operator class
- regress/sql/graphid.sql: Add cross-type operator and index tests
- regress/sql/cypher_match.sql: Add WHERE optimization tests
- regress/expected/*.out: Update expected output