// Plan-flip detection for the cache: a template planned blind (constants // lifted to parameters) can choose a structurally different operator tree // than value-sighted planning would -- exact scan cardinalities, resolved // anchor degrees, and rewrite gates priced from those exacts all go blind // under auto-parameterization, and a flipped plan can be arbitrarily worse // (the census measured one 17.5x allocation regression). The cache detects // the flip once, when a template is first planned, by also planning the // literal text and comparing the trees value-blind; a flipped template is // marked so execution routes through sighted planning instead of the // cached tree. Non-flipped templates -- the overwhelming majority -- keep // the full cache win.
package gql import ( ) var ( blindStrLit = regexp.MustCompile(`'(?:[^'\\]|\\.)*'`) blindParam = regexp.MustCompile(`\$\w+`) blindNumber = regexp.MustCompile(`\b\d+(?:\.\d+)?\b`) ) // valueBlind normalizes a canonical plan rendering so a literal and its // lifted parameter compare equal: strings, params, and numbers all become // `?`, and the [anchor: ...] provenance note drops entirely -- it records // WHICH SIGNAL decided the anchor (exact vs average), which legitimately // differs between sighted and blind planning even when the chosen // operator tree is identical. Both sides normalize identically, so // structure alone decides. func valueBlind( []string) string { := [:0] for , := range { if strings.HasPrefix(strings.TrimSpace(), "[anchor:") { continue } = append(, ) } = := strings.Join(, "\n") = blindStrLit.ReplaceAllString(, "?") = blindParam.ReplaceAllString(, "?") = blindNumber.ReplaceAllString(, "?") return } // planFlipped reports whether the template plan the cache would execute // (post adaptive anchor choice for this call's values) differs // structurally from the plan the literal query text produces. func planFlipped( string, *plan.Plan, *graph.SnapshotGraph) bool { , := parseDesugar() if != nil { return false } , := plan.Build(, ) if != nil { return false } return valueBlind(explain.Canonical(, plan.Estimate(, ))) != valueBlind(explain.Canonical(, plan.Estimate(, ))) }