@@ -2778,6 +2778,96 @@ static bool eval_where(const cbm_where_clause_t *w, binding_t *b) {
27782778 return is_and ;
27792779}
27802780
2781+ /* Three-valued WHERE evaluation for the early (seed-row) pass in
2782+ * execute_single: only the first MATCH alias is bound there, so a leaf on
2783+ * any other alias is UNKNOWN rather than true. The two-valued evaluator
2784+ * returns true for an unbound alias, which NOT / XOR then invert -- pruning
2785+ * every seed before relationship expansion (distilled from PR #1245). The
2786+ * full evaluation reruns after expansion (Step 3), so the early pass only has
2787+ * to prune on a definite false. Allocation-free, O(expression nodes). */
2788+ typedef enum { CYP_PARTIAL_FALSE = 0 , CYP_PARTIAL_TRUE , CYP_PARTIAL_UNKNOWN } cyp_partial_t ;
2789+
2790+ static cyp_partial_t partial_and (cyp_partial_t l , cyp_partial_t r ) {
2791+ if (l == CYP_PARTIAL_FALSE || r == CYP_PARTIAL_FALSE ) {
2792+ return CYP_PARTIAL_FALSE ;
2793+ }
2794+ return (l == CYP_PARTIAL_TRUE && r == CYP_PARTIAL_TRUE ) ? CYP_PARTIAL_TRUE
2795+ : CYP_PARTIAL_UNKNOWN ;
2796+ }
2797+
2798+ static cyp_partial_t partial_or (cyp_partial_t l , cyp_partial_t r ) {
2799+ if (l == CYP_PARTIAL_TRUE || r == CYP_PARTIAL_TRUE ) {
2800+ return CYP_PARTIAL_TRUE ;
2801+ }
2802+ return (l == CYP_PARTIAL_FALSE && r == CYP_PARTIAL_FALSE ) ? CYP_PARTIAL_FALSE
2803+ : CYP_PARTIAL_UNKNOWN ;
2804+ }
2805+
2806+ /* A condition whose alias is not bound yet is UNKNOWN; a literal-only LHS
2807+ * (func condition with no variable arg) has nothing to wait for. */
2808+ static cyp_partial_t eval_condition_partial (const cbm_condition_t * c , binding_t * b ) {
2809+ if (c -> variable && !binding_get (b , c -> variable ) && !binding_get_edge (b , c -> variable )) {
2810+ return CYP_PARTIAL_UNKNOWN ;
2811+ }
2812+ return eval_condition (c , b ) ? CYP_PARTIAL_TRUE : CYP_PARTIAL_FALSE ;
2813+ }
2814+
2815+ static cyp_partial_t eval_expr_partial (const cbm_expr_t * e , // NOLINT(misc-no-recursion)
2816+ binding_t * b ) {
2817+ if (!e ) {
2818+ return CYP_PARTIAL_TRUE ;
2819+ }
2820+ if (e -> type == EXPR_CONDITION ) {
2821+ return eval_condition_partial (& e -> cond , b );
2822+ }
2823+ cyp_partial_t left = eval_expr_partial (e -> left , b );
2824+ if (e -> type == EXPR_NOT ) {
2825+ if (left == CYP_PARTIAL_UNKNOWN ) {
2826+ return CYP_PARTIAL_UNKNOWN ;
2827+ }
2828+ return left == CYP_PARTIAL_TRUE ? CYP_PARTIAL_FALSE : CYP_PARTIAL_TRUE ;
2829+ }
2830+ /* Same short-circuits as eval_expr: a definite left decides AND / OR. */
2831+ if (e -> type == EXPR_AND && left == CYP_PARTIAL_FALSE ) {
2832+ return CYP_PARTIAL_FALSE ;
2833+ }
2834+ if (e -> type == EXPR_OR && left == CYP_PARTIAL_TRUE ) {
2835+ return CYP_PARTIAL_TRUE ;
2836+ }
2837+ cyp_partial_t right = eval_expr_partial (e -> right , b );
2838+ if (e -> type == EXPR_AND ) {
2839+ return partial_and (left , right );
2840+ }
2841+ if (e -> type == EXPR_OR ) {
2842+ return partial_or (left , right );
2843+ }
2844+ /* EXPR_XOR */
2845+ if (left == CYP_PARTIAL_UNKNOWN || right == CYP_PARTIAL_UNKNOWN ) {
2846+ return CYP_PARTIAL_UNKNOWN ;
2847+ }
2848+ return left != right ? CYP_PARTIAL_TRUE : CYP_PARTIAL_FALSE ;
2849+ }
2850+
2851+ static cyp_partial_t eval_where_partial (const cbm_where_clause_t * w , binding_t * b ) {
2852+ if (!w ) {
2853+ return CYP_PARTIAL_TRUE ;
2854+ }
2855+ if (w -> root ) {
2856+ return eval_expr_partial (w -> root , b );
2857+ }
2858+ /* Legacy flat evaluation */
2859+ if (w -> count == 0 ) {
2860+ return CYP_PARTIAL_TRUE ;
2861+ }
2862+ bool is_and = (w -> op && strcmp (w -> op , "AND" ) == 0 ) != 0 ;
2863+ cyp_partial_t result = is_and ? CYP_PARTIAL_TRUE : CYP_PARTIAL_FALSE ;
2864+ for (int i = 0 ; i < w -> count ; i ++ ) {
2865+ cyp_partial_t r = eval_condition_partial (& w -> conditions [i ], b );
2866+ result = is_and ? partial_and (result , r ) : partial_or (result , r );
2867+ }
2868+ return result ;
2869+ }
2870+
27812871/* Check if a string value looks like a regex pattern. */
27822872static bool looks_like_regex (const char * s ) {
27832873 if (!s ) {
@@ -5281,7 +5371,7 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
52815371 binding_t b = {0 };
52825372 b .store = store ;
52835373 binding_set (& b , var_name , & scanned [i ]);
5284- bool pass = ! q -> where || eval_where (q -> where , & b );
5374+ bool pass = eval_where_partial (q -> where , & b ) != CYP_PARTIAL_FALSE ;
52855375 if (pass ) {
52865376 bindings [bind_count ++ ] = b ;
52875377 } else {
0 commit comments