     var store: SQLiteQuadStore
     public var selfDescription: String { return "SQLite Plan" }
     public func evaluate() throws -> AnyIterator<TermResult> {
+        let store = self.store
         guard let dbh = try? store.db.prepare(query) else {
             return AnyIterator { return nil }
         }
         let projected = self.projected
-        let results = try dbh.lazy.compactMap { (row) -> TermResult? in
-            let d = try projected.map({ (name, id) throws -> (String, Term) in
-                guard let t = store.term(for: row[id]) else {
-                    throw SQLiteQuadStore.SQLiteQuadStoreError.idAccessError
-                }
-                return (name, t)
-            })
-            let r = TermResult(bindings: Dictionary(uniqueKeysWithValues: d))
-            return r
+        let results = dbh.lazy.compactMap { (row) -> TermResult? in
+            do {
+                let d = try projected.map({ (name, id) throws -> (String, Term) in
+                    guard let t = store.term(for: row[id]) else {
+                        throw SQLiteQuadStore.SQLiteQuadStoreError.idAccessError
+                    }
+                    return (name, t)
+                })
+                let r = TermResult(bindings: Dictionary(uniqueKeysWithValues: d))
+                return r
+            } catch {
+                return nil
+            }
         }
         return AnyIterator(results.makeIterator())
     }

        case .quad(let qp):
            let (q, projected, _) = try query(quad: qp, alias: "t")
            return SQLitePlan(query: q, projected: projected, store: self)
        case .bgp(let triples):
            return try plan(bgp: triples, activeGraph: activeGraph)
        default:
            return nil
        }
    }
    
    private func plan(bgp: [TriplePattern], activeGraph: Term) throws -> QueryPlan? {

extension SQLiteQuadStore: PlanningQuadStore {
    public func plan(algebra: Algebra, activeGraph: Term, dataset: Dataset) throws -> QueryPlan? {
        switch algebra {
        case let .project(a, vars):
            if let qp = try plan(algebra: a, activeGraph: activeGraph, dataset: dataset) {
                if let q = qp as? SQLitePlan {
                    let query = q.query
                    let projected = q.projected
                    let d = query.select(distinct: vars.compactMap { projected[$0] })
                    let p = projected.filter { vars.contains($0.key) }
                    return SQLitePlan(query: d, projected: p, store: self)
                }
            }
            return nil
        case .distinct(let a):
            if let qp = try plan(algebra: a, activeGraph: activeGraph, dataset: dataset) {
                if let q = qp as? SQLitePlan {
