Skip to content
Snippets Groups Projects
Commit 4d4ed839 authored by Julien Lopez's avatar Julien Lopez
Browse files

Generic translation does not rely entirely on types

parent fbb249c2
No related branches found
No related tags found
No related merge requests found
......@@ -36,7 +36,7 @@ public final class QIRDBNode<DBRepr> extends QIRNode {
*/
public QIRDBNode(final DBDriver<DBRepr> driver, final QIRNode toTranslate) throws NoSuchElementException {
super(toTranslate.sourceSection);
setType(toTranslate.getType().get(), toTranslate.getTypeDriver());
toTranslate.getType().ifPresent(t -> setType(t, toTranslate.getTypeDriver()));
this.driver = driver;
this.translation = driver.translate(toTranslate);
}
......@@ -45,6 +45,10 @@ public final class QIRDBNode<DBRepr> extends QIRNode {
return translation;
}
public final DBDriver<DBRepr> getDriver() {
return driver;
}
@Override
public final String toString() {
return "DBNode(" + translation + ")";
......
......@@ -65,7 +65,14 @@ final class QIRGenericTranslation implements IQIRVisitor<QIRNode> {
public final QIRNode visit(final QIRProject qirProject) {
if (!(qirProject.getTypeDriver() instanceof MEMDriver))
return new QIRDBNode<>(qirProject.getTypeDriver(), qirProject);
return new QIRProject(qirProject.getSourceSection(), qirProject.getFormatter().accept(this), qirProject.getChild().accept(this));
final QIRNode child = qirProject.getChild().accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(((QIRDBNode<?>) child).getDriver(), new QIRProject(qirProject.getSourceSection(), qirProject.getFormatter(), child));
} catch (QIRException e) {
}
return new QIRProject(qirProject.getSourceSection(), qirProject.getFormatter().accept(this), child);
}
@Override
......@@ -77,6 +84,13 @@ final class QIRGenericTranslation implements IQIRVisitor<QIRNode> {
public final QIRNode visit(final QIRFilter qirFilter) {
if (!(qirFilter.getTypeDriver() instanceof MEMDriver))
return new QIRDBNode<>(qirFilter.getTypeDriver(), qirFilter);
final QIRNode child = qirFilter.getChild().accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(((QIRDBNode<?>) child).getDriver(), new QIRFilter(qirFilter.getSourceSection(), qirFilter.getFilter(), child));
} catch (QIRException e) {
}
return new QIRFilter(qirFilter.getSourceSection(), qirFilter.getFilter().accept(this), qirFilter.getChild().accept(this));
}
......@@ -84,6 +98,13 @@ final class QIRGenericTranslation implements IQIRVisitor<QIRNode> {
public final QIRNode visit(final QIRGroupBy qirGroupBy) {
if (!(qirGroupBy.getTypeDriver() instanceof MEMDriver))
return new QIRDBNode<>(qirGroupBy.getTypeDriver(), qirGroupBy);
final QIRNode child = qirGroupBy.getChild().accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(((QIRDBNode<?>) child).getDriver(), new QIRGroupBy(qirGroupBy.getSourceSection(), qirGroupBy.getGroup(), child));
} catch (QIRException e) {
}
return new QIRGroupBy(qirGroupBy.getSourceSection(), qirGroupBy.getGroup().accept(this), qirGroupBy.getChild().accept(this));
}
......@@ -91,6 +112,13 @@ final class QIRGenericTranslation implements IQIRVisitor<QIRNode> {
public final QIRNode visit(final QIRSortBy qirSortBy) {
if (!(qirSortBy.getTypeDriver() instanceof MEMDriver))
return new QIRDBNode<>(qirSortBy.getTypeDriver(), qirSortBy);
final QIRNode child = qirSortBy.getChild().accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(((QIRDBNode<?>) child).getDriver(), new QIRSortBy(qirSortBy.getSourceSection(), qirSortBy.getSort(), qirSortBy.getIsAscending(), child));
} catch (QIRException e) {
}
return new QIRSortBy(qirSortBy.getSourceSection(), qirSortBy.getSort().accept(this), qirSortBy.getIsAscending().accept(this), qirSortBy.getChild().accept(this));
}
......@@ -124,7 +152,7 @@ final class QIRGenericTranslation implements IQIRVisitor<QIRNode> {
@Override
public final <DBRepr> QIRNode visit(final QIRDBNode<DBRepr> qirDBNode) {
throw new QIRException("Should not visit QIRDBNode in generic translation.");
return qirDBNode;
}
@Override
......
......@@ -85,7 +85,7 @@ public final class HBaseDriver extends DBDriver<HBaseQuery> {
@Override
public HBaseQuery translate(final QIRNode query) {
return query.accept(new HBaseQueryTranslator(conf));
return query.accept(new HBaseQueryTranslator(conf, dbName, configFile));
}
@Override
......
......@@ -24,9 +24,13 @@ import qir.util.QIRException;
*/
final class HBaseQueryTranslator extends QIRTranslator<HBaseQuery> {
private final Configuration conf;
private final String dbName;
private final String dbConfig;
HBaseQueryTranslator(final Configuration conf) {
HBaseQueryTranslator(final Configuration conf, final String dbName, final String dbConfig) {
this.conf = conf;
this.dbName = dbName;
this.dbConfig = dbConfig;
}
@Override
......@@ -43,6 +47,9 @@ final class HBaseQueryTranslator extends QIRTranslator<HBaseQuery> {
@Override
public final HBaseQuery visit(QIRScan qirScan) {
if (qirScan.getTable() instanceof QIRTable && ((QIRTable) qirScan.getTable()).getDbName() instanceof QIRString && ((QIRTable) qirScan.getTable()).getConfigFile() instanceof QIRString) {
final QIRTable table = (QIRTable) qirScan.getTable();
if (!((QIRString) table.getDbName()).getValue().equals(dbName) || !((QIRString) table.getConfigFile()).getValue().equals(dbConfig))
throw new QIRException("Subquery for another database detected.");
return new HBaseQuery(new Scan(), (String) ((QIRBaseValue<?>) ((QIRTable) qirScan.getTable()).getSchemaName()).getValue() + ":" +
(String) ((QIRBaseValue<?>) ((QIRTable) qirScan.getTable()).getTableName()).getValue());
}
......
......@@ -17,7 +17,7 @@ public final class HiveDriver extends SQLStringDriver {
@Override
public String translate(final QIRNode query) {
return query.accept(new HiveStringTranslator());
return query.accept(new HiveStringTranslator(dbName, configFile));
}
@Override
......
......@@ -19,6 +19,10 @@ import qir.ast.operator.QIROperator;
import qir.util.QIRException;
public class HiveStringTranslator extends SQLStringTranslator {
public HiveStringTranslator(String dbName, String dbConfig) {
super(dbName, dbConfig);
}
@Override
public final String visit(final QIRApply qirApply) {
// TODO: Improve implementation
......
......@@ -12,7 +12,7 @@ import qir.util.QIRException;
*/
public final class OracleDriver extends SQLStringDriver {
public static final String dbName = "Oracle";
private static final SQLStringTranslator translator = new SQLStringTranslator();
private final SQLStringTranslator translator = new SQLStringTranslator(dbName, configFile);
public OracleDriver(final String configFile) {
super(configFile);
......
......@@ -18,7 +18,7 @@ public final class PostgreSQLDriver extends SQLStringDriver {
@Override
public String translate(final QIRNode query) {
return query.accept(new SQLStringTranslator());
return query.accept(new SQLStringTranslator(dbName, configFile));
}
@Override
......
......@@ -35,6 +35,21 @@ public class SQLStringTranslator extends QIRTranslator<String> {
*/
private Map<String, String> prolog = new HashMap<>();
/**
* The name of the database the query will be sent to.
*/
private final String dbName;
/**
* The configuration file of the database the query will be sent to.
*/
private final String dbConfig;
public SQLStringTranslator(final String dbName, final String dbConfig) {
this.dbName = dbName;
this.dbConfig = dbConfig;
}
public final Map<String, String> popProlog() {
final Map<String, String> res = prolog;
prolog = new HashMap<>();
......@@ -51,8 +66,12 @@ public class SQLStringTranslator extends QIRTranslator<String> {
@Override
public final String visit(final QIRScan qirScan) {
if (qirScan.getTable() instanceof QIRTable && ((QIRTable) qirScan.getTable()).getDbName() instanceof QIRString && ((QIRTable) qirScan.getTable()).getConfigFile() instanceof QIRString)
if (qirScan.getTable() instanceof QIRTable && ((QIRTable) qirScan.getTable()).getDbName() instanceof QIRString && ((QIRTable) qirScan.getTable()).getConfigFile() instanceof QIRString) {
final QIRTable table = (QIRTable) qirScan.getTable();
if (!((QIRString) table.getDbName()).getValue().equals(dbName) || !((QIRString) table.getConfigFile()).getValue().equals(dbConfig))
throw new QIRException("Subquery for another database detected.");
return "select * from " + qirScan.getTable().accept(this);
}
throw new QIRException("QIR SQL Scan not implemented for table type: " + qirScan.getTable().getClass());
}
......
......@@ -39,6 +39,7 @@ public final class QIRFunctionType extends QIRType {
}
@Override
/* TODO: Fix contravariance */
protected final boolean isSubtypeOfAux(final QIRType other) {
return other instanceof QIRFunctionType && argumentType.isSubtypeOf(((QIRFunctionType) other).argumentType) && returnType.isSubtypeOf(((QIRFunctionType) other).returnType);
}
......
......@@ -153,7 +153,8 @@ public abstract class QIRSpecificTypeSystem extends QIRTypeSystem {
/**
* Throws a {@link QIRTypeErrorException} if the actual {@link QIRType} and the expected
* {@link QIRType} do not share a common subtype in the sense of
* {@link QIRType#isSubtypeOf(QIRType)}.
* {@link QIRType#isSubtypeOf(QIRType)}. TODO: Fix implementation or change name to
* checkEitherSubtype.
*
* @param actual The actual {@link QIRType} of the expression.
* @param expected The expected {@link QIRType} for the expression.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment