Skip to content

Commit

Permalink
[Spotless] Applying Google Code Format for opensearch directory #16 (o…
Browse files Browse the repository at this point in the history
…pensearch-project#1977)

* spotless apply for OpenSearch P1.

Signed-off-by: Mitchell Gale <[email protected]>

* Manual spotless changes

Signed-off-by: Mitchell Gale <[email protected]>

* ignore failures for checkstyles.

Signed-off-by: Mitchell Gale <[email protected]>

* Apply suggestions from code review

Co-authored-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

* Apply suggestions from code review

Co-authored-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

* Apply suggestions from code review

Co-authored-by: Yury-Fridlyand <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

* Address PR comment

Signed-off-by: Mitchell Gale <[email protected]>

* add order list in Content.java

Signed-off-by: Mitchell Gale <[email protected]>

* Update opensearch/src/main/java/org/opensearch/sql/opensearch/request/system/OpenSearchDescribeIndexRequest.java

Co-authored-by: Guian Gumpac <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

* Update opensearch/src/main/java/org/opensearch/sql/opensearch/response/agg/MetricParserHelper.java

Co-authored-by: Guian Gumpac <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>

* Fixing compilation error

Signed-off-by: Mitchell Gale <[email protected]>

---------

Signed-off-by: Mitchell Gale <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>
Co-authored-by: Yury-Fridlyand <[email protected]>
Co-authored-by: Guian Gumpac <[email protected]>
  • Loading branch information
3 people committed Aug 22, 2023
1 parent cfb2d91 commit c36c1dc
Show file tree
Hide file tree
Showing 94 changed files with 4,289 additions and 4,120 deletions.
4 changes: 4 additions & 0 deletions opensearch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins {
id 'java-library'
id "io.freefair.lombok"
id 'jacoco'
id 'info.solidsoft.pitest' version '1.9.0'
}

dependencies {
Expand All @@ -47,6 +48,9 @@ dependencies {
testImplementation group: 'org.opensearch.test', name: 'framework', version: "${opensearch_version}"
}

checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

test {
useJUnitPlatform()
testLogging {
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

0 comments on commit c36c1dc

Please sign in to comment.