|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Cypher from "@neo4j/cypher-builder"; |
| 21 | +import type { AttributeAdapter } from "../../../../../schema-model/attribute/model-adapters/AttributeAdapter"; |
| 22 | +import { filterFields, renameFields } from "../../../../../utils/utils"; |
| 23 | +import type { QueryASTNode } from "../../QueryASTNode"; |
| 24 | +import { AggregationField } from "./AggregationField"; |
| 25 | + |
| 26 | +export class DeprecatedAggregationAttributeField extends AggregationField { |
| 27 | + private attribute: AttributeAdapter; |
| 28 | + private aggregationProjection: Record<string, string>; |
| 29 | + |
| 30 | + constructor({ |
| 31 | + alias, |
| 32 | + attribute, |
| 33 | + aggregationProjection, |
| 34 | + }: { |
| 35 | + alias: string; |
| 36 | + attribute: AttributeAdapter; |
| 37 | + aggregationProjection: Record<string, string>; |
| 38 | + }) { |
| 39 | + super(alias); |
| 40 | + this.attribute = attribute; |
| 41 | + this.aggregationProjection = aggregationProjection; |
| 42 | + } |
| 43 | + |
| 44 | + public getChildren(): QueryASTNode[] { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + public getProjectionField(variable: Cypher.Variable): Record<string, Cypher.Expr> { |
| 49 | + return { [this.alias]: variable }; |
| 50 | + } |
| 51 | + |
| 52 | + public getAggregationExpr(target: Cypher.Variable): Cypher.Expr { |
| 53 | + const variable = target.property(this.attribute.databaseName); |
| 54 | + return this.createAggregationExpr(variable); |
| 55 | + } |
| 56 | + |
| 57 | + public getAggregationProjection(target: Cypher.Variable, returnVar: Cypher.Variable): Cypher.Clause { |
| 58 | + if (this.attribute.typeHelper.isString()) { |
| 59 | + const aggrProp = target.property(this.attribute.databaseName); |
| 60 | + const listVar = new Cypher.NamedVariable("list"); |
| 61 | + |
| 62 | + const projection = new Cypher.Return([this.createAggregationExpr(listVar), returnVar]); |
| 63 | + |
| 64 | + return new Cypher.With(target) |
| 65 | + .orderBy([Cypher.size(aggrProp), "DESC"]) |
| 66 | + .with([Cypher.collect(aggrProp), listVar]) |
| 67 | + .return(projection); |
| 68 | + } |
| 69 | + |
| 70 | + return new Cypher.With(target).return([this.getAggregationExpr(target), returnVar]); |
| 71 | + } |
| 72 | + |
| 73 | + private createAggregationExpr(target: Cypher.Variable | Cypher.Property): Cypher.Expr { |
| 74 | + if (this.attribute.typeHelper.isString()) { |
| 75 | + const listVar = new Cypher.NamedVariable("list"); |
| 76 | + return new Cypher.Map( |
| 77 | + this.filterProjection({ |
| 78 | + longest: Cypher.head(listVar), |
| 79 | + shortest: Cypher.last(listVar), |
| 80 | + }) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + // NOTE: These are types that are treated as numeric by aggregation |
| 85 | + if (this.attribute.typeHelper.isNumeric()) { |
| 86 | + return new Cypher.Map( |
| 87 | + this.filterProjection({ |
| 88 | + min: Cypher.min(target), |
| 89 | + max: Cypher.max(target), |
| 90 | + average: Cypher.avg(target), |
| 91 | + sum: Cypher.sum(target), |
| 92 | + }) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + if (this.attribute.typeHelper.isDateTime()) { |
| 97 | + return new Cypher.Map( |
| 98 | + this.filterProjection({ |
| 99 | + min: this.createDatetimeProjection(Cypher.min(target)), |
| 100 | + max: this.createDatetimeProjection(Cypher.max(target)), |
| 101 | + }) |
| 102 | + ); |
| 103 | + } |
| 104 | + if (this.attribute.typeHelper.isTemporal()) { |
| 105 | + return new Cypher.Map( |
| 106 | + this.filterProjection({ |
| 107 | + min: Cypher.min(target), |
| 108 | + max: Cypher.max(target), |
| 109 | + }) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + if (this.attribute.typeHelper.isID()) { |
| 114 | + return new Cypher.Map( |
| 115 | + this.filterProjection({ |
| 116 | + shortest: Cypher.min(target), |
| 117 | + longest: Cypher.max(target), |
| 118 | + }) |
| 119 | + ); |
| 120 | + } |
| 121 | + throw new Error(`Invalid aggregation type ${this.attribute.type.name}`); |
| 122 | + } |
| 123 | + |
| 124 | + // Filters and apply aliases in the projection |
| 125 | + private filterProjection(projectionFields: Record<string, Cypher.Expr>): Record<string, Cypher.Expr> { |
| 126 | + const filteredFields = filterFields(projectionFields, Object.keys(this.aggregationProjection)); |
| 127 | + return renameFields(filteredFields, this.aggregationProjection); |
| 128 | + } |
| 129 | + |
| 130 | + private createDatetimeProjection(expr: Cypher.Expr) { |
| 131 | + return Cypher.apoc.date.convertFormat(expr, "iso_zoned_date_time", "iso_offset_date_time"); |
| 132 | + } |
| 133 | +} |
0 commit comments