postgres.ts 30.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blend.
 *
 * blend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * blend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blend.  If not, see <http://www.gnu.org/licenses/>.
 */

import { Adapter } from "../core/adapter";
22
23
import { Metric } from "../core/metric";
import { Dimension } from "../core/dimension";
24
25
import { Clause } from "../core/clause";
import { Filter, FilterOperator } from "../core/filter";
26
import { AggregationType, RelationType } from "../common/types";
27
import { View } from "../core/view";
28
import { Pool, PoolConfig } from "pg";
29

30
31
32
33
34
35
36
37
38
39
40
interface ExpandedView {
    dimensions: Dimension[];
    metrics: Metric[];
    dimMetrics: Metric[];
    keys: Dimension[];
    clauses: Clause[];
    from: string;
    id: string;
    origin: boolean;
}

41
42
interface DimInfo {
    dim: Dimension;
43
44
45
46
47
48
    views: ExpandedView[];
}

interface DimTranslation {
    aliased: string;
    noalias: string;
49
}
50

51
52
53
54
55
56
57
58
export class PostgresAdapter extends Adapter {
    private pool: Pool;

    constructor (config: PoolConfig) {
        super();
        this.pool = new Pool(config);
    }
    public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
59
        const query = this.getQueryFromView(view);
60

61
62
63
        this.pool.connect((err, client, done) => {
            if (err) {
                cb (err);
64
                return;
65
66
67
68
69
70
71
            }
            client.query(query, [], (error, result) => {
                // call 'done()' to release client back to pool
                done();
                cb(error, (result) ? result.rows : null);
            });
        });
72
73
    }

74
75
    public materializeView(view: View): boolean {
        return false;
76
77
    }

