gortiz commented on code in PR #16094:
URL: https://github.com/apache/pinot/pull/16094#discussion_r2184541211
##########
pinot-spi/src/main/java/org/apache/pinot/spi/utils/JsonUtils.java:
##########
@@ -743,7 +743,17 @@ public static List<Map<String, String>> flatten(String
jsonString, JsonIndexConf
throw e;
}
}
- return JsonUtils.flatten(jsonNode, jsonIndexConfig);
+
+ try {
+ return JsonUtils.flatten(jsonNode, jsonIndexConfig);
+ } catch (IllegalArgumentException e) {
+ // IllegalArgumentException is a catch-all Exception from
'JsonUtils.flatten()' for scenarios other than OOM
+ if (jsonIndexConfig.getSkipInvalidJson()) {
+ return SKIPPED_FLATTENED_RECORD;
+ } else {
+ throw e;
+ }
+ }
Review Comment:
nit: Wouldn't it be better to move ` return JsonUtils.flatten(jsonNode,
jsonIndexConfig);` inside the first try-catch?
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/text/LuceneFSTIndexCreator.java:
##########
@@ -50,36 +56,56 @@ public class LuceneFSTIndexCreator implements
FSTIndexCreator {
*
* @param indexDir Index directory
* @param columnName Column name for which index is being created
+ * @param tableNameWithType table name with type
* @param sortedEntries Sorted entries of the unique values of the column.
* @throws IOException
*/
- public LuceneFSTIndexCreator(File indexDir, String columnName, String[]
sortedEntries)
+ public LuceneFSTIndexCreator(File indexDir, String columnName, String
tableNameWithType, String[] sortedEntries)
throws IOException {
+ this(indexDir, columnName, tableNameWithType, sortedEntries, new
FSTBuilder());
+ }
+
+ @VisibleForTesting
+ public LuceneFSTIndexCreator(File indexDir, String columnName, String
tableNameWithType, String[] sortedEntries,
+ FSTBuilder fstBuilder)
+ throws IOException {
+ _tableNameWithType = tableNameWithType;
_fstIndexFile = new File(indexDir, columnName +
V1Constants.Indexes.LUCENE_V912_FST_INDEX_FILE_EXTENSION);
- _fstBuilder = new FSTBuilder();
+ _fstBuilder = fstBuilder;
_dictId = 0;
if (sortedEntries != null) {
for (_dictId = 0; _dictId < sortedEntries.length; _dictId++) {
- _fstBuilder.addEntry(sortedEntries[_dictId], _dictId);
+ try {
+ _fstBuilder.addEntry(sortedEntries[_dictId], _dictId);
+ } catch (IOException ex) {
+ // Caught exception while trying to add, update metric and skip the
document
+ String metricKeyName =
+ _tableNameWithType + "-" +
FstIndexType.INDEX_DISPLAY_NAME.toUpperCase(Locale.US) + "-indexingError";
+ ServerMetrics.get().addMeteredTableValue(metricKeyName,
ServerMeter.INDEXING_FAILURES, 1);
Review Comment:
I think this should be optional, similar to how we do it with JSON indexes.
Even if the default value for the option is to not fail, we should give users
the ability to fail when invalid values are received
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/json/BaseJsonIndexCreator.java:
##########
@@ -94,6 +101,27 @@ public void add(String jsonString)
addFlattenedRecords(JsonUtils.flatten(jsonString, _jsonIndexConfig));
}
+ @Override
+ public void add(Object value)
+ throws IOException {
+ String valueToAdd;
+ try {
+ valueToAdd = JsonUtils.objectToString(value);
+ } catch (JsonProcessingException e) {
+ if (_jsonIndexConfig.getSkipInvalidJson()) {
+ // Caught exception while trying to add, update metric and add a
default SKIPPED_FLATTENED_RECORD
+ String metricKeyName =
+ _tableNameWithType + "-" +
JsonIndexType.INDEX_DISPLAY_NAME.toUpperCase(Locale.US) + "-indexingError";
+ ServerMetrics.get().addMeteredTableValue(metricKeyName,
ServerMeter.INDEXING_FAILURES, 1);
+ addFlattenedRecords(JsonUtils.SKIPPED_FLATTENED_RECORD);
+ return;
Review Comment:
nit: I think it may be useful to be able to specify the _skipped record_ as
index config. Only in case it is not defined we should use
JsonUtils.SKIPPED_FLATTENED_RECORD
##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/creator/HnswVectorIndexCreatorTest.java:
##########
@@ -0,0 +1,94 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.local.segment.index.creator;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.io.FileUtils;
+import
org.apache.pinot.segment.local.segment.creator.impl.vector.HnswVectorIndexCreator;
+import
org.apache.pinot.segment.local.segment.index.readers.vector.HnswVectorIndexReader;
+import org.apache.pinot.segment.spi.index.creator.VectorIndexConfig;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+
+public class HnswVectorIndexCreatorTest {
+ private static final File INDEX_DIR =
+ new File(FileUtils.getTempDirectory(),
HnswVectorIndexCreatorTest.class.toString());
+
+ @BeforeMethod
+ public void setUp()
+ throws IOException {
+ FileUtils.forceMkdir(INDEX_DIR);
+ }
+
+ @AfterMethod
+ public void tearDown()
+ throws IOException {
+ FileUtils.deleteDirectory(INDEX_DIR);
+ }
+
+ @Test
+ public void testIndexWriterReader()
+ throws IOException {
+ Map<String, String> properties = new HashMap<>();
+ properties.put("vectorIndexType", "HNSW");
+ properties.put("vectorDimension", "1536");
+
+ VectorIndexConfig config = new VectorIndexConfig(properties);
+ try (HnswVectorIndexCreator creator = new HnswVectorIndexCreator("foo",
INDEX_DIR, config)) {
+ float[] values1 = new float[] {5.0F, 42.0F, 54.33333F, 42.24F,
1001.045F};
+ creator.add(values1);
+ float[] values2 = new float[] {42.0F, 23423.0F, 42431.32532F,
6785676.3242F, 42.3F};
+ creator.add(values2);
+ float[] values3 = new float[] {1.0F, 2.0F, 3.0F, 4.0F, 5.0F};
+ creator.add(values3);
+ float[] values4 = new float[] {42.678F, 23423423.0F, 42431.32523432F,
6723485.3242F, 42342.3F};
+ creator.add(values4);
+ creator.seal();
+ }
+
+ // Use TextIndex reader to validate that reads work
+ try (HnswVectorIndexReader reader = new HnswVectorIndexReader("foo",
INDEX_DIR, 4, config)) {
+ int[] matchedDocIds = reader.getDocIds(new float[]{5.0F, 42.0F,
54.33333F, 42.24F, 3413.4F}, 3).toArray();
+ Assert.assertEquals(matchedDocIds.length, 3);
+ Assert.assertEquals(matchedDocIds[0], 0);
+ Assert.assertEquals(matchedDocIds[1], 2);
+ Assert.assertEquals(matchedDocIds[1], 2);
+
+ matchedDocIds = reader.getDocIds(new float[]{1.0F, 2.0F, 3.0F, 4.0F,
5.0F}, 1).toArray();
+ Assert.assertEquals(matchedDocIds.length, 1);
+ Assert.assertEquals(matchedDocIds[0], 2);
+
+ matchedDocIds = reader.getDocIds(new float[]{42.678F, 23423423.0F,
42431.32523432F, 6723485.3242F, 41.3F}, 1)
+ .toArray();
+ Assert.assertEquals(matchedDocIds.length, 1);
+ Assert.assertEquals(matchedDocIds[0], 3);
+
+ matchedDocIds = reader.getDocIds(new float[]{42.0F, 23423.0F,
42431.32532F, 6785676.3242F, 42.3F}, 1)
+ .toArray();
+ Assert.assertEquals(matchedDocIds.length, 1);
+ Assert.assertEquals(matchedDocIds[0], 1);
Review Comment:
It is a better practice to create a single test per scenario we want to
test. We have too many old test classes whose methods check for too many
things, but we should try to write new tests in a more effective way.
Here, IICU, we could have 4 different tests. We could even create the index
once in a setup method and then have four tests, one for each value, using the
index and applying some assertions. Ideally, each (or most) assertions should
also include a text describing the problem. I found copilot very useful for
that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]