From a014f22d8c8c5c15aa08500a02da0d0717d62b64 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Wed, 13 Nov 2019 21:06:46 +0100 Subject: Pass interpreter tests --- .../mimuw/cloudatlas/interpreter/Functions.java | 46 +++++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Functions.java') 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); } }; -- cgit v1.2.3