|
1 | 1 | package org.hypertrace.core.documentstore.postgres; |
2 | 2 |
|
3 | | -import static org.hypertrace.core.documentstore.Collection.UNSUPPORTED_QUERY_OPERATION; |
4 | | -import static org.hypertrace.core.documentstore.postgres.PostgresCollection.CREATED_AT; |
5 | | -import static org.hypertrace.core.documentstore.postgres.PostgresCollection.ID; |
6 | | -import static org.hypertrace.core.documentstore.postgres.PostgresCollection.UPDATED_AT; |
7 | | - |
8 | 3 | import java.util.Arrays; |
9 | | -import java.util.HashSet; |
10 | 4 | import java.util.List; |
11 | | -import java.util.Set; |
| 5 | +import java.util.Optional; |
12 | 6 | import java.util.stream.Collectors; |
13 | 7 | import org.apache.commons.lang3.StringUtils; |
14 | 8 | import org.hypertrace.core.documentstore.Filter; |
|
18 | 12 |
|
19 | 13 | class PostgresQueryParser { |
20 | 14 |
|
21 | | - private static final String QUESTION_MARK = "?"; |
22 | | - // postgres jsonb uses `->` instead of `.` for json field access |
23 | | - private static final String JSON_FIELD_ACCESSOR = "->"; |
24 | | - // postgres operator to fetch the value of json object as text. |
25 | | - private static final String JSON_DATA_ACCESSOR = "->>"; |
26 | | - private static final Set<String> OUTER_COLUMNS = |
27 | | - new HashSet<>() { |
28 | | - { |
29 | | - add(CREATED_AT); |
30 | | - add(ID); |
31 | | - add(UPDATED_AT); |
32 | | - } |
33 | | - }; |
| 15 | + static String parseSelections(List<String> selections) { |
| 16 | + return Optional.of( |
| 17 | + selections.stream() |
| 18 | + .map( |
| 19 | + selection -> |
| 20 | + String.format( |
| 21 | + "%s AS \"%s\"", |
| 22 | + PostgresUtils.prepareFieldAccessorExpr( |
| 23 | + selection, PostgresUtils.DOCUMENT_COLUMN), |
| 24 | + selection)) |
| 25 | + .collect(Collectors.joining(","))) |
| 26 | + .filter(str -> StringUtils.isNotBlank(str)) |
| 27 | + .orElse("*"); |
| 28 | + } |
34 | 29 |
|
35 | 30 | static String parseFilter(Filter filter, Builder paramsBuilder) { |
36 | 31 | if (filter.isComposite()) { |
37 | 32 | return parseCompositeFilter(filter, paramsBuilder); |
38 | 33 | } else { |
39 | | - return parseNonCompositeFilter(filter, paramsBuilder); |
| 34 | + return PostgresUtils.parseNonCompositeFilter( |
| 35 | + filter.getFieldName(), |
| 36 | + PostgresUtils.DOCUMENT_COLUMN, |
| 37 | + filter.getOp().toString(), |
| 38 | + filter.getValue(), |
| 39 | + paramsBuilder); |
40 | 40 | } |
41 | 41 | } |
42 | 42 |
|
43 | | - static String parseNonCompositeFilter(Filter filter, Builder paramsBuilder) { |
44 | | - Filter.Op op = filter.getOp(); |
45 | | - Object value = filter.getValue(); |
46 | | - String fieldName = filter.getFieldName(); |
47 | | - String fullFieldName = |
48 | | - prepareCast( |
49 | | - PostgresUtils.prepareFieldDataAccessorExpr(fieldName, PostgresUtils.DOCUMENT_COLUMN), |
50 | | - value); |
51 | | - StringBuilder filterString = new StringBuilder(fullFieldName); |
52 | | - String sqlOperator; |
53 | | - Boolean isMultiValued = false; |
54 | | - switch (op) { |
55 | | - case EQ: |
56 | | - sqlOperator = " = "; |
57 | | - break; |
58 | | - case GT: |
59 | | - sqlOperator = " > "; |
60 | | - break; |
61 | | - case LT: |
62 | | - sqlOperator = " < "; |
63 | | - break; |
64 | | - case GTE: |
65 | | - sqlOperator = " >= "; |
66 | | - break; |
67 | | - case LTE: |
68 | | - sqlOperator = " <= "; |
69 | | - break; |
70 | | - case LIKE: |
71 | | - // Case insensitive regex search, Append % at beginning and end of value to do a regex |
72 | | - // search |
73 | | - sqlOperator = " ILIKE "; |
74 | | - value = "%" + value + "%"; |
75 | | - break; |
76 | | - case NOT_IN: |
77 | | - // NOTE: Below two points |
78 | | - // 1. both NOT_IN and IN filter currently limited to non-array field |
79 | | - // - https://github.com/hypertrace/document-store/issues/32#issuecomment-781411676 |
80 | | - // 2. To make semantically opposite filter of IN, we need to check for if key is not present |
81 | | - // Ref in context of NEQ - |
82 | | - // https://github.com/hypertrace/document-store/pull/20#discussion_r547101520Other |
83 | | - // so, we need - "document->key IS NULL OR document->key->> NOT IN (v1, v2)" |
84 | | - StringBuilder notInFilterString = |
85 | | - PostgresUtils.prepareFieldAccessorExpr(fieldName, PostgresUtils.DOCUMENT_COLUMN); |
86 | | - if (notInFilterString != null && !OUTER_COLUMNS.contains(fieldName)) { |
87 | | - filterString = notInFilterString.append(" IS NULL OR ").append(fullFieldName); |
88 | | - } |
89 | | - sqlOperator = " NOT IN "; |
90 | | - isMultiValued = true; |
91 | | - value = prepareParameterizedStringForList((List<Object>) value, paramsBuilder); |
92 | | - break; |
93 | | - case IN: |
94 | | - // NOTE: both NOT_IN and IN filter currently limited to non-array field |
95 | | - // - https://github.com/hypertrace/document-store/issues/32#issuecomment-781411676 |
96 | | - sqlOperator = " IN "; |
97 | | - isMultiValued = true; |
98 | | - value = prepareParameterizedStringForList((List<Object>) value, paramsBuilder); |
99 | | - break; |
100 | | - case NOT_EXISTS: |
101 | | - sqlOperator = " IS NULL "; |
102 | | - value = null; |
103 | | - // For fields inside jsonb |
104 | | - StringBuilder notExists = |
105 | | - PostgresUtils.prepareFieldAccessorExpr(fieldName, PostgresUtils.DOCUMENT_COLUMN); |
106 | | - if (notExists != null && !OUTER_COLUMNS.contains(fieldName)) { |
107 | | - filterString = notExists; |
108 | | - } |
109 | | - break; |
110 | | - case EXISTS: |
111 | | - sqlOperator = " IS NOT NULL "; |
112 | | - value = null; |
113 | | - // For fields inside jsonb |
114 | | - StringBuilder exists = |
115 | | - PostgresUtils.prepareFieldAccessorExpr(fieldName, PostgresUtils.DOCUMENT_COLUMN); |
116 | | - if (exists != null && !OUTER_COLUMNS.contains(fieldName)) { |
117 | | - filterString = exists; |
118 | | - } |
119 | | - break; |
120 | | - case NEQ: |
121 | | - sqlOperator = " != "; |
122 | | - // https://github.com/hypertrace/document-store/pull/20#discussion_r547101520 |
123 | | - // The expected behaviour is to get all documents which either satisfy non equality |
124 | | - // condition |
125 | | - // or the key doesn't exist in them |
126 | | - // Semantics for handling if key not exists and if it exists, its value |
127 | | - // doesn't equal to the filter for Jsonb document will be done as: |
128 | | - // "document->key IS NULL OR document->key->> != value" |
129 | | - StringBuilder notEquals = |
130 | | - PostgresUtils.prepareFieldAccessorExpr(fieldName, PostgresUtils.DOCUMENT_COLUMN); |
131 | | - // For fields inside jsonb |
132 | | - if (notEquals != null && !OUTER_COLUMNS.contains(fieldName)) { |
133 | | - filterString = notEquals.append(" IS NULL OR ").append(fullFieldName); |
134 | | - } |
135 | | - break; |
136 | | - case CONTAINS: |
137 | | - // TODO: Matches condition inside an array of documents |
138 | | - default: |
139 | | - throw new UnsupportedOperationException(UNSUPPORTED_QUERY_OPERATION); |
140 | | - } |
141 | | - |
142 | | - filterString.append(sqlOperator); |
143 | | - if (value != null) { |
144 | | - if (isMultiValued) { |
145 | | - filterString.append(value); |
146 | | - } else { |
147 | | - filterString.append(QUESTION_MARK); |
148 | | - paramsBuilder.addObjectParam(value); |
149 | | - } |
150 | | - } |
151 | | - String filters = filterString.toString(); |
152 | | - return filters; |
153 | | - } |
154 | | - |
155 | 43 | static String parseCompositeFilter(Filter filter, Builder paramsBuilder) { |
156 | 44 | Filter.Op op = filter.getOp(); |
157 | 45 | switch (op) { |
@@ -192,40 +80,4 @@ static String parseOrderBys(List<OrderBy> orderBys) { |
192 | 80 | .filter(str -> !StringUtils.isEmpty(str)) |
193 | 81 | .collect(Collectors.joining(" , ")); |
194 | 82 | } |
195 | | - |
196 | | - private static String prepareParameterizedStringForList( |
197 | | - List<Object> values, Params.Builder paramsBuilder) { |
198 | | - String collect = |
199 | | - values.stream() |
200 | | - .map( |
201 | | - val -> { |
202 | | - paramsBuilder.addObjectParam(val); |
203 | | - return QUESTION_MARK; |
204 | | - }) |
205 | | - .collect(Collectors.joining(", ")); |
206 | | - return "(" + collect + ")"; |
207 | | - } |
208 | | - |
209 | | - private static String prepareCast(String field, Object value) { |
210 | | - String fmt = "CAST (%s AS %s)"; |
211 | | - |
212 | | - // handle the case if the value type is collection for filter operator - `IN` |
213 | | - // Currently, for `IN` operator, we are considering List collection, and it is fair |
214 | | - // assumption that all its value of the same types. Based on that and for consistency |
215 | | - // we will use CAST ( <field name> as <type> ) for all non string operator. |
216 | | - // Ref : https://github.com/hypertrace/document-store/pull/30#discussion_r571782575 |
217 | | - |
218 | | - if (value instanceof List<?> && ((List<Object>) value).size() > 0) { |
219 | | - List<Object> listValue = (List<Object>) value; |
220 | | - value = listValue.get(0); |
221 | | - } |
222 | | - |
223 | | - if (value instanceof Number) { |
224 | | - return String.format(fmt, field, "NUMERIC"); |
225 | | - } else if (value instanceof Boolean) { |
226 | | - return String.format(fmt, field, "BOOLEAN"); |
227 | | - } else /* default is string */ { |
228 | | - return field; |
229 | | - } |
230 | | - } |
231 | 83 | } |
0 commit comments