use of catdata.sql.SqlTable in project fql by CategoricalData.
the class InstExpJdbcAll method makeSchema.
public Schema<Ty, En, Sym, Fk, Att> makeSchema(AqlEnv env, SqlSchema info, AqlOptions ops) {
boolean checkJava = !(Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe);
TypeSide<Ty, Sym> typeSide = new SqlTypeSide(ops);
// typeSide.validate(true);
Collage<Ty, En, Sym, Fk, Att, Void, Void> col0 = new Collage<>(typeSide.collage());
Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
for (SqlTable table : info.tables) {
col0.ens.add(new En(table.name));
for (SqlColumn c : table.columns) {
if (col0.atts.containsKey(new Att(new En(table.name), c.name))) {
throw new RuntimeException("Name collision: table " + c.table.name + " col " + c.name + " against table " + col0.atts.get(new Att(new En(table.name), c.name)).first + "\n\n.Possible solution: set option jdbc_import_col_seperator so as to avoid name collisions.");
}
col0.atts.put(new Att(new En(table.name), c.name), new Pair<>(new En(table.name), new Ty(sqlTypeToAqlType(c.type.name))));
}
}
for (SqlForeignKey fk : info.fks) {
col0.fks.put(new Fk(new En(fk.source.name), fk.toString()), new Pair<>(new En(fk.source.name), new En(fk.target.name)));
Var v = new Var("x");
for (SqlColumn tcol : fk.map.keySet()) {
SqlColumn scol = fk.map.get(tcol);
Att l = new Att(new En(scol.table.name), scol.name);
Att r = new Att(new En(tcol.table.name), tcol.name);
Term<Ty, En, Sym, Fk, Att, Void, Void> lhs = Term.Att(l, Term.Var(v));
Term<Ty, En, Sym, Fk, Att, Void, Void> rhs = Term.Att(r, Term.Fk(new Fk(new En(fk.source.name), fk.toString()), Term.Var(v)));
eqs.add(new Triple<>(new Pair<>(v, new En(fk.source.name)), lhs, rhs));
col0.eqs.add(new Eq<>(new Ctx<>(new Pair<>(v, Chc.inRight(new En(fk.source.name)))), lhs, rhs));
}
}
DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(new AqlOptions(options, col0, env.defaults), col0, typeSide.js);
Schema<Ty, En, Sym, Fk, Att> sch = new Schema<>(typeSide, col0.ens, col0.atts.map, col0.fks.map, eqs, dp, checkJava);
return sch;
}
use of catdata.sql.SqlTable in project fql by CategoricalData.
the class InstExpJdbcAll method toInstance.
private Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> toInstance(AqlEnv env, SqlInstance inst, SqlSchema info) {
AqlOptions ops = new AqlOptions(options, null, env.defaults);
Schema<Ty, En, Sym, Fk, Att> sch = makeSchema(env, info, ops);
Ctx<En, Collection<Gen>> ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
for (Ty ty : sch.typeSide.tys) {
tys0.put(ty, new HashSet<>());
}
boolean schemaOnly = (Boolean) ops.getOrDefault(AqlOption.schema_only);
boolean nullOnErr = (Boolean) ops.getOrDefault(AqlOption.import_null_on_err_unsafe);
boolean dontCheckClosure = (Boolean) ops.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
if (!schemaOnly) {
int fr = 0;
Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, Gen>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Map<SqlColumn, Optional<Object>>, Gen> i1 = new HashMap<>();
SqlColumn thePk = null;
if (table.pk.size() == 1) {
thePk = Util.get0(table.pk);
}
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
Gen i = new Gen("v" + (fr++));
/* can't do this until Gen need not be unique
if (thePk == null) {
i = new Gen("v" + (fr++));
} else {
Optional<Object> x = tuple.get(thePk);
if (!x.isPresent()) {
throw new RuntimeException("Primary key col is null in " + tuple);
}
i = new Gen(x.get().toString()); //TODO aql
}
*/
i1.put(tuple, i);
// tuple.keySet().
// i2.put(i, tuple);
ens0.get(new En(table.name)).add(i);
for (SqlColumn c : table.columns) {
if (!atts0.containsKey(i)) {
atts0.put(i, new Ctx<>());
}
Optional<Object> val = tuple.get(c);
Term<Ty, Void, Sym, Void, Void, Void, Null<?>> xxx = InstExpJdbc.objectToSk(sch, val.orElse(null), i, new Att(new En(table.name), c.name), tys0, extraRepr, false, nullOnErr);
atts0.get(i).put(new Att(new En(table.name), c.name), xxx);
}
}
iso1.put(table, i1);
// iso2.put(table, i2);
}
for (SqlForeignKey fk : info.fks) {
for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
Gen tgen = iso1.get(fk.target).get(out);
Gen sgen = iso1.get(fk.source).get(in);
if (!fks0.containsKey(sgen)) {
fks0.put(sgen, new Ctx<>());
}
fks0.get(sgen).put(new Fk(new En(fk.source.name), fk.toString()), tgen);
}
}
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dontCheckClosure);
return new SaturatedInstance<>(alg, alg, (Boolean) ops.getOrDefault(AqlOption.require_consistency), (Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
use of catdata.sql.SqlTable in project fql by CategoricalData.
the class SqlToOpl method convert.
// : happy medium between CNF and non-CNF? ignore as many columns as possible?
// ////////////////////////////////////////////////////////////////////////////////////////////////////
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convert(SqlSchema info, SqlInstance inst, String S0, String I0) {
Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
for (SqlType type : info.types) {
sorts.add(Chc.inLeft(type));
}
for (SqlTable table : info.tables) {
sorts.add(Chc.inRight(table));
entities.add(Chc.inRight(table));
for (SqlColumn col : table.columns) {
symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
}
}
for (SqlForeignKey fk : info.fks) {
symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> head = new OplTerm<>("x");
OplCtx<Chc<SqlType, SqlTable>, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", Chc.inRight(fk.source))));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs0 = new OplTerm<>(Chc.inRight(Chc.inRight(fk)), Util.singList(head));
for (SqlColumn tcol : fk.map.keySet()) {
SqlColumn scol = fk.map.get(tcol);
Chc<SqlColumn, SqlForeignKey> l = Chc.inLeft(scol);
Chc<SqlColumn, SqlForeignKey> r = Chc.inLeft(tcol);
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> lhs = new OplTerm<>(Chc.inRight(l), Util.singList(head));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs = new OplTerm<>(Chc.inRight(r), Util.singList(rhs0));
equations.add(new Triple<>(ctx, lhs, rhs));
}
}
OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
sch.validate(sig);
OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
int fr = 0;
if (inst != null) {
Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, String>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Map<SqlColumn, Optional<Object>>, String> i1 = new HashMap<>();
// Map<String, Map<SqlColumn, Optional<Object>>> i2 = new HashMap<>();
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
String i = "v" + (fr++);
i1.put(tuple, i);
// i2.put(i, tuple);
gens.put(i, Chc.inRight(table));
for (SqlColumn col : table.columns) {
SqlType ty = col.type;
Optional<Object> val = tuple.get(col);
if (!val.isPresent()) {
continue;
}
symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
iso1.put(table, i1);
// iso2.put(table, i2);
}
for (SqlForeignKey fk : info.fks) {
for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
String tgen = iso1.get(fk.target).get(out);
String sgen = iso1.get(fk.source).get(in);
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
}
OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
P.toSig();
I.validate(sch, P, null);
return new Pair<>(sch, I);
}
use of catdata.sql.SqlTable in project fql by CategoricalData.
the class SqlToOpl method convertCnf.
// : code formatter should not wrap lines ever
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convertCnf(SqlSchema info, SqlInstance inst, String S0, String I0) {
if (!info.isCnf()) {
throw new RuntimeException("Schema not in categorical normal form");
}
Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
for (SqlType type : info.types) {
sorts.add(Chc.inLeft(type));
}
for (SqlTable table : info.tables) {
sorts.add(Chc.inRight(table));
entities.add(Chc.inRight(table));
for (SqlColumn col : table.columns) {
if (col.equals(table.getCnfId())) {
continue;
}
if (isFk(info, table, col)) {
continue;
}
symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
}
}
for (SqlForeignKey fk : info.fks) {
symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
}
OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
sch.validate(sig);
OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
int fr = 0;
if (inst != null) {
Map<SqlTable, Map<Object, String>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Object, String> i1 = new HashMap<>();
// Map<String, Object> i2 = new HashMap<>();
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
String i = "v" + (fr++);
if (!tuple.get(table.getCnfId()).isPresent()) {
throw new RuntimeException("Anomly: please report");
}
i1.put(tuple.get(table.getCnfId()).get(), i);
// i2.put(i, tuple.get(table.getCnfId()).get());
gens.put(i, Chc.inRight(table));
for (SqlColumn col : table.columns) {
if (col.equals(table.getCnfId())) {
continue;
}
if (isFk(info, table, col)) {
continue;
}
SqlType ty = col.type;
Optional<Object> val = tuple.get(col);
if (!val.isPresent()) {
continue;
}
symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
iso1.put(table, i1);
// iso2.put(table, i2);
}
for (SqlForeignKey fk : info.fks) {
for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
if (!out.get(fk.target.getCnfId()).isPresent() || !in.get(fk.source.getCnfId()).isPresent()) {
throw new RuntimeException("Anomaly: please report");
}
String tgen = iso1.get(fk.target).get(out.get(fk.target.getCnfId()).get());
String sgen = iso1.get(fk.source).get(in.get(fk.source.getCnfId()).get());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
}
OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
P.toSig();
I.validate(sch, P, null);
return new Pair<>(sch, I);
}
Aggregations