|
| 1 | +/* |
| 2 | + * Copyright 2023-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.vectorstore.s3; |
| 18 | + |
| 19 | +import java.time.ZoneOffset; |
| 20 | +import java.time.format.DateTimeFormatter; |
| 21 | +import java.util.Date; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +import org.jspecify.annotations.Nullable; |
| 27 | + |
| 28 | +import org.springframework.ai.vectorstore.filter.Filter; |
| 29 | + |
| 30 | +/** |
| 31 | + * Package-private helper used by {@link S3VectorStore} to evaluate a |
| 32 | + * {@link Filter.Expression} against metadata returned by the S3 Vectors ListVectors API. |
| 33 | + * |
| 34 | + * @author Jewoo Shin |
| 35 | + */ |
| 36 | +final class S3VectorStoreFilterExpressionEvaluator { |
| 37 | + |
| 38 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") |
| 39 | + .withZone(ZoneOffset.UTC); |
| 40 | + |
| 41 | + boolean evaluate(Filter.Expression expression, Map<String, Object> metadata) { |
| 42 | + return evaluateExpression(expression, metadata); |
| 43 | + } |
| 44 | + |
| 45 | + private boolean evaluateOperand(Filter.Operand operand, Map<String, Object> metadata) { |
| 46 | + if (operand instanceof Filter.Group group) { |
| 47 | + return evaluateOperand(group.content(), metadata); |
| 48 | + } |
| 49 | + if (operand instanceof Filter.Expression expression) { |
| 50 | + return evaluateExpression(expression, metadata); |
| 51 | + } |
| 52 | + throw new IllegalArgumentException("Unsupported operand type: " + operand.getClass().getName()); |
| 53 | + } |
| 54 | + |
| 55 | + private boolean evaluateExpression(Filter.Expression expression, Map<String, Object> metadata) { |
| 56 | + return switch (expression.type()) { |
| 57 | + case AND -> evaluateOperand(left(expression), metadata) && evaluateOperand(right(expression), metadata); |
| 58 | + case OR -> evaluateOperand(left(expression), metadata) || evaluateOperand(right(expression), metadata); |
| 59 | + case NOT -> !evaluateOperand(left(expression), metadata); |
| 60 | + case EQ -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) == 0; |
| 61 | + case NE -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) != 0; |
| 62 | + case GT -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) > 0; |
| 63 | + case GTE -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) >= 0; |
| 64 | + case LT -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) < 0; |
| 65 | + case LTE -> compare(metadataValue(left(expression), metadata), filterValue(right(expression))) <= 0; |
| 66 | + case IN -> { |
| 67 | + Object metaVal = metadataValue(left(expression), metadata); |
| 68 | + List<?> list = asList(filterValue(right(expression)), expression); |
| 69 | + yield list.stream().anyMatch(item -> compare(metaVal, item) == 0); |
| 70 | + } |
| 71 | + case NIN -> { |
| 72 | + Object metaVal = metadataValue(left(expression), metadata); |
| 73 | + List<?> list = asList(filterValue(right(expression)), expression); |
| 74 | + yield list.stream().noneMatch(item -> compare(metaVal, item) == 0); |
| 75 | + } |
| 76 | + case ISNULL -> metadataValue(left(expression), metadata) == null; |
| 77 | + case ISNOTNULL -> metadataValue(left(expression), metadata) != null; |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private Filter.Operand left(Filter.Expression expression) { |
| 82 | + Filter.Operand left = expression.left(); |
| 83 | + if (left == null) { |
| 84 | + throw new IllegalArgumentException( |
| 85 | + "Expression of type %s requires a left operand".formatted(expression.type())); |
| 86 | + } |
| 87 | + return left; |
| 88 | + } |
| 89 | + |
| 90 | + private Filter.Operand right(Filter.Expression expression) { |
| 91 | + Filter.Operand right = expression.right(); |
| 92 | + if (right == null) { |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "Expression of type %s requires a right operand".formatted(expression.type())); |
| 95 | + } |
| 96 | + return right; |
| 97 | + } |
| 98 | + |
| 99 | + private @Nullable Object metadataValue(Filter.Operand operand, Map<String, Object> metadata) { |
| 100 | + if (operand instanceof Filter.Key key) { |
| 101 | + String k = key.key(); |
| 102 | + if (k.length() >= 2 |
| 103 | + && ((k.startsWith("\"") && k.endsWith("\"")) || (k.startsWith("'") && k.endsWith("'")))) { |
| 104 | + k = k.substring(1, k.length() - 1); |
| 105 | + } |
| 106 | + return metadata.get(k); |
| 107 | + } |
| 108 | + throw new IllegalArgumentException("Expected a Key operand but got: " + operand.getClass().getName()); |
| 109 | + } |
| 110 | + |
| 111 | + private Object filterValue(Filter.Operand operand) { |
| 112 | + if (operand instanceof Filter.Value filterValue) { |
| 113 | + Object value = filterValue.value(); |
| 114 | + return (value instanceof Date date) ? DATE_FORMATTER.format(date.toInstant()) : value; |
| 115 | + } |
| 116 | + throw new IllegalArgumentException("Expected a Value operand but got: " + operand.getClass().getName()); |
| 117 | + } |
| 118 | + |
| 119 | + @SuppressWarnings("unchecked") |
| 120 | + private int compare(@Nullable Object metaVal, @Nullable Object filterVal) { |
| 121 | + if (metaVal == null && filterVal == null) { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + if (metaVal == null) { |
| 125 | + return -1; |
| 126 | + } |
| 127 | + if (filterVal == null) { |
| 128 | + return 1; |
| 129 | + } |
| 130 | + if (metaVal instanceof Number n1 && filterVal instanceof Number n2) { |
| 131 | + return Double.compare(n1.doubleValue(), n2.doubleValue()); |
| 132 | + } |
| 133 | + if (Objects.equals(metaVal, filterVal)) { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + if (metaVal instanceof Comparable comparable && filterVal instanceof Comparable) { |
| 137 | + try { |
| 138 | + return comparable.compareTo(filterVal); |
| 139 | + } |
| 140 | + catch (ClassCastException ex) { |
| 141 | + throw new IllegalArgumentException("Cannot compare values of incompatible types %s and %s" |
| 142 | + .formatted(metaVal.getClass().getName(), filterVal.getClass().getName()), ex); |
| 143 | + } |
| 144 | + } |
| 145 | + throw new IllegalArgumentException("Cannot compare values of types %s and %s" |
| 146 | + .formatted(metaVal.getClass().getName(), filterVal.getClass().getName())); |
| 147 | + } |
| 148 | + |
| 149 | + private List<?> asList(Object value, Filter.Expression expression) { |
| 150 | + if (value instanceof List<?> list) { |
| 151 | + return list; |
| 152 | + } |
| 153 | + throw new IllegalArgumentException( |
| 154 | + "Expected a List value for %s expression but got: %s".formatted(expression.type(), value)); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments