m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-11-13 21:06:46 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-11-13 21:06:46 +0100
commita014f22d8c8c5c15aa08500a02da0d0717d62b64 (patch)
tree129e978cb4675ffb4cb9c2b5e2d0c3a3981cb20c /src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java
parent530be09d2cf8e50ca4b3e172c847b361b2d710ad (diff)
Pass interpreter tests
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java46
1 files changed, 40 insertions, 6 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java
index 2fcb9b3..2b44633 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java
@@ -77,8 +77,12 @@ class Functions {
private static final UnaryOperation CEIL = new UnaryOperation() {
@Override
public Value perform(Value v) {
- // TODO
- throw new UnsupportedOperationException("CEIL Not yet implemented");
+ if(v.getType().isCompatible(TypePrimitive.DOUBLE)) {
+ if(v.isNull())
+ return new ValueDouble(null);
+ return new ValueDouble((double)Math.ceil(((ValueDouble)v).getValue()));
+ }
+ throw new IllegalArgumentException("Value must have type " + TypePrimitive.DOUBLE + ".");
}
};
@@ -96,8 +100,26 @@ class Functions {
private static final AggregationOperation SUM = new AggregationOperation() {
@Override
public Value perform(ValueList values) {
- // TODO
- throw new UnsupportedOperationException("SUM Not yet implemented");
+ Type elementType = ((TypeCollection)values.getType()).getElementType();
+ PrimaryType primaryType = elementType.getPrimaryType();
+
+ if(primaryType != PrimaryType.INT && primaryType != PrimaryType.DOUBLE && primaryType != PrimaryType.DURATION
+ && primaryType != PrimaryType.NULL) {
+ throw new IllegalArgumentException("Aggregation doesn't support type: " + elementType + ".");
+ }
+
+ ValueList nlist = Result.filterNullsList(values);
+ if(nlist.getValue() == null || nlist.isEmpty()) {
+ return ValueNull.getInstance();
+ }
+
+ Value result = nlist.get(0).getDefaultValue();
+
+ for(Value v : nlist) {
+ result = result.addValue(v);
+ }
+
+ return result;
}
};
@@ -151,8 +173,20 @@ class Functions {
private static final AggregationOperation OR = new AggregationOperation() {
@Override
public ValueBoolean perform(ValueList values) { // lazy
- // TODO
- throw new UnsupportedOperationException("OR Not yet implemented");
+ ValueList nlist = Result.filterNullsList(values);
+ if(nlist.getValue() == null) {
+ return new ValueBoolean(null);
+ } else if(values.isEmpty()) {
+ return new ValueBoolean(false);
+ }
+ for(Value v : nlist) {
+ if(v.getType().isCompatible(TypePrimitive.BOOLEAN)) {
+ if(v.isNull() || ((ValueBoolean)v).getValue())
+ return new ValueBoolean(true);
+ } else
+ throw new IllegalArgumentException("Aggregation doesn't support type: " + v.getType() + ".");
+ }
+ return new ValueBoolean(false);
}
};