diff options
Diffstat (limited to 'src')
6 files changed, 285 insertions, 9 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Interpreter.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Interpreter.java index 6129f48..339fce6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Interpreter.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Interpreter.java @@ -482,8 +482,7 @@ public class Interpreter { } public Result visit(RelOpEqC op, ValuesPair pair) { - // TODO - throw new UnsupportedOperationException("RelOpEqC Not yet implemented"); + return pair.left.isEqual(pair.right); } public Result visit(RelOpNeC op, ValuesPair pair) { @@ -495,8 +494,7 @@ public class Interpreter { } public Result visit(RelOpLeC op, ValuesPair pair) { - // TODO - throw new UnsupportedOperationException("RelOpLeC Not yet implemented"); + return pair.right.isLowerThan(pair.left).negate(); } public Result visit(RelOpGeC op, ValuesPair pair) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Result.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Result.java index 5c1a494..383c097 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Result.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Result.java @@ -135,6 +135,7 @@ abstract class Result { protected abstract Result binaryOperationTyped(BinaryOperation operation, ResultSingle right); protected abstract Result binaryOperationTyped(BinaryOperation operation, ResultColumn right); + protected abstract Result binaryOperationTyped(BinaryOperation operation, ResultList right); public Result binaryOperation(BinaryOperation operation, Result right) { return right.callMe(operation, this); @@ -151,12 +152,10 @@ abstract class Result { public abstract ValueList getColumn(); public ResultSingle aggregationOperation(AggregationOperation operation) { - // TODO throw new UnsupportedOperationException("aggregation Not yet implemented"); } public Result transformOperation(TransformOperation operation) { - // TODO throw new UnsupportedOperationException("transformation Not yet implemented"); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultColumn.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultColumn.java index 1c0db30..3e0b55a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultColumn.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultColumn.java @@ -28,6 +28,10 @@ class ResultColumn extends Result { return new ResultColumn(results); } + protected Result binaryOperationTyped(BinaryOperation operation, ResultList right) { + throw new UnsupportedOperationException("Binary operation not supported on ResultColumn and ResultLists"); + } + protected ResultColumn binaryOperationTyped(BinaryOperation operation, ResultColumn right) { List<Value> results = new ArrayList<Value>(); @@ -75,8 +79,7 @@ class ResultColumn extends Result { @Override public Result transformOperation(TransformOperation operation) { - // TODO: this should be a ResultList - return new ResultColumn(operation.perform(getColumn())); + return new ResultList(operation.perform(getColumn())); } @Override @@ -125,7 +128,7 @@ class ResultColumn extends Result { @Override public ResultSingle isNull() { - return new ResultSingle(new ValueBoolean(true)); + return new ResultSingle(new ValueBoolean(false)); } @Override diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultList.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultList.java new file mode 100644 index 0000000..c79a984 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultList.java @@ -0,0 +1,139 @@ +package pl.edu.mimuw.cloudatlas.interpreter; + +import java.util.ArrayList; +import java.util.List; + +import pl.edu.mimuw.cloudatlas.model.Type; +import pl.edu.mimuw.cloudatlas.model.TypePrimitive; +import pl.edu.mimuw.cloudatlas.model.TypeCollection; +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ValueBoolean; +import pl.edu.mimuw.cloudatlas.model.ValueList; + +class ResultList extends Result { + private final List<Value> list; + + public ResultList(List<Value> list) { + this.list = list; + } + + @Override + protected ResultList binaryOperationTyped(BinaryOperation operation, ResultSingle right) { + List<Value> results = new ArrayList<Value>(); + + for (Value value : list) { + results.add(operation.perform(value, right.getValue())); + } + + return new ResultList(results); + } + + protected Result binaryOperationTyped(BinaryOperation operation, ResultList right) { + throw new UnsupportedOperationException("Binary operation not supported on two ResultLists"); + } + + protected Result binaryOperationTyped(BinaryOperation operation, ResultColumn right) { + throw new UnsupportedOperationException("Binary operation not supported on ResultList and ResultColumn"); + } + + @Override + public ResultList unaryOperation(UnaryOperation operation) { + List<Value> results = new ArrayList<Value>(); + + for (Value value : list) { + results.add(operation.perform(value)); + } + return new ResultList(results); + } + + @Override + protected Result callMe(BinaryOperation operation, Result left) { + return left.binaryOperationTyped(operation, this); + } + + @Override + public Value getValue() { + throw new UnsupportedOperationException("ResultList: Not a ResultSingle."); + } + + @Override + public ValueList getList() { + return new ValueList(list, TypeCollection.computeElementType(list)); + } + + @Override + public ValueList getColumn() { + throw new UnsupportedOperationException("Not a ResultColumn."); + } + + @Override + public ResultSingle aggregationOperation(AggregationOperation operation) { + return new ResultSingle(operation.perform(getList())); + } + + @Override + public Result transformOperation(TransformOperation operation) { + return new ResultList(operation.perform(getList())); + } + + @Override + public Result filterNulls() { + throw new UnsupportedOperationException("Operation filterNulls not supported yet."); + } + + @Override + public Result first(int size) { + List<Value> subList = list.subList(0, Math.min(size, list.size())); + return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList))); + } + + @Override + public Result last(int size) { + List<Value> subList = list.subList( + Math.max(0, list.size() - size), + list.size() + ); + return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList))); + } + + @Override + public Result random(int size) { + return new ResultSingle( + randomList( + new ValueList( + list, + TypeCollection.computeElementType(list) + ), + size + ) + ); + } + + @Override + public ResultList convertTo(Type to) { + List<Value> results = new ArrayList<Value>(); + + for (Value value : list) { + results.add(value.convertTo(to)); + } + + return new ResultList(results); + } + + @Override + public ResultSingle isNull() { + return new ResultSingle(new ValueBoolean(true)); + } + + @Override + public Type getType() { + Type type = TypePrimitive.NULL; + for (Value value : list) { + if (value.getType() != TypePrimitive.NULL) { + type = value.getType(); + } + } + + return type; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultSingle.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultSingle.java index 3d88bb0..4898161 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultSingle.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultSingle.java @@ -55,6 +55,17 @@ class ResultSingle extends Result { return new ResultColumn(results); } + protected ResultList binaryOperationTyped(BinaryOperation operation, ResultList right) { + List<Value> results = new ArrayList<Value>(); + + for (Value value : right.getList()) { + results.add(operation.perform(this.value, value)); + } + + return new ResultList(results); + + } + @Override public ResultSingle unaryOperation(UnaryOperation operation) { return new ResultSingle(operation.perform(value)); diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java index aaac9ce..1c7be00 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/interpreter/InterpreterTests.java @@ -481,6 +481,132 @@ public class InterpreterTests { ); } + @Test + public void testBinopOnListResultFails() throws Exception { + assertInterpreterRun( + "SELECT max(distinct(cardinality) + num_cores) AS x", + new String[] { + } + ); + } + + @Test + public void testAggregateResultList() throws Exception { + assertInterpreterRun( + "SELECT sum(cardinality) AS cardinality; SELECT sum(distinct(cardinality)) AS dc", + new String[] { + "/uw: cardinality: 3", + "/uw: dc: 1", + "/pjwstk: cardinality: 2", + "/pjwstk: dc: 1", + "/: cardinality: 5", + "/: dc: 5" + } + ); + } + + @Test + public void testEq() throws Exception { + assertInterpreterRun( + "SELECT 1 = 1 AS oeo, 1 = 0 AS oez", + new String[] { + "/uw: oeo: true", + "/uw: oez: false", + "/pjwstk: oeo: true", + "/pjwstk: oez: false", + "/: oeo: true", + "/: oez: false" + } + ); + } + + @Test + public void testNeq() throws Exception { + assertInterpreterRun( + "SELECT 1 <> 1 AS ono, 1 <> 0 AS onz", + new String[] { + "/uw: ono: false", + "/uw: onz: true", + "/pjwstk: ono: false", + "/pjwstk: onz: true", + "/: ono: false", + "/: onz: true" + } + ); + } + + @Test + public void testLt() throws Exception { + assertInterpreterRun( + "SELECT 0 < 1 AS zlo, 1 < 1 AS olo, 1 < 0 AS olz", + new String[] { + "/uw: zlo: true", + "/uw: olo: false", + "/uw: olz: false", + "/pjwstk: zlo: true", + "/pjwstk: olo: false", + "/pjwstk: olz: false", + "/: zlo: true", + "/: olo: false", + "/: olz: false", + } + ); + } + + @Test + public void testLeq() throws Exception { + assertInterpreterRun( + "SELECT 0 <= 1 AS zlo, 1 <= 1 AS olo, 1 <= 0 AS olz", + new String[] { + "/uw: zlo: true", + "/uw: olo: true", + "/uw: olz: false", + "/pjwstk: zlo: true", + "/pjwstk: olo: true", + "/pjwstk: olz: false", + "/: zlo: true", + "/: olo: true", + "/: olz: false", + } + ); + } + + @Test + public void testGeq() throws Exception { + assertInterpreterRun( + "SELECT 0 >= 1 AS zgo, 1 >= 1 AS ogo, 1 >= 0 AS ogz", + new String[] { + "/uw: zgo: false", + "/uw: ogo: true", + "/uw: ogz: true", + "/pjwstk: zgo: false", + "/pjwstk: ogo: true", + "/pjwstk: ogz: true", + "/: zgo: false", + "/: ogo: true", + "/: ogz: true", + } + ); + } + + @Test + public void testGt() throws Exception { + assertInterpreterRun( + "SELECT 0 > 1 AS zgo, 1 > 1 AS ogo, 1 > 0 AS ogz", + new String[] { + "/uw: zgo: false", + "/uw: ogo: false", + "/uw: ogz: true", + "/pjwstk: zgo: false", + "/pjwstk: ogo: false", + "/pjwstk: ogz: true", + "/: zgo: false", + "/: ogo: false", + "/: ogz: true", + } + ); + } + private void assertInterpreterRun(String query, String[] expectedOutput) throws Exception { ByteArrayInputStream in = new ByteArrayInputStream(query.getBytes()); |