m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultColumn.java
blob: 3e0b55ac8999a71241e0c44f90d559821d2a5964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 ResultColumn extends Result {
    private final List<Value> column;

    public ResultColumn(List<Value> column) {
        this.column = column;
    }

    @Override
    protected ResultColumn binaryOperationTyped(BinaryOperation operation, ResultSingle right) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : column) {
            results.add(operation.perform(value, right.getValue()));
        }

        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>();

        for (int i = 0; i < column.size(); i++) {
            results.add(operation.perform(column.get(i), right.column.get(i)));
        }

        return new ResultColumn(results);
    }

    @Override
    public ResultColumn unaryOperation(UnaryOperation operation) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : column) {
            results.add(operation.perform(value));
        }
        return new ResultColumn(results);
    }

    @Override
    protected Result callMe(BinaryOperation operation, Result left) {
        return left.binaryOperationTyped(operation, this);
    }

    @Override
    public Value getValue() {
        throw new UnsupportedOperationException("Not a ResultSingle.");
    }

    @Override
    public ValueList getList() {
        throw new UnsupportedOperationException("Not a ResultList.");
    }

    @Override
    public ValueList getColumn() {
        return new ValueList(column, TypeCollection.computeElementType(column));
    }

    @Override
    public ResultSingle aggregationOperation(AggregationOperation operation) {
        return new ResultSingle(operation.perform(getColumn()));
    }

    @Override
    public Result transformOperation(TransformOperation operation) {
        return new ResultList(operation.perform(getColumn()));
    }

    @Override
    public Result filterNulls() {
        throw new UnsupportedOperationException("Operation filterNulls not supported yet.");
    }

    @Override
    public Result first(int size) {
        List<Value> subList = column.subList(0, Math.min(size, column.size()));
        return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList)));
    }

    @Override
    public Result last(int size) {
        List<Value> subList = column.subList(
                Math.max(0, column.size() - size),
                column.size()
        );
        return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList)));
    }

    @Override
    public Result random(int size) {
        return new ResultSingle(
            randomList(
                new ValueList(
                    column,
                    TypeCollection.computeElementType(column)
                ),
                size
            )
        );
    }

    @Override
    public ResultColumn convertTo(Type to) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : column) {
            results.add(value.convertTo(to));
        }

        return new ResultColumn(results);
    }

    @Override
    public ResultSingle isNull() {
        return new ResultSingle(new ValueBoolean(false));
    }

    @Override
    public Type getType() {
        Type type = TypePrimitive.NULL;
        for (Value value : column) {
            if (value.getType() != TypePrimitive.NULL) {
                type = value.getType();
            }
        }

        return type;
    }
}