78
    public getQueryFromView(view: View): string {
79
80
81
82
        /*
            Find the base (materialized) views that has this data and exapand
            than (make a parse to the format used in the adapter)
        */
83
84
        const materialized = this.searchMaterializedViews(view).sort((a, b) => {
            return (a.id < b.id) ? -1 : 1;
85
86
87
88
89
90
91
92
93
94
95
96
97
        }).map((item) => {
            return {
                id: item.id,
                from: "view_" + item.id,
                dimMetrics: [],
                metrics: item.metrics.filter((i) => {
                    return view.metrics.some((j) => i.name === j.name);
                }),
                dimensions: item.dimensions,
                keys: item.keys,
                clauses: item.clauses,
                origin: item.origin
            };
98
99
        });

100
101
        // Remove repeated views from the result
        let partialJoin = [materialized[0]];
102
103
        for (let i = 1; i < materialized.length; ++i) {
            if (materialized[i - 1].id !== materialized[i].id) {
104
                partialJoin.push(materialized[i]);
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
        /*
            If there is more than one source of data (tables/views)
            a join is needed.

            Partial Join represents how many sources still exists,
            every join reduces this number.
        */
        while (partialJoin.length > 1) {
            /*
                Variable map finds what dimenensions are still needed to
                complete this query, they are required for 2 reasons.
                1 - To make joins
                2 - Because they are in the query

                For each view that has this dimension we add one score to
                this dimension, if they are in the query the same.

                Automatically if the dimension is in the query there will be
                at least one view with this atribute (or the query could not be
                completed) so dimensions in the query always have score of
                at least 2.

                To make a join the dimension must be in 2 different views,
                creating a score of 2 either.

                If the score is less than 2 so this dimension is not required
                anymore and can be removed.
            */
            let map: { [key: string]: number } = {};
            let partialsChange = false;
            for (let i = 0; i < partialJoin.length; ++i) {
                const dims = partialJoin[i].dimensions;
                for (let k = 0; k < dims.length; ++k) {
                    if (!map[dims[k].name])  {
                        map[dims[k].name] = 1;
                    }

                    else {
                        ++map[dims[k].name];
                    }
                }
            }

            for (let i = 0; i < view.dimensions.length; ++i) {
                let dim = view.dimensions[i];
                while (dim !== null) {
                    if (map[dim.name])  {
                        ++map[dim.name];
                    }
                    dim = dim.parent;
                }
            }

            for (let i = 0; i < partialJoin.length; ++i) {
                const dims = partialJoin[i].dimensions.filter((item) => {
                    return map[item.name] > 1;
                });
                const keys = partialJoin[i].keys.filter((item) => {
                    return map[item.name] > 1;
                });
                /*
                    At this point the dimensions with less than score 2
                    are removed, if this happens the view is agreggated
                    again, with less dimensions, removing this dimension
                    from the view.
                */
                if (dims.length  < partialJoin[i].dimensions.length) {
                    const partial = new View({
                        metrics: partialJoin[i].metrics,
                        dimensions: dims,
                        keys: keys,
                        origin: false,
                        clauses: partialJoin[i].clauses,
                        materialized: false
                    });
                    const from  = "(" +
                                   this.buildQuery(partial, [partialJoin[i]]) +
                                   ") AS view_" + partial.id + "\n";

                    partialJoin[i].id = partial.id;
                    partialJoin[i].dimensions = partial.dimensions;
                    partialJoin[i].keys = partial.keys;
                    partialJoin[i].origin = partial.origin;
                    partialJoin[i].from = from;

                    partialsChange = true;
                }
            }
            /*
                If at least one of the views changed (have the number of
                dimensions reduced) returns to the begining of the loop
                again.

                Othewise we need to make a join.
            */
            if (!partialsChange) {
                /*
                    Sorting the views by keys.
                    If the keys are identical, then they
                    will be in sequence, and views with identical
                    keys can be joined.

                    Sort an array of keys is the same as sort a
                    array of strings.
                */
                const sorted = partialJoin.sort((a, b) => {
                    return this.compareKeys(a.keys, b.keys);
                });
                /*
                    First of all, the remaining views are splited in segments.

                    A segment contains views with the same keys that are great
                    to make joins. Joins like this do not create "dimensional
                    metrics".

                    In joins like this one row of each view will  be connected
                    with at most one row of each other table.
                */
                const segment = [[sorted[0]]];
                let segmentId = 0;
                for (let i = 1; i < sorted.length; ++i) {
                    if (this.compareKeys(sorted[i - 1].keys, sorted[i].keys) === 0) {
                        segment[segmentId].push(sorted[i]);
                    }
                    else {
                        ++segmentId;
                        segment.push([sorted[i]]);
                    }
                }

                partialJoin = [];
                let ableToJoin = false;

                for (let i = 0; i < segment.length; ++i) {
                    /*
                        If a segment has more than one view, a join can be made
                    */
                    if (segment[i].length > 1) {
                        let mets: Metric[] = [];
                        let clauses: Clause[] = [];
                        let dims: Dimension[] = [];
                        let dimMetrics: Metric[] = [];
                        for (let j = 0; j < segment[i].length; ++j) {
                            mets = mets.concat(segment[i][j].metrics);
                            clauses = clauses.concat(segment[i][j].clauses);
                            dims = dims.concat(segment[i][j].dimensions);
                            dimMetrics = dimMetrics.concat(segment[i][j].dimMetrics);
                        }

                        dims = this.removeDuplicatedDimensions(dims);
                        /*
                            Its atributes are just concatenated and the
                            duplicates removed.
                        */

                        const partial = new View({
                            metrics: mets,
                            dimensions: dims,
                            keys: segment[i][0].keys,
                            origin: false,
                            clauses: clauses,
                            materialized: false
                        });
                        const viewFrom  = "(" +
                                    this.buildQuery(partial, segment[i]) +
                                    ") AS view_" + partial.id + "\n";

                        partialJoin.push({
                            id: partial.id,
                            from: viewFrom,
                            dimMetrics: dimMetrics,
                            metrics: partial.metrics,
                            dimensions: partial.dimensions,
                            keys: partial.keys,
                            clauses: partial.clauses,
                            origin: partial.origin
                        });

                        ableToJoin = true;
                    }

                    else {
                        /*
                            If the segment has just one view, anything can be
                            done at this point, so just reinsert this view in
                            set of views.
                        */
                        partialJoin.push(segment[i][0]);
                    }
                }

                /*
                    If at least one join was made in the last part (a segment
                    with more than one view) than return to the begining of the
                    loop.

                    This permits after a join remove the dimensions that were
                    only choosen to this join, and are no longer required

                    Ideally the joins should be restrict the join method used
                    above, but in some cases this can not be done.

                    So if all the segments have only one view inside, move
                    to the next method.
                */
                if (!ableToJoin) {
                    /*
                        At this point 2 views will be joined, first the
                        similarity with each pair of views is calculated,
                        the pair with the biggedt similarity will be joined.

                        Similarity is calculated with the number of common
                        dimensions in the keys.
                    */
                    let similarity = 0;
                    let idx0 = 0;
                    let idx1 = 1;
                    for (let i = 0; i < partialJoin.length; ++i) {
                        for (let j = i + 1 ; j < partialJoin.length; ++j) {
                            const pi = partialJoin[i].keys;
                            const pj = partialJoin[j].keys;
                            let score = this.similarDimensions (pi, pj);
                            if (similarity < score) {
                                similarity = score;
                                idx0 = i;
                                idx1 = j;
                            }
                        }
                    }

                    const partial0 = partialJoin[idx0];
                    const partial1 = partialJoin[idx1];

                    partialJoin.splice(idx1, 1);
                    partialJoin.splice(idx0, 1);

                    /*
                        Once the views are select they are joined with the
                        same method, concatenedted its atributes and
                        removing duplicates, however the nasty effect of
                        this join is the creation of "dimensional metrics".

                        "Dimensional metrics" are metrics that can no longer
                        be aggregated, and at this point to the end
                        of a query they will act as dimensions.

                        This change happens to avoid inconsistency generated
                        by a join where one row of one table can be connected
                        to more than one of other table.

                        Take this example.

                        View0 : metrics [met0], dimensions [dim0]
                        values: [{met0: 10, dim0: 1}]
                        View1 : metrics [met1], dimensions [dim2]
                        values: [{met1: 10, dim2: 1}. {met1: 5, dim2: 2}]
                        View2 : metrics [], dimensions [dim0, dim1, dim2]
                        values: [
                            {dim0: 1, dim1: 1, dim2: 1},
                            {dim0: 1, dim1: 1, dim2: 2}
                        ]
                        The query is metrics [met0, met1] and dimensions [dim1]
                        First a join of View0 and View1 is made, the result
                        is: [
                            {dim0: 1, dim1: 1, dim2: 1, met0: 10},
                            {dim0: 1, dim1: 1, dim2: 2, met0: 10}
                        ]
                        Note that the value of met0 is duplicated.
                        Now dim0 is removed, than joined with view2 resulting
                        in: [
                            {met1: 10, dim1: 1, dim2: 1, met0: 10},
                            {met1: 5 , dim1: 1, dim2: 2, met0: 10}
                        ]

                        Lets assume that the agregation is SUM
                        If we remove dim2 and re-agregate the result is: [
                            {met1: 15, dim1: 1, met0: 20}
                        ]

                        This result is wrong. The replication of the value
                        met0 affects the result.

                        See if met1 was not required, first the dimemnsion would
                        be reduced, left dim0 and dim1, than joined that reduced
                        again resulting in the value [
                            {dim1:1, met0: 10}
                        ]

                        Is this case there is no duplication and the aggregation
                        does not include more rows than should.

                        To solve this problem the met0 must become a dimension,
                        in other words, not aggregated again. If the met0 was
                        not agregated in the query met0, met1, dim1 the result
                        is: [
                            {met1: 15, dim1: 1, met0: 10}
                        ]
                        what is compatible.

                        After this extreme long explanation what must be
                        known is: Joining views with diferent keys
                        generate "dimensional metrics".

                        Views with "dimensional metrics" can not used for future
                        queries because can not be re-agregated, so this must be
                        avoided and is one-query only views.
                    */

                    let dimMetrics: Metric[];
                    let mets: Metric[];
                    let dims = partial0.dimensions.concat(partial1.dimensions);
                    dims = this.removeDuplicatedDimensions(dims);
                    let keys = partial0.keys.concat(partial1.keys);
                    keys = this.removeDuplicatedDimensions(keys);
                    if (partial0.keys.length === similarity) {
                        /*
                            Here the metrics become dimensions, but the effect
                            can be reduced. If the keys of partial0
                            is a sub set of the keys ou partial1
                            than the number of rows of partial 1 is not
                            affected, in other words the metrics of partial1
                            can be aggregated and does not need to become
                            dimensions.
                        */
                        partial0.dimMetrics = partial0.dimMetrics.concat(partial0.metrics);
                        partial0.metrics = [];
                        mets = partial1.metrics;
                    }

                    else if (partial1.keys.length === similarity) {
                        /*
                            The same occurs if the keys of partia1 is a subset
                            of partial0.
                        */
                        partial1.dimMetrics = partial1.dimMetrics.concat(partial1.metrics);
                        partial1.metrics = [];
                        mets = partial0.metrics;
                    }

                    else {
                        /*
                            But if there is no sub set, than both sides have
                            the metrics turned in dimensions.
                        */
                        partial0.dimMetrics = partial0.dimMetrics.concat(partial0.metrics);
                        partial0.metrics = [];
                        partial1.dimMetrics = partial1.dimMetrics.concat(partial1.metrics);
                        partial1.metrics = [];
                        mets = [];
                    }

                    dimMetrics = partial0.dimMetrics.concat(partial1.dimMetrics);
                    const partial = new View({
                        metrics: mets,
                        dimensions: dims,
                        keys: keys,
                        origin: false,
                        clauses: partial0.clauses.concat(partial1.clauses),
                        materialized: false
                    });
468
469
470
471
472
473
474
475
                    const id = new View({
                        metrics: mets.concat(dimMetrics),
                        dimensions: dims,
                        keys: keys,
                        origin: false,
                        clauses: partial0.clauses.concat(partial1.clauses),
                        materialized: false
                    }).id;
476
477
                    const viewFrom  = "(" +
                            this.buildQuery(partial, [partial0, partial1]) +
478
                            ") AS view_" + id + "\n";
479
                    partialJoin.push({
480
                        id: id,
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
                        from: viewFrom,
                        dimMetrics: dimMetrics,
                        metrics: mets,
                        dimensions: dims,
                        keys: keys,
                        clauses: partial.clauses,
                        origin: false
                    });

                }
            }
        }

        /*
            When only one view remain, the query is made and a ;
            is added at the end.

            TODO: Probrably this last line adds one more
            layer to the query, that is in fact unnecessary.
            Think a way to remove-it.
        */
        return this.buildQuery(view, partialJoin) + ";";
503
504
    }

505
506
507
508
509
510
511
    private searchMaterializedViews(view: View): View[] {
        let r: View[] = [];
        if (view.materialized) {
            return [view];
        }

        else {
512
            let children = view.childViews;
513
514
515
516
517
518
519
520
            for (let i = 0; i < children.length; ++i) {
                r = r.concat(this.searchMaterializedViews(children[i]));
            }
        }

        return r;
    }

521
    private buildQuery(target: View, views: ExpandedView[]) {
522
523
        const metrics = target.metrics;
        const dimensions = target.dimensions;
524
        const clauses = target.clauses;
525
526

        let dimMap: {[key: string]: DimInfo} = {};
527
        let nameMap: {[key: string]: ExpandedView} = {};
528
529
530
531
532

        for (let i = 0; i < views.length; ++i) {
            const mets = views[i].metrics;
            const dims = views[i].dimensions;
            for (let j = 0; j < mets.length; ++j) {
533
                if (!nameMap[mets[j].name])  {
534
                    nameMap[mets[j].name] = views[i];
535
536
537
538
539
540
541
542
543
544
                }

            }

            for (let j = 0; j < dims.length; ++j) {
                if (!dimMap[dims[j].name])  {
                    dimMap[dims[j].name] = {
                        dim: dims[j],
                        views: [views[i]]
                    };
545
                    nameMap[dims[j].name] = views[i];
546
547
548
549
550
551
552
553
                }

                else {
                    dimMap[dims[j].name].views.push(views[i]);
                }
            }
        }

554
        // Projection
555
        const strMetrics = metrics.map((metric) => {
556
            const view = nameMap[metric.name];
557
558
559
560
561
562
            if (view) {
                return this.translateMetric(metric, view);
            }

            return "";
        }).filter((item) => item !== "");
563
564
565

        const parsedDimensions = dimensions.map((dimension) => {
            let dim = dimension;
566
            while (!nameMap[dim.name]) {
567
568
                dim = dim.parent;
            }
569
            const view = nameMap[dim.name];
570
            return this.translateDimension(dimension, dim, view);
571
572
        });

573
574
575
576
577
578
579
580
581
582
583
584
585
586
        let parsedDimMetrics: DimTranslation[] = [];

        for (let i = 0; i < views.length; ++i) {
            const dimMets = views[i].dimMetrics.map((item) => {
                return this.translateDimMetric(item, views[i]);
            });

            parsedDimMetrics = parsedDimMetrics.concat(dimMets);
        }

        const totalDimensions = parsedDimensions.concat(parsedDimMetrics);

        const strDimensions = totalDimensions.map ((item) => item.aliased);
        const grouped = totalDimensions.map((item) => item.noalias);
587
588
        const elements = strMetrics.concat(strDimensions);

589
590
        // Joins
        let conds: string[] = [];
591
592
593
594
595
596
597
598
        for (let i in dimMap) {
            let remainViews = dimMap[i].views.slice();
            let dim = dimMap[i].dim;
            let leftSide = this.buildColumn(dim, remainViews.shift().id);
            if (remainViews.length > 0) {
                while (remainViews.length > 0) {
                    const id = remainViews.shift().id;
                    const rightSide = this.buildColumn(dim, id);
599
                    conds.push(leftSide + " = " + rightSide);
600
601
602
603
604
                }
            }

        }

605
606
607
608
609
610
611
612
613
614
615
        // Selection
        let covered: Clause[] = [];
        for (let i = 0; i < views.length; ++i) {
            // Get the clauses that children already cover
            covered = covered.concat(views[i].clauses);
        }

        const toCover = clauses.filter((item) => !covered.some ((clause) => {
            return clause.id === item.id;
        }));

616
617
618
619
620
621
        toCover.forEach((item) => {
            const clause = "(" + this.translateClause(item, nameMap) + ")";
            if (clause !== "()") {
                conds.push(clause);
            }
        });
622
623
624

        // Assembly

625
        const projection = "SELECT " + elements.join(",");
626
        const source = " FROM " + views.map((view) => view.from).join(",");
627
        const selection = (conds.length > 0) ? " WHERE " + conds.join(" AND ") : "";
628
629
630
631
632
        let grouping = "";
        if (grouped.length > 0) {
            grouping = " GROUP BY " + grouped.join(",");
        }

633
        return projection + source + selection + grouping;
634
635
636

    }

637
638
639
640
641
642
643
644
    private getAggregateFunction(aggrType: AggregationType, origin: boolean): string {
        switch (aggrType) {
            case AggregationType.SUM:
                return "SUM";
            case AggregationType.AVG:
                return "AVG";
            case AggregationType.COUNT:
                return (origin) ? "COUNT" : "SUM";
645
646
647
648
            case AggregationType.MAX:
                return "MAX";
            case AggregationType.MIN:
                return "MIN";
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
            default:
                return  "";
        }

    }

    private translateRelation(relation: RelationType, arg: string): string {
        switch (relation) {
            case RelationType.DAY:
                return this.applyRelation("EXTRACT", ["DAY FROM "], [arg]);
            case RelationType.MONTH:
                return this.applyRelation("EXTRACT", ["MONTH FROM "], [arg]);
            case RelationType.YEAR:
                return this.applyRelation("EXTRACT", ["YEAR FROM "], [arg]);
            case RelationType.DAYOFWEEK:
                return this.applyRelation("EXTRACT", ["DOW FROM "], [arg]);
            default:
                return "";
        }

    }

    private applyRelation(name: string, args: string[], values: string[]): string {
        /*
            This adapter uses the concept of functions in Postgres to
            implement BLENDB sub-dimention relations, this functions
            applys the transformation to build the call of a Postgres
            funtion. Note that this function can be native from postgres,
            like EXTRACT, or even implemented on the database.
            This function is short and only used in the translateRelation
            method however is a bit complex and is possible to be used
            several times, because of that is puted appart to make easyer update
            and avoid problems
            Example
            applyRelation ("EXTRACT", "["DAY FROM"]", ["view_0.date"])
            output: EXTRACT(DAY FROM view_0.date)
        */
        return name + "(" + args.map((item, idx) => item + values[idx]).join(",") + ")";
    }

689
690
691
692
    private buildColumn (item: Metric|Dimension, id: string): string {
        const quotedName = "\"" + item.name + "\"";
        return "view_" + id + "." + quotedName;
    }
693

694
    private translateClause(clause: Clause, map: {[key: string]: ExpandedView}): string {
695
696
697
698
699
        const r = clause.filters.map((item) => {
            return this.translateFilter(item, map);
        }).filter((item) => {
            return item !== "";
        });
700
701
702
        return r.join(" OR ");
    }

703
    private translateFilter(filter: Filter, map: {[key: string]: ExpandedView}): string {
704
705
706
707
        if (!map[filter.target.name]) {
            return "";
        }

708
709
710
711
712
713
714
715
        const viewId = map[filter.target.name].id;
        const leftSide = this.buildColumn(filter.target, viewId);
        const op = this.translateOperator(filter.operator);
        const dataType = this.translateDataType(filter.target.dataType);
        const quotedValue = "'" + filter.value + "'";
        return leftSide + op + quotedValue + dataType;
    }

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
    private translateMetric(metric: Metric, view: ExpandedView): string {
        const func = this.getAggregateFunction(metric.aggregation, view.origin);
        const quotedName = "\"" + metric.name + "\"";
        const extMetric = func + "(" + this.buildColumn(metric, view.id) + ")";
        return extMetric + " AS " + quotedName;
    }

    private translateDimMetric(metric: Metric, view: ExpandedView): DimTranslation {
        const quotedName = "\"" + metric.name + "\"";
        const extMetric = this.buildColumn(metric, view.id);
        return { aliased: extMetric + " AS " + quotedName, noalias: extMetric };
    }

    private translateDimension(dimension: Dimension,
                               ancestor: Dimension,
                               view: ExpandedView): DimTranslation {
        const quotedName = "\"" + dimension.name + "\"";
        let extDimension = this.buildColumn(ancestor, view.id);
        let aux = dimension;
        while (aux.name !== ancestor.name) {
            extDimension = this.translateRelation(aux.relation, extDimension);
            aux = aux.parent;
        }
        return { aliased: extDimension + " AS " + quotedName, noalias: extDimension };
    }

742
743
744
745
746
747
    private translateOperator(op: FilterOperator): string {
        switch (op) {
            case FilterOperator.EQUAL:
                return " = ";
            case FilterOperator.NOTEQUAL:
                return " != ";
748
749
750
751
752
753
754
755
            case FilterOperator.GREATER:
                return " > ";
            case FilterOperator.LOWER:
                return " < ";
            case FilterOperator.GREATEREQ:
                return " >= ";
            case FilterOperator.LOWEREQ:
                return " <= ";
756
757
758
759
760
761
762
763
764
765
766
767
            default:
                return  "";
        }

    }

    private translateDataType(dt: string ): string {
        switch (dt) {
            case "date":
                return "::DATE";
            case "integer":
                return "::INTEGER";
768
769
            case "boolean":
                return "::BOOLEAN";
770
771
772
773
            default:
                return "";
        }
    }
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

    private compareKeys(a: Dimension[], b: Dimension[]): number {
        let length = 0;
        let res = a.length - b.length;
        if (a.length < b.length) {
            length = a.length;
        }
        else {
            length = b.length;
        }

        for (let i = 0; i < length; ++i) {
            if (a[i].name < b[i].name) {
                return -1;
            }

            else if (a[i].name > b[i].name) {
                return 1;
            }
        }

        return res;
    }

    private similarDimensions(a: Dimension[], b: Dimension[]): number {
        let count = 0;
        for (let i = 0; i < a.length; ++i) {
            if (b.some((itemB) => a[i].name === itemB.name)) {
                count++;
            }
        }
        return count;
    }

    private removeDuplicatedDimensions(candidateDims: Dimension[]): Dimension[] {
        let filterDims: { [key: string]: boolean } = {};
        const dims = [];
        for (let i = 0;  i < candidateDims.length; ++i) {
            if (!filterDims[candidateDims[i].name]) {
                dims.push(candidateDims[i]);
                filterDims[candidateDims[i].name] = true;
            }
        }
        return dims;
    }
819
}