Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixPPLAllowedFields #181

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions src/main/java/org/opensearch/agent/tools/PPLTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package org.opensearch.agent.tools;

import static org.opensearch.ml.common.CommonValue.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -16,9 +14,11 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -93,14 +93,35 @@

private static Gson gson = new Gson();

private static Map<String, String> defaultPromptDict;
private static Map<String, String> DEFAULT_PROMPT_DICT;

private static Set<String> ALLOWED_FIELDS_TYPE;

static {
ALLOWED_FIELDS_TYPE = new HashSet<>(); // from
// https://github.com/opensearch-project/sql/blob/2.x/docs/user/ppl/general/datatypes.rst#data-types-mapping
ALLOWED_FIELDS_TYPE.add("boolean");
ALLOWED_FIELDS_TYPE.add("byte");
ALLOWED_FIELDS_TYPE.add("short");
ALLOWED_FIELDS_TYPE.add("integer");
ALLOWED_FIELDS_TYPE.add("long");
ALLOWED_FIELDS_TYPE.add("float");
ALLOWED_FIELDS_TYPE.add("half_float");
ALLOWED_FIELDS_TYPE.add("scaled_float");
ALLOWED_FIELDS_TYPE.add("double");
ALLOWED_FIELDS_TYPE.add("keyword");
ALLOWED_FIELDS_TYPE.add("text");
ALLOWED_FIELDS_TYPE.add("date");
ALLOWED_FIELDS_TYPE.add("ip");
ALLOWED_FIELDS_TYPE.add("binary");
ALLOWED_FIELDS_TYPE.add("object");
ALLOWED_FIELDS_TYPE.add("nested");

try {
defaultPromptDict = loadDefaultPromptDict();
DEFAULT_PROMPT_DICT = loadDefaultPromptDict();
} catch (IOException e) {
log.error("fail to load default prompt dict" + e.getMessage());
defaultPromptDict = new HashMap<>();
DEFAULT_PROMPT_DICT = new HashMap<>();

Check warning on line 124 in src/main/java/org/opensearch/agent/tools/PPLTool.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/agent/tools/PPLTool.java#L124

Added line #L124 was not covered by tests
}
}

Expand All @@ -127,7 +148,7 @@
this.modelId = modelId;
this.pplModelType = PPLModelType.from(pplModelType);
if (contextPrompt.isEmpty()) {
this.contextPrompt = this.defaultPromptDict.getOrDefault(this.pplModelType.toString(), "");
this.contextPrompt = this.DEFAULT_PROMPT_DICT.getOrDefault(this.pplModelType.toString(), "");
} else {
this.contextPrompt = contextPrompt;
}
Expand All @@ -148,13 +169,15 @@
+ indexName
);
}
SearchRequest searchRequest = buildSearchRequest(indexName);

GetMappingsRequest getMappingsRequest = buildGetMappingRequest(indexName);
client.admin().indices().getMappings(getMappingsRequest, ActionListener.<GetMappingsResponse>wrap(getMappingsResponse -> {
Map<String, MappingMetadata> mappings = getMappingsResponse.getMappings();
if (mappings.size() == 0) {
throw new IllegalArgumentException("No matching mapping with index name: " + indexName);
}
String firstIndexName = (String) mappings.keySet().toArray()[0];
SearchRequest searchRequest = buildSearchRequest(firstIndexName);
client.search(searchRequest, ActionListener.<SearchResponse>wrap(searchResponse -> {
SearchHit[] searchHits = searchResponse.getHits().getHits();
String tableInfo = constructTableInfo(searchHits, mappings);
Expand Down Expand Up @@ -320,13 +343,17 @@
extractSamples(sampleSource, fieldsToSample, "");

for (String key : sortedKeys) {
String line = "- " + key + ": " + fieldsToType.get(key) + " (" + fieldsToSample.get(key) + ")";
tableInfoJoiner.add(line);
if (ALLOWED_FIELDS_TYPE.contains(fieldsToType.get(key))) {
String line = "- " + key + ": " + fieldsToType.get(key) + " (" + fieldsToSample.get(key) + ")";
tableInfoJoiner.add(line);
}
}
} else {
for (String key : sortedKeys) {
String line = "- " + key + ": " + fieldsToType.get(key);
tableInfoJoiner.add(line);
if (ALLOWED_FIELDS_TYPE.contains(fieldsToType.get(key))) {
String line = "- " + key + ": " + fieldsToType.get(key);
tableInfoJoiner.add(line);

Check warning on line 355 in src/main/java/org/opensearch/agent/tools/PPLTool.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/agent/tools/PPLTool.java#L354-L355

Added lines #L354 - L355 were not covered by tests
}
}
}

Expand Down
Loading