Search in sources :

Example 1 with Const

use of catdata.fql.decl.SigExp.Const in project fql by CategoricalData.

the class JDBCBridge method gatherInstance.

private static void gatherInstance(FQLProgram prog, Map<String, Set<Map<Object, Object>>> ret, Statement Stmt, String k, InstExp v) throws SQLException {
    Const t = v.type(prog).toConst(prog);
    for (String n : t.nodes) {
        ResultSet RS = Stmt.executeQuery("SELECT c0,c1 FROM " + k + "_" + n);
        Set<Map<Object, Object>> ms = new HashSet<>();
        while (RS.next()) {
            Map<Object, Object> m = new HashMap<>();
            m.put("c0", RS.getObject("c0"));
            m.put("c1", RS.getObject("c1"));
            ms.add(m);
        }
        RS.close();
        ret.put(k + "_" + n, ms);
    }
    for (Triple<String, String, String> n : t.attrs) {
        ResultSet RS = Stmt.executeQuery("SELECT c0,c1 FROM " + k + "_" + n.first);
        Set<Map<Object, Object>> ms = new HashSet<>();
        while (RS.next()) {
            Map<Object, Object> m = new HashMap<>();
            m.put("c0", RS.getObject("c0"));
            m.put("c1", RS.getObject("c1"));
            ms.add(m);
        }
        RS.close();
        ret.put(k + "_" + n.first, ms);
    }
    for (Triple<String, String, String> n : t.arrows) {
        ResultSet RS = Stmt.executeQuery("SELECT c0,c1 FROM " + k + "_" + n.first);
        Set<Map<Object, Object>> ms = new HashSet<>();
        while (RS.next()) {
            Map<Object, Object> m = new HashMap<>();
            m.put("c0", RS.getObject("c0"));
            m.put("c1", RS.getObject("c1"));
            ms.add(m);
        }
        RS.close();
        ret.put(k + "_" + n.first, ms);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Const(catdata.fql.decl.SigExp.Const) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with Const

use of catdata.fql.decl.SigExp.Const in project fql by CategoricalData.

the class JDBCBridge method maybeExecTransform.

private static List<PSM> maybeExecTransform(InstOps ops, FQLProgram prog, Statement Stmt, String k, TransExp v, PSMInterp interp, Map<String, Set<Map<Object, Object>>> ret) throws SQLException {
    List<PSM> psm = new LinkedList<>();
    Pair<String, String> val = prog.transforms.get(k).type(prog);
    InstExp i = prog.insts.get(val.first);
    Const ss = i.type(prog).toConst(prog);
    Signature s = ss.toSig(prog);
    psm.addAll(PSMGen.makeTables(k, s, false));
    switch(DefunctGlobalOptions.debug.fql.sqlKind) {
        case NATIVE:
            psm.addAll(v.accept(k, ops));
            interp.interpX(psm, ret);
            break;
        case H2:
        case JDBC:
        default:
            if (v instanceof TransExp.External && DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.H2) {
            } else {
                psm.addAll(v.accept(k, ops));
            }
            maybeExec(psm, Stmt, ret, interp, s);
            if (v.gather()) {
                gatherTransform(prog, ret, Stmt, k, ss);
            // have non SQL transform output into temps, so can gather them like any other
            }
            break;
    }
    return psm;
}
Also used : InstExp(catdata.fql.decl.InstExp) Const(catdata.fql.decl.SigExp.Const) Signature(catdata.fql.decl.Signature) TransExp(catdata.fql.decl.TransExp) PSM(catdata.fql.sql.PSM) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) LinkedList(java.util.LinkedList)

Example 3 with Const

use of catdata.fql.decl.SigExp.Const in project fql by CategoricalData.

the class RaToFql method trans.

private static Pair<String, Const> trans(Const src, EFlower fl, String pre) {
    // SigExp src0 = new SigExp.Var("S");
    LinkedList<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    List<String> nodes1 = new LinkedList<>();
    List<String> nodes2 = new LinkedList<>();
    List<String> nodes3 = new LinkedList<>();
    nodes1.add("adom");
    nodes2.add("adom");
    nodes2.add("guid");
    nodes3.add("adom");
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    attrs.add(new Triple<>("att", "adom", "str"));
    List<Triple<String, String, String>> edges1 = new LinkedList<>();
    List<Triple<String, String, String>> edges2 = new LinkedList<>();
    List<Triple<String, String, String>> edges3 = new LinkedList<>();
    List<Pair<String, String>> inodes1 = new LinkedList<>();
    List<Pair<String, String>> inodes2 = new LinkedList<>();
    List<Pair<String, String>> inodes3 = new LinkedList<>();
    inodes1.add(new Pair<>("adom", "adom"));
    inodes2.add(new Pair<>("adom", "adom"));
    inodes3.add(new Pair<>("adom", "adom"));
    List<Pair<String, String>> iattrs = new LinkedList<>();
    iattrs.add(new Pair<>("att", "att"));
    List<Pair<String, List<String>>> iedges1 = new LinkedList<>();
    List<Pair<String, List<String>>> iedges2 = new LinkedList<>();
    List<Pair<String, List<String>>> iedges3 = new LinkedList<>();
    for (String k : fl.from.keySet()) {
        String v = fl.from.get(k);
        inodes1.add(new Pair<>(k, v));
        nodes1.add(k);
        inodes2.add(new Pair<>(k, "guid"));
        for (Triple<String, String, String> arr : src.arrows) {
            if (arr.second.equals(v)) {
                List<String> l = new LinkedList<>();
                l.add(v);
                l.add(arr.first);
                edges1.add(new Triple<>(k + "_" + arr.first, k, "adom"));
                iedges1.add(new Pair<>(k + "_" + arr.first, l));
                edges2.add(new Triple<>(k + "_" + arr.first, "guid", "adom"));
                List<String> l0 = new LinkedList<>();
                l0.add("guid");
                l0.add(k + "_" + arr.first);
                iedges2.add(new Pair<>(k + "_" + arr.first, l0));
            }
        }
    }
    List<List<Triple<String, String, String>>> eqcs = merge(edges2, fl);
    Iterator<Triple<String, String, String>> it = edges2.iterator();
    while (it.hasNext()) {
        Triple<String, String, String> k = it.next();
        for (List<Triple<String, String, String>> v : eqcs) {
            if (v.contains(k) && !v.get(0).equals(k)) {
                it.remove();
            }
        }
    }
    for (Pair<String, List<String>> kk : iedges2) {
        Triple<String, String, String> k = new Triple<>(kk.second.get(1), "guid", "adom");
        for (List<Triple<String, String, String>> v : eqcs) {
            if (v.contains(k) && !v.get(0).equals(k)) {
                List<String> xxx = new LinkedList<>();
                xxx.add("guid");
                xxx.add(v.get(0).first);
                kk.second = xxx;
                break;
            }
        }
    }
    nodes3.add("guid");
    inodes3.add(new Pair<>("guid", "guid"));
    for (String k : fl.select.keySet()) {
        Pair<String, String> v = fl.select.get(k);
        edges3.add(new Triple<>(k, "guid", "adom"));
        Triple<String, String, String> t = new Triple<>(v.first + "_" + fl.from.get(v.first) + "_" + v.second, "guid", "adom");
        if (fl.from.get(v.first) == null) {
            throw new RuntimeException(v.first + " is not selectable in " + fl);
        }
        for (List<Triple<String, String, String>> eqc : eqcs) {
            if (eqc.contains(t)) {
                List<String> li = new LinkedList<>();
                li.add("guid");
                li.add(eqc.get(0).first);
                iedges3.add(new Pair<>(k, li));
            }
        }
    }
    Const sig1 = new Const(nodes1, attrs, edges1, eqs);
    Const sig2 = new Const(nodes2, attrs, edges2, eqs);
    Const sig3 = new Const(nodes3, attrs, edges3, eqs);
    MapExp.Const map1 = new MapExp.Const(inodes1, iattrs, iedges1, sig1, new Var("S"));
    MapExp.Const map2 = new MapExp.Const(inodes2, iattrs, iedges2, src, sig2);
    MapExp.Const map3 = new MapExp.Const(inodes3, iattrs, iedges3, sig3, sig2);
    String xxx = "";
    xxx += "\n\nschema " + pre + "fromSchema = " + sig1;
    xxx += "\n\nmapping " + pre + "fromMapping = " + map1 + " : " + pre + "fromSchema -> S";
    xxx += "\n\ninstance " + pre + "fromInstance = delta " + pre + "fromMapping I";
    xxx += "\n\nschema " + pre + "whereSchema = " + sig2;
    xxx += "\n\nmapping " + pre + "whereMapping = " + map2 + " : " + pre + "fromSchema -> " + pre + "whereSchema";
    xxx += "\n\ninstance " + pre + "whereInstance = pi " + pre + "whereMapping " + pre + "fromInstance";
    xxx += "\n\nschema " + pre + "Schema = " + sig3;
    xxx += "\n\nmapping " + pre + "selectMapping = " + map3 + " : " + pre + "Schema -> " + pre + "whereSchema";
    if (fl.distinct) {
        xxx += "\n\ninstance " + pre + "selectInstance = delta " + pre + "selectMapping " + pre + "whereInstance";
        xxx += "\n\ninstance " + pre + " = relationalize " + pre + "selectInstance";
    } else {
        xxx += "\n\ninstance " + pre + " = delta " + pre + "selectMapping " + pre + "whereInstance";
    }
    String comment = longSlash + "\n/* " + "Translation of " + pre + "  */\n" + longSlash;
    return new Pair<>(comment + xxx, sig3);
}
Also used : Var(catdata.fql.decl.SigExp.Var) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) MapExp(catdata.fql.decl.MapExp) Pair(catdata.Pair)

