3434
3535import java .io .File ;
3636import java .util .Collections ;
37+ import java .util .HashMap ;
3738import java .util .HashSet ;
3839import java .util .List ;
40+ import java .util .Map ;
3941import java .util .Optional ;
4042import java .util .Set ;
4143import java .util .concurrent .TimeUnit ;
4850import com .google .common .collect .ImmutableSet ;
4951import com .google .common .util .concurrent .UncheckedExecutionException ;
5052import com .redis .lettucemod .api .StatefulRedisModulesConnection ;
53+ import com .redis .lettucemod .search .AggregateOperation ;
5154import com .redis .lettucemod .search .AggregateWithCursorResults ;
5255import com .redis .lettucemod .search .CreateOptions ;
5356import com .redis .lettucemod .search .Document ;
5457import com .redis .lettucemod .search .Field ;
58+ import com .redis .lettucemod .search .Group ;
5559import com .redis .lettucemod .search .IndexInfo ;
5660import com .redis .lettucemod .search .SearchResults ;
5761import com .redis .lettucemod .util .ClientBuilder ;
@@ -161,6 +165,12 @@ public Set<String> getAllTables() throws SchemaNotFoundException {
161165 return builder .build ();
162166 }
163167
168+ /**
169+ *
170+ * @param schemaTableName SchemaTableName to load
171+ * @return RediSearchTable describing the RediSearch index
172+ * @throws TableNotFoundException if no index by that name was found
173+ */
164174 public RediSearchTable getTable (SchemaTableName tableName ) throws TableNotFoundException {
165175 try {
166176 return tableCache .getUnchecked (tableName );
@@ -172,13 +182,13 @@ public RediSearchTable getTable(SchemaTableName tableName) throws TableNotFoundE
172182
173183 @ SuppressWarnings ("unchecked" )
174184 public void createTable (SchemaTableName schemaTableName , List <RediSearchColumnHandle > columns ) {
175- String tableName = schemaTableName . getTableName ( );
176- if (!connection .sync ().ftList ().contains (tableName )) {
185+ String index = index ( schemaTableName );
186+ if (!connection .sync ().ftList ().contains (index )) {
177187 List <Field <String >> fields = columns .stream ().filter (c -> !c .getName ().equals ("_id" ))
178- .map (c -> buildField (c .getName (), c .getType ())).collect (Collectors .toList ());
188+ .map (c -> buildField (c .getName (), c .getType ())).collect (Collectors .toUnmodifiableList ());
179189 CreateOptions .Builder <String , String > options = CreateOptions .<String , String >builder ();
180- options .prefix (tableName + ":" );
181- connection .sync ().ftCreate (tableName , options .build (), fields .toArray (Field []::new ));
190+ options .prefix (index + ":" );
191+ connection .sync ().ftCreate (index , options .build (), fields .toArray (Field []::new ));
182192 }
183193 }
184194
@@ -210,19 +220,26 @@ public void dropColumn(SchemaTableName schemaTableName, String columnName) {
210220 throw new TrinoException (NOT_SUPPORTED , "This connector does not support dropping columns" );
211221 }
212222
213- private RediSearchTable loadTableSchema (SchemaTableName schemaTableName ) {
223+ /**
224+ *
225+ * @param schemaTableName SchemaTableName to load
226+ * @return RediSearchTable describing the RediSearch index
227+ * @throws TableNotFoundException if no index by that name was found
228+ */
229+ private RediSearchTable loadTableSchema (SchemaTableName schemaTableName ) throws TableNotFoundException {
214230 String index = schemaTableName .getTableName ();
215- Optional <IndexInfo > indexInfo = indexInfo (index );
216- if (indexInfo .isEmpty ()) {
231+ Optional <IndexInfo > indexInfoOptional = indexInfo (index );
232+ if (indexInfoOptional .isEmpty ()) {
217233 throw new TableNotFoundException (schemaTableName , format ("Index '%s' not found" , index ), null );
218234 }
235+ IndexInfo indexInfo = indexInfoOptional .get ();
219236 Set <String > fields = new HashSet <>();
220237 ImmutableList .Builder <RediSearchColumnHandle > columns = ImmutableList .builder ();
221238 for (RediSearchBuiltinField builtinfield : RediSearchBuiltinField .values ()) {
222239 fields .add (builtinfield .getName ());
223240 columns .add (builtinfield .getColumnHandle ());
224241 }
225- for (Field <String > indexedField : indexInfo .get (). getFields ()) {
242+ for (Field <String > indexedField : indexInfo .getFields ()) {
226243 RediSearchColumnHandle column = buildColumnHandle (indexedField );
227244 fields .add (column .getName ());
228245 columns .add (column );
@@ -237,8 +254,9 @@ private RediSearchTable loadTableSchema(SchemaTableName schemaTableName) {
237254 fields .add (docField );
238255 }
239256 }
240- return new RediSearchTable (new RediSearchTableHandle (RediSearchTableHandle .Type .SEARCH , schemaTableName ),
241- columns .build ());
257+ RediSearchTableHandle tableHandle = new RediSearchTableHandle (RediSearchTableHandle .Type .SEARCH ,
258+ schemaTableName );
259+ return new RediSearchTable (tableHandle , columns .build (), indexInfo );
242260 }
243261
244262 private Optional <IndexInfo > indexInfo (String index ) {
@@ -278,8 +296,7 @@ private Type columnType(TypeSignature typeSignature) {
278296 return typeManager .fromSqlType (typeSignature .toString ());
279297 }
280298
281- public SearchResults <String , String > search (RediSearchTableHandle tableHandle ,
282- List <RediSearchColumnHandle > columns ) {
299+ public SearchResults <String , String > search (RediSearchTableHandle tableHandle , String [] columns ) {
283300 Search search = translator .search (tableHandle , columns );
284301 log .info ("Running %s" , search );
285302 return connection .sync ().ftSearch (search .getIndex (), search .getQuery (), search .getOptions ());
@@ -288,20 +305,32 @@ public SearchResults<String, String> search(RediSearchTableHandle tableHandle,
288305 public AggregateWithCursorResults <String > aggregate (RediSearchTableHandle table ) {
289306 Aggregation aggregation = translator .aggregate (table );
290307 log .info ("Running %s" , aggregation );
291- return connection .sync ().ftAggregate (aggregation .getIndex (), aggregation .getQuery (),
292- aggregation .getCursorOptions (), aggregation .getOptions ());
308+ AggregateWithCursorResults <String > results = connection .sync ().ftAggregate (aggregation .getIndex (),
309+ aggregation .getQuery (), aggregation .getCursorOptions (), aggregation .getOptions ());
310+ List <AggregateOperation <?, ?>> groupBys = aggregation .getOptions ().getOperations ().stream ()
311+ .filter (o -> o .getType () == AggregateOperation .Type .GROUP ).collect (Collectors .toUnmodifiableList ());
312+ if (results .isEmpty () && !groupBys .isEmpty ()) {
313+ Group groupBy = (Group ) groupBys .get (0 );
314+ Optional <String > as = groupBy .getReducers ()[0 ].getAs ();
315+ if (as .isPresent ()) {
316+ Map <String , Object > doc = new HashMap <>();
317+ doc .put (as .get (), 0 );
318+ results .add (doc );
319+ }
320+ }
321+ return results ;
293322 }
294323
295324 public AggregateWithCursorResults <String > cursorRead (RediSearchTableHandle tableHandle , long cursor ) {
296- String index = index (tableHandle );
325+ String index = index (tableHandle . getSchemaTableName () );
297326 if (config .getCursorCount () > 0 ) {
298327 return connection .sync ().ftCursorRead (index , cursor , config .getCursorCount ());
299328 }
300329 return connection .sync ().ftCursorRead (index , cursor );
301330 }
302331
303- private String index (RediSearchTableHandle tableHandle ) {
304- return tableHandle . getSchemaTableName () .getTableName ();
332+ private String index (SchemaTableName schemaTableName ) {
333+ return schemaTableName .getTableName ();
305334 }
306335
307336 private Field <String > buildField (String columnName , Type columnType ) {
@@ -381,7 +410,11 @@ private TypeSignature varcharType() {
381410 }
382411
383412 public void cursorDelete (RediSearchTableHandle tableHandle , long cursor ) {
384- connection .sync ().ftCursorDelete (index (tableHandle ), cursor );
413+ connection .sync ().ftCursorDelete (index (tableHandle .getSchemaTableName ()), cursor );
414+ }
415+
416+ public Long deleteDocs (List <String > docIds ) {
417+ return connection .sync ().del (docIds .toArray (String []::new ));
385418 }
386419
387420}
0 commit comments