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

[Spotless] Applying Google Code Format for opensearch directory #16 #1977

Merged
3 changes: 3 additions & 0 deletions opensearch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ dependencies {
testImplementation group: 'org.opensearch.test', name: 'framework', version: "${opensearch_version}"
}

checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

pitest {
targetClasses = ['org.opensearch.sql.*']
pitestVersion = '1.9.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import org.opensearch.client.node.NodeClient;
import org.opensearch.ml.client.MachineLearningNodeClient;


public class MLClient {
private static MachineLearningNodeClient INSTANCE;

private MLClient() {

}
private MLClient() {}

/**
* get machine learning client.
*
* @param nodeClient node client
* @return machine learning client
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.client;

import java.util.List;
Expand All @@ -14,25 +13,26 @@
import org.opensearch.sql.opensearch.response.OpenSearchResponse;

/**
* OpenSearch client abstraction to wrap different OpenSearch client implementation. For
* example, implementation by node client for OpenSearch plugin or by REST client for
* standalone mode.
* OpenSearch client abstraction to wrap different OpenSearch client implementation. For example,
* implementation by node client for OpenSearch plugin or by REST client for standalone mode.
*/
public interface OpenSearchClient {

String META_CLUSTER_NAME = "CLUSTER_NAME";

/**
* Check if the given index exists.
*
* @param indexName index name
* @return true if exists, otherwise false
*/
boolean exists(String indexName);

/**
* Create OpenSearch index based on the given mappings.
*
* @param indexName index name
* @param mappings index mappings
* @param mappings index mappings
*/
void createIndex(String indexName, Map<String, Object> mappings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.data.type;

import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN;

import lombok.EqualsAndHashCode;

/**
* The type of a binary value. See
* The type of binary value. See<br>
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/binary/">doc</a>
*/
@EqualsAndHashCode(callSuper = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.opensearch.data.type;

import com.google.common.collect.ImmutableMap;
Expand All @@ -18,15 +17,11 @@
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;

/**
* The extension of ExprType in OpenSearch.
*/
/** The extension of ExprType in OpenSearch. */
@EqualsAndHashCode
public class OpenSearchDataType implements ExprType, Serializable {

/**
* The mapping (OpenSearch engine) type.
*/
/** The mapping (OpenSearch engine) type. */
public enum MappingType {
Invalid(null, ExprCoreType.UNKNOWN),
Text("text", ExprCoreType.UNKNOWN),
Expand All @@ -51,8 +46,7 @@ public enum MappingType {
private final String name;

// Associated `ExprCoreType`
@Getter
private final ExprCoreType exprCoreType;
@Getter private final ExprCoreType exprCoreType;

MappingType(String name, ExprCoreType exprCoreType) {
this.name = name;
Expand All @@ -64,16 +58,15 @@ public String toString() {
}
}

@EqualsAndHashCode.Exclude
@Getter
protected MappingType mappingType;
@EqualsAndHashCode.Exclude @Getter protected MappingType mappingType;

// resolved ExprCoreType
protected ExprCoreType exprCoreType;

/**
* Get a simplified type {@link ExprCoreType} if possible.
* To avoid returning `UNKNOWN` for `OpenSearch*Type`s, e.g. for IP, returns itself.
* Get a simplified type {@link ExprCoreType} if possible. To avoid returning `UNKNOWN` for
* `OpenSearch*Type`s, e.g. for IP, returns itself.
*
* @return An {@link ExprType}.
*/
public ExprType getExprType() {
Expand All @@ -84,22 +77,23 @@ public ExprType getExprType() {
}

/**
* Simple instances of OpenSearchDataType are created once during entire SQL engine lifetime
* and cached there. This reduces memory usage and increases type comparison.
* Note: Types with non-empty fields and properties are not cached.
* Simple instances of OpenSearchDataType are created once during entire SQL engine lifetime and
* cached there. This reduces memory usage and increases type comparison. Note: Types with
* non-empty fields and properties are not cached.
*/
private static final Map<String, OpenSearchDataType> instances = new HashMap<>();

static {
EnumUtils.getEnumList(MappingType.class).stream()
.filter(t -> t != MappingType.Invalid).forEach(t ->
instances.put(t.toString(), OpenSearchDataType.of(t)));
EnumUtils.getEnumList(ExprCoreType.class).forEach(t ->
instances.put(t.toString(), OpenSearchDataType.of(t)));
.filter(t -> t != MappingType.Invalid)
.forEach(t -> instances.put(t.toString(), OpenSearchDataType.of(t)));
EnumUtils.getEnumList(ExprCoreType.class)
.forEach(t -> instances.put(t.toString(), OpenSearchDataType.of(t)));
}

/**
* Parses index mapping and maps it to a Data type in the SQL plugin.
*
* @param indexMapping An input with keys and objects that need to be mapped to a data type.
* @return The mapping.
*/
Expand All @@ -110,37 +104,35 @@ public static Map<String, OpenSearchDataType> parseMapping(Map<String, Object> i
return result;
}

indexMapping.forEach((k, v) -> {
var innerMap = (Map<String, Object>)v;
// by default, the type is treated as an Object if "type" is not provided
var type = ((String) innerMap
.getOrDefault(
"type",
"object"))
.replace("_", "");
if (!EnumUtils.isValidEnumIgnoreCase(OpenSearchDataType.MappingType.class, type)) {
// unknown type, e.g. `alias`
// TODO resolve alias reference
return;
}
// create OpenSearchDataType
result.put(k, OpenSearchDataType.of(
EnumUtils.getEnumIgnoreCase(OpenSearchDataType.MappingType.class, type),
innerMap)
);
});
indexMapping.forEach(
(k, v) -> {
var innerMap = (Map<String, Object>) v;
// by default, the type is treated as an Object if "type" is not provided
var type = ((String) innerMap.getOrDefault("type", "object")).replace("_", "");
if (!EnumUtils.isValidEnumIgnoreCase(OpenSearchDataType.MappingType.class, type)) {
// unknown type, e.g. `alias`
// TODO resolve alias reference
return;
}
// create OpenSearchDataType
result.put(
k,
OpenSearchDataType.of(
EnumUtils.getEnumIgnoreCase(OpenSearchDataType.MappingType.class, type),
innerMap));
});
return result;
}

/**
* A constructor function which builds proper `OpenSearchDataType` for given mapping `Type`.
*
* @param mappingType A mapping type.
* @return An instance or inheritor of `OpenSearchDataType`.
*/
public static OpenSearchDataType of(MappingType mappingType, Map<String, Object> innerMap) {
OpenSearchDataType res = instances.getOrDefault(mappingType.toString(),
new OpenSearchDataType(mappingType)
);
OpenSearchDataType res =
instances.getOrDefault(mappingType.toString(), new OpenSearchDataType(mappingType));
switch (mappingType) {
case Object:
// TODO: use Object type once it has been added
Expand All @@ -158,9 +150,12 @@ public static OpenSearchDataType of(MappingType mappingType, Map<String, Object>
Map<String, OpenSearchDataType> fields =
parseMapping((Map<String, Object>) innerMap.getOrDefault("fields", Map.of()));
return (!fields.isEmpty()) ? OpenSearchTextType.of(fields) : OpenSearchTextType.of();
case GeoPoint: return OpenSearchGeoPointType.of();
case Binary: return OpenSearchBinaryType.of();
case Ip: return OpenSearchIpType.of();
case GeoPoint:
return OpenSearchGeoPointType.of();
case Binary:
return OpenSearchBinaryType.of();
case Ip:
return OpenSearchIpType.of();
case Date:
// Default date formatter is used when "" is passed as the second parameter
String format = (String) innerMap.getOrDefault("format", "");
Expand All @@ -173,6 +168,7 @@ public static OpenSearchDataType of(MappingType mappingType, Map<String, Object>
/**
* A constructor function which builds proper `OpenSearchDataType` for given mapping `Type`.
* Designed to be called by the mapping parser only (and tests).
*
* @param mappingType A mapping type.
* @return An instance or inheritor of `OpenSearchDataType`.
*/
Expand All @@ -182,6 +178,7 @@ public static OpenSearchDataType of(MappingType mappingType) {

/**
* A constructor function which builds proper `OpenSearchDataType` for given {@link ExprType}.
*
* @param type A type.
* @return An instance of `OpenSearchDataType`.
*/
Expand Down Expand Up @@ -211,9 +208,7 @@ protected OpenSearchDataType(ExprCoreType type) {

// For datatypes with properties (example: object and nested types)
// a read-only collection
@Getter
@EqualsAndHashCode.Exclude
Map<String, OpenSearchDataType> properties = ImmutableMap.of();
@Getter @EqualsAndHashCode.Exclude Map<String, OpenSearchDataType> properties = ImmutableMap.of();

@Override
// Called when building TypeEnvironment and when serializing PPL response
Expand All @@ -236,46 +231,52 @@ public String legacyTypeName() {
}

/**
* Clone type object without {@link #properties} - without info about nested object types.
* Note: Should be overriden by all derived classes for proper work.
* Clone type object without {@link #properties} - without info about nested object types. Note:
* Should be overriden by all derived classes for proper work.
*
* @return A cloned object.
*/
protected OpenSearchDataType cloneEmpty() {
return this.mappingType == null
? new OpenSearchDataType(this.exprCoreType) : new OpenSearchDataType(this.mappingType);
? new OpenSearchDataType(this.exprCoreType)
: new OpenSearchDataType(this.mappingType);
}

/**
* Flattens mapping tree into a single layer list of objects (pairs of name-types actually),
* which don't have nested types.
* See {@link OpenSearchDataTypeTest#traverseAndFlatten() test} for example.
* Flattens mapping tree into a single layer list of objects (pairs of name-types actually), which
* don't have nested types. See {@link OpenSearchDataTypeTest#traverseAndFlatten() test} for
* example.
*
* @param tree A list of `OpenSearchDataType`s - map between field name and its type.
* @return A list of all `OpenSearchDataType`s from given map on the same nesting level (1).
* Nested object names are prefixed by names of their host.
* Nested object names are prefixed by names of their host.
*/
public static Map<String, OpenSearchDataType> traverseAndFlatten(
Map<String, OpenSearchDataType> tree) {
final Map<String, OpenSearchDataType> result = new LinkedHashMap<>();
BiConsumer<Map<String, OpenSearchDataType>, String> visitLevel = new BiConsumer<>() {
@Override
public void accept(Map<String, OpenSearchDataType> subtree, String prefix) {
for (var entry : subtree.entrySet()) {
String entryKey = entry.getKey();
var nextPrefix = prefix.isEmpty() ? entryKey : String.format("%s.%s", prefix, entryKey);
result.put(nextPrefix, entry.getValue().cloneEmpty());
var nextSubtree = entry.getValue().getProperties();
if (!nextSubtree.isEmpty()) {
accept(nextSubtree, nextPrefix);
BiConsumer<Map<String, OpenSearchDataType>, String> visitLevel =
new BiConsumer<>() {
@Override
public void accept(Map<String, OpenSearchDataType> subtree, String prefix) {
for (var entry : subtree.entrySet()) {
String entryKey = entry.getKey();
var nextPrefix =
prefix.isEmpty() ? entryKey : String.format("%s.%s", prefix, entryKey);
result.put(nextPrefix, entry.getValue().cloneEmpty());
var nextSubtree = entry.getValue().getProperties();
if (!nextSubtree.isEmpty()) {
accept(nextSubtree, nextPrefix);
}
}
}
}
}
};
};
visitLevel.accept(tree, "");
return result;
}

/**
* Resolve type of identified from parsed mapping tree.
*
* @param tree Parsed mapping tree (not flattened).
* @param id An identifier.
* @return Resolved OpenSearchDataType or null if not found.
Expand Down
Loading
Loading