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

SortBy updated to handle desc, minor fixes

parent e1429e75
No related branches found
No related tags found
No related merge requests found
......@@ -11,13 +11,13 @@ import qir.visitor.IQIRVisitor;
*/
public final class QIRLcons extends QIRList {
/**
* The first value of the {@link QIRList}. TODO: Should be final?
* The first value of the {@link QIRList}.
*/
public QIRNode value;
public final QIRNode value;
/**
* The rest of the {@link QIRList}. TODO: Should be final?
* The rest of the {@link QIRList}.
*/
public QIRNode tail;
public final QIRNode tail;
public QIRLcons(final SourceSection source, final QIRNode value, final QIRNode tail) {
super(source);
......
......@@ -11,17 +11,17 @@ import qir.visitor.IQIRVisitor;
*/
public final class QIRTcons extends QIRTuple {
/**
* The name of the first field in the {@link QIRTuple}. TODO: Should be final?
* The name of the first field in the {@link QIRTuple}.
*/
public String id;
public final String id;
/**
* The value of the first field in the {@link QIRTuple}.
*/
public final QIRNode value;
/**
* The rest of the {@link QIRTuple}. TODO: Should be final?
* The rest of the {@link QIRTuple}.
*/
public QIRNode tail;
public final QIRNode tail;
public QIRTcons(final SourceSection source, final String id, final QIRNode value, final QIRNode tail) {
super(source);
......
......@@ -7,9 +7,9 @@ import qir.util.QIRAny;
import qir.visitor.IQIRVisitor;
/**
* The {@link QIRGroup} represents the "group by" operation of SQL.
* The {@link QIRGroupBy} represents the "group by" operation of SQL.
*/
public final class QIRGroup extends QIROperator {
public final class QIRGroupBy extends QIROperator {
/**
* The list of grouping rows.
*/
......@@ -19,7 +19,7 @@ public final class QIRGroup extends QIROperator {
*/
public final QIRNode child;
public QIRGroup(final SourceSection source, final QIRNode group, final QIRNode child) {
public QIRGroupBy(final SourceSection source, final QIRNode group, final QIRNode child) {
super(source);
this.group = group;
this.child = child;
......@@ -27,16 +27,16 @@ public final class QIRGroup extends QIROperator {
@Override
public final String toString() {
return "Group(" + group + ", " + child + ")";
return "GroupBy(" + group + ", " + child + ")";
}
@Override
public boolean equals(Object other) {
if (other instanceof QIRAny)
return true;
if (!(other instanceof QIRGroup))
if (!(other instanceof QIRGroupBy))
return false;
return group.equals(((QIRGroup) other).group) && child.equals(((QIRGroup) other).child);
return group.equals(((QIRGroupBy) other).group) && child.equals(((QIRGroupBy) other).child);
}
@Override
......
......@@ -7,36 +7,41 @@ import qir.util.QIRAny;
import qir.visitor.IQIRVisitor;
/**
* The {@link QIROrder} represents the "order by" operation of SQL.
* The {@link QIRSortBy} represents the "order by" operation of SQL.
*/
public final class QIROrder extends QIROperator {
public final class QIRSortBy extends QIROperator {
/**
* The list of ordering rows.
*/
public final QIRNode order;
/**
* A list of booleans telling for every row of {@link QIRSortBy#order} if it is ascending.
*/
public final QIRNode isAscending;
/**
* The next {@link QIROperator} in the tree.
*/
public final QIRNode child;
public QIROrder(final SourceSection source, final QIRNode order, final QIRNode child) {
public QIRSortBy(final SourceSection source, final QIRNode order, final QIRNode isAscending, final QIRNode child) {
super(source);
this.order = order;
this.isAscending = isAscending;
this.child = child;
}
@Override
public final String toString() {
return "Order(" + order + ", " + child + ")";
return "SortBy(" + order + ", " + isAscending + ", " + child + ")";
}
@Override
public boolean equals(Object other) {
if (other instanceof QIRAny)
return true;
if (!(other instanceof QIROrder))
if (!(other instanceof QIRSortBy))
return false;
return order.equals(((QIROrder) other).order) && child.equals(((QIROrder) other).child);
return order.equals(((QIRSortBy) other).order) && isAscending.equals(((QIRSortBy) other).isAscending) && child.equals(((QIRSortBy) other).child);
}
@Override
......
......@@ -6,8 +6,6 @@ import qir.visitor.QIRCompileVisitor;
public class QIRDriver {
public static final QIRNode run(final QIRNode root) {
System.out.println(root);
System.out.println(root.accept(new QIRCompileVisitor()));
return root.accept(new QIRCompileVisitor()).accept(new QIREvaluationVisitor());
}
}
\ No newline at end of file
......@@ -19,9 +19,9 @@ public interface IQIRVisitor<T> {
public abstract T visit(final QIRFilter qirFilter);
public abstract T visit(final QIRGroup qirGroup);
public abstract T visit(final QIRGroupBy qirGroup);
public abstract T visit(final QIROrder qirOrder);
public abstract T visit(final QIRSortBy qirOrder);
public abstract T visit(final QIRJoin qirJoin);
......
......@@ -31,6 +31,11 @@ public final class QIRCompileVisitor implements IQIRVisitor<QIRNode> {
if (table instanceof QIRTable)
return new QIRDBNode<>(table.sourceSection, ((QIRTable<?>) table).driver, qirScan);
if (table instanceof QIRDBNode)
try {
return new QIRDBNode<>(table.sourceSection, ((QIRDBNode<?>) table).driver, new QIRScan(qirScan.sourceSection, table));
} catch (QIRException e) {
}
return new QIRScan(qirScan.sourceSection, table);
}
......@@ -47,27 +52,27 @@ public final class QIRCompileVisitor implements IQIRVisitor<QIRNode> {
}
@Override
public final QIRNode visit(final QIRGroup qirGroup) {
public final QIRNode visit(final QIRGroupBy qirGroup) {
final QIRNode child = qirGroup.child.accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(child.sourceSection, ((QIRDBNode<?>) child).driver, new QIRGroup(qirGroup.sourceSection, qirGroup.group, child));
return new QIRDBNode<>(child.sourceSection, ((QIRDBNode<?>) child).driver, new QIRGroupBy(qirGroup.sourceSection, qirGroup.group, child));
} catch (QIRException e) {
}
return new QIRGroup(qirGroup.sourceSection, qirGroup.group.accept(this), child);
return new QIRGroupBy(qirGroup.sourceSection, qirGroup.group.accept(this), child);
}
@Override
public final QIRNode visit(final QIROrder qirOrder) {
public final QIRNode visit(final QIRSortBy qirOrder) {
final QIRNode child = qirOrder.child.accept(this);
if (child instanceof QIRDBNode)
try {
return new QIRDBNode<>(child.sourceSection, ((QIRDBNode<?>) child).driver, new QIROrder(qirOrder.sourceSection, qirOrder.order, child));
return new QIRDBNode<>(child.sourceSection, ((QIRDBNode<?>) child).driver, new QIRSortBy(qirOrder.sourceSection, qirOrder.order, qirOrder.isAscending, child));
} catch (QIRException e) {
}
return new QIROrder(qirOrder.sourceSection, qirOrder.order.accept(this), child);
return new QIRSortBy(qirOrder.sourceSection, qirOrder.order.accept(this), qirOrder.isAscending.accept(this), child);
}
@Override
......@@ -138,7 +143,7 @@ public final class QIRCompileVisitor implements IQIRVisitor<QIRNode> {
@Override
public final QIRNode visit(final QIRApply qirApply) {
return new QIRApply(qirApply.sourceSection, qirApply.left.accept(this), qirApply.right.accept(this));
return new QIRApply(qirApply.sourceSection, qirApply.left.accept(this), qirApply.right != null ? qirApply.right.accept(this) : null);
}
@Override
......
......@@ -67,12 +67,12 @@ public final class QIREvaluationVisitor implements IQIRVisitor<QIRNode> {
}
@Override
public QIRNode visit(QIRGroup qirGroup) {
public QIRNode visit(QIRGroupBy qirGroup) {
throw new QIRException("Group not implemented yet.");
}
@Override
public QIRNode visit(QIROrder qirOrder) {
public QIRNode visit(QIRSortBy qirOrder) {
throw new QIRException("Order not implemented yet.");
}
......
......@@ -37,14 +37,15 @@ public final class QIRFreeVarsVisitor implements IQIRVisitor<Map<String, QIRVari
}
@Override
public final Map<String, QIRVariable> visit(final QIRGroup qirGroup) {
public final Map<String, QIRVariable> visit(final QIRGroupBy qirGroup) {
qirGroup.group.accept(this);
return qirGroup.child.accept(this);
}
@Override
public final Map<String, QIRVariable> visit(final QIROrder qirOrder) {
public final Map<String, QIRVariable> visit(final QIRSortBy qirOrder) {
qirOrder.order.accept(this);
qirOrder.isAscending.accept(this);
return qirOrder.child.accept(this);
}
......
......@@ -40,13 +40,13 @@ public final class QIRGreedyReduceVisitor implements IQIRVisitor<QIRNode> {
}
@Override
public final QIRNode visit(final QIRGroup qirGroup) {
return new QIRGroup(qirGroup.sourceSection, qirGroup.group.accept(this), qirGroup.child.accept(this));
public final QIRNode visit(final QIRGroupBy qirGroup) {
return new QIRGroupBy(qirGroup.sourceSection, qirGroup.group.accept(this), qirGroup.child.accept(this));
}
@Override
public final QIRNode visit(final QIROrder qirOrder) {
return new QIROrder(qirOrder.sourceSection, qirOrder.order.accept(this), qirOrder.child.accept(this));
public final QIRNode visit(final QIRSortBy qirOrder) {
return new QIRSortBy(qirOrder.sourceSection, qirOrder.order.accept(this), qirOrder.isAscending.accept(this), qirOrder.child.accept(this));
}
@Override
......
......@@ -47,7 +47,9 @@ public final class QIRSQLStringVisitor implements IQIRVisitor<String> {
@Override
public final String visit(final QIRScan qirScan) {
return "select * from " + qirScan.table.accept(this);
if (qirScan.table instanceof QIRTable)
return "select * from " + qirScan.table.accept(this);
return "select * from (" + qirScan.table.accept(this) + ") as " + genFreshId();
}
@Override
......@@ -58,18 +60,25 @@ public final class QIRSQLStringVisitor implements IQIRVisitor<String> {
}
@Override
public final String visit(final QIRGroup qirGroup) {
public final String visit(final QIRGroupBy qirGroup) {
if (qirGroup.group instanceof QIRLambda)
return qirGroup.child.accept(this) + " group by " + ((QIRLambda) qirGroup.group).body.accept(this);
return qirGroup.child.accept(this) + " group by " + qirGroup.group.accept(this);
throw new QIRException("QIR SQL Group not implemented for group type: " + qirGroup.group.getClass());
}
@Override
public final String visit(final QIROrder qirOrder) {
if (qirOrder.order instanceof QIRLambda)
return qirOrder.child.accept(this) + " order by " + ((QIRLambda) qirOrder.order).body.accept(this);
else
return qirOrder.child.accept(this) + " order by " + qirOrder.order.accept(this);
public final String visit(final QIRSortBy qirOrder) {
if (qirOrder.order instanceof QIRLambda && ((QIRLambda) qirOrder.order).body instanceof QIRLcons && qirOrder.isAscending instanceof QIRLambda &&
((QIRLambda) qirOrder.isAscending).body instanceof QIRLcons) {
QIRNode rows = ((QIRLambda) qirOrder.order).body;
QIRNode ascs = ((QIRLambda) qirOrder.isAscending).body;
String o = ((QIRLcons) rows).value.accept(this) + (((QIRLcons) ascs).value.accept(this).equals(new QIRBoolean(null, true)) ? "" : " desc");
for (rows = ((QIRLcons) rows).tail, ascs = ((QIRLcons) ascs).tail; rows instanceof QIRLcons; rows = ((QIRLcons) rows).tail, ascs = ((QIRLcons) ascs).tail)
o += ", " + ((QIRLcons) rows).value.accept(this) + (((QIRLcons) ascs).value.accept(this).equals(new QIRBoolean(null, true)) ? "" : " desc");
return "select * from (" + qirOrder.child.accept(this) + ") as " + ((QIRLambda) qirOrder.order).var.accept(this) + " order by " + o;
}
throw new QIRException("QIR SQL OrderBy not implemented for: " + qirOrder.order.getClass() + " and " + qirOrder.isAscending.getClass());
}
@Override
......@@ -160,6 +169,10 @@ public final class QIRSQLStringVisitor implements IQIRVisitor<String> {
return name + " " + args.stream().map(arg -> arg.accept(this)).collect(Collectors.joining(" "));
if (name.equals("count") && args.size() == 0)
return "count(*)";
if (name.equals("like") && args.size() == 2)
return args.pop().accept(this) + " like " + args.pop().accept(this);
if (name.equals("notlike") && args.size() == 2)
return args.pop().accept(this) + " not like " + args.pop().accept(this);
}
return curr.accept(this) + "(" + args.stream().map(arg -> arg.accept(this)).collect(Collectors.joining(", ")) + ")";
}
......
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