Example 4 with Const

use of catdata.fql.decl.SigExp.Const in project fql by CategoricalData.

the class RaToFql method doED.

private static MapExp.Const doED(Map<String, List<String>> cols, Map<String, String> from, List<Pair<Pair<String, String>, Pair<String, String>>> where, Const target) {
    Set<Triple<String, String, Map<String, String>>> s = new HashSet<>();
    // can process some
    Set<Pair<String, String>> e = new HashSet<>();
    Map<String, String> origin = new HashMap<>();
    Map<String, String> origin2 = new HashMap<>();
    Map<String, String> origin3 = new HashMap<>();
    for (Entry<String, String> x : from.entrySet()) {
        // a AS b
        Map<String, String> map = new HashMap<>();
        if (!cols.containsKey(x.getValue())) {
            throw new RuntimeException("No table " + x.getValue() + " found in " + cols);
        }
        origin.put(x.getKey(), x.getValue());
        for (String col : cols.get(x.getValue())) {
            map.put(col, x.getKey() + "_" + col);
            origin.put(x.getKey() + "_" + col, "adom");
            origin3.put(x.getKey() + "_" + col, x.getValue());
            origin2.put(x.getKey() + "_" + col, col);
        }
        s.add(new Triple<>(x.getValue(), x.getKey(), map));
    }
    for (Pair<Pair<String, String>, Pair<String, String>> x : where) {
        e.add(new Pair<>(x.first.first + "_" + x.first.second, x.second.first + "_" + x.second.second));
    }
    // /////////////////////////////
    Set<Pair<String, Pair<String, String>>> sx = new HashSet<>();
    for (Triple<String, String, Map<String, String>> x : s) {
        // table name, fresh, map
        for (Entry<String, String> y : x.third.entrySet()) {
            sx.add(new Pair<>(x.first, new Pair<>(x.second, y.getValue())));
        }
    }
    // ////////////////////////////
    List<String> wn = new LinkedList<>();
    List<Triple<String, String, String>> wa = new LinkedList<>();
    List<Pair<List<String>, List<String>>> we = new LinkedList<>();
    List<Triple<String, String, String>> wat = new LinkedList<>();
    for (Pair<String, Pair<String, String>> x : sx) {
        if (!wn.contains(x.second.first)) {
            wn.add(x.second.first);
        }
        if (!wn.contains(x.second.second)) {
            wn.add(x.second.second);
        }
        Triple<String, String, String> t = new Triple<>(x.first + "_" + x.second.first + "_" + x.second.second, x.second.first, x.second.second);
        if (!wa.contains(t)) {
            wa.add(t);
        }
    }
    Set<Triple<String, String, String>> eqE = new HashSet<>();
    for (Pair<String, String> eq : e) {
        Triple<String, String, String> t = new Triple<>(eq.first + "_" + eq.second, eq.first, eq.second);
        if (!wa.contains(t)) {
            wa.add(t);
            eqE.add(t);
        }
    }
    Const w = new Const(wn, wat, wa, we);
    List<Pair<String, String>> omx = new LinkedList<>();
    for (String x : wn) {
        omx.add(new Pair<>(x, origin.get(x)));
    }
    List<Pair<String, List<String>>> emx = new LinkedList<>();
    for (Triple<String, String, String> x : wa) {
        String n = origin3.get(x.third);
        String m = origin2.get(x.third);
        List<String> l = new LinkedList<>();
        if (eqE.contains(x)) {
            l.add("adom");
        } else {
            l.add(n);
            l.add(n + "_" + m);
        }
        if (!emx.contains(new Pair<>(x.first, l))) {
            emx.add(new Pair<>(x.first, l));
        }
    }
    MapExp.Const f = new MapExp.Const(omx, new LinkedList<>(), emx, w, target);
    return f;
}
Also used : Pair(catdata.Pair) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) MapExp(catdata.fql.decl.MapExp)

