m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Interpreter.java
blob: 289f99d21d8e67e76ecde9291c26b0b7bca59401 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
 * Copyright (c) 2014, University of Warsaw
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package pl.edu.mimuw.cloudatlas.interpreter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import pl.edu.mimuw.cloudatlas.interpreter.query.PrettyPrinter;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.AliasedSelItemC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.AscOrderC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExpr;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprAddC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprDivC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprModC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprMulC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprNegC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BasicExprSubC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BoolExpr;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BoolExprBasicExprC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BoolExprCmpC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.BoolExprRegExpC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.CondExpr;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.CondExprAndC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.CondExprBoolExprC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.CondExprNotC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.CondExprOrC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.DescOrderC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EBoolC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.ECondExprC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EDblC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EFunC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EIdentC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EIntC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EStmtC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.EStrC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NoNullsC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NoOrderByC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NoOrderC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NoWhereC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NullFirstsC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Nulls;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.NullsLastC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Order;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.OrderBy;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.OrderByC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.OrderItem;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.OrderItemC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.ProgramC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOp;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpEqC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpGeC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpGtC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpLeC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpLtC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.RelOpNeC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.SelItem;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.SelItemC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Statement;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.StatementC;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Where;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.WhereC;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueBoolean;
import pl.edu.mimuw.cloudatlas.model.ValueDouble;
import pl.edu.mimuw.cloudatlas.model.ValueInt;
import pl.edu.mimuw.cloudatlas.model.ValueString;
import pl.edu.mimuw.cloudatlas.model.ZMI;

public class Interpreter {
    private static class ValuesPair {
        public final Result left;
        public final Result right;

        public ValuesPair(Result left, Result right) {
            this.left = left;
            this.right = right;
        }
    }

    private final ZMI zmi;

    public Interpreter(ZMI zmi) {
        this.zmi = zmi;
    }

    private static Boolean getBoolean(Value value) {
        if(value.getType().isCompatible(TypePrimitive.BOOLEAN)) {
            Boolean b = ((ValueBoolean)value).getValue();
            return b == null? false : b.booleanValue();
        }
        throw new InvalidTypeException(TypePrimitive.BOOLEAN, value.getType());
    }

    public List<QueryResult> interpretProgram(Program program) {
        return program.accept(new ProgramInterpreter(), zmi);
    }

    public List<QueryResult> interpretStatement(Statement statement) {
        return statement.accept(new StatementInterpreter(), zmi);
    }

    public class ProgramInterpreter implements Program.Visitor<List<QueryResult>, ZMI> {
        public List<QueryResult> visit(ProgramC program, ZMI zmi) {
            List<QueryResult> results = new ArrayList<QueryResult>();
            for(Statement s : program.liststatement_) {
                try {
                    List<QueryResult> l = s.accept(new StatementInterpreter(), zmi);
                    for(QueryResult qr : l)
                        if(qr.getName() == null)
                            throw new IllegalArgumentException("All items in top-level SELECT must be aliased.");
                    results.addAll(l);
                } catch(Exception exception) {
                    throw new InsideQueryException(PrettyPrinter.print(s), exception);
                }
            }
            return results;
        }
    }

    public class StatementInterpreter implements Statement.Visitor<List<QueryResult>, ZMI> {
        public List<QueryResult> visit(StatementC statement, ZMI zmi) {
            Table table = new Table(zmi);
            try {
                table = statement.where_.accept(new WhereInterpreter(), table);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(statement.where_), exception);
            }
            try {
                table = statement.orderby_.accept(new OrderByInterpreter(), table);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(statement.orderby_), exception);
            }
            List<QueryResult> ret = new ArrayList<QueryResult>();

            for(SelItem selItem : statement.listselitem_) {
                try {
                    QueryResult qr = selItem.accept(new SelItemInterpreter(), table);
                    if(qr.getName() != null) {
                        for(QueryResult qrRet : ret)
                            if(qr.getName().getName().equals(qrRet.getName().getName()))
                                throw new IllegalArgumentException("Alias collision.");
                    }
                    ret.add(qr);
                } catch(Exception exception) {
                    throw new InsideQueryException(PrettyPrinter.print(selItem), exception);
                }
            }

            return ret;
        }
    }

    public class WhereInterpreter implements Where.Visitor<Table, Table> {
        public Table visit(NoWhereC where, Table table) {
            return table;
        }

        public Table visit(WhereC where, Table table) {
            Table result = new Table(table);
            for(TableRow row : table) {
                Environment env = new EnvironmentRow(row, table.getColumns());
                Value value = where.condexpr_.accept(new CondExprInterpreter(), env).getValue();
                if(getBoolean(value))
                    result.appendRow(row);
            }
            return result;
        }
    }

    public class OrderByInterpreter implements OrderBy.Visitor<Table, Table> {
        public Table visit(NoOrderByC orderBy, Table table) {
            return table;
        }

        public Table visit(OrderByC orderBy, Table table) {
            for(OrderItem item : orderBy.listorderitem_) {
                try {
                    table = item.accept(new OrderItemInterpreter(), table);
                } catch(Exception exception) {
                    throw new InsideQueryException(PrettyPrinter.print(item), exception);
                }
            }
            return table;
        }
    }

    public class OrderItemInterpreter implements OrderItem.Visitor<Table, Table> {
        public Table visit(final OrderItemC orderItem, final Table table) {
            Comparator<TableRow> comparator = new Comparator<TableRow>() {
                @Override
                public int compare(TableRow row1, TableRow row2) {
                    Environment env1 = new EnvironmentRow(row1, table.getColumns());
                    Result expr1 = orderItem.condexpr_.accept(new CondExprInterpreter(), env1);
                    Environment env2 = new EnvironmentRow(row2, table.getColumns());
                    Result expr2 = orderItem.condexpr_.accept(new CondExprInterpreter(), env2);
                    ValuesPair pair = new ValuesPair(expr1, expr2);
                    int result = orderItem.nulls_.accept(new NullsInterpreter(), pair);
                    if(result == 0)
                        result = orderItem.order_.accept(new OrderInterpreter(), pair);
                    return result;
                }
            };
            table.sort(comparator);
            return table;
        }
    }

    public class OrderInterpreter implements Order.Visitor<Integer, ValuesPair> {
        private int compareAsc(ValuesPair pair) {
            if(getBoolean(pair.left.isEqual(pair.right).getValue()))
                return 0;
            if(getBoolean(pair.left.isLowerThan(pair.right).getValue()))
                return -1;
            return 1;
        }

        public Integer visit(AscOrderC order, ValuesPair pair) {
            return compareAsc(pair);
        }

        public Integer visit(DescOrderC order, ValuesPair pair) {
            return -compareAsc(pair);
        }

        public Integer visit(NoOrderC order, ValuesPair pair) {
            return compareAsc(pair);
        }
    }

    public class NullsInterpreter implements Nulls.Visitor<Integer, ValuesPair> {
        private Integer nullsFirst(ValuesPair pair) {
            if(pair.left.getValue().isNull()) {
                if(pair.right.getValue().isNull())
                    return 0;
                return -1;
            }
            if(pair.right.getValue().isNull())
                return 1;
            return 0;
        }

        public Integer visit(NoNullsC nulls, ValuesPair pair) {
            return nullsFirst(pair);
        }

        public Integer visit(NullFirstsC nulls, ValuesPair pair) {
            return nullsFirst(pair);
        }

        public Integer visit(NullsLastC nulls, ValuesPair pair) {
            return -nullsFirst(pair);
        }
    }

    public class SelItemInterpreter implements SelItem.Visitor<QueryResult, Table> {
        public QueryResult visit(SelItemC selItem, Table table) {
            Environment env = new EnvironmentTable(table);
            Result result = selItem.condexpr_.accept(new CondExprInterpreter(), env);
            return new QueryResult(result.getValue());
        }

        public QueryResult visit(AliasedSelItemC selItem, Table table) {
            Environment env = new EnvironmentTable(table);
            Result result = selItem.condexpr_.accept(new CondExprInterpreter(), env);
            return new QueryResult(new Attribute(selItem.qident_), result.getValue());
        }
    }

    public class BoolExprInterpreter implements BoolExpr.Visitor<Result, Environment> {
        public Result visit(BoolExprCmpC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return expr.relop_.accept(new RelOpInterpreter(), new ValuesPair(left, right));
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BoolExprRegExpC expr, Environment env) {
            try {
                Result left = expr.basicexpr_.accept(new BasicExprInterpreter(), env);
                return (new ResultSingle(new ValueString(expr.string_))).regExpr(left);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BoolExprBasicExprC expr, Environment env) {
            return expr.basicexpr_.accept(new BasicExprInterpreter(), env);
        }
    }

    public class CondExprInterpreter implements CondExpr.Visitor<Result, Environment> {
        public Result visit(CondExprOrC expr, Environment env) {
            try {
                Result left = expr.condexpr_1.accept(new CondExprInterpreter(), env);
                Result right = expr.condexpr_2.accept(new CondExprInterpreter(), env);
                return left.or(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(CondExprAndC expr, Environment env) {
            // TODO
            throw new UnsupportedOperationException("CondExprAndC Not yet implemented");
        }

        public Result visit(CondExprNotC expr, Environment env) {
            try {
                return expr.condexpr_.accept(new CondExprInterpreter(), env).negate();
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(CondExprBoolExprC expr, Environment env) {
            return expr.boolexpr_.accept(new BoolExprInterpreter(), env);
        }
    }

    public class BasicExprInterpreter implements BasicExpr.Visitor<Result, Environment> {
        public Result visit(BasicExprAddC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return left.addValue(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BasicExprSubC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return left.subtract(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BasicExprMulC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return left.multiply(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BasicExprDivC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return left.divide(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BasicExprModC expr, Environment env) {
            try {
                Result left = expr.basicexpr_1.accept(new BasicExprInterpreter(), env);
                Result right = expr.basicexpr_2.accept(new BasicExprInterpreter(), env);
                return left.modulo(right);
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(BasicExprNegC expr, Environment env) {
            try {
                return expr.basicexpr_.accept(new BasicExprInterpreter(), env).negate();
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public Result visit(EIdentC expr, Environment env) {
            Result res = env.getIdent(expr.qident_);
            return res;
        }

        public Result visit(EFunC expr, Environment env) {
            try {
                List<Result> arguments = new ArrayList<Result>(expr.listcondexpr_.size());
                for(CondExpr arg : expr.listcondexpr_) {
                    arguments.add(arg.accept(new CondExprInterpreter(), env));
                }

                Result res = Functions.getInstance().evaluate(expr.qident_, arguments);
                return res;
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }

        public ResultSingle visit(EStrC expr, Environment env) {
            return new ResultSingle(new ValueString(expr.string_));
        }

        public ResultSingle visit(EBoolC expr, Environment env) {
            ValueBoolean value;
            if(expr.qbool_.compareTo("true") == 0)
                value = new ValueBoolean(true);
            else if(expr.qbool_.compareTo("false") == 0)
                value = new ValueBoolean(false);
            else
                throw new InternalInterpreterException("Incorrect boolean constant: " + PrettyPrinter.print(expr));
            return new ResultSingle(value);
        }

        public ResultSingle visit(EIntC expr, Environment env) {
            try {
                return new ResultSingle(new ValueInt(Long.parseLong(expr.qinteger_)));
            } catch(NumberFormatException exception) {
                throw new InternalInterpreterException(exception.getMessage());
            }
        }

        public ResultSingle visit(EDblC expr, Environment env) {
            try {
                return new ResultSingle(new ValueDouble(Double.parseDouble(expr.qdouble_)));
            } catch(NumberFormatException exception) {
                throw new InternalInterpreterException(exception.getMessage());
            }
        }

        public Result visit(ECondExprC expr, Environment env) {
            return expr.condexpr_.accept(new CondExprInterpreter(), env);
        }

        public ResultSingle visit(EStmtC expr, Environment env) {
            try {
                List<QueryResult> l = expr.statement_.accept(new StatementInterpreter(), zmi);
                if(l.size() != 1)
                    throw new IllegalArgumentException("Nested queries must SELECT exactly one item.");
                return new ResultSingle(l.get(0).getValue());
            } catch(Exception exception) {
                throw new InsideQueryException(PrettyPrinter.print(expr), exception);
            }
        }
    }

    public class RelOpInterpreter implements RelOp.Visitor<Result, ValuesPair> {
        public Result visit(RelOpGtC op, ValuesPair pair) {
            return pair.left.isLowerThan(pair.right).negate().and(pair.left.isEqual(pair.right).negate());
        }

        public Result visit(RelOpEqC op, ValuesPair pair) {
            // TODO
            throw new UnsupportedOperationException("RelOpEqC Not yet implemented");
        }

        public Result visit(RelOpNeC op, ValuesPair pair) {
            return pair.left.isEqual(pair.right).negate();
        }

        public Result visit(RelOpLtC op, ValuesPair pair) {
            return pair.left.isLowerThan(pair.right);
        }

        public Result visit(RelOpLeC op, ValuesPair pair) {
            // TODO
            throw new UnsupportedOperationException("RelOpLeC Not yet implemented");
        }

        public Result visit(RelOpGeC op, ValuesPair pair) {
            return pair.left.isLowerThan(pair.right).negate();
        }
    }
}