Example 5 with Const

use of catdata.fql.decl.SigExp.Const in project fql by CategoricalData.

the class ToFullQueryExp method visit.

@Override
public FullQueryExp visit(FQLProgram env, Match e) {
    try {
        Const s = e.src.typeOf(env).toConst(env);
        Const t = e.dst.typeOf(env).toConst(env);
        Pair<Map<Set<Pair<String, String>>, String>, Map<Set<Pair<String, String>>, String>> xxx = computeEqCs(s, t, e.rel);
        Map<Set<Pair<String, String>>, String> node_map = xxx.first;
        Map<Set<Pair<String, String>>, String> attr_map = xxx.second;
        Set<Pair<List<String>, List<String>>> eqs = new HashSet<>();
        Set<Triple<String, String, String>> arrows = new HashSet<>();
        Set<String> nodes = new HashSet<>();
        Set<Triple<String, String, String>> attrs = new HashSet<>();
        List<Pair<String, String>> inj1Node = new LinkedList<>();
        List<Pair<String, String>> inj1Attrs = new LinkedList<>();
        List<Pair<String, String>> inj2Node = new LinkedList<>();
        List<Pair<String, String>> inj2Attrs = new LinkedList<>();
        List<Pair<String, List<String>>> inj2Arrows = new LinkedList<>();
        List<Pair<String, List<String>>> inj1Arrows = new LinkedList<>();
        for (Triple<String, String, String> att : s.attrs) {
            String eqc = lookupAttr("left", att, attr_map);
            attrs.add(new Triple<>(eqc, lookupNode("left", att.second, node_map), att.third));
            inj1Attrs.add(new Pair<>(att.first, eqc));
        }
        for (Triple<String, String, String> att : t.attrs) {
            String eqc = lookupAttr("right", att, attr_map);
            attrs.add(new Triple<>(eqc, lookupNode("right", att.second, node_map), att.third));
            inj2Attrs.add(new Pair<>(att.first, eqc));
        }
        for (String n : s.nodes) {
            String eqc = lookupNode("left", n, node_map);
            nodes.add(eqc);
            inj1Node.add(new Pair<>(n, eqc));
        }
        for (String n : t.nodes) {
            String eqc = lookupNode("right", n, node_map);
            nodes.add(eqc);
            inj2Node.add(new Pair<>(n, eqc));
        }
        for (Triple<String, String, String> n : s.arrows) {
            String eqc1 = lookupNode("left", n.second, node_map);
            String eqc2 = lookupNode("left", n.third, node_map);
            arrows.add(new Triple<>("left_" + n.first, eqc1, eqc2));
            List<String> l = new LinkedList<>();
            l.add(eqc1);
            l.add("left_" + n.first);
            inj1Arrows.add(new Pair<>(n.first, l));
        }
        for (Triple<String, String, String> n : t.arrows) {
            String eqc1 = lookupNode("right", n.second, node_map);
            String eqc2 = lookupNode("right", n.third, node_map);
            arrows.add(new Triple<>("right_" + n.first, eqc1, eqc2));
            List<String> l = new LinkedList<>();
            l.add(eqc1);
            l.add("right_" + n.first);
            inj2Arrows.add(new Pair<>(n.first, l));
        }
        for (Pair<List<String>, List<String>> eq : s.eqs) {
            List<String> lhs = new LinkedList<>();
            lhs.add(lookupNode("left", eq.first.get(0), node_map));
            for (int i = 1; i < eq.first.size(); i++) {
                lhs.add("left_" + eq.first.get(i));
            }
            List<String> rhs = new LinkedList<>();
            rhs.add(lookupNode("left", eq.second.get(0), node_map));
            for (int i = 1; i < eq.second.size(); i++) {
                rhs.add("left_" + eq.second.get(i));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
        for (Pair<List<String>, List<String>> eq : t.eqs) {
            List<String> lhs = new LinkedList<>();
            lhs.add(lookupNode("right", eq.first.get(0), node_map));
            for (int i = 1; i < eq.first.size(); i++) {
                lhs.add("right_" + eq.first.get(i));
            }
            List<String> rhs = new LinkedList<>();
            rhs.add(lookupNode("right", eq.second.get(0), node_map));
            for (int i = 1; i < eq.second.size(); i++) {
                rhs.add("right_" + eq.second.get(i));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
        Const x = new Const(new LinkedList<>(nodes), new LinkedList<>(attrs), new LinkedList<>(arrows), new LinkedList<>(eqs));
        MapExp.Const inj1 = new MapExp.Const(inj1Node, inj1Attrs, inj1Arrows, s, x);
        MapExp.Const inj2 = new MapExp.Const(inj2Node, inj2Attrs, inj2Arrows, t, x);
        switch(e.kind) {
            case "delta sigma forward":
                {
                    FullQueryExp q = new Comp(new Sigma(inj2), new Delta(inj1));
                    return q;
                }
            case "delta pi forward":
                {
                    FullQueryExp q = new Comp(new Pi(inj2), new Delta(inj1));
                    return q;
                }
            case "delta sigma backward":
                {
                    FullQueryExp q = new Comp(new Sigma(inj1), new Delta(inj2));
                    return q;
                }
            case "delta pi backward":
                FullQueryExp q = new Comp(new Pi(inj1), new Delta(inj2));
                return q;
            default:
                break;
        }
        throw new RuntimeException("Unknown kind: " + e.kind);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex.getLocalizedMessage());
    }
}
Also used : Sigma(catdata.fql.decl.FullQueryExp.Sigma) Comp(catdata.fql.decl.FullQueryExp.Comp) Pi(catdata.fql.decl.FullQueryExp.Pi) Pair(catdata.Pair) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) Delta(catdata.fql.decl.FullQueryExp.Delta)

Aggregations

Const (catdata.fql.decl.SigExp.Const)12 Pair (catdata.Pair)8 Triple (catdata.Triple)7 MapExp (catdata.fql.decl.MapExp)4 HashSet (java.util.HashSet)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 InstExp (catdata.fql.decl.InstExp)2 Var (catdata.fql.decl.SigExp.Var)2 LinkedList (java.util.LinkedList)2 Comp (catdata.fql.decl.FullQueryExp.Comp)1 Delta (catdata.fql.decl.FullQueryExp.Delta)1 Pi (catdata.fql.decl.FullQueryExp.Pi)1 Sigma (catdata.fql.decl.FullQueryExp.Sigma)1 Signature (catdata.fql.decl.Signature)1 TransExp (catdata.fql.decl.TransExp)1 ExpPSM (catdata.fql.sql.ExpPSM)1 PSM (catdata.fql.sql.PSM)1