From 8432bc17c7ab2aa03d6e92540239d49d7f7adc7d Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Thu, 10 Aug 2023 12:23:14 -0700 Subject: [PATCH 1/6] [Spotless] Applying Google Code Format for datasource files #6 (#1939) * Spotless apply for datasources. Signed-off-by: Mitchell Gale * ignore checkstyle for datasources Signed-off-by: Mitchell Gale --------- Signed-off-by: Mitchell Gale Signed-off-by: Mitchell Gale --- build.gradle | 10 +- datasources/build.gradle | 3 + .../datasources/auth/AuthenticationType.java | 4 +- .../DataSourceUserAuthorizationHelper.java | 5 +- ...DataSourceUserAuthorizationHelperImpl.java | 23 +- .../sql/datasources/encryptor/Encryptor.java | 1 - .../datasources/encryptor/EncryptorImpl.java | 42 +-- .../DataSourceNotFoundException.java | 5 +- .../datasources/exceptions/ErrorMessage.java | 18 +- .../CreateDataSourceActionRequest.java | 16 +- .../CreateDataSourceActionResponse.java | 6 +- .../DeleteDataSourceActionRequest.java | 12 +- .../DeleteDataSourceActionResponse.java | 4 +- .../transport/GetDataSourceActionRequest.java | 13 +- .../GetDataSourceActionResponse.java | 4 +- .../UpdateDataSourceActionRequest.java | 12 +- .../UpdateDataSourceActionResponse.java | 6 +- .../rest/RestDataSourceQueryAction.java | 206 ++++++++------ .../service/DataSourceLoaderCache.java | 5 +- .../service/DataSourceLoaderCacheImpl.java | 25 +- .../service/DataSourceMetadataStorage.java | 19 +- .../service/DataSourceServiceImpl.java | 41 ++- .../OpenSearchDataSourceMetadataStorage.java | 142 +++++----- .../TransportCreateDataSourceAction.java | 32 ++- .../TransportDeleteDataSourceAction.java | 32 ++- .../TransportGetDataSourceAction.java | 61 ++-- .../TransportUpdateDataSourceAction.java | 28 +- .../utils/XContentParserUtils.java | 16 +- .../auth/AuthenticationTypeTest.java | 1 - ...SourceUserAuthorizationHelperImplTest.java | 60 ++-- .../encryptor/EncryptorImplTest.java | 66 +++-- .../DataSourceLoaderCacheImplTest.java | 11 +- .../service/DataSourceServiceImplTest.java | 235 +++++++-------- ...enSearchDataSourceMetadataStorageTest.java | 268 ++++++++---------- .../TransportCreateDataSourceActionTest.java | 38 ++- .../TransportDeleteDataSourceActionTest.java | 34 +-- .../TransportGetDataSourceActionTest.java | 49 ++-- .../TransportUpdateDataSourceActionTest.java | 37 ++- .../sql/datasources/utils/SchedulerTest.java | 17 +- .../utils/XContentParserUtilsTest.java | 32 ++- 40 files changed, 813 insertions(+), 826 deletions(-) diff --git a/build.gradle b/build.gradle index a5beb0bc5d9..8c6b7d4bc04 100644 --- a/build.gradle +++ b/build.gradle @@ -84,14 +84,8 @@ repositories { spotless { java { target fileTree('.') { - include 'core/src/main/java/org/opensearch/sql/DataSourceSchemaName.java', - 'core/src/test/java/org/opensearch/sql/data/**/*.java', - 'core/src/test/java/org/opensearch/sql/config/**/*.java', - 'core/src/test/java/org/opensearch/sql/analysis/**/*.java', - 'core/src/main/java/org/opensearch/sql/planner/**/*.java', - 'core/src/main/java/org/opensearch/sql/storage/**/*.java', - 'core/src/main/java/org/opensearch/sql/utils/**/*.java', - 'core/src/main/java/org/opensearch/sql/monitor/**/*.java' + include 'datasources/**/*.java', + 'core/**/*.java' exclude '**/build/**', '**/build-*/**' } importOrder() diff --git a/datasources/build.gradle b/datasources/build.gradle index ef52db23055..830fadbc354 100644 --- a/datasources/build.gradle +++ b/datasources/build.gradle @@ -31,6 +31,9 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2' } +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + test { useJUnitPlatform() testLogging { diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java index 715e72c0c3b..b6581608bf8 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/AuthenticationType.java @@ -12,8 +12,8 @@ import java.util.Map; public enum AuthenticationType { - - BASICAUTH("basicauth"), AWSSIGV4AUTH("awssigv4"); + BASICAUTH("basicauth"), + AWSSIGV4AUTH("awssigv4"); private String name; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java index adcfb0bdfde..75d0ec85396 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelper.java @@ -8,9 +8,8 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for datasource authorization helper. - * The implementation of this class helps in determining - * if authorization is required and the roles associated with the user. + * Interface for datasource authorization helper. The implementation of this class helps in + * determining if authorization is required and the roles associated with the user. */ public interface DataSourceUserAuthorizationHelper { diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java index cd55991d006..67d747f0bff 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImpl.java @@ -19,36 +19,39 @@ public class DataSourceUserAuthorizationHelperImpl implements DataSourceUserAuth private final Client client; private Boolean isAuthorizationRequired() { - String userString = client.threadPool() - .getThreadContext().getTransient( - ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); + String userString = + client + .threadPool() + .getThreadContext() + .getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); return userString != null; } private List getUserRoles() { - String userString = client.threadPool() - .getThreadContext().getTransient( - ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); + String userString = + client + .threadPool() + .getThreadContext() + .getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); User user = User.parse(userString); return user.getRoles(); } - @Override public void authorizeDataSource(DataSourceMetadata dataSourceMetadata) { if (isAuthorizationRequired() && !dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { boolean isAuthorized = false; for (String role : getUserRoles()) { - if (dataSourceMetadata.getAllowedRoles().contains(role) - || role.equals("all_access")) { + if (dataSourceMetadata.getAllowedRoles().contains(role) || role.equals("all_access")) { isAuthorized = true; break; } } if (!isAuthorized) { throw new SecurityException( - String.format("User is not authorized to access datasource %s. " + String.format( + "User is not authorized to access datasource %s. " + "User should be mapped to any of the roles in %s for access.", dataSourceMetadata.getName(), dataSourceMetadata.getAllowedRoles().toString())); } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java index 578b66d0ba9..4572b45f539 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/Encryptor.java @@ -24,5 +24,4 @@ public interface Encryptor { * @return String plainText. */ String decrypt(String encryptedText); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java index 98f693eca19..c6abe78394d 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/encryptor/EncryptorImpl.java @@ -25,32 +25,40 @@ public class EncryptorImpl implements Encryptor { @Override public String encrypt(String plainText) { validate(masterKey); - final AwsCrypto crypto = AwsCrypto.builder() - .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) - .build(); + final AwsCrypto crypto = + AwsCrypto.builder() + .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) + .build(); - JceMasterKey jceMasterKey - = JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", - "opensearch.config.master.key", "AES/GCM/NoPadding"); + JceMasterKey jceMasterKey = + JceMasterKey.getInstance( + new SecretKeySpec(masterKey.getBytes(), "AES"), + "Custom", + "opensearch.config.master.key", + "AES/GCM/NoPadding"); - final CryptoResult encryptResult = crypto.encryptData(jceMasterKey, - plainText.getBytes(StandardCharsets.UTF_8)); + final CryptoResult encryptResult = + crypto.encryptData(jceMasterKey, plainText.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptResult.getResult()); } @Override public String decrypt(String encryptedText) { validate(masterKey); - final AwsCrypto crypto = AwsCrypto.builder() - .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) - .build(); + final AwsCrypto crypto = + AwsCrypto.builder() + .withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt) + .build(); - JceMasterKey jceMasterKey - = JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", - "opensearch.config.master.key", "AES/GCM/NoPadding"); + JceMasterKey jceMasterKey = + JceMasterKey.getInstance( + new SecretKeySpec(masterKey.getBytes(), "AES"), + "Custom", + "opensearch.config.master.key", + "AES/GCM/NoPadding"); - final CryptoResult decryptedResult - = crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText)); + final CryptoResult decryptedResult = + crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText)); return new String(decryptedResult.getResult()); } @@ -65,6 +73,4 @@ private void validate(String masterKey) { + "admin/datasources.rst#master-key-config-for-encrypting-credential-information"); } } - - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java index 484b0b92b29..40b601000c5 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/DataSourceNotFoundException.java @@ -7,12 +7,9 @@ package org.opensearch.sql.datasources.exceptions; -/** - * DataSourceNotFoundException. - */ +/** DataSourceNotFoundException. */ public class DataSourceNotFoundException extends RuntimeException { public DataSourceNotFoundException(String message) { super(message); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java index 265b3ddf310..386eb780cd7 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/exceptions/ErrorMessage.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.datasources.exceptions; import com.google.gson.Gson; @@ -12,27 +11,20 @@ import lombok.Getter; import org.opensearch.core.rest.RestStatus; -/** - * Error Message. - */ +/** Error Message. */ public class ErrorMessage { protected Throwable exception; private final int status; - @Getter - private final String type; + @Getter private final String type; - @Getter - private final String reason; + @Getter private final String reason; - @Getter - private final String details; + @Getter private final String details; - /** - * Error Message Constructor. - */ + /** Error Message Constructor. */ public ErrorMessage(Throwable exception, int status) { this.exception = exception; this.status = status; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java index 0cbb2355cae..b01d5b40ddc 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.datasources.model.transport; - import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; import java.io.IOException; @@ -17,15 +16,11 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.sql.datasource.model.DataSourceMetadata; -public class CreateDataSourceActionRequest - extends ActionRequest { +public class CreateDataSourceActionRequest extends ActionRequest { - @Getter - private DataSourceMetadata dataSourceMetadata; + @Getter private DataSourceMetadata dataSourceMetadata; - /** - * Constructor of CreateDataSourceActionRequest from StreamInput. - */ + /** Constructor of CreateDataSourceActionRequest from StreamInput. */ public CreateDataSourceActionRequest(StreamInput in) throws IOException { super(in); } @@ -38,9 +33,8 @@ public CreateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) { public ActionRequestValidationException validate() { if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java index 4ed0464a25e..aeb1e2d3d9a 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionResponse.java @@ -15,11 +15,9 @@ import org.opensearch.core.common.io.stream.StreamOutput; @RequiredArgsConstructor -public class CreateDataSourceActionResponse - extends ActionResponse { +public class CreateDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public CreateDataSourceActionResponse(StreamInput in) throws IOException { super(in); diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java index 1eb2d17bff3..d6e3bcb3f9c 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java @@ -18,8 +18,7 @@ public class DeleteDataSourceActionRequest extends ActionRequest { - @Getter - private String dataSourceName; + @Getter private String dataSourceName; /** Constructor of DeleteDataSourceActionRequest from StreamInput. */ public DeleteDataSourceActionRequest(StreamInput in) throws IOException { @@ -34,18 +33,15 @@ public DeleteDataSourceActionRequest(String dataSourceName) { public ActionRequestValidationException validate() { if (StringUtils.isEmpty(this.dataSourceName)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError("Datasource Name cannot be empty or null"); + exception.addValidationError("Datasource Name cannot be empty or null"); return exception; } else if (this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java index ec57c4aee7b..d8c29c2a670 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionResponse.java @@ -17,8 +17,7 @@ @RequiredArgsConstructor public class DeleteDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public DeleteDataSourceActionResponse(StreamInput in) throws IOException { super(in); @@ -29,5 +28,4 @@ public DeleteDataSourceActionResponse(StreamInput in) throws IOException { public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeString(result); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java index 23f48985439..2d9a4de35a6 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java @@ -19,12 +19,9 @@ @NoArgsConstructor public class GetDataSourceActionRequest extends ActionRequest { - @Getter - private String dataSourceName; + @Getter private String dataSourceName; - /** - * Constructor of GetDataSourceActionRequest from StreamInput. - */ + /** Constructor of GetDataSourceActionRequest from StreamInput. */ public GetDataSourceActionRequest(StreamInput in) throws IOException { super(in); } @@ -37,13 +34,11 @@ public GetDataSourceActionRequest(String dataSourceName) { public ActionRequestValidationException validate() { if (this.dataSourceName != null && this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java index dccb3e9b524..ac8d5d4c626 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionResponse.java @@ -17,8 +17,7 @@ @RequiredArgsConstructor public class GetDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public GetDataSourceActionResponse(StreamInput in) throws IOException { super(in); @@ -29,5 +28,4 @@ public GetDataSourceActionResponse(StreamInput in) throws IOException { public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeString(result); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java index 11bc2d1e20a..b502f348e23 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionRequest.java @@ -7,7 +7,6 @@ package org.opensearch.sql.datasources.model.transport; - import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; import java.io.IOException; @@ -17,11 +16,9 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.sql.datasource.model.DataSourceMetadata; -public class UpdateDataSourceActionRequest - extends ActionRequest { +public class UpdateDataSourceActionRequest extends ActionRequest { - @Getter - private DataSourceMetadata dataSourceMetadata; + @Getter private DataSourceMetadata dataSourceMetadata; /** Constructor of UpdateDataSourceActionRequest from StreamInput. */ public UpdateDataSourceActionRequest(StreamInput in) throws IOException { @@ -36,9 +33,8 @@ public UpdateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) { public ActionRequestValidationException validate() { if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) { ActionRequestValidationException exception = new ActionRequestValidationException(); - exception - .addValidationError( - "Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); + exception.addValidationError( + "Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); return exception; } else { return null; diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java index 88e8c41ea98..0be992d067f 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/model/transport/UpdateDataSourceActionResponse.java @@ -15,11 +15,9 @@ import org.opensearch.core.common.io.stream.StreamOutput; @RequiredArgsConstructor -public class UpdateDataSourceActionResponse - extends ActionResponse { +public class UpdateDataSourceActionResponse extends ActionResponse { - @Getter - private final String result; + @Getter private final String result; public UpdateDataSourceActionResponse(StreamInput in) throws IOException { super(in); diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java index 15735b945a8..b5929d0f202 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java @@ -47,7 +47,6 @@ import org.opensearch.sql.datasources.utils.Scheduler; import org.opensearch.sql.datasources.utils.XContentParserUtils; - public class RestDataSourceQueryAction extends BaseRestHandler { public static final String DATASOURCE_ACTIONS = "datasource_actions"; @@ -83,8 +82,9 @@ public List routes() { * Response body: * Ref [org.opensearch.sql.plugin.transport.datasource.model.GetDataSourceActionResponse] */ - new Route(GET, String.format(Locale.ROOT, "%s/{%s}", - BASE_DATASOURCE_ACTION_URL, "dataSourceName")), + new Route( + GET, + String.format(Locale.ROOT, "%s/{%s}", BASE_DATASOURCE_ACTION_URL, "dataSourceName")), new Route(GET, BASE_DATASOURCE_ACTION_URL), /* @@ -107,9 +107,9 @@ public List routes() { * Response body: Ref * [org.opensearch.sql.plugin.transport.datasource.model.DeleteDataSourceActionResponse] */ - new Route(DELETE, String.format(Locale.ROOT, "%s/{%s}", - BASE_DATASOURCE_ACTION_URL, "dataSourceName")) - ); + new Route( + DELETE, + String.format(Locale.ROOT, "%s/{%s}", BASE_DATASOURCE_ACTION_URL, "dataSourceName"))); } @Override @@ -125,101 +125,125 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient case DELETE: return executeDeleteRequest(restRequest, nodeClient); default: - return restChannel - -> restChannel.sendResponse(new BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, - String.valueOf(restRequest.method()))); + return restChannel -> + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.METHOD_NOT_ALLOWED, String.valueOf(restRequest.method()))); } } - private RestChannelConsumer executePostRequest(RestRequest restRequest, - NodeClient nodeClient) throws IOException { - - DataSourceMetadata dataSourceMetadata - = XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportCreateDataSourceAction.ACTION_TYPE, - new CreateDataSourceActionRequest(dataSourceMetadata), - new ActionListener<>() { - @Override - public void onResponse( - CreateDataSourceActionResponse createDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.CREATED, "application/json; charset=UTF-8", - createDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + private RestChannelConsumer executePostRequest(RestRequest restRequest, NodeClient nodeClient) + throws IOException { + + DataSourceMetadata dataSourceMetadata = + XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportCreateDataSourceAction.ACTION_TYPE, + new CreateDataSourceActionRequest(dataSourceMetadata), + new ActionListener<>() { + @Override + public void onResponse( + CreateDataSourceActionResponse createDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.CREATED, + "application/json; charset=UTF-8", + createDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeGetRequest(RestRequest restRequest, - NodeClient nodeClient) { + private RestChannelConsumer executeGetRequest(RestRequest restRequest, NodeClient nodeClient) { String dataSourceName = restRequest.param("dataSourceName"); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportGetDataSourceAction.ACTION_TYPE, - new GetDataSourceActionRequest(dataSourceName), - new ActionListener<>() { - @Override - public void onResponse(GetDataSourceActionResponse getDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.OK, "application/json; charset=UTF-8", - getDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportGetDataSourceAction.ACTION_TYPE, + new GetDataSourceActionRequest(dataSourceName), + new ActionListener<>() { + @Override + public void onResponse( + GetDataSourceActionResponse getDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.OK, + "application/json; charset=UTF-8", + getDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeUpdateRequest(RestRequest restRequest, - NodeClient nodeClient) throws IOException { - DataSourceMetadata dataSourceMetadata - = XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportUpdateDataSourceAction.ACTION_TYPE, - new UpdateDataSourceActionRequest(dataSourceMetadata), - new ActionListener<>() { - @Override - public void onResponse( - UpdateDataSourceActionResponse updateDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.OK, "application/json; charset=UTF-8", - updateDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + private RestChannelConsumer executeUpdateRequest(RestRequest restRequest, NodeClient nodeClient) + throws IOException { + DataSourceMetadata dataSourceMetadata = + XContentParserUtils.toDataSourceMetadata(restRequest.contentParser()); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportUpdateDataSourceAction.ACTION_TYPE, + new UpdateDataSourceActionRequest(dataSourceMetadata), + new ActionListener<>() { + @Override + public void onResponse( + UpdateDataSourceActionResponse updateDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.OK, + "application/json; charset=UTF-8", + updateDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } - private RestChannelConsumer executeDeleteRequest(RestRequest restRequest, - NodeClient nodeClient) { + private RestChannelConsumer executeDeleteRequest(RestRequest restRequest, NodeClient nodeClient) { String dataSourceName = restRequest.param("dataSourceName"); - return restChannel -> Scheduler.schedule(nodeClient, - () -> nodeClient.execute(TransportDeleteDataSourceAction.ACTION_TYPE, - new DeleteDataSourceActionRequest(dataSourceName), - new ActionListener<>() { - @Override - public void onResponse( - DeleteDataSourceActionResponse deleteDataSourceActionResponse) { - restChannel.sendResponse( - new BytesRestResponse(RestStatus.NO_CONTENT, "application/json; charset=UTF-8", - deleteDataSourceActionResponse.getResult())); - } - - @Override - public void onFailure(Exception e) { - handleException(e, restChannel); - } - })); + return restChannel -> + Scheduler.schedule( + nodeClient, + () -> + nodeClient.execute( + TransportDeleteDataSourceAction.ACTION_TYPE, + new DeleteDataSourceActionRequest(dataSourceName), + new ActionListener<>() { + @Override + public void onResponse( + DeleteDataSourceActionResponse deleteDataSourceActionResponse) { + restChannel.sendResponse( + new BytesRestResponse( + RestStatus.NO_CONTENT, + "application/json; charset=UTF-8", + deleteDataSourceActionResponse.getResult())); + } + + @Override + public void onFailure(Exception e) { + handleException(e, restChannel); + } + })); } private void handleException(Exception e, RestChannel restChannel) { @@ -240,8 +264,7 @@ private void handleException(Exception e, RestChannel restChannel) { private void reportError(final RestChannel channel, final Exception e, final RestStatus status) { channel.sendResponse( - new BytesRestResponse( - status, new ErrorMessage(e, status.getStatus()).toString())); + new BytesRestResponse(status, new ErrorMessage(e, status.getStatus()).toString())); } private static boolean isClientError(Exception e) { @@ -250,5 +273,4 @@ private static boolean isClientError(Exception e) { || e instanceof IllegalArgumentException || e instanceof IllegalStateException; } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java index 3fe2954c129..dbcc321b3f2 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCache.java @@ -4,8 +4,8 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for DataSourceLoaderCache which provides methods for - * fetch, loading and invalidating DataSource cache. + * Interface for DataSourceLoaderCache which provides methods for fetch, loading and invalidating + * DataSource cache. */ public interface DataSourceLoaderCache { @@ -16,5 +16,4 @@ public interface DataSourceLoaderCache { * @return {@link DataSource} */ DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java index ba9520fc0cd..44454dbd381 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImpl.java @@ -12,10 +12,9 @@ import org.opensearch.sql.storage.DataSourceFactory; /** - * Default implementation of DataSourceLoaderCache. This implementation - * utilizes Google Guava Cache {@link Cache} for caching DataSource objects - * against {@link DataSourceMetadata}. Expires the cache objects every 24 hrs after - * the last access. + * Default implementation of DataSourceLoaderCache. This implementation utilizes Google Guava Cache + * {@link Cache} for caching DataSource objects against {@link DataSourceMetadata}. Expires the + * cache objects every 24 hrs after the last access. */ public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { private final Map dataSourceFactoryMap; @@ -27,24 +26,24 @@ public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { * @param dataSourceFactorySet set of {@link DataSourceFactory}. */ public DataSourceLoaderCacheImpl(Set dataSourceFactorySet) { - this.dataSourceFactoryMap = dataSourceFactorySet.stream() - .collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); - this.dataSourceCache = CacheBuilder.newBuilder() - .maximumSize(1000) - .expireAfterAccess(24, TimeUnit.HOURS) - .build(); + this.dataSourceFactoryMap = + dataSourceFactorySet.stream() + .collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); + this.dataSourceCache = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(24, TimeUnit.HOURS).build(); } @Override public DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata) { DataSource dataSource = this.dataSourceCache.getIfPresent(dataSourceMetadata); if (dataSource == null) { - dataSource = this.dataSourceFactoryMap.get(dataSourceMetadata.getConnector()) - .createDataSource(dataSourceMetadata); + dataSource = + this.dataSourceFactoryMap + .get(dataSourceMetadata.getConnector()) + .createDataSource(dataSourceMetadata); this.dataSourceCache.put(dataSourceMetadata, dataSource); return dataSource; } return dataSource; } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java index e6483900c64..4d59c68fa0a 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceMetadataStorage.java @@ -13,29 +13,26 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; /** - * Interface for DataSourceMetadata Storage - * which will be only used by DataSourceService for Storage. + * Interface for DataSourceMetadata Storage which will be only used by DataSourceService for + * Storage. */ public interface DataSourceMetadataStorage { /** - * Returns all dataSource Metadata objects. The returned objects won't contain - * any of the credential info. + * Returns all dataSource Metadata objects. The returned objects won't contain any of the + * credential info. * * @return list of {@link DataSourceMetadata}. */ List getDataSourceMetadata(); - /** - * Gets {@link DataSourceMetadata} corresponding to the - * datasourceName from underlying storage. + * Gets {@link DataSourceMetadata} corresponding to the datasourceName from underlying storage. * * @param datasourceName name of the {@link DataSource}. */ Optional getDataSourceMetadata(String datasourceName); - /** * Stores {@link DataSourceMetadata} in underlying storage. * @@ -43,7 +40,6 @@ public interface DataSourceMetadataStorage { */ void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata); - /** * Updates {@link DataSourceMetadata} in underlying storage. * @@ -51,13 +47,10 @@ public interface DataSourceMetadataStorage { */ void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata); - /** - * Deletes {@link DataSourceMetadata} corresponding to the - * datasourceName from underlying storage. + * Deletes {@link DataSourceMetadata} corresponding to the datasourceName from underlying storage. * * @param datasourceName name of the {@link DataSource}. */ void deleteDataSourceMetadata(String datasourceName); - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java index 86afa90c2ba..2ac480bbf2e 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/service/DataSourceServiceImpl.java @@ -41,13 +41,11 @@ public class DataSourceServiceImpl implements DataSourceService { private final DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; - /** - * Construct from the set of {@link DataSourceFactory} at bootstrap time. - */ - public DataSourceServiceImpl(Set dataSourceFactories, - DataSourceMetadataStorage dataSourceMetadataStorage, - DataSourceUserAuthorizationHelper - dataSourceUserAuthorizationHelper) { + /** Construct from the set of {@link DataSourceFactory} at bootstrap time. */ + public DataSourceServiceImpl( + Set dataSourceFactories, + DataSourceMetadataStorage dataSourceMetadataStorage, + DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper) { this.dataSourceMetadataStorage = dataSourceMetadataStorage; this.dataSourceUserAuthorizationHelper = dataSourceUserAuthorizationHelper; this.dataSourceLoaderCache = new DataSourceLoaderCacheImpl(dataSourceFactories); @@ -55,8 +53,8 @@ public DataSourceServiceImpl(Set dataSourceFactories, @Override public Set getDataSourceMetadata(boolean isDefaultDataSourceRequired) { - List dataSourceMetadataList - = this.dataSourceMetadataStorage.getDataSourceMetadata(); + List dataSourceMetadataList = + this.dataSourceMetadataStorage.getDataSourceMetadata(); Set dataSourceMetadataSet = new HashSet<>(dataSourceMetadataList); if (isDefaultDataSourceRequired) { dataSourceMetadataSet.add(DataSourceMetadata.defaultOpenSearchDataSourceMetadata()); @@ -67,28 +65,26 @@ public Set getDataSourceMetadata(boolean isDefaultDataSource @Override public DataSourceMetadata getDataSourceMetadata(String datasourceName) { - Optional dataSourceMetadataOptional - = getDataSourceMetadataFromName(datasourceName); + Optional dataSourceMetadataOptional = + getDataSourceMetadataFromName(datasourceName); if (dataSourceMetadataOptional.isEmpty()) { - throw new IllegalArgumentException("DataSource with name: " + datasourceName - + " doesn't exist."); + throw new IllegalArgumentException( + "DataSource with name: " + datasourceName + " doesn't exist."); } removeAuthInfo(dataSourceMetadataOptional.get()); return dataSourceMetadataOptional.get(); } - @Override public DataSource getDataSource(String dataSourceName) { - Optional - dataSourceMetadataOptional = getDataSourceMetadataFromName(dataSourceName); + Optional dataSourceMetadataOptional = + getDataSourceMetadataFromName(dataSourceName); if (dataSourceMetadataOptional.isEmpty()) { throw new DataSourceNotFoundException( String.format("DataSource with name %s doesn't exist.", dataSourceName)); } else { DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); return dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); } } @@ -130,7 +126,6 @@ public Boolean dataSourceExists(String dataSourceName) { || this.dataSourceMetadataStorage.getDataSourceMetadata(dataSourceName).isPresent(); } - /** * This can be moved to a different validator class when we introduce more connectors. * @@ -159,7 +154,6 @@ private Optional getDataSourceMetadataFromName(String dataSo } } - // It is advised to avoid sending any kind credential // info in api response from security point of view. private void removeAuthInfo(Set dataSourceMetadataSet) { @@ -167,11 +161,8 @@ private void removeAuthInfo(Set dataSourceMetadataSet) { } private void removeAuthInfo(DataSourceMetadata dataSourceMetadata) { - HashMap safeProperties - = new HashMap<>(dataSourceMetadata.getProperties()); - safeProperties - .entrySet() - .removeIf(entry -> entry.getKey().contains("auth")); + HashMap safeProperties = new HashMap<>(dataSourceMetadata.getProperties()); + safeProperties.entrySet().removeIf(entry -> entry.getKey().contains("auth")); dataSourceMetadata.setProperties(safeProperties); } } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java b/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java index 0a9e42e2cdd..73eb297feac 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorage.java @@ -55,8 +55,8 @@ public class OpenSearchDataSourceMetadataStorage implements DataSourceMetadataSt private static final String DATASOURCE_INDEX_MAPPING_FILE_NAME = "datasources-index-mapping.yml"; private static final Integer DATASOURCE_QUERY_RESULT_SIZE = 10000; - private static final String DATASOURCE_INDEX_SETTINGS_FILE_NAME - = "datasources-index-settings.yml"; + private static final String DATASOURCE_INDEX_SETTINGS_FILE_NAME = + "datasources-index-settings.yml"; private static final Logger LOG = LogManager.getLogger(); private final Client client; private final ClusterService clusterService; @@ -64,15 +64,15 @@ public class OpenSearchDataSourceMetadataStorage implements DataSourceMetadataSt private final Encryptor encryptor; /** - * This class implements DataSourceMetadataStorage interface - * using OpenSearch as underlying storage. + * This class implements DataSourceMetadataStorage interface using OpenSearch as underlying + * storage. * - * @param client opensearch NodeClient. + * @param client opensearch NodeClient. * @param clusterService ClusterService. - * @param encryptor Encryptor. + * @param encryptor Encryptor. */ - public OpenSearchDataSourceMetadataStorage(Client client, ClusterService clusterService, - Encryptor encryptor) { + public OpenSearchDataSourceMetadataStorage( + Client client, ClusterService clusterService, Encryptor encryptor) { this.client = client; this.clusterService = clusterService; this.encryptor = encryptor; @@ -93,8 +93,7 @@ public Optional getDataSourceMetadata(String datasourceName) createDataSourcesIndex(); return Optional.empty(); } - return searchInDataSourcesIndex(QueryBuilders.termQuery("name", datasourceName)) - .stream() + return searchInDataSourcesIndex(QueryBuilders.termQuery("name", datasourceName)).stream() .findFirst() .map(x -> this.encryptDecryptAuthenticationData(x, false)); } @@ -111,14 +110,14 @@ public void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); ActionFuture indexResponseActionFuture; IndexResponse indexResponse; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { indexRequest.source(XContentParserUtils.convertToXContent(dataSourceMetadata)); indexResponseActionFuture = client.index(indexRequest); indexResponse = indexResponseActionFuture.actionGet(); } catch (VersionConflictEngineException exception) { - throw new IllegalArgumentException("A datasource already exists with name: " - + dataSourceMetadata.getName()); + throw new IllegalArgumentException( + "A datasource already exists with name: " + dataSourceMetadata.getName()); } catch (Exception e) { throw new RuntimeException(e); } @@ -126,27 +125,27 @@ public void createDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { if (indexResponse.getResult().equals(DocWriteResponse.Result.CREATED)) { LOG.debug("DatasourceMetadata : {} successfully created", dataSourceMetadata.getName()); } else { - throw new RuntimeException("Saving dataSource metadata information failed with result : " - + indexResponse.getResult().getLowercase()); + throw new RuntimeException( + "Saving dataSource metadata information failed with result : " + + indexResponse.getResult().getLowercase()); } } @Override public void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { encryptDecryptAuthenticationData(dataSourceMetadata, true); - UpdateRequest updateRequest - = new UpdateRequest(DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); + UpdateRequest updateRequest = + new UpdateRequest(DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); UpdateResponse updateResponse; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { updateRequest.doc(XContentParserUtils.convertToXContent(dataSourceMetadata)); updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - ActionFuture updateResponseActionFuture - = client.update(updateRequest); + ActionFuture updateResponseActionFuture = client.update(updateRequest); updateResponse = updateResponseActionFuture.actionGet(); } catch (DocumentMissingException exception) { - throw new DataSourceNotFoundException("Datasource with name: " - + dataSourceMetadata.getName() + " doesn't exist"); + throw new DataSourceNotFoundException( + "Datasource with name: " + dataSourceMetadata.getName() + " doesn't exist"); } catch (Exception e) { throw new RuntimeException(e); } @@ -154,8 +153,9 @@ public void updateDataSourceMetadata(DataSourceMetadata dataSourceMetadata) { if (updateResponse.getResult().equals(DocWriteResponse.Result.UPDATED)) { LOG.debug("DatasourceMetadata : {} successfully updated", dataSourceMetadata.getName()); } else { - throw new RuntimeException("Saving dataSource metadata information failed with result : " - + updateResponse.getResult().getLowercase()); + throw new RuntimeException( + "Saving dataSource metadata information failed with result : " + + updateResponse.getResult().getLowercase()); } } @@ -164,48 +164,54 @@ public void deleteDataSourceMetadata(String datasourceName) { DeleteRequest deleteRequest = new DeleteRequest(DATASOURCE_INDEX_NAME); deleteRequest.id(datasourceName); ActionFuture deleteResponseActionFuture; - try (ThreadContext.StoredContext storedContext = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext storedContext = + client.threadPool().getThreadContext().stashContext()) { deleteResponseActionFuture = client.delete(deleteRequest); } DeleteResponse deleteResponse = deleteResponseActionFuture.actionGet(); if (deleteResponse.getResult().equals(DocWriteResponse.Result.DELETED)) { LOG.debug("DatasourceMetadata : {} successfully deleted", datasourceName); } else if (deleteResponse.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) { - throw new DataSourceNotFoundException("Datasource with name: " - + datasourceName + " doesn't exist"); + throw new DataSourceNotFoundException( + "Datasource with name: " + datasourceName + " doesn't exist"); } else { - throw new RuntimeException("Deleting dataSource metadata information failed with result : " - + deleteResponse.getResult().getLowercase()); + throw new RuntimeException( + "Deleting dataSource metadata information failed with result : " + + deleteResponse.getResult().getLowercase()); } } private void createDataSourcesIndex() { try { - InputStream mappingFileStream = OpenSearchDataSourceMetadataStorage.class.getClassLoader() - .getResourceAsStream(DATASOURCE_INDEX_MAPPING_FILE_NAME); - InputStream settingsFileStream = OpenSearchDataSourceMetadataStorage.class.getClassLoader() - .getResourceAsStream(DATASOURCE_INDEX_SETTINGS_FILE_NAME); + InputStream mappingFileStream = + OpenSearchDataSourceMetadataStorage.class + .getClassLoader() + .getResourceAsStream(DATASOURCE_INDEX_MAPPING_FILE_NAME); + InputStream settingsFileStream = + OpenSearchDataSourceMetadataStorage.class + .getClassLoader() + .getResourceAsStream(DATASOURCE_INDEX_SETTINGS_FILE_NAME); CreateIndexRequest createIndexRequest = new CreateIndexRequest(DATASOURCE_INDEX_NAME); - createIndexRequest.mapping(IOUtils.toString(mappingFileStream, StandardCharsets.UTF_8), - XContentType.YAML) - .settings(IOUtils.toString(settingsFileStream, StandardCharsets.UTF_8), - XContentType.YAML); + createIndexRequest + .mapping(IOUtils.toString(mappingFileStream, StandardCharsets.UTF_8), XContentType.YAML) + .settings( + IOUtils.toString(settingsFileStream, StandardCharsets.UTF_8), XContentType.YAML); ActionFuture createIndexResponseActionFuture; - try (ThreadContext.StoredContext ignored = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext ignored = + client.threadPool().getThreadContext().stashContext()) { createIndexResponseActionFuture = client.admin().indices().create(createIndexRequest); } CreateIndexResponse createIndexResponse = createIndexResponseActionFuture.actionGet(); if (createIndexResponse.isAcknowledged()) { LOG.info("Index: {} creation Acknowledged", DATASOURCE_INDEX_NAME); } else { - throw new RuntimeException( - "Index creation is not acknowledged."); + throw new RuntimeException("Index creation is not acknowledged."); } } catch (Throwable e) { throw new RuntimeException( - "Internal server error while creating" + DATASOURCE_INDEX_NAME + " index:: " + "Internal server error while creating" + + DATASOURCE_INDEX_NAME + + " index:: " + e.getMessage()); } } @@ -217,17 +223,19 @@ private List searchInDataSourcesIndex(QueryBuilder query) { searchSourceBuilder.query(query); searchSourceBuilder.size(DATASOURCE_QUERY_RESULT_SIZE); searchRequest.source(searchSourceBuilder); - // strongly consistent reads is requred. more info https://github.com/opensearch-project/sql/issues/1801. + // strongly consistent reads is requred. more info + // https://github.com/opensearch-project/sql/issues/1801. searchRequest.preference("_primary"); ActionFuture searchResponseActionFuture; - try (ThreadContext.StoredContext ignored = client.threadPool().getThreadContext() - .stashContext()) { + try (ThreadContext.StoredContext ignored = + client.threadPool().getThreadContext().stashContext()) { searchResponseActionFuture = client.search(searchRequest); } SearchResponse searchResponse = searchResponseActionFuture.actionGet(); if (searchResponse.status().getStatus() != 200) { - throw new RuntimeException("Fetching dataSource metadata information failed with status : " - + searchResponse.status()); + throw new RuntimeException( + "Fetching dataSource metadata information failed with status : " + + searchResponse.status()); } else { List list = new ArrayList<>(); for (SearchHit searchHit : searchResponse.getHits().getHits()) { @@ -245,14 +253,15 @@ private List searchInDataSourcesIndex(QueryBuilder query) { } @SuppressWarnings("missingswitchdefault") - private DataSourceMetadata encryptDecryptAuthenticationData(DataSourceMetadata dataSourceMetadata, - Boolean isEncryption) { + private DataSourceMetadata encryptDecryptAuthenticationData( + DataSourceMetadata dataSourceMetadata, Boolean isEncryption) { Map propertiesMap = dataSourceMetadata.getProperties(); - Optional authTypeOptional - = propertiesMap.keySet().stream().filter(s -> s.endsWith("auth.type")) - .findFirst() - .map(propertiesMap::get) - .map(AuthenticationType::get); + Optional authTypeOptional = + propertiesMap.keySet().stream() + .filter(s -> s.endsWith("auth.type")) + .findFirst() + .map(propertiesMap::get) + .map(AuthenticationType::get); if (authTypeOptional.isPresent()) { switch (authTypeOptional.get()) { case BASICAUTH: @@ -266,8 +275,8 @@ private DataSourceMetadata encryptDecryptAuthenticationData(DataSourceMetadata d return dataSourceMetadata; } - private void handleBasicAuthPropertiesEncryptionDecryption(Map propertiesMap, - Boolean isEncryption) { + private void handleBasicAuthPropertiesEncryptionDecryption( + Map propertiesMap, Boolean isEncryption) { ArrayList list = new ArrayList<>(); propertiesMap.keySet().stream() .filter(s -> s.endsWith("auth.username")) @@ -280,21 +289,19 @@ private void handleBasicAuthPropertiesEncryptionDecryption(Map p encryptOrDecrypt(propertiesMap, isEncryption, list); } - private void encryptOrDecrypt(Map propertiesMap, Boolean isEncryption, - List keyIdentifiers) { + private void encryptOrDecrypt( + Map propertiesMap, Boolean isEncryption, List keyIdentifiers) { for (String key : keyIdentifiers) { if (isEncryption) { - propertiesMap.put(key, - this.encryptor.encrypt(propertiesMap.get(key))); + propertiesMap.put(key, this.encryptor.encrypt(propertiesMap.get(key))); } else { - propertiesMap.put(key, - this.encryptor.decrypt(propertiesMap.get(key))); + propertiesMap.put(key, this.encryptor.decrypt(propertiesMap.get(key))); } } } - private void handleSigV4PropertiesEncryptionDecryption(Map propertiesMap, - Boolean isEncryption) { + private void handleSigV4PropertiesEncryptionDecryption( + Map propertiesMap, Boolean isEncryption) { ArrayList list = new ArrayList<>(); propertiesMap.keySet().stream() .filter(s -> s.endsWith("auth.access_key")) @@ -306,5 +313,4 @@ private void handleSigV4PropertiesEncryptionDecryption(Map prope .ifPresent(list::add); encryptOrDecrypt(propertiesMap, isEncryption, list); } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java index fefd0250ce0..54ca92b6954 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceAction.java @@ -23,38 +23,44 @@ public class TransportCreateDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/create"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, CreateDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, CreateDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportCreateDataSourceAction action for creating datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportCreateDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportCreateDataSourceAction.NAME, transportService, actionFilters, + public TransportCreateDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportCreateDataSourceAction.NAME, + transportService, + actionFilters, CreateDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, CreateDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + CreateDataSourceActionRequest request, + ActionListener actionListener) { try { DataSourceMetadata dataSourceMetadata = request.getDataSourceMetadata(); dataSourceService.createDataSource(dataSourceMetadata); - actionListener.onResponse(new CreateDataSourceActionResponse("Created DataSource with name " - + dataSourceMetadata.getName())); + actionListener.onResponse( + new CreateDataSourceActionResponse( + "Created DataSource with name " + dataSourceMetadata.getName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java index 39e51aabefd..5578d406510 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceAction.java @@ -23,37 +23,43 @@ public class TransportDeleteDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/delete"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, DeleteDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, DeleteDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportDeleteDataSourceAction action for deleting datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportDeleteDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportDeleteDataSourceAction.NAME, transportService, actionFilters, + public TransportDeleteDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportDeleteDataSourceAction.NAME, + transportService, + actionFilters, DeleteDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, DeleteDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + DeleteDataSourceActionRequest request, + ActionListener actionListener) { try { dataSourceService.deleteDataSource(request.getDataSourceName()); - actionListener.onResponse(new DeleteDataSourceActionResponse("Deleted DataSource with name " - + request.getDataSourceName())); + actionListener.onResponse( + new DeleteDataSourceActionResponse( + "Deleted DataSource with name " + request.getDataSourceName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java index 477d10fa0b3..34ad59c80fe 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceAction.java @@ -26,30 +26,36 @@ public class TransportGetDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/read"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, GetDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, GetDataSourceActionResponse::new); private DataSourceService dataSourceService; /** * TransportGetDataSourceAction action for getting datasource. * - * @param transportService transportService. - * @param actionFilters actionFilters. + * @param transportService transportService. + * @param actionFilters actionFilters. * @param dataSourceService dataSourceService. */ @Inject - public TransportGetDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportGetDataSourceAction.NAME, transportService, actionFilters, + public TransportGetDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportGetDataSourceAction.NAME, + transportService, + actionFilters, GetDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, GetDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + GetDataSourceActionRequest request, + ActionListener actionListener) { try { String responseContent; if (request.getDataSourceName() == null) { @@ -66,30 +72,27 @@ protected void doExecute(Task task, GetDataSourceActionRequest request, private String handleGetAllDataSourcesRequest() { String responseContent; - Set dataSourceMetadataSet = - dataSourceService.getDataSourceMetadata(false); - responseContent = new JsonResponseFormatter>( - JsonResponseFormatter.Style.PRETTY) { - @Override - protected Object buildJsonObject(Set response) { - return response; - } - }.format(dataSourceMetadataSet); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(false); + responseContent = + new JsonResponseFormatter>(JsonResponseFormatter.Style.PRETTY) { + @Override + protected Object buildJsonObject(Set response) { + return response; + } + }.format(dataSourceMetadataSet); return responseContent; } private String handleSingleDataSourceRequest(String datasourceName) { String responseContent; - DataSourceMetadata dataSourceMetadata - = dataSourceService - .getDataSourceMetadata(datasourceName); - responseContent = new JsonResponseFormatter( - JsonResponseFormatter.Style.PRETTY) { - @Override - protected Object buildJsonObject(DataSourceMetadata response) { - return response; - } - }.format(dataSourceMetadata); + DataSourceMetadata dataSourceMetadata = dataSourceService.getDataSourceMetadata(datasourceName); + responseContent = + new JsonResponseFormatter(JsonResponseFormatter.Style.PRETTY) { + @Override + protected Object buildJsonObject(DataSourceMetadata response) { + return response; + } + }.format(dataSourceMetadata); return responseContent; } } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java index e01a8ffea27..4325282f83e 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceAction.java @@ -23,8 +23,8 @@ public class TransportUpdateDataSourceAction extends HandledTransportAction { public static final String NAME = "cluster:admin/opensearch/ql/datasources/update"; - public static final ActionType - ACTION_TYPE = new ActionType<>(NAME, UpdateDataSourceActionResponse::new); + public static final ActionType ACTION_TYPE = + new ActionType<>(NAME, UpdateDataSourceActionResponse::new); private DataSourceService dataSourceService; @@ -36,24 +36,30 @@ public class TransportUpdateDataSourceAction * @param dataSourceService dataSourceService. */ @Inject - public TransportUpdateDataSourceAction(TransportService transportService, - ActionFilters actionFilters, - DataSourceServiceImpl dataSourceService) { - super(TransportUpdateDataSourceAction.NAME, transportService, actionFilters, + public TransportUpdateDataSourceAction( + TransportService transportService, + ActionFilters actionFilters, + DataSourceServiceImpl dataSourceService) { + super( + TransportUpdateDataSourceAction.NAME, + transportService, + actionFilters, UpdateDataSourceActionRequest::new); this.dataSourceService = dataSourceService; } @Override - protected void doExecute(Task task, UpdateDataSourceActionRequest request, - ActionListener actionListener) { + protected void doExecute( + Task task, + UpdateDataSourceActionRequest request, + ActionListener actionListener) { try { dataSourceService.updateDataSource(request.getDataSourceMetadata()); - actionListener.onResponse(new UpdateDataSourceActionResponse("Updated DataSource with name " - + request.getDataSourceMetadata().getName())); + actionListener.onResponse( + new UpdateDataSourceActionResponse( + "Updated DataSource with name " + request.getDataSourceMetadata().getName())); } catch (Exception e) { actionListener.onFailure(e); } } - } diff --git a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java index 38a500afaea..1ad79addace 100644 --- a/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java +++ b/datasources/src/main/java/org/opensearch/sql/datasources/utils/XContentParserUtils.java @@ -22,9 +22,7 @@ import org.opensearch.sql.datasource.model.DataSourceMetadata; import org.opensearch.sql.datasource.model.DataSourceType; -/** - * Utitlity class to serialize and deserialize objects in XContent. - */ +/** Utitlity class to serialize and deserialize objects in XContent. */ @UtilityClass public class XContentParserUtils { public static final String NAME_FIELD = "name"; @@ -87,9 +85,13 @@ public static DataSourceMetadata toDataSourceMetadata(XContentParser parser) thr * @throws IOException IOException. */ public static DataSourceMetadata toDataSourceMetadata(String json) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - json)) { + try (XContentParser parser = + XContentType.JSON + .xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + json)) { return toDataSourceMetadata(parser); } } @@ -116,6 +118,4 @@ public static XContentBuilder convertToXContent(DataSourceMetadata metadata) thr builder.endObject(); return builder; } - - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java index 23bb4688e12..4bc4800093b 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/auth/AuthenticationTypeTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.datasources.auth; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java index 552bd0edf97..6ee3c12edd9 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/auth/DataSourceUserAuthorizationHelperImplTest.java @@ -27,65 +27,76 @@ public class DataSourceUserAuthorizationHelperImplTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Client client; - @InjectMocks - private DataSourceUserAuthorizationHelperImpl dataSourceUserAuthorizationHelper; - + @InjectMocks private DataSourceUserAuthorizationHelperImpl dataSourceUserAuthorizationHelper; @Test public void testAuthorizeDataSourceWithAllowedRoles() { String userString = "myuser|bckrole1,bckrol2|prometheus_access|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithAdminRole() { String userString = "myuser|bckrole1,bckrol2|all_access|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithNullUserString() { - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(null); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithDefaultDataSource() { String userString = "myuser|bckrole1,bckrol2|role1|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = DataSourceMetadata.defaultOpenSearchDataSourceMetadata(); - this.dataSourceUserAuthorizationHelper - .authorizeDataSource(dataSourceMetadata); + this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata); } @Test public void testAuthorizeDataSourceWithException() { String userString = "myuser|bckrole1,bckrol2|role1|myTenant"; - Mockito.when(client.threadPool().getThreadContext() - .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) + Mockito.when( + client + .threadPool() + .getThreadContext() + .getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)) .thenReturn(userString); DataSourceMetadata dataSourceMetadata = dataSourceMetadata(); - SecurityException securityException - = Assert.assertThrows(SecurityException.class, + SecurityException securityException = + Assert.assertThrows( + SecurityException.class, () -> this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata)); - Assert.assertEquals("User is not authorized to access datasource test. " + Assert.assertEquals( + "User is not authorized to access datasource test. " + "User should be mapped to any of the roles in [prometheus_access] for access.", securityException.getMessage()); } @@ -98,5 +109,4 @@ private DataSourceMetadata dataSourceMetadata() { dataSourceMetadata.setProperties(new HashMap<>()); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java index d62a5a957ac..26432b139b2 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/encryptor/EncryptorImplTest.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; - @ExtendWith(MockitoExtension.class) public class EncryptorImplTest { @@ -38,9 +37,11 @@ public void testMasterKeySize() { String input = "This is a test input"; String masterKey8 = "12345678"; Encryptor encryptor8 = new EncryptorImpl(masterKey8); - assertThrows(AwsCryptoException.class, () -> { - encryptor8.encrypt(input); - }); + assertThrows( + AwsCryptoException.class, + () -> { + encryptor8.encrypt(input); + }); String masterKey16 = "1234567812345678"; Encryptor encryptor16 = new EncryptorImpl(masterKey16); @@ -54,9 +55,11 @@ public void testMasterKeySize() { String masterKey17 = "12345678123456781"; Encryptor encryptor17 = new EncryptorImpl(masterKey17); - assertThrows(AwsCryptoException.class, () -> { - encryptor17.encrypt(input); - }); + assertThrows( + AwsCryptoException.class, + () -> { + encryptor17.encrypt(input); + }); } @Test @@ -64,9 +67,11 @@ public void testInvalidBase64String() { String encrypted = "invalidBase64String"; Encryptor encryptor = new EncryptorImpl("randomMasterKey"); - assertThrows(BadCiphertextException.class, () -> { - encryptor.decrypt(encrypted); - }); + assertThrows( + BadCiphertextException.class, + () -> { + encryptor.decrypt(encrypted); + }); } @Test @@ -80,19 +85,21 @@ public void testDecryptWithDifferentKey() { String encrypted = encryptor1.encrypt(input); - assertThrows(Exception.class, () -> { - encryptor2.decrypt(encrypted); - }); + assertThrows( + Exception.class, + () -> { + encryptor2.decrypt(encrypted); + }); } @Test public void testEncryptionAndDecryptionWithNullMasterKey() { String input = "This is a test input"; Encryptor encryptor = new EncryptorImpl(null); - IllegalStateException illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.encrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + IllegalStateException illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.encrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -100,10 +107,10 @@ public void testEncryptionAndDecryptionWithNullMasterKey() { + "https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/" + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); - illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.decrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.decrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -118,10 +125,10 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { String masterKey = ""; String input = "This is a test input"; Encryptor encryptor = new EncryptorImpl(masterKey); - IllegalStateException illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.encrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + IllegalStateException illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.encrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -129,10 +136,10 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { + "https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/" + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); - illegalStateException - = Assertions.assertThrows(IllegalStateException.class, - () -> encryptor.decrypt(input)); - Assertions.assertEquals("Master key is a required config for using create and" + illegalStateException = + Assertions.assertThrows(IllegalStateException.class, () -> encryptor.decrypt(input)); + Assertions.assertEquals( + "Master key is a required config for using create and" + " update datasource APIs." + "Please set plugins.query.datasources.encryption.masterkey config " + "in opensearch.yml in all the cluster nodes. " @@ -141,5 +148,4 @@ public void testEncryptionAndDecryptionWithEmptyMasterKey() { + "admin/datasources.rst#master-key-config-for-encrypting-credential-information", illegalStateException.getMessage()); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java index bf656857b02..b2ea221eb70 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceLoaderCacheImplTest.java @@ -25,11 +25,9 @@ @ExtendWith(MockitoExtension.class) class DataSourceLoaderCacheImplTest { - @Mock - private DataSourceFactory dataSourceFactory; + @Mock private DataSourceFactory dataSourceFactory; - @Mock - private StorageEngine storageEngine; + @Mock private StorageEngine storageEngine; @BeforeEach public void setup() { @@ -55,8 +53,8 @@ void testGetOrLoadDataSource() { dataSourceMetadata.setProperties(ImmutableMap.of()); DataSource dataSource = dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); - Assertions.assertEquals(dataSource, - dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata)); + Assertions.assertEquals( + dataSource, dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata)); verifyNoMoreInteractions(dataSourceFactory); } @@ -81,5 +79,4 @@ private DataSourceMetadata getMetadata() { dataSourceMetadata.setProperties(ImmutableMap.of()); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java index e1312ec582c..56d3586c6ea 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/service/DataSourceServiceImplTest.java @@ -46,15 +46,11 @@ @ExtendWith(MockitoExtension.class) class DataSourceServiceImplTest { - @Mock - private DataSourceFactory dataSourceFactory; - @Mock - private StorageEngine storageEngine; - @Mock - private DataSourceMetadataStorage dataSourceMetadataStorage; + @Mock private DataSourceFactory dataSourceFactory; + @Mock private StorageEngine storageEngine; + @Mock private DataSourceMetadataStorage dataSourceMetadataStorage; - @Mock - private DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; + @Mock private DataSourceUserAuthorizationHelper dataSourceUserAuthorizationHelper; private DataSourceService dataSourceService; @@ -75,7 +71,8 @@ public void setup() { { add(dataSourceFactory); } - }, dataSourceMetadataStorage, + }, + dataSourceMetadataStorage, dataSourceUserAuthorizationHelper); } @@ -91,22 +88,18 @@ void testGetDataSourceForDefaultOpenSearchDataSource() { @Test void testGetDataSourceForNonExistingDataSource() { - when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.empty()); + when(dataSourceMetadataStorage.getDataSourceMetadata("test")).thenReturn(Optional.empty()); DataSourceNotFoundException exception = assertThrows( - DataSourceNotFoundException.class, - () -> - dataSourceService.getDataSource("test")); + DataSourceNotFoundException.class, () -> dataSourceService.getDataSource("test")); assertEquals("DataSource with name test doesn't exist.", exception.getMessage()); - verify(dataSourceMetadataStorage, times(1)) - .getDataSourceMetadata("test"); + verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("test"); } @Test void testGetDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("test", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("test", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); doNothing().when(dataSourceUserAuthorizationHelper).authorizeDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) .thenReturn(Optional.of(dataSourceMetadata)); @@ -114,26 +107,31 @@ void testGetDataSourceSuccessCase() { assertEquals("test", dataSource.getName()); assertEquals(DataSourceType.OPENSEARCH, dataSource.getConnectorType()); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("test"); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); } @Test void testGetDataSourceWithAuthorizationFailure() { - DataSourceMetadata dataSourceMetadata = metadata("test", DataSourceType.OPENSEARCH, - Collections.singletonList("prometheus_access"), ImmutableMap.of()); - doThrow(new SecurityException("User is not authorized to access datasource test. " - + "User should be mapped to any of the roles in [prometheus_access] for access.")) + DataSourceMetadata dataSourceMetadata = + metadata( + "test", + DataSourceType.OPENSEARCH, + Collections.singletonList("prometheus_access"), + ImmutableMap.of()); + doThrow( + new SecurityException( + "User is not authorized to access datasource test. User should be mapped to any of" + + " the roles in [prometheus_access] for access.")) .when(dataSourceUserAuthorizationHelper) .authorizeDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) .thenReturn(Optional.of(dataSourceMetadata)); - - SecurityException securityException - = Assertions.assertThrows(SecurityException.class, - () -> dataSourceService.getDataSource("test")); - Assertions.assertEquals("User is not authorized to access datasource test. " + SecurityException securityException = + Assertions.assertThrows( + SecurityException.class, () -> dataSourceService.getDataSource("test")); + Assertions.assertEquals( + "User is not authorized to access datasource test. " + "User should be mapped to any of the roles in [prometheus_access] for access.", securityException.getMessage()); @@ -141,21 +139,23 @@ void testGetDataSourceWithAuthorizationFailure() { verify(dataSourceFactory, times(0)).createDataSource(dataSourceMetadata); } - @Test void testCreateDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); dataSourceService.createDataSource(dataSourceMetadata); - verify(dataSourceMetadataStorage, times(1)) - .createDataSourceMetadata(dataSourceMetadata); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceMetadataStorage, times(1)).createDataSourceMetadata(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.ofNullable(metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()))); + .thenReturn( + Optional.ofNullable( + metadata( + "testDS", + DataSourceType.OPENSEARCH, + Collections.emptyList(), + ImmutableMap.of()))); DataSource dataSource = dataSourceService.getDataSource("testDS"); assertEquals("testDS", dataSource.getName()); assertEquals(storageEngine, dataSource.getStorageEngine()); @@ -164,14 +164,15 @@ void testCreateDataSourceSuccessCase() { @Test void testCreateDataSourceWithDisallowedDatasourceName() { - DataSourceMetadata dataSourceMetadata = metadata("testDS$$$", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata( + "testDS$$$", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("DataSource Name: testDS$$$ contains illegal characters." + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "DataSource Name: testDS$$$ contains illegal characters." + " Allowed characters: a-zA-Z0-9_-*@.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); @@ -181,14 +182,14 @@ void testCreateDataSourceWithDisallowedDatasourceName() { @Test void testCreateDataSourceWithEmptyDatasourceName() { - DataSourceMetadata dataSourceMetadata = metadata("", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("Missing Name Field from a DataSource. Name is a required parameter.", + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "Missing Name Field from a DataSource. Name is a required parameter.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); verify(dataSourceFactory, times(0)).createDataSource(dataSourceMetadata); @@ -197,14 +198,14 @@ void testCreateDataSourceWithEmptyDatasourceName() { @Test void testCreateDataSourceWithNullParameters() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), null); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), null); IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, - () -> - dataSourceService.createDataSource(dataSourceMetadata)); - assertEquals("Missing properties field in datasource configuration. " + () -> dataSourceService.createDataSource(dataSourceMetadata)); + assertEquals( + "Missing properties field in datasource configuration. " + "Properties are required parameters.", exception.getMessage()); verify(dataSourceFactory, times(1)).getDataSourceType(); @@ -219,88 +220,99 @@ void testGetDataSourceMetadataSet() { properties.put("prometheus.auth.type", "basicauth"); properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); - when(dataSourceMetadataStorage.getDataSourceMetadata()).thenReturn(new ArrayList<>() { - { - add(metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - properties)); - } - }); - Set dataSourceMetadataSet - = dataSourceService.getDataSourceMetadata(false); + when(dataSourceMetadataStorage.getDataSourceMetadata()) + .thenReturn( + new ArrayList<>() { + { + add( + metadata( + "testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), properties)); + } + }); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(false); assertEquals(1, dataSourceMetadataSet.size()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataSet.iterator().next(); assertTrue(dataSourceMetadata.getProperties().containsKey("prometheus.uri")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.type")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.username")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.password")); - assertFalse(dataSourceMetadataSet - .contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); + assertFalse( + dataSourceMetadataSet.contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata(); } @Test void testGetDataSourceMetadataSetWithDefaultDatasource() { - when(dataSourceMetadataStorage.getDataSourceMetadata()).thenReturn(new ArrayList<>() { - { - add(metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - ImmutableMap.of())); - } - }); - Set dataSourceMetadataSet - = dataSourceService.getDataSourceMetadata(true); + when(dataSourceMetadataStorage.getDataSourceMetadata()) + .thenReturn( + new ArrayList<>() { + { + add( + metadata( + "testDS", + DataSourceType.PROMETHEUS, + Collections.emptyList(), + ImmutableMap.of())); + } + }); + Set dataSourceMetadataSet = dataSourceService.getDataSourceMetadata(true); assertEquals(2, dataSourceMetadataSet.size()); - assertTrue(dataSourceMetadataSet - .contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); + assertTrue( + dataSourceMetadataSet.contains(DataSourceMetadata.defaultOpenSearchDataSourceMetadata())); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata(); } @Test void testUpdateDataSourceSuccessCase() { - DataSourceMetadata dataSourceMetadata = metadata("testDS", DataSourceType.OPENSEARCH, - Collections.emptyList(), ImmutableMap.of()); + DataSourceMetadata dataSourceMetadata = + metadata("testDS", DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); dataSourceService.updateDataSource(dataSourceMetadata); - verify(dataSourceMetadataStorage, times(1)) - .updateDataSourceMetadata(dataSourceMetadata); - verify(dataSourceFactory, times(1)) - .createDataSource(dataSourceMetadata); + verify(dataSourceMetadataStorage, times(1)).updateDataSourceMetadata(dataSourceMetadata); + verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); } @Test void testUpdateDefaultDataSource() { - DataSourceMetadata dataSourceMetadata = metadata(DEFAULT_DATASOURCE_NAME, - DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); - UnsupportedOperationException unsupportedOperationException - = assertThrows(UnsupportedOperationException.class, + DataSourceMetadata dataSourceMetadata = + metadata( + DEFAULT_DATASOURCE_NAME, + DataSourceType.OPENSEARCH, + Collections.emptyList(), + ImmutableMap.of()); + UnsupportedOperationException unsupportedOperationException = + assertThrows( + UnsupportedOperationException.class, () -> dataSourceService.updateDataSource(dataSourceMetadata)); - assertEquals("Not allowed to update default datasource :" + DEFAULT_DATASOURCE_NAME, + assertEquals( + "Not allowed to update default datasource :" + DEFAULT_DATASOURCE_NAME, unsupportedOperationException.getMessage()); } @Test void testDeleteDatasource() { dataSourceService.deleteDataSource("testDS"); - verify(dataSourceMetadataStorage, times(1)) - .deleteDataSourceMetadata("testDS"); + verify(dataSourceMetadataStorage, times(1)).deleteDataSourceMetadata("testDS"); } @Test void testDeleteDefaultDatasource() { - UnsupportedOperationException unsupportedOperationException - = assertThrows(UnsupportedOperationException.class, - () -> dataSourceService.deleteDataSource(DEFAULT_DATASOURCE_NAME)); - assertEquals("Not allowed to delete default datasource :" + DEFAULT_DATASOURCE_NAME, + UnsupportedOperationException unsupportedOperationException = + assertThrows( + UnsupportedOperationException.class, + () -> dataSourceService.deleteDataSource(DEFAULT_DATASOURCE_NAME)); + assertEquals( + "Not allowed to delete default datasource :" + DEFAULT_DATASOURCE_NAME, unsupportedOperationException.getMessage()); } @Test void testDataSourceExists() { - when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.empty()); + when(dataSourceMetadataStorage.getDataSourceMetadata("test")).thenReturn(Optional.empty()); Assertions.assertFalse(dataSourceService.dataSourceExists("test")); when(dataSourceMetadataStorage.getDataSourceMetadata("test")) - .thenReturn(Optional.of(metadata("test", DataSourceType.PROMETHEUS, - List.of(), ImmutableMap.of()))); + .thenReturn( + Optional.of(metadata("test", DataSourceType.PROMETHEUS, List.of(), ImmutableMap.of()))); Assertions.assertTrue(dataSourceService.dataSourceExists("test")); } @@ -310,9 +322,8 @@ void testDataSourceExistsForDefaultDataSource() { verifyNoInteractions(dataSourceMetadataStorage); } - DataSourceMetadata metadata(String name, DataSourceType type, - List allowedRoles, - Map properties) { + DataSourceMetadata metadata( + String name, DataSourceType type, List allowedRoles, Map properties) { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName(name); dataSourceMetadata.setConnector(type); @@ -329,13 +340,15 @@ void testRemovalOfAuthorizationInfo() { properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); DataSourceMetadata dataSourceMetadata = - new DataSourceMetadata("testDS", DataSourceType.PROMETHEUS, - Collections.singletonList("prometheus_access"), properties); + new DataSourceMetadata( + "testDS", + DataSourceType.PROMETHEUS, + Collections.singletonList("prometheus_access"), + properties); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) .thenReturn(Optional.of(dataSourceMetadata)); - DataSourceMetadata dataSourceMetadata1 - = dataSourceService.getDataSourceMetadata("testDS"); + DataSourceMetadata dataSourceMetadata1 = dataSourceService.getDataSourceMetadata("testDS"); assertEquals("testDS", dataSourceMetadata1.getName()); assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata1.getConnector()); assertFalse(dataSourceMetadata1.getProperties().containsKey("prometheus.auth.type")); @@ -345,10 +358,11 @@ void testRemovalOfAuthorizationInfo() { @Test void testGetDataSourceMetadataForNonExistingDataSource() { - when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.empty()); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, - () -> dataSourceService.getDataSourceMetadata("testDS")); + when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")).thenReturn(Optional.empty()); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> dataSourceService.getDataSourceMetadata("testDS")); assertEquals("DataSource with name: testDS doesn't exist.", exception.getMessage()); } @@ -360,16 +374,15 @@ void testGetDataSourceMetadataForSpecificDataSourceName() { properties.put("prometheus.auth.username", "username"); properties.put("prometheus.auth.password", "password"); when(dataSourceMetadataStorage.getDataSourceMetadata("testDS")) - .thenReturn(Optional.ofNullable( - metadata("testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), - properties))); - DataSourceMetadata dataSourceMetadata - = this.dataSourceService.getDataSourceMetadata("testDS"); + .thenReturn( + Optional.ofNullable( + metadata( + "testDS", DataSourceType.PROMETHEUS, Collections.emptyList(), properties))); + DataSourceMetadata dataSourceMetadata = this.dataSourceService.getDataSourceMetadata("testDS"); assertTrue(dataSourceMetadata.getProperties().containsKey("prometheus.uri")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.type")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.username")); assertFalse(dataSourceMetadata.getProperties().containsKey("prometheus.auth.password")); verify(dataSourceMetadataStorage, times(1)).getDataSourceMetadata("testDS"); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java index bab568bc323..cc663d56e6f 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/storage/OpenSearchDataSourceMetadataStorageTest.java @@ -52,33 +52,25 @@ public class OpenSearchDataSourceMetadataStorageTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Client client; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ClusterService clusterService; - @Mock - private Encryptor encryptor; + + @Mock private Encryptor encryptor; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private SearchResponse searchResponse; - @Mock - private ActionFuture searchResponseActionFuture; - @Mock - private ActionFuture createIndexResponseActionFuture; - @Mock - private ActionFuture indexResponseActionFuture; - @Mock - private IndexResponse indexResponse; - @Mock - private ActionFuture updateResponseActionFuture; - @Mock - private UpdateResponse updateResponse; - @Mock - private ActionFuture deleteResponseActionFuture; - @Mock - private DeleteResponse deleteResponse; - @Mock - private SearchHit searchHit; - @InjectMocks - private OpenSearchDataSourceMetadataStorage openSearchDataSourceMetadataStorage; + @Mock private ActionFuture searchResponseActionFuture; + @Mock private ActionFuture createIndexResponseActionFuture; + @Mock private ActionFuture indexResponseActionFuture; + @Mock private IndexResponse indexResponse; + @Mock private ActionFuture updateResponseActionFuture; + @Mock private UpdateResponse updateResponse; + @Mock private ActionFuture deleteResponseActionFuture; + @Mock private DeleteResponse deleteResponse; + @Mock private SearchHit searchHit; + @InjectMocks private OpenSearchDataSourceMetadataStorage openSearchDataSourceMetadataStorage; @SneakyThrows @Test @@ -91,28 +83,24 @@ public void testGetDataSourceMetadata() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn(getBasicDataSourceMetadataString()); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn(getBasicDataSourceMetadataString()); Mockito.when(encryptor.decrypt("password")).thenReturn("password"); Mockito.when(encryptor.decrypt("username")).thenReturn("username"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("password", - dataSourceMetadata.getProperties().get("prometheus.auth.password")); - Assertions.assertEquals("username", - dataSourceMetadata.getProperties().get("prometheus.auth.username")); - Assertions.assertEquals("basicauth", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "password", dataSourceMetadata.getProperties().get("prometheus.auth.password")); + Assertions.assertEquals( + "username", dataSourceMetadata.getProperties().get("prometheus.auth.username")); + Assertions.assertEquals( + "basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } @SneakyThrows @@ -124,9 +112,12 @@ public void testGetDataSourceMetadataWith404SearchResponse() { Mockito.when(searchResponseActionFuture.actionGet()).thenReturn(searchResponse); Mockito.when(searchResponse.status()).thenReturn(RestStatus.NOT_FOUND); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> openSearchDataSourceMetadataStorage.getDataSourceMetadata( - TEST_DATASOURCE_INDEX_NAME)); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + openSearchDataSourceMetadataStorage.getDataSourceMetadata( + TEST_DATASOURCE_INDEX_NAME)); Assertions.assertEquals( "Fetching dataSource metadata information failed with status : NOT_FOUND", runtimeException.getMessage()); @@ -143,15 +134,13 @@ public void testGetDataSourceMetadataWithParsingFailed() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn("..testDs"); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn("..testDs"); - Assertions.assertThrows(RuntimeException.class, - () -> openSearchDataSourceMetadataStorage.getDataSourceMetadata( - TEST_DATASOURCE_INDEX_NAME)); + Assertions.assertThrows( + RuntimeException.class, + () -> + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME)); } @SneakyThrows @@ -165,28 +154,24 @@ public void testGetDataSourceMetadataWithAWSSigV4() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); - Mockito.when(searchHit.getSourceAsString()) - .thenReturn(getAWSSigv4DataSourceMetadataString()); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); + Mockito.when(searchHit.getSourceAsString()).thenReturn(getAWSSigv4DataSourceMetadataString()); Mockito.when(encryptor.decrypt("secret_key")).thenReturn("secret_key"); Mockito.when(encryptor.decrypt("access_key")).thenReturn("access_key"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("secret_key", - dataSourceMetadata.getProperties().get("prometheus.auth.secret_key")); - Assertions.assertEquals("access_key", - dataSourceMetadata.getProperties().get("prometheus.auth.access_key")); - Assertions.assertEquals("awssigv4", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "secret_key", dataSourceMetadata.getProperties().get("prometheus.auth.secret_key")); + Assertions.assertEquals( + "access_key", dataSourceMetadata.getProperties().get("prometheus.auth.access_key")); + Assertions.assertEquals( + "awssigv4", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } @SneakyThrows @@ -200,31 +185,27 @@ public void testGetDataSourceMetadataWithBasicAuth() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); Mockito.when(searchHit.getSourceAsString()) .thenReturn(getDataSourceMetadataStringWithBasicAuthentication()); Mockito.when(encryptor.decrypt("username")).thenReturn("username"); Mockito.when(encryptor.decrypt("password")).thenReturn("password"); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); - + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isEmpty()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataOptional.get(); Assertions.assertEquals(TEST_DATASOURCE_INDEX_NAME, dataSourceMetadata.getName()); Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); - Assertions.assertEquals("username", - dataSourceMetadata.getProperties().get("prometheus.auth.username")); - Assertions.assertEquals("password", - dataSourceMetadata.getProperties().get("prometheus.auth.password")); - Assertions.assertEquals("basicauth", - dataSourceMetadata.getProperties().get("prometheus.auth.type")); + Assertions.assertEquals( + "username", dataSourceMetadata.getProperties().get("prometheus.auth.username")); + Assertions.assertEquals( + "password", dataSourceMetadata.getProperties().get("prometheus.auth.password")); + Assertions.assertEquals( + "basicauth", dataSourceMetadata.getProperties().get("prometheus.auth.type")); } - @SneakyThrows @Test public void testGetDataSourceMetadataList() { @@ -236,15 +217,12 @@ public void testGetDataSourceMetadataList() { Mockito.when(searchResponse.getHits()) .thenReturn( new SearchHits( - new SearchHit[] {searchHit}, - new TotalHits(21, TotalHits.Relation.EQUAL_TO), - 1.0F)); + new SearchHit[] {searchHit}, new TotalHits(21, TotalHits.Relation.EQUAL_TO), 1.0F)); Mockito.when(searchHit.getSourceAsString()) .thenReturn(getDataSourceMetadataStringWithNoAuthentication()); - List dataSourceMetadataList - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(); - + List dataSourceMetadataList = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(); Assertions.assertEquals(1, dataSourceMetadataList.size()); DataSourceMetadata dataSourceMetadata = dataSourceMetadataList.get(0); @@ -252,7 +230,6 @@ public void testGetDataSourceMetadataList() { Assertions.assertEquals(DataSourceType.PROMETHEUS, dataSourceMetadata.getConnector()); } - @SneakyThrows @Test public void testGetDataSourceMetadataListWithNoIndex() { @@ -264,8 +241,8 @@ public void testGetDataSourceMetadataListWithNoIndex() { .thenReturn(new CreateIndexResponse(true, true, DATASOURCE_INDEX_NAME)); Mockito.when(client.index(ArgumentMatchers.any())).thenReturn(indexResponseActionFuture); - List dataSourceMetadataList - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(); + List dataSourceMetadataList = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(); Assertions.assertEquals(0, dataSourceMetadataList.size()); } @@ -281,8 +258,8 @@ public void testGetDataSourceMetadataWithNoIndex() { .thenReturn(new CreateIndexResponse(true, true, DATASOURCE_INDEX_NAME)); Mockito.when(client.index(ArgumentMatchers.any())).thenReturn(indexResponseActionFuture); - Optional dataSourceMetadataOptional - = openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); + Optional dataSourceMetadataOptional = + openSearchDataSourceMetadataStorage.getDataSourceMetadata(TEST_DATASOURCE_INDEX_NAME); Assertions.assertFalse(dataSourceMetadataOptional.isPresent()); } @@ -310,8 +287,6 @@ public void testCreateDataSourceMetadata() { Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -334,7 +309,6 @@ public void testCreateDataSourceMetadataWithOutCreatingIndex() { Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); } - @Test public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { @@ -351,10 +325,14 @@ public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { Mockito.when(indexResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Saving dataSource metadata information failed with result : not_found", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Saving dataSource metadata information failed with result : not_found", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); @@ -362,8 +340,6 @@ public void testCreateDataSourceMetadataFailedWithNotFoundResponse() { Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -381,20 +357,19 @@ public void testCreateDataSourceMetadataWithVersionConflict() { .thenThrow(VersionConflictEngineException.class); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); IllegalArgumentException illegalArgumentException = - Assertions.assertThrows(IllegalArgumentException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("A datasource already exists with name: testDS", - illegalArgumentException.getMessage()); - + Assertions.assertThrows( + IllegalArgumentException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "A datasource already exists with name: testDS", illegalArgumentException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -412,19 +387,20 @@ public void testCreateDataSourceMetadataWithException() { .thenThrow(new RuntimeException("error while indexing")); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("java.lang.RuntimeException: error while indexing", - runtimeException.getMessage()); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "java.lang.RuntimeException: error while indexing", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(1)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).index(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext(); - - } @Test @@ -440,9 +416,12 @@ public void testCreateDataSourceMetadataWithIndexCreationFailed() { .thenReturn(new CreateIndexResponse(false, false, DATASOURCE_INDEX_NAME)); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( - dataSourceMetadata)); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.createDataSourceMetadata( + dataSourceMetadata)); Assertions.assertEquals( "Internal server error while creating.ql-datasources index:: " + "Index creation is not acknowledged.", @@ -470,7 +449,6 @@ public void testUpdateDataSourceMetadata() { Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -482,10 +460,14 @@ public void testUpdateDataSourceMetadataWithNotFoundResult() { Mockito.when(updateResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Saving dataSource metadata information failed with result : not_found", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Saving dataSource metadata information failed with result : not_found", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); @@ -493,32 +475,31 @@ public void testUpdateDataSourceMetadataWithNotFoundResult() { Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test public void testUpdateDataSourceMetadataWithDocumentMissingException() { Mockito.when(encryptor.encrypt("secret_key")).thenReturn("secret_key"); Mockito.when(encryptor.encrypt("access_key")).thenReturn("access_key"); - Mockito.when(client.update(ArgumentMatchers.any())).thenThrow(new DocumentMissingException( - ShardId.fromString("[2][2]"), "testDS")); + Mockito.when(client.update(ArgumentMatchers.any())) + .thenThrow(new DocumentMissingException(ShardId.fromString("[2][2]"), "testDS")); DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); dataSourceMetadata.setName("testDS"); - DataSourceNotFoundException dataSourceNotFoundException = - Assertions.assertThrows(DataSourceNotFoundException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("Datasource with name: testDS doesn't exist", - dataSourceNotFoundException.getMessage()); + Assertions.assertThrows( + DataSourceNotFoundException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "Datasource with name: testDS doesn't exist", dataSourceNotFoundException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -530,19 +511,20 @@ public void testUpdateDataSourceMetadataWithRuntimeException() { DataSourceMetadata dataSourceMetadata = getDataSourceMetadata(); dataSourceMetadata.setName("testDS"); - - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( - dataSourceMetadata)); - Assertions.assertEquals("java.lang.RuntimeException: error message", - runtimeException.getMessage()); + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> + this.openSearchDataSourceMetadataStorage.updateDataSourceMetadata( + dataSourceMetadata)); + Assertions.assertEquals( + "java.lang.RuntimeException: error message", runtimeException.getMessage()); Mockito.verify(encryptor, Mockito.times(1)).encrypt("secret_key"); Mockito.verify(encryptor, Mockito.times(1)).encrypt("access_key"); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); Mockito.verify(client, Mockito.times(1)).update(ArgumentMatchers.any()); Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(1)).stashContext(); - } @Test @@ -566,11 +548,11 @@ public void testDeleteDataSourceMetadataWhichisAlreadyDeleted() { Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); DataSourceNotFoundException dataSourceNotFoundException = - Assertions.assertThrows(DataSourceNotFoundException.class, + Assertions.assertThrows( + DataSourceNotFoundException.class, () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); - Assertions.assertEquals("Datasource with name: testDS doesn't exist", - dataSourceNotFoundException.getMessage()); - + Assertions.assertEquals( + "Datasource with name: testDS doesn't exist", dataSourceNotFoundException.getMessage()); Mockito.verifyNoInteractions(encryptor); Mockito.verify(client.admin().indices(), Mockito.times(0)).create(ArgumentMatchers.any()); @@ -584,9 +566,12 @@ public void testDeleteDataSourceMetadataWithUnexpectedResult() { Mockito.when(deleteResponseActionFuture.actionGet()).thenReturn(deleteResponse); Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOOP); - RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, - () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); - Assertions.assertEquals("Deleting dataSource metadata information failed with result : noop", + RuntimeException runtimeException = + Assertions.assertThrows( + RuntimeException.class, + () -> this.openSearchDataSourceMetadataStorage.deleteDataSourceMetadata("testDS")); + Assertions.assertEquals( + "Deleting dataSource metadata information failed with result : noop", runtimeException.getMessage()); Mockito.verifyNoInteractions(encryptor); @@ -666,5 +651,4 @@ private DataSourceMetadata getDataSourceMetadata() { dataSourceMetadata.setProperties(properties); return dataSourceMetadata; } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java index ccae5de2a9d..f1a3a2875e7 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportCreateDataSourceActionTest.java @@ -27,27 +27,23 @@ @ExtendWith(MockitoExtension.class) public class TransportCreateDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportCreateDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportCreateDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; + @Captor private ArgumentCaptor createDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportCreateDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportCreateDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -61,10 +57,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).createDataSource(dataSourceMetadata); Mockito.verify(actionListener) .onResponse(createDataSourceActionResponseArgumentCaptor.capture()); - CreateDataSourceActionResponse createDataSourceActionResponse - = createDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Created DataSource with name test_datasource", - createDataSourceActionResponse.getResult()); + CreateDataSourceActionResponse createDataSourceActionResponse = + createDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Created DataSource with name test_datasource", createDataSourceActionResponse.getResult()); } @Test @@ -72,7 +68,8 @@ public void testDoExecuteWithException() { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); - doThrow(new RuntimeException("Error")).when(dataSourceService) + doThrow(new RuntimeException("Error")) + .when(dataSourceService) .createDataSource(dataSourceMetadata); CreateDataSourceActionRequest request = new CreateDataSourceActionRequest(dataSourceMetadata); action.doExecute(task, request, actionListener); @@ -80,7 +77,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java index e97e7d1a650..ea581de20c3 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportDeleteDataSourceActionTest.java @@ -25,28 +25,23 @@ @ExtendWith(MockitoExtension.class) public class TransportDeleteDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportDeleteDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportDeleteDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor deleteDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportDeleteDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportDeleteDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -57,10 +52,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).deleteDataSource("test_datasource"); Mockito.verify(actionListener) .onResponse(deleteDataSourceActionResponseArgumentCaptor.capture()); - DeleteDataSourceActionResponse deleteDataSourceActionResponse - = deleteDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Deleted DataSource with name test_datasource", - deleteDataSourceActionResponse.getResult()); + DeleteDataSourceActionResponse deleteDataSourceActionResponse = + deleteDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Deleted DataSource with name test_datasource", deleteDataSourceActionResponse.getResult()); } @Test @@ -72,7 +67,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java index fc4439470e1..4f04afd667d 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportGetDataSourceActionTest.java @@ -34,27 +34,22 @@ @ExtendWith(MockitoExtension.class) public class TransportGetDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportGetDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportGetDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor getDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportGetDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportGetDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -63,23 +58,22 @@ public void testDoExecute() { dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); GetDataSourceActionRequest request = new GetDataSourceActionRequest("test_datasource"); - when(dataSourceService.getDataSourceMetadata("test_datasource")) - .thenReturn(dataSourceMetadata); + when(dataSourceService.getDataSourceMetadata("test_datasource")).thenReturn(dataSourceMetadata); action.doExecute(task, request, actionListener); verify(dataSourceService, times(1)).getDataSourceMetadata("test_datasource"); Mockito.verify(actionListener).onResponse(getDataSourceActionResponseArgumentCaptor.capture()); - GetDataSourceActionResponse getDataSourceActionResponse - = getDataSourceActionResponseArgumentCaptor.getValue(); + GetDataSourceActionResponse getDataSourceActionResponse = + getDataSourceActionResponseArgumentCaptor.getValue(); JsonResponseFormatter dataSourceMetadataJsonResponseFormatter = - new JsonResponseFormatter<>( - JsonResponseFormatter.Style.PRETTY) { + new JsonResponseFormatter<>(JsonResponseFormatter.Style.PRETTY) { @Override protected Object buildJsonObject(DataSourceMetadata response) { return response; } }; - Assertions.assertEquals(dataSourceMetadataJsonResponseFormatter.format(dataSourceMetadata), + Assertions.assertEquals( + dataSourceMetadataJsonResponseFormatter.format(dataSourceMetadata), getDataSourceActionResponse.getResult()); DataSourceMetadata result = new Gson().fromJson(getDataSourceActionResponse.getResult(), DataSourceMetadata.class); @@ -100,18 +94,16 @@ public void testDoExecuteForGetAllDataSources() { action.doExecute(task, request, actionListener); verify(dataSourceService, times(1)).getDataSourceMetadata(false); Mockito.verify(actionListener).onResponse(getDataSourceActionResponseArgumentCaptor.capture()); - GetDataSourceActionResponse getDataSourceActionResponse - = getDataSourceActionResponseArgumentCaptor.getValue(); + GetDataSourceActionResponse getDataSourceActionResponse = + getDataSourceActionResponseArgumentCaptor.getValue(); JsonResponseFormatter> dataSourceMetadataJsonResponseFormatter = - new JsonResponseFormatter<>( - JsonResponseFormatter.Style.PRETTY) { + new JsonResponseFormatter<>(JsonResponseFormatter.Style.PRETTY) { @Override protected Object buildJsonObject(Set response) { return response; } }; - Type setType = new TypeToken>() { - }.getType(); + Type setType = new TypeToken>() {}.getType(); Assertions.assertEquals( dataSourceMetadataJsonResponseFormatter.format(Collections.singleton(dataSourceMetadata)), getDataSourceActionResponse.getResult()); @@ -131,7 +123,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java index 4b5a6e0f573..998a1aa7b21 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/transport/TransportUpdateDataSourceActionTest.java @@ -27,28 +27,23 @@ @ExtendWith(MockitoExtension.class) public class TransportUpdateDataSourceActionTest { - @Mock - private TransportService transportService; - @Mock - private TransportUpdateDataSourceAction action; - @Mock - private DataSourceServiceImpl dataSourceService; - @Mock - private Task task; - @Mock - private ActionListener actionListener; + @Mock private TransportService transportService; + @Mock private TransportUpdateDataSourceAction action; + @Mock private DataSourceServiceImpl dataSourceService; + @Mock private Task task; + @Mock private ActionListener actionListener; @Captor private ArgumentCaptor updateDataSourceActionResponseArgumentCaptor; - @Captor - private ArgumentCaptor exceptionArgumentCaptor; + @Captor private ArgumentCaptor exceptionArgumentCaptor; @BeforeEach public void setUp() { - action = new TransportUpdateDataSourceAction(transportService, - new ActionFilters(new HashSet<>()), dataSourceService); + action = + new TransportUpdateDataSourceAction( + transportService, new ActionFilters(new HashSet<>()), dataSourceService); } @Test @@ -62,10 +57,10 @@ public void testDoExecute() { verify(dataSourceService, times(1)).updateDataSource(dataSourceMetadata); Mockito.verify(actionListener) .onResponse(updateDataSourceActionResponseArgumentCaptor.capture()); - UpdateDataSourceActionResponse updateDataSourceActionResponse - = updateDataSourceActionResponseArgumentCaptor.getValue(); - Assertions.assertEquals("Updated DataSource with name test_datasource", - updateDataSourceActionResponse.getResult()); + UpdateDataSourceActionResponse updateDataSourceActionResponse = + updateDataSourceActionResponseArgumentCaptor.getValue(); + Assertions.assertEquals( + "Updated DataSource with name test_datasource", updateDataSourceActionResponse.getResult()); } @Test @@ -73,7 +68,8 @@ public void testDoExecuteWithException() { DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); dataSourceMetadata.setName("test_datasource"); dataSourceMetadata.setConnector(DataSourceType.PROMETHEUS); - doThrow(new RuntimeException("Error")).when(dataSourceService) + doThrow(new RuntimeException("Error")) + .when(dataSourceService) .updateDataSource(dataSourceMetadata); UpdateDataSourceActionRequest request = new UpdateDataSourceActionRequest(dataSourceMetadata); action.doExecute(task, request, actionListener); @@ -81,7 +77,6 @@ public void testDoExecuteWithException() { Mockito.verify(actionListener).onFailure(exceptionArgumentCaptor.capture()); Exception exception = exceptionArgumentCaptor.getValue(); Assertions.assertTrue(exception instanceof RuntimeException); - Assertions.assertEquals("Error", - exception.getMessage()); + Assertions.assertEquals("Error", exception.getMessage()); } } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java index e3dac306cd4..ff23cdcabbd 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/utils/SchedulerTest.java @@ -19,27 +19,24 @@ @ExtendWith(MockitoExtension.class) public class SchedulerTest { - @Mock - private NodeClient nodeClient; + @Mock private NodeClient nodeClient; - @Mock - private ThreadPool threadPool; + @Mock private ThreadPool threadPool; @Test public void testSchedule() { Mockito.when(nodeClient.threadPool()).thenReturn(threadPool); Mockito.doAnswer( - invocation -> { - Runnable task = invocation.getArgument(0); - task.run(); - return null; - }) + invocation -> { + Runnable task = invocation.getArgument(0); + task.run(); + return null; + }) .when(threadPool) .schedule(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any()); AtomicBoolean isRun = new AtomicBoolean(false); Scheduler.schedule(nodeClient, () -> isRun.set(true)); Assert.assertTrue(isRun.get()); } - } diff --git a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java index f47d0503e7e..c0c05c02821 100644 --- a/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java +++ b/datasources/src/test/java/org/opensearch/sql/datasources/utils/XContentParserUtilsTest.java @@ -30,7 +30,8 @@ public void testConvertToXContent() { XContentBuilder contentBuilder = XContentParserUtils.convertToXContent(dataSourceMetadata); String contentString = BytesReference.bytes(contentBuilder).utf8ToString(); - Assertions.assertEquals("{\"name\":\"testDS\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", + Assertions.assertEquals( + "{\"name\":\"testDS\",\"connector\":\"PROMETHEUS\",\"allowedRoles\":[\"prometheus_access\"],\"properties\":{\"prometheus.uri\":\"https://localhost:9090\"}}", contentString); } @@ -49,7 +50,6 @@ public void testToDataSourceMetadataFromJson() { Assertions.assertEquals(retrievedMetadata, dataSourceMetadata); Assertions.assertEquals("prometheus_access", retrievedMetadata.getAllowedRoles().get(0)); - } @SneakyThrows @@ -62,9 +62,12 @@ public void testToDataSourceMetadataFromJsonWithoutName() { Gson gson = new Gson(); String json = gson.toJson(dataSourceMetadata); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("name and connector are required fields.", exception.getMessage()); } @@ -78,9 +81,12 @@ public void testToDataSourceMetadataFromJsonWithoutConnector() { Gson gson = new Gson(); String json = gson.toJson(dataSourceMetadata); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("name and connector are required fields.", exception.getMessage()); } @@ -92,10 +98,12 @@ public void testToDataSourceMetadataFromJsonUsingUnknownObject() { Gson gson = new Gson(); String json = gson.toJson(hashMap); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - XContentParserUtils.toDataSourceMetadata(json); - }); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> { + XContentParserUtils.toDataSourceMetadata(json); + }); Assertions.assertEquals("Unknown field: test", exception.getMessage()); } - } From 9ddd89604d3fea0569384dab049d80bfc2f9851f Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Mon, 14 Aug 2023 14:21:27 -0700 Subject: [PATCH 2/6] [Spotless] Applying Google Code Format for common #7 (#1940) * Spotless apply for common directory. Signed-off-by: Mitchell Gale * Igoring checkstyle for common Signed-off-by: Mitchell Gale * Spotless apply on string utils, Signed-off-by: Mitchell Gale * Typo fix Signed-off-by: Yury-Fridlyand --------- Signed-off-by: Mitchell Gale Signed-off-by: Yury-Fridlyand Co-authored-by: Yury-Fridlyand --- build.gradle | 3 +- common/build.gradle | 7 +- .../antlr/CaseInsensitiveCharStream.java | 1 - .../antlr/SyntaxAnalysisErrorListener.java | 1 - .../common/antlr/SyntaxCheckException.java | 1 - .../AwsSigningInterceptor.java | 32 ++-- .../BasicAuthenticationInterceptor.java | 6 +- .../opensearch/sql/common/grok/Converter.java | 57 +++--- .../org/opensearch/sql/common/grok/Grok.java | 66 +++---- .../sql/common/grok/GrokCompiler.java | 62 +++---- .../opensearch/sql/common/grok/GrokUtils.java | 47 ++--- .../org/opensearch/sql/common/grok/Match.java | 143 +++++++------- .../common/grok/exception/GrokException.java | 12 +- .../sql/common/response/ResponseListener.java | 1 - .../sql/common/setting/LegacySettings.java | 24 +-- .../sql/common/setting/Settings.java | 24 +-- .../sql/common/utils/QueryContext.java | 26 ++- .../sql/common/utils/StringUtils.java | 67 +++---- .../AwsSigningInterceptorTest.java | 65 +++---- .../BasicAuthenticationInterceptorTest.java | 25 +-- .../sql/common/grok/ApacheDataTypeTest.java | 29 ++- .../sql/common/grok/ApacheTest.java | 2 - .../opensearch/sql/common/grok/BasicTest.java | 8 +- .../sql/common/grok/CaptureTest.java | 6 +- .../common/grok/GrokDocumentationTest.java | 44 +++-- .../opensearch/sql/common/grok/GrokTest.java | 175 ++++++++++-------- .../sql/common/grok/MessagesTest.java | 7 +- .../sql/common/grok/ResourceManager.java | 4 +- 28 files changed, 431 insertions(+), 514 deletions(-) diff --git a/build.gradle b/build.gradle index 8c6b7d4bc04..0d8e3f067a7 100644 --- a/build.gradle +++ b/build.gradle @@ -84,7 +84,8 @@ repositories { spotless { java { target fileTree('.') { - include 'datasources/**/*.java', + include 'common/**/*.java', + 'datasources/**/*.java', 'core/**/*.java' exclude '**/build/**', '**/build-*/**' } diff --git a/common/build.gradle b/common/build.gradle index 0367035ce2f..25cdcd6566e 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -31,6 +31,11 @@ repositories { mavenCentral() } +// Being ignored as a temporary measure before being removed in favour of +// spotless https://github.com/opensearch-project/sql/issues/1101 +checkstyleTest.ignoreFailures = true +checkstyleMain.ignoreFailures = true + dependencies { api "org.antlr:antlr4-runtime:4.7.1" api group: 'com.google.guava', name: 'guava', version: '32.0.1-jre' @@ -62,4 +67,4 @@ configurations.all { resolutionStrategy.force "org.apache.httpcomponents:httpcore:4.4.13" resolutionStrategy.force "joda-time:joda-time:2.10.12" resolutionStrategy.force "org.slf4j:slf4j-api:1.7.36" -} \ No newline at end of file +} diff --git a/common/src/main/java/org/opensearch/sql/common/antlr/CaseInsensitiveCharStream.java b/common/src/main/java/org/opensearch/sql/common/antlr/CaseInsensitiveCharStream.java index 0036da32a1a..89381872ce8 100644 --- a/common/src/main/java/org/opensearch/sql/common/antlr/CaseInsensitiveCharStream.java +++ b/common/src/main/java/org/opensearch/sql/common/antlr/CaseInsensitiveCharStream.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.antlr; import org.antlr.v4.runtime.CharStream; diff --git a/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxAnalysisErrorListener.java b/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxAnalysisErrorListener.java index b499a529674..76cbf52d583 100644 --- a/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxAnalysisErrorListener.java +++ b/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxAnalysisErrorListener.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.antlr; import java.util.Locale; diff --git a/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxCheckException.java b/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxCheckException.java index 806cb7208bb..d3c9c111efa 100644 --- a/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxCheckException.java +++ b/common/src/main/java/org/opensearch/sql/common/antlr/SyntaxCheckException.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.antlr; public class SyntaxCheckException extends RuntimeException { diff --git a/common/src/main/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptor.java b/common/src/main/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptor.java index 6c65c69c317..e2d33dca8b1 100644 --- a/common/src/main/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptor.java +++ b/common/src/main/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptor.java @@ -31,15 +31,17 @@ public class AwsSigningInterceptor implements Interceptor { private static final Logger LOG = LogManager.getLogger(); /** - * AwsSigningInterceptor which intercepts http requests - * and adds required headers for sigv4 authentication. + * AwsSigningInterceptor which intercepts http requests and adds required headers for sigv4 + * authentication. * * @param awsCredentialsProvider awsCredentialsProvider. * @param region region. * @param serviceName serviceName. */ - public AwsSigningInterceptor(@NonNull AWSCredentialsProvider awsCredentialsProvider, - @NonNull String region, @NonNull String serviceName) { + public AwsSigningInterceptor( + @NonNull AWSCredentialsProvider awsCredentialsProvider, + @NonNull String region, + @NonNull String serviceName) { this.okHttpAwsV4Signer = new OkHttpAwsV4Signer(region, serviceName); this.awsCredentialsProvider = awsCredentialsProvider; } @@ -48,25 +50,27 @@ public AwsSigningInterceptor(@NonNull AWSCredentialsProvider awsCredentialsProvi public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); - DateTimeFormatter timestampFormat = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'") - .withZone(ZoneId.of("GMT")); + DateTimeFormatter timestampFormat = + DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneId.of("GMT")); - - Request.Builder newRequestBuilder = request.newBuilder() - .addHeader("x-amz-date", timestampFormat.format(ZonedDateTime.now())) - .addHeader("host", request.url().host()); + Request.Builder newRequestBuilder = + request + .newBuilder() + .addHeader("x-amz-date", timestampFormat.format(ZonedDateTime.now())) + .addHeader("host", request.url().host()); AWSCredentials awsCredentials = awsCredentialsProvider.getCredentials(); if (awsCredentialsProvider instanceof STSAssumeRoleSessionCredentialsProvider) { - newRequestBuilder.addHeader("x-amz-security-token", + newRequestBuilder.addHeader( + "x-amz-security-token", ((STSAssumeRoleSessionCredentialsProvider) awsCredentialsProvider) .getCredentials() .getSessionToken()); } Request newRequest = newRequestBuilder.build(); - Request signed = okHttpAwsV4Signer.sign(newRequest, - awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey()); + Request signed = + okHttpAwsV4Signer.sign( + newRequest, awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey()); return chain.proceed(signed); } - } diff --git a/common/src/main/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptor.java b/common/src/main/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptor.java index 34634d15801..2275482e306 100644 --- a/common/src/main/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptor.java +++ b/common/src/main/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptor.java @@ -22,13 +22,11 @@ public BasicAuthenticationInterceptor(@NonNull String username, @NonNull String this.credentials = Credentials.basic(username, password); } - @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); - Request authenticatedRequest = request.newBuilder() - .header("Authorization", credentials).build(); + Request authenticatedRequest = + request.newBuilder().header("Authorization", credentials).build(); return chain.proceed(authenticatedRequest); } - } diff --git a/common/src/main/java/org/opensearch/sql/common/grok/Converter.java b/common/src/main/java/org/opensearch/sql/common/grok/Converter.java index ebbe13f7611..ddd3a2bbb45 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/Converter.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/Converter.java @@ -23,9 +23,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -/** - * Convert String argument to the right type. - */ +/** Convert String argument to the right type. */ public class Converter { public enum Type { @@ -51,13 +49,13 @@ public enum Type { private static final Pattern SPLITTER = Pattern.compile("[:;]"); private static final Map TYPES = - Arrays.stream(Type.values()) - .collect(Collectors.toMap(t -> t.name().toLowerCase(), t -> t)); + Arrays.stream(Type.values()).collect(Collectors.toMap(t -> t.name().toLowerCase(), t -> t)); private static final Map TYPE_ALIASES = Arrays.stream(Type.values()) - .flatMap(type -> type.aliases.stream() - .map(alias -> new AbstractMap.SimpleEntry<>(alias, type))) + .flatMap( + type -> + type.aliases.stream().map(alias -> new AbstractMap.SimpleEntry<>(alias, type))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); private static Type getType(String key) { @@ -69,34 +67,30 @@ private static Type getType(String key) { return type; } - /** - * getConverters. - */ - public static Map> - getConverters(Collection groupNames, Object... params) { + /** getConverters. */ + public static Map> getConverters( + Collection groupNames, Object... params) { return groupNames.stream() .filter(Converter::containsDelimiter) - .collect(Collectors.toMap(Function.identity(), key -> { - String[] list = splitGrokPattern(key); - IConverter converter = getType(list[1]).converter; - if (list.length == 3) { - converter = converter.newConverter(list[2], params); - } - return converter; - })); + .collect( + Collectors.toMap( + Function.identity(), + key -> { + String[] list = splitGrokPattern(key); + IConverter converter = getType(list[1]).converter; + if (list.length == 3) { + converter = converter.newConverter(list[2], params); + } + return converter; + })); } - /** - * getGroupTypes. - */ + /** getGroupTypes. */ public static Map getGroupTypes(Collection groupNames) { return groupNames.stream() .filter(Converter::containsDelimiter) .map(Converter::splitGrokPattern) - .collect(Collectors.toMap( - l -> l[0], - l -> getType(l[1]) - )); + .collect(Collectors.toMap(l -> l[0], l -> getType(l[1]))); } public static String extractKey(String key) { @@ -120,7 +114,6 @@ default IConverter newConverter(String param, Object... params) { } } - static class DateConverter implements IConverter { private final DateTimeFormatter formatter; @@ -138,8 +131,12 @@ private DateConverter(DateTimeFormatter formatter, ZoneId timeZone) { @Override public Instant convert(String value) { - TemporalAccessor dt = formatter - .parseBest(value.trim(), ZonedDateTime::from, LocalDateTime::from, OffsetDateTime::from, + TemporalAccessor dt = + formatter.parseBest( + value.trim(), + ZonedDateTime::from, + LocalDateTime::from, + OffsetDateTime::from, Instant::from, LocalDate::from); if (dt instanceof ZonedDateTime) { diff --git a/common/src/main/java/org/opensearch/sql/common/grok/Grok.java b/common/src/main/java/org/opensearch/sql/common/grok/Grok.java index f20f99cbc3d..6dfab3f7910 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/Grok.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/Grok.java @@ -16,36 +16,29 @@ import org.opensearch.sql.common.grok.Converter.IConverter; /** - * {@code Grok} parse arbitrary text and structure it. - *
- * {@code Grok} is simple API that allows you to easily parse logs - * and other files (single line). With {@code Grok}, - * you can turn unstructured log and event data into structured data. + * {@code Grok} parse arbitrary text and structure it.
+ * {@code Grok} is simple API that allows you to easily parse logs and other files (single line). + * With {@code Grok}, you can turn unstructured log and event data into structured data. * * @since 0.0.1 */ public class Grok implements Serializable { - /** - * Named regex of the originalGrokPattern. - */ + /** Named regex of the originalGrokPattern. */ private final String namedRegex; + /** - * Map of the named regex of the originalGrokPattern - * with id = namedregexid and value = namedregex. + * Map of the named regex of the originalGrokPattern with id = namedregexid and value = + * namedregex. */ private final Map namedRegexCollection; - /** - * Original {@code Grok} pattern (expl: %{IP}). - */ + + /** Original {@code Grok} pattern (expl: %{IP}). */ private final String originalGrokPattern; - /** - * Pattern of the namedRegex. - */ + + /** Pattern of the namedRegex. */ private final Pattern compiledNamedRegex; - /** - * {@code Grok} patterns definition. - */ + /** {@code Grok} patterns definition. */ private final Map grokPatternDefinition; public final Set namedGroups; @@ -54,19 +47,16 @@ public class Grok implements Serializable { public final Map> converters; - /** - * only use in grok discovery. - */ + /** only use in grok discovery. */ private String savedPattern = ""; - /** - * Grok. - */ - public Grok(String pattern, - String namedRegex, - Map namedRegexCollection, - Map patternDefinitions, - ZoneId defaultTimeZone) { + /** Grok. */ + public Grok( + String pattern, + String namedRegex, + Map namedRegexCollection, + Map patternDefinitions, + ZoneId defaultTimeZone) { this.originalGrokPattern = pattern; this.namedRegex = namedRegex; this.compiledNamedRegex = Pattern.compile(namedRegex); @@ -132,8 +122,8 @@ public Map getNamedRegexCollection() { } /** - * Match the given log with the named regex. - * And return the json representation of the matched element + * Match the given log with the named regex. And return the json representation of the + * matched element * * @param log : log to match * @return map containing matches @@ -144,8 +134,8 @@ public Map capture(String log) { } /** - * Match the given list of log with the named regex - * and return the list of json representation of the matched elements. + * Match the given list of log with the named regex and return the list of json + * representation of the matched elements. * * @param logs : list of log * @return list of maps containing matches @@ -159,8 +149,8 @@ public ArrayList> capture(List logs) { } /** - * Match the given text with the named regex - * {@code Grok} will extract data from the string and get an extence of {@link Match}. + * Match the given text with the named regex {@code Grok} will extract data from the + * string and get an extence of {@link Match}. * * @param text : Single line of log * @return Grok Match @@ -172,9 +162,7 @@ public Match match(CharSequence text) { Matcher matcher = compiledNamedRegex.matcher(text); if (matcher.find()) { - return new Match( - text, this, matcher, matcher.start(0), matcher.end(0) - ); + return new Match(text, this, matcher, matcher.start(0), matcher.end(0)); } return Match.EMPTY; diff --git a/common/src/main/java/org/opensearch/sql/common/grok/GrokCompiler.java b/common/src/main/java/org/opensearch/sql/common/grok/GrokCompiler.java index 18894fc7a1a..aba96ad4cb1 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/GrokCompiler.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/GrokCompiler.java @@ -31,13 +31,10 @@ public class GrokCompiler implements Serializable { // We don't want \n and commented line private static final Pattern patternLinePattern = Pattern.compile("^([A-z0-9_]+)\\s+(.*)$"); - /** - * {@code Grok} patterns definitions. - */ + /** {@code Grok} patterns definitions. */ private final Map grokPatternDefinitions = new HashMap<>(); - private GrokCompiler() { - } + private GrokCompiler() {} public static GrokCompiler newInstance() { return new GrokCompiler(); @@ -50,10 +47,10 @@ public Map getPatternDefinitions() { /** * Registers a new pattern definition. * - * @param name : Pattern Name + * @param name : Pattern Name * @param pattern : Regular expression Or {@code Grok} pattern * @throws GrokException runtime expt - **/ + */ public void register(String name, String pattern) { name = Objects.requireNonNull(name).trim(); pattern = Objects.requireNonNull(pattern).trim(); @@ -63,9 +60,7 @@ public void register(String name, String pattern) { } } - /** - * Registers multiple pattern definitions. - */ + /** Registers multiple pattern definitions. */ public void register(Map patternDefinitions) { Objects.requireNonNull(patternDefinitions); patternDefinitions.forEach(this::register); @@ -78,12 +73,9 @@ public void register(InputStream input) throws IOException { register(input, StandardCharsets.UTF_8); } - /** - * Registers multiple pattern definitions from a given inputStream. - */ + /** Registers multiple pattern definitions from a given inputStream. */ public void register(InputStream input, Charset charset) throws IOException { - try ( - BufferedReader in = new BufferedReader(new InputStreamReader(input, charset))) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(input, charset))) { in.lines() .map(patternLinePattern::matcher) .filter(Matcher::matches) @@ -91,11 +83,10 @@ public void register(InputStream input, Charset charset) throws IOException { } } - /** - * Registers multiple pattern definitions from a given Reader. - */ + /** Registers multiple pattern definitions from a given Reader. */ public void register(Reader input) throws IOException { - new BufferedReader(input).lines() + new BufferedReader(input) + .lines() .map(patternLinePattern::matcher) .filter(Matcher::matches) .forEach(m -> register(m.group(1), m.group(2))); @@ -109,9 +100,7 @@ public void registerPatternFromClasspath(String path) throws GrokException { registerPatternFromClasspath(path, StandardCharsets.UTF_8); } - /** - * registerPatternFromClasspath. - */ + /** registerPatternFromClasspath. */ public void registerPatternFromClasspath(String path, Charset charset) throws GrokException { final InputStream inputStream = this.getClass().getResourceAsStream(path); try (Reader reader = new InputStreamReader(inputStream, charset)) { @@ -121,9 +110,7 @@ public void registerPatternFromClasspath(String path, Charset charset) throws Gr } } - /** - * Compiles a given Grok pattern and returns a Grok object which can parse the pattern. - */ + /** Compiles a given Grok pattern and returns a Grok object which can parse the pattern. */ public Grok compile(String pattern) throws IllegalArgumentException { return compile(pattern, false); } @@ -135,11 +122,11 @@ public Grok compile(final String pattern, boolean namedOnly) throws IllegalArgum /** * Compiles a given Grok pattern and returns a Grok object which can parse the pattern. * - * @param pattern : Grok pattern (ex: %{IP}) - * @param defaultTimeZone : time zone used to parse a timestamp when it doesn't contain - * the time zone - * @param namedOnly : Whether to capture named expressions only or not (i.e. %{IP:ip} - * but not ${IP}) + * @param pattern : Grok pattern (ex: %{IP}) + * @param defaultTimeZone : time zone used to parse a timestamp when it doesn't contain the time + * zone + * @param namedOnly : Whether to capture named expressions only or not (i.e. %{IP:ip} but not + * ${IP}) * @return a compiled pattern * @throws IllegalArgumentException when pattern definition is invalid */ @@ -184,14 +171,15 @@ public Grok compile(final String pattern, ZoneId defaultTimeZone, boolean namedO for (int i = 0; i < count; i++) { String definitionOfPattern = patternDefinitions.get(group.get("pattern")); if (definitionOfPattern == null) { - throw new IllegalArgumentException(format("No definition for key '%s' found, aborting", - group.get("pattern"))); + throw new IllegalArgumentException( + format("No definition for key '%s' found, aborting", group.get("pattern"))); } String replacement = String.format("(?%s)", index, definitionOfPattern); if (namedOnly && group.get("subname") == null) { replacement = String.format("(?:%s)", definitionOfPattern); } - namedRegexCollection.put("name" + index, + namedRegexCollection.put( + "name" + index, (group.get("subname") != null ? group.get("subname") : group.get("name"))); namedRegex = StringUtils.replace(namedRegex, "%{" + group.get("name") + "}", replacement, 1); @@ -205,12 +193,6 @@ public Grok compile(final String pattern, ZoneId defaultTimeZone, boolean namedO throw new IllegalArgumentException("Pattern not found"); } - return new Grok( - pattern, - namedRegex, - namedRegexCollection, - patternDefinitions, - defaultTimeZone - ); + return new Grok(pattern, namedRegex, namedRegexCollection, patternDefinitions, defaultTimeZone); } } diff --git a/common/src/main/java/org/opensearch/sql/common/grok/GrokUtils.java b/common/src/main/java/org/opensearch/sql/common/grok/GrokUtils.java index 9ff65acde25..4b145bbbe8a 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/GrokUtils.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/GrokUtils.java @@ -12,7 +12,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; - /** * {@code GrokUtils} contain set of useful tools or methods. * @@ -20,29 +19,25 @@ */ public class GrokUtils { - /** - * Extract Grok patter like %{FOO} to FOO, Also Grok pattern with semantic. - */ - public static final Pattern GROK_PATTERN = Pattern.compile( - "%\\{" - + "(?" - + "(?[A-z0-9]+)" - + "(?::(?[A-z0-9_:;,\\-\\/\\s\\.']+))?" - + ")" - + "(?:=(?" - + "(?:" - + "(?:[^{}]+|\\.+)+" - + ")+" - + ")" - + ")?" - + "\\}"); - - public static final Pattern NAMED_REGEX = Pattern - .compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>"); - - /** - * getNameGroups. - */ + /** Extract Grok patter like %{FOO} to FOO, Also Grok pattern with semantic. */ + public static final Pattern GROK_PATTERN = + Pattern.compile( + "%\\{" + + "(?" + + "(?[A-z0-9]+)" + + "(?::(?[A-z0-9_:;,\\-\\/\\s\\.']+))?" + + ")" + + "(?:=(?" + + "(?:" + + "(?:[^{}]+|\\.+)+" + + ")+" + + ")" + + ")?" + + "\\}"); + + public static final Pattern NAMED_REGEX = Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>"); + + /** getNameGroups. */ public static Set getNameGroups(String regex) { Set namedGroups = new LinkedHashSet<>(); Matcher matcher = NAMED_REGEX.matcher(regex); @@ -52,9 +47,7 @@ public static Set getNameGroups(String regex) { return namedGroups; } - /** - * namedGroups. - */ + /** namedGroups. */ public static Map namedGroups(Matcher matcher, Set groupNames) { Map namedGroups = new LinkedHashMap<>(); for (String groupName : groupNames) { diff --git a/common/src/main/java/org/opensearch/sql/common/grok/Match.java b/common/src/main/java/org/opensearch/sql/common/grok/Match.java index 6831f35ceec..3771817bba7 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/Match.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/Match.java @@ -5,7 +5,6 @@ package org.opensearch.sql.common.grok; - import static java.lang.String.format; import java.util.ArrayList; @@ -31,9 +30,7 @@ public class Match { private boolean keepEmptyCaptures = true; private Map capture = Collections.emptyMap(); - /** - * Create a new {@code Match} object. - */ + /** Create a new {@code Match} object. */ public Match(CharSequence subject, Grok grok, Matcher match, int start, int end) { this.subject = subject; this.grok = grok; @@ -42,9 +39,7 @@ public Match(CharSequence subject, Grok grok, Matcher match, int start, int end) this.end = end; } - /** - * Create Empty grok matcher. - */ + /** Create Empty grok matcher. */ public static final Match EMPTY = new Match("", null, null, 0, 0); public Matcher getMatch() { @@ -59,9 +54,7 @@ public int getEnd() { return end; } - /** - * Ignore empty captures. - */ + /** Ignore empty captures. */ public void setKeepEmptyCaptures(boolean ignore) { // clear any cached captures if (capture.size() > 0) { @@ -97,8 +90,8 @@ public Map capture() { * * @param flattened will it flatten values. * @return the matched elements. - * @throws GrokException if a keys has multiple non-null values, but only if flattened is set - * to true. + * @throws GrokException if a keys has multiple non-null values, but only if flattened is set to + * true. */ private Map capture(boolean flattened) throws GrokException { if (match == null) { @@ -116,70 +109,69 @@ private Map capture(boolean flattened) throws GrokException { Map mappedw = GrokUtils.namedGroups(this.match, this.grok.namedGroups); - mappedw.forEach((key, valueString) -> { - String id = this.grok.getNamedRegexCollectionById(key); - if (id != null && !id.isEmpty()) { - key = id; - } - - if ("UNWANTED".equals(key)) { - return; - } - - Object value = valueString; - if (valueString != null) { - IConverter converter = grok.converters.get(key); - - if (converter != null) { - key = Converter.extractKey(key); - try { - value = converter.convert(valueString); - } catch (Exception e) { - capture.put(key + "_grokfailure", e.toString()); + mappedw.forEach( + (key, valueString) -> { + String id = this.grok.getNamedRegexCollectionById(key); + if (id != null && !id.isEmpty()) { + key = id; } - if (value instanceof String) { - value = cleanString((String) value); + if ("UNWANTED".equals(key)) { + return; } - } else { - value = cleanString(valueString); - } - } else if (!isKeepEmptyCaptures()) { - return; - } - - if (capture.containsKey(key)) { - Object currentValue = capture.get(key); - if (flattened) { - if (currentValue == null && value != null) { - capture.put(key, value); - } - if (currentValue != null && value != null) { - throw new GrokException( - format( - "key '%s' has multiple non-null values, this is not allowed in flattened mode," - + " values:'%s', '%s'", - key, - currentValue, - value)); + Object value = valueString; + if (valueString != null) { + IConverter converter = grok.converters.get(key); + + if (converter != null) { + key = Converter.extractKey(key); + try { + value = converter.convert(valueString); + } catch (Exception e) { + capture.put(key + "_grokfailure", e.toString()); + } + + if (value instanceof String) { + value = cleanString((String) value); + } + } else { + value = cleanString(valueString); + } + } else if (!isKeepEmptyCaptures()) { + return; } - } else { - if (currentValue instanceof List) { - @SuppressWarnings("unchecked") - List cvl = (List) currentValue; - cvl.add(value); + + if (capture.containsKey(key)) { + Object currentValue = capture.get(key); + + if (flattened) { + if (currentValue == null && value != null) { + capture.put(key, value); + } + if (currentValue != null && value != null) { + throw new GrokException( + format( + "key '%s' has multiple non-null values, this is not allowed in flattened" + + " mode, values:'%s', '%s'", + key, currentValue, value)); + } + } else { + if (currentValue instanceof List) { + @SuppressWarnings("unchecked") + List cvl = (List) currentValue; + cvl.add(value); + } else { + List list = new ArrayList(); + list.add(currentValue); + list.add(value); + capture.put(key, list); + } + } } else { - List list = new ArrayList(); - list.add(currentValue); - list.add(value); - capture.put(key, list); + capture.put(key, value); } - } - } else { - capture.put(key, value); - } - }); + }); capture = Collections.unmodifiableMap(capture); @@ -189,13 +181,11 @@ private Map capture(boolean flattened) throws GrokException { /** * Match to the subject the regex and save the matched element into a map * - *

Multiple values to the same key are flattened to one value: the sole non-null value will - * be captured. - * Should there be multiple non-null values a RuntimeException is being thrown. + *

Multiple values to the same key are flattened to one value: the sole non-null value will be + * captured. Should there be multiple non-null values a RuntimeException is being thrown. * *

This can be used in cases like: (foo (.*:message) bar|bar (.*:message) foo) where the regexp - * guarantees that only - * one value will be captured. + * guarantees that only one value will be captured. * *

See also {@link #capture} which returns multiple values of the same key as list. * @@ -220,9 +210,7 @@ private String cleanString(String value) { char firstChar = value.charAt(0); char lastChar = value.charAt(value.length() - 1); - if (firstChar == lastChar - && (firstChar == '"' || firstChar == '\'') - ) { + if (firstChar == lastChar && (firstChar == '"' || firstChar == '\'')) { if (value.length() <= 2) { return ""; } else { @@ -249,5 +237,4 @@ private String cleanString(String value) { public Boolean isNull() { return this.match == null; } - } diff --git a/common/src/main/java/org/opensearch/sql/common/grok/exception/GrokException.java b/common/src/main/java/org/opensearch/sql/common/grok/exception/GrokException.java index 54ca8aada3a..0e9d6d2ddf4 100644 --- a/common/src/main/java/org/opensearch/sql/common/grok/exception/GrokException.java +++ b/common/src/main/java/org/opensearch/sql/common/grok/exception/GrokException.java @@ -6,9 +6,8 @@ package org.opensearch.sql.common.grok.exception; /** - * Signals that an {@code Grok} exception of some sort has occurred. - * This class is the general class of - * exceptions produced by failed or interrupted Grok operations. + * Signals that an {@code Grok} exception of some sort has occurred. This class is the general class + * of exceptions produced by failed or interrupted Grok operations. * * @since 0.0.4 */ @@ -16,9 +15,7 @@ public class GrokException extends RuntimeException { private static final long serialVersionUID = 1L; - /** - * Creates a new GrokException. - */ + /** Creates a new GrokException. */ public GrokException() { super(); } @@ -27,7 +24,7 @@ public GrokException() { * Constructs a new GrokException. * * @param message the reason for the exception - * @param cause the underlying Throwable that caused this exception to be thrown. + * @param cause the underlying Throwable that caused this exception to be thrown. */ public GrokException(String message, Throwable cause) { super(message, cause); @@ -50,5 +47,4 @@ public GrokException(String message) { public GrokException(Throwable cause) { super(cause); } - } diff --git a/common/src/main/java/org/opensearch/sql/common/response/ResponseListener.java b/common/src/main/java/org/opensearch/sql/common/response/ResponseListener.java index 3d5eadc692a..bac79ddbbd4 100644 --- a/common/src/main/java/org/opensearch/sql/common/response/ResponseListener.java +++ b/common/src/main/java/org/opensearch/sql/common/response/ResponseListener.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.response; /** diff --git a/common/src/main/java/org/opensearch/sql/common/setting/LegacySettings.java b/common/src/main/java/org/opensearch/sql/common/setting/LegacySettings.java index 172a0d8023a..e8dc76645ae 100644 --- a/common/src/main/java/org/opensearch/sql/common/setting/LegacySettings.java +++ b/common/src/main/java/org/opensearch/sql/common/setting/LegacySettings.java @@ -3,42 +3,31 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.setting; import lombok.Getter; import lombok.RequiredArgsConstructor; -/** - * Legacy Open Distro Settings. - */ +/** Legacy Open Distro Settings. */ public abstract class LegacySettings { @RequiredArgsConstructor public enum Key { - /** - * Legacy SQL Settings. - */ + /** Legacy SQL Settings. */ SQL_ENABLED("opendistro.sql.enabled"), SQL_QUERY_SLOWLOG("opendistro.sql.query.slowlog"), SQL_CURSOR_KEEPALIVE("opendistro.sql.cursor.keep_alive"), METRICS_ROLLING_WINDOW("opendistro.sql.metrics.rollingwindow"), METRICS_ROLLING_INTERVAL("opendistro.sql.metrics.rollinginterval"), - /** - * Legacy PPL Settings. - */ + /** Legacy PPL Settings. */ PPL_ENABLED("opendistro.ppl.enabled"), PPL_QUERY_MEMORY_LIMIT("opendistro.ppl.query.memory_limit"), - /** - * Legacy Common Settings. - */ + /** Legacy Common Settings. */ QUERY_SIZE_LIMIT("opendistro.query.size_limit"), - /** - * Deprecated Settings. - */ + /** Deprecated Settings. */ SQL_NEW_ENGINE_ENABLED("opendistro.sql.engine.new.enabled"), QUERY_ANALYSIS_ENABLED("opendistro.sql.query.analysis.enabled"), QUERY_ANALYSIS_SEMANTIC_SUGGESTION("opendistro.sql.query.analysis.semantic.suggestion"), @@ -47,8 +36,7 @@ public enum Key { SQL_CURSOR_ENABLED("opendistro.sql.cursor.enabled"), SQL_CURSOR_FETCH_SIZE("opendistro.sql.cursor.fetch_size"); - @Getter - private final String keyValue; + @Getter private final String keyValue; } public abstract T getSettingValue(Key key); diff --git a/common/src/main/java/org/opensearch/sql/common/setting/Settings.java b/common/src/main/java/org/opensearch/sql/common/setting/Settings.java index 3b0eba157d4..1e5243f91f9 100644 --- a/common/src/main/java/org/opensearch/sql/common/setting/Settings.java +++ b/common/src/main/java/org/opensearch/sql/common/setting/Settings.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.setting; import com.google.common.base.Strings; @@ -14,29 +13,21 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -/** - * Setting. - */ +/** Setting. */ public abstract class Settings { @RequiredArgsConstructor public enum Key { - /** - * SQL Settings. - */ + /** SQL Settings. */ SQL_ENABLED("plugins.sql.enabled"), SQL_SLOWLOG("plugins.sql.slowlog"), SQL_CURSOR_KEEP_ALIVE("plugins.sql.cursor.keep_alive"), SQL_DELETE_ENABLED("plugins.sql.delete.enabled"), - /** - * PPL Settings. - */ + /** PPL Settings. */ PPL_ENABLED("plugins.ppl.enabled"), - /** - * Common Settings for SQL and PPL. - */ + /** Common Settings for SQL and PPL. */ QUERY_MEMORY_LIMIT("plugins.query.memory_limit"), QUERY_SIZE_LIMIT("plugins.query.size_limit"), ENCYRPTION_MASTER_KEY("plugins.query.datasources.encryption.masterkey"), @@ -47,8 +38,7 @@ public enum Key { CLUSTER_NAME("cluster.name"); - @Getter - private final String keyValue; + @Getter private final String keyValue; private static final Map ALL_KEYS; @@ -66,9 +56,7 @@ public static Optional of(String keyValue) { } } - /** - * Get Setting Value. - */ + /** Get Setting Value. */ public abstract T getSettingValue(Key key); public abstract List getSettings(); diff --git a/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java b/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java index ab11029d73e..04b97332adc 100644 --- a/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java +++ b/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.utils; import java.time.LocalDateTime; @@ -13,25 +12,23 @@ import org.apache.logging.log4j.ThreadContext; /** - * Utility class for recording and accessing context for the query being executed. - * Implementation Details: context variables is being persisted statically in the thread context + * Utility class for recording and accessing context for the query being executed. Implementation + * Details: context variables is being persisted statically in the thread context + * * @see: @ThreadContext */ public class QueryContext { - /** - * The key of the request id in the context map. - */ + /** The key of the request id in the context map. */ private static final String REQUEST_ID_KEY = "request_id"; /** * Generates a random UUID and adds to the {@link ThreadContext} as the request id. - *

- * Note: If a request id already present, this method will overwrite it with a new - * one. This is to pre-vent re-using the same request id for different requests in - * case the same thread handles both of them. But this also means one should not - * call this method twice on the same thread within the lifetime of the request. - *

+ * + *

Note: If a request id already present, this method will overwrite it with a new one. This is + * to pre-vent re-using the same request id for different requests in case the same thread handles + * both of them. But this also means one should not call this method twice on the same thread + * within the lifetime of the request. */ public static String addRequestId() { var id = UUID.randomUUID().toString(); @@ -41,6 +38,7 @@ public static String addRequestId() { /** * Get RequestID. + * * @return the current request id from {@link ThreadContext}. */ public static String getRequestId() { @@ -52,8 +50,8 @@ public static String getRequestId() { } /** - * Wraps a given instance of {@link Runnable} into a new one which gets all the - * entries from current ThreadContext map. + * Wraps a given instance of {@link Runnable} into a new one which gets all the entries from + * current ThreadContext map. * * @param task the instance of Runnable to wrap * @return the new task diff --git a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java index 06992453383..4b7752a9de5 100644 --- a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java +++ b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.utils; import com.google.common.base.Strings; @@ -12,52 +11,38 @@ public class StringUtils { /** - * Unquote any string with mark specified. - * @param text string - * @param mark quotation mark - * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been - * removed - */ - public static String unquote(String text, String mark) { - if (isQuoted(text, mark)) { - return text.substring(mark.length(), text.length() - mark.length()); - } - return text; - } - - /** - * Unquote Identifier which has " or ' or ` as mark. - * Strings quoted by ' or " with two of these quotes appearing next to each other in the quote - * acts as an escape + * Unquote Identifier which has " or ' as mark. Strings quoted by ' or " with two of these quotes + * appearing next to each other in the quote acts as an escape
* Example: 'Test''s' will result in 'Test's', similar with those single quotes being replaced - * with double. + * with double quote. Supports escaping quotes (single/double) and escape characters using the `\` + * characters. + * * @param text string - * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been - * removed + * @return An unquoted string whose outer pair of (single/double) quotes have been removed */ public static String unquoteText(String text) { - if (text.length() < 2) { return text; } - char enclosingQuote; + char enclosingQuote = 0; char firstChar = text.charAt(0); char lastChar = text.charAt(text.length() - 1); - if (firstChar == lastChar - && (firstChar == '\'' - || firstChar == '"' - || firstChar == '`')) { - enclosingQuote = firstChar; - } else { + if (firstChar != lastChar) { return text; } - if (enclosingQuote == '`') { + if (firstChar == '`') { return text.substring(1, text.length() - 1); } + if (firstChar == lastChar && (firstChar == '\'' || firstChar == '"')) { + enclosingQuote = firstChar; + } else { + return text; + } + char currentChar; char nextChar; @@ -67,21 +52,22 @@ public static String unquoteText(String text) { for (int chIndex = 1; chIndex < text.length() - 1; chIndex++) { currentChar = text.charAt(chIndex); nextChar = text.charAt(chIndex + 1); - if (currentChar == enclosingQuote - && nextChar == currentChar) { + + if ((currentChar == '\\' && (nextChar == '"' || nextChar == '\\' || nextChar == '\'')) + || (currentChar == nextChar && currentChar == enclosingQuote)) { chIndex++; + currentChar = nextChar; } textSB.append(currentChar); } - return textSB.toString(); } /** * Unquote Identifier which has ` as mark. + * * @param identifier identifier that possibly enclosed by double quotes or back ticks - * @return An unquoted string whose outer pair of (double/back-tick) quotes have been - * removed + * @return An unquoted string whose outer pair of (double/back-tick) quotes have been removed */ public static String unquoteIdentifier(String identifier) { if (isQuoted(identifier, "`")) { @@ -92,16 +78,15 @@ public static String unquoteIdentifier(String identifier) { } /** - * Returns a formatted string using the specified format string and - * arguments, as well as the {@link Locale#ROOT} locale. + * Returns a formatted string using the specified format string and arguments, as well as the + * {@link Locale#ROOT} locale. * * @param format format string - * @param args arguments referenced by the format specifiers in the format string + * @param args arguments referenced by the format specifiers in the format string * @return A formatted string * @throws IllegalFormatException If a format string contains an illegal syntax, a format - * specifier that is incompatible with the given arguments, - * insufficient arguments given the format string, or other - * illegal conditions. + * specifier that is incompatible with the given arguments, insufficient arguments given the + * format string, or other illegal conditions. * @see java.lang.String#format(Locale, String, Object...) */ public static String format(final String format, Object... args) { diff --git a/common/src/test/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptorTest.java b/common/src/test/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptorTest.java index 894f3974ce5..435ac9dc937 100644 --- a/common/src/test/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptorTest.java +++ b/common/src/test/java/org/opensearch/sql/common/authinterceptors/AwsSigningInterceptorTest.java @@ -7,9 +7,6 @@ package org.opensearch.sql.common.authinterceptors; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.AWSSessionCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; @@ -26,42 +23,40 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.opensearch.sql.common.authinterceptors.AwsSigningInterceptor; @ExtendWith(MockitoExtension.class) public class AwsSigningInterceptorTest { - @Mock - private Interceptor.Chain chain; + @Mock private Interceptor.Chain chain; - @Captor - ArgumentCaptor requestArgumentCaptor; + @Captor ArgumentCaptor requestArgumentCaptor; - @Mock - private STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider; + @Mock private STSAssumeRoleSessionCredentialsProvider stsAssumeRoleSessionCredentialsProvider; @Test void testConstructors() { - Assertions.assertThrows(NullPointerException.class, () -> - new AwsSigningInterceptor(null, "us-east-1", "aps")); - Assertions.assertThrows(NullPointerException.class, () -> - new AwsSigningInterceptor(getStaticAWSCredentialsProvider("accessKey", "secretKey"), null, - "aps")); - Assertions.assertThrows(NullPointerException.class, () -> - new AwsSigningInterceptor(getStaticAWSCredentialsProvider("accessKey", "secretKey"), - "us-east-1", null)); + Assertions.assertThrows( + NullPointerException.class, () -> new AwsSigningInterceptor(null, "us-east-1", "aps")); + Assertions.assertThrows( + NullPointerException.class, + () -> + new AwsSigningInterceptor( + getStaticAWSCredentialsProvider("accessKey", "secretKey"), null, "aps")); + Assertions.assertThrows( + NullPointerException.class, + () -> + new AwsSigningInterceptor( + getStaticAWSCredentialsProvider("accessKey", "secretKey"), "us-east-1", null)); } @Test @SneakyThrows void testIntercept() { - Mockito.when(chain.request()).thenReturn(new Request.Builder() - .url("http://localhost:9090") - .build()); - AwsSigningInterceptor awsSigningInterceptor - = new AwsSigningInterceptor( - getStaticAWSCredentialsProvider("testAccessKey", "testSecretKey"), - "us-east-1", "aps"); + Mockito.when(chain.request()) + .thenReturn(new Request.Builder().url("http://localhost:9090").build()); + AwsSigningInterceptor awsSigningInterceptor = + new AwsSigningInterceptor( + getStaticAWSCredentialsProvider("testAccessKey", "testSecretKey"), "us-east-1", "aps"); awsSigningInterceptor.intercept(chain); Mockito.verify(chain).proceed(requestArgumentCaptor.capture()); Request request = requestArgumentCaptor.getValue(); @@ -70,31 +65,26 @@ void testIntercept() { Assertions.assertNotNull(request.headers("host")); } - @Test @SneakyThrows void testSTSCredentialsProviderInterceptor() { - Mockito.when(chain.request()).thenReturn(new Request.Builder() - .url("http://localhost:9090") - .build()); + Mockito.when(chain.request()) + .thenReturn(new Request.Builder().url("http://localhost:9090").build()); Mockito.when(stsAssumeRoleSessionCredentialsProvider.getCredentials()) .thenReturn(getAWSSessionCredentials()); - AwsSigningInterceptor awsSigningInterceptor - = new AwsSigningInterceptor(stsAssumeRoleSessionCredentialsProvider, - "us-east-1", "aps"); + AwsSigningInterceptor awsSigningInterceptor = + new AwsSigningInterceptor(stsAssumeRoleSessionCredentialsProvider, "us-east-1", "aps"); awsSigningInterceptor.intercept(chain); Mockito.verify(chain).proceed(requestArgumentCaptor.capture()); Request request = requestArgumentCaptor.getValue(); Assertions.assertNotNull(request.headers("Authorization")); Assertions.assertNotNull(request.headers("x-amz-date")); Assertions.assertNotNull(request.headers("host")); - Assertions.assertEquals("session_token", - request.headers("x-amz-security-token").get(0)); + Assertions.assertEquals("session_token", request.headers("x-amz-security-token").get(0)); } - - private AWSCredentialsProvider getStaticAWSCredentialsProvider(String accessKey, - String secretKey) { + private AWSCredentialsProvider getStaticAWSCredentialsProvider( + String accessKey, String secretKey) { return new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)); } @@ -116,5 +106,4 @@ public String getAWSSecretKey() { } }; } - } diff --git a/common/src/test/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptorTest.java b/common/src/test/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptorTest.java index 596894da6db..d59928d2efa 100644 --- a/common/src/test/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptorTest.java +++ b/common/src/test/java/org/opensearch/sql/common/authinterceptors/BasicAuthenticationInterceptorTest.java @@ -24,29 +24,25 @@ @ExtendWith(MockitoExtension.class) public class BasicAuthenticationInterceptorTest { - @Mock - private Interceptor.Chain chain; + @Mock private Interceptor.Chain chain; - @Captor - ArgumentCaptor requestArgumentCaptor; + @Captor ArgumentCaptor requestArgumentCaptor; @Test void testConstructors() { - Assertions.assertThrows(NullPointerException.class, () -> - new BasicAuthenticationInterceptor(null, "test")); - Assertions.assertThrows(NullPointerException.class, () -> - new BasicAuthenticationInterceptor("testAdmin", null)); + Assertions.assertThrows( + NullPointerException.class, () -> new BasicAuthenticationInterceptor(null, "test")); + Assertions.assertThrows( + NullPointerException.class, () -> new BasicAuthenticationInterceptor("testAdmin", null)); } - @Test @SneakyThrows void testIntercept() { - Mockito.when(chain.request()).thenReturn(new Request.Builder() - .url("http://localhost:9090") - .build()); - BasicAuthenticationInterceptor basicAuthenticationInterceptor - = new BasicAuthenticationInterceptor("testAdmin", "testPassword"); + Mockito.when(chain.request()) + .thenReturn(new Request.Builder().url("http://localhost:9090").build()); + BasicAuthenticationInterceptor basicAuthenticationInterceptor = + new BasicAuthenticationInterceptor("testAdmin", "testPassword"); basicAuthenticationInterceptor.intercept(chain); Mockito.verify(chain).proceed(requestArgumentCaptor.capture()); Request request = requestArgumentCaptor.getValue(); @@ -54,5 +50,4 @@ void testIntercept() { Collections.singletonList(Credentials.basic("testAdmin", "testPassword")), request.headers("Authorization")); } - } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/ApacheDataTypeTest.java b/common/src/test/java/org/opensearch/sql/common/grok/ApacheDataTypeTest.java index 09695c8220a..7eb0e964dee 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/ApacheDataTypeTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/ApacheDataTypeTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.common.grok; - import static org.junit.Assert.assertEquals; import com.google.common.io.Resources; @@ -42,12 +41,13 @@ public void setup() throws Exception { @Test public void test002_httpd_access_semi() throws GrokException { - Grok grok = compiler.compile( - "%{IPORHOST:clientip} %{USER:ident;boolean} %{USER:auth} " - + "\\[%{HTTPDATE:timestamp;date;dd/MMM/yyyy:HH:mm:ss Z}\\] \"(?:%{WORD:verb;string} " - + "%{NOTSPACE:request}" - + "(?: HTTP/%{NUMBER:httpversion;float})?|%{DATA:rawrequest})\" %{NUMBER:response;int} " - + "(?:%{NUMBER:bytes;long}|-)"); + Grok grok = + compiler.compile( + "%{IPORHOST:clientip} %{USER:ident;boolean} %{USER:auth}" + + " \\[%{HTTPDATE:timestamp;date;dd/MMM/yyyy:HH:mm:ss Z}\\]" + + " \"(?:%{WORD:verb;string} %{NOTSPACE:request}(?:" + + " HTTP/%{NUMBER:httpversion;float})?|%{DATA:rawrequest})\" %{NUMBER:response;int}" + + " (?:%{NUMBER:bytes;long}|-)"); System.out.println(line); Match gm = grok.match(line); @@ -61,17 +61,17 @@ public void test002_httpd_access_semi() throws GrokException { assertEquals(map.get("httpversion"), 1.1f); assertEquals(map.get("bytes"), 12846L); assertEquals("GET", map.get("verb")); - } @Test public void test002_httpd_access_colon() throws GrokException { - Grok grok = compiler.compile( - "%{IPORHOST:clientip} %{USER:ident:boolean} %{USER:auth} " - + "\\[%{HTTPDATE:timestamp:date:dd/MMM/yyyy:HH:mm:ss Z}\\] \"(?:%{WORD:verb:string} " - + "%{NOTSPACE:request}" - + "(?: HTTP/%{NUMBER:httpversion:float})?|%{DATA:rawrequest})\" %{NUMBER:response:int} " - + "(?:%{NUMBER:bytes:long}|-)"); + Grok grok = + compiler.compile( + "%{IPORHOST:clientip} %{USER:ident:boolean} %{USER:auth}" + + " \\[%{HTTPDATE:timestamp:date:dd/MMM/yyyy:HH:mm:ss Z}\\]" + + " \"(?:%{WORD:verb:string} %{NOTSPACE:request}(?:" + + " HTTP/%{NUMBER:httpversion:float})?|%{DATA:rawrequest})\" %{NUMBER:response:int}" + + " (?:%{NUMBER:bytes:long}|-)"); Match gm = grok.match(line); Map map = gm.capture(); @@ -85,6 +85,5 @@ public void test002_httpd_access_colon() throws GrokException { assertEquals(map.get("httpversion"), 1.1f); assertEquals(map.get("bytes"), 12846L); assertEquals("GET", map.get("verb")); - } } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/ApacheTest.java b/common/src/test/java/org/opensearch/sql/common/grok/ApacheTest.java index 33113d1996e..db420b16d35 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/ApacheTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/ApacheTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.common.grok; - import com.google.common.io.Resources; import java.io.BufferedReader; import java.io.File; @@ -65,5 +64,4 @@ public void test002_nasa_httpd_access() throws GrokException, IOException { br.close(); } } - } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/BasicTest.java b/common/src/test/java/org/opensearch/sql/common/grok/BasicTest.java index 26df7ba57ed..c724b58f3ee 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/BasicTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/BasicTest.java @@ -33,8 +33,7 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class BasicTest { - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); private GrokCompiler compiler; @@ -111,8 +110,8 @@ public void test005_testLoadPatternFromFile() throws IOException, GrokException public void test006_testLoadPatternFromFileIso_8859_1() throws IOException, GrokException { File temp = tempFolder.newFile("grok-tmp-pattern"); try (FileOutputStream fis = new FileOutputStream(temp); - BufferedWriter bw = new BufferedWriter( - new OutputStreamWriter(fis, StandardCharsets.ISO_8859_1))) { + BufferedWriter bw = + new BufferedWriter(new OutputStreamWriter(fis, StandardCharsets.ISO_8859_1))) { bw.write("TEST \\u2022"); } @@ -130,5 +129,4 @@ public void test007_testLoadPatternFromReader() throws IOException, GrokExceptio Grok grok = compiler.compile("%{TEST}"); assertEquals("(?\\u20AC)", grok.getNamedRegex()); } - } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/CaptureTest.java b/common/src/test/java/org/opensearch/sql/common/grok/CaptureTest.java index 1173541e161..60e2761c83d 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/CaptureTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/CaptureTest.java @@ -98,7 +98,8 @@ public void test005_captureSubName() throws GrokException { Map map = match.capture(); assertEquals(1, map.size()); assertEquals("Hello", map.get(subname).toString()); - assertEquals("{abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_abcdef=Hello}", + assertEquals( + "{abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_abcdef=Hello}", map.toString()); } @@ -145,7 +146,8 @@ public void test008_flattenDuplicateKeys() throws GrokException { m3.captureFlattened(); fail("should report error due tu ambiguity"); } catch (RuntimeException e) { - assertThat(e.getMessage(), + assertThat( + e.getMessage(), containsString("has multiple non-null values, this is not allowed in flattened mode")); } } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/GrokDocumentationTest.java b/common/src/test/java/org/opensearch/sql/common/grok/GrokDocumentationTest.java index 22115a825f9..15d450e812f 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/GrokDocumentationTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/GrokDocumentationTest.java @@ -40,23 +40,43 @@ public void assureCodeInReadmeWorks() { Assertions.assertThat(capture).hasSize(22); final boolean debug = false; - final Object[] keywordArray = new Object[] {"COMBINEDAPACHELOG", - "COMMONAPACHELOG", "clientip", "ident", "auth", "timestamp", "MONTHDAY", - "MONTH", "YEAR", "TIME", "HOUR", "MINUTE", "SECOND", "INT", "verb", - "httpversion", "rawrequest", "request", "response", "bytes", "referrer", - "agent"}; + final Object[] keywordArray = + new Object[] { + "COMBINEDAPACHELOG", + "COMMONAPACHELOG", + "clientip", + "ident", + "auth", + "timestamp", + "MONTHDAY", + "MONTH", + "YEAR", + "TIME", + "HOUR", + "MINUTE", + "SECOND", + "INT", + "verb", + "httpversion", + "rawrequest", + "request", + "response", + "bytes", + "referrer", + "agent" + }; if (debug) { capture.keySet().stream().forEach(System.err::println); } - assertTrue(new HashSet(Arrays.asList(keywordArray)) - .containsAll(new HashSet(capture.keySet()))); + assertTrue( + new HashSet(Arrays.asList(keywordArray)) + .containsAll(new HashSet(capture.keySet()))); Arrays.asList(keywordArray).stream() .forEach(o -> assertThat(capture.keySet(), hasItem((String) o))); - assertThat(new HashSet(capture.keySet()), - containsInAnyOrder(keywordArray)); - assertTrue(new HashSet(capture.keySet()) - .containsAll(new HashSet(Arrays.asList(keywordArray)))); - + assertThat(new HashSet(capture.keySet()), containsInAnyOrder(keywordArray)); + assertTrue( + new HashSet(capture.keySet()) + .containsAll(new HashSet(Arrays.asList(keywordArray)))); } } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/GrokTest.java b/common/src/test/java/org/opensearch/sql/common/grok/GrokTest.java index b5e83668076..862f9b8195e 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/GrokTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/GrokTest.java @@ -37,7 +37,6 @@ import org.junit.runners.MethodSorters; import org.opensearch.sql.common.grok.exception.GrokException; - @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class GrokTest { @@ -138,7 +137,6 @@ public void test002_numbers() { Match gm = grok.match("-42"); Map map = gm.capture(); assertEquals("{NUMBER=-42}", map.toString()); - } @Test @@ -152,7 +150,6 @@ public void test003_word() { gm = grok.match("abc"); map = gm.capture(); assertEquals("{WORD=abc}", map.toString()); - } @Test @@ -162,7 +159,6 @@ public void test004_space() { Match gm = grok.match("abc dc"); Map map = gm.capture(); assertEquals("{SPACE=}", map.toString()); - } @Test @@ -172,7 +168,6 @@ public void test004_number() { Match gm = grok.match("Something costs $55.4!"); Map map = gm.capture(); assertEquals("{NUMBER=55.4}", map.toString()); - } @Test @@ -182,7 +177,6 @@ public void test005_notSpace() { Match gm = grok.match("abc dc"); Map map = gm.capture(); assertEquals("{NOTSPACE=abc}", map.toString()); - } @Test @@ -209,7 +203,6 @@ public void test007_uuid() { gm = grok.match("03A8413C-F604-4D21-8F4D-24B19D98B5A7"); map = gm.capture(); assertEquals("{UUID=03A8413C-F604-4D21-8F4D-24B19D98B5A7}", map.toString()); - } @Test @@ -219,7 +212,6 @@ public void test008_mac() { Match gm = grok.match("5E:FF:56:A2:AF:15"); Map map = gm.capture(); assertEquals("{MAC=5E:FF:56:A2:AF:15}", map.toString()); - } @Test @@ -241,10 +233,12 @@ public void test010_hostPort() { Match gm = grok.match("www.google.fr:80"); Map map = gm.capture(); - assertEquals(ImmutableMap.of( - "HOSTPORT", "www.google.fr:80", - "IPORHOST", "www.google.fr", - "PORT", "80"), map); + assertEquals( + ImmutableMap.of( + "HOSTPORT", "www.google.fr:80", + "IPORHOST", "www.google.fr", + "PORT", "80"), + map); } @Test @@ -267,10 +261,11 @@ public void test011_combineApache() { assertEquals(map.get("TIME").toString(), "01:36:30"); gm = - grok.match("112.169.19.192 - - [06/Mar/2013:01:36:30 +0900] \"GET " - + "/wp-content/plugins/easy-table/themes/default/style.css?ver=1.0 HTTP/1.1\" " - + "304 - \"http://www.nflabs.com/\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " - + "AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22\""); + grok.match( + "112.169.19.192 - - [06/Mar/2013:01:36:30 +0900] \"GET" + + " /wp-content/plugins/easy-table/themes/default/style.css?ver=1.0 HTTP/1.1\" 304" + + " - \"http://www.nflabs.com/\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)" + + " AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22\""); map = gm.capture(); assertEquals( map.get("agent").toString(), @@ -278,7 +273,8 @@ public void test011_combineApache() { + "Chrome/25.0.1364.152 Safari/537.22"); assertEquals(map.get("clientip").toString(), "112.169.19.192"); assertEquals(map.get("httpversion").toString(), "1.1"); - assertEquals(map.get("request").toString(), + assertEquals( + map.get("request").toString(), "/wp-content/plugins/easy-table/themes/default/style.css?ver=1.0"); assertEquals(map.get("TIME").toString(), "01:36:30"); } @@ -319,7 +315,7 @@ public void test013_IpSet() throws Throwable { Grok grok = compiler.compile("%{IP}"); try (FileReader fr = new FileReader(Resources.getResource(ResourceManager.IP).getFile()); - BufferedReader br = new BufferedReader(fr)) { + BufferedReader br = new BufferedReader(fr)) { String line; System.out.println("Starting test with ip"); while ((line = br.readLine()) != null) { @@ -336,10 +332,31 @@ public void test014_month() { Grok grok = compiler.compile("%{MONTH}"); - String[] months = - {"Jan", "January", "Feb", "February", "Mar", "March", "Apr", "April", "May", "Jun", "June", - "Jul", "July", "Aug", "August", "Sep", "September", "Oct", "October", "Nov", - "November", "Dec", "December"}; + String[] months = { + "Jan", + "January", + "Feb", + "February", + "Mar", + "March", + "Apr", + "April", + "May", + "Jun", + "June", + "Jul", + "July", + "Aug", + "August", + "Sep", + "September", + "Oct", + "October", + "Nov", + "November", + "Dec", + "December" + }; int counter = 0; for (String month : months) { Match match = grok.match(month); @@ -355,20 +372,21 @@ public void test015_iso8601() throws GrokException { Grok grok = compiler.compile("%{TIMESTAMP_ISO8601}"); String[] times = { - "2001-01-01T00:00:00", - "1974-03-02T04:09:09", - "2010-05-03T08:18:18+00:00", - "2004-07-04T12:27:27-00:00", - "2001-09-05T16:36:36+0000", - "2001-11-06T20:45:45-0000", - "2001-12-07T23:54:54Z", - "2001-01-01T00:00:00.123456", - "1974-03-02T04:09:09.123456", - "2010-05-03T08:18:18.123456+00:00", - "2004-07-04T12:27:27.123456-00:00", - "2001-09-05T16:36:36.123456+0000", - "2001-11-06T20:45:45.123456-0000", - "2001-12-07T23:54:54.123456Z"}; + "2001-01-01T00:00:00", + "1974-03-02T04:09:09", + "2010-05-03T08:18:18+00:00", + "2004-07-04T12:27:27-00:00", + "2001-09-05T16:36:36+0000", + "2001-11-06T20:45:45-0000", + "2001-12-07T23:54:54Z", + "2001-01-01T00:00:00.123456", + "1974-03-02T04:09:09.123456", + "2010-05-03T08:18:18.123456+00:00", + "2004-07-04T12:27:27.123456-00:00", + "2001-09-05T16:36:36.123456+0000", + "2001-11-06T20:45:45.123456-0000", + "2001-12-07T23:54:54.123456Z" + }; int counter = 0; for (String time : times) { @@ -385,33 +403,34 @@ public void test016_uri() throws GrokException { Grok grok = compiler.compile("%{URI}"); String[] uris = { - "http://www.google.com", - "telnet://helloworld", - "http://www.example.com/", - "http://www.example.com/test.html", - "http://www.example.com/test.html?foo=bar", - "http://www.example.com/test.html?foo=bar&fizzle=baz", - "http://www.example.com:80/test.html?foo=bar&fizzle=baz", - "https://www.example.com:443/test.html?foo=bar&fizzle=baz", - "https://user@www.example.com:443/test.html?foo=bar&fizzle=baz", - "https://user:pass@somehost/fetch.pl", - "puppet:///", - "http://www.foo.com", - "http://www.foo.com/", - "http://www.foo.com/?testing", - "http://www.foo.com/?one=two", - "http://www.foo.com/?one=two&foo=bar", - "foo://somehost.com:12345", - "foo://user@somehost.com:12345", - "foo://user@somehost.com:12345/", - "foo://user@somehost.com:12345/foo.bar/baz/fizz", - "foo://user@somehost.com:12345/foo.bar/baz/fizz?test", - "foo://user@somehost.com:12345/foo.bar/baz/fizz?test=1&sink&foo=4", - "http://www.google.com/search?hl=en&source=hp&q=hello+world+%5E%40%23%24&btnG=Google+Search", - "http://www.freebsd.org/cgi/url.cgi?ports/sysutils/grok/pkg-descr", - "http://www.google.com/search?q=CAPTCHA+ssh&start=0&ie=utf-8&oe=utf-8&client=firefox-a" - + "&rls=org.mozilla:en-US:official", - "svn+ssh://somehost:12345/testing"}; + "http://www.google.com", + "telnet://helloworld", + "http://www.example.com/", + "http://www.example.com/test.html", + "http://www.example.com/test.html?foo=bar", + "http://www.example.com/test.html?foo=bar&fizzle=baz", + "http://www.example.com:80/test.html?foo=bar&fizzle=baz", + "https://www.example.com:443/test.html?foo=bar&fizzle=baz", + "https://user@www.example.com:443/test.html?foo=bar&fizzle=baz", + "https://user:pass@somehost/fetch.pl", + "puppet:///", + "http://www.foo.com", + "http://www.foo.com/", + "http://www.foo.com/?testing", + "http://www.foo.com/?one=two", + "http://www.foo.com/?one=two&foo=bar", + "foo://somehost.com:12345", + "foo://user@somehost.com:12345", + "foo://user@somehost.com:12345/", + "foo://user@somehost.com:12345/foo.bar/baz/fizz", + "foo://user@somehost.com:12345/foo.bar/baz/fizz?test", + "foo://user@somehost.com:12345/foo.bar/baz/fizz?test=1&sink&foo=4", + "http://www.google.com/search?hl=en&source=hp&q=hello+world+%5E%40%23%24&btnG=Google+Search", + "http://www.freebsd.org/cgi/url.cgi?ports/sysutils/grok/pkg-descr", + "http://www.google.com/search?q=CAPTCHA+ssh&start=0&ie=utf-8&oe=utf-8&client=firefox-a" + + "&rls=org.mozilla:en-US:official", + "svn+ssh://somehost:12345/testing" + }; int counter = 0; for (String uri : uris) { @@ -429,10 +448,7 @@ public void test017_nonMachingList() throws GrokException { Grok grok = compiler.compile("%{URI}"); String[] uris = { - "http://www.google.com", - "telnet://helloworld", - "", - "svn+ssh://somehost:12345/testing" + "http://www.google.com", "telnet://helloworld", "", "svn+ssh://somehost:12345/testing" }; int counter = 0; @@ -458,9 +474,7 @@ public void test018_namedOnlySimpleCase() throws GrokException { String text = "<< barfoobarfoo >>"; Match match = grok.match(text); Map map = match.capture(); - assertEquals("unable to parse: " + text, - text, - map.get("text")); + assertEquals("unable to parse: " + text, text, map.get("text")); } @Test @@ -488,9 +502,7 @@ private void testPatternRepetitions(boolean namedOnly, String pattern) throws Gr private void assertMatches(String description, Grok grok, String text) { Match match = grok.match(text); Map map = match.capture(); - assertEquals(format("%s: unable to parse '%s'", description, text), - text, - map.get("text")); + assertEquals(format("%s: unable to parse '%s'", description, text), text, map.get("text")); } @Test @@ -630,8 +642,8 @@ public void createGrokWithDefaultPatterns() throws GrokException { compiler.compile("%{USERNAME}", false); } - private void ensureAbortsWithDefinitionMissing(String pattern, String compilePattern, - boolean namedOnly) { + private void ensureAbortsWithDefinitionMissing( + String pattern, String compilePattern, boolean namedOnly) { try { compiler.compile(pattern); compiler.compile(compilePattern, namedOnly); @@ -643,10 +655,11 @@ private void ensureAbortsWithDefinitionMissing(String pattern, String compilePat @Test public void testGroupTypes() { - Grok grok = compiler.compile( - "%{HTTPDATE:timestamp;date;dd/MMM/yyyy:HH:mm:ss Z} %{USERNAME:username:text} " - + "%{IPORHOST:host}:%{POSINT:port:integer}", - true); + Grok grok = + compiler.compile( + "%{HTTPDATE:timestamp;date;dd/MMM/yyyy:HH:mm:ss Z} %{USERNAME:username:text} " + + "%{IPORHOST:host}:%{POSINT:port:integer}", + true); assertEquals(Converter.Type.DATETIME, grok.groupTypes.get("timestamp")); assertEquals(Converter.Type.STRING, grok.groupTypes.get("username")); assertEquals(Converter.Type.INT, grok.groupTypes.get("port")); @@ -667,8 +680,8 @@ public void testTimeZone() { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); Grok grok = compiler.compile("%{DATESTAMP:timestamp;date;MM/dd/yyyy HH:mm:ss}", true); Instant instant = (Instant) grok.match(date).capture().get("timestamp"); - assertEquals(ZonedDateTime.parse(date, dtf.withZone(ZoneOffset.systemDefault())).toInstant(), - instant); + assertEquals( + ZonedDateTime.parse(date, dtf.withZone(ZoneOffset.systemDefault())).toInstant(), instant); // set default timezone to PST ZoneId pst = ZoneId.of("PST", ZoneId.SHORT_IDS); diff --git a/common/src/test/java/org/opensearch/sql/common/grok/MessagesTest.java b/common/src/test/java/org/opensearch/sql/common/grok/MessagesTest.java index 98cbb3aaebf..930da8caa8b 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/MessagesTest.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/MessagesTest.java @@ -16,7 +16,6 @@ import org.junit.Test; import org.opensearch.sql.common.grok.exception.GrokException; - public class MessagesTest { @Test @@ -26,8 +25,9 @@ public void test001_linux_messages() throws GrokException, IOException { Grok grok = compiler.compile("%{MESSAGESLOG}"); - BufferedReader br = new BufferedReader( - new FileReader(Resources.getResource(ResourceManager.MESSAGES).getFile())); + BufferedReader br = + new BufferedReader( + new FileReader(Resources.getResource(ResourceManager.MESSAGES).getFile())); String line; System.out.println("Starting test with linux messages log -- may take a while"); while ((line = br.readLine()) != null) { @@ -38,5 +38,4 @@ public void test001_linux_messages() throws GrokException, IOException { } br.close(); } - } diff --git a/common/src/test/java/org/opensearch/sql/common/grok/ResourceManager.java b/common/src/test/java/org/opensearch/sql/common/grok/ResourceManager.java index a13a72cd00a..fba64b59d33 100644 --- a/common/src/test/java/org/opensearch/sql/common/grok/ResourceManager.java +++ b/common/src/test/java/org/opensearch/sql/common/grok/ResourceManager.java @@ -5,9 +5,7 @@ package org.opensearch.sql.common.grok; -/** - * {@code ResourceManager} . - */ +/** {@code ResourceManager} . */ public final class ResourceManager { public static final String PATTERNS = "patterns/patterns"; From b18eb640fa51ae12b6fe75a84815e2aa7e7b61df Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Tue, 22 Aug 2023 09:35:20 -0700 Subject: [PATCH 3/6] Spotless apply Signed-off-by: Mitchell Gale --- .../sql/common/utils/QueryContext.java | 2 - .../sql/analysis/AnalysisContext.java | 21 +- .../org/opensearch/sql/analysis/Analyzer.java | 221 ++- ...ataSourceSchemaIdentifierNameResolver.java | 25 +- .../sql/analysis/ExpressionAnalyzer.java | 135 +- .../ExpressionReferenceOptimizer.java | 78 +- .../sql/analysis/HighlightAnalyzer.java | 5 +- .../sql/analysis/NamedExpressionAnalyzer.java | 17 +- .../sql/analysis/NestedAnalyzer.java | 68 +- .../sql/analysis/QualifierAnalyzer.java | 22 +- .../analysis/SelectExpressionAnalyzer.java | 94 +- .../sql/analysis/TypeEnvironment.java | 33 +- .../analysis/WindowExpressionAnalyzer.java | 57 +- .../sql/analysis/symbol/Namespace.java | 7 +- .../sql/analysis/symbol/Symbol.java | 5 +- .../sql/analysis/symbol/SymbolTable.java | 78 +- .../sql/ast/AbstractNodeVisitor.java | 6 +- .../java/org/opensearch/sql/ast/Node.java | 5 +- .../org/opensearch/sql/ast/dsl/AstDSL.java | 82 +- .../sql/ast/expression/AggregateFunction.java | 9 +- .../opensearch/sql/ast/expression/Alias.java | 21 +- .../sql/ast/expression/AllFields.java | 8 +- .../opensearch/sql/ast/expression/And.java | 5 +- .../sql/ast/expression/Argument.java | 5 +- .../sql/ast/expression/AttributeList.java | 8 +- .../sql/ast/expression/Between.java | 4 +- .../opensearch/sql/ast/expression/Case.java | 18 +- .../opensearch/sql/ast/expression/Cast.java | 19 +- .../sql/ast/expression/Compare.java | 1 - .../sql/ast/expression/DataType.java | 9 +- .../sql/ast/expression/EqualTo.java | 11 +- .../opensearch/sql/ast/expression/Field.java | 9 +- .../sql/ast/expression/Function.java | 13 +- .../sql/ast/expression/HighlightFunction.java | 4 +- .../org/opensearch/sql/ast/expression/In.java | 8 +- .../sql/ast/expression/Interval.java | 1 - .../sql/ast/expression/IntervalUnit.java | 7 +- .../opensearch/sql/ast/expression/Let.java | 5 +- .../sql/ast/expression/Literal.java | 6 +- .../opensearch/sql/ast/expression/Map.java | 5 +- .../ast/expression/NestedAllTupleFields.java | 9 +- .../opensearch/sql/ast/expression/Not.java | 5 +- .../org/opensearch/sql/ast/expression/Or.java | 5 +- .../sql/ast/expression/ParseMethod.java | 4 +- .../sql/ast/expression/QualifiedName.java | 33 +- .../ast/expression/RelevanceFieldList.java | 12 +- .../sql/ast/expression/ScoreFunction.java | 4 +- .../opensearch/sql/ast/expression/Span.java | 6 +- .../sql/ast/expression/SpanUnit.java | 5 +- .../ast/expression/UnresolvedArgument.java | 5 +- .../ast/expression/UnresolvedAttribute.java | 6 +- .../ast/expression/UnresolvedExpression.java | 1 - .../opensearch/sql/ast/expression/When.java | 14 +- .../sql/ast/expression/WindowFunction.java | 2 - .../opensearch/sql/ast/expression/Xor.java | 5 +- .../opensearch/sql/ast/statement/Explain.java | 4 +- .../opensearch/sql/ast/statement/Query.java | 4 +- .../sql/ast/statement/Statement.java | 4 +- .../java/org/opensearch/sql/ast/tree/AD.java | 1 - .../opensearch/sql/ast/tree/Aggregation.java | 31 +- .../opensearch/sql/ast/tree/CloseCursor.java | 9 +- .../org/opensearch/sql/ast/tree/Dedupe.java | 5 +- .../org/opensearch/sql/ast/tree/Eval.java | 5 +- .../opensearch/sql/ast/tree/FetchCursor.java | 8 +- .../org/opensearch/sql/ast/tree/Filter.java | 5 +- .../org/opensearch/sql/ast/tree/Head.java | 5 +- .../org/opensearch/sql/ast/tree/Kmeans.java | 1 - .../org/opensearch/sql/ast/tree/Limit.java | 2 - .../java/org/opensearch/sql/ast/tree/ML.java | 9 +- .../org/opensearch/sql/ast/tree/Paginate.java | 8 +- .../org/opensearch/sql/ast/tree/Parse.java | 25 +- .../org/opensearch/sql/ast/tree/Project.java | 12 +- .../org/opensearch/sql/ast/tree/RareTopN.java | 5 +- .../org/opensearch/sql/ast/tree/Relation.java | 22 +- .../sql/ast/tree/RelationSubquery.java | 9 +- .../org/opensearch/sql/ast/tree/Rename.java | 1 - .../org/opensearch/sql/ast/tree/Sort.java | 18 +- .../sql/ast/tree/TableFunction.java | 10 +- .../sql/ast/tree/UnresolvedPlan.java | 5 +- .../org/opensearch/sql/ast/tree/Values.java | 6 +- .../data/model/AbstractExprNumberValue.java | 5 +- .../sql/data/model/AbstractExprValue.java | 28 +- .../sql/data/model/ExprBooleanValue.java | 5 +- .../sql/data/model/ExprByteValue.java | 5 +- .../sql/data/model/ExprCollectionValue.java | 13 +- .../sql/data/model/ExprDateValue.java | 13 +- .../sql/data/model/ExprDatetimeValue.java | 20 +- .../sql/data/model/ExprDoubleValue.java | 5 +- .../sql/data/model/ExprFloatValue.java | 5 +- .../sql/data/model/ExprIntegerValue.java | 5 +- .../sql/data/model/ExprIntervalValue.java | 11 +- .../sql/data/model/ExprLongValue.java | 5 +- .../sql/data/model/ExprMissingValue.java | 16 +- .../sql/data/model/ExprNullValue.java | 12 +- .../sql/data/model/ExprShortValue.java | 5 +- .../sql/data/model/ExprStringValue.java | 14 +- .../sql/data/model/ExprTimeValue.java | 17 +- .../sql/data/model/ExprTimestampValue.java | 29 +- .../sql/data/model/ExprTupleValue.java | 13 +- .../opensearch/sql/data/model/ExprValue.java | 81 +- .../sql/data/model/ExprValueUtils.java | 32 +- .../sql/data/type/ExprCoreType.java | 56 +- .../opensearch/sql/data/type/ExprType.java | 28 +- .../sql/data/type/WideningTypeRule.java | 28 +- .../sql/data/utils/ExprValueOrdering.java | 1 - .../data/utils/NaturalExprValueOrdering.java | 1 - .../utils/NullsFirstExprValueOrdering.java | 1 - .../utils/NullsLastExprValueOrdering.java | 1 - .../data/utils/ReverseExprValueOrdering.java | 1 - .../sql/datasource/DataSourceService.java | 23 +- .../sql/datasource/model/DataSource.java | 8 +- .../datasource/model/DataSourceMetadata.java | 17 +- .../ExpressionEvaluationException.java | 5 +- .../sql/exception/NoCursorException.java | 7 +- .../sql/exception/QueryEngineException.java | 5 +- .../sql/exception/SemanticCheckException.java | 5 +- .../UnsupportedCursorRequestException.java | 7 +- .../sql/executor/ExecutionContext.java | 7 +- .../sql/executor/ExecutionEngine.java | 36 +- .../org/opensearch/sql/executor/Explain.java | 169 +- .../org/opensearch/sql/executor/QueryId.java | 12 +- .../opensearch/sql/executor/QueryManager.java | 5 +- .../opensearch/sql/executor/QueryService.java | 43 +- .../sql/executor/execution/AbstractPlan.java | 16 +- .../sql/executor/execution/CommandPlan.java | 19 +- .../sql/executor/execution/ExplainPlan.java | 15 +- .../sql/executor/execution/QueryPlan.java | 17 +- .../executor/execution/QueryPlanFactory.java | 65 +- .../execution/StreamingQueryPlan.java | 28 +- .../pagination/CanPaginateVisitor.java | 35 +- .../sql/executor/pagination/Cursor.java | 3 +- .../executor/pagination/PlanSerializer.java | 36 +- .../sql/executor/streaming/Batch.java | 4 +- .../MicroBatchStreamingExecution.java | 12 +- .../sql/executor/streaming/Offset.java | 4 +- .../executor/streaming/StreamingSource.java | 4 +- .../org/opensearch/sql/expression/DSL.java | 148 +- .../opensearch/sql/expression/Expression.java | 29 +- .../sql/expression/ExpressionNodeVisitor.java | 20 +- .../sql/expression/FunctionExpression.java | 12 +- .../sql/expression/HighlightExpression.java | 30 +- .../sql/expression/LiteralExpression.java | 5 +- .../expression/NamedArgumentExpression.java | 4 +- .../sql/expression/NamedExpression.java | 22 +- .../sql/expression/ReferenceExpression.java | 45 +- .../aggregation/AggregationState.java | 9 +- .../expression/aggregation/Aggregator.java | 32 +- .../aggregation/AggregatorFunction.java | 153 +- .../expression/aggregation/AvgAggregator.java | 56 +- .../aggregation/CountAggregator.java | 5 +- .../expression/aggregation/MaxAggregator.java | 5 +- .../expression/aggregation/MinAggregator.java | 10 +- .../aggregation/NamedAggregator.java | 30 +- .../aggregation/StdDevAggregator.java | 24 +- .../expression/aggregation/SumAggregator.java | 13 +- .../aggregation/TakeAggregator.java | 9 +- .../aggregation/VarianceAggregator.java | 18 +- .../conditional/cases/CaseClause.java | 28 +- .../conditional/cases/WhenClause.java | 23 +- .../expression/datetime/CalendarLookup.java | 5 +- .../datetime/DateTimeFormatterUtil.java | 246 +-- .../expression/datetime/DateTimeFunction.java | 1438 +++++++++-------- .../expression/datetime/IntervalClause.java | 4 +- .../sql/expression/env/Environment.java | 19 +- .../function/BuiltinFunctionName.java | 48 +- .../function/BuiltinFunctionRepository.java | 57 +- .../function/DefaultFunctionResolver.java | 46 +- .../expression/function/FunctionBuilder.java | 7 +- .../sql/expression/function/FunctionDSL.java | 381 +++-- .../function/FunctionImplementation.java | 13 +- .../sql/expression/function/FunctionName.java | 8 +- .../function/FunctionProperties.java | 36 +- .../expression/function/FunctionResolver.java | 4 +- .../function/FunctionSignature.java | 17 +- .../function/OpenSearchFunctions.java | 57 +- .../function/RelevanceFunctionResolver.java | 26 +- .../function/SerializableBiFunction.java | 8 +- .../function/SerializableFunction.java | 4 +- .../function/SerializableNoArgFunction.java | 8 +- .../function/SerializableQuadFunction.java | 1 - .../function/SerializableTriFunction.java | 1 - .../function/TableFunctionImplementation.java | 5 +- .../arthmetic/ArithmeticFunction.java | 377 +++-- .../arthmetic/MathematicalFunction.java | 906 ++++++----- .../operator/convert/TypeCastOperator.java | 246 +-- .../predicate/BinaryPredicateOperator.java | 178 +- .../predicate/UnaryPredicateOperator.java | 84 +- .../sql/expression/parse/GrokExpression.java | 17 +- .../sql/expression/parse/ParseExpression.java | 20 +- .../expression/parse/PatternsExpression.java | 24 +- .../sql/expression/parse/RegexExpression.java | 12 +- .../sql/expression/span/SpanExpression.java | 17 +- .../expression/system/SystemFunctions.java | 25 +- .../sql/expression/text/TextFunction.java | 303 ++-- .../expression/window/WindowDefinition.java | 9 +- .../window/WindowFunctionExpression.java | 17 +- .../expression/window/WindowFunctions.java | 15 +- .../aggregation/AggregateWindowFunction.java | 6 +- .../window/frame/CurrentRowWindowFrame.java | 21 +- .../window/frame/PeerRowsWindowFrame.java | 52 +- .../expression/window/frame/WindowFrame.java | 22 +- .../window/ranking/DenseRankFunction.java | 6 +- .../window/ranking/RankFunction.java | 11 +- .../window/ranking/RankingWindowFunction.java | 30 +- .../window/ranking/RowNumberFunction.java | 2 - .../sql/ast/expression/CastTest.java | 2 - .../sql/ast/expression/QualifiedNameTest.java | 2 - .../sql/common/utils/StringUtilsTest.java | 2 - .../opensearch/sql/executor/ExplainTest.java | 155 +- .../sql/executor/QueryServiceTest.java | 134 +- .../executor/execution/CommandPlanTest.java | 12 +- .../executor/execution/ExplainPlanTest.java | 9 +- .../IntervalTriggerExecutionTest.java | 8 +- .../execution/QueryPlanFactoryTest.java | 40 +- .../sql/executor/execution/QueryPlanTest.java | 98 +- .../execution/StreamingQueryPlanTest.java | 8 +- .../pagination/CanPaginateVisitorTest.java | 299 ++-- .../sql/executor/pagination/CursorTest.java | 1 - .../pagination/PlanSerializerTest.java | 33 +- .../streaming/DefaultMetadataLogTest.java | 1 - .../MicroBatchStreamingExecutionTest.java | 20 +- .../expression/ExpressionNodeVisitorTest.java | 78 +- .../sql/expression/ExpressionTestBase.java | 1 - .../expression/HighlightExpressionTest.java | 46 +- .../sql/expression/NamedExpressionTest.java | 6 +- .../expression/ReferenceExpressionTest.java | 76 +- .../aggregation/AggregationTest.java | 61 +- .../aggregation/AvgAggregatorTest.java | 36 +- .../aggregation/CountAggregatorTest.java | 55 +- .../aggregation/MaxAggregatorTest.java | 33 +- .../aggregation/MinAggregatorTest.java | 33 +- .../aggregation/StdDevAggregatorTest.java | 9 +- .../aggregation/SumAggregatorTest.java | 46 +- .../aggregation/TakeAggregatorTest.java | 28 +- .../conditional/ConditionalFunctionTest.java | 47 +- .../conditional/cases/CaseClauseTest.java | 12 +- .../conditional/cases/WhenClauseTest.java | 2 - .../datetime/AddTimeAndSubTimeTest.java | 93 +- .../expression/datetime/ConvertTZTest.java | 133 +- .../datetime/DateAddAndAddDateTest.java | 24 +- .../sql/expression/datetime/DateDiffTest.java | 47 +- .../datetime/DateSubAndSubDateTest.java | 24 +- .../datetime/DateTimeFunctionTest.java | 1112 +++++-------- .../sql/expression/datetime/DateTimeTest.java | 30 +- .../expression/datetime/DateTimeTestBase.java | 119 +- .../sql/expression/datetime/ExtractTest.java | 62 +- .../expression/datetime/FromUnixTimeTest.java | 80 +- .../datetime/IntervalClauseTest.java | 14 +- .../sql/expression/datetime/MakeDateTest.java | 44 +- .../sql/expression/datetime/MakeTimeTest.java | 40 +- .../datetime/NowLikeFunctionTest.java | 114 +- .../datetime/PeriodFunctionsTest.java | 38 +- .../expression/datetime/StrToDateTest.java | 143 +- .../sql/expression/datetime/TimeDiffTest.java | 10 +- .../expression/datetime/TimeStampAddTest.java | 241 ++- .../datetime/TimeStampDiffTest.java | 242 ++- .../expression/datetime/TimestampTest.java | 139 +- .../expression/datetime/ToSecondsTest.java | 31 +- .../datetime/UnixTimeStampTest.java | 57 +- .../datetime/UnixTwoWayConversionTest.java | 21 +- .../sql/expression/datetime/WeekdayTest.java | 111 +- .../sql/expression/datetime/YearweekTest.java | 95 +- .../function/BuiltinFunctionNameTest.java | 4 +- .../BuiltinFunctionRepositoryTest.java | 108 +- .../function/DefaultFunctionResolverTest.java | 92 +- .../function/FunctionDSLDefineTest.java | 7 +- .../function/FunctionDSLTestBase.java | 55 +- .../function/FunctionDSLimplNoArgTest.java | 1 - .../function/FunctionDSLimplTestBase.java | 21 +- .../function/FunctionDSLimplTwoArgTest.java | 1 - ...nctionDSLimplWithPropertiesNoArgsTest.java | 2 +- ...nctionDSLimplWithPropertiesOneArgTest.java | 5 +- ...nctionDSLimplWithPropertiesTwoArgTest.java | 4 +- ...ctionDSLimplWithPropertiesTwoArgsTest.java | 5 +- .../FunctionDSLnullMissingHandlingTest.java | 50 +- .../function/FunctionPropertiesTest.java | 23 +- .../function/FunctionSignatureTest.java | 7 +- .../function/OpenSearchFunctionsTest.java | 266 ++- .../RelevanceFunctionResolverTest.java | 15 +- .../function/WideningTypeRuleTest.java | 67 +- .../arthmetic/ArithmeticFunctionTest.java | 157 +- .../arthmetic/MathematicalFunctionTest.java | 1060 ++++-------- .../convert/TypeCastOperatorTest.java | 10 +- .../BinaryPredicateOperatorTest.java | 251 +-- .../predicate/UnaryPredicateOperatorTest.java | 113 +- .../expression/parse/GrokExpressionTest.java | 119 +- .../parse/PatternsExpressionTest.java | 61 +- .../expression/parse/RegexExpressionTest.java | 90 +- .../system/SystemFunctionsTest.java | 39 +- .../sql/expression/text/TextFunctionTest.java | 184 +-- .../window/CurrentRowWindowFrameTest.java | 69 +- .../AggregateWindowFunctionTest.java | 19 +- .../window/frame/PeerRowsWindowFrameTest.java | 178 +- .../ranking/RankingWindowFunctionTest.java | 119 +- .../sql/monitor/AlwaysHealthyMonitorTest.java | 1 - .../sql/planner/DefaultImplementorTest.java | 194 ++- .../sql/planner/PlanContextTest.java | 3 +- .../opensearch/sql/planner/PlannerTest.java | 52 +- .../sql/planner/SerializablePlanTest.java | 1 - .../planner/logical/LogicalDedupeTest.java | 17 +- .../sql/planner/logical/LogicalEvalTest.java | 9 +- .../logical/LogicalPlanNodeVisitorTest.java | 119 +- .../planner/logical/LogicalRelationTest.java | 6 +- .../sql/planner/logical/LogicalSortTest.java | 1 - .../optimizer/LogicalPlanOptimizerTest.java | 251 ++- .../optimizer/pattern/PatternsTest.java | 10 +- .../physical/AggregationOperatorTest.java | 1001 +++++++----- .../planner/physical/DedupeOperatorTest.java | 13 +- .../planner/physical/EvalOperatorTest.java | 16 +- .../planner/physical/FilterOperatorTest.java | 41 +- .../planner/physical/LimitOperatorTest.java | 37 +- .../planner/physical/NestedOperatorTest.java | 411 +++-- .../physical/PhysicalPlanNodeVisitorTest.java | 64 +- .../planner/physical/PhysicalPlanTest.java | 61 +- .../physical/PhysicalPlanTestBase.java | 375 +++-- .../planner/physical/ProjectOperatorTest.java | 133 +- .../physical/RareTopNOperatorTest.java | 100 +- .../planner/physical/RemoveOperatorTest.java | 4 +- .../planner/physical/RenameOperatorTest.java | 35 +- .../planner/physical/SortOperatorTest.java | 7 +- .../planner/physical/ValuesOperatorTest.java | 12 +- .../planner/physical/WindowOperatorTest.java | 209 ++- .../physical/collector/RoundingTest.java | 7 +- .../datasource/DataSourceTableScanTest.java | 31 +- .../datasource/DataSourceTableTest.java | 16 +- .../assigner/SlidingWindowAssignerTest.java | 30 +- .../assigner/TumblingWindowAssignerTest.java | 16 +- .../sql/storage/TableScanOperatorTest.java | 56 +- .../bindingtuple/BindingTupleTest.java | 18 +- .../storage/write/TableWriteOperatorTest.java | 55 +- .../opensearch/sql/utils/ComparisonUtil.java | 52 +- .../sql/utils/DateTimeUtilsTest.java | 11 +- .../opensearch/sql/utils/MatcherUtils.java | 13 +- .../sql/utils/SystemIndexUtilsTest.java | 5 +- .../opensearch/sql/utils/TestOperator.java | 9 +- .../sql/executor/DefaultExecutionEngine.java | 8 +- .../sql/executor/DefaultQueryManager.java | 4 +- 337 files changed, 9447 insertions(+), 9825 deletions(-) diff --git a/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java b/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java index 04b97332adc..686263238aa 100644 --- a/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java +++ b/common/src/main/java/org/opensearch/sql/common/utils/QueryContext.java @@ -5,9 +5,7 @@ package org.opensearch.sql.common.utils; -import java.time.LocalDateTime; import java.util.Map; -import java.util.Optional; import java.util.UUID; import org.apache.logging.log4j.ThreadContext; diff --git a/core/src/main/java/org/opensearch/sql/analysis/AnalysisContext.java b/core/src/main/java/org/opensearch/sql/analysis/AnalysisContext.java index 4704d0566bb..f1f29e9b385 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/AnalysisContext.java +++ b/core/src/main/java/org/opensearch/sql/analysis/AnalysisContext.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import java.util.ArrayList; @@ -13,19 +12,14 @@ import org.opensearch.sql.expression.NamedExpression; import org.opensearch.sql.expression.function.FunctionProperties; -/** - * The context used for Analyzer. - */ +/** The context used for Analyzer. */ public class AnalysisContext { - /** - * Environment stack for symbol scope management. - */ + /** Environment stack for symbol scope management. */ private TypeEnvironment environment; - @Getter - private final List namedParseExpressions; - @Getter - private final FunctionProperties functionProperties; + @Getter private final List namedParseExpressions; + + @Getter private final FunctionProperties functionProperties; public AnalysisContext() { this(new TypeEnvironment(null)); @@ -33,6 +27,7 @@ public AnalysisContext() { /** * Class CTOR. + * * @param environment Env to set to a new instance. */ public AnalysisContext(TypeEnvironment environment) { @@ -41,9 +36,7 @@ public AnalysisContext(TypeEnvironment environment) { this.functionProperties = new FunctionProperties(); } - /** - * Push a new environment. - */ + /** Push a new environment. */ public void push() { environment = new TypeEnvironment(environment); } diff --git a/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java b/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java index 2776ca3b6a2..d5e8b93b13d 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; @@ -29,7 +28,6 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; -import org.apache.commons.math3.analysis.function.Exp; import org.opensearch.sql.DataSourceSchemaName; import org.opensearch.sql.analysis.symbol.Namespace; import org.opensearch.sql.analysis.symbol.Symbol; @@ -122,9 +120,7 @@ public class Analyzer extends AbstractNodeVisitor private final BuiltinFunctionRepository repository; - /** - * Constructor. - */ + /** Constructor. */ public Analyzer( ExpressionAnalyzer expressionAnalyzer, DataSourceService dataSourceService, @@ -143,8 +139,8 @@ public LogicalPlan analyze(UnresolvedPlan unresolved, AnalysisContext context) { @Override public LogicalPlan visitRelation(Relation node, AnalysisContext context) { QualifiedName qualifiedName = node.getTableQualifiedName(); - DataSourceSchemaIdentifierNameResolver dataSourceSchemaIdentifierNameResolver - = new DataSourceSchemaIdentifierNameResolver(dataSourceService, qualifiedName.getParts()); + DataSourceSchemaIdentifierNameResolver dataSourceSchemaIdentifierNameResolver = + new DataSourceSchemaIdentifierNameResolver(dataSourceService, qualifiedName.getParts()); String tableName = dataSourceSchemaIdentifierNameResolver.getIdentifierName(); context.push(); TypeEnvironment curEnv = context.peek(); @@ -152,13 +148,15 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) { if (DATASOURCES_TABLE_NAME.equals(tableName)) { table = new DataSourceTable(dataSourceService); } else { - table = dataSourceService - .getDataSource(dataSourceSchemaIdentifierNameResolver.getDataSourceName()) - .getStorageEngine() - .getTable(new DataSourceSchemaName( - dataSourceSchemaIdentifierNameResolver.getDataSourceName(), - dataSourceSchemaIdentifierNameResolver.getSchemaName()), - dataSourceSchemaIdentifierNameResolver.getIdentifierName()); + table = + dataSourceService + .getDataSource(dataSourceSchemaIdentifierNameResolver.getDataSourceName()) + .getStorageEngine() + .getTable( + new DataSourceSchemaName( + dataSourceSchemaIdentifierNameResolver.getDataSourceName(), + dataSourceSchemaIdentifierNameResolver.getSchemaName()), + dataSourceSchemaIdentifierNameResolver.getIdentifierName()); } table.getFieldTypes().forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v)); table @@ -167,13 +165,13 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) { // Put index name or its alias in index namespace on type environment so qualifier // can be removed when analyzing qualified name. The value (expr type) here doesn't matter. - curEnv.define(new Symbol(Namespace.INDEX_NAME, - (node.getAlias() == null) ? tableName : node.getAlias()), STRUCT); + curEnv.define( + new Symbol(Namespace.INDEX_NAME, (node.getAlias() == null) ? tableName : node.getAlias()), + STRUCT); return new LogicalRelation(tableName, table); } - @Override public LogicalPlan visitRelationSubquery(RelationSubquery node, AnalysisContext context) { LogicalPlan subquery = analyze(node.getChild().get(0), context); @@ -189,20 +187,28 @@ public LogicalPlan visitRelationSubquery(RelationSubquery node, AnalysisContext @Override public LogicalPlan visitTableFunction(TableFunction node, AnalysisContext context) { QualifiedName qualifiedName = node.getFunctionName(); - DataSourceSchemaIdentifierNameResolver dataSourceSchemaIdentifierNameResolver - = new DataSourceSchemaIdentifierNameResolver(this.dataSourceService, - qualifiedName.getParts()); - - FunctionName functionName - = FunctionName.of(dataSourceSchemaIdentifierNameResolver.getIdentifierName()); - List arguments = node.getArguments().stream() - .map(unresolvedExpression -> this.expressionAnalyzer.analyze(unresolvedExpression, context)) - .collect(Collectors.toList()); - TableFunctionImplementation tableFunctionImplementation - = (TableFunctionImplementation) repository.compile(context.getFunctionProperties(), - dataSourceService - .getDataSource(dataSourceSchemaIdentifierNameResolver.getDataSourceName()) - .getStorageEngine().getFunctions(), functionName, arguments); + DataSourceSchemaIdentifierNameResolver dataSourceSchemaIdentifierNameResolver = + new DataSourceSchemaIdentifierNameResolver( + this.dataSourceService, qualifiedName.getParts()); + + FunctionName functionName = + FunctionName.of(dataSourceSchemaIdentifierNameResolver.getIdentifierName()); + List arguments = + node.getArguments().stream() + .map( + unresolvedExpression -> + this.expressionAnalyzer.analyze(unresolvedExpression, context)) + .collect(Collectors.toList()); + TableFunctionImplementation tableFunctionImplementation = + (TableFunctionImplementation) + repository.compile( + context.getFunctionProperties(), + dataSourceService + .getDataSource(dataSourceSchemaIdentifierNameResolver.getDataSourceName()) + .getStorageEngine() + .getFunctions(), + functionName, + arguments); context.push(); TypeEnvironment curEnv = context.peek(); Table table = tableFunctionImplementation.applyArguments(); @@ -237,30 +243,28 @@ public LogicalPlan visitFilter(Filter node, AnalysisContext context) { } /** - * Ensure NESTED function is not used in GROUP BY, and HAVING clauses. - * Fallback to legacy engine. Can remove when support is added for NESTED function in WHERE, - * GROUP BY, ORDER BY, and HAVING clauses. + * Ensure NESTED function is not used in GROUP BY, and HAVING clauses. Fallback to legacy engine. + * Can remove when support is added for NESTED function in WHERE, GROUP BY, ORDER BY, and HAVING + * clauses. + * * @param condition : Filter condition */ private void verifySupportsCondition(Expression condition) { if (condition instanceof FunctionExpression) { - if (((FunctionExpression) condition).getFunctionName().getFunctionName().equalsIgnoreCase( - BuiltinFunctionName.NESTED.name() - )) { + if (((FunctionExpression) condition) + .getFunctionName() + .getFunctionName() + .equalsIgnoreCase(BuiltinFunctionName.NESTED.name())) { throw new SyntaxCheckException( "Falling back to legacy engine. Nested function is not supported in WHERE," - + " GROUP BY, and HAVING clauses." - ); + + " GROUP BY, and HAVING clauses."); } - ((FunctionExpression)condition).getArguments().stream() - .forEach(e -> verifySupportsCondition(e) - ); + ((FunctionExpression) condition) + .getArguments().stream().forEach(e -> verifySupportsCondition(e)); } } - /** - * Build {@link LogicalRename}. - */ + /** Build {@link LogicalRename}. */ @Override public LogicalPlan visitRename(Rename node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -271,8 +275,8 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) { // We should define the new target field in the context instead of analyze it. if (renameMap.getTarget() instanceof Field) { ReferenceExpression target = - new ReferenceExpression(((Field) renameMap.getTarget()).getField().toString(), - origin.type()); + new ReferenceExpression( + ((Field) renameMap.getTarget()).getField().toString(), origin.type()); ReferenceExpression originExpr = DSL.ref(origin.toString(), origin.type()); TypeEnvironment curEnv = context.peek(); curEnv.remove(originExpr); @@ -287,17 +291,15 @@ public LogicalPlan visitRename(Rename node, AnalysisContext context) { return new LogicalRename(child, renameMapBuilder.build()); } - /** - * Build {@link LogicalAggregation}. - */ + /** Build {@link LogicalAggregation}. */ @Override public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) { final LogicalPlan child = node.getChild().get(0).accept(this, context); ImmutableList.Builder aggregatorBuilder = new ImmutableList.Builder<>(); for (UnresolvedExpression expr : node.getAggExprList()) { NamedExpression aggExpr = namedExpressionAnalyzer.analyze(expr, context); - aggregatorBuilder - .add(new NamedAggregator(aggExpr.getNameOrAlias(), (Aggregator) aggExpr.getDelegated())); + aggregatorBuilder.add( + new NamedAggregator(aggExpr.getNameOrAlias(), (Aggregator) aggExpr.getDelegated())); } ImmutableList.Builder groupbyBuilder = new ImmutableList.Builder<>(); @@ -317,16 +319,17 @@ public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) { // new context context.push(); TypeEnvironment newEnv = context.peek(); - aggregators.forEach(aggregator -> newEnv.define(new Symbol(Namespace.FIELD_NAME, - aggregator.getName()), aggregator.type())); - groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME, - group.getNameOrAlias()), group.type())); + aggregators.forEach( + aggregator -> + newEnv.define( + new Symbol(Namespace.FIELD_NAME, aggregator.getName()), aggregator.type())); + groupBys.forEach( + group -> + newEnv.define(new Symbol(Namespace.FIELD_NAME, group.getNameOrAlias()), group.type())); return new LogicalAggregation(child, aggregators, groupBys); } - /** - * Build {@link LogicalRareTopN}. - */ + /** Build {@link LogicalRareTopN}. */ @Override public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) { final LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -346,10 +349,10 @@ public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) { // new context context.push(); TypeEnvironment newEnv = context.peek(); - groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME, - group.toString()), group.type())); - fields.forEach(field -> newEnv.define(new Symbol(Namespace.FIELD_NAME, - field.toString()), field.type())); + groupBys.forEach( + group -> newEnv.define(new Symbol(Namespace.FIELD_NAME, group.toString()), group.type())); + fields.forEach( + field -> newEnv.define(new Symbol(Namespace.FIELD_NAME, field.toString()), field.type())); List options = node.getNoOfResults(); Integer noOfResults = (Integer) options.get(0).getValue().getValue(); @@ -400,28 +403,28 @@ public LogicalPlan visitProject(Project node, AnalysisContext context) { } List namedExpressions = - selectExpressionAnalyzer.analyze(node.getProjectList(), context, + selectExpressionAnalyzer.analyze( + node.getProjectList(), + context, new ExpressionReferenceOptimizer(expressionAnalyzer.getRepository(), child)); for (UnresolvedExpression expr : node.getProjectList()) { - NestedAnalyzer nestedAnalyzer = new NestedAnalyzer( - namedExpressions, expressionAnalyzer, child - ); + NestedAnalyzer nestedAnalyzer = + new NestedAnalyzer(namedExpressions, expressionAnalyzer, child); child = nestedAnalyzer.analyze(expr, context); } // new context context.push(); TypeEnvironment newEnv = context.peek(); - namedExpressions.forEach(expr -> newEnv.define(new Symbol(Namespace.FIELD_NAME, - expr.getNameOrAlias()), expr.type())); + namedExpressions.forEach( + expr -> + newEnv.define(new Symbol(Namespace.FIELD_NAME, expr.getNameOrAlias()), expr.type())); List namedParseExpressions = context.getNamedParseExpressions(); return new LogicalProject(child, namedExpressions, namedParseExpressions); } - /** - * Build {@link LogicalEval}. - */ + /** Build {@link LogicalEval}. */ @Override public LogicalPlan visitEval(Eval node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -438,9 +441,7 @@ public LogicalPlan visitEval(Eval node, AnalysisContext context) { return new LogicalEval(child, expressionsBuilder.build()); } - /** - * Build {@link ParseExpression} to context and skip to child nodes. - */ + /** Build {@link ParseExpression} to context and skip to child nodes. */ @Override public LogicalPlan visitParse(Parse node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -451,18 +452,19 @@ public LogicalPlan visitParse(Parse node, AnalysisContext context) { Expression patternExpression = DSL.literal(pattern); TypeEnvironment curEnv = context.peek(); - ParseUtils.getNamedGroupCandidates(parseMethod, pattern, arguments).forEach(group -> { - ParseExpression expr = ParseUtils.createParseExpression(parseMethod, sourceField, - patternExpression, DSL.literal(group)); - curEnv.define(new Symbol(Namespace.FIELD_NAME, group), expr.type()); - context.getNamedParseExpressions().add(new NamedExpression(group, expr)); - }); + ParseUtils.getNamedGroupCandidates(parseMethod, pattern, arguments) + .forEach( + group -> { + ParseExpression expr = + ParseUtils.createParseExpression( + parseMethod, sourceField, patternExpression, DSL.literal(group)); + curEnv.define(new Symbol(Namespace.FIELD_NAME, group), expr.type()); + context.getNamedParseExpressions().add(new NamedExpression(group, expr)); + }); return child; } - /** - * Build {@link LogicalSort}. - */ + /** Build {@link LogicalSort}. */ @Override public LogicalPlan visitSort(Sort node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -476,8 +478,7 @@ public LogicalPlan visitSort(Sort node, AnalysisContext context) { var analyzed = expressionAnalyzer.analyze(sortField.getField(), context); if (analyzed == null) { throw new UnsupportedOperationException( - String.format("Invalid use of expression %s", sortField.getField()) - ); + String.format("Invalid use of expression %s", sortField.getField())); } Expression expression = optimizer.optimize(analyzed, context); return ImmutablePair.of(analyzeSortOption(sortField.getFieldArgs()), expression); @@ -486,9 +487,7 @@ public LogicalPlan visitSort(Sort node, AnalysisContext context) { return new LogicalSort(child, sortList); } - /** - * Build {@link LogicalDedupe}. - */ + /** Build {@link LogicalDedupe}. */ @Override public LogicalPlan visitDedupe(Dedupe node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -508,9 +507,7 @@ public LogicalPlan visitDedupe(Dedupe node, AnalysisContext context) { consecutive); } - /** - * Logical head is identical to {@link LogicalLimit}. - */ + /** Logical head is identical to {@link LogicalLimit}. */ public LogicalPlan visitHead(Head node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); return new LogicalLimit(child, node.getSize(), node.getFrom()); @@ -521,16 +518,15 @@ public LogicalPlan visitValues(Values node, AnalysisContext context) { List> values = node.getValues(); List> valueExprs = new ArrayList<>(); for (List value : values) { - valueExprs.add(value.stream() - .map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context)) - .collect(Collectors.toList())); + valueExprs.add( + value.stream() + .map(val -> (LiteralExpression) expressionAnalyzer.analyze(val, context)) + .collect(Collectors.toList())); } return new LogicalValues(valueExprs); } - /** - * Build {@link LogicalMLCommons} for Kmeans command. - */ + /** Build {@link LogicalMLCommons} for Kmeans command. */ @Override public LogicalPlan visitKmeans(Kmeans node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -542,9 +538,7 @@ public LogicalPlan visitKmeans(Kmeans node, AnalysisContext context) { return new LogicalMLCommons(child, "kmeans", options); } - /** - * Build {@link LogicalAD} for AD command. - */ + /** Build {@link LogicalAD} for AD command. */ @Override public LogicalPlan visitAD(AD node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); @@ -557,21 +551,21 @@ public LogicalPlan visitAD(AD node, AnalysisContext context) { currentEnv.define(new Symbol(Namespace.FIELD_NAME, RCF_ANOMALOUS), ExprCoreType.BOOLEAN); } else { currentEnv.define(new Symbol(Namespace.FIELD_NAME, RCF_ANOMALY_GRADE), ExprCoreType.DOUBLE); - currentEnv.define(new Symbol(Namespace.FIELD_NAME, - (String) node.getArguments().get(TIME_FIELD).getValue()), ExprCoreType.TIMESTAMP); + currentEnv.define( + new Symbol(Namespace.FIELD_NAME, (String) node.getArguments().get(TIME_FIELD).getValue()), + ExprCoreType.TIMESTAMP); } return new LogicalAD(child, options); } - /** - * Build {@link LogicalML} for ml command. - */ + /** Build {@link LogicalML} for ml command. */ @Override public LogicalPlan visitML(ML node, AnalysisContext context) { LogicalPlan child = node.getChild().get(0).accept(this, context); TypeEnvironment currentEnv = context.peek(); node.getOutputSchema(currentEnv).entrySet().stream() - .forEach(v -> currentEnv.define(new Symbol(Namespace.FIELD_NAME, v.getKey()), v.getValue())); + .forEach( + v -> currentEnv.define(new Symbol(Namespace.FIELD_NAME, v.getKey()), v.getValue())); return new LogicalML(child, node.getArguments()); } @@ -584,8 +578,9 @@ public LogicalPlan visitPaginate(Paginate paginate, AnalysisContext context) { @Override public LogicalPlan visitFetchCursor(FetchCursor cursor, AnalysisContext context) { - return new LogicalFetchCursor(cursor.getCursor(), - dataSourceService.getDataSource(DEFAULT_DATASOURCE_NAME).getStorageEngine()); + return new LogicalFetchCursor( + cursor.getCursor(), + dataSourceService.getDataSource(DEFAULT_DATASOURCE_NAME).getStorageEngine()); } @Override @@ -594,13 +589,13 @@ public LogicalPlan visitCloseCursor(CloseCursor closeCursor, AnalysisContext con } /** - * The first argument is always "asc", others are optional. - * Given nullFirst argument, use its value. Otherwise just use DEFAULT_ASC/DESC. + * The first argument is always "asc", others are optional. Given nullFirst argument, use its + * value. Otherwise just use DEFAULT_ASC/DESC. */ private SortOption analyzeSortOption(List fieldArgs) { Boolean asc = (Boolean) fieldArgs.get(0).getValue().getValue(); - Optional nullFirst = fieldArgs.stream() - .filter(option -> "nullFirst".equals(option.getArgName())).findFirst(); + Optional nullFirst = + fieldArgs.stream().filter(option -> "nullFirst".equals(option.getArgName())).findFirst(); if (nullFirst.isPresent()) { Boolean isNullFirst = (Boolean) nullFirst.get().getValue().getValue(); diff --git a/core/src/main/java/org/opensearch/sql/analysis/DataSourceSchemaIdentifierNameResolver.java b/core/src/main/java/org/opensearch/sql/analysis/DataSourceSchemaIdentifierNameResolver.java index 1bb83169074..01145dc7df3 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/DataSourceSchemaIdentifierNameResolver.java +++ b/core/src/main/java/org/opensearch/sql/analysis/DataSourceSchemaIdentifierNameResolver.java @@ -7,13 +7,8 @@ package org.opensearch.sql.analysis; -import com.google.common.collect.ImmutableSet; import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import org.opensearch.sql.ast.expression.QualifiedName; import org.opensearch.sql.datasource.DataSourceService; -import org.opensearch.sql.datasource.model.DataSourceMetadata; public class DataSourceSchemaIdentifierNameResolver { @@ -29,19 +24,17 @@ public class DataSourceSchemaIdentifierNameResolver { private static final String DOT = "."; /** - * Data model for capturing dataSourceName, schema and identifier from - * fully qualifiedName. In the current state, it is used to capture - * DataSourceSchemaTable name and DataSourceSchemaFunction in case of table - * functions. + * Data model for capturing dataSourceName, schema and identifier from fully qualifiedName. In the + * current state, it is used to capture DataSourceSchemaTable name and DataSourceSchemaFunction in + * case of table functions. * * @param dataSourceService {@link DataSourceService}. - * @param parts parts of qualifiedName. + * @param parts parts of qualifiedName. */ - public DataSourceSchemaIdentifierNameResolver(DataSourceService dataSourceService, - List parts) { + public DataSourceSchemaIdentifierNameResolver( + DataSourceService dataSourceService, List parts) { this.dataSourceService = dataSourceService; - List remainingParts - = captureSchemaName(captureDataSourceName(parts)); + List remainingParts = captureSchemaName(captureDataSourceName(parts)); identifierName = String.join(DOT, remainingParts); } @@ -57,7 +50,6 @@ public String getSchemaName() { return schemaName; } - // Capture datasource name and return remaining parts(schema name and table name) // from the fully qualified name. private List captureDataSourceName(List parts) { @@ -74,12 +66,11 @@ private List captureDataSourceName(List parts) { private List captureSchemaName(List parts) { if (parts.size() > 1 && (DEFAULT_SCHEMA_NAME.equals(parts.get(0)) - || INFORMATION_SCHEMA_NAME.contains(parts.get(0)))) { + || INFORMATION_SCHEMA_NAME.contains(parts.get(0)))) { schemaName = parts.get(0); return parts.subList(1, parts.size()); } else { return parts; } } - } diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java index a88bef3fe76..5a8d6fe976e 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import static org.opensearch.sql.ast.dsl.AstDSL.and; @@ -49,7 +48,6 @@ import org.opensearch.sql.ast.expression.When; import org.opensearch.sql.ast.expression.WindowFunction; import org.opensearch.sql.ast.expression.Xor; -import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.data.model.ExprValueUtils; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; @@ -78,19 +76,19 @@ * Expression}. */ public class ExpressionAnalyzer extends AbstractNodeVisitor { - @Getter - private final BuiltinFunctionRepository repository; + @Getter private final BuiltinFunctionRepository repository; @Override public Expression visitCast(Cast node, AnalysisContext context) { final Expression expression = node.getExpression().accept(this, context); - return (Expression) repository - .compile(context.getFunctionProperties(), node.convertFunctionName(), + return (Expression) + repository.compile( + context.getFunctionProperties(), + node.convertFunctionName(), Collections.singletonList(expression)); } - public ExpressionAnalyzer( - BuiltinFunctionRepository repository) { + public ExpressionAnalyzer(BuiltinFunctionRepository repository) { this.repository = repository; } @@ -113,8 +111,8 @@ public Expression visitEqualTo(EqualTo node, AnalysisContext context) { @Override public Expression visitLiteral(Literal node, AnalysisContext context) { - return DSL - .literal(ExprValueUtils.fromObjectValue(node.getValue(), node.getType().getCoreType())); + return DSL.literal( + ExprValueUtils.fromObjectValue(node.getValue(), node.getType().getCoreType())); } @Override @@ -163,8 +161,12 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext for (UnresolvedExpression arg : node.getArgList()) { builder.add(arg.accept(this, context)); } - Aggregator aggregator = (Aggregator) repository.compile( - context.getFunctionProperties(), builtinFunctionName.get().getName(), builder.build()); + Aggregator aggregator = + (Aggregator) + repository.compile( + context.getFunctionProperties(), + builtinFunctionName.get().getName(), + builder.build()); aggregator.distinct(node.getDistinct()); if (node.condition() != null) { aggregator.condition(analyze(node.condition(), context)); @@ -177,8 +179,8 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext @Override public Expression visitRelevanceFieldList(RelevanceFieldList node, AnalysisContext context) { - return new LiteralExpression(ExprValueUtils.tupleValue( - ImmutableMap.copyOf(node.getFieldList()))); + return new LiteralExpression( + ExprValueUtils.tupleValue(ImmutableMap.copyOf(node.getFieldList()))); } @Override @@ -186,19 +188,19 @@ public Expression visitFunction(Function node, AnalysisContext context) { FunctionName functionName = FunctionName.of(node.getFuncName()); List arguments = node.getFuncArgs().stream() - .map(unresolvedExpression -> { - var ret = analyze(unresolvedExpression, context); - if (ret == null) { - throw new UnsupportedOperationException( - String.format("Invalid use of expression %s", unresolvedExpression) - ); - } else { - return ret; - } - }) + .map( + unresolvedExpression -> { + var ret = analyze(unresolvedExpression, context); + if (ret == null) { + throw new UnsupportedOperationException( + String.format("Invalid use of expression %s", unresolvedExpression)); + } else { + return ret; + } + }) .collect(Collectors.toList()); - return (Expression) repository.compile(context.getFunctionProperties(), - functionName, arguments); + return (Expression) + repository.compile(context.getFunctionProperties(), functionName, arguments); } @SuppressWarnings("unchecked") @@ -220,18 +222,20 @@ public Expression visitHighlightFunction(HighlightFunction node, AnalysisContext /** * visitScoreFunction removes the score function from the AST and replaces it with the child - * relevance function node. If the optional boost variable is provided, the boost argument - * of the relevance function is combined. + * relevance function node. If the optional boost variable is provided, the boost argument of the + * relevance function is combined. * - * @param node score function node + * @param node score function node * @param context analysis context for the query * @return resolved relevance function */ public Expression visitScoreFunction(ScoreFunction node, AnalysisContext context) { Literal boostArg = node.getRelevanceFieldWeight(); if (!boostArg.getType().equals(DataType.DOUBLE)) { - throw new SemanticCheckException(String.format("Expected boost type '%s' but got '%s'", - DataType.DOUBLE.name(), boostArg.getType().name())); + throw new SemanticCheckException( + String.format( + "Expected boost type '%s' but got '%s'", + DataType.DOUBLE.name(), boostArg.getType().name())); } Double thisBoostValue = ((Double) boostArg.getValue()); @@ -249,10 +253,9 @@ public Expression visitScoreFunction(ScoreFunction node, AnalysisContext context Literal boostArgLiteral = (Literal) ((UnresolvedArgument) expr).getValue(); Double boostValue = Double.parseDouble((String) boostArgLiteral.getValue()) * thisBoostValue; - UnresolvedArgument newBoostArg = new UnresolvedArgument( - argumentName, - new Literal(boostValue.toString(), DataType.STRING) - ); + UnresolvedArgument newBoostArg = + new UnresolvedArgument( + argumentName, new Literal(boostValue.toString(), DataType.STRING)); updatedFuncArgs.add(newBoostArg); } else { updatedFuncArgs.add(expr); @@ -261,18 +264,18 @@ public Expression visitScoreFunction(ScoreFunction node, AnalysisContext context // since nothing was found, add an argument if (!doesFunctionContainBoostArgument) { - UnresolvedArgument newBoostArg = new UnresolvedArgument( + UnresolvedArgument newBoostArg = + new UnresolvedArgument( "boost", new Literal(Double.toString(thisBoostValue), DataType.STRING)); updatedFuncArgs.add(newBoostArg); } // create a new function expression with boost argument and resolve it - Function updatedRelevanceQueryUnresolvedExpr = new Function( - relevanceQueryUnresolvedExpr.getFuncName(), - updatedFuncArgs); + Function updatedRelevanceQueryUnresolvedExpr = + new Function(relevanceQueryUnresolvedExpr.getFuncName(), updatedFuncArgs); OpenSearchFunctions.OpenSearchFunction relevanceQueryExpr = - (OpenSearchFunctions.OpenSearchFunction) updatedRelevanceQueryUnresolvedExpr - .accept(this, context); + (OpenSearchFunctions.OpenSearchFunction) + updatedRelevanceQueryUnresolvedExpr.accept(this, context); relevanceQueryExpr.setScoreTracked(true); return relevanceQueryExpr; } @@ -301,16 +304,16 @@ public Expression visitCompare(Compare node, AnalysisContext context) { Expression left = analyze(node.getLeft(), context); Expression right = analyze(node.getRight(), context); return (Expression) - repository.compile(context.getFunctionProperties(), - functionName, Arrays.asList(left, right)); + repository.compile( + context.getFunctionProperties(), functionName, Arrays.asList(left, right)); } @Override public Expression visitBetween(Between node, AnalysisContext context) { return and( - compare(">=", node.getValue(), node.getLowerBound()), - compare("<=", node.getValue(), node.getUpperBound()) - ).accept(this, context); + compare(">=", node.getValue(), node.getLowerBound()), + compare("<=", node.getValue(), node.getUpperBound())) + .accept(this, context); } @Override @@ -321,16 +324,18 @@ public Expression visitCase(Case node, AnalysisContext context) { whens.add((WhenClause) analyze(when, context)); } else { // Merge case value and condition (compare value) into a single equal condition - whens.add((WhenClause) analyze( - new When( - new Function("=", Arrays.asList(node.getCaseValue(), when.getCondition())), - when.getResult() - ), context)); + whens.add( + (WhenClause) + analyze( + new When( + new Function("=", Arrays.asList(node.getCaseValue(), when.getCondition())), + when.getResult()), + context)); } } - Expression defaultResult = (node.getElseClause() == null) - ? null : analyze(node.getElseClause(), context); + Expression defaultResult = + (node.getElseClause() == null) ? null : analyze(node.getElseClause(), context); CaseClause caseClause = new CaseClause(whens, defaultResult); // To make this simple, require all result type same regardless of implicit convert @@ -346,8 +351,7 @@ public Expression visitCase(Case node, AnalysisContext context) { @Override public Expression visitWhen(When node, AnalysisContext context) { return new WhenClause( - analyze(node.getCondition(), context), - analyze(node.getResult(), context)); + analyze(node.getCondition(), context), analyze(node.getResult(), context)); } @Override @@ -377,10 +381,7 @@ public Expression visitQualifiedName(QualifiedName node, AnalysisContext context Optional.ofNullable(typeEnv.lookupAllFields(Namespace.HIDDEN_FIELD_NAME).get(part)); if (exprType.isPresent()) { return visitMetadata( - qualifierAnalyzer.unqualified(node), - (ExprCoreType) exprType.get(), - context - ); + qualifierAnalyzer.unqualified(node), (ExprCoreType) exprType.get(), context); } } } @@ -401,15 +402,15 @@ public Expression visitUnresolvedArgument(UnresolvedArgument node, AnalysisConte } /** - * If QualifiedName is actually a reserved metadata field, return the expr type associated - * with the metadata field. - * @param ident metadata field name + * If QualifiedName is actually a reserved metadata field, return the expr type associated with + * the metadata field. + * + * @param ident metadata field name * @param context analysis context * @return DSL reference */ - private Expression visitMetadata(String ident, - ExprCoreType exprCoreType, - AnalysisContext context) { + private Expression visitMetadata( + String ident, ExprCoreType exprCoreType, AnalysisContext context) { return DSL.ref(ident, exprCoreType); } @@ -422,8 +423,8 @@ private Expression visitIdentifier(String ident, AnalysisContext context) { } TypeEnvironment typeEnv = context.peek(); - ReferenceExpression ref = DSL.ref(ident, - typeEnv.resolve(new Symbol(Namespace.FIELD_NAME, ident))); + ReferenceExpression ref = + DSL.ref(ident, typeEnv.resolve(new Symbol(Namespace.FIELD_NAME, ident))); return ref; } diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java index eaf5c4abca0..8fb30fdbbf2 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import java.util.HashMap; @@ -26,25 +25,20 @@ import org.opensearch.sql.planner.logical.LogicalWindow; /** - * The optimizer used to replace the expression referred in the SelectClause - * e.g. The query SELECT abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name). - * will be translated the AST - * Project[abs(age), sub(sum(age), avg(age)) - * Agg(agg=[sum(age), avg(age)], group=[abs(age)]] - * Relation - * The sum(age) and avg(age) in the Project could be replace by the analyzed reference, the - * LogicalPlan should be - * LogicalProject[Ref("abs(age)"), sub(Ref("sum(age)"), Ref("avg(age)")) - * LogicalAgg(agg=[sum(age), avg(age)], group=[abs(age)]] - * LogicalRelation + * The optimizer used to replace the expression referred in the SelectClause e.g. The query SELECT + * abs(name), sum(age)-avg(age) FROM test GROUP BY abs(name). will be translated the AST + * Project[abs(age), sub(sum(age), avg(age)) Agg(agg=[sum(age), avg(age)], group=[abs(age)]] + * Relation The sum(age) and avg(age) in the Project could be replace by the analyzed reference, the + * LogicalPlan should be LogicalProject[Ref("abs(age)"), sub(Ref("sum(age)"), Ref("avg(age)")) + * LogicalAgg(agg=[sum(age), avg(age)], group=[abs(age)]] LogicalRelation */ public class ExpressionReferenceOptimizer extends ExpressionNodeVisitor { private final BuiltinFunctionRepository repository; /** - * The map of expression and it's reference. - * For example, The NamedAggregator should produce the map of Aggregator to Ref(name) + * The map of expression and it's reference. For example, The NamedAggregator should produce the + * map of Aggregator to Ref(name) */ private final Map expressionMap = new HashMap<>(); @@ -69,17 +63,16 @@ public Expression visitFunction(FunctionExpression node, AnalysisContext context return expressionMap.get(node); } else { final List args = - node.getArguments().stream().map(expr -> expr.accept(this, context)) + node.getArguments().stream() + .map(expr -> expr.accept(this, context)) .collect(Collectors.toList()); - Expression optimizedFunctionExpression = (Expression) repository.compile( - context.getFunctionProperties(), - node.getFunctionName(), - args - ); + Expression optimizedFunctionExpression = + (Expression) + repository.compile(context.getFunctionProperties(), node.getFunctionName(), args); // Propagate scoreTracked for OpenSearch functions if (optimizedFunctionExpression instanceof OpenSearchFunctions.OpenSearchFunction) { - ((OpenSearchFunctions.OpenSearchFunction) optimizedFunctionExpression).setScoreTracked( - ((OpenSearchFunctions.OpenSearchFunction)node).isScoreTracked()); + ((OpenSearchFunctions.OpenSearchFunction) optimizedFunctionExpression) + .setScoreTracked(((OpenSearchFunctions.OpenSearchFunction) node).isScoreTracked()); } return optimizedFunctionExpression; } @@ -98,19 +91,17 @@ public Expression visitNamed(NamedExpression node, AnalysisContext context) { return node.getDelegated().accept(this, context); } - /** - * Implement this because Case/When is not registered in function repository. - */ + /** Implement this because Case/When is not registered in function repository. */ @Override public Expression visitCase(CaseClause node, AnalysisContext context) { if (expressionMap.containsKey(node)) { return expressionMap.get(node); } - List whenClauses = node.getWhenClauses() - .stream() - .map(expr -> (WhenClause) expr.accept(this, context)) - .collect(Collectors.toList()); + List whenClauses = + node.getWhenClauses().stream() + .map(expr -> (WhenClause) expr.accept(this, context)) + .collect(Collectors.toList()); Expression defaultResult = null; if (node.getDefaultResult() != null) { defaultResult = node.getDefaultResult().accept(this, context); @@ -121,14 +112,10 @@ public Expression visitCase(CaseClause node, AnalysisContext context) { @Override public Expression visitWhen(WhenClause node, AnalysisContext context) { return new WhenClause( - node.getCondition().accept(this, context), - node.getResult().accept(this, context)); + node.getCondition().accept(this, context), node.getResult().accept(this, context)); } - - /** - * Expression Map Builder. - */ + /** Expression Map Builder. */ class ExpressionMapBuilder extends LogicalPlanNodeVisitor { @Override @@ -140,20 +127,27 @@ public Void visitNode(LogicalPlan plan, Void context) { @Override public Void visitAggregation(LogicalAggregation plan, Void context) { // Create the mapping for all the aggregator. - plan.getAggregatorList().forEach(namedAggregator -> expressionMap - .put(namedAggregator.getDelegated(), - new ReferenceExpression(namedAggregator.getName(), namedAggregator.type()))); + plan.getAggregatorList() + .forEach( + namedAggregator -> + expressionMap.put( + namedAggregator.getDelegated(), + new ReferenceExpression(namedAggregator.getName(), namedAggregator.type()))); // Create the mapping for all the group by. - plan.getGroupByList().forEach(groupBy -> expressionMap - .put(groupBy.getDelegated(), - new ReferenceExpression(groupBy.getNameOrAlias(), groupBy.type()))); + plan.getGroupByList() + .forEach( + groupBy -> + expressionMap.put( + groupBy.getDelegated(), + new ReferenceExpression(groupBy.getNameOrAlias(), groupBy.type()))); return null; } @Override public Void visitWindow(LogicalWindow plan, Void context) { Expression windowFunc = plan.getWindowFunction(); - expressionMap.put(windowFunc, + expressionMap.put( + windowFunc, new ReferenceExpression(((NamedExpression) windowFunc).getName(), windowFunc.type())); return visitNode(plan, context); } diff --git a/core/src/main/java/org/opensearch/sql/analysis/HighlightAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/HighlightAnalyzer.java index 0a15c6bac86..386c6e9d9fd 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/HighlightAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/HighlightAnalyzer.java @@ -14,10 +14,7 @@ import org.opensearch.sql.planner.logical.LogicalHighlight; import org.opensearch.sql.planner.logical.LogicalPlan; -/** - * Analyze the highlight in the {@link AnalysisContext} to construct the {@link - * LogicalPlan}. - */ +/** Analyze the highlight in the {@link AnalysisContext} to construct the {@link LogicalPlan}. */ @RequiredArgsConstructor public class HighlightAnalyzer extends AbstractNodeVisitor { private final ExpressionAnalyzer expressionAnalyzer; diff --git a/core/src/main/java/org/opensearch/sql/analysis/NamedExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/NamedExpressionAnalyzer.java index 1d318c55886..43bd411b42f 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/NamedExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/NamedExpressionAnalyzer.java @@ -3,33 +3,26 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import lombok.RequiredArgsConstructor; import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.expression.Alias; import org.opensearch.sql.ast.expression.QualifiedName; -import org.opensearch.sql.ast.expression.Span; import org.opensearch.sql.ast.expression.UnresolvedExpression; import org.opensearch.sql.expression.DSL; import org.opensearch.sql.expression.NamedExpression; -import org.opensearch.sql.expression.span.SpanExpression; /** - * Analyze the Alias node in the {@link AnalysisContext} to construct the list of - * {@link NamedExpression}. + * Analyze the Alias node in the {@link AnalysisContext} to construct the list of {@link + * NamedExpression}. */ @RequiredArgsConstructor -public class NamedExpressionAnalyzer extends - AbstractNodeVisitor { +public class NamedExpressionAnalyzer extends AbstractNodeVisitor { private final ExpressionAnalyzer expressionAnalyzer; - /** - * Analyze Select fields. - */ - public NamedExpression analyze(UnresolvedExpression expression, - AnalysisContext analysisContext) { + /** Analyze Select fields. */ + public NamedExpression analyze(UnresolvedExpression expression, AnalysisContext analysisContext) { return expression.accept(this, analysisContext); } diff --git a/core/src/main/java/org/opensearch/sql/analysis/NestedAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/NestedAnalyzer.java index f0508245578..ef8f1428018 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/NestedAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/NestedAnalyzer.java @@ -27,8 +27,7 @@ import org.opensearch.sql.planner.logical.LogicalPlan; /** - * Analyze the Nested Function in the {@link AnalysisContext} to construct the {@link - * LogicalPlan}. + * Analyze the Nested Function in the {@link AnalysisContext} to construct the {@link LogicalPlan}. */ @RequiredArgsConstructor public class NestedAnalyzer extends AbstractNodeVisitor { @@ -52,15 +51,15 @@ public LogicalPlan visitNestedAllTupleFields(NestedAllTupleFields node, Analysis for (NamedExpression namedExpr : namedExpressions) { if (isNestedFunction(namedExpr.getDelegated())) { ReferenceExpression field = - (ReferenceExpression) ((FunctionExpression) namedExpr.getDelegated()) - .getArguments().get(0); + (ReferenceExpression) + ((FunctionExpression) namedExpr.getDelegated()).getArguments().get(0); // If path is same as NestedAllTupleFields path - if (field.getAttr().substring(0, field.getAttr().lastIndexOf(".")) + if (field + .getAttr() + .substring(0, field.getAttr().lastIndexOf(".")) .equalsIgnoreCase(node.getPath())) { - args.add(Map.of( - "field", field, - "path", new ReferenceExpression(node.getPath(), STRING))); + args.add(Map.of("field", field, "path", new ReferenceExpression(node.getPath(), STRING))); } } } @@ -75,20 +74,24 @@ public LogicalPlan visitFunction(Function node, AnalysisContext context) { List expressions = node.getFuncArgs(); validateArgs(expressions); ReferenceExpression nestedField = - (ReferenceExpression)expressionAnalyzer.analyze(expressions.get(0), context); + (ReferenceExpression) expressionAnalyzer.analyze(expressions.get(0), context); Map args; // Path parameter is supplied if (expressions.size() == 2) { - args = Map.of( - "field", nestedField, - "path", (ReferenceExpression)expressionAnalyzer.analyze(expressions.get(1), context) - ); + args = + Map.of( + "field", + nestedField, + "path", + (ReferenceExpression) expressionAnalyzer.analyze(expressions.get(1), context)); } else { - args = Map.of( - "field", (ReferenceExpression)expressionAnalyzer.analyze(expressions.get(0), context), - "path", generatePath(nestedField.toString()) - ); + args = + Map.of( + "field", + (ReferenceExpression) expressionAnalyzer.analyze(expressions.get(0), context), + "path", + generatePath(nestedField.toString())); } return mergeChildIfLogicalNested(new ArrayList<>(Arrays.asList(args))); @@ -97,8 +100,9 @@ public LogicalPlan visitFunction(Function node, AnalysisContext context) { } /** - * NestedAnalyzer visits all functions in SELECT clause, creates logical plans for each and - * merges them. This is to avoid another merge rule in LogicalPlanOptimizer:create(). + * NestedAnalyzer visits all functions in SELECT clause, creates logical plans for each and merges + * them. This is to avoid another merge rule in LogicalPlanOptimizer:create(). + * * @param args field and path params to add to logical plan. * @return child of logical nested with added args, or new LogicalNested. */ @@ -113,34 +117,33 @@ private LogicalPlan mergeChildIfLogicalNested(List args) { if (args.size() < 1 || args.size() > 2) { throw new IllegalArgumentException( - "on nested object only allowed 2 parameters (field,path) or 1 parameter (field)" - ); + "on nested object only allowed 2 parameters (field,path) or 1 parameter (field)"); } for (int i = 0; i < args.size(); i++) { if (!(args.get(i) instanceof QualifiedName)) { throw new IllegalArgumentException( - String.format("Illegal nested field name: %s", args.get(i).toString()) - ); + String.format("Illegal nested field name: %s", args.get(i).toString())); } - if (i == 0 && ((QualifiedName)args.get(i)).getParts().size() < 2) { + if (i == 0 && ((QualifiedName) args.get(i)).getParts().size() < 2) { throw new IllegalArgumentException( - String.format("Illegal nested field name: %s", args.get(i).toString()) - ); + String.format("Illegal nested field name: %s", args.get(i).toString())); } } } /** * Generate nested path dynamically. Assumes at least one level of nesting in supplied string. + * * @param field : Nested field to generate path of. * @return : Path of field derived from last level of nesting. */ @@ -150,12 +153,15 @@ public static ReferenceExpression generatePath(String field) { /** * Check if supplied expression is a nested function. + * * @param expr Expression checking if is nested function. * @return True if expression is a nested function. */ public static Boolean isNestedFunction(Expression expr) { return (expr instanceof FunctionExpression - && ((FunctionExpression) expr).getFunctionName().getFunctionName() - .equalsIgnoreCase(BuiltinFunctionName.NESTED.name())); + && ((FunctionExpression) expr) + .getFunctionName() + .getFunctionName() + .equalsIgnoreCase(BuiltinFunctionName.NESTED.name())); } } diff --git a/core/src/main/java/org/opensearch/sql/analysis/QualifierAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/QualifierAnalyzer.java index d1e31d00796..27dd6a22437 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/QualifierAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/QualifierAnalyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import java.util.Arrays; @@ -15,9 +14,7 @@ import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.exception.SemanticCheckException; -/** - * Analyzer that analyzes qualifier(s) in a full field name. - */ +/** Analyzer that analyzes qualifier(s) in a full field name. */ @RequiredArgsConstructor public class QualifierAnalyzer { @@ -28,11 +25,11 @@ public String unqualified(String... parts) { } /** - * Get unqualified name if its qualifier symbol found is in index namespace - * on type environment. Unqualified name means name with qualifier removed. - * For example, unqualified name of "accounts.age" or "acc.age" is "age". + * Get unqualified name if its qualifier symbol found is in index namespace on type environment. + * Unqualified name means name with qualifier removed. For example, unqualified name of + * "accounts.age" or "acc.age" is "age". * - * @return unqualified name if criteria met above, otherwise original name + * @return unqualified name if criteria met above, otherwise original name */ public String unqualified(QualifiedName fullName) { return isQualifierIndexOrAlias(fullName) ? fullName.rest().toString() : fullName.toString(); @@ -66,10 +63,11 @@ private void resolveQualifierSymbol(QualifiedName fullName, String qualifier) { } catch (SemanticCheckException e) { // Throw syntax check intentionally to indicate fall back to old engine. // Need change to semantic check exception in future. - throw new SyntaxCheckException(String.format( - "The qualifier [%s] of qualified name [%s] must be an field name, index name or its " - + "alias", qualifier, fullName)); + throw new SyntaxCheckException( + String.format( + "The qualifier [%s] of qualified name [%s] must be an field name, index name or its " + + "alias", + qualifier, fullName)); } } - } diff --git a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java index 734f37378bf..be1ae2d235d 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/SelectExpressionAnalyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import com.google.common.collect.ImmutableList; @@ -30,23 +29,21 @@ import org.opensearch.sql.expression.ReferenceExpression; /** - * Analyze the select list in the {@link AnalysisContext} to construct the list of - * {@link NamedExpression}. + * Analyze the select list in the {@link AnalysisContext} to construct the list of {@link + * NamedExpression}. */ @RequiredArgsConstructor public class SelectExpressionAnalyzer - extends - AbstractNodeVisitor, AnalysisContext> { + extends AbstractNodeVisitor, AnalysisContext> { private final ExpressionAnalyzer expressionAnalyzer; private ExpressionReferenceOptimizer optimizer; - /** - * Analyze Select fields. - */ - public List analyze(List selectList, - AnalysisContext analysisContext, - ExpressionReferenceOptimizer optimizer) { + /** Analyze Select fields. */ + public List analyze( + List selectList, + AnalysisContext analysisContext, + ExpressionReferenceOptimizer optimizer) { this.optimizer = optimizer; ImmutableList.Builder builder = new ImmutableList.Builder<>(); for (UnresolvedExpression unresolvedExpression : selectList) { @@ -68,50 +65,44 @@ public List visitAlias(Alias node, AnalysisContext context) { } Expression expr = referenceIfSymbolDefined(node, context); - return Collections.singletonList(DSL.named( - unqualifiedNameIfFieldOnly(node, context), - expr, - node.getAlias())); + return Collections.singletonList( + DSL.named(unqualifiedNameIfFieldOnly(node, context), expr, node.getAlias())); } /** - * The Alias could be - * 1. SELECT name, AVG(age) FROM s BY name -> - * Project(Alias("name", expr), Alias("AVG(age)", aggExpr)) - * Agg(Alias("AVG(age)", aggExpr)) - * 2. SELECT length(name), AVG(age) FROM s BY length(name) - * Project(Alias("name", expr), Alias("AVG(age)", aggExpr)) - * Agg(Alias("AVG(age)", aggExpr)) - * 3. SELECT length(name) as l, AVG(age) FROM s BY l - * Project(Alias("name", expr, l), Alias("AVG(age)", aggExpr)) - * Agg(Alias("AVG(age)", aggExpr), Alias("length(name)", groupExpr)) + * The Alias could be 1. SELECT name, AVG(age) FROM s BY name -> Project(Alias("name", expr), + * Alias("AVG(age)", aggExpr)) Agg(Alias("AVG(age)", aggExpr)) 2. SELECT length(name), AVG(age) + * FROM s BY length(name) Project(Alias("name", expr), Alias("AVG(age)", aggExpr)) + * Agg(Alias("AVG(age)", aggExpr)) 3. SELECT length(name) as l, AVG(age) FROM s BY l + * Project(Alias("name", expr, l), Alias("AVG(age)", aggExpr)) Agg(Alias("AVG(age)", aggExpr), + * Alias("length(name)", groupExpr)) */ - private Expression referenceIfSymbolDefined(Alias expr, - AnalysisContext context) { + private Expression referenceIfSymbolDefined(Alias expr, AnalysisContext context) { UnresolvedExpression delegatedExpr = expr.getDelegated(); // Pass named expression because expression like window function loses full name // (OVER clause) and thus depends on name in alias to be replaced correctly return optimizer.optimize( DSL.named( - expr.getName(), - delegatedExpr.accept(expressionAnalyzer, context), - expr.getAlias()), + expr.getName(), delegatedExpr.accept(expressionAnalyzer, context), expr.getAlias()), context); } @Override - public List visitAllFields(AllFields node, - AnalysisContext context) { + public List visitAllFields(AllFields node, AnalysisContext context) { TypeEnvironment environment = context.peek(); Map lookupAllFields = environment.lookupAllFields(Namespace.FIELD_NAME); - return lookupAllFields.entrySet().stream().map(entry -> DSL.named(entry.getKey(), - new ReferenceExpression(entry.getKey(), entry.getValue()))).collect(Collectors.toList()); + return lookupAllFields.entrySet().stream() + .map( + entry -> + DSL.named( + entry.getKey(), new ReferenceExpression(entry.getKey(), entry.getValue()))) + .collect(Collectors.toList()); } @Override - public List visitNestedAllTupleFields(NestedAllTupleFields node, - AnalysisContext context) { + public List visitNestedAllTupleFields( + NestedAllTupleFields node, AnalysisContext context) { TypeEnvironment environment = context.peek(); Map lookupAllTupleFields = environment.lookupAllTupleFields(Namespace.FIELD_NAME); @@ -121,25 +112,25 @@ public List visitNestedAllTupleFields(NestedAllTupleFields node Pattern p = Pattern.compile(node.getPath() + "\\.[^\\.]+$"); return lookupAllTupleFields.entrySet().stream() .filter(field -> p.matcher(field.getKey()).find()) - .map(entry -> { - Expression nestedFunc = new Function( - "nested", - List.of( - new QualifiedName(List.of(entry.getKey().split("\\.")))) - ).accept(expressionAnalyzer, context); - return DSL.named("nested(" + entry.getKey() + ")", nestedFunc); - }) + .map( + entry -> { + Expression nestedFunc = + new Function( + "nested", + List.of(new QualifiedName(List.of(entry.getKey().split("\\."))))) + .accept(expressionAnalyzer, context); + return DSL.named("nested(" + entry.getKey() + ")", nestedFunc); + }) .collect(Collectors.toList()); } /** - * Get unqualified name if select item is just a field. For example, suppose an index - * named "accounts", return "age" for "SELECT accounts.age". But do nothing for expression - * in "SELECT ABS(accounts.age)". - * Note that an assumption is made implicitly that original name field in Alias must be - * the same as the values in QualifiedName. This is true because AST builder does this. - * Otherwise, what unqualified() returns will override Alias's name as NamedExpression's name - * even though the QualifiedName doesn't have qualifier. + * Get unqualified name if select item is just a field. For example, suppose an index named + * "accounts", return "age" for "SELECT accounts.age". But do nothing for expression in "SELECT + * ABS(accounts.age)". Note that an assumption is made implicitly that original name field in + * Alias must be the same as the values in QualifiedName. This is true because AST builder does + * this. Otherwise, what unqualified() returns will override Alias's name as NamedExpression's + * name even though the QualifiedName doesn't have qualifier. */ private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) { UnresolvedExpression selectItem = node.getDelegated(); @@ -149,5 +140,4 @@ private String unqualifiedNameIfFieldOnly(Alias node, AnalysisContext context) { } return node.getName(); } - } diff --git a/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java b/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java index 8032cba706b..18693a63e62 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java +++ b/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import static org.opensearch.sql.analysis.symbol.Namespace.FIELD_NAME; @@ -21,12 +20,9 @@ import org.opensearch.sql.expression.ReferenceExpression; import org.opensearch.sql.expression.env.Environment; -/** - * The definition of Type Environment. - */ +/** The definition of Type Environment. */ public class TypeEnvironment implements Environment { - @Getter - private final TypeEnvironment parent; + @Getter private final TypeEnvironment parent; private final SymbolTable symbolTable; /** @@ -64,15 +60,14 @@ public ExprType resolve(Symbol symbol) { return typeOptional.get(); } } - throw new SemanticCheckException( - String.format("can't resolve %s in type env", symbol)); + throw new SemanticCheckException(String.format("can't resolve %s in type env", symbol)); } /** * Resolve all fields in the current environment. * - * @param namespace a namespace - * @return all symbols in the namespace + * @param namespace a namespace + * @return all symbols in the namespace */ public Map lookupAllFields(Namespace namespace) { Map result = new LinkedHashMap<>(); @@ -82,8 +77,9 @@ public Map lookupAllFields(Namespace namespace) { /** * Resolve all fields in the current environment. - * @param namespace a namespace - * @return all symbols in the namespace + * + * @param namespace a namespace + * @return all symbols in the namespace */ public Map lookupAllTupleFields(Namespace namespace) { Map result = new LinkedHashMap<>(); @@ -95,7 +91,7 @@ public Map lookupAllTupleFields(Namespace namespace) { * Define symbol with the type. * * @param symbol symbol to define - * @param type type + * @param type type */ public void define(Symbol symbol, ExprType type) { symbolTable.store(symbol, type); @@ -114,18 +110,13 @@ public void remove(Symbol symbol) { symbolTable.remove(symbol); } - /** - * Remove ref. - */ + /** Remove ref. */ public void remove(ReferenceExpression ref) { remove(new Symbol(FIELD_NAME, ref.getAttr())); } - /** - * Clear all fields in the current environment. - */ + /** Clear all fields in the current environment. */ public void clearAllFields() { - lookupAllFields(FIELD_NAME).keySet().forEach( - v -> remove(new Symbol(Namespace.FIELD_NAME, v))); + lookupAllFields(FIELD_NAME).keySet().forEach(v -> remove(new Symbol(Namespace.FIELD_NAME, v))); } } diff --git a/core/src/main/java/org/opensearch/sql/analysis/WindowExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/WindowExpressionAnalyzer.java index 3abcf9e1402..c4229e46643 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/WindowExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/WindowExpressionAnalyzer.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis; import static org.opensearch.sql.ast.tree.Sort.SortOption.DEFAULT_ASC; @@ -29,28 +28,25 @@ import org.opensearch.sql.planner.logical.LogicalWindow; /** - * Window expression analyzer that analyzes window function expression in expression list - * in project operator. + * Window expression analyzer that analyzes window function expression in expression list in project + * operator. */ @RequiredArgsConstructor public class WindowExpressionAnalyzer extends AbstractNodeVisitor { - /** - * Expression analyzer. - */ + /** Expression analyzer. */ private final ExpressionAnalyzer expressionAnalyzer; - /** - * Child node to be wrapped by a new window operator. - */ + /** Child node to be wrapped by a new window operator. */ private final LogicalPlan child; /** - * Analyze the given project item and return window operator (with child node inside) - * if the given project item is a window function. - * @param projectItem project item - * @param context analysis context - * @return window operator or original child if not windowed + * Analyze the given project item and return window operator (with child node inside) if the given + * project item is a window function. + * + * @param projectItem project item + * @param context analysis context + * @return window operator or original child if not windowed */ public LogicalPlan analyze(UnresolvedExpression projectItem, AnalysisContext context) { LogicalPlan window = projectItem.accept(this, context); @@ -77,26 +73,24 @@ public LogicalPlan visitAlias(Alias node, AnalysisContext context) { return new LogicalWindow(child, namedWindowFunction, windowDefinition); } return new LogicalWindow( - new LogicalSort(child, allSortItems), - namedWindowFunction, - windowDefinition); + new LogicalSort(child, allSortItems), namedWindowFunction, windowDefinition); } private List analyzePartitionList(WindowFunction node, AnalysisContext context) { - return node.getPartitionByList() - .stream() - .map(expr -> expressionAnalyzer.analyze(expr, context)) - .collect(Collectors.toList()); + return node.getPartitionByList().stream() + .map(expr -> expressionAnalyzer.analyze(expr, context)) + .collect(Collectors.toList()); } - private List> analyzeSortList(WindowFunction node, - AnalysisContext context) { - return node.getSortList() - .stream() - .map(pair -> ImmutablePair - .of(analyzeSortOption(pair.getLeft()), - expressionAnalyzer.analyze(pair.getRight(), context))) - .collect(Collectors.toList()); + private List> analyzeSortList( + WindowFunction node, AnalysisContext context) { + return node.getSortList().stream() + .map( + pair -> + ImmutablePair.of( + analyzeSortOption(pair.getLeft()), + expressionAnalyzer.analyze(pair.getRight(), context))) + .collect(Collectors.toList()); } /** @@ -107,9 +101,6 @@ private SortOption analyzeSortOption(SortOption option) { if (option.getNullOrder() == null) { return (option.getSortOrder() == DESC) ? DEFAULT_DESC : DEFAULT_ASC; } - return new SortOption( - (option.getSortOrder() == DESC) ? DESC : ASC, - option.getNullOrder()); + return new SortOption((option.getSortOrder() == DESC) ? DESC : ASC, option.getNullOrder()); } - } diff --git a/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java b/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java index 32cb9dee353..e8a74540145 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java +++ b/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java @@ -3,14 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis.symbol; -/** - * Namespace of symbol to avoid naming conflict. - */ +/** Namespace of symbol to avoid naming conflict. */ public enum Namespace { - INDEX_NAME("Index"), FIELD_NAME("Field"), HIDDEN_FIELD_NAME("HiddenField"), @@ -21,5 +17,4 @@ public enum Namespace { Namespace(String name) { this.name = name; } - } diff --git a/core/src/main/java/org/opensearch/sql/analysis/symbol/Symbol.java b/core/src/main/java/org/opensearch/sql/analysis/symbol/Symbol.java index 8cc9505710b..98fa4b3569b 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/symbol/Symbol.java +++ b/core/src/main/java/org/opensearch/sql/analysis/symbol/Symbol.java @@ -3,16 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis.symbol; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; -/** - * Symbol in the scope. - */ +/** Symbol in the scope. */ @ToString @Getter @RequiredArgsConstructor diff --git a/core/src/main/java/org/opensearch/sql/analysis/symbol/SymbolTable.java b/core/src/main/java/org/opensearch/sql/analysis/symbol/SymbolTable.java index be7435c288f..8bb6824a63d 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/symbol/SymbolTable.java +++ b/core/src/main/java/org/opensearch/sql/analysis/symbol/SymbolTable.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.analysis.symbol; import static java.util.Collections.emptyMap; @@ -17,21 +16,16 @@ import java.util.TreeMap; import org.opensearch.sql.data.type.ExprType; -/** - * Symbol table for symbol definition and resolution. - */ +/** Symbol table for symbol definition and resolution. */ public class SymbolTable { - /** - * Two-dimension hash table to manage symbols with type in different namespace. - */ + /** Two-dimension hash table to manage symbols with type in different namespace. */ private Map> tableByNamespace = new EnumMap<>(Namespace.class); /** - * Two-dimension hash table to manage symbols with type in different namespace. - * Comparing with tableByNamespace, orderedTable use the LinkedHashMap to keep the order of - * symbol. + * Two-dimension hash table to manage symbols with type in different namespace. Comparing with + * tableByNamespace, orderedTable use the LinkedHashMap to keep the order of symbol. */ private Map> orderedTable = new EnumMap<>(Namespace.class); @@ -40,38 +34,32 @@ public class SymbolTable { * Store symbol with the type. Create new map for namespace for the first time. * * @param symbol symbol to define - * @param type symbol type + * @param type symbol type */ public void store(Symbol symbol, ExprType type) { - tableByNamespace.computeIfAbsent( - symbol.getNamespace(), - ns -> new TreeMap<>() - ).put(symbol.getName(), type); + tableByNamespace + .computeIfAbsent(symbol.getNamespace(), ns -> new TreeMap<>()) + .put(symbol.getName(), type); - orderedTable.computeIfAbsent( - symbol.getNamespace(), - ns -> new LinkedHashMap<>() - ).put(symbol.getName(), type); + orderedTable + .computeIfAbsent(symbol.getNamespace(), ns -> new LinkedHashMap<>()) + .put(symbol.getName(), type); } - /** - * Remove a symbol from SymbolTable. - */ + /** Remove a symbol from SymbolTable. */ public void remove(Symbol symbol) { tableByNamespace.computeIfPresent( symbol.getNamespace(), (k, v) -> { v.remove(symbol.getName()); return v; - } - ); + }); orderedTable.computeIfPresent( symbol.getNamespace(), (k, v) -> { v.remove(symbol.getName()); return v; - } - ); + }); } /** @@ -104,42 +92,42 @@ public Map lookupByPrefix(Symbol prefix) { } /** - * Look up all top level symbols in the namespace. - * this function is mainly used by SELECT * use case to get the top level fields - * Todo. currently, the top level fields is the field which doesn't include "." in the name or - * the prefix doesn't exist in the symbol table. - * e.g. The symbol table includes person, person.name, person/2.0. - * person, is the top level field - * person.name, isn't the top level field, because the prefix (person) in symbol table - * person/2.0, is the top level field, because the prefix (person/2) isn't in symbol table + * Look up all top level symbols in the namespace. this function is mainly used by SELECT * use + * case to get the top level fields Todo. currently, the top level fields is the field which + * doesn't include "." in the name or the prefix doesn't exist in the symbol table. e.g. The + * symbol table includes person, person.name, person/2.0. person, is the top level field + * person.name, isn't the top level field, because the prefix (person) in symbol table person/2.0, + * is the top level field, because the prefix (person/2) isn't in symbol table * - * @param namespace a namespace - * @return all symbols in the namespace map + * @param namespace a namespace + * @return all symbols in the namespace map */ public Map lookupAllFields(Namespace namespace) { final LinkedHashMap allSymbols = orderedTable.getOrDefault(namespace, new LinkedHashMap<>()); final LinkedHashMap results = new LinkedHashMap<>(); - allSymbols.entrySet().stream().filter(entry -> { - String symbolName = entry.getKey(); - int lastDot = symbolName.lastIndexOf("."); - return -1 == lastDot || !allSymbols.containsKey(symbolName.substring(0, lastDot)); - }).forEach(entry -> results.put(entry.getKey(), entry.getValue())); + allSymbols.entrySet().stream() + .filter( + entry -> { + String symbolName = entry.getKey(); + int lastDot = symbolName.lastIndexOf("."); + return -1 == lastDot || !allSymbols.containsKey(symbolName.substring(0, lastDot)); + }) + .forEach(entry -> results.put(entry.getKey(), entry.getValue())); return results; } /** * Look up all top level symbols in the namespace. * - * @param namespace a namespace - * @return all symbols in the namespace map + * @param namespace a namespace + * @return all symbols in the namespace map */ public Map lookupAllTupleFields(Namespace namespace) { final LinkedHashMap allSymbols = orderedTable.getOrDefault(namespace, new LinkedHashMap<>()); final LinkedHashMap result = new LinkedHashMap<>(); - allSymbols.entrySet().stream() - .forEach(entry -> result.put(entry.getKey(), entry.getValue())); + allSymbols.entrySet().stream().forEach(entry -> result.put(entry.getKey(), entry.getValue())); return result; } diff --git a/core/src/main/java/org/opensearch/sql/ast/AbstractNodeVisitor.java b/core/src/main/java/org/opensearch/sql/ast/AbstractNodeVisitor.java index f02bc07ccc6..973b10310bd 100644 --- a/core/src/main/java/org/opensearch/sql/ast/AbstractNodeVisitor.java +++ b/core/src/main/java/org/opensearch/sql/ast/AbstractNodeVisitor.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast; import org.opensearch.sql.ast.expression.AggregateFunction; @@ -62,9 +61,7 @@ import org.opensearch.sql.ast.tree.TableFunction; import org.opensearch.sql.ast.tree.Values; -/** - * AST nodes visitor Defines the traverse path. - */ +/** AST nodes visitor Defines the traverse path. */ public abstract class AbstractNodeVisitor { public T visit(Node node, C context) { @@ -73,6 +70,7 @@ public T visit(Node node, C context) { /** * Visit child node. + * * @param node {@link Node} * @param context Context * @return Return Type. diff --git a/core/src/main/java/org/opensearch/sql/ast/Node.java b/core/src/main/java/org/opensearch/sql/ast/Node.java index f3147eeb432..faaf51f221f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/Node.java +++ b/core/src/main/java/org/opensearch/sql/ast/Node.java @@ -3,16 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast; import java.util.List; import lombok.EqualsAndHashCode; import lombok.ToString; -/** - * AST node. - */ +/** AST node. */ @EqualsAndHashCode @ToString public abstract class Node { diff --git a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java index d5f10fcfd4b..c9ca9cc40ac 100644 --- a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java +++ b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.dsl; import java.util.Arrays; @@ -63,9 +62,7 @@ import org.opensearch.sql.ast.tree.UnresolvedPlan; import org.opensearch.sql.ast.tree.Values; -/** - * Class of static methods to create specific node instances. - */ +/** Class of static methods to create specific node instances. */ @UtilityClass public class AstDSL { @@ -132,8 +129,9 @@ public static UnresolvedPlan rename(UnresolvedPlan input, Map... maps) { /** * Initialize Values node by rows of literals. - * @param values rows in which each row is a list of literal values - * @return Values node + * + * @param values rows in which each row is a list of literal values + * @return Values node */ @SafeVarargs public UnresolvedPlan values(List... values) { @@ -249,27 +247,19 @@ public static Function function(String funcName, UnresolvedExpression... funcArg } /** - * CASE - * WHEN search_condition THEN result_expr - * [WHEN search_condition THEN result_expr] ... - * [ELSE result_expr] - * END + * CASE WHEN search_condition THEN result_expr [WHEN search_condition THEN result_expr] ... [ELSE + * result_expr] END */ - public UnresolvedExpression caseWhen(UnresolvedExpression elseClause, - When... whenClauses) { + public UnresolvedExpression caseWhen(UnresolvedExpression elseClause, When... whenClauses) { return caseWhen(null, elseClause, whenClauses); } /** - * CASE case_value_expr - * WHEN compare_expr THEN result_expr - * [WHEN compare_expr THEN result_expr] ... - * [ELSE result_expr] - * END + * CASE case_value_expr WHEN compare_expr THEN result_expr [WHEN compare_expr THEN result_expr] + * ... [ELSE result_expr] END */ - public UnresolvedExpression caseWhen(UnresolvedExpression caseValueExpr, - UnresolvedExpression elseClause, - When... whenClauses) { + public UnresolvedExpression caseWhen( + UnresolvedExpression caseValueExpr, UnresolvedExpression elseClause, When... whenClauses) { return new Case(caseValueExpr, Arrays.asList(whenClauses), elseClause); } @@ -281,19 +271,20 @@ public When when(UnresolvedExpression condition, UnresolvedExpression result) { return new When(condition, result); } - public UnresolvedExpression highlight(UnresolvedExpression fieldName, - java.util.Map arguments) { + public UnresolvedExpression highlight( + UnresolvedExpression fieldName, java.util.Map arguments) { return new HighlightFunction(fieldName, arguments); } - public UnresolvedExpression score(UnresolvedExpression relevanceQuery, - Literal relevanceFieldWeight) { + public UnresolvedExpression score( + UnresolvedExpression relevanceQuery, Literal relevanceFieldWeight) { return new ScoreFunction(relevanceQuery, relevanceFieldWeight); } - public UnresolvedExpression window(UnresolvedExpression function, - List partitionByList, - List> sortList) { + public UnresolvedExpression window( + UnresolvedExpression function, + List partitionByList, + List> sortList) { return new WindowFunction(function, partitionByList, sortList); } @@ -328,9 +319,10 @@ public static UnresolvedExpression compare( return new Compare(operator, left, right); } - public static UnresolvedExpression between(UnresolvedExpression value, - UnresolvedExpression lowerBound, - UnresolvedExpression upperBound) { + public static UnresolvedExpression between( + UnresolvedExpression value, + UnresolvedExpression lowerBound, + UnresolvedExpression upperBound) { return new Between(value, lowerBound, upperBound); } @@ -398,9 +390,7 @@ public static List defaultFieldsArgs() { return exprList(argument("exclude", booleanLiteral(false))); } - /** - * Default Stats Command Args. - */ + /** Default Stats Command Args. */ public static List defaultStatsArgs() { return exprList( argument("partitions", intLiteral(1)), @@ -409,9 +399,7 @@ public static List defaultStatsArgs() { argument("dedupsplit", booleanLiteral(false))); } - /** - * Default Dedup Command Args. - */ + /** Default Dedup Command Args. */ public static List defaultDedupArgs() { return exprList( argument("number", intLiteral(1)), @@ -447,9 +435,12 @@ public static List defaultTopArgs() { return exprList(argument("noOfResults", intLiteral(10))); } - public static RareTopN rareTopN(UnresolvedPlan input, CommandType commandType, - List noOfResults, List groupList, - Field... fields) { + public static RareTopN rareTopN( + UnresolvedPlan input, + CommandType commandType, + List noOfResults, + List groupList, + Field... fields) { return new RareTopN(input, commandType, noOfResults, Arrays.asList(fields), groupList) .attach(input); } @@ -458,11 +449,12 @@ public static Limit limit(UnresolvedPlan input, Integer limit, Integer offset) { return new Limit(limit, offset).attach(input); } - public static Parse parse(UnresolvedPlan input, ParseMethod parseMethod, - UnresolvedExpression sourceField, - Literal pattern, - java.util.Map arguments) { + public static Parse parse( + UnresolvedPlan input, + ParseMethod parseMethod, + UnresolvedExpression sourceField, + Literal pattern, + java.util.Map arguments) { return new Parse(parseMethod, sourceField, pattern, arguments, input); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/AggregateFunction.java b/core/src/main/java/org/opensearch/sql/ast/expression/AggregateFunction.java index e8f730d7e93..5208e39623d 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/AggregateFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/AggregateFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Collections; @@ -17,8 +16,8 @@ import org.opensearch.sql.common.utils.StringUtils; /** - * Expression node of aggregate functions. - * Params include aggregate function name (AVG, SUM, MAX etc.) and the field to aggregate. + * Expression node of aggregate functions. Params include aggregate function name (AVG, SUM, MAX + * etc.) and the field to aggregate. */ @Getter @EqualsAndHashCode(callSuper = false) @@ -27,13 +26,16 @@ public class AggregateFunction extends UnresolvedExpression { private final String funcName; private final UnresolvedExpression field; private final List argList; + @Setter @Accessors(fluent = true) private UnresolvedExpression condition; + private Boolean distinct = false; /** * Constructor. + * * @param funcName function name. * @param field {@link UnresolvedExpression}. */ @@ -45,6 +47,7 @@ public AggregateFunction(String funcName, UnresolvedExpression field) { /** * Constructor. + * * @param funcName function name. * @param field {@link UnresolvedExpression}. * @param distinct whether distinct field is specified or not. diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Alias.java b/core/src/main/java/org/opensearch/sql/ast/expression/Alias.java index 4183b19a3e1..7b3078629b9 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Alias.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Alias.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import lombok.AllArgsConstructor; @@ -14,10 +13,10 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** - * Alias abstraction that associate an unnamed expression with a name and an optional alias. - * The name and alias information preserved is useful for semantic analysis and response - * formatting eventually. This can avoid restoring the info in toString() method which is - * inaccurate because original info is already lost. + * Alias abstraction that associate an unnamed expression with a name and an optional alias. The + * name and alias information preserved is useful for semantic analysis and response formatting + * eventually. This can avoid restoring the info in toString() method which is inaccurate because + * original info is already lost. */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @@ -26,19 +25,13 @@ @ToString public class Alias extends UnresolvedExpression { - /** - * Original field name. - */ + /** Original field name. */ private final String name; - /** - * Expression aliased. - */ + /** Expression aliased. */ private final UnresolvedExpression delegated; - /** - * Optional field alias. - */ + /** Optional field alias. */ private String alias; @Override diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/AllFields.java b/core/src/main/java/org/opensearch/sql/ast/expression/AllFields.java index 1f5d9198173..b9b90ea24a8 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/AllFields.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/AllFields.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Collections; @@ -13,16 +12,13 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * Represent the All fields which is been used in SELECT *. - */ +/** Represent the All fields which is been used in SELECT *. */ @ToString @EqualsAndHashCode(callSuper = false) public class AllFields extends UnresolvedExpression { public static final AllFields INSTANCE = new AllFields(); - private AllFields() { - } + private AllFields() {} public static AllFields of() { return INSTANCE; diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/And.java b/core/src/main/java/org/opensearch/sql/ast/expression/And.java index 8d8c48f3b23..565f1eb4abe 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/And.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/And.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of logic AND. - */ +/** Expression node of logic AND. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Argument.java b/core/src/main/java/org/opensearch/sql/ast/expression/Argument.java index f054710a32b..4c2a485ea77 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Argument.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Argument.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Argument. - */ +/** Argument. */ @Getter @ToString @RequiredArgsConstructor diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/AttributeList.java b/core/src/main/java/org/opensearch/sql/ast/expression/AttributeList.java index 7e1fdb15161..d137a83957b 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/AttributeList.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/AttributeList.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -14,15 +13,12 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node that includes a list of Expression nodes. - */ +/** Expression node that includes a list of Expression nodes. */ @ToString @EqualsAndHashCode(callSuper = false) @AllArgsConstructor public class AttributeList extends UnresolvedExpression { - @Getter - private List attrList; + @Getter private List attrList; @Override public List getChild() { diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Between.java b/core/src/main/java/org/opensearch/sql/ast/expression/Between.java index 886c9a92824..e13c3fb187c 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Between.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Between.java @@ -12,9 +12,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * Unresolved expression for BETWEEN. - */ +/** Unresolved expression for BETWEEN. */ @Data @EqualsAndHashCode(callSuper = false) public class Between extends UnresolvedExpression { diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Case.java b/core/src/main/java/org/opensearch/sql/ast/expression/Case.java index 81c74f3ea49..583bd0de972 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Case.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Case.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -15,29 +14,23 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * AST node that represents CASE clause similar as Switch statement in programming language. - */ +/** AST node that represents CASE clause similar as Switch statement in programming language. */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @Getter @ToString public class Case extends UnresolvedExpression { - /** - * Value to be compared by WHEN statements. Null in the case of CASE WHEN conditions. - */ + /** Value to be compared by WHEN statements. Null in the case of CASE WHEN conditions. */ private final UnresolvedExpression caseValue; /** - * Expression list that represents WHEN statements. Each is a mapping from condition - * to its result. + * Expression list that represents WHEN statements. Each is a mapping from condition to its + * result. */ private final List whenClauses; - /** - * Expression that represents ELSE statement result. - */ + /** Expression that represents ELSE statement result. */ private final UnresolvedExpression elseClause; @Override @@ -58,5 +51,4 @@ public List getChild() { public T accept(AbstractNodeVisitor nodeVisitor, C context) { return nodeVisitor.visitCase(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Cast.java b/core/src/main/java/org/opensearch/sql/ast/expression/Cast.java index 9121dbd87c3..2019346fb52 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Cast.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Cast.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import static org.opensearch.sql.expression.function.BuiltinFunctionName.CAST_TO_BOOLEAN; @@ -33,9 +32,7 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.function.FunctionName; -/** - * AST node that represents Cast clause. - */ +/** AST node that represents Cast clause. */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @Getter @@ -59,19 +56,16 @@ public class Cast extends UnresolvedExpression { .put("datetime", CAST_TO_DATETIME.getName()) .build(); - /** - * The source expression cast from. - */ + /** The source expression cast from. */ private final UnresolvedExpression expression; - /** - * Expression that represents name of the target type. - */ + /** Expression that represents name of the target type. */ private final UnresolvedExpression convertedType; /** * Check if the given function name is a cast function or not. - * @param name function name + * + * @param name function name * @return true if cast function, otherwise false. */ public static boolean isCastFunction(FunctionName name) { @@ -80,7 +74,8 @@ public static boolean isCastFunction(FunctionName name) { /** * Get the cast function name for a given target data type. - * @param targetType target data type + * + * @param targetType target data type * @return cast function name corresponding */ public static FunctionName getCastFunctionName(ExprType targetType) { diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Compare.java b/core/src/main/java/org/opensearch/sql/ast/expression/Compare.java index 25cf3e0f732..8ba6ba5116c 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Compare.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Compare.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/DataType.java b/core/src/main/java/org/opensearch/sql/ast/expression/DataType.java index 8755a15177f..0b9add8f55f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/DataType.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/DataType.java @@ -3,17 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.opensearch.sql.data.type.ExprCoreType; -/** - * The DataType defintion in AST. - * Question, could we use {@link ExprCoreType} directly in AST? - */ +/** The DataType defintion in AST. Question, could we use {@link ExprCoreType} directly in AST? */ @RequiredArgsConstructor public enum DataType { TYPE_ERROR(ExprCoreType.UNKNOWN), @@ -32,6 +28,5 @@ public enum DataType { TIMESTAMP(ExprCoreType.TIMESTAMP), INTERVAL(ExprCoreType.INTERVAL); - @Getter - private final ExprCoreType coreType; + @Getter private final ExprCoreType coreType; } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/EqualTo.java b/core/src/main/java/org/opensearch/sql/ast/expression/EqualTo.java index 806f897abfc..344f3c5164a 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/EqualTo.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/EqualTo.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,17 +13,13 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of binary operator or comparison relation EQUAL. - */ +/** Expression node of binary operator or comparison relation EQUAL. */ @ToString @EqualsAndHashCode(callSuper = false) @AllArgsConstructor public class EqualTo extends UnresolvedExpression { - @Getter - private UnresolvedExpression left; - @Getter - private UnresolvedExpression right; + @Getter private UnresolvedExpression left; + @Getter private UnresolvedExpression right; @Override public List getChild() { diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Field.java b/core/src/main/java/org/opensearch/sql/ast/expression/Field.java index 9a8109fbe35..0a2d726ad41 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Field.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Field.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -23,16 +22,12 @@ public class Field extends UnresolvedExpression { private final List fieldArgs; - /** - * Constructor of Field. - */ + /** Constructor of Field. */ public Field(UnresolvedExpression field) { this(field, Collections.emptyList()); } - /** - * Constructor of Field. - */ + /** Constructor of Field. */ public Field(UnresolvedExpression field, List fieldArgs) { this.field = field; this.fieldArgs = fieldArgs; diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Function.java b/core/src/main/java/org/opensearch/sql/ast/expression/Function.java index c712d860f40..184342c5e3f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Function.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Function.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Collections; @@ -14,10 +13,9 @@ import lombok.RequiredArgsConstructor; import org.opensearch.sql.ast.AbstractNodeVisitor; - /** - * Expression node of scalar function. - * Params include function name (@funcName) and function arguments (@funcArgs) + * Expression node of scalar function. Params include function name (@funcName) and function + * arguments (@funcArgs) */ @Getter @EqualsAndHashCode(callSuper = false) @@ -38,9 +36,8 @@ public R accept(AbstractNodeVisitor nodeVisitor, C context) { @Override public String toString() { - return String.format("%s(%s)", funcName, - funcArgs.stream() - .map(Object::toString) - .collect(Collectors.joining(", "))); + return String.format( + "%s(%s)", + funcName, funcArgs.stream().map(Object::toString).collect(Collectors.joining(", "))); } } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/HighlightFunction.java b/core/src/main/java/org/opensearch/sql/ast/expression/HighlightFunction.java index 0d4e57a78ce..128d9327e8a 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/HighlightFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/HighlightFunction.java @@ -13,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of Highlight function. - */ +/** Expression node of Highlight function. */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @Getter diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/In.java b/core/src/main/java/org/opensearch/sql/ast/expression/In.java index 9ce1c124cb4..38c1b91b43c 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/In.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/In.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -15,10 +14,9 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** - * Expression node of one-to-many mapping relation IN. - * Params include the field expression and/or wildcard field expression, - * nested field expression (@field). - * And the values that the field is mapped to (@valueList). + * Expression node of one-to-many mapping relation IN. Params include the field expression and/or + * wildcard field expression, nested field expression (@field). And the values that the field is + * mapped to (@valueList). */ @Getter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Interval.java b/core/src/main/java/org/opensearch/sql/ast/expression/Interval.java index 84b6ba02d14..c26f829f486 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Interval.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Interval.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Collections; diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/IntervalUnit.java b/core/src/main/java/org/opensearch/sql/ast/expression/IntervalUnit.java index 2a86c89cf63..19e1b07e39b 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/IntervalUnit.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/IntervalUnit.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -13,7 +12,7 @@ @Getter @RequiredArgsConstructor -public enum IntervalUnit { +public enum IntervalUnit { UNKNOWN, MICROSECOND, @@ -44,9 +43,7 @@ public enum IntervalUnit { INTERVAL_UNITS = builder.add(IntervalUnit.values()).build(); } - /** - * Util method to get interval unit given the unit name. - */ + /** Util method to get interval unit given the unit name. */ public static IntervalUnit of(String unit) { return INTERVAL_UNITS.stream() .filter(v -> unit.equalsIgnoreCase(v.name())) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Let.java b/core/src/main/java/org/opensearch/sql/ast/expression/Let.java index cea2a091e58..2f63a25f106 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Let.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Let.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Represent the assign operation. e.g. velocity = distance/speed. - */ +/** Represent the assign operation. e.g. velocity = distance/speed. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Literal.java b/core/src/main/java/org/opensearch/sql/ast/expression/Literal.java index 3ff360dbf04..80c877dd976 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Literal.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Literal.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -14,9 +13,8 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** - * Expression node of literal type - * Params include literal value (@value) and - * literal data type (@type) which can be selected from {@link DataType}. + * Expression node of literal type Params include literal value (@value) and literal data type + * (@type) which can be selected from {@link DataType}. */ @Getter @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Map.java b/core/src/main/java/org/opensearch/sql/ast/expression/Map.java index 45e98f127f5..73d8d77261d 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Map.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Map.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of one-to-one mapping relation. - */ +/** Expression node of one-to-one mapping relation. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/NestedAllTupleFields.java b/core/src/main/java/org/opensearch/sql/ast/expression/NestedAllTupleFields.java index adf2025e6ca..be89dd9f191 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/NestedAllTupleFields.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/NestedAllTupleFields.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Collections; @@ -11,18 +10,14 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * Represents all tuple fields used in nested function. - */ +/** Represents all tuple fields used in nested function. */ @RequiredArgsConstructor @EqualsAndHashCode(callSuper = false) public class NestedAllTupleFields extends UnresolvedExpression { - @Getter - private final String path; + @Getter private final String path; @Override public List getChild() { diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Not.java b/core/src/main/java/org/opensearch/sql/ast/expression/Not.java index 2926c7e5cd0..423cb088efe 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Not.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Not.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of the logic NOT. - */ +/** Expression node of the logic NOT. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Or.java b/core/src/main/java/org/opensearch/sql/ast/expression/Or.java index b0dabb6e4ef..cc59170d319 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Or.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Or.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of the logic OR. - */ +/** Expression node of the logic OR. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/ParseMethod.java b/core/src/main/java/org/opensearch/sql/ast/expression/ParseMethod.java index 83a46323e6d..7a2587c5f04 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/ParseMethod.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/ParseMethod.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import lombok.Getter; @@ -15,6 +14,5 @@ public enum ParseMethod { GROK("grok"), PATTERNS("patterns"); - @Getter - private final String name; + @Getter private final String name; } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java b/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java index 8b16119dc0e..7e5e7adf820 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import static java.util.Objects.requireNonNull; @@ -29,9 +28,7 @@ public QualifiedName(String name) { this.parts = Collections.singletonList(name); } - /** - * QualifiedName Constructor. - */ + /** QualifiedName Constructor. */ public QualifiedName(Iterable parts) { List partsList = StreamSupport.stream(parts.spliterator(), false).collect(toList()); if (partsList.isEmpty()) { @@ -40,9 +37,7 @@ public QualifiedName(Iterable parts) { this.parts = partsList; } - /** - * Construct {@link QualifiedName} from list of string. - */ + /** Construct {@link QualifiedName} from list of string. */ public static QualifiedName of(String first, String... rest) { requireNonNull(first); ArrayList parts = new ArrayList<>(); @@ -55,9 +50,7 @@ public static QualifiedName of(Iterable parts) { return new QualifiedName(parts); } - /** - * Get Prefix of {@link QualifiedName}. - */ + /** Get Prefix of {@link QualifiedName}. */ public Optional getPrefix() { if (parts.size() == 1) { return Optional.empty(); @@ -71,7 +64,8 @@ public String getSuffix() { /** * Get first part of the qualified name. - * @return first part + * + * @return first part */ public Optional first() { if (parts.size() == 1) { @@ -81,17 +75,12 @@ public Optional first() { } /** - * Get rest parts of the qualified name. Assume that there must be remaining parts - * so caller is responsible for the check (first() or size() must be called first). - * For example: - * {@code - * QualifiedName name = ... - * Optional first = name.first(); - * if (first.isPresent()) { - * name.rest() ... - * } - * } - * @return rest part(s) + * Get rest parts of the qualified name. Assume that there must be remaining parts so caller is + * responsible for the check (first() or size() must be called first). For example: {@code + * QualifiedName name = ... Optional first = name.first(); if (first.isPresent()) { + * name.rest() ... } } + * + * @return rest part(s) */ public QualifiedName rest() { return QualifiedName.of(parts.subList(1, parts.size())); diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java b/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java index 3166fe45c3c..a9e26a611fc 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.List; @@ -13,14 +12,11 @@ import lombok.Getter; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node that includes a list of RelevanceField nodes. - */ +/** Expression node that includes a list of RelevanceField nodes. */ @EqualsAndHashCode(callSuper = false) @AllArgsConstructor public class RelevanceFieldList extends UnresolvedExpression { - @Getter - private java.util.Map fieldList; + @Getter private java.util.Map fieldList; @Override public List getChild() { @@ -34,9 +30,7 @@ public R accept(AbstractNodeVisitor nodeVisitor, C context) { @Override public String toString() { - return fieldList - .entrySet() - .stream() + return fieldList.entrySet().stream() .map(e -> String.format("\"%s\" ^ %s", e.getKey(), e.getValue())) .collect(Collectors.joining(", ")); } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/ScoreFunction.java b/core/src/main/java/org/opensearch/sql/ast/expression/ScoreFunction.java index 1b73f9bd951..26d784dd8ca 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/ScoreFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/ScoreFunction.java @@ -13,8 +13,8 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** - * Expression node of Score function. - * Score takes a relevance-search expression as an argument and returns it + * Expression node of Score function. Score takes a relevance-search expression as an argument and + * returns it */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Span.java b/core/src/main/java/org/opensearch/sql/ast/expression/Span.java index e57205c19c2..edd309b22d8 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Span.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Span.java @@ -13,10 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Span expression node. - * Params include field expression and the span value. - */ +/** Span expression node. Params include field expression and the span value. */ @Getter @EqualsAndHashCode(callSuper = false) @RequiredArgsConstructor @@ -35,5 +32,4 @@ public List getChild() { public R accept(AbstractNodeVisitor nodeVisitor, C context) { return nodeVisitor.visitSpan(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/SpanUnit.java b/core/src/main/java/org/opensearch/sql/ast/expression/SpanUnit.java index f1f3fb19d4f..5252781edbf 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/SpanUnit.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/SpanUnit.java @@ -42,9 +42,7 @@ public enum SpanUnit { SPAN_UNITS = builder.add(SpanUnit.values()).build(); } - /** - * Util method to get span unit given the unit name. - */ + /** Util method to get span unit given the unit name. */ public static SpanUnit of(String unit) { switch (unit) { case "": @@ -64,5 +62,4 @@ public static SpanUnit of(String unit) { public static String getName(SpanUnit unit) { return unit.name; } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedArgument.java b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedArgument.java index e4def038ed0..2c6eee46e9c 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedArgument.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedArgument.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -13,9 +12,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Argument. - */ +/** Argument. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedAttribute.java b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedAttribute.java index f4bc88853f0..e1754cbf76f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedAttribute.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedAttribute.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -15,9 +14,8 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** - * Expression node, representing the syntax that is not resolved to - * any other expression nodes yet but non-negligible - * This expression is often created as the index name, field name etc. + * Expression node, representing the syntax that is not resolved to any other expression nodes yet + * but non-negligible This expression is often created as the index name, field name etc. */ @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedExpression.java b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedExpression.java index ee3922f797e..9b2d530b7ff 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedExpression.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/UnresolvedExpression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import lombok.EqualsAndHashCode; diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/When.java b/core/src/main/java/org/opensearch/sql/ast/expression/When.java index a52870b4083..db747592e50 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/When.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/When.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -15,23 +14,17 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * AST node that represents WHEN clause. - */ +/** AST node that represents WHEN clause. */ @EqualsAndHashCode(callSuper = false) @Getter @RequiredArgsConstructor @ToString public class When extends UnresolvedExpression { - /** - * WHEN condition, either a search condition or compare value if case value present. - */ + /** WHEN condition, either a search condition or compare value if case value present. */ private final UnresolvedExpression condition; - /** - * Result to return if condition matched. - */ + /** Result to return if condition matched. */ private final UnresolvedExpression result; @Override @@ -43,5 +36,4 @@ public List getChild() { public T accept(AbstractNodeVisitor nodeVisitor, C context) { return nodeVisitor.visitWhen(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/WindowFunction.java b/core/src/main/java/org/opensearch/sql/ast/expression/WindowFunction.java index 9a7535e1fe4..47f52657659 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/WindowFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/WindowFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import com.google.common.collect.ImmutableList; @@ -42,5 +41,4 @@ public List getChild() { public T accept(AbstractNodeVisitor nodeVisitor, C context) { return nodeVisitor.visitWindowFunction(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/Xor.java b/core/src/main/java/org/opensearch/sql/ast/expression/Xor.java index 731feccd339..40db015fd73 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/Xor.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/Xor.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import java.util.Arrays; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Expression node of the logic XOR. - */ +/** Expression node of the logic XOR. */ @Getter @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/ast/statement/Explain.java b/core/src/main/java/org/opensearch/sql/ast/statement/Explain.java index 1e364d69fdc..5ee260c1306 100644 --- a/core/src/main/java/org/opensearch/sql/ast/statement/Explain.java +++ b/core/src/main/java/org/opensearch/sql/ast/statement/Explain.java @@ -12,9 +12,7 @@ import lombok.EqualsAndHashCode; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Explain Statement. - */ +/** Explain Statement. */ @Data @EqualsAndHashCode(callSuper = false) public class Explain extends Statement { diff --git a/core/src/main/java/org/opensearch/sql/ast/statement/Query.java b/core/src/main/java/org/opensearch/sql/ast/statement/Query.java index 82efdde4ddc..6366451b72b 100644 --- a/core/src/main/java/org/opensearch/sql/ast/statement/Query.java +++ b/core/src/main/java/org/opensearch/sql/ast/statement/Query.java @@ -16,9 +16,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.tree.UnresolvedPlan; -/** - * Query Statement. - */ +/** Query Statement. */ @Getter @Setter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/statement/Statement.java b/core/src/main/java/org/opensearch/sql/ast/statement/Statement.java index e32a8dbfd8a..d90071a0ca6 100644 --- a/core/src/main/java/org/opensearch/sql/ast/statement/Statement.java +++ b/core/src/main/java/org/opensearch/sql/ast/statement/Statement.java @@ -11,9 +11,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * Statement is the high interface of core engine. - */ +/** Statement is the high interface of core engine. */ public abstract class Statement extends Node { @Override public R accept(AbstractNodeVisitor visitor, C context) { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/AD.java b/core/src/main/java/org/opensearch/sql/ast/tree/AD.java index e9aee25c230..0e1cc33db99 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/AD.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/AD.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Aggregation.java b/core/src/main/java/org/opensearch/sql/ast/tree/Aggregation.java index e9fa26e9818..f098d0ec537 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Aggregation.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Aggregation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -17,9 +16,7 @@ import org.opensearch.sql.ast.expression.Argument; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * Logical plan node of Aggregation, the interface for building aggregation actions in queries. - */ +/** Logical plan node of Aggregation, the interface for building aggregation actions in queries. */ @Getter @Setter @ToString @@ -32,23 +29,21 @@ public class Aggregation extends UnresolvedPlan { private List argExprList; private UnresolvedPlan child; - /** - * Aggregation Constructor without span and argument. - */ - public Aggregation(List aggExprList, - List sortExprList, - List groupExprList) { + /** Aggregation Constructor without span and argument. */ + public Aggregation( + List aggExprList, + List sortExprList, + List groupExprList) { this(aggExprList, sortExprList, groupExprList, null, Collections.emptyList()); } - /** - * Aggregation Constructor. - */ - public Aggregation(List aggExprList, - List sortExprList, - List groupExprList, - UnresolvedExpression span, - List argExprList) { + /** Aggregation Constructor. */ + public Aggregation( + List aggExprList, + List sortExprList, + List groupExprList, + UnresolvedExpression span, + List argExprList) { this.aggExprList = aggExprList; this.sortExprList = sortExprList; this.groupExprList = groupExprList; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/CloseCursor.java b/core/src/main/java/org/opensearch/sql/ast/tree/CloseCursor.java index cf82c2b0709..832b79d34a6 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/CloseCursor.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/CloseCursor.java @@ -9,15 +9,10 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * AST node to represent close cursor operation. - * Actually a wrapper to the AST. - */ +/** AST node to represent close cursor operation. Actually a wrapper to the AST. */ public class CloseCursor extends UnresolvedPlan { - /** - * An instance of {@link FetchCursor}. - */ + /** An instance of {@link FetchCursor}. */ private UnresolvedPlan cursor; @Override diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Dedupe.java b/core/src/main/java/org/opensearch/sql/ast/tree/Dedupe.java index 6514d65a04b..3173f42f9c3 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Dedupe.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Dedupe.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -18,9 +17,7 @@ import org.opensearch.sql.ast.expression.Argument; import org.opensearch.sql.ast.expression.Field; -/** - * AST node represent Dedupe operation. - */ +/** AST node represent Dedupe operation. */ @Getter @Setter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Eval.java b/core/src/main/java/org/opensearch/sql/ast/tree/Eval.java index 184f2ac4971..ecceabd7573 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Eval.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Eval.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -16,9 +15,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.expression.Let; -/** - * AST node represent Eval operation. - */ +/** AST node represent Eval operation. */ @Getter @Setter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java b/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java index aa327c295b0..e34127c9d35 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java @@ -10,15 +10,11 @@ import lombok.RequiredArgsConstructor; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * An unresolved plan that represents fetching the next - * batch in paginationed plan. - */ +/** An unresolved plan that represents fetching the next batch in paginationed plan. */ @RequiredArgsConstructor @EqualsAndHashCode(callSuper = false) public class FetchCursor extends UnresolvedPlan { - @Getter - final String cursor; + @Getter final String cursor; @Override public T accept(AbstractNodeVisitor nodeVisitor, C context) { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Filter.java b/core/src/main/java/org/opensearch/sql/ast/tree/Filter.java index 81280789300..6c57275db9f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Filter.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Filter.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -14,9 +13,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * Logical plan node of Filter, the interface for building filters in queries. - */ +/** Logical plan node of Filter, the interface for building filters in queries. */ @ToString @EqualsAndHashCode(callSuper = false) @Getter diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Head.java b/core/src/main/java/org/opensearch/sql/ast/tree/Head.java index 8590c212ad7..bf6b2caed13 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Head.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Head.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -16,9 +15,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * AST node represent Head operation. - */ +/** AST node represent Head operation. */ @Getter @Setter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Kmeans.java b/core/src/main/java/org/opensearch/sql/ast/tree/Kmeans.java index 5d2e32c28bb..ed03efed8af 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Kmeans.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Kmeans.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Limit.java b/core/src/main/java/org/opensearch/sql/ast/tree/Limit.java index f9b8a8feabb..9290776172d 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Limit.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Limit.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -38,5 +37,4 @@ public List getChild() { public T accept(AbstractNodeVisitor visitor, C context) { return visitor.visitLimit(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/ML.java b/core/src/main/java/org/opensearch/sql/ast/tree/ML.java index 2f83a993b7b..320fb2bcf79 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/ML.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/ML.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import static org.opensearch.sql.utils.MLCommonsConstants.ACTION; @@ -66,7 +65,7 @@ public List getChild() { } private String getAction() { - return (String) arguments.get(ACTION).getValue(); + return (String) arguments.get(ACTION).getValue(); } /** @@ -85,7 +84,7 @@ public Map getOutputSchema(TypeEnvironment env) { return getPredictOutputSchema(); default: throw new IllegalArgumentException( - "Action error. Please indicate train, predict or trainandpredict."); + "Action error. Please indicate train, predict or trainandpredict."); } } @@ -122,8 +121,8 @@ public Map getPredictOutputSchema() { * @return the schema */ public Map getTrainOutputSchema() { - boolean isAsync = arguments.containsKey(ASYNC) - ? (boolean) arguments.get(ASYNC).getValue() : false; + boolean isAsync = + arguments.containsKey(ASYNC) ? (boolean) arguments.get(ASYNC).getValue() : false; Map res = new HashMap<>(Map.of(STATUS, ExprCoreType.STRING)); if (isAsync) { res.put(TASKID, ExprCoreType.STRING); diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Paginate.java b/core/src/main/java/org/opensearch/sql/ast/tree/Paginate.java index 55e0e8c7a6c..69b11600c96 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Paginate.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Paginate.java @@ -13,16 +13,12 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * AST node to represent pagination operation. - * Actually a wrapper to the AST. - */ +/** AST node to represent pagination operation. Actually a wrapper to the AST. */ @RequiredArgsConstructor @EqualsAndHashCode(callSuper = false) @ToString public class Paginate extends UnresolvedPlan { - @Getter - private final int pageSize; + @Getter private final int pageSize; private UnresolvedPlan child; public Paginate(int pageSize, UnresolvedPlan child) { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Parse.java b/core/src/main/java/org/opensearch/sql/ast/tree/Parse.java index 02a69c93af1..e91c4a68ffb 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Parse.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Parse.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -20,9 +19,7 @@ import org.opensearch.sql.ast.expression.ParseMethod; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * AST node represent Parse with regex operation. - */ +/** AST node represent Parse with regex operation. */ @Getter @Setter @ToString @@ -30,29 +27,19 @@ @RequiredArgsConstructor @AllArgsConstructor public class Parse extends UnresolvedPlan { - /** - * Method used to parse a field. - */ + /** Method used to parse a field. */ private final ParseMethod parseMethod; - /** - * Field. - */ + /** Field. */ private final UnresolvedExpression sourceField; - /** - * Pattern. - */ + /** Pattern. */ private final Literal pattern; - /** - * Optional arguments. - */ + /** Optional arguments. */ private final Map arguments; - /** - * Child Plan. - */ + /** Child Plan. */ private UnresolvedPlan child; @Override diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Project.java b/core/src/main/java/org/opensearch/sql/ast/tree/Project.java index 33c71288550..cffb4dfdce2 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Project.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Project.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -17,15 +16,12 @@ import org.opensearch.sql.ast.expression.Argument; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * Logical plan node of Project, the interface for building the list of searching fields. - */ +/** Logical plan node of Project, the interface for building the list of searching fields. */ @ToString @Getter @EqualsAndHashCode(callSuper = false) public class Project extends UnresolvedPlan { - @Setter - private List projectList; + @Setter private List projectList; private List argExprList; private UnresolvedPlan child; @@ -43,9 +39,7 @@ public boolean hasArgument() { return !argExprList.isEmpty(); } - /** - * The Project could been used to exclude fields from the source. - */ + /** The Project could been used to exclude fields from the source. */ public boolean isExcluded() { if (hasArgument()) { Argument argument = argExprList.get(0); diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/RareTopN.java b/core/src/main/java/org/opensearch/sql/ast/tree/RareTopN.java index 407d37e8e45..2cbe170541e 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/RareTopN.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/RareTopN.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import java.util.Collections; @@ -19,9 +18,7 @@ import org.opensearch.sql.ast.expression.Field; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * AST node represent RareTopN operation. - */ +/** AST node represent RareTopN operation. */ @Getter @Setter @ToString diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Relation.java b/core/src/main/java/org/opensearch/sql/ast/tree/Relation.java index 8c3868329ff..ec5264a86b7 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Relation.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Relation.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -18,9 +17,7 @@ import org.opensearch.sql.ast.expression.QualifiedName; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * Logical plan node of Relation, the interface for building the searching sources. - */ +/** Logical plan node of Relation, the interface for building the searching sources. */ @AllArgsConstructor @ToString @EqualsAndHashCode(callSuper = false) @@ -39,9 +36,7 @@ public Relation(UnresolvedExpression tableName, String alias) { this.alias = alias; } - /** - * Optional alias name for the relation. - */ + /** Optional alias name for the relation. */ private String alias; /** @@ -72,9 +67,9 @@ public String getAlias() { } /** - * Get Qualified name preservs parts of the user given identifiers. - * This can later be utilized to determine DataSource,Schema and Table Name during - * Analyzer stage. So Passing QualifiedName directly to Analyzer Stage. + * Get Qualified name preservs parts of the user given identifiers. This can later be utilized to + * determine DataSource,Schema and Table Name during Analyzer stage. So Passing QualifiedName + * directly to Analyzer Stage. * * @return TableQualifiedName. */ @@ -82,9 +77,10 @@ public QualifiedName getTableQualifiedName() { if (tableName.size() == 1) { return (QualifiedName) tableName.get(0); } else { - return new QualifiedName(tableName.stream() - .map(UnresolvedExpression::toString) - .collect(Collectors.joining(COMMA))); + return new QualifiedName( + tableName.stream() + .map(UnresolvedExpression::toString) + .collect(Collectors.joining(COMMA))); } } diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/RelationSubquery.java b/core/src/main/java/org/opensearch/sql/ast/tree/RelationSubquery.java index 89122bea7f6..5ab4d71dd9f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/RelationSubquery.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/RelationSubquery.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -14,9 +13,7 @@ import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -/** - * Logical plan node of RelationSubquery. - */ +/** Logical plan node of RelationSubquery. */ @AllArgsConstructor @EqualsAndHashCode(callSuper = false) @RequiredArgsConstructor @@ -25,9 +22,7 @@ public class RelationSubquery extends UnresolvedPlan { private UnresolvedPlan query; private String alias; - /** - * Take subquery alias as table name. - */ + /** Take subquery alias as table name. */ public String getAliasAsTableName() { return alias; } diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Rename.java b/core/src/main/java/org/opensearch/sql/ast/tree/Rename.java index 69700c871c4..e6f760aca00 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Rename.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Rename.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Sort.java b/core/src/main/java/org/opensearch/sql/ast/tree/Sort.java index 5fb4139bea6..073cb7aa1b4 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Sort.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Sort.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import static org.opensearch.sql.ast.tree.Sort.NullOrder.NULL_FIRST; @@ -22,9 +21,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.expression.Field; -/** - * AST node for Sort {@link Sort#sortList} represent a list of sort expression and sort options. - */ +/** AST node for Sort {@link Sort#sortList} represent a list of sort expression and sort options. */ @ToString @EqualsAndHashCode(callSuper = false) @Getter @@ -50,19 +47,14 @@ public T accept(AbstractNodeVisitor nodeVisitor, C context) { return nodeVisitor.visitSort(this, context); } - /** - * Sort Options. - */ + /** Sort Options. */ @Data public static class SortOption { - /** - * Default ascending sort option, null first. - */ + /** Default ascending sort option, null first. */ public static SortOption DEFAULT_ASC = new SortOption(ASC, NULL_FIRST); - /** - * Default descending sort option, null last. - */ + + /** Default descending sort option, null last. */ public static SortOption DEFAULT_DESC = new SortOption(DESC, NULL_LAST); private final SortOrder sortOrder; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java b/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java index 064cbf24fec..711a8a0ec5f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -11,16 +10,12 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Setter; import lombok.ToString; import org.opensearch.sql.ast.AbstractNodeVisitor; -import org.opensearch.sql.ast.expression.Let; import org.opensearch.sql.ast.expression.QualifiedName; import org.opensearch.sql.ast.expression.UnresolvedExpression; -/** - * ASTNode for Table Function. - */ +/** ASTNode for Table Function. */ @ToString @EqualsAndHashCode(callSuper = false) @RequiredArgsConstructor @@ -28,8 +23,7 @@ public class TableFunction extends UnresolvedPlan { private final UnresolvedExpression functionName; - @Getter - private final List arguments; + @Getter private final List arguments; public QualifiedName getFunctionName() { return (QualifiedName) functionName; diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/UnresolvedPlan.java b/core/src/main/java/org/opensearch/sql/ast/tree/UnresolvedPlan.java index 672a4602ede..3074303b0d7 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/UnresolvedPlan.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/UnresolvedPlan.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import lombok.EqualsAndHashCode; @@ -11,9 +10,7 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; import org.opensearch.sql.ast.Node; -/** - * Abstract unresolved plan. - */ +/** Abstract unresolved plan. */ @EqualsAndHashCode(callSuper = false) @ToString public abstract class UnresolvedPlan extends Node { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/Values.java b/core/src/main/java/org/opensearch/sql/ast/tree/Values.java index 5a662912f9e..65d7e8d7cb2 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/Values.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/Values.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.tree; import com.google.common.collect.ImmutableList; @@ -16,9 +15,7 @@ import org.opensearch.sql.ast.Node; import org.opensearch.sql.ast.expression.Literal; -/** - * AST node class for a sequence of literal values. - */ +/** AST node class for a sequence of literal values. */ @ToString @Getter @EqualsAndHashCode(callSuper = false) @@ -41,5 +38,4 @@ public T accept(AbstractNodeVisitor nodeVisitor, C context) { public List getChild() { return ImmutableList.of(); } - } diff --git a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprNumberValue.java b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprNumberValue.java index 1f6363c0680..48781df8471 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprNumberValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprNumberValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import com.google.common.base.Objects; import lombok.RequiredArgsConstructor; -/** - * Expression Number Value. - */ +/** Expression Number Value. */ @RequiredArgsConstructor public abstract class AbstractExprNumberValue extends AbstractExprValue { private final Number value; diff --git a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java index ad2c2ddb494..bed7fcfddf5 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java @@ -3,23 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.exception.ExpressionEvaluationException; -/** - * Abstract ExprValue. - */ +/** Abstract ExprValue. */ public abstract class AbstractExprValue implements ExprValue { - /** - * The customize compareTo logic. - */ + /** The customize compareTo logic. */ @Override public int compareTo(ExprValue other) { if (this.isNull() || this.isMissing() || other.isNull() || other.isMissing()) { throw new IllegalStateException( - "[BUG] Unreachable, Comparing with NULL or MISSING is undefined"); + "[BUG] Unreachable, Comparing with NULL or MISSING is undefined"); } if ((this.isNumber() && other.isNumber()) || (this.isDateTime() && other.isDateTime()) @@ -34,13 +29,8 @@ public int compareTo(ExprValue other) { } /** - * The customize equals logic. - * The table below list the NULL and MISSING handling logic. - * A B A == B - * NULL NULL TRUE - * NULL MISSING FALSE - * MISSING NULL FALSE - * MISSING MISSING TRUE + * The customize equals logic. The table below list the NULL and MISSING handling logic. A B A == + * B NULL NULL TRUE NULL MISSING FALSE MISSING NULL FALSE MISSING MISSING TRUE */ @Override public boolean equals(Object o) { @@ -59,13 +49,9 @@ public boolean equals(Object o) { } } - /** - * The expression value compare. - */ + /** The expression value compare. */ public abstract int compare(ExprValue other); - /** - * The expression value equal. - */ + /** The expression value equal. */ public abstract boolean equal(ExprValue other); } diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprBooleanValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprBooleanValue.java index d655c0dabbb..ee97cf22286 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprBooleanValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprBooleanValue.java @@ -3,16 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import com.google.common.base.Objects; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Boolean Value. - */ +/** Expression Boolean Value. */ public class ExprBooleanValue extends AbstractExprValue { private static final ExprBooleanValue TRUE = new ExprBooleanValue(true); private static final ExprBooleanValue FALSE = new ExprBooleanValue(false); diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprByteValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprByteValue.java index b39e6e9d7fe..8e74a83cf36 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprByteValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprByteValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Byte Value. - */ +/** Expression Byte Value. */ public class ExprByteValue extends AbstractExprNumberValue { public ExprByteValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprCollectionValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprCollectionValue.java index 1326733263b..d1f56c3d76c 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprCollectionValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprCollectionValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import com.google.common.base.Objects; @@ -15,9 +14,7 @@ import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Collection Value. - */ +/** Expression Collection Value. */ @RequiredArgsConstructor public class ExprCollectionValue extends AbstractExprValue { private final List valueList; @@ -43,9 +40,7 @@ public List collectionValue() { @Override public String toString() { - return valueList.stream() - .map(Object::toString) - .collect(Collectors.joining(", ", "[", "]")); + return valueList.stream().map(Object::toString).collect(Collectors.joining(", ", "[", "]")); } @Override @@ -68,9 +63,7 @@ public boolean equal(ExprValue o) { } } - /** - * Only compare the size of the list. - */ + /** Only compare the size of the list. */ @Override public int compare(ExprValue other) { return Integer.compare(valueList.size(), other.collectionValue().size()); diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprDateValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprDateValue.java index 57ce87df475..d1f6f83815c 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprDateValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprDateValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL; @@ -22,23 +21,19 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; -/** - * Expression Date Value. - */ +/** Expression Date Value. */ @RequiredArgsConstructor public class ExprDateValue extends AbstractExprValue { private final LocalDate date; - /** - * Constructor of ExprDateValue. - */ + /** Constructor of ExprDateValue. */ public ExprDateValue(String date) { try { this.date = LocalDate.parse(date, DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL); } catch (DateTimeParseException e) { - throw new SemanticCheckException(String.format("date:%s in unsupported format, please use " - + "yyyy-MM-dd", date)); + throw new SemanticCheckException( + String.format("date:%s in unsupported format, please use " + "yyyy-MM-dd", date)); } } diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprDatetimeValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprDatetimeValue.java index 8d40aaf82c8..02fa8354b8c 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprDatetimeValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprDatetimeValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_WITH_TZ; @@ -23,20 +22,19 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; - @RequiredArgsConstructor public class ExprDatetimeValue extends AbstractExprValue { private final LocalDateTime datetime; - /** - * Constructor with datetime string as input. - */ + /** Constructor with datetime string as input. */ public ExprDatetimeValue(String datetime) { try { this.datetime = LocalDateTime.parse(datetime, DATE_TIME_FORMATTER_WITH_TZ); } catch (DateTimeParseException e) { - throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please " - + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", datetime)); + throw new SemanticCheckException( + String.format( + "datetime:%s in unsupported format, please " + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", + datetime)); } } @@ -77,9 +75,11 @@ public boolean equal(ExprValue other) { @Override public String value() { - return String.format("%s %s", DateTimeFormatter.ISO_DATE.format(datetime), - DateTimeFormatter.ISO_TIME.format((datetime.getNano() == 0) - ? datetime.truncatedTo(ChronoUnit.SECONDS) : datetime)); + return String.format( + "%s %s", + DateTimeFormatter.ISO_DATE.format(datetime), + DateTimeFormatter.ISO_TIME.format( + (datetime.getNano() == 0) ? datetime.truncatedTo(ChronoUnit.SECONDS) : datetime)); } @Override diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprDoubleValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprDoubleValue.java index 171b064e68b..c192fe901ce 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprDoubleValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprDoubleValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Double Value. - */ +/** Expression Double Value. */ public class ExprDoubleValue extends AbstractExprNumberValue { public ExprDoubleValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprFloatValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprFloatValue.java index dc454b4b508..6d321687fb4 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprFloatValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprFloatValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Float Value. - */ +/** Expression Float Value. */ public class ExprFloatValue extends AbstractExprNumberValue { public ExprFloatValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprIntegerValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprIntegerValue.java index 06947766fca..81321c1f121 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprIntegerValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprIntegerValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Integer Value. - */ +/** Expression Integer Value. */ public class ExprIntegerValue extends AbstractExprNumberValue { public ExprIntegerValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprIntervalValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprIntervalValue.java index 25a3115e8c2..754520d7c87 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprIntervalValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprIntervalValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.time.temporal.TemporalAmount; @@ -27,7 +26,8 @@ public int compare(ExprValue other) { TemporalAmount otherInterval = other.intervalValue(); if (!interval.getClass().equals(other.intervalValue().getClass())) { throw new ExpressionEvaluationException( - String.format("invalid to compare intervals with units %s and %s", + String.format( + "invalid to compare intervals with units %s and %s", unit(), ((ExprIntervalValue) other).unit())); } return Long.compare( @@ -49,12 +49,9 @@ public ExprType type() { return ExprCoreType.INTERVAL; } - /** - * Util method to get temporal unit stored locally. - */ + /** Util method to get temporal unit stored locally. */ public TemporalUnit unit() { - return interval.getUnits() - .stream() + return interval.getUnits().stream() .filter(v -> interval.get(v) != 0) .findAny() .orElse(interval.getUnits().get(0)); diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprLongValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprLongValue.java index 1df590246cb..537a164490f 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprLongValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprLongValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Long Value. - */ +/** Expression Long Value. */ public class ExprLongValue extends AbstractExprNumberValue { public ExprLongValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprMissingValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprMissingValue.java index 99080747737..80add24042c 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprMissingValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprMissingValue.java @@ -3,21 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.util.Objects; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Missing Value. - */ +/** Expression Missing Value. */ public class ExprMissingValue extends AbstractExprValue { private static final ExprMissingValue instance = new ExprMissingValue(); - private ExprMissingValue() { - } + private ExprMissingValue() {} public static ExprMissingValue of() { return instance; @@ -40,13 +36,13 @@ public boolean isMissing() { @Override public int compare(ExprValue other) { - throw new IllegalStateException(String.format("[BUG] Unreachable, Comparing with MISSING is " - + "undefined")); + throw new IllegalStateException( + String.format("[BUG] Unreachable, Comparing with MISSING is " + "undefined")); } /** - * Missing value is equal to Missing value. - * Notes, this function should only used for Java Object Compare. + * Missing value is equal to Missing value. Notes, this function should only used for Java Object + * Compare. */ @Override public boolean equal(ExprValue other) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprNullValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprNullValue.java index 54d4811d33f..ac2e185e654 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprNullValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprNullValue.java @@ -3,21 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.util.Objects; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Null Value. - */ +/** Expression Null Value. */ public class ExprNullValue extends AbstractExprValue { private static final ExprNullValue instance = new ExprNullValue(); - private ExprNullValue() { - } + private ExprNullValue() {} @Override public int hashCode() { @@ -55,8 +51,8 @@ public int compare(ExprValue other) { } /** - * NULL value is equal to NULL value. - * Notes, this function should only used for Java Object Compare. + * NULL value is equal to NULL value. Notes, this function should only used for Java Object + * Compare. */ @Override public boolean equal(ExprValue other) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprShortValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprShortValue.java index 3e5f6858bc9..418cefa166a 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprShortValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprShortValue.java @@ -3,15 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; -/** - * Expression Short Value. - */ +/** Expression Short Value. */ public class ExprShortValue extends AbstractExprNumberValue { public ExprShortValue(Number value) { diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprStringValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprStringValue.java index c41c23d6ac9..90c999f724a 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprStringValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprStringValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.time.LocalDate; @@ -15,9 +14,7 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; -/** - * Expression String Value. - */ +/** Expression String Value. */ @RequiredArgsConstructor public class ExprStringValue extends AbstractExprValue { private final String value; @@ -44,11 +41,14 @@ public LocalDateTime datetimeValue() { } catch (SemanticCheckException e) { try { return new ExprDatetimeValue( - LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0))) + LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0))) .datetimeValue(); } catch (SemanticCheckException exception) { - throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please " - + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", value)); + throw new SemanticCheckException( + String.format( + "datetime:%s in unsupported format, please " + + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", + value)); } } } diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java index db5bf7cb52e..1da9c89d728 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import static java.time.format.DateTimeFormatter.ISO_LOCAL_TIME; @@ -23,23 +22,20 @@ import org.opensearch.sql.exception.SemanticCheckException; import org.opensearch.sql.expression.function.FunctionProperties; -/** - * Expression Time Value. - */ +/** Expression Time Value. */ @RequiredArgsConstructor public class ExprTimeValue extends AbstractExprValue { private final LocalTime time; - /** - * Constructor of ExprTimeValue. - */ + /** Constructor of ExprTimeValue. */ public ExprTimeValue(String time) { try { this.time = LocalTime.parse(time, DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL); } catch (DateTimeParseException e) { - throw new SemanticCheckException(String.format("time:%s in unsupported format, please use " - + "HH:mm:ss[.SSSSSSSSS]", time)); + throw new SemanticCheckException( + String.format( + "time:%s in unsupported format, please use " + "HH:mm:ss[.SSSSSSSSS]", time)); } } @@ -67,8 +63,7 @@ public LocalDateTime datetimeValue(FunctionProperties functionProperties) { } public Instant timestampValue(FunctionProperties functionProperties) { - return ZonedDateTime.of(dateValue(functionProperties), timeValue(), UTC_ZONE_ID) - .toInstant(); + return ZonedDateTime.of(dateValue(functionProperties), timeValue(), UTC_ZONE_ID).toInstant(); } @Override diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprTimestampValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprTimestampValue.java index d15cee5e71e..661ee549b22 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprTimestampValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprTimestampValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import static org.opensearch.sql.utils.DateTimeFormatters.DATE_TIME_FORMATTER_VARIABLE_NANOS; @@ -22,33 +21,33 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; -/** - * Expression Timestamp Value. - */ +/** Expression Timestamp Value. */ @RequiredArgsConstructor public class ExprTimestampValue extends AbstractExprValue { private final Instant timestamp; - /** - * Constructor. - */ + /** Constructor. */ public ExprTimestampValue(String timestamp) { try { - this.timestamp = LocalDateTime.parse(timestamp, DATE_TIME_FORMATTER_VARIABLE_NANOS) - .atZone(UTC_ZONE_ID) - .toInstant(); + this.timestamp = + LocalDateTime.parse(timestamp, DATE_TIME_FORMATTER_VARIABLE_NANOS) + .atZone(UTC_ZONE_ID) + .toInstant(); } catch (DateTimeParseException e) { - throw new SemanticCheckException(String.format("timestamp:%s in unsupported format, please " - + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", timestamp)); + throw new SemanticCheckException( + String.format( + "timestamp:%s in unsupported format, please " + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", + timestamp)); } - } @Override public String value() { - return timestamp.getNano() == 0 ? DATE_TIME_FORMATTER_WITHOUT_NANO.withZone(UTC_ZONE_ID) - .format(timestamp.truncatedTo(ChronoUnit.SECONDS)) + return timestamp.getNano() == 0 + ? DATE_TIME_FORMATTER_WITHOUT_NANO + .withZone(UTC_ZONE_ID) + .format(timestamp.truncatedTo(ChronoUnit.SECONDS)) : DATE_TIME_FORMATTER_VARIABLE_NANOS.withZone(UTC_ZONE_ID).format(timestamp); } diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprTupleValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprTupleValue.java index 749de931eea..856075bed82 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprTupleValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprTupleValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.util.Iterator; @@ -18,9 +17,7 @@ import org.opensearch.sql.storage.bindingtuple.BindingTuple; import org.opensearch.sql.storage.bindingtuple.LazyBindingTuple; -/** - * Expression Tuple Value. - */ +/** Expression Tuple Value. */ @RequiredArgsConstructor public class ExprTupleValue extends AbstractExprValue { @@ -47,8 +44,7 @@ public ExprType type() { @Override public String toString() { - return valueMap.entrySet() - .stream() + return valueMap.entrySet().stream() .map(entry -> String.format("%s:%s", entry.getKey(), entry.getValue())) .collect(Collectors.joining(",", "{", "}")); } @@ -70,6 +66,7 @@ public ExprValue keyValue(String key) { /** * Override the equals method. + * * @return true for equal, otherwise false. */ public boolean equal(ExprValue o) { @@ -91,9 +88,7 @@ public boolean equal(ExprValue o) { } } - /** - * Only compare the size of the map. - */ + /** Only compare the size of the map. */ @Override public int compare(ExprValue other) { return Integer.compare(valueMap.size(), other.tupleValue().size()); diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprValue.java index 1ae03de37be..86bead77b7d 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprValue.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.model; import java.io.Serializable; @@ -19,18 +18,12 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; import org.opensearch.sql.storage.bindingtuple.BindingTuple; -/** - * The definition of the Expression Value. - */ +/** The definition of the Expression Value. */ public interface ExprValue extends Serializable, Comparable { - /** - * Get the Object value of the Expression Value. - */ + /** Get the Object value of the Expression Value. */ Object value(); - /** - * Get the {@link ExprCoreType} of the Expression Value. - */ + /** Get the {@link ExprCoreType} of the Expression Value. */ ExprType type(); /** @@ -69,136 +62,104 @@ default boolean isDateTime() { return false; } - /** - * Get the {@link BindingTuple}. - */ + /** Get the {@link BindingTuple}. */ default BindingTuple bindingTuples() { return BindingTuple.EMPTY; } - /** - * Get byte value. - */ + /** Get byte value. */ default Byte byteValue() { throw new ExpressionEvaluationException( "invalid to get byteValue from value of type " + type()); } - /** - * Get short value. - */ + /** Get short value. */ default Short shortValue() { throw new ExpressionEvaluationException( "invalid to get shortValue from value of type " + type()); } - /** - * Get integer value. - */ + /** Get integer value. */ default Integer integerValue() { throw new ExpressionEvaluationException( "invalid to get integerValue from value of type " + type()); } - /** - * Get long value. - */ + /** Get long value. */ default Long longValue() { throw new ExpressionEvaluationException( "invalid to get longValue from value of type " + type()); } - /** - * Get float value. - */ + /** Get float value. */ default Float floatValue() { throw new ExpressionEvaluationException( "invalid to get floatValue from value of type " + type()); } - /** - * Get float value. - */ + /** Get float value. */ default Double doubleValue() { throw new ExpressionEvaluationException( "invalid to get doubleValue from value of type " + type()); } - /** - * Get string value. - */ + /** Get string value. */ default String stringValue() { throw new ExpressionEvaluationException( "invalid to get stringValue from value of type " + type()); } - /** - * Get boolean value. - */ + /** Get boolean value. */ default Boolean booleanValue() { throw new ExpressionEvaluationException( "invalid to get booleanValue from value of type " + type()); } - /** - * Get timestamp value. - */ + /** Get timestamp value. */ default Instant timestampValue() { throw new ExpressionEvaluationException( "invalid to get timestampValue from value of type " + type()); } - /** - * Get time value. - */ + /** Get time value. */ default LocalTime timeValue() { throw new ExpressionEvaluationException( "invalid to get timeValue from value of type " + type()); } - /** - * Get date value. - */ + /** Get date value. */ default LocalDate dateValue() { throw new ExpressionEvaluationException( "invalid to get dateValue from value of type " + type()); } - /** - * Get datetime value. - */ + /** Get datetime value. */ default LocalDateTime datetimeValue() { throw new ExpressionEvaluationException( "invalid to get datetimeValue from value of type " + type()); } - /** - * Get interval value. - */ + /** Get interval value. */ default TemporalAmount intervalValue() { throw new ExpressionEvaluationException( "invalid to get intervalValue from value of type " + type()); } - /** - * Get map value. - */ + /** Get map value. */ default Map tupleValue() { throw new ExpressionEvaluationException( "invalid to get tupleValue from value of type " + type()); } - /** - * Get collection value. - */ + /** Get collection value. */ default List collectionValue() { throw new ExpressionEvaluationException( "invalid to get collectionValue from value of type " + type()); } /** - * Get the value specified by key from {@link ExprTupleValue}. - * This method only be implemented in {@link ExprTupleValue}. + * Get the value specified by key from {@link ExprTupleValue}. This method only be implemented in + * {@link ExprTupleValue}. */ default ExprValue keyValue(String key) { return ExprMissingValue.of(); diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprValueUtils.java b/core/src/main/java/org/opensearch/sql/data/model/ExprValueUtils.java index 43a3140ef36..a259eb9fba7 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprValueUtils.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprValueUtils.java @@ -18,9 +18,7 @@ import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.exception.ExpressionEvaluationException; -/** - * The definition of {@link ExprValue} factory. - */ +/** The definition of {@link ExprValue} factory. */ @UtilityClass public class ExprValueUtils { public static final ExprValue LITERAL_TRUE = ExprBooleanValue.of(true); @@ -80,19 +78,15 @@ public static ExprValue timestampValue(Instant value) { return new ExprTimestampValue(value); } - /** - * {@link ExprTupleValue} constructor. - */ + /** {@link ExprTupleValue} constructor. */ public static ExprValue tupleValue(Map map) { LinkedHashMap valueMap = new LinkedHashMap<>(); - map.forEach((k, v) -> valueMap - .put(k, v instanceof ExprValue ? (ExprValue) v : fromObjectValue(v))); + map.forEach( + (k, v) -> valueMap.put(k, v instanceof ExprValue ? (ExprValue) v : fromObjectValue(v))); return new ExprTupleValue(valueMap); } - /** - * {@link ExprCollectionValue} constructor. - */ + /** {@link ExprCollectionValue} constructor. */ public static ExprValue collectionValue(List list) { List valueList = new ArrayList<>(); list.forEach(o -> valueList.add(fromObjectValue(o))); @@ -107,9 +101,7 @@ public static ExprValue nullValue() { return ExprNullValue.of(); } - /** - * Construct ExprValue from Object. - */ + /** Construct ExprValue from Object. */ public static ExprValue fromObjectValue(Object o) { if (null == o) { return LITERAL_NULL; @@ -149,19 +141,17 @@ public static ExprValue fromObjectValue(Object o) { } } - /** - * Construct ExprValue from Object with ExprCoreType. - */ + /** Construct ExprValue from Object with ExprCoreType. */ public static ExprValue fromObjectValue(Object o, ExprCoreType type) { switch (type) { case TIMESTAMP: - return new ExprTimestampValue((String)o); + return new ExprTimestampValue((String) o); case DATE: - return new ExprDateValue((String)o); + return new ExprDateValue((String) o); case TIME: - return new ExprTimeValue((String)o); + return new ExprTimeValue((String) o); case DATETIME: - return new ExprDatetimeValue((String)o); + return new ExprDatetimeValue((String) o); default: return fromObjectValue(o); } diff --git a/core/src/main/java/org/opensearch/sql/data/type/ExprCoreType.java b/core/src/main/java/org/opensearch/sql/data/type/ExprCoreType.java index 815f94a9df6..f1979d86660 100644 --- a/core/src/main/java/org/opensearch/sql/data/type/ExprCoreType.java +++ b/core/src/main/java/org/opensearch/sql/data/type/ExprCoreType.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.type; import com.google.common.collect.ImmutableMap; @@ -15,25 +14,18 @@ import java.util.Set; import java.util.stream.Collectors; -/** - * Expression Type. - */ +/** Expression Type. */ public enum ExprCoreType implements ExprType { - /** - * Unknown due to unsupported data type. - */ + /** Unknown due to unsupported data type. */ UNKNOWN, /** - * Undefined type for special literal such as NULL. - * As the root of data type tree, it is compatible with any other type. - * In other word, undefined type is the "narrowest" type. + * Undefined type for special literal such as NULL. As the root of data type tree, it is + * compatible with any other type. In other word, undefined type is the "narrowest" type. */ UNDEFINED, - /** - * Numbers. - */ + /** Numbers. */ BYTE(UNDEFINED), SHORT(BYTE), INTEGER(SHORT), @@ -41,43 +33,29 @@ public enum ExprCoreType implements ExprType { FLOAT(LONG), DOUBLE(FLOAT), - /** - * String. - */ + /** String. */ STRING(UNDEFINED), - /** - * Boolean. - */ + /** Boolean. */ BOOLEAN(STRING), - /** - * Date. - */ + /** Date. */ DATE(STRING), TIME(STRING), DATETIME(STRING, DATE, TIME), TIMESTAMP(STRING, DATETIME), INTERVAL(UNDEFINED), - /** - * Struct. - */ + /** Struct. */ STRUCT(UNDEFINED), - /** - * Array. - */ + /** Array. */ ARRAY(UNDEFINED); - /** - * Parents (wider/compatible types) of current base type. - */ + /** Parents (wider/compatible types) of current base type. */ private final List parents = new ArrayList<>(); - /** - * The mapping between Type and legacy JDBC type name. - */ + /** The mapping between Type and legacy JDBC type name. */ private static final Map LEGACY_TYPE_NAME_MAPPING = new ImmutableMap.Builder() .put(STRUCT, "OBJECT") @@ -116,14 +94,12 @@ public String legacyTypeName() { return LEGACY_TYPE_NAME_MAPPING.getOrDefault(this, this.name()); } - /** - * Return all the valid ExprCoreType. - */ + /** Return all the valid ExprCoreType. */ public static List coreTypes() { return Arrays.stream(ExprCoreType.values()) - .filter(type -> type != UNKNOWN) - .filter(type -> type != UNDEFINED) - .collect(Collectors.toList()); + .filter(type -> type != UNKNOWN) + .filter(type -> type != UNDEFINED) + .collect(Collectors.toList()); } public static Set numberTypes() { diff --git a/core/src/main/java/org/opensearch/sql/data/type/ExprType.java b/core/src/main/java/org/opensearch/sql/data/type/ExprType.java index 782714ba70e..58d6ee346ba 100644 --- a/core/src/main/java/org/opensearch/sql/data/type/ExprType.java +++ b/core/src/main/java/org/opensearch/sql/data/type/ExprType.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.type; import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN; @@ -13,13 +12,9 @@ import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.expression.Expression; -/** - * The Type of {@link Expression} and {@link ExprValue}. - */ +/** The Type of {@link Expression} and {@link ExprValue}. */ public interface ExprType { - /** - * Is compatible with other types. - */ + /** Is compatible with other types. */ default boolean isCompatible(ExprType other) { if (this.equals(other)) { return true; @@ -37,30 +32,25 @@ default boolean isCompatible(ExprType other) { } /** - * Should cast this type to other type or not. By default, cast is always required - * if the given type is different from this type. + * Should cast this type to other type or not. By default, cast is always required if the given + * type is different from this type. + * * @param other other data type - * @return true if cast is required, otherwise false + * @return true if cast is required, otherwise false */ default boolean shouldCast(ExprType other) { return !this.equals(other); } - /** - * Get the parent type. - */ + /** Get the parent type. */ default List getParent() { return Arrays.asList(UNKNOWN); } - /** - * Get the type name. - */ + /** Get the type name. */ String typeName(); - /** - * Get the legacy type name for old engine. - */ + /** Get the legacy type name for old engine. */ default String legacyTypeName() { return typeName(); } diff --git a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java index e1f356782f5..bcec7bf318e 100644 --- a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java +++ b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.type; import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN; @@ -12,16 +11,9 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; /** - * The definition of widening type rule for expression value. - * ExprType Widens to data types - * INTEGER LONG, FLOAT, DOUBLE - * LONG FLOAT, DOUBLE - * FLOAT DOUBLE - * DOUBLE DOUBLE - * STRING STRING - * BOOLEAN BOOLEAN - * ARRAY ARRAY - * STRUCT STRUCT + * The definition of widening type rule for expression value. ExprType Widens to data types INTEGER + * LONG, FLOAT, DOUBLE LONG FLOAT, DOUBLE FLOAT DOUBLE DOUBLE DOUBLE STRING STRING BOOLEAN BOOLEAN + * ARRAY ARRAY STRUCT STRUCT */ @UtilityClass public class WideningTypeRule { @@ -29,8 +21,8 @@ public class WideningTypeRule { public static final int TYPE_EQUAL = 0; /** - * The widening distance is calculated from the leaf to root. - * e.g. distance(INTEGER, FLOAT) = 2, but distance(FLOAT, INTEGER) = IMPOSSIBLE_WIDENING + * The widening distance is calculated from the leaf to root. e.g. distance(INTEGER, FLOAT) = 2, + * but distance(FLOAT, INTEGER) = IMPOSSIBLE_WIDENING * * @param type1 widen from type * @param type2 widen to type @@ -48,15 +40,15 @@ private static int distance(ExprType type1, ExprType type2, int distance) { } else { return type1.getParent().stream() .map(parentOfType1 -> distance(parentOfType1, type2, distance + 1)) - .reduce(Math::min).get(); + .reduce(Math::min) + .get(); } } /** - * The max type among two types. The max is defined as follow - * if type1 could widen to type2, then max is type2, vice versa - * if type1 could't widen to type2 and type2 could't widen to type1, - * then throw {@link ExpressionEvaluationException}. + * The max type among two types. The max is defined as follow if type1 could widen to type2, then + * max is type2, vice versa if type1 could't widen to type2 and type2 could't widen to type1, then + * throw {@link ExpressionEvaluationException}. * * @param type1 type1 * @param type2 type2 diff --git a/core/src/main/java/org/opensearch/sql/data/utils/ExprValueOrdering.java b/core/src/main/java/org/opensearch/sql/data/utils/ExprValueOrdering.java index ef390dc53b9..812c8dba3d7 100644 --- a/core/src/main/java/org/opensearch/sql/data/utils/ExprValueOrdering.java +++ b/core/src/main/java/org/opensearch/sql/data/utils/ExprValueOrdering.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.utils; import com.google.common.collect.Ordering; diff --git a/core/src/main/java/org/opensearch/sql/data/utils/NaturalExprValueOrdering.java b/core/src/main/java/org/opensearch/sql/data/utils/NaturalExprValueOrdering.java index 13c3606f72b..f3285fd6f9c 100644 --- a/core/src/main/java/org/opensearch/sql/data/utils/NaturalExprValueOrdering.java +++ b/core/src/main/java/org/opensearch/sql/data/utils/NaturalExprValueOrdering.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.utils; import com.google.common.collect.Ordering; diff --git a/core/src/main/java/org/opensearch/sql/data/utils/NullsFirstExprValueOrdering.java b/core/src/main/java/org/opensearch/sql/data/utils/NullsFirstExprValueOrdering.java index 03890bba61f..82fac55e0c0 100644 --- a/core/src/main/java/org/opensearch/sql/data/utils/NullsFirstExprValueOrdering.java +++ b/core/src/main/java/org/opensearch/sql/data/utils/NullsFirstExprValueOrdering.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.utils; import com.google.common.collect.Ordering; diff --git a/core/src/main/java/org/opensearch/sql/data/utils/NullsLastExprValueOrdering.java b/core/src/main/java/org/opensearch/sql/data/utils/NullsLastExprValueOrdering.java index 589d4b30431..0221e508876 100644 --- a/core/src/main/java/org/opensearch/sql/data/utils/NullsLastExprValueOrdering.java +++ b/core/src/main/java/org/opensearch/sql/data/utils/NullsLastExprValueOrdering.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.utils; import com.google.common.collect.Ordering; diff --git a/core/src/main/java/org/opensearch/sql/data/utils/ReverseExprValueOrdering.java b/core/src/main/java/org/opensearch/sql/data/utils/ReverseExprValueOrdering.java index 65fceacf99b..ca3cb251f43 100644 --- a/core/src/main/java/org/opensearch/sql/data/utils/ReverseExprValueOrdering.java +++ b/core/src/main/java/org/opensearch/sql/data/utils/ReverseExprValueOrdering.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.data.utils; import com.google.common.collect.Ordering; diff --git a/core/src/main/java/org/opensearch/sql/datasource/DataSourceService.java b/core/src/main/java/org/opensearch/sql/datasource/DataSourceService.java index 9167737a70b..3d6ddc864e6 100644 --- a/core/src/main/java/org/opensearch/sql/datasource/DataSourceService.java +++ b/core/src/main/java/org/opensearch/sql/datasource/DataSourceService.java @@ -9,9 +9,7 @@ import org.opensearch.sql.datasource.model.DataSource; import org.opensearch.sql.datasource.model.DataSourceMetadata; -/** - * DataSource Service manage {@link DataSource}. - */ +/** DataSource Service manage {@link DataSource}. */ public interface DataSourceService { /** @@ -22,21 +20,19 @@ public interface DataSourceService { */ DataSource getDataSource(String dataSourceName); - /** - * Returns all dataSource Metadata objects. The returned objects won't contain - * any of the credential info. + * Returns all dataSource Metadata objects. The returned objects won't contain any of the + * credential info. * - * @param isDefaultDataSourceRequired is used to specify - * if default opensearch connector is required in the output list. + * @param isDefaultDataSourceRequired is used to specify if default opensearch connector is + * required in the output list. * @return set of {@link DataSourceMetadata}. */ Set getDataSourceMetadata(boolean isDefaultDataSourceRequired); - /** - * Returns dataSourceMetadata object with specific name. - * The returned objects won't contain any crendetial info. + * Returns dataSourceMetadata object with specific name. The returned objects won't contain any + * crendetial info. * * @param name name of the {@link DataSource}. * @return set of {@link DataSourceMetadata}. @@ -57,7 +53,6 @@ public interface DataSourceService { */ void updateDataSource(DataSourceMetadata dataSourceMetadata); - /** * Deletes {@link DataSource} corresponding to the DataSource name. * @@ -66,8 +61,8 @@ public interface DataSourceService { void deleteDataSource(String dataSourceName); /** - * Returns true {@link Boolean} if datasource with dataSourceName exists - * or else false {@link Boolean}. + * Returns true {@link Boolean} if datasource with dataSourceName exists or else false {@link + * Boolean}. * * @param dataSourceName name of the {@link DataSource}. */ diff --git a/core/src/main/java/org/opensearch/sql/datasource/model/DataSource.java b/core/src/main/java/org/opensearch/sql/datasource/model/DataSource.java index 5deb4609619..9623102efb2 100644 --- a/core/src/main/java/org/opensearch/sql/datasource/model/DataSource.java +++ b/core/src/main/java/org/opensearch/sql/datasource/model/DataSource.java @@ -12,9 +12,7 @@ import lombok.RequiredArgsConstructor; import org.opensearch.sql.storage.StorageEngine; -/** - * Each user configured datasource mapping to one instance of DataSource per JVM. - */ +/** Each user configured datasource mapping to one instance of DataSource per JVM. */ @Getter @RequiredArgsConstructor @EqualsAndHashCode @@ -24,7 +22,5 @@ public class DataSource { private final DataSourceType connectorType; - @EqualsAndHashCode.Exclude - private final StorageEngine storageEngine; - + @EqualsAndHashCode.Exclude private final StorageEngine storageEngine; } diff --git a/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java b/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java index 7945f8aec3a..8e5ff7e1a65 100644 --- a/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java +++ b/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceMetadata.java @@ -5,7 +5,6 @@ package org.opensearch.sql.datasource.model; - import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME; import com.fasterxml.jackson.annotation.JsonFormat; @@ -30,25 +29,25 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class DataSourceMetadata { - @JsonProperty - private String name; + @JsonProperty private String name; @JsonProperty @JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) private DataSourceType connector; - @JsonProperty - private List allowedRoles; + @JsonProperty private List allowedRoles; - @JsonProperty - private Map properties; + @JsonProperty private Map properties; /** * Default OpenSearch {@link DataSourceMetadata}. Which is used to register default OpenSearch * {@link DataSource} to {@link DataSourceService}. */ public static DataSourceMetadata defaultOpenSearchDataSourceMetadata() { - return new DataSourceMetadata(DEFAULT_DATASOURCE_NAME, - DataSourceType.OPENSEARCH, Collections.emptyList(), ImmutableMap.of()); + return new DataSourceMetadata( + DEFAULT_DATASOURCE_NAME, + DataSourceType.OPENSEARCH, + Collections.emptyList(), + ImmutableMap.of()); } } diff --git a/core/src/main/java/org/opensearch/sql/exception/ExpressionEvaluationException.java b/core/src/main/java/org/opensearch/sql/exception/ExpressionEvaluationException.java index 65ea187666f..d11f6e4f9e9 100644 --- a/core/src/main/java/org/opensearch/sql/exception/ExpressionEvaluationException.java +++ b/core/src/main/java/org/opensearch/sql/exception/ExpressionEvaluationException.java @@ -3,12 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.exception; -/** - * Exception for Expression Evaluation. - */ +/** Exception for Expression Evaluation. */ public class ExpressionEvaluationException extends QueryEngineException { public ExpressionEvaluationException(String message) { super(message); diff --git a/core/src/main/java/org/opensearch/sql/exception/NoCursorException.java b/core/src/main/java/org/opensearch/sql/exception/NoCursorException.java index 9383bece573..57f75c2cb06 100644 --- a/core/src/main/java/org/opensearch/sql/exception/NoCursorException.java +++ b/core/src/main/java/org/opensearch/sql/exception/NoCursorException.java @@ -6,8 +6,7 @@ package org.opensearch.sql.exception; /** - * This should be thrown on serialization of a PhysicalPlan tree if paging is finished. - * Processing of such exception should outcome of responding no cursor to the user. + * This should be thrown on serialization of a PhysicalPlan tree if paging is finished. Processing + * of such exception should outcome of responding no cursor to the user. */ -public class NoCursorException extends RuntimeException { -} +public class NoCursorException extends RuntimeException {} diff --git a/core/src/main/java/org/opensearch/sql/exception/QueryEngineException.java b/core/src/main/java/org/opensearch/sql/exception/QueryEngineException.java index ce90ecff5c6..b3d13bef71a 100644 --- a/core/src/main/java/org/opensearch/sql/exception/QueryEngineException.java +++ b/core/src/main/java/org/opensearch/sql/exception/QueryEngineException.java @@ -3,12 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.exception; -/** - * Query analysis abstract exception. - */ +/** Query analysis abstract exception. */ public class QueryEngineException extends RuntimeException { public QueryEngineException(String message) { diff --git a/core/src/main/java/org/opensearch/sql/exception/SemanticCheckException.java b/core/src/main/java/org/opensearch/sql/exception/SemanticCheckException.java index 8673dbfc3cb..6e0c184af80 100644 --- a/core/src/main/java/org/opensearch/sql/exception/SemanticCheckException.java +++ b/core/src/main/java/org/opensearch/sql/exception/SemanticCheckException.java @@ -3,12 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.exception; -/** - * Semantic Check Exception. - */ +/** Semantic Check Exception. */ public class SemanticCheckException extends QueryEngineException { public SemanticCheckException(String message) { super(message); diff --git a/core/src/main/java/org/opensearch/sql/exception/UnsupportedCursorRequestException.java b/core/src/main/java/org/opensearch/sql/exception/UnsupportedCursorRequestException.java index 6ed8e02e5fc..81173045715 100644 --- a/core/src/main/java/org/opensearch/sql/exception/UnsupportedCursorRequestException.java +++ b/core/src/main/java/org/opensearch/sql/exception/UnsupportedCursorRequestException.java @@ -5,8 +5,5 @@ package org.opensearch.sql.exception; -/** - * This should be thrown by V2 engine to support fallback scenario. - */ -public class UnsupportedCursorRequestException extends RuntimeException { -} +/** This should be thrown by V2 engine to support fallback scenario. */ +public class UnsupportedCursorRequestException extends RuntimeException {} diff --git a/core/src/main/java/org/opensearch/sql/executor/ExecutionContext.java b/core/src/main/java/org/opensearch/sql/executor/ExecutionContext.java index 8a3162068f5..22d14972eba 100644 --- a/core/src/main/java/org/opensearch/sql/executor/ExecutionContext.java +++ b/core/src/main/java/org/opensearch/sql/executor/ExecutionContext.java @@ -9,12 +9,9 @@ import lombok.Getter; import org.opensearch.sql.storage.split.Split; -/** - * Execution context hold planning related information. - */ +/** Execution context hold planning related information. */ public class ExecutionContext { - @Getter - private final Optional split; + @Getter private final Optional split; public ExecutionContext(Split split) { this.split = Optional.of(split); diff --git a/core/src/main/java/org/opensearch/sql/executor/ExecutionEngine.java b/core/src/main/java/org/opensearch/sql/executor/ExecutionEngine.java index 9465da22c91..02c4d4a4b83 100644 --- a/core/src/main/java/org/opensearch/sql/executor/ExecutionEngine.java +++ b/core/src/main/java/org/opensearch/sql/executor/ExecutionEngine.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.executor; import java.util.List; @@ -17,39 +16,33 @@ import org.opensearch.sql.executor.pagination.Cursor; import org.opensearch.sql.planner.physical.PhysicalPlan; -/** - * Execution engine that encapsulates execution details. - */ +/** Execution engine that encapsulates execution details. */ public interface ExecutionEngine { /** - * Execute physical plan and call back response listener. - * Todo. deprecated this interface after finalize {@link ExecutionContext}. + * Execute physical plan and call back response listener. Todo. deprecated this interface after + * finalize {@link ExecutionContext}. * - * @param plan executable physical plan + * @param plan executable physical plan * @param listener response listener */ void execute(PhysicalPlan plan, ResponseListener listener); - /** - * Execute physical plan with {@link ExecutionContext} and call back response listener. - */ - void execute(PhysicalPlan plan, ExecutionContext context, - ResponseListener listener); + /** Execute physical plan with {@link ExecutionContext} and call back response listener. */ + void execute( + PhysicalPlan plan, ExecutionContext context, ResponseListener listener); /** - * Explain physical plan and call back response listener. The reason why this has to - * be part of execution engine interface is that the physical plan probably needs to - * be executed to get more info for profiling, such as actual execution time, rows fetched etc. + * Explain physical plan and call back response listener. The reason why this has to be part of + * execution engine interface is that the physical plan probably needs to be executed to get more + * info for profiling, such as actual execution time, rows fetched etc. * - * @param plan physical plan to explain + * @param plan physical plan to explain * @param listener response listener */ void explain(PhysicalPlan plan, ResponseListener listener); - /** - * Data class that encapsulates ExprValue. - */ + /** Data class that encapsulates ExprValue. */ @Data class QueryResponse { private final Schema schema; @@ -70,8 +63,8 @@ public static class Column { } /** - * Data class that encapsulates explain result. This can help decouple core engine - * from concrete explain response format. + * Data class that encapsulates explain result. This can help decouple core engine from concrete + * explain response format. */ @Data class ExplainResponse { @@ -86,5 +79,4 @@ class ExplainResponseNode { private Map description; private List children; } - } diff --git a/core/src/main/java/org/opensearch/sql/executor/Explain.java b/core/src/main/java/org/opensearch/sql/executor/Explain.java index 7c16e0b7200..0f05b99383f 100644 --- a/core/src/main/java/org/opensearch/sql/executor/Explain.java +++ b/core/src/main/java/org/opensearch/sql/executor/Explain.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.executor; import com.google.common.collect.ImmutableMap; @@ -35,11 +34,9 @@ import org.opensearch.sql.planner.physical.WindowOperator; import org.opensearch.sql.storage.TableScanOperator; -/** - * Visitor that explains a physical plan to JSON format. - */ +/** Visitor that explains a physical plan to JSON format. */ public class Explain extends PhysicalPlanNodeVisitor - implements Function { + implements Function { @Override public ExplainResponse apply(PhysicalPlan plan) { @@ -48,109 +45,160 @@ public ExplainResponse apply(PhysicalPlan plan) { @Override public ExplainResponseNode visitProject(ProjectOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "fields", node.getProjectList().toString()))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of("fields", node.getProjectList().toString()))); } @Override public ExplainResponseNode visitFilter(FilterOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "conditions", node.getConditions().toString()))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of("conditions", node.getConditions().toString()))); } @Override public ExplainResponseNode visitSort(SortOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "sortList", describeSortList(node.getSortList())))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of("sortList", describeSortList(node.getSortList())))); } @Override public ExplainResponseNode visitTableScan(TableScanOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "request", node.toString()))); + return explain( + node, + context, + explainNode -> explainNode.setDescription(ImmutableMap.of("request", node.toString()))); } @Override public ExplainResponseNode visitAggregation(AggregationOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "aggregators", node.getAggregatorList().toString(), - "groupBy", node.getGroupByExprList().toString()))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of( + "aggregators", node.getAggregatorList().toString(), + "groupBy", node.getGroupByExprList().toString()))); } @Override public ExplainResponseNode visitWindow(WindowOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "function", node.getWindowFunction().toString(), - "definition", ImmutableMap.of( - "partitionBy", node.getWindowDefinition().getPartitionByList().toString(), - "sortList", describeSortList(node.getWindowDefinition().getSortList()))))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of( + "function", node.getWindowFunction().toString(), + "definition", + ImmutableMap.of( + "partitionBy", + node.getWindowDefinition().getPartitionByList().toString(), + "sortList", + describeSortList(node.getWindowDefinition().getSortList()))))); } @Override public ExplainResponseNode visitRename(RenameOperator node, Object context) { Map renameMappingDescription = - node.getMapping() - .entrySet() - .stream() - .collect(Collectors.toMap( - e -> e.getKey().toString(), - e -> e.getValue().toString())); + node.getMapping().entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())); - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "mapping", renameMappingDescription))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription(ImmutableMap.of("mapping", renameMappingDescription))); } @Override public ExplainResponseNode visitRemove(RemoveOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "removeList", node.getRemoveList().toString()))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of("removeList", node.getRemoveList().toString()))); } @Override public ExplainResponseNode visitEval(EvalOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "expressions", convertPairListToMap(node.getExpressionList())))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of("expressions", convertPairListToMap(node.getExpressionList())))); } @Override public ExplainResponseNode visitDedupe(DedupeOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "dedupeList", node.getDedupeList().toString(), - "allowedDuplication", node.getAllowedDuplication(), - "keepEmpty", node.getKeepEmpty(), - "consecutive", node.getConsecutive()))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of( + "dedupeList", node.getDedupeList().toString(), + "allowedDuplication", node.getAllowedDuplication(), + "keepEmpty", node.getKeepEmpty(), + "consecutive", node.getConsecutive()))); } @Override public ExplainResponseNode visitRareTopN(RareTopNOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "commandType", node.getCommandType(), - "noOfResults", node.getNoOfResults(), - "fields", node.getFieldExprList().toString(), - "groupBy", node.getGroupByExprList().toString() - ))); + return explain( + node, + context, + explainNode -> + explainNode.setDescription( + ImmutableMap.of( + "commandType", node.getCommandType(), + "noOfResults", node.getNoOfResults(), + "fields", node.getFieldExprList().toString(), + "groupBy", node.getGroupByExprList().toString()))); } @Override public ExplainResponseNode visitValues(ValuesOperator node, Object context) { - return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of( - "values", node.getValues()))); + return explain( + node, + context, + explainNode -> explainNode.setDescription(ImmutableMap.of("values", node.getValues()))); } @Override public ExplainResponseNode visitLimit(LimitOperator node, Object context) { - return explain(node, context, explanNode -> explanNode.setDescription(ImmutableMap.of( - "limit", node.getLimit(), "offset", node.getOffset()))); + return explain( + node, + context, + explanNode -> + explanNode.setDescription( + ImmutableMap.of("limit", node.getLimit(), "offset", node.getOffset()))); } @Override public ExplainResponseNode visitNested(NestedOperator node, Object context) { - return explain(node, context, explanNode -> explanNode.setDescription(ImmutableMap.of( - "nested", node.getFields()))); + return explain( + node, + context, + explanNode -> explanNode.setDescription(ImmutableMap.of("nested", node.getFields()))); } - protected ExplainResponseNode explain(PhysicalPlan node, Object context, - Consumer doExplain) { + protected ExplainResponseNode explain( + PhysicalPlan node, Object context, Consumer doExplain) { ExplainResponseNode explainNode = new ExplainResponseNode(getOperatorName(node)); List children = new ArrayList<>(); @@ -169,19 +217,18 @@ private String getOperatorName(PhysicalPlan node) { private Map convertPairListToMap(List> pairs) { return pairs.stream() - .collect(Collectors.toMap( - p -> p.getLeft().toString(), - p -> p.getRight().toString())); + .collect(Collectors.toMap(p -> p.getLeft().toString(), p -> p.getRight().toString())); } private Map> describeSortList( List> sortList) { return sortList.stream() - .collect(Collectors.toMap( - p -> p.getRight().toString(), - p -> ImmutableMap.of( - "sortOrder", p.getLeft().getSortOrder().toString(), - "nullOrder", p.getLeft().getNullOrder().toString()))); + .collect( + Collectors.toMap( + p -> p.getRight().toString(), + p -> + ImmutableMap.of( + "sortOrder", p.getLeft().getSortOrder().toString(), + "nullOrder", p.getLeft().getNullOrder().toString()))); } - } diff --git a/core/src/main/java/org/opensearch/sql/executor/QueryId.java b/core/src/main/java/org/opensearch/sql/executor/QueryId.java index 933cb5d82dc..eea8166e2a5 100644 --- a/core/src/main/java/org/opensearch/sql/executor/QueryId.java +++ b/core/src/main/java/org/opensearch/sql/executor/QueryId.java @@ -12,18 +12,14 @@ import org.apache.commons.lang3.RandomStringUtils; import org.opensearch.sql.executor.execution.AbstractPlan; -/** - * Query id of {@link AbstractPlan}. - */ +/** Query id of {@link AbstractPlan}. */ public class QueryId { - /** - * Query id. - */ - @Getter - private final String queryId; + /** Query id. */ + @Getter private final String queryId; /** * Generate {@link QueryId}. + * * @return {@link QueryId}. */ public static QueryId queryId() { diff --git a/core/src/main/java/org/opensearch/sql/executor/QueryManager.java b/core/src/main/java/org/opensearch/sql/executor/QueryManager.java index 5b41d7ce2e8..ed87290065f 100644 --- a/core/src/main/java/org/opensearch/sql/executor/QueryManager.java +++ b/core/src/main/java/org/opensearch/sql/executor/QueryManager.java @@ -11,13 +11,14 @@ import org.opensearch.sql.executor.execution.AbstractPlan; /** - * QueryManager is the high-level interface of core engine. - * Frontend submit {@link AbstractPlan} to QueryManager. + * QueryManager is the high-level interface of core engine. Frontend submit {@link AbstractPlan} to + * QueryManager. */ public interface QueryManager { /** * Submit {@link AbstractPlan}. + * * @param queryPlan {@link AbstractPlan}. * @return {@link QueryId}. */ diff --git a/core/src/main/java/org/opensearch/sql/executor/QueryService.java b/core/src/main/java/org/opensearch/sql/executor/QueryService.java index 94e70819204..e71ad81c254 100644 --- a/core/src/main/java/org/opensearch/sql/executor/QueryService.java +++ b/core/src/main/java/org/opensearch/sql/executor/QueryService.java @@ -18,9 +18,7 @@ import org.opensearch.sql.planner.logical.LogicalPlan; import org.opensearch.sql.planner.physical.PhysicalPlan; -/** - * The low level interface of core engine. - */ +/** The low level interface of core engine. */ @RequiredArgsConstructor public class QueryService { @@ -31,14 +29,14 @@ public class QueryService { private final Planner planner; /** - * Execute the {@link UnresolvedPlan}, using {@link ResponseListener} to get response. - * Todo. deprecated this interface after finalize {@link PlanContext}. + * Execute the {@link UnresolvedPlan}, using {@link ResponseListener} to get response. Todo. + * deprecated this interface after finalize {@link PlanContext}. * - * @param plan {@link UnresolvedPlan} + * @param plan {@link UnresolvedPlan} * @param listener {@link ResponseListener} */ - public void execute(UnresolvedPlan plan, - ResponseListener listener) { + public void execute( + UnresolvedPlan plan, ResponseListener listener) { try { executePlan(analyze(plan), PlanContext.emptyPlanContext(), listener); } catch (Exception e) { @@ -48,22 +46,23 @@ public void execute(UnresolvedPlan plan, /** * Execute the {@link UnresolvedPlan}, with {@link PlanContext} and using {@link ResponseListener} - * to get response. - * Todo. Pass split from PlanContext to ExecutionEngine in following PR. + * to get response. Todo. Pass split from PlanContext to ExecutionEngine in following PR. * * @param plan {@link LogicalPlan} * @param planContext {@link PlanContext} * @param listener {@link ResponseListener} */ - public void executePlan(LogicalPlan plan, - PlanContext planContext, - ResponseListener listener) { + public void executePlan( + LogicalPlan plan, + PlanContext planContext, + ResponseListener listener) { try { planContext .getSplit() .ifPresentOrElse( split -> executionEngine.execute(plan(plan), new ExecutionContext(split), listener), - () -> executionEngine.execute( + () -> + executionEngine.execute( plan(plan), ExecutionContext.emptyExecutionContext(), listener)); } catch (Exception e) { listener.onFailure(e); @@ -71,14 +70,14 @@ public void executePlan(LogicalPlan plan, } /** - * Explain the query in {@link UnresolvedPlan} using {@link ResponseListener} to - * get and format explain response. + * Explain the query in {@link UnresolvedPlan} using {@link ResponseListener} to get and format + * explain response. * * @param plan {@link UnresolvedPlan} * @param listener {@link ResponseListener} for explain response */ - public void explain(UnresolvedPlan plan, - ResponseListener listener) { + public void explain( + UnresolvedPlan plan, ResponseListener listener) { try { executionEngine.explain(plan(analyze(plan)), listener); } catch (Exception e) { @@ -86,16 +85,12 @@ public void explain(UnresolvedPlan plan, } } - /** - * Analyze {@link UnresolvedPlan}. - */ + /** Analyze {@link UnresolvedPlan}. */ public LogicalPlan analyze(UnresolvedPlan plan) { return analyzer.analyze(plan, new AnalysisContext()); } - /** - * Translate {@link LogicalPlan} to {@link PhysicalPlan}. - */ + /** Translate {@link LogicalPlan} to {@link PhysicalPlan}. */ public PhysicalPlan plan(LogicalPlan plan) { return planner.plan(plan); } diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/AbstractPlan.java b/core/src/main/java/org/opensearch/sql/executor/execution/AbstractPlan.java index 1654293c043..23091777ce7 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/AbstractPlan.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/AbstractPlan.java @@ -8,28 +8,20 @@ package org.opensearch.sql.executor.execution; - import lombok.Getter; import lombok.RequiredArgsConstructor; import org.opensearch.sql.common.response.ResponseListener; import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.executor.QueryId; -/** - * AbstractPlan represent the execution entity of the Statement. - */ +/** AbstractPlan represent the execution entity of the Statement. */ @RequiredArgsConstructor public abstract class AbstractPlan { - /** - * Uniq query id. - */ - @Getter - private final QueryId queryId; + /** Uniq query id. */ + @Getter private final QueryId queryId; - /** - * Start query execution. - */ + /** Start query execution. */ public abstract void execute(); /** diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/CommandPlan.java b/core/src/main/java/org/opensearch/sql/executor/execution/CommandPlan.java index 0ea52660845..103e0e10812 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/CommandPlan.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/CommandPlan.java @@ -15,26 +15,25 @@ import org.opensearch.sql.executor.QueryService; /** - * Query plan which does not reflect a search query being executed. - * It contains a command or an action, for example, a DDL query. + * Query plan which does not reflect a search query being executed. It contains a command or an + * action, for example, a DDL query. */ public class CommandPlan extends AbstractPlan { - /** - * The query plan ast. - */ + /** The query plan ast. */ protected final UnresolvedPlan plan; - /** - * Query service. - */ + /** Query service. */ protected final QueryService queryService; protected final ResponseListener listener; /** Constructor. */ - public CommandPlan(QueryId queryId, UnresolvedPlan plan, QueryService queryService, - ResponseListener listener) { + public CommandPlan( + QueryId queryId, + UnresolvedPlan plan, + QueryService queryService, + ResponseListener listener) { super(queryId); this.plan = plan; this.queryService = queryService; diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/ExplainPlan.java b/core/src/main/java/org/opensearch/sql/executor/execution/ExplainPlan.java index 8c784f82edc..7868a390019 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/ExplainPlan.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/ExplainPlan.java @@ -12,21 +12,18 @@ import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.executor.QueryId; -/** - * Explain plan. - */ +/** Explain plan. */ public class ExplainPlan extends AbstractPlan { private final AbstractPlan plan; private final ResponseListener explainListener; - /** - * Constructor. - */ - public ExplainPlan(QueryId queryId, - AbstractPlan plan, - ResponseListener explainListener) { + /** Constructor. */ + public ExplainPlan( + QueryId queryId, + AbstractPlan plan, + ResponseListener explainListener) { super(queryId); this.plan = plan; this.explainListener = explainListener; diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlan.java b/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlan.java index aeecf3e76ff..0ebdb875f65 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlan.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlan.java @@ -17,19 +17,13 @@ import org.opensearch.sql.executor.QueryId; import org.opensearch.sql.executor.QueryService; -/** - * Query plan which includes a select query. - */ +/** Query plan which includes a select query. */ public class QueryPlan extends AbstractPlan { - /** - * The query plan ast. - */ + /** The query plan ast. */ protected final UnresolvedPlan plan; - /** - * Query service. - */ + /** Query service. */ protected final QueryService queryService; protected final ResponseListener listener; @@ -75,8 +69,9 @@ public void execute() { @Override public void explain(ResponseListener listener) { if (pageSize.isPresent()) { - listener.onFailure(new NotImplementedException( - "`explain` feature for paginated requests is not implemented yet.")); + listener.onFailure( + new NotImplementedException( + "`explain` feature for paginated requests is not implemented yet.")); } else { queryService.explain(plan, listener); } diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlanFactory.java b/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlanFactory.java index 3273eb3c18a..52d7126e174 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlanFactory.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/QueryPlanFactory.java @@ -27,9 +27,7 @@ import org.opensearch.sql.executor.QueryService; import org.opensearch.sql.executor.pagination.CanPaginateVisitor; -/** - * QueryExecution Factory. - */ +/** QueryExecution Factory. */ @RequiredArgsConstructor public class QueryPlanFactory extends AbstractNodeVisitor< @@ -38,14 +36,12 @@ public class QueryPlanFactory Optional>, Optional>>> { - /** - * Query Service. - */ + /** Query Service. */ private final QueryService queryService; /** - * NO_CONSUMER_RESPONSE_LISTENER should never be called. It is only used as constructor - * parameter of {@link QueryPlan}. + * NO_CONSUMER_RESPONSE_LISTENER should never be called. It is only used as constructor parameter + * of {@link QueryPlan}. */ @VisibleForTesting protected static final ResponseListener @@ -64,9 +60,7 @@ public void onFailure(Exception e) { } }; - /** - * Create QueryExecution from Statement. - */ + /** Create QueryExecution from Statement. */ public AbstractPlan create( Statement statement, Optional> queryListener, @@ -74,12 +68,12 @@ public AbstractPlan create( return statement.accept(this, Pair.of(queryListener, explainListener)); } - /** - * Creates a QueryPlan from a cursor. - */ - public AbstractPlan create(String cursor, boolean isExplain, - ResponseListener queryResponseListener, - ResponseListener explainListener) { + /** Creates a QueryPlan from a cursor. */ + public AbstractPlan create( + String cursor, + boolean isExplain, + ResponseListener queryResponseListener, + ResponseListener explainListener) { QueryId queryId = QueryId.queryId(); var plan = new QueryPlan(queryId, new FetchCursor(cursor), queryService, queryResponseListener); return isExplain ? new ExplainPlan(queryId, plan, explainListener) : plan; @@ -89,27 +83,32 @@ boolean canConvertToCursor(UnresolvedPlan plan) { return plan.accept(new CanPaginateVisitor(), null); } - /** - * Creates a {@link CloseCursor} command on a cursor. - */ - public AbstractPlan createCloseCursor(String cursor, - ResponseListener queryResponseListener) { - return new CommandPlan(QueryId.queryId(), new CloseCursor().attach(new FetchCursor(cursor)), - queryService, queryResponseListener); + /** Creates a {@link CloseCursor} command on a cursor. */ + public AbstractPlan createCloseCursor( + String cursor, ResponseListener queryResponseListener) { + return new CommandPlan( + QueryId.queryId(), + new CloseCursor().attach(new FetchCursor(cursor)), + queryService, + queryResponseListener); } @Override public AbstractPlan visitQuery( Query node, - Pair>, - Optional>> + Pair< + Optional>, + Optional>> context) { Preconditions.checkArgument( context.getLeft().isPresent(), "[BUG] query listener must be not null"); if (node.getFetchSize() > 0) { if (canConvertToCursor(node.getPlan())) { - return new QueryPlan(QueryId.queryId(), node.getPlan(), node.getFetchSize(), + return new QueryPlan( + QueryId.queryId(), + node.getPlan(), + node.getFetchSize(), queryService, context.getLeft().get()); } else { @@ -117,24 +116,24 @@ public AbstractPlan visitQuery( throw new UnsupportedCursorRequestException(); } } else { - return new QueryPlan(QueryId.queryId(), node.getPlan(), queryService, - context.getLeft().get()); + return new QueryPlan( + QueryId.queryId(), node.getPlan(), queryService, context.getLeft().get()); } } @Override public AbstractPlan visitExplain( Explain node, - Pair>, - Optional>> + Pair< + Optional>, + Optional>> context) { Preconditions.checkArgument( context.getRight().isPresent(), "[BUG] explain listener must be not null"); return new ExplainPlan( QueryId.queryId(), - create(node.getStatement(), - Optional.of(NO_CONSUMER_RESPONSE_LISTENER), Optional.empty()), + create(node.getStatement(), Optional.of(NO_CONSUMER_RESPONSE_LISTENER), Optional.empty()), context.getRight().get()); } } diff --git a/core/src/main/java/org/opensearch/sql/executor/execution/StreamingQueryPlan.java b/core/src/main/java/org/opensearch/sql/executor/execution/StreamingQueryPlan.java index 9bb37b064cd..52348319c85 100644 --- a/core/src/main/java/org/opensearch/sql/executor/execution/StreamingQueryPlan.java +++ b/core/src/main/java/org/opensearch/sql/executor/execution/StreamingQueryPlan.java @@ -24,9 +24,7 @@ import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; import org.opensearch.sql.planner.logical.LogicalRelation; -/** - * Streaming Query Plan. - */ +/** Streaming Query Plan. */ public class StreamingQueryPlan extends QueryPlan { private static final Logger log = LogManager.getLogger(StreamingQueryPlan.class); @@ -35,14 +33,13 @@ public class StreamingQueryPlan extends QueryPlan { private MicroBatchStreamingExecution streamingExecution; - /** - * constructor. - */ - public StreamingQueryPlan(QueryId queryId, - UnresolvedPlan plan, - QueryService queryService, - ResponseListener listener, - ExecutionStrategy executionStrategy) { + /** constructor. */ + public StreamingQueryPlan( + QueryId queryId, + UnresolvedPlan plan, + QueryService queryService, + ResponseListener listener, + ExecutionStrategy executionStrategy) { super(queryId, plan, queryService, listener); this.executionStrategy = executionStrategy; @@ -70,16 +67,13 @@ public void execute() { } interface ExecutionStrategy { - /** - * execute task. - */ + /** execute task. */ void execute(Runnable task) throws InterruptedException; } /** - * execute task with fixed interval. - * if task run time < interval, trigger next task on next interval. - * if task run time >= interval, trigger next task immediately. + * execute task with fixed interval. if task run time < interval, trigger next task on next + * interval. if task run time >= interval, trigger next task immediately. */ @RequiredArgsConstructor public static class IntervalTriggerExecution implements ExecutionStrategy { diff --git a/core/src/main/java/org/opensearch/sql/executor/pagination/CanPaginateVisitor.java b/core/src/main/java/org/opensearch/sql/executor/pagination/CanPaginateVisitor.java index e304c132bdd..c7ae350245a 100644 --- a/core/src/main/java/org/opensearch/sql/executor/pagination/CanPaginateVisitor.java +++ b/core/src/main/java/org/opensearch/sql/executor/pagination/CanPaginateVisitor.java @@ -41,19 +41,16 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; /** - * Use this unresolved plan visitor to check if a plan can be serialized by PaginatedPlanCache. - * If
plan.accept(new CanPaginateVisitor(...))
returns true, - * then PaginatedPlanCache.convertToCursor will succeed. Otherwise, it will fail. - * The purpose of this visitor is to activate legacy engine fallback mechanism. - * Currently, V2 engine does not support queries with: - * - aggregation (GROUP BY clause or aggregation functions like min/max) - * - in memory aggregation (window function) - * - LIMIT/OFFSET clause(s) - * - without FROM clause - * - JOIN - * - a subquery - * V2 also requires that the table being queried should be an OpenSearch index. - * See PaginatedPlanCache.canConvertToCursor for usage. + * Use this unresolved plan visitor to check if a plan can be serialized by PaginatedPlanCache. If + * + *
plan.accept(new CanPaginateVisitor(...))
+ * + * returns true, then PaginatedPlanCache.convertToCursor will succeed. Otherwise, it will + * fail. The purpose of this visitor is to activate legacy engine fallback mechanism. Currently, V2 + * engine does not support queries with: - aggregation (GROUP BY clause or aggregation functions + * like min/max) - in memory aggregation (window function) - LIMIT/OFFSET clause(s) - without FROM + * clause - JOIN - a subquery V2 also requires that the table being queried should be an OpenSearch + * index. See PaginatedPlanCache.canConvertToCursor for usage. */ public class CanPaginateVisitor extends AbstractNodeVisitor { @@ -80,8 +77,8 @@ protected Boolean canPaginate(Node node, Object context) { // https://github.com/opensearch-project/sql/issues/1471 @Override public Boolean visitSort(Sort node, Object context) { - return node.getSortList().stream().allMatch(f -> f.getField() instanceof QualifiedName - && visitField(f, context)) + return node.getSortList().stream() + .allMatch(f -> f.getField() instanceof QualifiedName && visitField(f, context)) && canPaginate(node, context); } @@ -116,8 +113,8 @@ public Boolean visitLiteral(Literal node, Object context) { @Override public Boolean visitField(Field node, Object context) { - return canPaginate(node, context) && node.getFieldArgs().stream() - .allMatch(n -> n.accept(this, context)); + return canPaginate(node, context) + && node.getFieldArgs().stream().allMatch(n -> n.accept(this, context)); } @Override @@ -192,8 +189,8 @@ public Boolean visitFunction(Function node, Object context) { @Override public Boolean visitIn(In node, Object context) { - return canPaginate(node, context) && node.getValueList().stream() - .allMatch(n -> n.accept(this, context)); + return canPaginate(node, context) + && node.getValueList().stream().allMatch(n -> n.accept(this, context)); } @Override diff --git a/core/src/main/java/org/opensearch/sql/executor/pagination/Cursor.java b/core/src/main/java/org/opensearch/sql/executor/pagination/Cursor.java index bb320f5c670..59a14a2d728 100644 --- a/core/src/main/java/org/opensearch/sql/executor/pagination/Cursor.java +++ b/core/src/main/java/org/opensearch/sql/executor/pagination/Cursor.java @@ -14,8 +14,7 @@ public class Cursor { public static final Cursor None = new Cursor(null); - @Getter - private final String data; + @Getter private final String data; public String toString() { return data; diff --git a/core/src/main/java/org/opensearch/sql/executor/pagination/PlanSerializer.java b/core/src/main/java/org/opensearch/sql/executor/pagination/PlanSerializer.java index 07cf174d73f..40a6cdfa84d 100644 --- a/core/src/main/java/org/opensearch/sql/executor/pagination/PlanSerializer.java +++ b/core/src/main/java/org/opensearch/sql/executor/pagination/PlanSerializer.java @@ -24,8 +24,8 @@ import org.opensearch.sql.storage.StorageEngine; /** - * This class is entry point to paged requests. It is responsible to cursor serialization - * and deserialization. + * This class is entry point to paged requests. It is responsible to cursor serialization and + * deserialization. */ @RequiredArgsConstructor public class PlanSerializer { @@ -33,14 +33,11 @@ public class PlanSerializer { private final StorageEngine engine; - - /** - * Converts a physical plan tree to a cursor. - */ + /** Converts a physical plan tree to a cursor. */ public Cursor convertToCursor(PhysicalPlan plan) { try { - return new Cursor(CURSOR_PREFIX - + serialize(((SerializablePlan) plan).getPlanForSerialization())); + return new Cursor( + CURSOR_PREFIX + serialize(((SerializablePlan) plan).getPlanForSerialization())); // ClassCastException thrown when a plan in the tree doesn't implement SerializablePlan } catch (NotSerializableException | ClassCastException | NoCursorException e) { return Cursor.None; @@ -49,6 +46,7 @@ public Cursor convertToCursor(PhysicalPlan plan) { /** * Serializes and compresses the object. + * * @param object The object. * @return Encoded binary data. */ @@ -61,9 +59,12 @@ protected String serialize(Serializable object) throws NotSerializableException ByteArrayOutputStream out = new ByteArrayOutputStream(); // GZIP provides 35-45%, lzma from apache commons-compress has few % better compression - GZIPOutputStream gzip = new GZIPOutputStream(out) { { - this.def.setLevel(Deflater.BEST_COMPRESSION); - } }; + GZIPOutputStream gzip = + new GZIPOutputStream(out) { + { + this.def.setLevel(Deflater.BEST_COMPRESSION); + } + }; gzip.write(output.toByteArray()); gzip.close(); @@ -77,24 +78,23 @@ protected String serialize(Serializable object) throws NotSerializableException /** * Decompresses and deserializes the binary data. + * * @param code Encoded binary data. * @return An object. */ protected Serializable deserialize(String code) { try { - GZIPInputStream gzip = new GZIPInputStream( - new ByteArrayInputStream(HashCode.fromString(code).asBytes())); - ObjectInputStream objectInput = new CursorDeserializationStream( - new ByteArrayInputStream(gzip.readAllBytes())); + GZIPInputStream gzip = + new GZIPInputStream(new ByteArrayInputStream(HashCode.fromString(code).asBytes())); + ObjectInputStream objectInput = + new CursorDeserializationStream(new ByteArrayInputStream(gzip.readAllBytes())); return (Serializable) objectInput.readObject(); } catch (Exception e) { throw new IllegalStateException("Failed to deserialize object", e); } } - /** - * Converts a cursor to a physical plan tree. - */ + /** Converts a cursor to a physical plan tree. */ public PhysicalPlan convertToPlan(String cursor) { if (!cursor.startsWith(CURSOR_PREFIX)) { throw new UnsupportedOperationException("Unsupported cursor"); diff --git a/core/src/main/java/org/opensearch/sql/executor/streaming/Batch.java b/core/src/main/java/org/opensearch/sql/executor/streaming/Batch.java index cd7d7dae5a9..ab03c4fb533 100644 --- a/core/src/main/java/org/opensearch/sql/executor/streaming/Batch.java +++ b/core/src/main/java/org/opensearch/sql/executor/streaming/Batch.java @@ -8,9 +8,7 @@ import lombok.Data; import org.opensearch.sql.storage.split.Split; -/** - * A batch of streaming execution. - */ +/** A batch of streaming execution. */ @Data public class Batch { private final Split split; diff --git a/core/src/main/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecution.java b/core/src/main/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecution.java index c31ed18c572..4e05484f15a 100644 --- a/core/src/main/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecution.java +++ b/core/src/main/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecution.java @@ -20,9 +20,7 @@ import org.opensearch.sql.planner.PlanContext; import org.opensearch.sql.planner.logical.LogicalPlan; -/** - * Micro batch streaming execution. - */ +/** Micro batch streaming execution. */ public class MicroBatchStreamingExecution { private static final Logger log = LogManager.getLogger(MicroBatchStreamingExecution.class); @@ -46,9 +44,7 @@ public class MicroBatchStreamingExecution { /** keep track the latest commit batchId. */ private final MetadataLog committedLog; - /** - * Constructor. - */ + /** Constructor. */ public MicroBatchStreamingExecution( StreamingSource source, LogicalPlan batchPlan, @@ -63,9 +59,7 @@ public MicroBatchStreamingExecution( this.committedLog = committedLog; } - /** - * Pull the {@link Batch} from {@link StreamingSource} and execute the {@link Batch}. - */ + /** Pull the {@link Batch} from {@link StreamingSource} and execute the {@link Batch}. */ public void execute() { Long latestBatchId = offsetLog.getLatest().map(Pair::getKey).orElse(INITIAL_LATEST_BATCH_ID); Long latestCommittedBatchId = diff --git a/core/src/main/java/org/opensearch/sql/executor/streaming/Offset.java b/core/src/main/java/org/opensearch/sql/executor/streaming/Offset.java index 00f040e437d..27960da84d3 100644 --- a/core/src/main/java/org/opensearch/sql/executor/streaming/Offset.java +++ b/core/src/main/java/org/opensearch/sql/executor/streaming/Offset.java @@ -7,9 +7,7 @@ import lombok.Data; -/** - * Offset. - */ +/** Offset. */ @Data public class Offset { diff --git a/core/src/main/java/org/opensearch/sql/executor/streaming/StreamingSource.java b/core/src/main/java/org/opensearch/sql/executor/streaming/StreamingSource.java index ebd3fa714b7..2b45a45d9ac 100644 --- a/core/src/main/java/org/opensearch/sql/executor/streaming/StreamingSource.java +++ b/core/src/main/java/org/opensearch/sql/executor/streaming/StreamingSource.java @@ -7,9 +7,7 @@ import java.util.Optional; -/** - * Streaming source. - */ +/** Streaming source. */ public interface StreamingSource { /** * Get current {@link Offset} of stream data. diff --git a/core/src/main/java/org/opensearch/sql/expression/DSL.java b/core/src/main/java/org/opensearch/sql/expression/DSL.java index 3f1897e4838..4341668b693 100644 --- a/core/src/main/java/org/opensearch/sql/expression/DSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/DSL.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import java.util.Arrays; @@ -29,8 +28,7 @@ public class DSL { - private DSL() { - } + private DSL() {} public static LiteralExpression literal(Byte value) { return new LiteralExpression(ExprValueUtils.byteValue(value)); @@ -68,9 +66,7 @@ public static LiteralExpression literal(ExprValue value) { return new LiteralExpression(value); } - /** - * Wrap a number to {@link LiteralExpression}. - */ + /** Wrap a number to {@link LiteralExpression}. */ public static LiteralExpression literal(Number value) { if (value instanceof Integer) { return new LiteralExpression(ExprValueUtils.integerValue(value.intValue())); @@ -88,21 +84,21 @@ public static ReferenceExpression ref(String ref, ExprType type) { } /** - * Wrap a named expression if not yet. The intent is that different languages may use - * Alias or not when building AST. This caused either named or unnamed expression - * is resolved by analyzer. To make unnamed expression acceptable for logical project, - * it is required to wrap it by named expression here before passing to logical project. + * Wrap a named expression if not yet. The intent is that different languages may use Alias or not + * when building AST. This caused either named or unnamed expression is resolved by analyzer. To + * make unnamed expression acceptable for logical project, it is required to wrap it by named + * expression here before passing to logical project. * - * @param expression expression - * @return expression if named already or expression wrapped by named expression. + * @param expression expression + * @return expression if named already or expression wrapped by named expression. */ public static NamedExpression named(Expression expression) { if (expression instanceof NamedExpression) { return (NamedExpression) expression; } if (expression instanceof ParseExpression) { - return named(((ParseExpression) expression).getIdentifier().valueOf().stringValue(), - expression); + return named( + ((ParseExpression) expression).getIdentifier().valueOf().stringValue(), expression); } return named(expression.toString(), expression); } @@ -127,18 +123,18 @@ public static NamedArgumentExpression namedArgument(String name, String value) { return namedArgument(name, literal(value)); } - public static GrokExpression grok(Expression sourceField, Expression pattern, - Expression identifier) { + public static GrokExpression grok( + Expression sourceField, Expression pattern, Expression identifier) { return new GrokExpression(sourceField, pattern, identifier); } - public static RegexExpression regex(Expression sourceField, Expression pattern, - Expression identifier) { + public static RegexExpression regex( + Expression sourceField, Expression pattern, Expression identifier) { return new RegexExpression(sourceField, pattern, identifier); } - public static PatternsExpression patterns(Expression sourceField, Expression pattern, - Expression identifier) { + public static PatternsExpression patterns( + Expression sourceField, Expression pattern, Expression identifier) { return new PatternsExpression(sourceField, pattern, identifier); } @@ -268,7 +264,6 @@ public static FunctionExpression signum(Expression... expressions) { public static FunctionExpression sinh(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.SINH, expressions); - } public static FunctionExpression sqrt(Expression... expressions) { @@ -364,8 +359,7 @@ public static FunctionExpression dayname(Expression... expressions) { } public static FunctionExpression dayofmonth( - FunctionProperties functionProperties, - Expression... expressions) { + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.DAYOFMONTH, expressions); } @@ -379,8 +373,7 @@ public static FunctionExpression dayofyear(Expression... expressions) { } public static FunctionExpression day_of_month( - FunctionProperties functionProperties, - Expression... expressions) { + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.DAY_OF_MONTH, expressions); } @@ -394,8 +387,8 @@ public static FunctionExpression day_of_week( return compile(functionProperties, BuiltinFunctionName.DAY_OF_WEEK, expressions); } - public static FunctionExpression extract(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression extract( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.EXTRACT, expressions); } @@ -419,8 +412,8 @@ public static FunctionExpression hour_of_day(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.HOUR_OF_DAY, expressions); } - public static FunctionExpression last_day(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression last_day( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.LAST_DAY, expressions); } @@ -477,14 +470,13 @@ public static FunctionExpression timestamp(Expression... expressions) { return timestamp(FunctionProperties.None, expressions); } - public static FunctionExpression timestamp(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression timestamp( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.TIMESTAMP, expressions); } public static FunctionExpression date_format( - FunctionProperties functionProperties, - Expression... expressions) { + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.DATE_FORMAT, expressions); } @@ -492,8 +484,8 @@ public static FunctionExpression to_days(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.TO_DAYS, expressions); } - public static FunctionExpression to_seconds(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression to_seconds( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.TO_SECONDS, expressions); } @@ -506,8 +498,8 @@ public static FunctionExpression week( return compile(functionProperties, BuiltinFunctionName.WEEK, expressions); } - public static FunctionExpression weekday(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression weekday( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.WEEKDAY, expressions); } @@ -530,13 +522,13 @@ public static FunctionExpression yearweek( return compile(functionProperties, BuiltinFunctionName.YEARWEEK, expressions); } - public static FunctionExpression str_to_date(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression str_to_date( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.STR_TO_DATE, expressions); } - + public static FunctionExpression sec_to_time(Expression... expressions) { - return compile(FunctionProperties.None, BuiltinFunctionName.SEC_TO_TIME, expressions); + return compile(FunctionProperties.None, BuiltinFunctionName.SEC_TO_TIME, expressions); } public static FunctionExpression substr(Expression... expressions) { @@ -771,8 +763,7 @@ public static FunctionExpression iffunction(Expression... expressions) { return compile(FunctionProperties.None, BuiltinFunctionName.IF, expressions); } - public static Expression cases(Expression defaultResult, - WhenClause... whenClauses) { + public static Expression cases(Expression defaultResult, WhenClause... whenClauses) { return new CaseClause(Arrays.asList(whenClauses), defaultResult); } @@ -884,53 +875,52 @@ public static FunctionExpression score_query(Expression... args) { return compile(FunctionProperties.None, BuiltinFunctionName.SCORE_QUERY, args); } - public static FunctionExpression now(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression now(FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.NOW, args); } - public static FunctionExpression current_timestamp(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression current_timestamp( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.CURRENT_TIMESTAMP, args); } - public static FunctionExpression localtimestamp(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression localtimestamp( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.LOCALTIMESTAMP, args); } - public static FunctionExpression localtime(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression localtime( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.LOCALTIME, args); } - public static FunctionExpression sysdate(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression sysdate( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.SYSDATE, args); } - public static FunctionExpression curtime(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression curtime( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.CURTIME, args); } - public static FunctionExpression current_time(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression current_time( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.CURRENT_TIME, args); } - public static FunctionExpression curdate(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression curdate( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.CURDATE, args); } - public static FunctionExpression current_date(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression current_date( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.CURRENT_DATE, args); } - public static FunctionExpression time_format(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression time_format( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.TIME_FORMAT, expressions); } @@ -938,38 +928,36 @@ public static FunctionExpression timestampadd(Expression... expressions) { return timestampadd(FunctionProperties.None, expressions); } - public static FunctionExpression timestampadd(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression timestampadd( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.TIMESTAMPADD, expressions); } - public static FunctionExpression timestampdiff(FunctionProperties functionProperties, - Expression... expressions) { + public static FunctionExpression timestampdiff( + FunctionProperties functionProperties, Expression... expressions) { return compile(functionProperties, BuiltinFunctionName.TIMESTAMPDIFF, expressions); } - - public static FunctionExpression utc_date(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression utc_date( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.UTC_DATE, args); } - public static FunctionExpression utc_time(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression utc_time( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.UTC_TIME, args); } - public static FunctionExpression utc_timestamp(FunctionProperties functionProperties, - Expression... args) { + public static FunctionExpression utc_timestamp( + FunctionProperties functionProperties, Expression... args) { return compile(functionProperties, BuiltinFunctionName.UTC_TIMESTAMP, args); - } @SuppressWarnings("unchecked") - private static - T compile(FunctionProperties functionProperties, - BuiltinFunctionName bfn, Expression... args) { - return (T) BuiltinFunctionRepository.getInstance().compile(functionProperties, - bfn.getName(), Arrays.asList(args)); + private static T compile( + FunctionProperties functionProperties, BuiltinFunctionName bfn, Expression... args) { + return (T) + BuiltinFunctionRepository.getInstance() + .compile(functionProperties, bfn.getName(), Arrays.asList(args)); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/Expression.java b/core/src/main/java/org/opensearch/sql/expression/Expression.java index 25a8173efa0..e4a5f908e5a 100644 --- a/core/src/main/java/org/opensearch/sql/expression/Expression.java +++ b/core/src/main/java/org/opensearch/sql/expression/Expression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import java.io.Serializable; @@ -11,36 +10,28 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.env.Environment; -/** - * The definition of the resolved expression. - */ +/** The definition of the resolved expression. */ public interface Expression extends Serializable { - /** - * Evaluate the value of expression that does not depend on value environment. - */ + /** Evaluate the value of expression that does not depend on value environment. */ default ExprValue valueOf() { return valueOf(null); } - /** - * Evaluate the value of expression in the value environment. - */ + /** Evaluate the value of expression in the value environment. */ ExprValue valueOf(Environment valueEnv); - /** - * The type of the expression. - */ + /** The type of the expression. */ ExprType type(); /** * Accept a visitor to visit current expression node. - * @param visitor visitor - * @param context context - * @param result type - * @param context type - * @return result accumulated by visitor when visiting + * + * @param visitor visitor + * @param context context + * @param result type + * @param context type + * @return result accumulated by visitor when visiting */ T accept(ExpressionNodeVisitor visitor, C context); - } diff --git a/core/src/main/java/org/opensearch/sql/expression/ExpressionNodeVisitor.java b/core/src/main/java/org/opensearch/sql/expression/ExpressionNodeVisitor.java index e3d4e386744..1f11f0805bd 100644 --- a/core/src/main/java/org/opensearch/sql/expression/ExpressionNodeVisitor.java +++ b/core/src/main/java/org/opensearch/sql/expression/ExpressionNodeVisitor.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import org.opensearch.sql.expression.aggregation.Aggregator; @@ -15,8 +14,9 @@ /** * Abstract visitor for expression tree nodes. - * @param type of return value to accumulate when visiting. - * @param type of context. + * + * @param type of return value to accumulate when visiting. + * @param type of context. */ public abstract class ExpressionNodeVisitor { @@ -26,9 +26,10 @@ public T visitNode(Expression node, C context) { /** * Visit children nodes in function arguments. - * @param node function node - * @param context context - * @return result + * + * @param node function node + * @param context context + * @return result */ public T visitChildren(FunctionImplementation node, C context) { T result = defaultResult(); @@ -81,10 +82,9 @@ public T visitNamedAggregator(NamedAggregator node, C context) { } /** - * Call visitFunction() by default rather than visitChildren(). - * This makes CASE/WHEN able to be handled: - * 1) by visitFunction() if not overwritten: ex. FilterQueryBuilder - * 2) by visitCase/When() otherwise if any special logic: ex. ExprReferenceOptimizer + * Call visitFunction() by default rather than visitChildren(). This makes CASE/WHEN able to be + * handled: 1) by visitFunction() if not overwritten: ex. FilterQueryBuilder 2) by + * visitCase/When() otherwise if any special logic: ex. ExprReferenceOptimizer */ public T visitCase(CaseClause node, C context) { return visitFunction(node, context); diff --git a/core/src/main/java/org/opensearch/sql/expression/FunctionExpression.java b/core/src/main/java/org/opensearch/sql/expression/FunctionExpression.java index 2a695f26e6e..b67eb38c000 100644 --- a/core/src/main/java/org/opensearch/sql/expression/FunctionExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/FunctionExpression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import java.util.List; @@ -14,22 +13,17 @@ import org.opensearch.sql.expression.function.FunctionImplementation; import org.opensearch.sql.expression.function.FunctionName; -/** - * Function Expression. - */ +/** Function Expression. */ @EqualsAndHashCode @RequiredArgsConstructor @ToString public abstract class FunctionExpression implements Expression, FunctionImplementation { - @Getter - private final FunctionName functionName; + @Getter private final FunctionName functionName; - @Getter - private final List arguments; + @Getter private final List arguments; @Override public T accept(ExpressionNodeVisitor visitor, C context) { return visitor.visitFunction(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/HighlightExpression.java b/core/src/main/java/org/opensearch/sql/expression/HighlightExpression.java index 804c38a6f77..79cc07f048b 100644 --- a/core/src/main/java/org/opensearch/sql/expression/HighlightExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/HighlightExpression.java @@ -20,9 +20,7 @@ import org.opensearch.sql.expression.env.Environment; import org.opensearch.sql.expression.function.BuiltinFunctionName; -/** - * Highlight Expression. - */ +/** Highlight Expression. */ @Getter public class HighlightExpression extends FunctionExpression { private final Expression highlightField; @@ -30,17 +28,19 @@ public class HighlightExpression extends FunctionExpression { /** * HighlightExpression Constructor. + * * @param highlightField : Highlight field for expression. */ public HighlightExpression(Expression highlightField) { super(BuiltinFunctionName.HIGHLIGHT.getName(), List.of(highlightField)); this.highlightField = highlightField; - this.type = this.highlightField.toString().contains("*") - ? ExprCoreType.STRUCT : ExprCoreType.ARRAY; + this.type = + this.highlightField.toString().contains("*") ? ExprCoreType.STRUCT : ExprCoreType.ARRAY; } /** * Return collection value matching highlight field. + * * @param valueEnv : Dataset to parse value from. * @return : collection value of highlight fields. */ @@ -57,15 +57,15 @@ public ExprValue valueOf(Environment valueEnv) { // used in conjunction with other highlight calls, we need to ensure // only wildcard regex matching is mapped to wildcard call. if (this.type == ExprCoreType.STRUCT && value.type() == ExprCoreType.STRUCT) { - value = new ExprTupleValue( - new LinkedHashMap(value.tupleValue() - .entrySet() - .stream() - .filter(s -> matchesHighlightRegex(s.getKey(), - StringUtils.unquoteText(highlightField.toString()))) - .collect(Collectors.toMap( - e -> e.getKey(), - e -> e.getValue())))); + value = + new ExprTupleValue( + new LinkedHashMap( + value.tupleValue().entrySet().stream() + .filter( + s -> + matchesHighlightRegex( + s.getKey(), StringUtils.unquoteText(highlightField.toString()))) + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())))); if (value.tupleValue().isEmpty()) { value = ExprValueUtils.missingValue(); } @@ -76,6 +76,7 @@ public ExprValue valueOf(Environment valueEnv) { /** * Get type for HighlightExpression. + * * @return : Expression type. */ @Override @@ -90,6 +91,7 @@ public T accept(ExpressionNodeVisitor visitor, C context) { /** * Check if field matches the wildcard pattern used in highlight query. + * * @param field Highlight selected field for query * @param pattern Wildcard regex to match field against * @return True if field matches wildcard pattern diff --git a/core/src/main/java/org/opensearch/sql/expression/LiteralExpression.java b/core/src/main/java/org/opensearch/sql/expression/LiteralExpression.java index adb8e197d1a..eba03e84307 100644 --- a/core/src/main/java/org/opensearch/sql/expression/LiteralExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/LiteralExpression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import lombok.EqualsAndHashCode; @@ -12,9 +11,7 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.env.Environment; -/** - * Literal Expression. - */ +/** Literal Expression. */ @EqualsAndHashCode @RequiredArgsConstructor public class LiteralExpression implements Expression { diff --git a/core/src/main/java/org/opensearch/sql/expression/NamedArgumentExpression.java b/core/src/main/java/org/opensearch/sql/expression/NamedArgumentExpression.java index 0f4601f1bff..c3ce60bd4a7 100644 --- a/core/src/main/java/org/opensearch/sql/expression/NamedArgumentExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/NamedArgumentExpression.java @@ -13,9 +13,7 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.env.Environment; -/** - * Named argument expression that represents function argument with name. - */ +/** Named argument expression that represents function argument with name. */ @RequiredArgsConstructor @Getter @EqualsAndHashCode diff --git a/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java b/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java index 26996eb93d7..febd468e723 100644 --- a/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/NamedExpression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import com.google.common.base.Strings; @@ -16,9 +15,8 @@ import org.opensearch.sql.expression.env.Environment; /** - * Named expression that represents expression with name. - * Please see more details in associated unresolved expression operator - * {@link org.opensearch.sql.ast.expression.Alias}. + * Named expression that represents expression with name. Please see more details in associated + * unresolved expression operator {@link org.opensearch.sql.ast.expression.Alias}. */ @AllArgsConstructor @EqualsAndHashCode @@ -26,19 +24,13 @@ @RequiredArgsConstructor public class NamedExpression implements Expression { - /** - * Expression name. - */ + /** Expression name. */ private final String name; - /** - * Expression that being named. - */ + /** Expression that being named. */ private final Expression delegated; - /** - * Optional alias. - */ + /** Optional alias. */ private String alias; @Override @@ -53,7 +45,8 @@ public ExprType type() { /** * Get expression name using name or its alias (if it's present). - * @return expression name + * + * @return expression name */ public String getNameOrAlias() { return Strings.isNullOrEmpty(alias) ? name : alias; @@ -68,5 +61,4 @@ public T accept(ExpressionNodeVisitor visitor, C context) { public String toString() { return getNameOrAlias(); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/ReferenceExpression.java b/core/src/main/java/org/opensearch/sql/expression/ReferenceExpression.java index 3c5b2af23cb..67a916a7863 100644 --- a/core/src/main/java/org/opensearch/sql/expression/ReferenceExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/ReferenceExpression.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import static org.opensearch.sql.utils.ExpressionUtils.PATH_SEP; @@ -22,16 +21,15 @@ @EqualsAndHashCode @RequiredArgsConstructor public class ReferenceExpression implements Expression { - @Getter - private final String attr; + @Getter private final String attr; - @Getter - private final List paths; + @Getter private final List paths; private final ExprType type; /** * Constructor of ReferenceExpression. + * * @param ref the field name. e.g. addr.state/addr. * @param type type. */ @@ -63,34 +61,17 @@ public String toString() { } /** - * Resolve the ExprValue from {@link ExprTupleValue} using paths. - * Considering the following sample data. - * { - * "name": "bob smith" - * "project.year": 1990, - * "project": { - * "year": "2020" - * } - * "address": { - * "state": "WA", - * "city": "seattle", - * "project.year": 1990 - * } - * "address.local": { - * "state": "WA", - * } - * } - * The paths could be - * 1. top level, e.g. "name", which will be resolved as "bob smith" - * 2. multiple paths, e.g. "name.address.state", which will be resolved as "WA" - * 3. special case, the "." is the path separator, but it is possible that the path include - * ".", for handling this use case, we define the resolve rule as bellow, e.g. "project.year" is - * resolved as 1990 instead of 2020. Note. This logic only applied top level none object field. - * e.g. "address.local.state" been resolved to Missing. but "address.project.year" could been - * resolved as 1990. + * Resolve the ExprValue from {@link ExprTupleValue} using paths. Considering the following sample + * data. { "name": "bob smith" "project.year": 1990, "project": { "year": "2020" } "address": { + * "state": "WA", "city": "seattle", "project.year": 1990 } "address.local": { "state": "WA", } } + * The paths could be 1. top level, e.g. "name", which will be resolved as "bob smith" 2. multiple + * paths, e.g. "name.address.state", which will be resolved as "WA" 3. special case, the "." is + * the path separator, but it is possible that the path include ".", for handling this use case, + * we define the resolve rule as bellow, e.g. "project.year" is resolved as 1990 instead of 2020. + * Note. This logic only applied top level none object field. e.g. "address.local.state" been + * resolved to Missing. but "address.project.year" could been resolved as 1990. * - *

Resolve Rule - * 1. Resolve the full name by combine the paths("x"."y"."z") as whole ("x.y.z"). + *

Resolve Rule 1. Resolve the full name by combine the paths("x"."y"."z") as whole ("x.y.z"). * 2. Resolve the path recursively through ExprValue. * * @param value {@link ExprTupleValue}. diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregationState.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregationState.java index 345c6c00dd1..478e3caf54c 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregationState.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregationState.java @@ -3,18 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.storage.bindingtuple.BindingTuple; -/** - * Maintain the state when {@link Aggregator} iterate on the {@link BindingTuple}. - */ +/** Maintain the state when {@link Aggregator} iterate on the {@link BindingTuple}. */ public interface AggregationState { - /** - * Get {@link ExprValue} result. - */ + /** Get {@link ExprValue} result. */ ExprValue result(); } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/Aggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/Aggregator.java index a122ea6540e..a2a3ce76c35 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/Aggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/Aggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import java.util.List; @@ -26,36 +25,34 @@ import org.opensearch.sql.storage.bindingtuple.BindingTuple; /** - * Aggregator which will iterate on the {@link BindingTuple}s to aggregate the result. - * The Aggregator is not well fit into Expression, because it has side effect. - * But we still want to make it implement {@link Expression} interface to make - * {@link ExpressionAnalyzer} easier. + * Aggregator which will iterate on the {@link BindingTuple}s to aggregate the result. The + * Aggregator is not well fit into Expression, because it has side effect. But we still want to make + * it implement {@link Expression} interface to make {@link ExpressionAnalyzer} easier. */ @EqualsAndHashCode @RequiredArgsConstructor public abstract class Aggregator implements FunctionImplementation, Expression { - @Getter - private final FunctionName functionName; - @Getter - private final List arguments; + @Getter private final FunctionName functionName; + @Getter private final List arguments; protected final ExprCoreType returnType; + @Setter @Getter @Accessors(fluent = true) protected Expression condition; + @Setter @Getter @Accessors(fluent = true) protected Boolean distinct = false; - /** - * Create an {@link AggregationState} which will be used for aggregation. - */ + /** Create an {@link AggregationState} which will be used for aggregation. */ public abstract S create(); /** * Iterate on {@link ExprValue}. + * * @param value {@link ExprValue} * @param state {@link AggregationState} * @return {@link AggregationState} @@ -63,9 +60,9 @@ public abstract class Aggregator protected abstract S iterate(ExprValue value, S state); /** - * Let the aggregator iterate on the {@link BindingTuple} - * To filter out ExprValues that are missing, null or cannot satisfy {@link #condition} - * Before the specific aggregator iterating ExprValue in the tuple. + * Let the aggregator iterate on the {@link BindingTuple} To filter out ExprValues that are + * missing, null or cannot satisfy {@link #condition} Before the specific aggregator iterating + * ExprValue in the tuple. * * @param tuple {@link BindingTuple} * @param state {@link AggregationState} @@ -95,14 +92,11 @@ public T accept(ExpressionNodeVisitor visitor, C context) { return visitor.visitAggregator(this, context); } - /** - * Util method to get value of condition in aggregation filter. - */ + /** Util method to get value of condition in aggregation filter. */ public boolean conditionValue(BindingTuple tuple) { if (condition == null) { return true; } return ExprValueUtils.getBooleanValue(condition.valueOf(tuple)); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregatorFunction.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregatorFunction.java index a24eeca1c11..944bb7b50f8 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregatorFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/AggregatorFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.data.type.ExprCoreType.ARRAY; @@ -35,12 +34,9 @@ import org.opensearch.sql.expression.function.FunctionSignature; /** - * The definition of aggregator function - * avg, Accepts two numbers and produces a number. - * sum, Accepts two numbers and produces a number. - * max, Accepts two numbers and produces a number. - * min, Accepts two numbers and produces a number. - * count, Accepts two numbers and produces a number. + * The definition of aggregator function avg, Accepts two numbers and produces a number. sum, + * Accepts two numbers and produces a number. max, Accepts two numbers and produces a number. min, + * Accepts two numbers and produces a number. count, Accepts two numbers and produces a number. */ @UtilityClass public class AggregatorFunction { @@ -67,26 +63,37 @@ private static DefaultFunctionResolver avg() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> new AvgAggregator(arguments, DOUBLE)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATE)), (functionProperties, arguments) -> new AvgAggregator(arguments, DATE)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATETIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATETIME)), (functionProperties, arguments) -> new AvgAggregator(arguments, DATETIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIME)), (functionProperties, arguments) -> new AvgAggregator(arguments, TIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), (functionProperties, arguments) -> new AvgAggregator(arguments, TIMESTAMP)) - .build() - ); + .build()); } private static DefaultFunctionResolver count() { FunctionName functionName = BuiltinFunctionName.COUNT.getName(); - DefaultFunctionResolver functionResolver = new DefaultFunctionResolver(functionName, - ExprCoreType.coreTypes().stream().collect(Collectors.toMap( - type -> new FunctionSignature(functionName, Collections.singletonList(type)), - type -> (functionProperties, arguments) -> new CountAggregator(arguments, INTEGER)))); + DefaultFunctionResolver functionResolver = + new DefaultFunctionResolver( + functionName, + ExprCoreType.coreTypes().stream() + .collect( + Collectors.toMap( + type -> + new FunctionSignature(functionName, Collections.singletonList(type)), + type -> + (functionProperties, arguments) -> + new CountAggregator(arguments, INTEGER)))); return functionResolver; } @@ -95,16 +102,19 @@ private static DefaultFunctionResolver sum() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(INTEGER)), + .put( + new FunctionSignature(functionName, Collections.singletonList(INTEGER)), (functionProperties, arguments) -> new SumAggregator(arguments, INTEGER)) - .put(new FunctionSignature(functionName, Collections.singletonList(LONG)), + .put( + new FunctionSignature(functionName, Collections.singletonList(LONG)), (functionProperties, arguments) -> new SumAggregator(arguments, LONG)) - .put(new FunctionSignature(functionName, Collections.singletonList(FLOAT)), + .put( + new FunctionSignature(functionName, Collections.singletonList(FLOAT)), (functionProperties, arguments) -> new SumAggregator(arguments, FLOAT)) - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> new SumAggregator(arguments, DOUBLE)) - .build() - ); + .build()); } private static DefaultFunctionResolver min() { @@ -112,23 +122,32 @@ private static DefaultFunctionResolver min() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(INTEGER)), + .put( + new FunctionSignature(functionName, Collections.singletonList(INTEGER)), (functionProperties, arguments) -> new MinAggregator(arguments, INTEGER)) - .put(new FunctionSignature(functionName, Collections.singletonList(LONG)), + .put( + new FunctionSignature(functionName, Collections.singletonList(LONG)), (functionProperties, arguments) -> new MinAggregator(arguments, LONG)) - .put(new FunctionSignature(functionName, Collections.singletonList(FLOAT)), + .put( + new FunctionSignature(functionName, Collections.singletonList(FLOAT)), (functionProperties, arguments) -> new MinAggregator(arguments, FLOAT)) - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> new MinAggregator(arguments, DOUBLE)) - .put(new FunctionSignature(functionName, Collections.singletonList(STRING)), + .put( + new FunctionSignature(functionName, Collections.singletonList(STRING)), (functionProperties, arguments) -> new MinAggregator(arguments, STRING)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATE)), (functionProperties, arguments) -> new MinAggregator(arguments, DATE)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATETIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATETIME)), (functionProperties, arguments) -> new MinAggregator(arguments, DATETIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIME)), (functionProperties, arguments) -> new MinAggregator(arguments, TIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), (functionProperties, arguments) -> new MinAggregator(arguments, TIMESTAMP)) .build()); } @@ -138,26 +157,34 @@ private static DefaultFunctionResolver max() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(INTEGER)), + .put( + new FunctionSignature(functionName, Collections.singletonList(INTEGER)), (functionProperties, arguments) -> new MaxAggregator(arguments, INTEGER)) - .put(new FunctionSignature(functionName, Collections.singletonList(LONG)), + .put( + new FunctionSignature(functionName, Collections.singletonList(LONG)), (functionProperties, arguments) -> new MaxAggregator(arguments, LONG)) - .put(new FunctionSignature(functionName, Collections.singletonList(FLOAT)), + .put( + new FunctionSignature(functionName, Collections.singletonList(FLOAT)), (functionProperties, arguments) -> new MaxAggregator(arguments, FLOAT)) - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> new MaxAggregator(arguments, DOUBLE)) - .put(new FunctionSignature(functionName, Collections.singletonList(STRING)), + .put( + new FunctionSignature(functionName, Collections.singletonList(STRING)), (functionProperties, arguments) -> new MaxAggregator(arguments, STRING)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATE)), (functionProperties, arguments) -> new MaxAggregator(arguments, DATE)) - .put(new FunctionSignature(functionName, Collections.singletonList(DATETIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DATETIME)), (functionProperties, arguments) -> new MaxAggregator(arguments, DATETIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIME)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIME)), (functionProperties, arguments) -> new MaxAggregator(arguments, TIME)) - .put(new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), + .put( + new FunctionSignature(functionName, Collections.singletonList(TIMESTAMP)), (functionProperties, arguments) -> new MaxAggregator(arguments, TIMESTAMP)) - .build() - ); + .build()); } private static DefaultFunctionResolver varSamp() { @@ -165,10 +192,10 @@ private static DefaultFunctionResolver varSamp() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> varianceSample(arguments, DOUBLE)) - .build() - ); + .build()); } private static DefaultFunctionResolver varPop() { @@ -176,10 +203,10 @@ private static DefaultFunctionResolver varPop() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> variancePopulation(arguments, DOUBLE)) - .build() - ); + .build()); } private static DefaultFunctionResolver stddevSamp() { @@ -187,10 +214,10 @@ private static DefaultFunctionResolver stddevSamp() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> stddevSample(arguments, DOUBLE)) - .build() - ); + .build()); } private static DefaultFunctionResolver stddevPop() { @@ -198,20 +225,22 @@ private static DefaultFunctionResolver stddevPop() { return new DefaultFunctionResolver( functionName, new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), + .put( + new FunctionSignature(functionName, Collections.singletonList(DOUBLE)), (functionProperties, arguments) -> stddevPopulation(arguments, DOUBLE)) - .build() - ); + .build()); } private static DefaultFunctionResolver take() { FunctionName functionName = BuiltinFunctionName.TAKE.getName(); - DefaultFunctionResolver functionResolver = new DefaultFunctionResolver(functionName, - new ImmutableMap.Builder() - .put(new FunctionSignature(functionName, ImmutableList.of(STRING, INTEGER)), - (functionProperties, arguments) -> new TakeAggregator(arguments, ARRAY)) - .build()); + DefaultFunctionResolver functionResolver = + new DefaultFunctionResolver( + functionName, + new ImmutableMap.Builder() + .put( + new FunctionSignature(functionName, ImmutableList.of(STRING, INTEGER)), + (functionProperties, arguments) -> new TakeAggregator(arguments, ARRAY)) + .build()); return functionResolver; } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/AvgAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/AvgAggregator.java index a899a6b45b6..c5289680184 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/AvgAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/AvgAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static java.time.temporal.ChronoUnit.MILLIS; @@ -27,14 +26,14 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; /** - * The average aggregator aggregate the value evaluated by the expression. - * If the expression evaluated result is NULL or MISSING, then the result is NULL. + * The average aggregator aggregate the value evaluated by the expression. If the expression + * evaluated result is NULL or MISSING, then the result is NULL. */ public class AvgAggregator extends Aggregator { /** - * To process by different ways different data types, we need to store the type. - * Input data has the same type as the result. + * To process by different ways different data types, we need to store the type. Input data has + * the same type as the result. */ private final ExprCoreType dataType; @@ -56,7 +55,7 @@ public AvgState create() { return new TimeAvgState(); case DOUBLE: return new DoubleAvgState(); - default: //unreachable code - we don't expose signatures for unsupported types + default: // unreachable code - we don't expose signatures for unsupported types throw new IllegalArgumentException( String.format("avg aggregation over %s type is not supported", dataType)); } @@ -72,9 +71,7 @@ public String toString() { return String.format(Locale.ROOT, "avg(%s)", format(getArguments())); } - /** - * Average State. - */ + /** Average State. */ protected abstract static class AvgState implements AggregationState { protected ExprValue count; protected ExprValue total; @@ -117,15 +114,16 @@ public ExprValue result() { } return new ExprDateValue( - new ExprTimestampValue(Instant.ofEpochMilli( - DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())) - .dateValue()); + new ExprTimestampValue( + Instant.ofEpochMilli( + DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())) + .dateValue()); } @Override protected AvgState iterate(ExprValue value) { - total = DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())) - .valueOf(); + total = + DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())).valueOf(); return super.iterate(value); } } @@ -138,15 +136,16 @@ public ExprValue result() { } return new ExprDatetimeValue( - new ExprTimestampValue(Instant.ofEpochMilli( - DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())) - .datetimeValue()); + new ExprTimestampValue( + Instant.ofEpochMilli( + DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())) + .datetimeValue()); } @Override protected AvgState iterate(ExprValue value) { - total = DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())) - .valueOf(); + total = + DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())).valueOf(); return super.iterate(value); } } @@ -158,14 +157,15 @@ public ExprValue result() { return ExprNullValue.of(); } - return new ExprTimestampValue(Instant.ofEpochMilli( - DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())); + return new ExprTimestampValue( + Instant.ofEpochMilli( + DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue())); } @Override protected AvgState iterate(ExprValue value) { - total = DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())) - .valueOf(); + total = + DSL.add(DSL.literal(total), DSL.literal(value.timestampValue().toEpochMilli())).valueOf(); return super.iterate(value); } } @@ -177,14 +177,16 @@ public ExprValue result() { return ExprNullValue.of(); } - return new ExprTimeValue(LocalTime.MIN.plus( - DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue(), MILLIS)); + return new ExprTimeValue( + LocalTime.MIN.plus( + DSL.divide(DSL.literal(total), DSL.literal(count)).valueOf().longValue(), MILLIS)); } @Override protected AvgState iterate(ExprValue value) { - total = DSL.add(DSL.literal(total), - DSL.literal(MILLIS.between(LocalTime.MIN, value.timeValue()))).valueOf(); + total = + DSL.add(DSL.literal(total), DSL.literal(MILLIS.between(LocalTime.MIN, value.timeValue()))) + .valueOf(); return super.iterate(value); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/CountAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/CountAggregator.java index 813842cadc9..c4c02eb1d3e 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/CountAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/CountAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.utils.ExpressionUtils.format; @@ -43,9 +42,7 @@ public String toString() { : String.format(Locale.ROOT, "count(%s)", format(getArguments())); } - /** - * Count State. - */ + /** Count State. */ protected static class CountState implements AggregationState { protected int count; diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/MaxAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/MaxAggregator.java index e9123c0ac20..863d4603a7e 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/MaxAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/MaxAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; @@ -45,9 +44,7 @@ protected static class MaxState implements AggregationState { } public void max(ExprValue value) { - maxResult = maxResult.isNull() ? value - : (maxResult.compareTo(value) > 0) - ? maxResult : value; + maxResult = maxResult.isNull() ? value : (maxResult.compareTo(value) > 0) ? maxResult : value; } @Override diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/MinAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/MinAggregator.java index 897fe857ffe..c1b9ebcd4fb 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/MinAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/MinAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; @@ -16,8 +15,8 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; /** - * The minimum aggregator aggregate the value evaluated by the expression. - * If the expression evaluated result is NULL or MISSING, then the result is NULL. + * The minimum aggregator aggregate the value evaluated by the expression. If the expression + * evaluated result is NULL or MISSING, then the result is NULL. */ public class MinAggregator extends Aggregator { @@ -25,7 +24,6 @@ public MinAggregator(List arguments, ExprCoreType returnType) { super(BuiltinFunctionName.MIN.getName(), arguments, returnType); } - @Override public MinState create() { return new MinState(); @@ -50,9 +48,7 @@ protected static class MinState implements AggregationState { } public void min(ExprValue value) { - minResult = minResult.isNull() ? value - : (minResult.compareTo(value) < 0) - ? minResult : value; + minResult = minResult.isNull() ? value : (minResult.compareTo(value) < 0) ? minResult : value; } @Override diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java index 510c5d1e454..1c10af5fc7a 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import lombok.EqualsAndHashCode; @@ -12,35 +11,26 @@ import org.opensearch.sql.expression.ExpressionNodeVisitor; /** - * NamedAggregator expression that represents expression with name. - * Please see more details in associated unresolved expression operator - * {@link org.opensearch.sql.ast.expression.Alias}. + * NamedAggregator expression that represents expression with name. Please see more details in + * associated unresolved expression operator {@link org.opensearch.sql.ast.expression.Alias}. */ @EqualsAndHashCode(callSuper = false) public class NamedAggregator extends Aggregator { - /** - * Aggregator name. - */ + /** Aggregator name. */ private final String name; - /** - * Aggregator that being named. - */ - @Getter - private final Aggregator delegated; + /** Aggregator that being named. */ + @Getter private final Aggregator delegated; /** - * NamedAggregator. - * The aggregator properties {@link #condition} and {@link #distinct} - * are inherited by named aggregator to avoid errors introduced by the property inconsistency. + * NamedAggregator. The aggregator properties {@link #condition} and {@link #distinct} are + * inherited by named aggregator to avoid errors introduced by the property inconsistency. * * @param name name * @param delegated delegated */ - public NamedAggregator( - String name, - Aggregator delegated) { + public NamedAggregator(String name, Aggregator delegated) { super(delegated.getFunctionName(), delegated.getArguments(), delegated.returnType); this.name = name; this.delegated = delegated; @@ -60,7 +50,8 @@ protected AggregationState iterate(ExprValue value, AggregationState state) { /** * Get expression name using name or its alias (if it's present). - * @return expression name + * + * @return expression name */ public String getName() { return name; @@ -75,5 +66,4 @@ public T accept(ExpressionNodeVisitor visitor, C context) { public String toString() { return getName(); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/StdDevAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/StdDevAggregator.java index 0cd84944492..d5422bc7887 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/StdDevAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/StdDevAggregator.java @@ -26,26 +26,18 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.function.BuiltinFunctionName; -/** - * StandardDeviation Aggregator. - */ +/** StandardDeviation Aggregator. */ public class StdDevAggregator extends Aggregator { private final boolean isSampleStdDev; - /** - * Build Population Variance {@link VarianceAggregator}. - */ - public static Aggregator stddevPopulation(List arguments, - ExprCoreType returnType) { + /** Build Population Variance {@link VarianceAggregator}. */ + public static Aggregator stddevPopulation(List arguments, ExprCoreType returnType) { return new StdDevAggregator(false, arguments, returnType); } - /** - * Build Sample Variance {@link VarianceAggregator}. - */ - public static Aggregator stddevSample(List arguments, - ExprCoreType returnType) { + /** Build Sample Variance {@link VarianceAggregator}. */ + public static Aggregator stddevSample(List arguments, ExprCoreType returnType) { return new StdDevAggregator(true, arguments, returnType); } @@ -53,7 +45,7 @@ public static Aggregator stddevSample(List arguments, * VarianceAggregator constructor. * * @param isSampleStdDev true for sample standard deviation aggregator, false for population - * standard deviation aggregator. + * standard deviation aggregator. * @param arguments aggregator arguments. * @param returnType aggregator return types. */ @@ -74,8 +66,8 @@ public StdDevAggregator.StdDevState create() { } @Override - protected StdDevAggregator.StdDevState iterate(ExprValue value, - StdDevAggregator.StdDevState state) { + protected StdDevAggregator.StdDevState iterate( + ExprValue value, StdDevAggregator.StdDevState state) { state.evaluate(value); return state; } diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/SumAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/SumAggregator.java index f5b042034a2..d6377219808 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/SumAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/SumAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.data.model.ExprValueUtils.doubleValue; @@ -28,8 +27,8 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; /** - * The sum aggregator aggregate the value evaluated by the expression. - * If the expression evaluated result is NULL or MISSING, then the result is NULL. + * The sum aggregator aggregate the value evaluated by the expression. If the expression evaluated + * result is NULL or MISSING, then the result is NULL. */ public class SumAggregator extends Aggregator { @@ -54,9 +53,7 @@ public String toString() { return String.format(Locale.ROOT, "sum(%s)", format(getArguments())); } - /** - * Sum State. - */ + /** Sum State. */ protected static class SumState implements AggregationState { private final ExprCoreType type; @@ -69,9 +66,7 @@ protected static class SumState implements AggregationState { isEmptyCollection = true; } - /** - * Add value to current sumResult. - */ + /** Add value to current sumResult. */ public void add(ExprValue value) { switch (type) { case INTEGER: diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/TakeAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/TakeAggregator.java index cff08bb0988..8791973353c 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/TakeAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/TakeAggregator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.opensearch.sql.utils.ExpressionUtils.format; @@ -18,8 +17,8 @@ import org.opensearch.sql.expression.function.BuiltinFunctionName; /** - * The take aggregator keeps and returns the original values of a field. - * If the field value is NULL or MISSING, then it is skipped. + * The take aggregator keeps and returns the original values of a field. If the field value is NULL + * or MISSING, then it is skipped. */ public class TakeAggregator extends Aggregator { @@ -43,9 +42,7 @@ public String toString() { return String.format(Locale.ROOT, "take(%s)", format(getArguments())); } - /** - * Take State. - */ + /** Take State. */ protected static class TakeState implements AggregationState { protected int index; protected int size; diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/VarianceAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/VarianceAggregator.java index bd9f0948f61..920830d2667 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/VarianceAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/VarianceAggregator.java @@ -26,26 +26,18 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.function.BuiltinFunctionName; -/** - * Variance Aggregator. - */ +/** Variance Aggregator. */ public class VarianceAggregator extends Aggregator { private final boolean isSampleVariance; - /** - * Build Population Variance {@link VarianceAggregator}. - */ - public static Aggregator variancePopulation(List arguments, - ExprCoreType returnType) { + /** Build Population Variance {@link VarianceAggregator}. */ + public static Aggregator variancePopulation(List arguments, ExprCoreType returnType) { return new VarianceAggregator(false, arguments, returnType); } - /** - * Build Sample Variance {@link VarianceAggregator}. - */ - public static Aggregator varianceSample(List arguments, - ExprCoreType returnType) { + /** Build Sample Variance {@link VarianceAggregator}. */ + public static Aggregator varianceSample(List arguments, ExprCoreType returnType) { return new VarianceAggregator(true, arguments, returnType); } diff --git a/core/src/main/java/org/opensearch/sql/expression/conditional/cases/CaseClause.java b/core/src/main/java/org/opensearch/sql/expression/conditional/cases/CaseClause.java index ad7860a6dc5..e18f33c6aee 100644 --- a/core/src/main/java/org/opensearch/sql/expression/conditional/cases/CaseClause.java +++ b/core/src/main/java/org/opensearch/sql/expression/conditional/cases/CaseClause.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.conditional.cases; import static org.opensearch.sql.data.type.ExprCoreType.UNDEFINED; @@ -32,19 +31,13 @@ @ToString public class CaseClause extends FunctionExpression { - /** - * List of WHEN clauses. - */ + /** List of WHEN clauses. */ private final List whenClauses; - /** - * Default result if none of WHEN conditions match. - */ + /** Default result if none of WHEN conditions match. */ private final Expression defaultResult; - /** - * Initialize case clause. - */ + /** Initialize case clause. */ public CaseClause(List whenClauses, Expression defaultResult) { super(FunctionName.of("case"), concatArgs(whenClauses, defaultResult)); this.whenClauses = whenClauses; @@ -75,15 +68,13 @@ public T accept(ExpressionNodeVisitor visitor, C context) { } /** - * Get types of each result in WHEN clause and ELSE clause. - * Exclude UNKNOWN type from NULL literal which means NULL in THEN or ELSE clause - * is not included in result. + * Get types of each result in WHEN clause and ELSE clause. Exclude UNKNOWN type from NULL literal + * which means NULL in THEN or ELSE clause is not included in result. + * * @return all result types. Use list so caller can generate friendly error message. */ public List allResultTypes() { - List types = whenClauses.stream() - .map(WhenClause::type) - .collect(Collectors.toList()); + List types = whenClauses.stream().map(WhenClause::type).collect(Collectors.toList()); if (defaultResult != null) { types.add(defaultResult.type()); } @@ -92,8 +83,8 @@ public List allResultTypes() { return types; } - private static List concatArgs(List whenClauses, - Expression defaultResult) { + private static List concatArgs( + List whenClauses, Expression defaultResult) { ImmutableList.Builder args = ImmutableList.builder(); whenClauses.forEach(args::add); @@ -102,5 +93,4 @@ private static List concatArgs(List whenClauses, } return args.build(); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/conditional/cases/WhenClause.java b/core/src/main/java/org/opensearch/sql/expression/conditional/cases/WhenClause.java index fd2eeab9830..7eb731a1f20 100644 --- a/core/src/main/java/org/opensearch/sql/expression/conditional/cases/WhenClause.java +++ b/core/src/main/java/org/opensearch/sql/expression/conditional/cases/WhenClause.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.conditional.cases; import com.google.common.collect.ImmutableList; @@ -18,27 +17,19 @@ import org.opensearch.sql.expression.env.Environment; import org.opensearch.sql.expression.function.FunctionName; -/** - * WHEN clause that consists of a condition and a result corresponding. - */ +/** WHEN clause that consists of a condition and a result corresponding. */ @EqualsAndHashCode(callSuper = false) @Getter @ToString public class WhenClause extends FunctionExpression { - /** - * Condition that must be a predicate. - */ + /** Condition that must be a predicate. */ private final Expression condition; - /** - * Result to return if condition is evaluated to true. - */ + /** Result to return if condition is evaluated to true. */ private final Expression result; - /** - * Initialize when clause. - */ + /** Initialize when clause. */ public WhenClause(Expression condition, Expression result) { super(FunctionName.of("when"), ImmutableList.of(condition, result)); this.condition = condition; @@ -47,8 +38,9 @@ public WhenClause(Expression condition, Expression result) { /** * Evaluate when condition. - * @param valueEnv value env - * @return is condition satisfied + * + * @param valueEnv value env + * @return is condition satisfied */ public boolean isTrue(Environment valueEnv) { ExprValue result = condition.valueOf(valueEnv); @@ -72,5 +64,4 @@ public ExprType type() { public T accept(ExpressionNodeVisitor visitor, C context) { return visitor.visitWhen(this, context); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/datetime/CalendarLookup.java b/core/src/main/java/org/opensearch/sql/expression/datetime/CalendarLookup.java index c5b63439915..0837075f7cf 100644 --- a/core/src/main/java/org/opensearch/sql/expression/datetime/CalendarLookup.java +++ b/core/src/main/java/org/opensearch/sql/expression/datetime/CalendarLookup.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import com.google.common.collect.ImmutableList; @@ -18,6 +17,7 @@ class CalendarLookup { /** * Get a calendar for the specific mode. + * * @param mode Mode to get calendar for. * @param date Date to get calendar for. */ @@ -38,6 +38,7 @@ private static Calendar getCalendar(int mode, LocalDate date) { /** * Set first day of week, minimal days in first week and date in calendar. + * * @param firstDayOfWeek the given first day of the week. * @param minimalDaysInWeek the given minimal days required in the first week of the year. * @param date the given date. @@ -52,6 +53,7 @@ private static Calendar getCalendar(int firstDayOfWeek, int minimalDaysInWeek, L /** * Returns week number for date according to mode. + * * @param mode Integer for mode. Valid mode values are 0 to 7. * @param date LocalDate for date. */ @@ -68,6 +70,7 @@ static int getWeekNumber(int mode, LocalDate date) { /** * Returns year for date according to mode. + * * @param mode Integer for mode. Valid mode values are 0 to 7. * @param date LocalDate for date. */ diff --git a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFormatterUtil.java b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFormatterUtil.java index 55bfa67f3f6..339ebe4b2d9 100644 --- a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFormatterUtil.java +++ b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFormatterUtil.java @@ -28,8 +28,8 @@ import org.opensearch.sql.expression.function.FunctionProperties; /** - * This class converts a SQL style DATE_FORMAT format specifier and converts it to a - * Java SimpleDateTime format. + * This class converts a SQL style DATE_FORMAT format specifier and converts it to a Java + * SimpleDateTime format. */ class DateTimeFormatterUtil { private static final int SUFFIX_SPECIAL_START_TH = 11; @@ -39,8 +39,7 @@ class DateTimeFormatterUtil { private static final String NANO_SEC_FORMAT = "'%06d'"; private static final Map SUFFIX_CONVERTER = - ImmutableMap.builder() - .put(1, "st").put(2, "nd").put(3, "rd").build(); + ImmutableMap.builder().put(1, "st").put(2, "nd").put(3, "rd").build(); // The following have special cases that need handling outside of the format options provided // by the DateTimeFormatter class. @@ -50,52 +49,70 @@ interface DateTimeFormatHandler { private static final Map DATE_HANDLERS = ImmutableMap.builder() - .put("%a", (date) -> "EEE") // %a => EEE - Abbreviated weekday name (Sun..Sat) - .put("%b", (date) -> "LLL") // %b => LLL - Abbreviated month name (Jan..Dec) - .put("%c", (date) -> "MM") // %c => MM - Month, numeric (0..12) - .put("%d", (date) -> "dd") // %d => dd - Day of the month, numeric (00..31) - .put("%e", (date) -> "d") // %e => d - Day of the month, numeric (0..31) - .put("%H", (date) -> "HH") // %H => HH - (00..23) - .put("%h", (date) -> "hh") // %h => hh - (01..12) - .put("%I", (date) -> "hh") // %I => hh - (01..12) - .put("%i", (date) -> "mm") // %i => mm - Minutes, numeric (00..59) - .put("%j", (date) -> "DDD") // %j => DDD - (001..366) - .put("%k", (date) -> "H") // %k => H - (0..23) - .put("%l", (date) -> "h") // %l => h - (1..12) - .put("%p", (date) -> "a") // %p => a - AM or PM - .put("%M", (date) -> "LLLL") // %M => LLLL - Month name (January..December) - .put("%m", (date) -> "MM") // %m => MM - Month, numeric (00..12) - .put("%r", (date) -> "hh:mm:ss a") // %r => hh:mm:ss a - hh:mm:ss followed by AM or PM - .put("%S", (date) -> "ss") // %S => ss - Seconds (00..59) - .put("%s", (date) -> "ss") // %s => ss - Seconds (00..59) - .put("%T", (date) -> "HH:mm:ss") // %T => HH:mm:ss - .put("%W", (date) -> "EEEE") // %W => EEEE - Weekday name (Sunday..Saturday) - .put("%Y", (date) -> "yyyy") // %Y => yyyy - Year, numeric, 4 digits - .put("%y", (date) -> "yy") // %y => yy - Year, numeric, 2 digits - // The following are not directly supported by DateTimeFormatter. - .put("%D", (date) -> // %w - Day of month with English suffix - String.format("'%d%s'", date.getDayOfMonth(), getSuffix(date.getDayOfMonth()))) - .put("%f", (date) -> // %f - Microseconds - String.format(NANO_SEC_FORMAT, (date.getNano() / 1000))) - .put("%w", (date) -> // %w - Day of week (0 indexed) - String.format("'%d'", date.getDayOfWeek().getValue())) - .put("%U", (date) -> // %U Week where Sunday is the first day - WEEK() mode 0 - String.format("'%d'", CalendarLookup.getWeekNumber(0, date.toLocalDate()))) - .put("%u", (date) -> // %u Week where Monday is the first day - WEEK() mode 1 - String.format("'%d'", CalendarLookup.getWeekNumber(1, date.toLocalDate()))) - .put("%V", (date) -> // %V Week where Sunday is the first day - WEEK() mode 2 used with %X - String.format("'%d'", CalendarLookup.getWeekNumber(2, date.toLocalDate()))) - .put("%v", (date) -> // %v Week where Monday is the first day - WEEK() mode 3 used with %x - String.format("'%d'", CalendarLookup.getWeekNumber(3, date.toLocalDate()))) - .put("%X", (date) -> // %X Year for week where Sunday is the first day, 4 digits used with %V - String.format("'%d'", CalendarLookup.getYearNumber(2, date.toLocalDate()))) - .put("%x", (date) -> // %x Year for week where Monday is the first day, 4 digits used with %v - String.format("'%d'", CalendarLookup.getYearNumber(3, date.toLocalDate()))) - .build(); - - //Handlers for the time_format function. - //Some format specifiers return 0 or null to align with MySQL. - //https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format + .put("%a", (date) -> "EEE") // %a => EEE - Abbreviated weekday name (Sun..Sat) + .put("%b", (date) -> "LLL") // %b => LLL - Abbreviated month name (Jan..Dec) + .put("%c", (date) -> "MM") // %c => MM - Month, numeric (0..12) + .put("%d", (date) -> "dd") // %d => dd - Day of the month, numeric (00..31) + .put("%e", (date) -> "d") // %e => d - Day of the month, numeric (0..31) + .put("%H", (date) -> "HH") // %H => HH - (00..23) + .put("%h", (date) -> "hh") // %h => hh - (01..12) + .put("%I", (date) -> "hh") // %I => hh - (01..12) + .put("%i", (date) -> "mm") // %i => mm - Minutes, numeric (00..59) + .put("%j", (date) -> "DDD") // %j => DDD - (001..366) + .put("%k", (date) -> "H") // %k => H - (0..23) + .put("%l", (date) -> "h") // %l => h - (1..12) + .put("%p", (date) -> "a") // %p => a - AM or PM + .put("%M", (date) -> "LLLL") // %M => LLLL - Month name (January..December) + .put("%m", (date) -> "MM") // %m => MM - Month, numeric (00..12) + .put("%r", (date) -> "hh:mm:ss a") // %r => hh:mm:ss a - hh:mm:ss followed by AM or PM + .put("%S", (date) -> "ss") // %S => ss - Seconds (00..59) + .put("%s", (date) -> "ss") // %s => ss - Seconds (00..59) + .put("%T", (date) -> "HH:mm:ss") // %T => HH:mm:ss + .put("%W", (date) -> "EEEE") // %W => EEEE - Weekday name (Sunday..Saturday) + .put("%Y", (date) -> "yyyy") // %Y => yyyy - Year, numeric, 4 digits + .put("%y", (date) -> "yy") // %y => yy - Year, numeric, 2 digits + // The following are not directly supported by DateTimeFormatter. + .put( + "%D", + (date) -> // %w - Day of month with English suffix + String.format("'%d%s'", date.getDayOfMonth(), getSuffix(date.getDayOfMonth()))) + .put( + "%f", + (date) -> // %f - Microseconds + String.format(NANO_SEC_FORMAT, (date.getNano() / 1000))) + .put( + "%w", + (date) -> // %w - Day of week (0 indexed) + String.format("'%d'", date.getDayOfWeek().getValue())) + .put( + "%U", + (date) -> // %U Week where Sunday is the first day - WEEK() mode 0 + String.format("'%d'", CalendarLookup.getWeekNumber(0, date.toLocalDate()))) + .put( + "%u", + (date) -> // %u Week where Monday is the first day - WEEK() mode 1 + String.format("'%d'", CalendarLookup.getWeekNumber(1, date.toLocalDate()))) + .put( + "%V", + (date) -> // %V Week where Sunday is the first day - WEEK() mode 2 used with %X + String.format("'%d'", CalendarLookup.getWeekNumber(2, date.toLocalDate()))) + .put( + "%v", + (date) -> // %v Week where Monday is the first day - WEEK() mode 3 used with %x + String.format("'%d'", CalendarLookup.getWeekNumber(3, date.toLocalDate()))) + .put( + "%X", + (date) -> // %X Year for week where Sunday is the first day, 4 digits used with %V + String.format("'%d'", CalendarLookup.getYearNumber(2, date.toLocalDate()))) + .put( + "%x", + (date) -> // %x Year for week where Monday is the first day, 4 digits used with %v + String.format("'%d'", CalendarLookup.getYearNumber(3, date.toLocalDate()))) + .build(); + + // Handlers for the time_format function. + // Some format specifiers return 0 or null to align with MySQL. + // https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format private static final Map TIME_HANDLERS = ImmutableMap.builder() .put("%a", (date) -> null) @@ -121,7 +138,9 @@ interface DateTimeFormatHandler { .put("%Y", (date) -> "0000") .put("%y", (date) -> "00") .put("%D", (date) -> null) - .put("%f", (date) -> // %f - Microseconds + .put( + "%f", + (date) -> // %f - Microseconds String.format(NANO_SEC_FORMAT, (date.getNano() / 1000))) .put("%w", (date) -> null) .put("%U", (date) -> null) @@ -157,7 +176,7 @@ interface DateTimeFormatHandler { .put("%Y", "u") // %Y => yyyy - Year, numeric, 4 digits .put("%y", "u") // %y => yy - Year, numeric, 2 digits .put("%f", "n") // %f => n - Nanoseconds - //The following have been implemented but cannot be aligned with + // The following have been implemented but cannot be aligned with // MySQL due to the limitations of the DatetimeFormatter .put("%D", "d") // %w - Day of month with English suffix .put("%w", "e") // %w - Day of week (0 indexed) @@ -170,20 +189,19 @@ interface DateTimeFormatHandler { .build(); private static final Pattern pattern = Pattern.compile("%."); - private static final Pattern CHARACTERS_WITH_NO_MOD_LITERAL_BEHIND_PATTERN - = Pattern.compile("(? handler, - LocalDateTime datetime) { + static ExprValue getFormattedString( + ExprValue formatExpr, Map handler, LocalDateTime datetime) { StringBuffer cleanFormat = getCleanFormat(formatExpr); final Matcher matcher = pattern.matcher(cleanFormat.toString()); final StringBuffer format = new StringBuffer(); try { while (matcher.find()) { - matcher.appendReplacement(format, - handler.getOrDefault(matcher.group(), (d) -> - String.format("'%s'", matcher.group().replaceFirst(MOD_LITERAL, ""))) + matcher.appendReplacement( + format, + handler + .getOrDefault( + matcher.group(), + (d) -> String.format("'%s'", matcher.group().replaceFirst(MOD_LITERAL, ""))) .getFormat(datetime)); } } catch (Exception e) { @@ -219,12 +240,13 @@ static ExprValue getFormattedString(ExprValue formatExpr, // English Locale matches SQL requirements. // 'AM'/'PM' instead of 'a.m.'/'p.m.' // 'Sat' instead of 'Sat.' etc - return new ExprStringValue(datetime.format( - DateTimeFormatter.ofPattern(format.toString(), Locale.ENGLISH))); + return new ExprStringValue( + datetime.format(DateTimeFormatter.ofPattern(format.toString(), Locale.ENGLISH))); } /** * Format the date using the date format String. + * * @param dateExpr the date ExprValue of Date/Datetime/Timestamp/String type. * @param formatExpr the format ExprValue of String type. * @return Date formatted using format and returned as a String. @@ -242,13 +264,14 @@ static ExprValue getFormattedDateOfToday(ExprValue formatExpr, ExprValue time, C /** * Format the date using the date format String. + * * @param timeExpr the date ExprValue of Date/Datetime/Timestamp/String type. * @param formatExpr the format ExprValue of String type. * @return Date formatted using format and returned as a String. */ static ExprValue getFormattedTime(ExprValue timeExpr, ExprValue formatExpr) { - //Initializes DateTime with LocalDate.now(). This is safe because the date is ignored. - //The time_format function will only return 0 or null for invalid string format specifiers. + // Initializes DateTime with LocalDate.now(). This is safe because the date is ignored. + // The time_format function will only return 0 or null for invalid string format specifiers. final LocalDateTime time = LocalDateTime.of(LocalDate.now(), timeExpr.timeValue()); return getFormattedString(formatExpr, TIME_HANDLERS, time); @@ -266,30 +289,33 @@ private static boolean canGetTime(TemporalAccessor ta) { && ta.isSupported(ChronoField.SECOND_OF_MINUTE)); } - static ExprValue parseStringWithDateOrTime(FunctionProperties fp, - ExprValue datetimeStringExpr, - ExprValue formatExpr) { + static ExprValue parseStringWithDateOrTime( + FunctionProperties fp, ExprValue datetimeStringExpr, ExprValue formatExpr) { - //Replace patterns with % for Java DateTimeFormatter + // Replace patterns with % for Java DateTimeFormatter StringBuffer cleanFormat = getCleanFormat(formatExpr); final Matcher matcher = pattern.matcher(cleanFormat.toString()); final StringBuffer format = new StringBuffer(); while (matcher.find()) { - matcher.appendReplacement(format, - STR_TO_DATE_FORMATS.getOrDefault(matcher.group(), + matcher.appendReplacement( + format, + STR_TO_DATE_FORMATS.getOrDefault( + matcher.group(), String.format("'%s'", matcher.group().replaceFirst(MOD_LITERAL, "")))); } matcher.appendTail(format); TemporalAccessor taWithMissingFields; - //Return NULL for invalid parse in string to align with MySQL + // Return NULL for invalid parse in string to align with MySQL try { - //Get Temporal Accessor to initially parse string without default values - taWithMissingFields = new DateTimeFormatterBuilder() - .appendPattern(format.toString()) - .toFormatter().withResolverStyle(ResolverStyle.STRICT) - .parseUnresolved(datetimeStringExpr.stringValue(), new ParsePosition(0)); + // Get Temporal Accessor to initially parse string without default values + taWithMissingFields = + new DateTimeFormatterBuilder() + .appendPattern(format.toString()) + .toFormatter() + .withResolverStyle(ResolverStyle.STRICT) + .parseUnresolved(datetimeStringExpr.stringValue(), new ParsePosition(0)); if (taWithMissingFields == null) { throw new DateTimeException("Input string could not be parsed properly."); } @@ -300,31 +326,42 @@ static ExprValue parseStringWithDateOrTime(FunctionProperties fp, return ExprNullValue.of(); } - int year = taWithMissingFields.isSupported(ChronoField.YEAR) - ? taWithMissingFields.get(ChronoField.YEAR) : 2000; - - int month = taWithMissingFields.isSupported(ChronoField.MONTH_OF_YEAR) - ? taWithMissingFields.get(ChronoField.MONTH_OF_YEAR) : 1; - - int day = taWithMissingFields.isSupported(ChronoField.DAY_OF_MONTH) - ? taWithMissingFields.get(ChronoField.DAY_OF_MONTH) : 1; - - int hour = taWithMissingFields.isSupported(ChronoField.HOUR_OF_DAY) - ? taWithMissingFields.get(ChronoField.HOUR_OF_DAY) : 0; - - int minute = taWithMissingFields.isSupported(ChronoField.MINUTE_OF_HOUR) - ? taWithMissingFields.get(ChronoField.MINUTE_OF_HOUR) : 0; - - int second = taWithMissingFields.isSupported(ChronoField.SECOND_OF_MINUTE) - ? taWithMissingFields.get(ChronoField.SECOND_OF_MINUTE) : 0; - - //Fill returned datetime with current date if only Time information was parsed + int year = + taWithMissingFields.isSupported(ChronoField.YEAR) + ? taWithMissingFields.get(ChronoField.YEAR) + : 2000; + + int month = + taWithMissingFields.isSupported(ChronoField.MONTH_OF_YEAR) + ? taWithMissingFields.get(ChronoField.MONTH_OF_YEAR) + : 1; + + int day = + taWithMissingFields.isSupported(ChronoField.DAY_OF_MONTH) + ? taWithMissingFields.get(ChronoField.DAY_OF_MONTH) + : 1; + + int hour = + taWithMissingFields.isSupported(ChronoField.HOUR_OF_DAY) + ? taWithMissingFields.get(ChronoField.HOUR_OF_DAY) + : 0; + + int minute = + taWithMissingFields.isSupported(ChronoField.MINUTE_OF_HOUR) + ? taWithMissingFields.get(ChronoField.MINUTE_OF_HOUR) + : 0; + + int second = + taWithMissingFields.isSupported(ChronoField.SECOND_OF_MINUTE) + ? taWithMissingFields.get(ChronoField.SECOND_OF_MINUTE) + : 0; + + // Fill returned datetime with current date if only Time information was parsed LocalDateTime output; if (!canGetDate(taWithMissingFields)) { - output = LocalDateTime.of( - LocalDate.now(fp.getQueryStartClock()), - LocalTime.of(hour, minute, second) - ); + output = + LocalDateTime.of( + LocalDate.now(fp.getQueryStartClock()), LocalTime.of(hour, minute, second)); } else { output = LocalDateTime.of(year, month, day, hour, minute, second); } @@ -334,6 +371,7 @@ static ExprValue parseStringWithDateOrTime(FunctionProperties fp, /** * Returns English suffix of incoming value. + * * @param val Incoming value. * @return English suffix as String (st, nd, rd, th) */ diff --git a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java index cd5ef23d1c0..84c670cb1c7 100644 --- a/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java @@ -3,10 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; - import static java.time.temporal.ChronoUnit.DAYS; import static java.time.temporal.ChronoUnit.HOURS; import static java.time.temporal.ChronoUnit.MICROS; @@ -100,14 +98,13 @@ import org.opensearch.sql.utils.DateTimeUtils; /** - * The definition of date and time functions. - * 1) have the clear interface for function define. - * 2) the implementation should rely on ExprValue. + * The definition of date and time functions. 1) have the clear interface for function define. 2) + * the implementation should rely on ExprValue. */ @UtilityClass @SuppressWarnings("unchecked") public class DateTimeFunction { - //The number of seconds per day + // The number of seconds per day public static final long SECONDS_PER_DAY = 86400; // The number of days from year zero to year 1970. @@ -254,11 +251,12 @@ public void register(BuiltinFunctionRepository repository) { * `now(y) return different values. */ private FunctionResolver now(FunctionName functionName) { - return define(functionName, + return define( + functionName, implWithProperties( - functionProperties -> new ExprDatetimeValue( - formatNow(functionProperties.getQueryStartClock())), DATETIME) - ); + functionProperties -> + new ExprDatetimeValue(formatNow(functionProperties.getQueryStartClock())), + DATETIME)); } private FunctionResolver now() { @@ -277,25 +275,28 @@ private FunctionResolver localtime() { return now(BuiltinFunctionName.LOCALTIME.getName()); } - /** - * SYSDATE() returns the time at which it executes. - */ + /** SYSDATE() returns the time at which it executes. */ private FunctionResolver sysdate() { - return define(BuiltinFunctionName.SYSDATE.getName(), - implWithProperties(functionProperties - -> new ExprDatetimeValue(formatNow(Clock.systemDefaultZone())), DATETIME), - FunctionDSL.implWithProperties((functionProperties, v) -> new ExprDatetimeValue( - formatNow(Clock.systemDefaultZone(), v.integerValue())), DATETIME, INTEGER) - ); + return define( + BuiltinFunctionName.SYSDATE.getName(), + implWithProperties( + functionProperties -> new ExprDatetimeValue(formatNow(Clock.systemDefaultZone())), + DATETIME), + FunctionDSL.implWithProperties( + (functionProperties, v) -> + new ExprDatetimeValue(formatNow(Clock.systemDefaultZone(), v.integerValue())), + DATETIME, + INTEGER)); } - /** - * Synonym for @see `now`. - */ + /** Synonym for @see `now`. */ private FunctionResolver curtime(FunctionName functionName) { - return define(functionName, - implWithProperties(functionProperties -> new ExprTimeValue( - formatNow(functionProperties.getQueryStartClock()).toLocalTime()), TIME)); + return define( + functionName, + implWithProperties( + functionProperties -> + new ExprTimeValue(formatNow(functionProperties.getQueryStartClock()).toLocalTime()), + TIME)); } private FunctionResolver curtime() { @@ -307,9 +308,12 @@ private FunctionResolver current_time() { } private FunctionResolver curdate(FunctionName functionName) { - return define(functionName, - implWithProperties(functionProperties -> new ExprDateValue( - formatNow(functionProperties.getQueryStartClock()).toLocalDate()), DATE)); + return define( + functionName, + implWithProperties( + functionProperties -> + new ExprDateValue(formatNow(functionProperties.getQueryStartClock()).toLocalDate()), + DATE)); } private FunctionResolver curdate() { @@ -321,33 +325,27 @@ private FunctionResolver current_date() { } /** - * A common signature for `date_add` and `date_sub`. - * Specify a start date and add/subtract a temporal amount to/from the date. - * The return type depends on the date type and the interval unit. Detailed supported signatures: - * (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME - * MySQL has these signatures too - * (DATE, INTERVAL) -> DATE // when interval has no time part - * (TIME, INTERVAL) -> TIME // when interval has no date part - * (STRING, INTERVAL) -> STRING // when argument has date or datetime string, - * // result has date or datetime depending on interval type + * A common signature for `date_add` and `date_sub`. Specify a start date and add/subtract a + * temporal amount to/from the date. The return type depends on the date type and the interval + * unit. Detailed supported signatures: (DATE/DATETIME/TIMESTAMP/TIME, INTERVAL) -> DATETIME MySQL + * has these signatures too (DATE, INTERVAL) -> DATE // when interval has no time part (TIME, + * INTERVAL) -> TIME // when interval has no date part (STRING, INTERVAL) -> STRING // when + * argument has date or datetime string, // result has date or datetime depending on interval type */ private Stream> get_date_add_date_sub_signatures( SerializableTriFunction function) { return Stream.of( implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, DATE, INTERVAL), - implWithProperties(nullMissingHandlingWithProperties(function), - DATETIME, DATETIME, INTERVAL), - implWithProperties(nullMissingHandlingWithProperties(function), - DATETIME, TIMESTAMP, INTERVAL), - implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, TIME, INTERVAL) - ); + implWithProperties( + nullMissingHandlingWithProperties(function), DATETIME, DATETIME, INTERVAL), + implWithProperties( + nullMissingHandlingWithProperties(function), DATETIME, TIMESTAMP, INTERVAL), + implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, TIME, INTERVAL)); } /** - * A common signature for `adddate` and `subdate`. - * Adds/subtracts an integer number of days to/from the first argument. - * (DATE, LONG) -> DATE - * (TIME/DATETIME/TIMESTAMP, LONG) -> DATETIME + * A common signature for `adddate` and `subdate`. Adds/subtracts an integer number of days + * to/from the first argument. (DATE, LONG) -> DATE (TIME/DATETIME/TIMESTAMP, LONG) -> DATETIME */ private Stream> get_adddate_subdate_signatures( SerializableTriFunction function) { @@ -355,87 +353,124 @@ private FunctionResolver current_date() { implWithProperties(nullMissingHandlingWithProperties(function), DATE, DATE, LONG), implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, DATETIME, LONG), implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, TIMESTAMP, LONG), - implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, TIME, LONG) - ); + implWithProperties(nullMissingHandlingWithProperties(function), DATETIME, TIME, LONG)); } private DefaultFunctionResolver adddate() { - return define(BuiltinFunctionName.ADDDATE.getName(), + return define( + BuiltinFunctionName.ADDDATE.getName(), (SerializableFunction>[]) (Stream.concat( - get_date_add_date_sub_signatures(DateTimeFunction::exprAddDateInterval), - get_adddate_subdate_signatures(DateTimeFunction::exprAddDateDays)) + get_date_add_date_sub_signatures(DateTimeFunction::exprAddDateInterval), + get_adddate_subdate_signatures(DateTimeFunction::exprAddDateDays)) .toArray(SerializableFunction[]::new))); } /** - * Adds expr2 to expr1 and returns the result. - * (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME - * (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME - * TODO: MySQL has these signatures too - * (STRING, STRING/TIME) -> STRING // second arg - string with time only - * (x, STRING) -> NULL // second arg - string with timestamp - * (x, STRING/DATE) -> x // second arg - string with date only + * Adds expr2 to expr1 and returns the result. (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME + * (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME TODO: MySQL has these + * signatures too (STRING, STRING/TIME) -> STRING // second arg - string with time only (x, + * STRING) -> NULL // second arg - string with timestamp (x, STRING/DATE) -> x // second arg - + * string with date only */ private DefaultFunctionResolver addtime() { - return define(BuiltinFunctionName.ADDTIME.getName(), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - TIME, TIME, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - TIME, TIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - TIME, TIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - TIME, TIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATETIME, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATETIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATETIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATETIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATE, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATE, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATE, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, DATE, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, TIMESTAMP, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, TIMESTAMP, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, TIMESTAMP, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), - DATETIME, TIMESTAMP, TIMESTAMP) - ); - } - - /** - * Converts date/time from a specified timezone to another specified timezone. - * The supported signatures: - * (DATETIME, STRING, STRING) -> DATETIME - * (STRING, STRING, STRING) -> DATETIME - */ - private DefaultFunctionResolver convert_tz() { - return define(BuiltinFunctionName.CONVERT_TZ.getName(), - impl(nullMissingHandling(DateTimeFunction::exprConvertTZ), - DATETIME, DATETIME, STRING, STRING), - impl(nullMissingHandling(DateTimeFunction::exprConvertTZ), - DATETIME, STRING, STRING, STRING) - ); + return define( + BuiltinFunctionName.ADDTIME.getName(), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), TIME, TIME, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), TIME, TIME, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), TIME, TIME, DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + TIME, + TIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATETIME, + TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATETIME, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATETIME, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATETIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), DATETIME, DATE, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), DATETIME, DATE, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATE, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + DATE, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + TIMESTAMP, + TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + TIMESTAMP, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + TIMESTAMP, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprAddTime), + DATETIME, + TIMESTAMP, + TIMESTAMP)); } /** - * Extracts the date part of a date and time value. - * Also to construct a date type. The supported signatures: - * STRING/DATE/DATETIME/TIMESTAMP -> DATE + * Converts date/time from a specified timezone to another specified timezone. The supported + * signatures: (DATETIME, STRING, STRING) -> DATETIME (STRING, STRING, STRING) -> DATETIME + */ + private DefaultFunctionResolver convert_tz() { + return define( + BuiltinFunctionName.CONVERT_TZ.getName(), + impl( + nullMissingHandling(DateTimeFunction::exprConvertTZ), + DATETIME, + DATETIME, + STRING, + STRING), + impl( + nullMissingHandling(DateTimeFunction::exprConvertTZ), + DATETIME, + STRING, + STRING, + STRING)); + } + + /** + * Extracts the date part of a date and time value. Also to construct a date type. The supported + * signatures: STRING/DATE/DATETIME/TIMESTAMP -> DATE */ private DefaultFunctionResolver date() { - return define(BuiltinFunctionName.DATE.getName(), + return define( + BuiltinFunctionName.DATE.getName(), impl(nullMissingHandling(DateTimeFunction::exprDate), DATE, STRING), impl(nullMissingHandling(DateTimeFunction::exprDate), DATE, DATE), impl(nullMissingHandling(DateTimeFunction::exprDate), DATE, DATETIME), @@ -447,273 +482,308 @@ private DefaultFunctionResolver date() { * (DATE/DATETIME/TIMESTAMP/TIME, DATE/DATETIME/TIMESTAMP/TIME) -> LONG */ private DefaultFunctionResolver datediff() { - return define(BuiltinFunctionName.DATEDIFF.getName(), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATE, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATETIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATE, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATETIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATE, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIME, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIMESTAMP, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATE, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIMESTAMP, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIMESTAMP, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIMESTAMP, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATETIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, TIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), - LONG, DATETIME, TIME)); - } - - /** - * Specify a datetime with time zone field and a time zone to convert to. - * Returns a local date time. - * (STRING, STRING) -> DATETIME - * (STRING) -> DATETIME + return define( + BuiltinFunctionName.DATEDIFF.getName(), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), LONG, DATE, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATETIME, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATE, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATETIME, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), LONG, DATE, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), LONG, TIME, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), LONG, TIME, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIMESTAMP, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATE, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIMESTAMP, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIMESTAMP, + TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIMESTAMP, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATETIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + TIME, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprDateDiff), + LONG, + DATETIME, + TIME)); + } + + /** + * Specify a datetime with time zone field and a time zone to convert to. Returns a local date + * time. (STRING, STRING) -> DATETIME (STRING) -> DATETIME */ private FunctionResolver datetime() { - return define(BuiltinFunctionName.DATETIME.getName(), - impl(nullMissingHandling(DateTimeFunction::exprDateTime), - DATETIME, STRING, STRING), - impl(nullMissingHandling(DateTimeFunction::exprDateTimeNoTimezone), - DATETIME, STRING) - ); + return define( + BuiltinFunctionName.DATETIME.getName(), + impl(nullMissingHandling(DateTimeFunction::exprDateTime), DATETIME, STRING, STRING), + impl(nullMissingHandling(DateTimeFunction::exprDateTimeNoTimezone), DATETIME, STRING)); } private DefaultFunctionResolver date_add() { - return define(BuiltinFunctionName.DATE_ADD.getName(), + return define( + BuiltinFunctionName.DATE_ADD.getName(), (SerializableFunction>[]) get_date_add_date_sub_signatures(DateTimeFunction::exprAddDateInterval) .toArray(SerializableFunction[]::new)); } private DefaultFunctionResolver date_sub() { - return define(BuiltinFunctionName.DATE_SUB.getName(), + return define( + BuiltinFunctionName.DATE_SUB.getName(), (SerializableFunction>[]) get_date_add_date_sub_signatures(DateTimeFunction::exprSubDateInterval) .toArray(SerializableFunction[]::new)); } - /** - * DAY(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31). - */ + /** DAY(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31). */ private DefaultFunctionResolver day() { - return define(BuiltinFunctionName.DAY.getName(), + return define( + BuiltinFunctionName.DAY.getName(), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, STRING)); } /** - * DAYNAME(STRING/DATE/DATETIME/TIMESTAMP). - * return the name of the weekday for date, including Monday, Tuesday, Wednesday, - * Thursday, Friday, Saturday and Sunday. + * DAYNAME(STRING/DATE/DATETIME/TIMESTAMP). return the name of the weekday for date, including + * Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. */ private DefaultFunctionResolver dayName() { - return define(BuiltinFunctionName.DAYNAME.getName(), + return define( + BuiltinFunctionName.DAYNAME.getName(), impl(nullMissingHandling(DateTimeFunction::exprDayName), STRING, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayName), STRING, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayName), STRING, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayName), STRING, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprDayName), STRING, STRING)); } - /** - * DAYOFMONTH(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31). - */ + /** DAYOFMONTH(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31). */ private DefaultFunctionResolver dayOfMonth(BuiltinFunctionName name) { - return define(name.getName(), - implWithProperties(nullMissingHandlingWithProperties( - (functionProperties, arg) -> DateTimeFunction.dayOfMonthToday( - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + name.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.dayOfMonthToday(functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, STRING), - impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, TIMESTAMP)); } /** - * DAYOFWEEK(STRING/DATE/DATETIME/TIME/TIMESTAMP). - * return the weekday index for date (1 = Sunday, 2 = Monday, ..., 7 = Saturday). + * DAYOFWEEK(STRING/DATE/DATETIME/TIME/TIMESTAMP). return the weekday index for date (1 = Sunday, + * 2 = Monday, ..., 7 = Saturday). */ private DefaultFunctionResolver dayOfWeek(FunctionName name) { - return define(name, - implWithProperties(nullMissingHandlingWithProperties( - (functionProperties, arg) -> DateTimeFunction.dayOfWeekToday( - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + name, + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.dayOfWeekToday(functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprDayOfWeek), INTEGER, STRING)); } - /** - * DAYOFYEAR(STRING/DATE/DATETIME/TIMESTAMP). - * return the day of the year for date (1-366). - */ + /** DAYOFYEAR(STRING/DATE/DATETIME/TIMESTAMP). return the day of the year for date (1-366). */ private DefaultFunctionResolver dayOfYear(BuiltinFunctionName dayOfYear) { - return define(dayOfYear.getName(), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) - -> DateTimeFunction.dayOfYearToday( - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + dayOfYear.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.dayOfYearToday(functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, STRING)); } private DefaultFunctionResolver extract() { - return define(BuiltinFunctionName.EXTRACT.getName(), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprExtractForTime), - LONG, STRING, TIME), + return define( + BuiltinFunctionName.EXTRACT.getName(), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprExtractForTime), + LONG, + STRING, + TIME), impl(nullMissingHandling(DateTimeFunction::exprExtract), LONG, STRING, DATE), impl(nullMissingHandling(DateTimeFunction::exprExtract), LONG, STRING, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprExtract), LONG, STRING, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprExtract), LONG, STRING, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprExtract), LONG, STRING, STRING)); } - /** - * FROM_DAYS(LONG). return the date value given the day number N. - */ + /** FROM_DAYS(LONG). return the date value given the day number N. */ private DefaultFunctionResolver from_days() { - return define(BuiltinFunctionName.FROM_DAYS.getName(), + return define( + BuiltinFunctionName.FROM_DAYS.getName(), impl(nullMissingHandling(DateTimeFunction::exprFromDays), DATE, LONG)); } private FunctionResolver from_unixtime() { - return define(BuiltinFunctionName.FROM_UNIXTIME.getName(), + return define( + BuiltinFunctionName.FROM_UNIXTIME.getName(), impl(nullMissingHandling(DateTimeFunction::exprFromUnixTime), DATETIME, DOUBLE), - impl(nullMissingHandling(DateTimeFunction::exprFromUnixTimeFormat), - STRING, DOUBLE, STRING)); + impl( + nullMissingHandling(DateTimeFunction::exprFromUnixTimeFormat), STRING, DOUBLE, STRING)); } private DefaultFunctionResolver get_format() { - return define(BuiltinFunctionName.GET_FORMAT.getName(), - impl(nullMissingHandling(DateTimeFunction::exprGetFormat), STRING, STRING, STRING) - ); + return define( + BuiltinFunctionName.GET_FORMAT.getName(), + impl(nullMissingHandling(DateTimeFunction::exprGetFormat), STRING, STRING, STRING)); } - /** - * HOUR(STRING/TIME/DATETIME/DATE/TIMESTAMP). return the hour value for time. - */ + /** HOUR(STRING/TIME/DATETIME/DATE/TIMESTAMP). return the hour value for time. */ private DefaultFunctionResolver hour(BuiltinFunctionName name) { - return define(name.getName(), + return define( + name.getName(), impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, TIMESTAMP)); } - private DefaultFunctionResolver last_day() { - return define(BuiltinFunctionName.LAST_DAY.getName(), + private DefaultFunctionResolver last_day() { + return define( + BuiltinFunctionName.LAST_DAY.getName(), impl(nullMissingHandling(DateTimeFunction::exprLastDay), DATE, STRING), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) - -> DateTimeFunction.exprLastDayToday( - functionProperties.getQueryStartClock())), DATE, TIME), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.exprLastDayToday(functionProperties.getQueryStartClock())), + DATE, + TIME), impl(nullMissingHandling(DateTimeFunction::exprLastDay), DATE, DATE), impl(nullMissingHandling(DateTimeFunction::exprLastDay), DATE, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprLastDay), DATE, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprLastDay), DATE, TIMESTAMP)); } private FunctionResolver makedate() { - return define(BuiltinFunctionName.MAKEDATE.getName(), + return define( + BuiltinFunctionName.MAKEDATE.getName(), impl(nullMissingHandling(DateTimeFunction::exprMakeDate), DATE, DOUBLE, DOUBLE)); } private FunctionResolver maketime() { - return define(BuiltinFunctionName.MAKETIME.getName(), + return define( + BuiltinFunctionName.MAKETIME.getName(), impl(nullMissingHandling(DateTimeFunction::exprMakeTime), TIME, DOUBLE, DOUBLE, DOUBLE)); } - /** - * MICROSECOND(STRING/TIME/DATETIME/TIMESTAMP). return the microsecond value for time. - */ + /** MICROSECOND(STRING/TIME/DATETIME/TIMESTAMP). return the microsecond value for time. */ private DefaultFunctionResolver microsecond() { - return define(BuiltinFunctionName.MICROSECOND.getName(), + return define( + BuiltinFunctionName.MICROSECOND.getName(), impl(nullMissingHandling(DateTimeFunction::exprMicrosecond), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprMicrosecond), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprMicrosecond), INTEGER, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprMicrosecond), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprMicrosecond), INTEGER, TIMESTAMP)); } - /** - * MINUTE(STRING/TIME/DATETIME/TIMESTAMP). return the minute value for time. - */ + /** MINUTE(STRING/TIME/DATETIME/TIMESTAMP). return the minute value for time. */ private DefaultFunctionResolver minute(BuiltinFunctionName name) { - return define(name.getName(), + return define( + name.getName(), impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, DATE), - impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, TIMESTAMP)); } - /** - * MINUTE(STRING/TIME/DATETIME/TIMESTAMP). return the minute value for time. - */ + /** MINUTE(STRING/TIME/DATETIME/TIMESTAMP). return the minute value for time. */ private DefaultFunctionResolver minute_of_day() { - return define(BuiltinFunctionName.MINUTE_OF_DAY.getName(), + return define( + BuiltinFunctionName.MINUTE_OF_DAY.getName(), impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprMinuteOfDay), INTEGER, TIMESTAMP)); } - /** - * MONTH(STRING/DATE/DATETIME/TIMESTAMP). return the month for date (1-12). - */ + /** MONTH(STRING/DATE/DATETIME/TIMESTAMP). return the month for date (1-12). */ private DefaultFunctionResolver month(BuiltinFunctionName month) { - return define(month.getName(), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) - -> DateTimeFunction.monthOfYearToday( - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + month.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.monthOfYearToday(functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, STRING)); } - /** - * MONTHNAME(STRING/DATE/DATETIME/TIMESTAMP). return the full name of the month for date. - */ + /** MONTHNAME(STRING/DATE/DATETIME/TIMESTAMP). return the full name of the month for date. */ private DefaultFunctionResolver monthName() { - return define(BuiltinFunctionName.MONTHNAME.getName(), + return define( + BuiltinFunctionName.MONTHNAME.getName(), impl(nullMissingHandling(DateTimeFunction::exprMonthName), STRING, DATE), impl(nullMissingHandling(DateTimeFunction::exprMonthName), STRING, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprMonthName), STRING, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprMonthName), STRING, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprMonthName), STRING, STRING)); } /** @@ -721,130 +791,161 @@ private DefaultFunctionResolver monthName() { * (INTEGER, INTEGER) -> INTEGER */ private DefaultFunctionResolver period_add() { - return define(BuiltinFunctionName.PERIOD_ADD.getName(), - impl(nullMissingHandling(DateTimeFunction::exprPeriodAdd), INTEGER, INTEGER, INTEGER) - ); + return define( + BuiltinFunctionName.PERIOD_ADD.getName(), + impl(nullMissingHandling(DateTimeFunction::exprPeriodAdd), INTEGER, INTEGER, INTEGER)); } /** - * Returns the number of months between periods P1 and P2. - * P1 and P2 should be in the format YYMM or YYYYMM. - * (INTEGER, INTEGER) -> INTEGER + * Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM + * or YYYYMM. (INTEGER, INTEGER) -> INTEGER */ private DefaultFunctionResolver period_diff() { - return define(BuiltinFunctionName.PERIOD_DIFF.getName(), - impl(nullMissingHandling(DateTimeFunction::exprPeriodDiff), INTEGER, INTEGER, INTEGER) - ); + return define( + BuiltinFunctionName.PERIOD_DIFF.getName(), + impl(nullMissingHandling(DateTimeFunction::exprPeriodDiff), INTEGER, INTEGER, INTEGER)); } - /** - * QUARTER(STRING/DATE/DATETIME/TIMESTAMP). return the month for date (1-4). - */ + /** QUARTER(STRING/DATE/DATETIME/TIMESTAMP). return the month for date (1-4). */ private DefaultFunctionResolver quarter() { - return define(BuiltinFunctionName.QUARTER.getName(), + return define( + BuiltinFunctionName.QUARTER.getName(), impl(nullMissingHandling(DateTimeFunction::exprQuarter), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprQuarter), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprQuarter), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprQuarter), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprQuarter), INTEGER, STRING)); } private DefaultFunctionResolver sec_to_time() { - return define(BuiltinFunctionName.SEC_TO_TIME.getName(), + return define( + BuiltinFunctionName.SEC_TO_TIME.getName(), impl((nullMissingHandling(DateTimeFunction::exprSecToTime)), TIME, INTEGER), impl((nullMissingHandling(DateTimeFunction::exprSecToTime)), TIME, LONG), impl((nullMissingHandling(DateTimeFunction::exprSecToTimeWithNanos)), TIME, DOUBLE), - impl((nullMissingHandling(DateTimeFunction::exprSecToTimeWithNanos)), TIME, FLOAT) - ); + impl((nullMissingHandling(DateTimeFunction::exprSecToTimeWithNanos)), TIME, FLOAT)); } - /** - * SECOND(STRING/TIME/DATETIME/TIMESTAMP). return the second value for time. - */ + /** SECOND(STRING/TIME/DATETIME/TIMESTAMP). return the second value for time. */ private DefaultFunctionResolver second(BuiltinFunctionName name) { - return define(name.getName(), + return define( + name.getName(), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, STRING), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIME), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIMESTAMP) - ); + impl(nullMissingHandling(DateTimeFunction::exprSecond), INTEGER, TIMESTAMP)); } private DefaultFunctionResolver subdate() { - return define(BuiltinFunctionName.SUBDATE.getName(), + return define( + BuiltinFunctionName.SUBDATE.getName(), (SerializableFunction>[]) (Stream.concat( - get_date_add_date_sub_signatures(DateTimeFunction::exprSubDateInterval), - get_adddate_subdate_signatures(DateTimeFunction::exprSubDateDays)) + get_date_add_date_sub_signatures(DateTimeFunction::exprSubDateInterval), + get_adddate_subdate_signatures(DateTimeFunction::exprSubDateDays)) .toArray(SerializableFunction[]::new))); } /** - * Subtracts expr2 from expr1 and returns the result. - * (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME - * (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME - * TODO: MySQL has these signatures too - * (STRING, STRING/TIME) -> STRING // second arg - string with time only - * (x, STRING) -> NULL // second arg - string with timestamp - * (x, STRING/DATE) -> x // second arg - string with date only + * Subtracts expr2 from expr1 and returns the result. (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME + * (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME TODO: MySQL has these + * signatures too (STRING, STRING/TIME) -> STRING // second arg - string with time only (x, + * STRING) -> NULL // second arg - string with timestamp (x, STRING/DATE) -> x // second arg - + * string with date only */ private DefaultFunctionResolver subtime() { - return define(BuiltinFunctionName.SUBTIME.getName(), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - TIME, TIME, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - TIME, TIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - TIME, TIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - TIME, TIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATETIME, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATETIME, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATETIME, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATETIME, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATE, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATE, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATE, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, DATE, TIMESTAMP), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, TIMESTAMP, TIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, TIMESTAMP, DATE), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, TIMESTAMP, DATETIME), - implWithProperties(nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), - DATETIME, TIMESTAMP, TIMESTAMP) - ); - } - - /** - * Extracts a date, time, or datetime from the given string. - * It accomplishes this using another string which specifies the input format. + return define( + BuiltinFunctionName.SUBTIME.getName(), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), TIME, TIME, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), TIME, TIME, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), TIME, TIME, DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + TIME, + TIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATETIME, + TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATETIME, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATETIME, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATETIME, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), DATETIME, DATE, TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), DATETIME, DATE, DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATE, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + DATE, + TIMESTAMP), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + TIMESTAMP, + TIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + TIMESTAMP, + DATE), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + TIMESTAMP, + DATETIME), + implWithProperties( + nullMissingHandlingWithProperties(DateTimeFunction::exprSubTime), + DATETIME, + TIMESTAMP, + TIMESTAMP)); + } + + /** + * Extracts a date, time, or datetime from the given string. It accomplishes this using another + * string which specifies the input format. */ private DefaultFunctionResolver str_to_date() { - return define(BuiltinFunctionName.STR_TO_DATE.getName(), + return define( + BuiltinFunctionName.STR_TO_DATE.getName(), implWithProperties( - nullMissingHandlingWithProperties((functionProperties, arg, format) - -> DateTimeFunction.exprStrToDate(functionProperties, arg, format)), - DATETIME, STRING, STRING)); + nullMissingHandlingWithProperties( + (functionProperties, arg, format) -> + DateTimeFunction.exprStrToDate(functionProperties, arg, format)), + DATETIME, + STRING, + STRING)); } /** - * Extracts the time part of a date and time value. - * Also to construct a time type. The supported signatures: - * STRING/DATE/DATETIME/TIME/TIMESTAMP -> TIME + * Extracts the time part of a date and time value. Also to construct a time type. The supported + * signatures: STRING/DATE/DATETIME/TIME/TIMESTAMP -> TIME */ private DefaultFunctionResolver time() { - return define(BuiltinFunctionName.TIME.getName(), + return define( + BuiltinFunctionName.TIME.getName(), impl(nullMissingHandling(DateTimeFunction::exprTime), TIME, STRING), impl(nullMissingHandling(DateTimeFunction::exprTime), TIME, DATE), impl(nullMissingHandling(DateTimeFunction::exprTime), TIME, DATETIME), @@ -853,18 +954,15 @@ private DefaultFunctionResolver time() { } /** - * Returns different between two times as a time. - * (TIME, TIME) -> TIME - * MySQL has these signatures too - * (DATE, DATE) -> TIME // result is > 24 hours - * (DATETIME, DATETIME) -> TIME // result is > 24 hours - * (TIMESTAMP, TIMESTAMP) -> TIME // result is > 24 hours - * (x, x) -> NULL // when args have different types - * (STRING, STRING) -> TIME // argument strings contain same types only - * (STRING, STRING) -> NULL // argument strings are different types + * Returns different between two times as a time. (TIME, TIME) -> TIME MySQL has these signatures + * too (DATE, DATE) -> TIME // result is > 24 hours (DATETIME, DATETIME) -> TIME // result is > 24 + * hours (TIMESTAMP, TIMESTAMP) -> TIME // result is > 24 hours (x, x) -> NULL // when args have + * different types (STRING, STRING) -> TIME // argument strings contain same types only (STRING, + * STRING) -> NULL // argument strings are different types */ private DefaultFunctionResolver timediff() { - return define(BuiltinFunctionName.TIMEDIFF.getName(), + return define( + BuiltinFunctionName.TIMEDIFF.getName(), impl(nullMissingHandling(DateTimeFunction::exprTimeDiff), TIME, TIME, TIME)); } @@ -872,90 +970,120 @@ private DefaultFunctionResolver timediff() { * TIME_TO_SEC(STRING/TIME/DATETIME/TIMESTAMP). return the time argument, converted to seconds. */ private DefaultFunctionResolver time_to_sec() { - return define(BuiltinFunctionName.TIME_TO_SEC.getName(), + return define( + BuiltinFunctionName.TIME_TO_SEC.getName(), impl(nullMissingHandling(DateTimeFunction::exprTimeToSec), LONG, STRING), impl(nullMissingHandling(DateTimeFunction::exprTimeToSec), LONG, TIME), impl(nullMissingHandling(DateTimeFunction::exprTimeToSec), LONG, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprTimeToSec), LONG, DATETIME) - ); + impl(nullMissingHandling(DateTimeFunction::exprTimeToSec), LONG, DATETIME)); } /** - * Extracts the timestamp of a date and time value. - * Input strings may contain a timestamp only in format 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]' - * STRING/DATE/TIME/DATETIME/TIMESTAMP -> TIMESTAMP - * STRING/DATE/TIME/DATETIME/TIMESTAMP, STRING/DATE/TIME/DATETIME/TIMESTAMP -> TIMESTAMP - * All types are converted to TIMESTAMP actually before the function call - it is responsibility - * of the automatic cast mechanism defined in `ExprCoreType` and performed by `TypeCastOperator`. + * Extracts the timestamp of a date and time value. Input strings may contain a timestamp only in + * format 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]' STRING/DATE/TIME/DATETIME/TIMESTAMP -> TIMESTAMP + * STRING/DATE/TIME/DATETIME/TIMESTAMP, STRING/DATE/TIME/DATETIME/TIMESTAMP -> TIMESTAMP All types + * are converted to TIMESTAMP actually before the function call - it is responsibility of the + * automatic cast mechanism defined in `ExprCoreType` and performed by `TypeCastOperator`. */ private DefaultFunctionResolver timestamp() { - return define(BuiltinFunctionName.TIMESTAMP.getName(), + return define( + BuiltinFunctionName.TIMESTAMP.getName(), impl(nullMissingHandling(v -> v), TIMESTAMP, TIMESTAMP), // We can use FunctionProperties.None, because it is not used. It is required to convert // TIME to other datetime types, but arguments there are already converted. - impl(nullMissingHandling((v1, v2) -> exprAddTime(FunctionProperties.None, v1, v2)), - TIMESTAMP, TIMESTAMP, TIMESTAMP)); + impl( + nullMissingHandling((v1, v2) -> exprAddTime(FunctionProperties.None, v1, v2)), + TIMESTAMP, + TIMESTAMP, + TIMESTAMP)); } /** - * Adds an interval of time to the provided DATE/DATETIME/TIME/TIMESTAMP/STRING argument. - * The interval of time added is determined by the given first and second arguments. - * The first argument is an interval type, and must be one of the tokens below... - * [MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR] - * The second argument is the amount of the interval type to be added. - * The third argument is the DATE/DATETIME/TIME/TIMESTAMP/STRING to add to. + * Adds an interval of time to the provided DATE/DATETIME/TIME/TIMESTAMP/STRING argument. The + * interval of time added is determined by the given first and second arguments. The first + * argument is an interval type, and must be one of the tokens below... [MICROSECOND, SECOND, + * MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR] The second argument is the amount of the + * interval type to be added. The third argument is the DATE/DATETIME/TIME/TIMESTAMP/STRING to add + * to. + * * @return The DATETIME representing the summed DATE/DATETIME/TIME/TIMESTAMP and interval. */ private DefaultFunctionResolver timestampadd() { - return define(BuiltinFunctionName.TIMESTAMPADD.getName(), - impl(nullMissingHandling(DateTimeFunction::exprTimestampAdd), - DATETIME, STRING, INTEGER, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprTimestampAdd), - DATETIME, STRING, INTEGER, TIMESTAMP), + return define( + BuiltinFunctionName.TIMESTAMPADD.getName(), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampAdd), + DATETIME, + STRING, + INTEGER, + DATETIME), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampAdd), + DATETIME, + STRING, + INTEGER, + TIMESTAMP), implWithProperties( nullMissingHandlingWithProperties( - (functionProperties, part, amount, time) -> exprTimestampAddForTimeType( - functionProperties.getQueryStartClock(), - part, - amount, - time)), - DATETIME, STRING, INTEGER, TIME)); + (functionProperties, part, amount, time) -> + exprTimestampAddForTimeType( + functionProperties.getQueryStartClock(), part, amount, time)), + DATETIME, + STRING, + INTEGER, + TIME)); } /** - * Finds the difference between provided DATE/DATETIME/TIME/TIMESTAMP/STRING arguments. - * The first argument is an interval type, and must be one of the tokens below... - * [MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR] - * The second argument the DATE/DATETIME/TIME/TIMESTAMP/STRING representing the start time. - * The third argument is the DATE/DATETIME/TIME/TIMESTAMP/STRING representing the end time. + * Finds the difference between provided DATE/DATETIME/TIME/TIMESTAMP/STRING arguments. The first + * argument is an interval type, and must be one of the tokens below... [MICROSECOND, SECOND, + * MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR] The second argument the + * DATE/DATETIME/TIME/TIMESTAMP/STRING representing the start time. The third argument is the + * DATE/DATETIME/TIME/TIMESTAMP/STRING representing the end time. + * * @return A LONG representing the difference between arguments, using the given interval type. */ private DefaultFunctionResolver timestampdiff() { - return define(BuiltinFunctionName.TIMESTAMPDIFF.getName(), - impl(nullMissingHandling(DateTimeFunction::exprTimestampDiff), - DATETIME, STRING, DATETIME, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprTimestampDiff), - DATETIME, STRING, DATETIME, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprTimestampDiff), - DATETIME, STRING, TIMESTAMP, DATETIME), - impl(nullMissingHandling(DateTimeFunction::exprTimestampDiff), - DATETIME, STRING, TIMESTAMP, TIMESTAMP), + return define( + BuiltinFunctionName.TIMESTAMPDIFF.getName(), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampDiff), + DATETIME, + STRING, + DATETIME, + DATETIME), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampDiff), + DATETIME, + STRING, + DATETIME, + TIMESTAMP), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampDiff), + DATETIME, + STRING, + TIMESTAMP, + DATETIME), + impl( + nullMissingHandling(DateTimeFunction::exprTimestampDiff), + DATETIME, + STRING, + TIMESTAMP, + TIMESTAMP), implWithProperties( nullMissingHandlingWithProperties( - (functionProperties, part, startTime, endTime) -> exprTimestampDiffForTimeType( - functionProperties, - part, - startTime, - endTime)), - DATETIME, STRING, TIME, TIME) - ); + (functionProperties, part, startTime, endTime) -> + exprTimestampDiffForTimeType(functionProperties, part, startTime, endTime)), + DATETIME, + STRING, + TIME, + TIME)); } - /** - * TO_DAYS(STRING/DATE/DATETIME/TIMESTAMP). return the day number of the given date. - */ + /** TO_DAYS(STRING/DATE/DATETIME/TIMESTAMP). return the day number of the given date. */ private DefaultFunctionResolver to_days() { - return define(BuiltinFunctionName.TO_DAYS.getName(), + return define( + BuiltinFunctionName.TO_DAYS.getName(), impl(nullMissingHandling(DateTimeFunction::exprToDays), LONG, STRING), impl(nullMissingHandling(DateTimeFunction::exprToDays), LONG, TIMESTAMP), impl(nullMissingHandling(DateTimeFunction::exprToDays), LONG, DATE), @@ -963,156 +1091,162 @@ private DefaultFunctionResolver to_days() { } /** - * TO_SECONDS(TIMESTAMP/LONG). return the seconds number of the given date. - * Arguments of type STRING/TIMESTAMP/LONG are also accepted. - * STRING/TIMESTAMP/LONG arguments are automatically cast to TIMESTAMP. + * TO_SECONDS(TIMESTAMP/LONG). return the seconds number of the given date. Arguments of type + * STRING/TIMESTAMP/LONG are also accepted. STRING/TIMESTAMP/LONG arguments are automatically cast + * to TIMESTAMP. */ private DefaultFunctionResolver to_seconds() { - return define(BuiltinFunctionName.TO_SECONDS.getName(), + return define( + BuiltinFunctionName.TO_SECONDS.getName(), impl(nullMissingHandling(DateTimeFunction::exprToSeconds), LONG, TIMESTAMP), impl(nullMissingHandling(DateTimeFunction::exprToSecondsForIntType), LONG, LONG)); } private FunctionResolver unix_timestamp() { - return define(BuiltinFunctionName.UNIX_TIMESTAMP.getName(), - implWithProperties(functionProperties - -> DateTimeFunction.unixTimeStamp(functionProperties.getQueryStartClock()), LONG), + return define( + BuiltinFunctionName.UNIX_TIMESTAMP.getName(), + implWithProperties( + functionProperties -> + DateTimeFunction.unixTimeStamp(functionProperties.getQueryStartClock()), + LONG), impl(nullMissingHandling(DateTimeFunction::unixTimeStampOf), DOUBLE, DATE), impl(nullMissingHandling(DateTimeFunction::unixTimeStampOf), DOUBLE, DATETIME), impl(nullMissingHandling(DateTimeFunction::unixTimeStampOf), DOUBLE, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::unixTimeStampOf), DOUBLE, DOUBLE) - ); + impl(nullMissingHandling(DateTimeFunction::unixTimeStampOf), DOUBLE, DOUBLE)); } - /** - * UTC_DATE(). return the current UTC Date in format yyyy-MM-dd - */ + /** UTC_DATE(). return the current UTC Date in format yyyy-MM-dd */ private DefaultFunctionResolver utc_date() { - return define(BuiltinFunctionName.UTC_DATE.getName(), - implWithProperties(functionProperties - -> exprUtcDate(functionProperties), DATE)); + return define( + BuiltinFunctionName.UTC_DATE.getName(), + implWithProperties(functionProperties -> exprUtcDate(functionProperties), DATE)); } - /** - * UTC_TIME(). return the current UTC Time in format HH:mm:ss - */ + /** UTC_TIME(). return the current UTC Time in format HH:mm:ss */ private DefaultFunctionResolver utc_time() { - return define(BuiltinFunctionName.UTC_TIME.getName(), - implWithProperties(functionProperties - -> exprUtcTime(functionProperties), TIME)); + return define( + BuiltinFunctionName.UTC_TIME.getName(), + implWithProperties(functionProperties -> exprUtcTime(functionProperties), TIME)); } - /** - * UTC_TIMESTAMP(). return the current UTC TimeStamp in format yyyy-MM-dd HH:mm:ss - */ + /** UTC_TIMESTAMP(). return the current UTC TimeStamp in format yyyy-MM-dd HH:mm:ss */ private DefaultFunctionResolver utc_timestamp() { - return define(BuiltinFunctionName.UTC_TIMESTAMP.getName(), - implWithProperties(functionProperties - -> exprUtcTimeStamp(functionProperties), DATETIME)); + return define( + BuiltinFunctionName.UTC_TIMESTAMP.getName(), + implWithProperties(functionProperties -> exprUtcTimeStamp(functionProperties), DATETIME)); } - /** - * WEEK(DATE[,mode]). return the week number for date. - */ + /** WEEK(DATE[,mode]). return the week number for date. */ private DefaultFunctionResolver week(BuiltinFunctionName week) { - return define(week.getName(), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) - -> DateTimeFunction.weekOfYearToday( - DEFAULT_WEEK_OF_YEAR_MODE, - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + week.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + DateTimeFunction.weekOfYearToday( + DEFAULT_WEEK_OF_YEAR_MODE, functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, TIMESTAMP), impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, STRING), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, time, modeArg) - -> DateTimeFunction.weekOfYearToday( - modeArg, - functionProperties.getQueryStartClock())), INTEGER, TIME, INTEGER), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, time, modeArg) -> + DateTimeFunction.weekOfYearToday( + modeArg, functionProperties.getQueryStartClock())), + INTEGER, + TIME, + INTEGER), impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATE, INTEGER), impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATETIME, INTEGER), impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, TIMESTAMP, INTEGER), - impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, STRING, INTEGER) - ); + impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, STRING, INTEGER)); } private DefaultFunctionResolver weekday() { - return define(BuiltinFunctionName.WEEKDAY.getName(), - implWithProperties(nullMissingHandlingWithProperties( - (functionProperties, arg) -> new ExprIntegerValue( - formatNow(functionProperties.getQueryStartClock()).getDayOfWeek().getValue() - 1)), - INTEGER, TIME), + return define( + BuiltinFunctionName.WEEKDAY.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + new ExprIntegerValue( + formatNow(functionProperties.getQueryStartClock()).getDayOfWeek().getValue() + - 1)), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, STRING)); } - /** - * YEAR(STRING/DATE/DATETIME/TIMESTAMP). return the year for date (1000-9999). - */ + /** YEAR(STRING/DATE/DATETIME/TIMESTAMP). return the year for date (1000-9999). */ private DefaultFunctionResolver year() { - return define(BuiltinFunctionName.YEAR.getName(), + return define( + BuiltinFunctionName.YEAR.getName(), impl(nullMissingHandling(DateTimeFunction::exprYear), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprYear), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprYear), INTEGER, TIMESTAMP), - impl(nullMissingHandling(DateTimeFunction::exprYear), INTEGER, STRING) - ); + impl(nullMissingHandling(DateTimeFunction::exprYear), INTEGER, STRING)); } - /** - * YEARWEEK(DATE[,mode]). return the week number for date. - */ + /** YEARWEEK(DATE[,mode]). return the week number for date. */ private DefaultFunctionResolver yearweek() { - return define(BuiltinFunctionName.YEARWEEK.getName(), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) - -> yearweekToday( - DEFAULT_WEEK_OF_YEAR_MODE, - functionProperties.getQueryStartClock())), INTEGER, TIME), + return define( + BuiltinFunctionName.YEARWEEK.getName(), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, arg) -> + yearweekToday( + DEFAULT_WEEK_OF_YEAR_MODE, functionProperties.getQueryStartClock())), + INTEGER, + TIME), impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, DATE), impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, DATETIME), impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, TIMESTAMP), impl(nullMissingHandling(DateTimeFunction::exprYearweekWithoutMode), INTEGER, STRING), - implWithProperties(nullMissingHandlingWithProperties((functionProperties, time, modeArg) - -> yearweekToday( - modeArg, - functionProperties.getQueryStartClock())), INTEGER, TIME, INTEGER), + implWithProperties( + nullMissingHandlingWithProperties( + (functionProperties, time, modeArg) -> + yearweekToday(modeArg, functionProperties.getQueryStartClock())), + INTEGER, + TIME, + INTEGER), impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, DATE, INTEGER), impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, DATETIME, INTEGER), impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, TIMESTAMP, INTEGER), - impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, STRING, INTEGER) - ); + impl(nullMissingHandling(DateTimeFunction::exprYearweek), INTEGER, STRING, INTEGER)); } /** - * Formats date according to format specifier. First argument is date, second is format. - * Detailed supported signatures: - * (STRING, STRING) -> STRING - * (DATE, STRING) -> STRING - * (DATETIME, STRING) -> STRING - * (TIME, STRING) -> STRING - * (TIMESTAMP, STRING) -> STRING + * Formats date according to format specifier. First argument is date, second is format. Detailed + * supported signatures: (STRING, STRING) -> STRING (DATE, STRING) -> STRING (DATETIME, STRING) -> + * STRING (TIME, STRING) -> STRING (TIMESTAMP, STRING) -> STRING */ private DefaultFunctionResolver date_format() { - return define(BuiltinFunctionName.DATE_FORMAT.getName(), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), - STRING, STRING, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), - STRING, DATE, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), - STRING, DATETIME, STRING), + return define( + BuiltinFunctionName.DATE_FORMAT.getName(), + impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), STRING, STRING, STRING), + impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), STRING, DATE, STRING), + impl( + nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), STRING, DATETIME, STRING), implWithProperties( nullMissingHandlingWithProperties( - (functionProperties, time, formatString) - -> DateTimeFormatterUtil.getFormattedDateOfToday( + (functionProperties, time, formatString) -> + DateTimeFormatterUtil.getFormattedDateOfToday( formatString, time, functionProperties.getQueryStartClock())), - STRING, TIME, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), - STRING, TIMESTAMP, STRING) - ); + STRING, + TIME, + STRING), + impl( + nullMissingHandling(DateTimeFormatterUtil::getFormattedDate), + STRING, + TIMESTAMP, + STRING)); } - private ExprValue dayOfMonthToday(Clock clock) { return new ExprIntegerValue(LocalDateTime.now(clock).getDayOfMonth()); } @@ -1144,8 +1278,8 @@ private ExprValue dayOfWeekToday(Clock clock) { * @param interval ExprValue of Interval type, the temporal amount to add. * @return Datetime resulted from `interval` added to `datetime`. */ - private ExprValue exprAddDateInterval(FunctionProperties functionProperties, - ExprValue datetime, ExprValue interval) { + private ExprValue exprAddDateInterval( + FunctionProperties functionProperties, ExprValue datetime, ExprValue interval) { return exprDateApplyInterval(functionProperties, datetime, interval.intervalValue(), true); } @@ -1158,36 +1292,33 @@ private ExprValue exprAddDateInterval(FunctionProperties functionProperties, * @param isAdd A flag: true to isAdd, false to subtract. * @return Datetime calculated. */ - private ExprValue exprDateApplyInterval(FunctionProperties functionProperties, - ExprValue datetime, - TemporalAmount interval, - Boolean isAdd) { + private ExprValue exprDateApplyInterval( + FunctionProperties functionProperties, + ExprValue datetime, + TemporalAmount interval, + Boolean isAdd) { var dt = extractDateTime(datetime, functionProperties); return new ExprDatetimeValue(isAdd ? dt.plus(interval) : dt.minus(interval)); } - + /** - * Formats date according to format specifier. First argument is time, second is format. - * Detailed supported signatures: - * (STRING, STRING) -> STRING - * (DATE, STRING) -> STRING - * (DATETIME, STRING) -> STRING - * (TIME, STRING) -> STRING - * (TIMESTAMP, STRING) -> STRING + * Formats date according to format specifier. First argument is time, second is format. Detailed + * supported signatures: (STRING, STRING) -> STRING (DATE, STRING) -> STRING (DATETIME, STRING) -> + * STRING (TIME, STRING) -> STRING (TIMESTAMP, STRING) -> STRING */ private DefaultFunctionResolver time_format() { - return define(BuiltinFunctionName.TIME_FORMAT.getName(), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), - STRING, STRING, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), - STRING, DATE, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), - STRING, DATETIME, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), - STRING, TIME, STRING), - impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), - STRING, TIMESTAMP, STRING) - ); + return define( + BuiltinFunctionName.TIME_FORMAT.getName(), + impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), STRING, STRING, STRING), + impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), STRING, DATE, STRING), + impl( + nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), STRING, DATETIME, STRING), + impl(nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), STRING, TIME, STRING), + impl( + nullMissingHandling(DateTimeFormatterUtil::getFormattedTime), + STRING, + TIMESTAMP, + STRING)); } /** @@ -1198,8 +1329,8 @@ private DefaultFunctionResolver time_format() { * @param days ExprValue of Long type, representing the number of days to add. * @return Date/Datetime resulted from days added to `datetime`. */ - private ExprValue exprAddDateDays(FunctionProperties functionProperties, - ExprValue datetime, ExprValue days) { + private ExprValue exprAddDateDays( + FunctionProperties functionProperties, ExprValue datetime, ExprValue days) { return exprDateApplyDays(functionProperties, datetime, days.longValue(), true); } @@ -1212,11 +1343,11 @@ private ExprValue exprAddDateDays(FunctionProperties functionProperties, * @param isAdd A flag: true to add, false to subtract. * @return Datetime calculated. */ - private ExprValue exprDateApplyDays(FunctionProperties functionProperties, - ExprValue datetime, Long days, Boolean isAdd) { + private ExprValue exprDateApplyDays( + FunctionProperties functionProperties, ExprValue datetime, Long days, Boolean isAdd) { if (datetime.type() == DATE) { - return new ExprDateValue(isAdd ? datetime.dateValue().plusDays(days) - : datetime.dateValue().minusDays(days)); + return new ExprDateValue( + isAdd ? datetime.dateValue().plusDays(days) : datetime.dateValue().minusDays(days)); } var dt = extractDateTime(datetime, functionProperties); return new ExprDatetimeValue(isAdd ? dt.plusDays(days) : dt.minusDays(days)); @@ -1231,12 +1362,16 @@ private ExprValue exprDateApplyDays(FunctionProperties functionProperties, * @param isAdd A flag: true to add, false to subtract. * @return A value calculated. */ - private ExprValue exprApplyTime(FunctionProperties functionProperties, - ExprValue temporal, ExprValue temporalDelta, Boolean isAdd) { + private ExprValue exprApplyTime( + FunctionProperties functionProperties, + ExprValue temporal, + ExprValue temporalDelta, + Boolean isAdd) { var interval = Duration.between(LocalTime.MIN, temporalDelta.timeValue()); - var result = isAdd - ? extractDateTime(temporal, functionProperties).plus(interval) - : extractDateTime(temporal, functionProperties).minus(interval); + var result = + isAdd + ? extractDateTime(temporal, functionProperties).plus(interval) + : extractDateTime(temporal, functionProperties).minus(interval); return temporal.type() == TIME ? new ExprTimeValue(result.toLocalTime()) : new ExprDatetimeValue(result); @@ -1250,18 +1385,18 @@ private ExprValue exprApplyTime(FunctionProperties functionProperties, * @param temporalDelta A Date/Time/Datetime/Timestamp object to add time from. * @return A value calculated. */ - private ExprValue exprAddTime(FunctionProperties functionProperties, - ExprValue temporal, ExprValue temporalDelta) { + private ExprValue exprAddTime( + FunctionProperties functionProperties, ExprValue temporal, ExprValue temporalDelta) { return exprApplyTime(functionProperties, temporal, temporalDelta, true); } /** - * CONVERT_TZ function implementation for ExprValue. - * Returns null for time zones outside of +13:00 and -12:00. + * CONVERT_TZ function implementation for ExprValue. Returns null for time zones outside of +13:00 + * and -12:00. * * @param startingDateTime ExprValue of DateTime that is being converted from - * @param fromTz ExprValue of time zone, representing the time to convert from. - * @param toTz ExprValue of time zone, representing the time to convert to. + * @param fromTz ExprValue of time zone, representing the time to convert from. + * @param toTz ExprValue of time zone, representing the time to convert to. * @return DateTime that has been converted to the to_tz timezone. */ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, ExprValue toTz) { @@ -1278,8 +1413,7 @@ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, Ex || !DateTimeUtils.isValidMySqlTimeZoneId(convertedToTz)) { return ExprNullValue.of(); } - ZonedDateTime zonedDateTime = - startingDateTime.datetimeValue().atZone(convertedFromTz); + ZonedDateTime zonedDateTime = startingDateTime.datetimeValue().atZone(convertedFromTz); return new ExprDatetimeValue( zonedDateTime.withZoneSameInstant(convertedToTz).toLocalDateTime()); @@ -1305,19 +1439,19 @@ private ExprValue exprDate(ExprValue exprValue) { } /** - * Calculate the value in days from one date to the other. - * Only the date parts of the values are used in the calculation. + * Calculate the value in days from one date to the other. Only the date parts of the values are + * used in the calculation. * * @param first The first value. * @param second The second value. * @return The diff. */ - private ExprValue exprDateDiff(FunctionProperties functionProperties, - ExprValue first, ExprValue second) { + private ExprValue exprDateDiff( + FunctionProperties functionProperties, ExprValue first, ExprValue second) { // java inverses the value, so we have to swap 1 and 2 - return new ExprLongValue(DAYS.between( - extractDate(second, functionProperties), - extractDate(first, functionProperties))); + return new ExprLongValue( + DAYS.between( + extractDate(second, functionProperties), extractDate(first, functionProperties))); } /** @@ -1357,10 +1491,7 @@ private ExprValue exprDateTime(ExprValue dateTime, ExprValue timeZone) { ldt = new ExprDatetimeValue(dateTime.stringValue()); toTz = defaultTimeZone; } - convertTZResult = exprConvertTZ( - ldt, - new ExprStringValue(toTz), - timeZone); + convertTZResult = exprConvertTZ(ldt, new ExprStringValue(toTz), timeZone); return convertTZResult; } @@ -1426,8 +1557,8 @@ private ExprValue exprDayOfYear(ExprValue date) { public ExprLongValue formatExtractFunction(ExprValue part, ExprValue datetime) { String partName = part.stringValue().toUpperCase(); LocalDateTime arg = datetime.datetimeValue(); - String text = arg.format(DateTimeFormatter.ofPattern( - extract_formats.get(partName), Locale.ENGLISH)); + String text = + arg.format(DateTimeFormatter.ofPattern(extract_formats.get(partName), Locale.ENGLISH)); return new ExprLongValue(Long.parseLong(text)); } @@ -1450,12 +1581,10 @@ private ExprValue exprExtract(ExprValue part, ExprValue datetime) { * @param time The time to be formatted. * @return A LONG */ - private ExprValue exprExtractForTime(FunctionProperties functionProperties, - ExprValue part, - ExprValue time) { + private ExprValue exprExtractForTime( + FunctionProperties functionProperties, ExprValue part, ExprValue time) { return formatExtractFunction( - part, - new ExprDatetimeValue(extractDateTime(time, functionProperties))); + part, new ExprDatetimeValue(extractDateTime(time, functionProperties))); } /** @@ -1484,9 +1613,8 @@ private ExprValue exprFromUnixTime(ExprValue time) { private LocalDateTime exprFromUnixTimeImpl(ExprValue time) { return LocalDateTime.ofInstant( - Instant.ofEpochSecond((long)Math.floor(time.doubleValue())), - UTC_ZONE_ID) - .withNano((int)((time.doubleValue() % 1) * 1E9)); + Instant.ofEpochSecond((long) Math.floor(time.doubleValue())), UTC_ZONE_ID) + .withNano((int) ((time.doubleValue() % 1) * 1E9)); } private ExprValue exprFromUnixTimeFormat(ExprValue time, ExprValue format) { @@ -1506,9 +1634,8 @@ private ExprValue exprFromUnixTimeFormat(ExprValue time, ExprValue format) { */ private ExprValue exprGetFormat(ExprValue type, ExprValue format) { if (formats.contains(type.stringValue().toLowerCase(), format.stringValue().toLowerCase())) { - return new ExprStringValue(formats.get( - type.stringValue().toLowerCase(), - format.stringValue().toLowerCase())); + return new ExprStringValue( + formats.get(type.stringValue().toLowerCase(), format.stringValue().toLowerCase())); } return ExprNullValue.of(); @@ -1521,8 +1648,7 @@ private ExprValue exprGetFormat(ExprValue type, ExprValue format) { * @return ExprValue. */ private ExprValue exprHour(ExprValue time) { - return new ExprIntegerValue( - HOURS.between(LocalTime.MIN, time.timeValue())); + return new ExprIntegerValue(HOURS.between(LocalTime.MIN, time.timeValue())); } /** @@ -1533,9 +1659,7 @@ private ExprValue exprHour(ExprValue time) { */ private LocalDate getLastDay(LocalDate today) { return LocalDate.of( - today.getYear(), - today.getMonth(), - today.getMonth().length(today.isLeapYear())); + today.getYear(), today.getMonth(), today.getMonth().length(today.isLeapYear())); } /** @@ -1560,11 +1684,9 @@ private ExprValue exprLastDayToday(Clock clock) { /** * Following MySQL, function receives arguments of type double and rounds them before use. - * Furthermore: - * - zero year interpreted as 2000 - * - negative year is not accepted - * - @dayOfYear should be greater than 1 - * - if @dayOfYear is greater than 365/366, calculation goes to the next year(s) + * Furthermore: - zero year interpreted as 2000 - negative year is not accepted - @dayOfYear + * should be greater than 1 - if @dayOfYear is greater than 365/366, calculation goes to the next + * year(s) * * @param yearExpr year * @param dayOfYearExp day of the @year, starting from 1 @@ -1580,12 +1702,13 @@ private ExprValue exprMakeDate(ExprValue yearExpr, ExprValue dayOfYearExp) { if (0 == year) { year = 2000; } - return new ExprDateValue(LocalDate.ofYearDay((int)year, 1).plusDays(dayOfYear - 1)); + return new ExprDateValue(LocalDate.ofYearDay((int) year, 1).plusDays(dayOfYear - 1)); } /** * Following MySQL, function receives arguments of type double. @hour and @minute are rounded, * while @second used as is, including fraction part. + * * @param hourExpr hour * @param minuteExpr minute * @param secondExpr second @@ -1598,8 +1721,9 @@ private ExprValue exprMakeTime(ExprValue hourExpr, ExprValue minuteExpr, ExprVal if (0 > hour || 0 > minute || 0 > second) { return ExprNullValue.of(); } - return new ExprTimeValue(LocalTime.parse(String.format("%02d:%02d:%012.9f", - hour, minute, second), DateTimeFormatter.ISO_TIME)); + return new ExprTimeValue( + LocalTime.parse( + String.format("%02d:%02d:%012.9f", hour, minute, second), DateTimeFormatter.ISO_TIME)); } /** @@ -1620,8 +1744,7 @@ private ExprValue exprMicrosecond(ExprValue time) { * @return ExprValue. */ private ExprValue exprMinute(ExprValue time) { - return new ExprIntegerValue( - (MINUTES.between(LocalTime.MIN, time.timeValue()) % 60)); + return new ExprIntegerValue((MINUTES.between(LocalTime.MIN, time.timeValue()) % 60)); } /** @@ -1631,8 +1754,7 @@ private ExprValue exprMinute(ExprValue time) { * @return ExprValue. */ private ExprValue exprMinuteOfDay(ExprValue time) { - return new ExprIntegerValue( - MINUTES.between(LocalTime.MIN, time.timeValue())); + return new ExprIntegerValue(MINUTES.between(LocalTime.MIN, time.timeValue())); } /** @@ -1675,8 +1797,7 @@ private LocalDate parseDatePeriod(Integer period) { } /** - * Adds N months to period P (in the format YYMM or YYYYMM). - * Returns a value in the format YYYYMM. + * Adds N months to period P (in the format YYMM or YYYYMM). Returns a value in the format YYYYMM. * * @param period Period in the format YYMM or YYYYMM. * @param months Amount of months to add. @@ -1684,19 +1805,20 @@ private LocalDate parseDatePeriod(Integer period) { */ private ExprValue exprPeriodAdd(ExprValue period, ExprValue months) { // We should add a day to make string parsable and remove it afterwards - var input = period.integerValue() * 100 + 1; // adds 01 to end of the string + var input = period.integerValue() * 100 + 1; // adds 01 to end of the string var parsedDate = parseDatePeriod(input); if (parsedDate == null) { return ExprNullValue.of(); } var res = DATE_FORMATTER_LONG_YEAR.format(parsedDate.plusMonths(months.integerValue())); - return new ExprIntegerValue(Integer.parseInt( - res.substring(0, res.length() - 2))); // Remove the day part, .eg. 20070101 -> 200701 + return new ExprIntegerValue( + Integer.parseInt( + res.substring(0, res.length() - 2))); // Remove the day part, .eg. 20070101 -> 200701 } /** - * Returns the number of months between periods P1 and P2. - * P1 and P2 should be in the format YYMM or YYYYMM. + * Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM + * or YYYYMM. * * @param period1 Period in the format YYMM or YYYYMM. * @param period2 Period in the format YYMM or YYYYMM. @@ -1724,6 +1846,7 @@ private ExprValue exprQuarter(ExprValue date) { /** * Returns TIME value of sec_to_time function for an INTEGER or LONG arguments. + * * @param totalSeconds The total number of seconds * @return A TIME value */ @@ -1732,17 +1855,17 @@ private ExprValue exprSecToTime(ExprValue totalSeconds) { } /** - * Helper function which obtains the decimal portion of the seconds value passed in. - * Uses BigDecimal to prevent issues with math on floating point numbers. - * Return is formatted to be used with Duration.ofSeconds(); + * Helper function which obtains the decimal portion of the seconds value passed in. Uses + * BigDecimal to prevent issues with math on floating point numbers. Return is formatted to be + * used with Duration.ofSeconds(); * * @param seconds and ExprDoubleValue or ExprFloatValue for the seconds * @return A LONG representing the nanoseconds portion */ private long formatNanos(ExprValue seconds) { - //Convert ExprValue to BigDecimal + // Convert ExprValue to BigDecimal BigDecimal formattedNanos = BigDecimal.valueOf(seconds.doubleValue()); - //Extract only the nanosecond part + // Extract only the nanosecond part formattedNanos = formattedNanos.subtract(BigDecimal.valueOf(formattedNanos.intValue())); return formattedNanos.scaleByPowerOfTen(9).longValue(); @@ -1750,6 +1873,7 @@ private long formatNanos(ExprValue seconds) { /** * Returns TIME value of sec_to_time function for FLOAT or DOUBLE arguments. + * * @param totalSeconds The total number of seconds * @return A TIME value */ @@ -1767,8 +1891,7 @@ private ExprValue exprSecToTimeWithNanos(ExprValue totalSeconds) { * @return ExprValue. */ private ExprValue exprSecond(ExprValue time) { - return new ExprIntegerValue( - (SECONDS.between(LocalTime.MIN, time.timeValue()) % 60)); + return new ExprIntegerValue((SECONDS.between(LocalTime.MIN, time.timeValue()) % 60)); } /** @@ -1779,8 +1902,8 @@ private ExprValue exprSecond(ExprValue time) { * @param days ExprValue of Long type, representing the number of days to subtract. * @return Date/Datetime resulted from days subtracted to date. */ - private ExprValue exprSubDateDays(FunctionProperties functionProperties, - ExprValue date, ExprValue days) { + private ExprValue exprSubDateDays( + FunctionProperties functionProperties, ExprValue date, ExprValue days) { return exprDateApplyDays(functionProperties, date, days.longValue(), false); } @@ -1792,8 +1915,8 @@ private ExprValue exprSubDateDays(FunctionProperties functionProperties, * @param expr ExprValue of Interval type, the temporal amount to subtract. * @return Datetime resulted from expr subtracted to `datetime`. */ - private ExprValue exprSubDateInterval(FunctionProperties functionProperties, - ExprValue datetime, ExprValue expr) { + private ExprValue exprSubDateInterval( + FunctionProperties functionProperties, ExprValue datetime, ExprValue expr) { return exprDateApplyInterval(functionProperties, datetime, expr.intervalValue(), false); } @@ -1804,14 +1927,13 @@ private ExprValue exprSubDateInterval(FunctionProperties functionProperties, * @param temporalDelta A Date/Time/Datetime/Timestamp to subtract time from. * @return A value calculated. */ - private ExprValue exprSubTime(FunctionProperties functionProperties, - ExprValue temporal, ExprValue temporalDelta) { + private ExprValue exprSubTime( + FunctionProperties functionProperties, ExprValue temporal, ExprValue temporalDelta) { return exprApplyTime(functionProperties, temporal, temporalDelta, false); } - private ExprValue exprStrToDate(FunctionProperties fp, - ExprValue dateTimeExpr, - ExprValue formatStringExp) { + private ExprValue exprStrToDate( + FunctionProperties fp, ExprValue dateTimeExpr, ExprValue formatStringExp) { return DateTimeFormatterUtil.parseStringWithDateOrTime(fp, dateTimeExpr, formatStringExp); } @@ -1838,8 +1960,8 @@ private ExprValue exprTime(ExprValue exprValue) { */ private ExprValue exprTimeDiff(ExprValue first, ExprValue second) { // java inverses the value, so we have to swap 1 and 2 - return new ExprTimeValue(LocalTime.MIN.plus( - Duration.between(second.timeValue(), first.timeValue()))); + return new ExprTimeValue( + LocalTime.MIN.plus(Duration.between(second.timeValue(), first.timeValue()))); } /** @@ -1852,9 +1974,8 @@ private ExprValue exprTimeToSec(ExprValue time) { return new ExprLongValue(time.timeValue().toSecondOfDay()); } - private ExprValue exprTimestampAdd(ExprValue partExpr, - ExprValue amountExpr, - ExprValue datetimeExpr) { + private ExprValue exprTimestampAdd( + ExprValue partExpr, ExprValue amountExpr, ExprValue datetimeExpr) { String part = partExpr.stringValue(); int amount = amountExpr.integerValue(); LocalDateTime datetime = datetimeExpr.datetimeValue(); @@ -1895,13 +2016,9 @@ private ExprValue exprTimestampAdd(ExprValue partExpr, return new ExprDatetimeValue(datetime.plus(amount, temporalUnit)); } - private ExprValue exprTimestampAddForTimeType(Clock clock, - ExprValue partExpr, - ExprValue amountExpr, - ExprValue timeExpr) { - LocalDateTime datetime = LocalDateTime.of( - formatNow(clock).toLocalDate(), - timeExpr.timeValue()); + private ExprValue exprTimestampAddForTimeType( + Clock clock, ExprValue partExpr, ExprValue amountExpr, ExprValue timeExpr) { + LocalDateTime datetime = LocalDateTime.of(formatNow(clock).toLocalDate(), timeExpr.timeValue()); return exprTimestampAdd(partExpr, amountExpr, new ExprDatetimeValue(datetime)); } @@ -1942,19 +2059,13 @@ private ExprValue getTimeDifference(String part, LocalDateTime startTime, LocalD } private ExprValue exprTimestampDiff( - ExprValue partExpr, - ExprValue startTimeExpr, - ExprValue endTimeExpr) { + ExprValue partExpr, ExprValue startTimeExpr, ExprValue endTimeExpr) { return getTimeDifference( - partExpr.stringValue(), - startTimeExpr.datetimeValue(), - endTimeExpr.datetimeValue()); + partExpr.stringValue(), startTimeExpr.datetimeValue(), endTimeExpr.datetimeValue()); } - private ExprValue exprTimestampDiffForTimeType(FunctionProperties fp, - ExprValue partExpr, - ExprValue startTimeExpr, - ExprValue endTimeExpr) { + private ExprValue exprTimestampDiffForTimeType( + FunctionProperties fp, ExprValue partExpr, ExprValue startTimeExpr, ExprValue endTimeExpr) { return getTimeDifference( partExpr.stringValue(), extractDateTime(startTimeExpr, fp), @@ -1988,8 +2099,8 @@ private ExprValue exprUtcTime(FunctionProperties functionProperties) { * @return ExprValue. */ private ExprValue exprUtcTimeStamp(FunctionProperties functionProperties) { - var zdt = ZonedDateTime.now(functionProperties.getQueryStartClock()) - .withZoneSameInstant(UTC_ZONE_ID); + var zdt = + ZonedDateTime.now(functionProperties.getQueryStartClock()).withZoneSameInstant(UTC_ZONE_ID); return new ExprDatetimeValue(zdt.toLocalDateTime()); } @@ -2027,25 +2138,25 @@ private DateTimeFormatter getFormatter(int dateAsInt) { throw new DateTimeException("Integer argument was out of range"); } - //Check below from YYYYMMDD - MMDD which format should be used + // Check below from YYYYMMDD - MMDD which format should be used switch (length) { - //Check if dateAsInt is at least 8 digits long + // Check if dateAsInt is at least 8 digits long case FULL_DATE_LENGTH: return DATE_FORMATTER_LONG_YEAR; - //Check if dateAsInt is at least 6 digits long + // Check if dateAsInt is at least 6 digits long case SHORT_DATE_LENGTH: return DATE_FORMATTER_SHORT_YEAR; - //Check if dateAsInt is at least 5 digits long + // Check if dateAsInt is at least 5 digits long case SINGLE_DIGIT_YEAR_DATE_LENGTH: return DATE_FORMATTER_SINGLE_DIGIT_YEAR; - //Check if dateAsInt is at least 4 digits long + // Check if dateAsInt is at least 4 digits long case NO_YEAR_DATE_LENGTH: return DATE_FORMATTER_NO_YEAR; - //Check if dateAsInt is at least 3 digits long + // Check if dateAsInt is at least 3 digits long case SINGLE_DIGIT_MONTH_DATE_LENGTH: return DATE_FORMATTER_SINGLE_DIGIT_MONTH; @@ -2064,15 +2175,16 @@ private DateTimeFormatter getFormatter(int dateAsInt) { */ private ExprValue exprToSecondsForIntType(ExprValue dateExpr) { try { - //Attempt to parse integer argument as date - LocalDate date = LocalDate.parse(String.valueOf(dateExpr.integerValue()), - getFormatter(dateExpr.integerValue())); + // Attempt to parse integer argument as date + LocalDate date = + LocalDate.parse( + String.valueOf(dateExpr.integerValue()), getFormatter(dateExpr.integerValue())); - return new ExprLongValue(date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC) - + DAYS_0000_TO_1970 * SECONDS_PER_DAY); + return new ExprLongValue( + date.toEpochSecond(LocalTime.MIN, ZoneOffset.UTC) + DAYS_0000_TO_1970 * SECONDS_PER_DAY); } catch (DateTimeException ignored) { - //Return null if parsing error + // Return null if parsing error return ExprNullValue.of(); } } @@ -2121,12 +2233,14 @@ private ExprValue unixTimeStampOf(ExprValue value) { private Double unixTimeStampOfImpl(ExprValue value) { // Also, according to MySQL documentation: // The date argument may be a DATE, DATETIME, or TIMESTAMP ... - switch ((ExprCoreType)value.type()) { - case DATE: return value.dateValue().toEpochSecond(LocalTime.MIN, ZoneOffset.UTC) + 0d; - case DATETIME: return value.datetimeValue().toEpochSecond(ZoneOffset.UTC) - + value.datetimeValue().getNano() / 1E9; - case TIMESTAMP: return value.timestampValue().getEpochSecond() - + value.timestampValue().getNano() / 1E9; + switch ((ExprCoreType) value.type()) { + case DATE: + return value.dateValue().toEpochSecond(LocalTime.MIN, ZoneOffset.UTC) + 0d; + case DATETIME: + return value.datetimeValue().toEpochSecond(ZoneOffset.UTC) + + value.datetimeValue().getNano() / 1E9; + case TIMESTAMP: + return value.timestampValue().getEpochSecond() + value.timestampValue().getNano() / 1E9; default: // ... or a number in YYMMDD, YYMMDDhhmmss, YYYYMMDD, or YYYYMMDDhhmmss format. // If the argument includes a time part, it may optionally include a fractional @@ -2172,8 +2286,8 @@ private Double unixTimeStampOfImpl(ExprValue value) { } /** - * Week for date implementation for ExprValue. - * When mode is not specified default value mode 0 is used for default_week_format. + * Week for date implementation for ExprValue. When mode is not specified default value mode 0 is + * used for default_week_format. * * @param date ExprValue of Date/Datetime/Timestamp/String type. * @return ExprValue. @@ -2203,12 +2317,11 @@ private ExprIntegerValue extractYearweek(LocalDate date, int mode) { // Needed to align with MySQL. Due to how modes for this function work. // See description of modes here ... // https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week - int modeJava = CalendarLookup.getWeekNumber(mode, date) != 0 ? mode : - mode <= 4 ? 2 : - 7; + int modeJava = CalendarLookup.getWeekNumber(mode, date) != 0 ? mode : mode <= 4 ? 2 : 7; - int formatted = CalendarLookup.getYearNumber(modeJava, date) * 100 - + CalendarLookup.getWeekNumber(modeJava, date); + int formatted = + CalendarLookup.getYearNumber(modeJava, date) * 100 + + CalendarLookup.getWeekNumber(modeJava, date); return new ExprIntegerValue(formatted); } @@ -2224,8 +2337,8 @@ private ExprValue exprYearweek(ExprValue date, ExprValue mode) { } /** - * Yearweek for date implementation for ExprValue. - * When mode is not specified default value mode 0 is used. + * Yearweek for date implementation for ExprValue. When mode is not specified default value mode 0 + * is used. * * @param date ExprValue of Date/Datetime/Time/Timestamp/String type. * @return ExprValue. @@ -2248,19 +2361,22 @@ private LocalDateTime formatNow(Clock clock) { /** * Prepare LocalDateTime value. Truncate fractional second part according to the argument. - * @param fsp argument is given to specify a fractional seconds precision from 0 to 6, - * the return value includes a fractional seconds part of that many digits. + * + * @param fsp argument is given to specify a fractional seconds precision from 0 to 6, the return + * value includes a fractional seconds part of that many digits. * @return LocalDateTime object. */ - private LocalDateTime formatNow(Clock clock, Integer fsp) { + private LocalDateTime formatNow(Clock clock, Integer fsp) { var res = LocalDateTime.now(clock); var defaultPrecision = 9; // There are 10^9 nanoseconds in one second if (fsp < 0 || fsp > 6) { // Check that the argument is in the allowed range [0, 6] throw new IllegalArgumentException( String.format("Invalid `fsp` value: %d, allowed 0 to 6", fsp)); } - var nano = new BigDecimal(res.getNano()) - .setScale(fsp - defaultPrecision, RoundingMode.DOWN).intValue(); + var nano = + new BigDecimal(res.getNano()) + .setScale(fsp - defaultPrecision, RoundingMode.DOWN) + .intValue(); return res.withNano(nano); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/datetime/IntervalClause.java b/core/src/main/java/org/opensearch/sql/expression/datetime/IntervalClause.java index 3df8489b20f..5170d49fc72 100644 --- a/core/src/main/java/org/opensearch/sql/expression/datetime/IntervalClause.java +++ b/core/src/main/java/org/opensearch/sql/expression/datetime/IntervalClause.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.opensearch.sql.data.model.ExprValueUtils.getIntegerValue; @@ -45,7 +44,8 @@ public void register(BuiltinFunctionRepository repository) { } private DefaultFunctionResolver interval() { - return define(BuiltinFunctionName.INTERVAL.getName(), + return define( + BuiltinFunctionName.INTERVAL.getName(), impl(nullMissingHandling(IntervalClause::interval), INTERVAL, INTEGER, STRING), impl(nullMissingHandling(IntervalClause::interval), INTERVAL, LONG, STRING)); } diff --git a/core/src/main/java/org/opensearch/sql/expression/env/Environment.java b/core/src/main/java/org/opensearch/sql/expression/env/Environment.java index d96d0c0a502..b1377f22aec 100644 --- a/core/src/main/java/org/opensearch/sql/expression/env/Environment.java +++ b/core/src/main/java/org/opensearch/sql/expression/env/Environment.java @@ -3,33 +3,30 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.env; /** * The definition of the environment. - * @param the type of expression + * + * @param the type of expression * @param the type of expression value */ public interface Environment { - /** - * resolve the value of expression from the environment. - */ + /** resolve the value of expression from the environment. */ V resolve(E var); /** * Extend the environment. * - * @param env environment - * @param expr expression. - * @param value expression value. - * @param the type of expression + * @param env environment + * @param expr expression. + * @param value expression value. + * @param the type of expression * @param the type of expression value * @return extended environment. */ - static Environment extendEnv( - Environment env, E expr, V value) { + static Environment extendEnv(Environment env, E expr, V value) { return var -> { if (var.equals(expr)) { return value; diff --git a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java index 728712f5372..f50fa927b88 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java @@ -12,15 +12,11 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -/** - * Builtin Function Name. - */ +/** Builtin Function Name. */ @Getter @RequiredArgsConstructor public enum BuiltinFunctionName { - /** - * Mathematical Functions. - */ + /** Mathematical Functions. */ ABS(FunctionName.of("abs")), CEIL(FunctionName.of("ceil")), CEILING(FunctionName.of("ceiling")), @@ -59,9 +55,7 @@ public enum BuiltinFunctionName { SIN(FunctionName.of("sin")), TAN(FunctionName.of("tan")), - /** - * Date and Time Functions. - */ + /** Date and Time Functions. */ ADDDATE(FunctionName.of("adddate")), ADDTIME(FunctionName.of("addtime")), CONVERT_TZ(FunctionName.of("convert_tz")), @@ -135,14 +129,10 @@ public enum BuiltinFunctionName { LOCALTIMESTAMP(FunctionName.of("localtimestamp")), SYSDATE(FunctionName.of("sysdate")), - /** - * Text Functions. - */ + /** Text Functions. */ TOSTRING(FunctionName.of("tostring")), - /** - * Arithmetic Operators. - */ + /** Arithmetic Operators. */ ADD(FunctionName.of("+")), ADDFUNCTION(FunctionName.of("add")), DIVIDE(FunctionName.of("/")), @@ -155,9 +145,7 @@ public enum BuiltinFunctionName { SUBTRACT(FunctionName.of("-")), SUBTRACTFUNCTION(FunctionName.of("subtract")), - /** - * Boolean Operators. - */ + /** Boolean Operators. */ AND(FunctionName.of("and")), OR(FunctionName.of("or")), XOR(FunctionName.of("xor")), @@ -171,9 +159,7 @@ public enum BuiltinFunctionName { LIKE(FunctionName.of("like")), NOT_LIKE(FunctionName.of("not like")), - /** - * Aggregation Function. - */ + /** Aggregation Function. */ AVG(FunctionName.of("avg")), SUM(FunctionName.of("sum")), COUNT(FunctionName.of("count")), @@ -192,9 +178,7 @@ public enum BuiltinFunctionName { // Not always an aggregation query NESTED(FunctionName.of("nested")), - /** - * Text Functions. - */ + /** Text Functions. */ ASCII(FunctionName.of("ascii")), CONCAT(FunctionName.of("concat")), CONCAT_WS(FunctionName.of("concat_ws")), @@ -215,9 +199,7 @@ public enum BuiltinFunctionName { TRIM(FunctionName.of("trim")), UPPER(FunctionName.of("upper")), - /** - * NULL Test. - */ + /** NULL Test. */ IS_NULL(FunctionName.of("is null")), IS_NOT_NULL(FunctionName.of("is not null")), IFNULL(FunctionName.of("ifnull")), @@ -231,9 +213,7 @@ public enum BuiltinFunctionName { INTERVAL(FunctionName.of("interval")), - /** - * Data Type Convert Function. - */ + /** Data Type Convert Function. */ CAST_TO_STRING(FunctionName.of("cast_to_string")), CAST_TO_BYTE(FunctionName.of("cast_to_byte")), CAST_TO_SHORT(FunctionName.of("cast_to_short")), @@ -248,9 +228,7 @@ public enum BuiltinFunctionName { CAST_TO_DATETIME(FunctionName.of("cast_to_datetime")), TYPEOF(FunctionName.of("typeof")), - /** - * Relevance Function. - */ + /** Relevance Function. */ MATCH(FunctionName.of("match")), SIMPLE_QUERY_STRING(FunctionName.of("simple_query_string")), MATCH_PHRASE(FunctionName.of("match_phrase")), @@ -264,9 +242,7 @@ public enum BuiltinFunctionName { SCOREQUERY(FunctionName.of("scorequery")), SCORE_QUERY(FunctionName.of("score_query")), - /** - * Legacy Relevance Function. - */ + /** Legacy Relevance Function. */ QUERY(FunctionName.of("query")), MATCH_QUERY(FunctionName.of("match_query")), MATCHQUERY(FunctionName.of("matchquery")), diff --git a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionRepository.java b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionRepository.java index 0eb11a92806..2e16d5f01f6 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionRepository.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionRepository.java @@ -38,10 +38,8 @@ import org.opensearch.sql.storage.StorageEngine; /** - * Builtin Function Repository. - * Repository registers datasource specific functions under datasource namespace and - * universal functions under default namespace. - * + * Builtin Function Repository. Repository registers datasource specific functions under datasource + * namespace and universal functions under default namespace. */ public class BuiltinFunctionRepository { @@ -96,23 +94,20 @@ public void register(FunctionResolver resolver) { functionResolverMap.put(resolver.getFunctionName(), resolver); } - /** - * Compile FunctionExpression using core function resolver. - * - */ - public FunctionImplementation compile(FunctionProperties functionProperties, - FunctionName functionName, List expressions) { + /** Compile FunctionExpression using core function resolver. */ + public FunctionImplementation compile( + FunctionProperties functionProperties, + FunctionName functionName, + List expressions) { return compile(functionProperties, Collections.emptyList(), functionName, expressions); } - - /** - * Compile FunctionExpression within {@link StorageEngine} provided {@link FunctionResolver}. - */ - public FunctionImplementation compile(FunctionProperties functionProperties, - Collection dataSourceFunctionResolver, - FunctionName functionName, - List expressions) { + /** Compile FunctionExpression within {@link StorageEngine} provided {@link FunctionResolver}. */ + public FunctionImplementation compile( + FunctionProperties functionProperties, + Collection dataSourceFunctionResolver, + FunctionName functionName, + List expressions) { FunctionBuilder resolvedFunctionBuilder = resolve( dataSourceFunctionResolver, @@ -134,8 +129,9 @@ public FunctionImplementation compile(FunctionProperties functionProperties, public FunctionBuilder resolve( Collection dataSourceFunctionResolver, FunctionSignature functionSignature) { - Map dataSourceFunctionMap = dataSourceFunctionResolver.stream() - .collect(Collectors.toMap(FunctionResolver::getFunctionName, t -> t)); + Map dataSourceFunctionMap = + dataSourceFunctionResolver.stream() + .collect(Collectors.toMap(FunctionResolver::getFunctionName, t -> t)); // first, resolve in datasource provide function resolver. // second, resolve in builtin function resolver. @@ -171,14 +167,13 @@ private Optional resolve( } /** - * Wrap resolved function builder's arguments by cast function to cast input expression value - * to value of target type at runtime. For example, suppose unresolved signature is - * equal(BOOL,STRING) and its resolved function builder is F with signature equal(BOOL,BOOL). - * In this case, wrap F and return equal(BOOL, cast_to_bool(STRING)). + * Wrap resolved function builder's arguments by cast function to cast input expression value to + * value of target type at runtime. For example, suppose unresolved signature is + * equal(BOOL,STRING) and its resolved function builder is F with signature equal(BOOL,BOOL). In + * this case, wrap F and return equal(BOOL, cast_to_bool(STRING)). */ - private FunctionBuilder castArguments(List sourceTypes, - List targetTypes, - FunctionBuilder funcBuilder) { + private FunctionBuilder castArguments( + List sourceTypes, List targetTypes, FunctionBuilder funcBuilder) { return (fp, arguments) -> { List argsCasted = new ArrayList<>(); for (int i = 0; i < arguments.size(); i++) { @@ -208,10 +203,10 @@ private boolean isCastRequired(ExprType sourceType, ExprType targetType) { private Function cast(Expression arg, ExprType targetType) { FunctionName castFunctionName = getCastFunctionName(targetType); if (castFunctionName == null) { - throw new ExpressionEvaluationException(StringUtils.format( - "Type conversion to type %s is not supported", targetType)); + throw new ExpressionEvaluationException( + StringUtils.format("Type conversion to type %s is not supported", targetType)); } - return functionProperties -> (Expression) compile(functionProperties, - castFunctionName, List.of(arg)); + return functionProperties -> + (Expression) compile(functionProperties, castFunctionName, List.of(arg)); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/DefaultFunctionResolver.java b/core/src/main/java/org/opensearch/sql/expression/function/DefaultFunctionResolver.java index a28fa7e0ada..5d0f31594b2 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/DefaultFunctionResolver.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/DefaultFunctionResolver.java @@ -18,52 +18,53 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; /** - * The Function Resolver hold the overload {@link FunctionBuilder} implementation. - * is composed by {@link FunctionName} which identified the function name - * and a map of {@link FunctionSignature} and {@link FunctionBuilder} - * to represent the overloaded implementation + * The Function Resolver hold the overload {@link FunctionBuilder} implementation. is composed by + * {@link FunctionName} which identified the function name and a map of {@link FunctionSignature} + * and {@link FunctionBuilder} to represent the overloaded implementation */ @Builder @RequiredArgsConstructor public class DefaultFunctionResolver implements FunctionResolver { - @Getter - private final FunctionName functionName; + @Getter private final FunctionName functionName; + @Singular("functionBundle") private final Map functionBundle; /** - * Resolve the {@link FunctionBuilder} by using input {@link FunctionSignature}. - * If the {@link FunctionBuilder} exactly match the input {@link FunctionSignature}, return it. - * If applying the widening rule, found the most match one, return it. - * If nothing found, throw {@link ExpressionEvaluationException} + * Resolve the {@link FunctionBuilder} by using input {@link FunctionSignature}. If the {@link + * FunctionBuilder} exactly match the input {@link FunctionSignature}, return it. If applying the + * widening rule, found the most match one, return it. If nothing found, throw {@link + * ExpressionEvaluationException} * * @return function signature and its builder */ @Override public Pair resolve(FunctionSignature unresolvedSignature) { - PriorityQueue> functionMatchQueue = new PriorityQueue<>( - Map.Entry.comparingByKey()); + PriorityQueue> functionMatchQueue = + new PriorityQueue<>(Map.Entry.comparingByKey()); for (FunctionSignature functionSignature : functionBundle.keySet()) { functionMatchQueue.add( - new AbstractMap.SimpleEntry<>(unresolvedSignature.match(functionSignature), - functionSignature)); + new AbstractMap.SimpleEntry<>( + unresolvedSignature.match(functionSignature), functionSignature)); } Map.Entry bestMatchEntry = functionMatchQueue.peek(); if (FunctionSignature.isVarArgFunction(bestMatchEntry.getValue().getParamTypeList()) - && (unresolvedSignature.getParamTypeList().isEmpty() + && (unresolvedSignature.getParamTypeList().isEmpty() || unresolvedSignature.getParamTypeList().size() > 9)) { throw new ExpressionEvaluationException( - String.format("%s function expected 1-9 arguments, but got %d", - functionName, unresolvedSignature.getParamTypeList().size())); + String.format( + "%s function expected 1-9 arguments, but got %d", + functionName, unresolvedSignature.getParamTypeList().size())); } if (FunctionSignature.NOT_MATCH.equals(bestMatchEntry.getKey()) - && !FunctionSignature.isVarArgFunction(bestMatchEntry.getValue().getParamTypeList())) { + && !FunctionSignature.isVarArgFunction(bestMatchEntry.getValue().getParamTypeList())) { throw new ExpressionEvaluationException( - String.format("%s function expected %s, but get %s", functionName, + String.format( + "%s function expected %s, but get %s", + functionName, formatFunctions(functionBundle.keySet()), - unresolvedSignature.formatTypes() - )); + unresolvedSignature.formatTypes())); } else { FunctionSignature resolvedSignature = bestMatchEntry.getValue(); return Pair.of(resolvedSignature, functionBundle.get(resolvedSignature)); @@ -71,7 +72,8 @@ public Pair resolve(FunctionSignature unreso } private String formatFunctions(Set functionSignatures) { - return functionSignatures.stream().map(FunctionSignature::formatTypes) + return functionSignatures.stream() + .map(FunctionSignature::formatTypes) .collect(Collectors.joining(",", "{", "}")); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionBuilder.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionBuilder.java index b6e32a1d27c..a529885c166 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionBuilder.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionBuilder.java @@ -3,15 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.util.List; import org.opensearch.sql.expression.Expression; /** - * The definition of function which create {@link FunctionImplementation} - * from input {@link Expression} list. + * The definition of function which create {@link FunctionImplementation} from input {@link + * Expression} list. */ public interface FunctionBuilder { @@ -19,7 +18,7 @@ public interface FunctionBuilder { * Create {@link FunctionImplementation} from input {@link Expression} list. * * @param functionProperties context for function execution. - * @param arguments {@link Expression} list. + * @param arguments {@link Expression} list. * @return {@link FunctionImplementation} */ FunctionImplementation apply(FunctionProperties functionProperties, List arguments); diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionDSL.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionDSL.java index c57d96caea4..8ebbfd3a3c6 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionDSL.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionDSL.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.util.Arrays; @@ -21,21 +20,19 @@ import org.opensearch.sql.expression.env.Environment; import org.opensearch.sql.expression.function.DefaultFunctionResolver.DefaultFunctionResolverBuilder; -/** - * Function Define Utility. - */ +/** Function Define Utility. */ @UtilityClass public class FunctionDSL { /** * Define overloaded function with implementation. * * @param functionName function name. - * @param functions a list of function implementation. + * @param functions a list of function implementation. * @return FunctionResolver. */ - public static DefaultFunctionResolver define(FunctionName functionName, - SerializableFunction>... functions) { + public static DefaultFunctionResolver define( + FunctionName functionName, + SerializableFunction>... functions) { return define(functionName, List.of(functions)); } @@ -43,11 +40,13 @@ public static DefaultFunctionResolver define(FunctionName functionName, * Define overloaded function with implementation. * * @param functionName function name. - * @param functions a list of function implementation. + * @param functions a list of function implementation. * @return FunctionResolver. */ - public static DefaultFunctionResolver define(FunctionName functionName, List< - SerializableFunction>> functions) { + public static DefaultFunctionResolver define( + FunctionName functionName, + List>> + functions) { DefaultFunctionResolverBuilder builder = DefaultFunctionResolver.builder(); builder.functionName(functionName); @@ -58,7 +57,6 @@ public static DefaultFunctionResolver define(FunctionName functionName, List< return builder.build(); } - /** * Implementation of no args function that uses FunctionProperties. * @@ -67,8 +65,8 @@ public static DefaultFunctionResolver define(FunctionName functionName, List< * @return no args function implementation. */ public static SerializableFunction> - implWithProperties(SerializableFunction function, - ExprType returnType) { + implWithProperties( + SerializableFunction function, ExprType returnType) { return functionName -> { FunctionSignature functionSignature = new FunctionSignature(functionName, Collections.emptyList()); @@ -95,53 +93,54 @@ public String toString() { } /** - * Implementation of a function that takes one argument, returns a value, and - * requires FunctionProperties to complete. + * Implementation of a function that takes one argument, returns a value, and requires + * FunctionProperties to complete. * - * @param function {@link ExprValue} based unary function. + * @param function {@link ExprValue} based unary function. * @param returnType return type. * @param argsType argument type. * @return Unary Function Implementation. */ public static SerializableFunction> implWithProperties( - SerializableBiFunction function, - ExprType returnType, - ExprType argsType) { + SerializableBiFunction function, + ExprType returnType, + ExprType argsType) { return functionName -> { FunctionSignature functionSignature = new FunctionSignature(functionName, Collections.singletonList(argsType)); FunctionBuilder functionBuilder = - (functionProperties, arguments) -> new FunctionExpression(functionName, arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - ExprValue value = arguments.get(0).valueOf(valueEnv); - return function.apply(functionProperties, value); - } - - @Override - public ExprType type() { - return returnType; - } - - @Override - public String toString() { - return String.format("%s(%s)", functionName, - arguments.stream() - .map(Object::toString) - .collect(Collectors.joining(", "))); - } - }; + (functionProperties, arguments) -> + new FunctionExpression(functionName, arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + ExprValue value = arguments.get(0).valueOf(valueEnv); + return function.apply(functionProperties, value); + } + + @Override + public ExprType type() { + return returnType; + } + + @Override + public String toString() { + return String.format( + "%s(%s)", + functionName, + arguments.stream().map(Object::toString).collect(Collectors.joining(", "))); + } + }; return Pair.of(functionSignature, functionBuilder); }; } /** - * Implementation of a function that takes two arguments, returns a value, and - * requires FunctionProperties to complete. + * Implementation of a function that takes two arguments, returns a value, and requires + * FunctionProperties to complete. * - * @param function {@link ExprValue} based Binary function. + * @param function {@link ExprValue} based Binary function. * @param returnType return type. * @param args1Type first argument type. * @param args2Type second argument type. @@ -149,45 +148,46 @@ public String toString() { */ public static SerializableFunction> implWithProperties( - SerializableTriFunction function, - ExprType returnType, - ExprType args1Type, - ExprType args2Type) { + SerializableTriFunction function, + ExprType returnType, + ExprType args1Type, + ExprType args2Type) { return functionName -> { FunctionSignature functionSignature = new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type)); FunctionBuilder functionBuilder = - (functionProperties, arguments) -> new FunctionExpression(functionName, arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - ExprValue arg1 = arguments.get(0).valueOf(valueEnv); - ExprValue arg2 = arguments.get(1).valueOf(valueEnv); - return function.apply(functionProperties, arg1, arg2); - } - - @Override - public ExprType type() { - return returnType; - } - - @Override - public String toString() { - return String.format("%s(%s)", functionName, - arguments.stream() - .map(Object::toString) - .collect(Collectors.joining(", "))); - } - }; + (functionProperties, arguments) -> + new FunctionExpression(functionName, arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + ExprValue arg1 = arguments.get(0).valueOf(valueEnv); + ExprValue arg2 = arguments.get(1).valueOf(valueEnv); + return function.apply(functionProperties, arg1, arg2); + } + + @Override + public ExprType type() { + return returnType; + } + + @Override + public String toString() { + return String.format( + "%s(%s)", + functionName, + arguments.stream().map(Object::toString).collect(Collectors.joining(", "))); + } + }; return Pair.of(functionSignature, functionBuilder); }; } /** - * Implementation of a function that takes three arguments, returns a value, and - * requires FunctionProperties to complete. + * Implementation of a function that takes three arguments, returns a value, and requires + * FunctionProperties to complete. * - * @param function {@link ExprValue} based Binary function. + * @param function {@link ExprValue} based Binary function. * @param returnType return type. * @param args1Type first argument type. * @param args2Type second argument type. @@ -196,43 +196,40 @@ public String toString() { */ public static SerializableFunction> implWithProperties( - SerializableQuadFunction< - FunctionProperties, - ExprValue, - ExprValue, - ExprValue, - ExprValue> function, - ExprType returnType, - ExprType args1Type, - ExprType args2Type, - ExprType args3Type) { + SerializableQuadFunction + function, + ExprType returnType, + ExprType args1Type, + ExprType args2Type, + ExprType args3Type) { return functionName -> { FunctionSignature functionSignature = new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type, args3Type)); FunctionBuilder functionBuilder = - (functionProperties, arguments) -> new FunctionExpression(functionName, arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - ExprValue arg1 = arguments.get(0).valueOf(valueEnv); - ExprValue arg2 = arguments.get(1).valueOf(valueEnv); - ExprValue arg3 = arguments.get(2).valueOf(valueEnv); - return function.apply(functionProperties, arg1, arg2, arg3); - } - - @Override - public ExprType type() { - return returnType; - } - - @Override - public String toString() { - return String.format("%s(%s)", functionName, - arguments.stream() - .map(Object::toString) - .collect(Collectors.joining(", "))); - } - }; + (functionProperties, arguments) -> + new FunctionExpression(functionName, arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + ExprValue arg1 = arguments.get(0).valueOf(valueEnv); + ExprValue arg2 = arguments.get(1).valueOf(valueEnv); + ExprValue arg3 = arguments.get(2).valueOf(valueEnv); + return function.apply(functionProperties, arg1, arg2, arg3); + } + + @Override + public ExprType type() { + return returnType; + } + + @Override + public String toString() { + return String.format( + "%s(%s)", + functionName, + arguments.stream().map(Object::toString).collect(Collectors.joining(", "))); + } + }; return Pair.of(functionSignature, functionBuilder); }; } @@ -240,28 +237,25 @@ public String toString() { /** * No Arg Function Implementation. * - * @param function {@link ExprValue} based unary function. + * @param function {@link ExprValue} based unary function. * @param returnType return type. * @return Unary Function Implementation. */ public static SerializableFunction> impl( - SerializableNoArgFunction function, - ExprType returnType) { + SerializableNoArgFunction function, ExprType returnType) { return implWithProperties(fp -> function.get(), returnType); } /** * Unary Function Implementation. * - * @param function {@link ExprValue} based unary function. + * @param function {@link ExprValue} based unary function. * @param returnType return type. - * @param argsType argument type. + * @param argsType argument type. * @return Unary Function Implementation. */ public static SerializableFunction> impl( - SerializableFunction function, - ExprType returnType, - ExprType argsType) { + SerializableFunction function, ExprType returnType, ExprType argsType) { return implWithProperties((fp, arg) -> function.apply(arg), returnType, argsType); } @@ -269,10 +263,10 @@ public static SerializableFunction> impl( @@ -281,17 +275,17 @@ public static SerializableFunction - function.apply(arg1, arg2), returnType, args1Type, args2Type); + return implWithProperties( + (fp, arg1, arg2) -> function.apply(arg1, arg2), returnType, args1Type, args2Type); } /** * Triple Function Implementation. * - * @param function {@link ExprValue} based unary function. + * @param function {@link ExprValue} based unary function. * @param returnType return type. - * @param args1Type argument type. - * @param args2Type argument type. + * @param args1Type argument type. + * @param args2Type argument type. * @return Binary Function Implementation. */ public static SerializableFunction> impl( @@ -305,26 +299,31 @@ public static SerializableFunction new FunctionExpression(functionName, arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - ExprValue arg1 = arguments.get(0).valueOf(valueEnv); - ExprValue arg2 = arguments.get(1).valueOf(valueEnv); - ExprValue arg3 = arguments.get(2).valueOf(valueEnv); - return function.apply(arg1, arg2, arg3); - } - - @Override - public ExprType type() { - return returnType; - } - - @Override - public String toString() { - return String.format("%s(%s, %s, %s)", functionName, arguments.get(0).toString(), - arguments.get(1).toString(), arguments.get(2).toString()); - } - }; + (functionProperties, arguments) -> + new FunctionExpression(functionName, arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + ExprValue arg1 = arguments.get(0).valueOf(valueEnv); + ExprValue arg2 = arguments.get(1).valueOf(valueEnv); + ExprValue arg3 = arguments.get(2).valueOf(valueEnv); + return function.apply(arg1, arg2, arg3); + } + + @Override + public ExprType type() { + return returnType; + } + + @Override + public String toString() { + return String.format( + "%s(%s, %s, %s)", + functionName, + arguments.get(0).toString(), + arguments.get(1).toString(), + arguments.get(2).toString()); + } + }; return Pair.of(functionSignature, functionBuilder); }; } @@ -332,11 +331,11 @@ public String toString() { /** * Quadruple Function Implementation. * - * @param function {@link ExprValue} based unary function. + * @param function {@link ExprValue} based unary function. * @param returnType return type. - * @param args1Type argument type. - * @param args2Type argument type. - * @param args3Type argument type. + * @param args1Type argument type. + * @param args2Type argument type. + * @param args3Type argument type. * @return Quadruple Function Implementation. */ public static SerializableFunction> impl( @@ -349,42 +348,41 @@ public static SerializableFunction { FunctionSignature functionSignature = - new FunctionSignature(functionName, Arrays.asList( - args1Type, - args2Type, - args3Type, - args4Type)); + new FunctionSignature( + functionName, Arrays.asList(args1Type, args2Type, args3Type, args4Type)); FunctionBuilder functionBuilder = - (functionProperties, arguments) -> new FunctionExpression(functionName, arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - ExprValue arg1 = arguments.get(0).valueOf(valueEnv); - ExprValue arg2 = arguments.get(1).valueOf(valueEnv); - ExprValue arg3 = arguments.get(2).valueOf(valueEnv); - ExprValue arg4 = arguments.get(3).valueOf(valueEnv); - return function.apply(arg1, arg2, arg3, arg4); - } - - @Override - public ExprType type() { - return returnType; - } - - @Override - public String toString() { - return String.format("%s(%s, %s, %s, %s)", functionName, arguments.get(0).toString(), - arguments.get(1).toString(), - arguments.get(2).toString(), - arguments.get(3).toString()); - } - }; + (functionProperties, arguments) -> + new FunctionExpression(functionName, arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + ExprValue arg1 = arguments.get(0).valueOf(valueEnv); + ExprValue arg2 = arguments.get(1).valueOf(valueEnv); + ExprValue arg3 = arguments.get(2).valueOf(valueEnv); + ExprValue arg4 = arguments.get(3).valueOf(valueEnv); + return function.apply(arg1, arg2, arg3, arg4); + } + + @Override + public ExprType type() { + return returnType; + } + + @Override + public String toString() { + return String.format( + "%s(%s, %s, %s, %s)", + functionName, + arguments.get(0).toString(), + arguments.get(1).toString(), + arguments.get(2).toString(), + arguments.get(3).toString()); + } + }; return Pair.of(functionSignature, functionBuilder); }; } - /** - * Wrapper the unary ExprValue function with default NULL and MISSING handling. - */ + /** Wrapper the unary ExprValue function with default NULL and MISSING handling. */ public static SerializableFunction nullMissingHandling( SerializableFunction function) { return value -> { @@ -398,9 +396,7 @@ public static SerializableFunction nullMissingHandling( }; } - /** - * Wrapper the binary ExprValue function with default NULL and MISSING handling. - */ + /** Wrapper the binary ExprValue function with default NULL and MISSING handling. */ public static SerializableBiFunction nullMissingHandling( SerializableBiFunction function) { return (v1, v2) -> { @@ -414,9 +410,7 @@ public static SerializableBiFunction nullMissin }; } - /** - * Wrapper the triple ExprValue function with default NULL and MISSING handling. - */ + /** Wrapper the triple ExprValue function with default NULL and MISSING handling. */ public SerializableTriFunction nullMissingHandling( SerializableTriFunction function) { return (v1, v2, v3) -> { @@ -431,12 +425,12 @@ public SerializableTriFunction nullM } /** - * Wrapper the unary ExprValue function that is aware of FunctionProperties, - * with default NULL and MISSING handling. + * Wrapper the unary ExprValue function that is aware of FunctionProperties, with default NULL and + * MISSING handling. */ public static SerializableBiFunction - nullMissingHandlingWithProperties( - SerializableBiFunction implementation) { + nullMissingHandlingWithProperties( + SerializableBiFunction implementation) { return (functionProperties, v1) -> { if (v1.isMissing()) { return ExprValueUtils.missingValue(); @@ -453,8 +447,9 @@ public SerializableTriFunction nullM * with default NULL and MISSING handling. */ public static SerializableTriFunction - nullMissingHandlingWithProperties( - SerializableTriFunction implementation) { + nullMissingHandlingWithProperties( + SerializableTriFunction + implementation) { return (functionProperties, v1, v2) -> { if (v1.isMissing() || v2.isMissing()) { return ExprValueUtils.missingValue(); @@ -471,18 +466,10 @@ public SerializableTriFunction nullM * with default NULL and MISSING handling. */ public static SerializableQuadFunction< - FunctionProperties, - ExprValue, - ExprValue, - ExprValue, - ExprValue> + FunctionProperties, ExprValue, ExprValue, ExprValue, ExprValue> nullMissingHandlingWithProperties( - SerializableQuadFunction< - FunctionProperties, - ExprValue, - ExprValue, - ExprValue, - ExprValue> implementation) { + SerializableQuadFunction + implementation) { return (functionProperties, v1, v2, v3) -> { if (v1.isMissing() || v2.isMissing() || v3.isMissing()) { return ExprValueUtils.missingValue(); diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionImplementation.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionImplementation.java index d829e012255..4fd265a8906 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionImplementation.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionImplementation.java @@ -3,24 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.util.List; import org.opensearch.sql.expression.Expression; -/** - * The definition of Function Implementation. - */ +/** The definition of Function Implementation. */ public interface FunctionImplementation { - /** - * Get Function Name. - */ + /** Get Function Name. */ FunctionName getFunctionName(); - /** - * Get Function Arguments. - */ + /** Get Function Arguments. */ List getArguments(); } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionName.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionName.java index cb3d5fab929..ae2987a1648 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionName.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionName.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; @@ -11,14 +10,11 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -/** - * The definition of Function Name. - */ +/** The definition of Function Name. */ @EqualsAndHashCode @RequiredArgsConstructor public class FunctionName implements Serializable { - @Getter - private final String functionName; + @Getter private final String functionName; public static FunctionName of(String functionName) { return new FunctionName(functionName.toLowerCase()); diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionProperties.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionProperties.java index 42227480513..100c98bd38e 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionProperties.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionProperties.java @@ -19,9 +19,7 @@ public class FunctionProperties implements Serializable { private final Instant nowInstant; private final ZoneId currentZoneId; - /** - * By default, use current time and current timezone. - */ + /** By default, use current time and current timezone. */ public FunctionProperties() { nowInstant = Instant.now(); currentZoneId = ZoneId.systemDefault(); @@ -29,6 +27,7 @@ public FunctionProperties() { /** * Method to access current system clock. + * * @return a ticking clock that tells the time. */ public Clock getSystemClock() { @@ -36,29 +35,28 @@ public Clock getSystemClock() { } /** - * Method to get time when query began execution. - * Clock class combines an instant Supplier and a time zone. - * @return a fixed clock that returns the time execution started at. + * Method to get time when query began execution. Clock class combines an instant Supplier and a + * time zone. * + * @return a fixed clock that returns the time execution started at. */ public Clock getQueryStartClock() { return Clock.fixed(nowInstant, currentZoneId); } - /** - * Use when compiling functions that do not rely on function properties. - */ - public static final FunctionProperties None = new FunctionProperties() { - @Override - public Clock getSystemClock() { - throw new UnexpectedCallException(); - } + /** Use when compiling functions that do not rely on function properties. */ + public static final FunctionProperties None = + new FunctionProperties() { + @Override + public Clock getSystemClock() { + throw new UnexpectedCallException(); + } - @Override - public Clock getQueryStartClock() { - throw new UnexpectedCallException(); - } - }; + @Override + public Clock getQueryStartClock() { + throw new UnexpectedCallException(); + } + }; class UnexpectedCallException extends RuntimeException { public UnexpectedCallException() { diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionResolver.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionResolver.java index 1635b6f8461..eaede1da7e4 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionResolver.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionResolver.java @@ -8,8 +8,8 @@ import org.apache.commons.lang3.tuple.Pair; /** - * An interface for any class that can provide a {@ref FunctionBuilder} - * given a {@ref FunctionSignature}. + * An interface for any class that can provide a {@ref FunctionBuilder} given a {@ref + * FunctionSignature}. */ public interface FunctionResolver { Pair resolve(FunctionSignature unresolvedSignature); diff --git a/core/src/main/java/org/opensearch/sql/expression/function/FunctionSignature.java b/core/src/main/java/org/opensearch/sql/expression/function/FunctionSignature.java index 0c59d71c256..665486afdc9 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/FunctionSignature.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/FunctionSignature.java @@ -15,9 +15,7 @@ import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.data.type.WideningTypeRule; -/** - * Function signature is composed by function name and arguments list. - */ +/** Function signature is composed by function name and arguments list. */ @Getter @RequiredArgsConstructor @EqualsAndHashCode @@ -31,9 +29,8 @@ public class FunctionSignature { /** * calculate the function signature match degree. * - * @return EXACTLY_MATCH: exactly match - * NOT_MATCH: not match - * By widening rule, the small number means better match + * @return EXACTLY_MATCH: exactly match NOT_MATCH: not match By widening rule, the small number + * means better match */ public int match(FunctionSignature functionSignature) { List functionTypeList = functionSignature.getParamTypeList(); @@ -60,18 +57,14 @@ public int match(FunctionSignature functionSignature) { return matchDegree; } - /** - * util function for formatted arguments list. - */ + /** util function for formatted arguments list. */ public String formatTypes() { return getParamTypeList().stream() .map(ExprType::typeName) .collect(Collectors.joining(",", "[", "]")); } - /** - * util function - returns true if function has variable arguments. - */ + /** util function - returns true if function has variable arguments. */ protected static boolean isVarArgFunction(List argTypes) { return argTypes.size() == 1 && argTypes.get(0) == ARRAY; } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/OpenSearchFunctions.java b/core/src/main/java/org/opensearch/sql/expression/function/OpenSearchFunctions.java index c5fcb010f5f..8d8928c16ae 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/OpenSearchFunctions.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/OpenSearchFunctions.java @@ -22,9 +22,7 @@ @UtilityClass public class OpenSearchFunctions { - /** - * Add functions specific to OpenSearch to repository. - */ + /** Add functions specific to OpenSearch to repository. */ public void register(BuiltinFunctionRepository repository) { repository.register(match_bool_prefix()); repository.register(multi_match(BuiltinFunctionName.MULTI_MATCH)); @@ -101,19 +99,20 @@ private static FunctionResolver nested() { @Override public Pair resolve( FunctionSignature unresolvedSignature) { - return Pair.of(unresolvedSignature, + return Pair.of( + unresolvedSignature, (functionProperties, arguments) -> - new FunctionExpression(BuiltinFunctionName.NESTED.getName(), arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - return valueEnv.resolve(getArguments().get(0)); - } - - @Override - public ExprType type() { - return getArguments().get(0).type(); - } - }); + new FunctionExpression(BuiltinFunctionName.NESTED.getName(), arguments) { + @Override + public ExprValue valueOf(Environment valueEnv) { + return valueEnv.resolve(getArguments().get(0)); + } + + @Override + public ExprType type() { + return getArguments().get(0).type(); + } + }); } @Override @@ -123,9 +122,6 @@ public FunctionName getFunctionName() { }; } - - - private static FunctionResolver score(BuiltinFunctionName score) { FunctionName funcName = score.getName(); return new RelevanceFunctionResolver(funcName); @@ -135,12 +131,11 @@ public static class OpenSearchFunction extends FunctionExpression { private final FunctionName functionName; private final List arguments; - @Getter - @Setter - private boolean isScoreTracked; + @Getter @Setter private boolean isScoreTracked; /** * Required argument constructor. + * * @param functionName name of the function * @param arguments a list of expressions */ @@ -153,9 +148,10 @@ public OpenSearchFunction(FunctionName functionName, List arguments) @Override public ExprValue valueOf(Environment valueEnv) { - throw new UnsupportedOperationException(String.format( - "OpenSearch defined function [%s] is only supported in WHERE and HAVING clause.", - functionName)); + throw new UnsupportedOperationException( + String.format( + "OpenSearch defined function [%s] is only supported in WHERE and HAVING clause.", + functionName)); } @Override @@ -165,10 +161,15 @@ public ExprType type() { @Override public String toString() { - List args = arguments.stream() - .map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) - .getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) - .collect(Collectors.toList()); + List args = + arguments.stream() + .map( + arg -> + String.format( + "%s=%s", + ((NamedArgumentExpression) arg).getArgName(), + ((NamedArgumentExpression) arg).getValue().toString())) + .collect(Collectors.toList()); return String.format("%s(%s)", functionName, String.join(", ", args)); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/RelevanceFunctionResolver.java b/core/src/main/java/org/opensearch/sql/expression/function/RelevanceFunctionResolver.java index ef0ac9226c8..ae882897d01 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/RelevanceFunctionResolver.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/RelevanceFunctionResolver.java @@ -14,17 +14,18 @@ import org.opensearch.sql.exception.SemanticCheckException; @RequiredArgsConstructor -public class RelevanceFunctionResolver - implements FunctionResolver { +public class RelevanceFunctionResolver implements FunctionResolver { - @Getter - private final FunctionName functionName; + @Getter private final FunctionName functionName; @Override public Pair resolve(FunctionSignature unresolvedSignature) { if (!unresolvedSignature.getFunctionName().equals(functionName)) { - throw new SemanticCheckException(String.format("Expected '%s' but got '%s'", - functionName.getFunctionName(), unresolvedSignature.getFunctionName().getFunctionName())); + throw new SemanticCheckException( + String.format( + "Expected '%s' but got '%s'", + functionName.getFunctionName(), + unresolvedSignature.getFunctionName().getFunctionName())); } List paramTypes = unresolvedSignature.getParamTypeList(); // Check if all but the first parameter are of type STRING. @@ -36,13 +37,15 @@ public Pair resolve(FunctionSignature unreso } } - FunctionBuilder buildFunction = (functionProperties, args) - -> new OpenSearchFunctions.OpenSearchFunction(functionName, args); + FunctionBuilder buildFunction = + (functionProperties, args) -> + new OpenSearchFunctions.OpenSearchFunction(functionName, args); return Pair.of(unresolvedSignature, buildFunction); } - /** Returns a helpful error message when expected parameter type does not match the - * specified parameter type. + /** + * Returns a helpful error message when expected parameter type does not match the specified + * parameter type. * * @param i 0-based index of the parameter in a function signature. * @param paramType the type of the ith parameter at run-time. @@ -50,7 +53,8 @@ public Pair resolve(FunctionSignature unreso * @return A user-friendly error message that informs of the type difference. */ private String getWrongParameterErrorMessage(int i, ExprType paramType, ExprType expectedType) { - return String.format("Expected type %s instead of %s for parameter #%d", + return String.format( + "Expected type %s instead of %s for parameter #%d", expectedType.typeName(), paramType.typeName(), i + 1); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/function/SerializableBiFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/SerializableBiFunction.java index 5b3aaf31f3b..9f182e4c851 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/SerializableBiFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/SerializableBiFunction.java @@ -3,14 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; import java.util.function.BiFunction; -/** - * Serializable BiFunction. - */ -public interface SerializableBiFunction extends BiFunction, Serializable { -} +/** Serializable BiFunction. */ +public interface SerializableBiFunction extends BiFunction, Serializable {} diff --git a/core/src/main/java/org/opensearch/sql/expression/function/SerializableFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/SerializableFunction.java index 467c034c398..fb3e2f2cfbb 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/SerializableFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/SerializableFunction.java @@ -3,11 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; import java.util.function.Function; -public interface SerializableFunction extends Function, Serializable { -} +public interface SerializableFunction extends Function, Serializable {} diff --git a/core/src/main/java/org/opensearch/sql/expression/function/SerializableNoArgFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/SerializableNoArgFunction.java index e68d6084b46..6eaf699bf90 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/SerializableNoArgFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/SerializableNoArgFunction.java @@ -3,14 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; import java.util.function.Supplier; -/** - * Serializable no argument function. - */ -public interface SerializableNoArgFunction extends Supplier, Serializable { -} +/** Serializable no argument function. */ +public interface SerializableNoArgFunction extends Supplier, Serializable {} diff --git a/core/src/main/java/org/opensearch/sql/expression/function/SerializableQuadFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/SerializableQuadFunction.java index 056a17d5b35..7285d9a32f1 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/SerializableQuadFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/SerializableQuadFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; diff --git a/core/src/main/java/org/opensearch/sql/expression/function/SerializableTriFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/SerializableTriFunction.java index 911012fcdb9..e980b1c82ac 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/SerializableTriFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/SerializableTriFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import java.io.Serializable; diff --git a/core/src/main/java/org/opensearch/sql/expression/function/TableFunctionImplementation.java b/core/src/main/java/org/opensearch/sql/expression/function/TableFunctionImplementation.java index f35ffe48981..b8b90bf5e3f 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/TableFunctionImplementation.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/TableFunctionImplementation.java @@ -9,11 +9,8 @@ import org.opensearch.sql.storage.Table; -/** - * Interface for table function which returns Table when executed. - */ +/** Interface for table function which returns Table when executed. */ public interface TableFunctionImplementation extends FunctionImplementation { Table applyArguments(); - } diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunction.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunction.java index e25c5cda20b..3969b5310ec 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.arthmetic; import static org.opensearch.sql.data.type.ExprCoreType.BYTE; @@ -30,12 +29,10 @@ import org.opensearch.sql.expression.function.FunctionName; /** - * The definition of arithmetic function - * add, Accepts two numbers and produces a number. - * subtract, Accepts two numbers and produces a number. - * multiply, Accepts two numbers and produces a number. - * divide, Accepts two numbers and produces a number. - * module, Accepts two numbers and produces a number. + * The definition of arithmetic function add, Accepts two numbers and produces a number. subtract, + * Accepts two numbers and produces a number. multiply, Accepts two numbers and produces a number. + * divide, Accepts two numbers and produces a number. module, Accepts two numbers and produces a + * number. */ @UtilityClass public class ArithmeticFunction { @@ -59,33 +56,47 @@ public static void register(BuiltinFunctionRepository repository) { } /** - * Definition of add(x, y) function. - * Returns the number x plus number y - * The supported signature of add function is - * (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of add(x, y) function. Returns the number x plus number y The supported signature of + * add function is (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: + * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) -> wider type between types of x and y */ private static DefaultFunctionResolver addBase(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling( - (v1, v2) -> new ExprByteValue(v1.byteValue() + v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling( - (v1, v2) -> new ExprShortValue(v1.shortValue() + v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> new ExprIntegerValue(Math.addExact(v1.integerValue(), v2.integerValue()))), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling( - (v1, v2) -> new ExprLongValue(Math.addExact(v1.longValue(), v2.longValue()))), - LONG, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> new ExprFloatValue(v1.floatValue() + v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling( - (v1, v2) -> new ExprDoubleValue(v1.doubleValue() + v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); + return define( + functionName, + impl( + nullMissingHandling((v1, v2) -> new ExprByteValue(v1.byteValue() + v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling((v1, v2) -> new ExprShortValue(v1.shortValue() + v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + new ExprIntegerValue(Math.addExact(v1.integerValue(), v2.integerValue()))), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (v1, v2) -> new ExprLongValue(Math.addExact(v1.longValue(), v2.longValue()))), + LONG, + LONG, + LONG), + impl( + nullMissingHandling((v1, v2) -> new ExprFloatValue(v1.floatValue() + v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( + (v1, v2) -> new ExprDoubleValue(v1.doubleValue() + v2.doubleValue())), + DOUBLE, + DOUBLE, + DOUBLE)); } private static DefaultFunctionResolver add() { @@ -97,39 +108,67 @@ private static DefaultFunctionResolver addFunction() { } /** - * Definition of divide(x, y) function. - * Returns the number x divided by number y - * The supported signature of divide function is - * (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of divide(x, y) function. Returns the number x divided by number y The supported + * signature of divide function is (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: + * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) -> wider type between types of x and y */ private static DefaultFunctionResolver divideBase(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling( - (v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() : - new ExprByteValue(v1.byteValue() / v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling( - (v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprShortValue(v1.shortValue() / v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> v2.integerValue() == 0 ? ExprNullValue.of() : - new ExprIntegerValue(v1.integerValue() / v2.integerValue())), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling( - (v1, v2) -> v2.longValue() == 0 ? ExprNullValue.of() : - new ExprLongValue(v1.longValue() / v2.longValue())), - LONG, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> v2.floatValue() == 0 ? ExprNullValue.of() : - new ExprFloatValue(v1.floatValue() / v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling( - (v1, v2) -> v2.doubleValue() == 0 ? ExprNullValue.of() : - new ExprDoubleValue(v1.doubleValue() / v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); + return define( + functionName, + impl( + nullMissingHandling( + (v1, v2) -> + v2.byteValue() == 0 + ? ExprNullValue.of() + : new ExprByteValue(v1.byteValue() / v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprShortValue(v1.shortValue() / v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.integerValue() == 0 + ? ExprNullValue.of() + : new ExprIntegerValue(v1.integerValue() / v2.integerValue())), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (v1, v2) -> + v2.longValue() == 0 + ? ExprNullValue.of() + : new ExprLongValue(v1.longValue() / v2.longValue())), + LONG, + LONG, + LONG), + impl( + nullMissingHandling( + (v1, v2) -> + v2.floatValue() == 0 + ? ExprNullValue.of() + : new ExprFloatValue(v1.floatValue() / v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.doubleValue() == 0 + ? ExprNullValue.of() + : new ExprDoubleValue(v1.doubleValue() / v2.doubleValue())), + DOUBLE, + DOUBLE, + DOUBLE)); } private static DefaultFunctionResolver divide() { @@ -141,39 +180,67 @@ private static DefaultFunctionResolver divideFunction() { } /** - * Definition of modulus(x, y) function. - * Returns the number x modulo by number y - * The supported signature of modulo function is - * (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of modulus(x, y) function. Returns the number x modulo by number y The supported + * signature of modulo function is (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: + * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) -> wider type between types of x and y */ private static DefaultFunctionResolver modulusBase(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling( - (v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() : - new ExprByteValue(v1.byteValue() % v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling( - (v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprShortValue(v1.shortValue() % v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> v2.integerValue() == 0 ? ExprNullValue.of() : - new ExprIntegerValue(v1.integerValue() % v2.integerValue())), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling( - (v1, v2) -> v2.longValue() == 0 ? ExprNullValue.of() : - new ExprLongValue(v1.longValue() % v2.longValue())), - LONG, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> v2.floatValue() == 0 ? ExprNullValue.of() : - new ExprFloatValue(v1.floatValue() % v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling( - (v1, v2) -> v2.doubleValue() == 0 ? ExprNullValue.of() : - new ExprDoubleValue(v1.doubleValue() % v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); + return define( + functionName, + impl( + nullMissingHandling( + (v1, v2) -> + v2.byteValue() == 0 + ? ExprNullValue.of() + : new ExprByteValue(v1.byteValue() % v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprShortValue(v1.shortValue() % v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.integerValue() == 0 + ? ExprNullValue.of() + : new ExprIntegerValue(v1.integerValue() % v2.integerValue())), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (v1, v2) -> + v2.longValue() == 0 + ? ExprNullValue.of() + : new ExprLongValue(v1.longValue() % v2.longValue())), + LONG, + LONG, + LONG), + impl( + nullMissingHandling( + (v1, v2) -> + v2.floatValue() == 0 + ? ExprNullValue.of() + : new ExprFloatValue(v1.floatValue() % v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.doubleValue() == 0 + ? ExprNullValue.of() + : new ExprDoubleValue(v1.doubleValue() % v2.doubleValue())), + DOUBLE, + DOUBLE, + DOUBLE)); } private static DefaultFunctionResolver mod() { @@ -189,34 +256,47 @@ private static DefaultFunctionResolver modulusFunction() { } /** - * Definition of multiply(x, y) function. - * Returns the number x multiplied by number y - * The supported signature of multiply function is - * (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of multiply(x, y) function. Returns the number x multiplied by number y The + * supported signature of multiply function is (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: + * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) -> wider type between types of x and y */ private static DefaultFunctionResolver multiplyBase(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling( - (v1, v2) -> new ExprByteValue(v1.byteValue() * v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling( - (v1, v2) -> new ExprShortValue(v1.shortValue() * v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> new ExprIntegerValue(Math.multiplyExact(v1.integerValue(), - v2.integerValue()))), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling( + return define( + functionName, + impl( + nullMissingHandling((v1, v2) -> new ExprByteValue(v1.byteValue() * v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling((v1, v2) -> new ExprShortValue(v1.shortValue() * v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + new ExprIntegerValue(Math.multiplyExact(v1.integerValue(), v2.integerValue()))), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( (v1, v2) -> new ExprLongValue(Math.multiplyExact(v1.longValue(), v2.longValue()))), - LONG, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> new ExprFloatValue(v1.floatValue() * v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling( + LONG, + LONG, + LONG), + impl( + nullMissingHandling((v1, v2) -> new ExprFloatValue(v1.floatValue() * v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( (v1, v2) -> new ExprDoubleValue(v1.doubleValue() * v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); + DOUBLE, + DOUBLE, + DOUBLE)); } private static DefaultFunctionResolver multiply() { @@ -228,34 +308,47 @@ private static DefaultFunctionResolver multiplyFunction() { } /** - * Definition of subtract(x, y) function. - * Returns the number x minus number y - * The supported signature of subtract function is - * (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of subtract(x, y) function. Returns the number x minus number y The supported + * signature of subtract function is (x: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE, y: + * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE) -> wider type between types of x and y */ private static DefaultFunctionResolver subtractBase(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling( - (v1, v2) -> new ExprByteValue(v1.byteValue() - v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling( - (v1, v2) -> new ExprShortValue(v1.shortValue() - v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> new ExprIntegerValue(Math.subtractExact(v1.integerValue(), - v2.integerValue()))), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling( + return define( + functionName, + impl( + nullMissingHandling((v1, v2) -> new ExprByteValue(v1.byteValue() - v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling((v1, v2) -> new ExprShortValue(v1.shortValue() - v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + new ExprIntegerValue(Math.subtractExact(v1.integerValue(), v2.integerValue()))), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( (v1, v2) -> new ExprLongValue(Math.subtractExact(v1.longValue(), v2.longValue()))), - LONG, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> new ExprFloatValue(v1.floatValue() - v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling( + LONG, + LONG, + LONG), + impl( + nullMissingHandling((v1, v2) -> new ExprFloatValue(v1.floatValue() - v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( (v1, v2) -> new ExprDoubleValue(v1.doubleValue() - v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); + DOUBLE, + DOUBLE, + DOUBLE)); } private static DefaultFunctionResolver subtract() { diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java index 810d292ca29..21a348504ea 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.arthmetic; import static org.opensearch.sql.data.type.ExprCoreType.BYTE; @@ -102,10 +101,14 @@ public static void register(BuiltinFunctionRepository repository) { * @return DefaultFunctionResolver for math functions. */ private static DefaultFunctionResolver baseMathFunction( - FunctionName functionName, SerializableFunction formula, ExprCoreType returnType) { - return define(functionName, ExprCoreType.numberTypes().stream().map(type -> - impl(nullMissingHandling(formula), returnType, type)).collect(Collectors.toList())); + FunctionName functionName, + SerializableFunction formula, + ExprCoreType returnType) { + return define( + functionName, + ExprCoreType.numberTypes().stream() + .map(type -> impl(nullMissingHandling(formula), returnType, type)) + .collect(Collectors.toList())); } /** @@ -113,20 +116,20 @@ private static DefaultFunctionResolver baseMathFunction( * LONG FLOAT -> FLOAT DOUBLE -> DOUBLE */ private static DefaultFunctionResolver abs() { - return define(BuiltinFunctionName.ABS.getName(), - impl(nullMissingHandling(v -> new ExprByteValue(Math.abs(v.byteValue()))), - BYTE, BYTE), - impl(nullMissingHandling(v -> new ExprShortValue(Math.abs(v.shortValue()))), - SHORT, SHORT), - impl(nullMissingHandling(v -> new ExprIntegerValue(Math.abs(v.integerValue()))), - INTEGER, INTEGER), - impl(nullMissingHandling(v -> new ExprLongValue(Math.abs(v.longValue()))), - LONG, LONG), - impl(nullMissingHandling(v -> new ExprFloatValue(Math.abs(v.floatValue()))), - FLOAT, FLOAT), - impl(nullMissingHandling(v -> new ExprDoubleValue(Math.abs(v.doubleValue()))), - DOUBLE, DOUBLE) - ); + return define( + BuiltinFunctionName.ABS.getName(), + impl(nullMissingHandling(v -> new ExprByteValue(Math.abs(v.byteValue()))), BYTE, BYTE), + impl(nullMissingHandling(v -> new ExprShortValue(Math.abs(v.shortValue()))), SHORT, SHORT), + impl( + nullMissingHandling(v -> new ExprIntegerValue(Math.abs(v.integerValue()))), + INTEGER, + INTEGER), + impl(nullMissingHandling(v -> new ExprLongValue(Math.abs(v.longValue()))), LONG, LONG), + impl(nullMissingHandling(v -> new ExprFloatValue(Math.abs(v.floatValue()))), FLOAT, FLOAT), + impl( + nullMissingHandling(v -> new ExprDoubleValue(Math.abs(v.doubleValue()))), + DOUBLE, + DOUBLE)); } /** @@ -134,83 +137,94 @@ private static DefaultFunctionResolver abs() { * to The supported signature of ceil/ceiling function is DOUBLE -> INTEGER */ private static DefaultFunctionResolver ceil() { - return define(BuiltinFunctionName.CEIL.getName(), - impl(nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))), - LONG, DOUBLE) - ); + return define( + BuiltinFunctionName.CEIL.getName(), + impl( + nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))), LONG, DOUBLE)); } private static DefaultFunctionResolver ceiling() { - return define(BuiltinFunctionName.CEILING.getName(), - impl(nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))), - LONG, DOUBLE) - ); + return define( + BuiltinFunctionName.CEILING.getName(), + impl( + nullMissingHandling(v -> new ExprLongValue(Math.ceil(v.doubleValue()))), LONG, DOUBLE)); } /** - * Definition of conv(x, a, b) function. - * Convert number x from base a to base b - * The supported signature of floor function is - * (STRING, INTEGER, INTEGER) -> STRING - * (INTEGER, INTEGER, INTEGER) -> STRING + * Definition of conv(x, a, b) function. Convert number x from base a to base b The supported + * signature of floor function is (STRING, INTEGER, INTEGER) -> STRING (INTEGER, INTEGER, INTEGER) + * -> STRING */ private static DefaultFunctionResolver conv() { - return define(BuiltinFunctionName.CONV.getName(), - impl(nullMissingHandling((x, a, b) -> new ExprStringValue( - Integer.toString(Integer.parseInt(x.stringValue(), a.integerValue()), - b.integerValue()))), - STRING, STRING, INTEGER, INTEGER), - impl(nullMissingHandling((x, a, b) -> new ExprStringValue( - Integer.toString(Integer.parseInt(x.integerValue().toString(), a.integerValue()), - b.integerValue()))), - STRING, INTEGER, INTEGER, INTEGER) - ); - } - - /** - * Definition of crc32(x) function. - * Calculate a cyclic redundancy check value and returns a 32-bit unsigned value - * The supported signature of crc32 function is - * STRING -> LONG + return define( + BuiltinFunctionName.CONV.getName(), + impl( + nullMissingHandling( + (x, a, b) -> + new ExprStringValue( + Integer.toString( + Integer.parseInt(x.stringValue(), a.integerValue()), + b.integerValue()))), + STRING, + STRING, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (x, a, b) -> + new ExprStringValue( + Integer.toString( + Integer.parseInt(x.integerValue().toString(), a.integerValue()), + b.integerValue()))), + STRING, + INTEGER, + INTEGER, + INTEGER)); + } + + /** + * Definition of crc32(x) function. Calculate a cyclic redundancy check value and returns a 32-bit + * unsigned value The supported signature of crc32 function is STRING -> LONG */ private static DefaultFunctionResolver crc32() { - return define(BuiltinFunctionName.CRC32.getName(), - impl(nullMissingHandling(v -> { - CRC32 crc = new CRC32(); - crc.update(v.stringValue().getBytes()); - return new ExprLongValue(crc.getValue()); - }), - LONG, STRING) - ); + return define( + BuiltinFunctionName.CRC32.getName(), + impl( + nullMissingHandling( + v -> { + CRC32 crc = new CRC32(); + crc.update(v.stringValue().getBytes()); + return new ExprLongValue(crc.getValue()); + }), + LONG, + STRING)); } - /** - * Definition of e() function. - * Get the Euler's number. - * () -> DOUBLE - */ + /** Definition of e() function. Get the Euler's number. () -> DOUBLE */ private static DefaultFunctionResolver euler() { - return define(BuiltinFunctionName.E.getName(), - impl(() -> new ExprDoubleValue(Math.E), DOUBLE) - ); + return define(BuiltinFunctionName.E.getName(), impl(() -> new ExprDoubleValue(Math.E), DOUBLE)); } /** - * Definition of exp(x) function. Calculate exponent function e to the x - * The supported signature of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of exp(x) function. Calculate exponent function e to the x The supported signature + * of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver exp() { - return baseMathFunction(BuiltinFunctionName.EXP.getName(), - v -> new ExprDoubleValue(Math.exp(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.EXP.getName(), + v -> new ExprDoubleValue(Math.exp(v.doubleValue())), + DOUBLE); } /** - * Definition of expm1(x) function. Calculate exponent function e to the x, minus 1 - * The supported signature of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of expm1(x) function. Calculate exponent function e to the x, minus 1 The supported + * signature of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver expm1() { - return baseMathFunction(BuiltinFunctionName.EXPM1.getName(), - v -> new ExprDoubleValue(Math.expm1(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.EXPM1.getName(), + v -> new ExprDoubleValue(Math.expm1(v.doubleValue())), + DOUBLE); } /** @@ -218,10 +232,12 @@ private static DefaultFunctionResolver expm1() { * The supported signature of floor function is DOUBLE -> INTEGER */ private static DefaultFunctionResolver floor() { - return define(BuiltinFunctionName.FLOOR.getName(), - impl(nullMissingHandling(v -> new ExprLongValue(Math.floor(v.doubleValue()))), - LONG, DOUBLE) - ); + return define( + BuiltinFunctionName.FLOOR.getName(), + impl( + nullMissingHandling(v -> new ExprLongValue(Math.floor(v.doubleValue()))), + LONG, + DOUBLE)); } /** @@ -229,9 +245,13 @@ private static DefaultFunctionResolver floor() { * ln function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver ln() { - return baseMathFunction(BuiltinFunctionName.LN.getName(), - v -> v.doubleValue() <= 0 ? ExprNullValue.of() : - new ExprDoubleValue(Math.log(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.LN.getName(), + v -> + v.doubleValue() <= 0 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.log(v.doubleValue())), + DOUBLE); } /** @@ -240,37 +260,54 @@ private static DefaultFunctionResolver ln() { * INTEGER/LONG/FLOAT/DOUBLE]) -> DOUBLE */ private static DefaultFunctionResolver log() { - ImmutableList.Builder>> builder = new ImmutableList.Builder<>(); + ImmutableList.Builder< + SerializableFunction>> + builder = new ImmutableList.Builder<>(); // build unary log(x), SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE for (ExprType type : ExprCoreType.numberTypes()) { - builder.add(impl(nullMissingHandling(v -> v.doubleValue() <= 0 ? ExprNullValue.of() : - new ExprDoubleValue(Math.log(v.doubleValue()))), - DOUBLE, type)); + builder.add( + impl( + nullMissingHandling( + v -> + v.doubleValue() <= 0 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.log(v.doubleValue()))), + DOUBLE, + type)); } // build binary function log(b, x) for (ExprType baseType : ExprCoreType.numberTypes()) { for (ExprType numberType : ExprCoreType.numberTypes()) { - builder.add(impl(nullMissingHandling((b, x) -> b.doubleValue() <= 0 || x.doubleValue() <= 0 - ? ExprNullValue.of() : new ExprDoubleValue( - Math.log(x.doubleValue()) / Math.log(b.doubleValue()))), - DOUBLE, baseType, numberType)); + builder.add( + impl( + nullMissingHandling( + (b, x) -> + b.doubleValue() <= 0 || x.doubleValue() <= 0 + ? ExprNullValue.of() + : new ExprDoubleValue( + Math.log(x.doubleValue()) / Math.log(b.doubleValue()))), + DOUBLE, + baseType, + numberType)); } } return define(BuiltinFunctionName.LOG.getName(), builder.build()); } - /** * Definition of log10(x) function. Calculate base-10 logarithm of x The supported signature of * log function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver log10() { - return baseMathFunction(BuiltinFunctionName.LOG10.getName(), - v -> v.doubleValue() <= 0 ? ExprNullValue.of() : - new ExprDoubleValue(Math.log10(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.LOG10.getName(), + v -> + v.doubleValue() <= 0 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.log10(v.doubleValue())), + DOUBLE); } /** @@ -278,60 +315,90 @@ private static DefaultFunctionResolver log10() { * function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver log2() { - return baseMathFunction(BuiltinFunctionName.LOG2.getName(), - v -> v.doubleValue() <= 0 ? ExprNullValue.of() : - new ExprDoubleValue(Math.log(v.doubleValue()) / Math.log(2)), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.LOG2.getName(), + v -> + v.doubleValue() <= 0 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.log(v.doubleValue()) / Math.log(2)), + DOUBLE); } /** - * Definition of mod(x, y) function. - * Calculate the remainder of x divided by y - * The supported signature of mod function is - * (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) - * -> wider type between types of x and y + * Definition of mod(x, y) function. Calculate the remainder of x divided by y The supported + * signature of mod function is (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) -> + * wider type between types of x and y */ private static DefaultFunctionResolver mod() { - return define(BuiltinFunctionName.MOD.getName(), - impl(nullMissingHandling((v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() : - new ExprByteValue(v1.byteValue() % v2.byteValue())), - BYTE, BYTE, BYTE), - impl(nullMissingHandling((v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprShortValue(v1.shortValue() % v2.shortValue())), - SHORT, SHORT, SHORT), - impl(nullMissingHandling((v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprIntegerValue(Math.floorMod(v1.integerValue(), v2.integerValue()))), - INTEGER, INTEGER, INTEGER), - impl(nullMissingHandling((v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprLongValue(Math.floorMod(v1.longValue(), v2.longValue()))), - LONG, LONG, LONG), - impl(nullMissingHandling((v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprFloatValue(v1.floatValue() % v2.floatValue())), - FLOAT, FLOAT, FLOAT), - impl(nullMissingHandling((v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() : - new ExprDoubleValue(v1.doubleValue() % v2.doubleValue())), - DOUBLE, DOUBLE, DOUBLE) - ); - } - - /** - * Definition of pi() function. - * Get the value of pi. - * () -> DOUBLE - */ + return define( + BuiltinFunctionName.MOD.getName(), + impl( + nullMissingHandling( + (v1, v2) -> + v2.byteValue() == 0 + ? ExprNullValue.of() + : new ExprByteValue(v1.byteValue() % v2.byteValue())), + BYTE, + BYTE, + BYTE), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprShortValue(v1.shortValue() % v2.shortValue())), + SHORT, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprIntegerValue( + Math.floorMod(v1.integerValue(), v2.integerValue()))), + INTEGER, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprLongValue(Math.floorMod(v1.longValue(), v2.longValue()))), + LONG, + LONG, + LONG), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprFloatValue(v1.floatValue() % v2.floatValue())), + FLOAT, + FLOAT, + FLOAT), + impl( + nullMissingHandling( + (v1, v2) -> + v2.shortValue() == 0 + ? ExprNullValue.of() + : new ExprDoubleValue(v1.doubleValue() % v2.doubleValue())), + DOUBLE, + DOUBLE, + DOUBLE)); + } + + /** Definition of pi() function. Get the value of pi. () -> DOUBLE */ private static DefaultFunctionResolver pi() { - return define(BuiltinFunctionName.PI.getName(), - impl(() -> new ExprDoubleValue(Math.PI), DOUBLE) - ); + return define( + BuiltinFunctionName.PI.getName(), impl(() -> new ExprDoubleValue(Math.PI), DOUBLE)); } /** - * Definition of pow(x, y)/power(x, y) function. - * Calculate the value of x raised to the power of y - * The supported signature of pow/power function is - * (INTEGER, INTEGER) -> DOUBLE - * (LONG, LONG) -> DOUBLE - * (FLOAT, FLOAT) -> DOUBLE - * (DOUBLE, DOUBLE) -> DOUBLE + * Definition of pow(x, y)/power(x, y) function. Calculate the value of x raised to the power of y + * The supported signature of pow/power function is (INTEGER, INTEGER) -> DOUBLE (LONG, LONG) -> + * DOUBLE (FLOAT, FLOAT) -> DOUBLE (DOUBLE, DOUBLE) -> DOUBLE */ private static DefaultFunctionResolver pow() { return define(BuiltinFunctionName.POW.getName(), powerFunctionImpl()); @@ -341,336 +408,445 @@ private static DefaultFunctionResolver power() { return define(BuiltinFunctionName.POWER.getName(), powerFunctionImpl()); } - private List>> powerFunctionImpl() { + private List>> + powerFunctionImpl() { return Arrays.asList( - impl(nullMissingHandling( - (v1, v2) -> new ExprDoubleValue(Math.pow(v1.shortValue(), v2.shortValue()))), - DOUBLE, SHORT, SHORT), - impl(nullMissingHandling( - (v1, v2) -> new ExprDoubleValue(Math.pow(v1.integerValue(), v2.integerValue()))), - DOUBLE, INTEGER, INTEGER), - impl(nullMissingHandling( - (v1, v2) -> new ExprDoubleValue(Math.pow(v1.longValue(), v2.longValue()))), - DOUBLE, LONG, LONG), - impl(nullMissingHandling( - (v1, v2) -> v1.floatValue() <= 0 && v2.floatValue() != Math.floor(v2.floatValue()) - ? ExprNullValue.of() : - new ExprDoubleValue(Math.pow(v1.floatValue(), v2.floatValue()))), - DOUBLE, FLOAT, FLOAT), - impl(nullMissingHandling( - (v1, v2) -> v1.doubleValue() <= 0 && v2.doubleValue() != Math.floor(v2.doubleValue()) - ? ExprNullValue.of() : - new ExprDoubleValue(Math.pow(v1.doubleValue(), v2.doubleValue()))), - DOUBLE, DOUBLE, DOUBLE)); - } - - /** - * Definition of rand() and rand(N) function. - * rand() returns a random floating-point value in the range 0 <= value < 1.0 - * If integer N is specified, the seed is initialized prior to execution. + impl( + nullMissingHandling( + (v1, v2) -> new ExprDoubleValue(Math.pow(v1.shortValue(), v2.shortValue()))), + DOUBLE, + SHORT, + SHORT), + impl( + nullMissingHandling( + (v1, v2) -> new ExprDoubleValue(Math.pow(v1.integerValue(), v2.integerValue()))), + DOUBLE, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (v1, v2) -> new ExprDoubleValue(Math.pow(v1.longValue(), v2.longValue()))), + DOUBLE, + LONG, + LONG), + impl( + nullMissingHandling( + (v1, v2) -> + v1.floatValue() <= 0 && v2.floatValue() != Math.floor(v2.floatValue()) + ? ExprNullValue.of() + : new ExprDoubleValue(Math.pow(v1.floatValue(), v2.floatValue()))), + DOUBLE, + FLOAT, + FLOAT), + impl( + nullMissingHandling( + (v1, v2) -> + v1.doubleValue() <= 0 && v2.doubleValue() != Math.floor(v2.doubleValue()) + ? ExprNullValue.of() + : new ExprDoubleValue(Math.pow(v1.doubleValue(), v2.doubleValue()))), + DOUBLE, + DOUBLE, + DOUBLE)); + } + + /** + * Definition of rand() and rand(N) function. rand() returns a random floating-point value in the + * range 0 <= value < 1.0 If integer N is specified, the seed is initialized prior to execution. * One implication of this behavior is with identical argument N,rand(N) returns the same value - * each time, and thus produces a repeatable sequence of column values. - * The supported signature of rand function is - * ([INTEGER]) -> FLOAT + * each time, and thus produces a repeatable sequence of column values. The supported signature of + * rand function is ([INTEGER]) -> FLOAT */ private static DefaultFunctionResolver rand() { - return define(BuiltinFunctionName.RAND.getName(), + return define( + BuiltinFunctionName.RAND.getName(), impl(() -> new ExprFloatValue(new Random().nextFloat()), FLOAT), - impl(nullMissingHandling( - v -> new ExprFloatValue(new Random(v.integerValue()).nextFloat())), FLOAT, INTEGER) - ); + impl( + nullMissingHandling(v -> new ExprFloatValue(new Random(v.integerValue()).nextFloat())), + FLOAT, + INTEGER)); } /** - * Definition of rint(x) function. - * Returns the closest whole integer value to x - * The supported signature is - * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of rint(x) function. Returns the closest whole integer value to x The supported + * signature is BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver rint() { - return baseMathFunction(BuiltinFunctionName.RINT.getName(), - v -> new ExprDoubleValue(Math.rint(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.RINT.getName(), + v -> new ExprDoubleValue(Math.rint(v.doubleValue())), + DOUBLE); } /** - * Definition of round(x)/round(x, d) function. - * Rounds the argument x to d decimal places, d defaults to 0 if not specified. - * The supported signature of round function is - * (x: INTEGER [, y: INTEGER]) -> INTEGER - * (x: LONG [, y: INTEGER]) -> LONG - * (x: FLOAT [, y: INTEGER]) -> FLOAT - * (x: DOUBLE [, y: INTEGER]) -> DOUBLE + * Definition of round(x)/round(x, d) function. Rounds the argument x to d decimal places, d + * defaults to 0 if not specified. The supported signature of round function is (x: INTEGER [, y: + * INTEGER]) -> INTEGER (x: LONG [, y: INTEGER]) -> LONG (x: FLOAT [, y: INTEGER]) -> FLOAT (x: + * DOUBLE [, y: INTEGER]) -> DOUBLE */ private static DefaultFunctionResolver round() { - return define(BuiltinFunctionName.ROUND.getName(), + return define( + BuiltinFunctionName.ROUND.getName(), // rand(x) - impl(nullMissingHandling(v -> new ExprLongValue((long) Math.round(v.integerValue()))), - LONG, INTEGER), - impl(nullMissingHandling(v -> new ExprLongValue((long) Math.round(v.longValue()))), - LONG, LONG), - impl(nullMissingHandling(v -> new ExprDoubleValue((double) Math.round(v.floatValue()))), - DOUBLE, FLOAT), - impl(nullMissingHandling(v -> new ExprDoubleValue(new BigDecimal(v.doubleValue()) - .setScale(0, RoundingMode.HALF_UP).doubleValue())), - DOUBLE, DOUBLE), + impl( + nullMissingHandling(v -> new ExprLongValue((long) Math.round(v.integerValue()))), + LONG, + INTEGER), + impl( + nullMissingHandling(v -> new ExprLongValue((long) Math.round(v.longValue()))), + LONG, + LONG), + impl( + nullMissingHandling(v -> new ExprDoubleValue((double) Math.round(v.floatValue()))), + DOUBLE, + FLOAT), + impl( + nullMissingHandling( + v -> + new ExprDoubleValue( + new BigDecimal(v.doubleValue()) + .setScale(0, RoundingMode.HALF_UP) + .doubleValue())), + DOUBLE, + DOUBLE), // rand(x, d) - impl(nullMissingHandling((x, d) -> new ExprLongValue(new BigDecimal(x.integerValue()) - .setScale(d.integerValue(), RoundingMode.HALF_UP).longValue())), - LONG, INTEGER, INTEGER), - impl(nullMissingHandling((x, d) -> new ExprLongValue(new BigDecimal(x.longValue()) - .setScale(d.integerValue(), RoundingMode.HALF_UP).longValue())), - LONG, LONG, INTEGER), - impl(nullMissingHandling((x, d) -> new ExprDoubleValue(new BigDecimal(x.floatValue()) - .setScale(d.integerValue(), RoundingMode.HALF_UP).doubleValue())), - DOUBLE, FLOAT, INTEGER), - impl(nullMissingHandling((x, d) -> new ExprDoubleValue(new BigDecimal(x.doubleValue()) - .setScale(d.integerValue(), RoundingMode.HALF_UP).doubleValue())), - DOUBLE, DOUBLE, INTEGER)); - } - - /** - * Definition of sign(x) function. - * Returns the sign of the argument as -1, 0, or 1 - * depending on whether x is negative, zero, or positive - * The supported signature is + impl( + nullMissingHandling( + (x, d) -> + new ExprLongValue( + new BigDecimal(x.integerValue()) + .setScale(d.integerValue(), RoundingMode.HALF_UP) + .longValue())), + LONG, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (x, d) -> + new ExprLongValue( + new BigDecimal(x.longValue()) + .setScale(d.integerValue(), RoundingMode.HALF_UP) + .longValue())), + LONG, + LONG, + INTEGER), + impl( + nullMissingHandling( + (x, d) -> + new ExprDoubleValue( + new BigDecimal(x.floatValue()) + .setScale(d.integerValue(), RoundingMode.HALF_UP) + .doubleValue())), + DOUBLE, + FLOAT, + INTEGER), + impl( + nullMissingHandling( + (x, d) -> + new ExprDoubleValue( + new BigDecimal(x.doubleValue()) + .setScale(d.integerValue(), RoundingMode.HALF_UP) + .doubleValue())), + DOUBLE, + DOUBLE, + INTEGER)); + } + + /** + * Definition of sign(x) function. Returns the sign of the argument as -1, 0, or 1 depending on + * whether x is negative, zero, or positive The supported signature is * SHORT/INTEGER/LONG/FLOAT/DOUBLE -> INTEGER */ private static DefaultFunctionResolver sign() { - return baseMathFunction(BuiltinFunctionName.SIGN.getName(), - v -> new ExprIntegerValue(Math.signum(v.doubleValue())), INTEGER); + return baseMathFunction( + BuiltinFunctionName.SIGN.getName(), + v -> new ExprIntegerValue(Math.signum(v.doubleValue())), + INTEGER); } /** - * Definition of signum(x) function. - * Returns the sign of the argument as -1.0, 0, or 1.0 - * depending on whether x is negative, zero, or positive - * The supported signature is + * Definition of signum(x) function. Returns the sign of the argument as -1.0, 0, or 1.0 depending + * on whether x is negative, zero, or positive The supported signature is * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> INTEGER */ private static DefaultFunctionResolver signum() { - return baseMathFunction(BuiltinFunctionName.SIGNUM.getName(), - v -> new ExprIntegerValue(Math.signum(v.doubleValue())), INTEGER); + return baseMathFunction( + BuiltinFunctionName.SIGNUM.getName(), + v -> new ExprIntegerValue(Math.signum(v.doubleValue())), + INTEGER); } /** - * Definition of sinh(x) function. - * Returns the hyperbolix sine of x, defined as (((e^x) - (e^(-x))) / 2) - * The supported signature is - * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of sinh(x) function. Returns the hyperbolix sine of x, defined as (((e^x) - + * (e^(-x))) / 2) The supported signature is BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver sinh() { - return baseMathFunction(BuiltinFunctionName.SINH.getName(), - v -> new ExprDoubleValue(Math.sinh(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.SINH.getName(), + v -> new ExprDoubleValue(Math.sinh(v.doubleValue())), + DOUBLE); } /** - * Definition of sqrt(x) function. - * Calculate the square root of a non-negative number x - * The supported signature is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of sqrt(x) function. Calculate the square root of a non-negative number x The + * supported signature is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver sqrt() { - return baseMathFunction(BuiltinFunctionName.SQRT.getName(), - v -> v.doubleValue() < 0 ? ExprNullValue.of() : - new ExprDoubleValue(Math.sqrt(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.SQRT.getName(), + v -> + v.doubleValue() < 0 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.sqrt(v.doubleValue())), + DOUBLE); } /** - * Definition of cbrt(x) function. - * Calculate the cube root of a number x - * The supported signature is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of cbrt(x) function. Calculate the cube root of a number x The supported signature + * is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver cbrt() { - return baseMathFunction(BuiltinFunctionName.CBRT.getName(), - v -> new ExprDoubleValue(Math.cbrt(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.CBRT.getName(), + v -> new ExprDoubleValue(Math.cbrt(v.doubleValue())), + DOUBLE); } /** - * Definition of truncate(x, d) function. - * Returns the number x, truncated to d decimal places - * The supported signature of round function is - * (x: INTEGER, y: INTEGER) -> LONG - * (x: LONG, y: INTEGER) -> LONG - * (x: FLOAT, y: INTEGER) -> DOUBLE - * (x: DOUBLE, y: INTEGER) -> DOUBLE + * Definition of truncate(x, d) function. Returns the number x, truncated to d decimal places The + * supported signature of round function is (x: INTEGER, y: INTEGER) -> LONG (x: LONG, y: INTEGER) + * -> LONG (x: FLOAT, y: INTEGER) -> DOUBLE (x: DOUBLE, y: INTEGER) -> DOUBLE */ private static DefaultFunctionResolver truncate() { - return define(BuiltinFunctionName.TRUNCATE.getName(), - impl(nullMissingHandling((x, y) -> new ExprLongValue(BigDecimal.valueOf(x.integerValue()) - .setScale(y.integerValue(), RoundingMode.DOWN).longValue())), - LONG, INTEGER, INTEGER), - impl(nullMissingHandling((x, y) -> new ExprLongValue(BigDecimal.valueOf(x.longValue()) - .setScale(y.integerValue(), RoundingMode.DOWN).longValue())), - LONG, LONG, INTEGER), - impl(nullMissingHandling((x, y) -> new ExprDoubleValue(BigDecimal.valueOf(x.floatValue()) - .setScale(y.integerValue(), RoundingMode.DOWN).doubleValue())), - DOUBLE, FLOAT, INTEGER), - impl(nullMissingHandling((x, y) -> new ExprDoubleValue(BigDecimal.valueOf(x.doubleValue()) - .setScale(y.integerValue(), RoundingMode.DOWN).doubleValue())), - DOUBLE, DOUBLE, INTEGER)); - } - - /** - * Definition of acos(x) function. - * Calculates the arc cosine of x, that is, the value whose cosine is x. - * Returns NULL if x is not in the range -1 to 1. - * The supported signature of acos function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + return define( + BuiltinFunctionName.TRUNCATE.getName(), + impl( + nullMissingHandling( + (x, y) -> + new ExprLongValue( + BigDecimal.valueOf(x.integerValue()) + .setScale(y.integerValue(), RoundingMode.DOWN) + .longValue())), + LONG, + INTEGER, + INTEGER), + impl( + nullMissingHandling( + (x, y) -> + new ExprLongValue( + BigDecimal.valueOf(x.longValue()) + .setScale(y.integerValue(), RoundingMode.DOWN) + .longValue())), + LONG, + LONG, + INTEGER), + impl( + nullMissingHandling( + (x, y) -> + new ExprDoubleValue( + BigDecimal.valueOf(x.floatValue()) + .setScale(y.integerValue(), RoundingMode.DOWN) + .doubleValue())), + DOUBLE, + FLOAT, + INTEGER), + impl( + nullMissingHandling( + (x, y) -> + new ExprDoubleValue( + BigDecimal.valueOf(x.doubleValue()) + .setScale(y.integerValue(), RoundingMode.DOWN) + .doubleValue())), + DOUBLE, + DOUBLE, + INTEGER)); + } + + /** + * Definition of acos(x) function. Calculates the arc cosine of x, that is, the value whose cosine + * is x. Returns NULL if x is not in the range -1 to 1. The supported signature of acos function + * is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver acos() { - return define(BuiltinFunctionName.ACOS.getName(), + return define( + BuiltinFunctionName.ACOS.getName(), ExprCoreType.numberTypes().stream() - .map(type -> impl(nullMissingHandling( - v -> v.doubleValue() < -1 || v.doubleValue() > 1 ? ExprNullValue.of() : - new ExprDoubleValue(Math.acos(v.doubleValue()))), - DOUBLE, type)).collect(Collectors.toList())); - } - - /** - * Definition of asin(x) function. - * Calculates the arc sine of x, that is, the value whose sine is x. - * Returns NULL if x is not in the range -1 to 1. - * The supported signature of asin function is + .map( + type -> + impl( + nullMissingHandling( + v -> + v.doubleValue() < -1 || v.doubleValue() > 1 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.acos(v.doubleValue()))), + DOUBLE, + type)) + .collect(Collectors.toList())); + } + + /** + * Definition of asin(x) function. Calculates the arc sine of x, that is, the value whose sine is + * x. Returns NULL if x is not in the range -1 to 1. The supported signature of asin function is * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver asin() { - return define(BuiltinFunctionName.ASIN.getName(), + return define( + BuiltinFunctionName.ASIN.getName(), ExprCoreType.numberTypes().stream() - .map(type -> impl(nullMissingHandling( - v -> v.doubleValue() < -1 || v.doubleValue() > 1 ? ExprNullValue.of() : - new ExprDoubleValue(Math.asin(v.doubleValue()))), - DOUBLE, type)).collect(Collectors.toList())); - } - - /** - * Definition of atan(x) and atan(y, x) function. - * atan(x) calculates the arc tangent of x, that is, the value whose tangent is x. - * atan(y, x) calculates the arc tangent of y / x, except that the signs of both arguments - * are used to determine the quadrant of the result. - * The supported signature of atan function is - * (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) -> DOUBLE + .map( + type -> + impl( + nullMissingHandling( + v -> + v.doubleValue() < -1 || v.doubleValue() > 1 + ? ExprNullValue.of() + : new ExprDoubleValue(Math.asin(v.doubleValue()))), + DOUBLE, + type)) + .collect(Collectors.toList())); + } + + /** + * Definition of atan(x) and atan(y, x) function. atan(x) calculates the arc tangent of x, that + * is, the value whose tangent is x. atan(y, x) calculates the arc tangent of y / x, except that + * the signs of both arguments are used to determine the quadrant of the result. The supported + * signature of atan function is (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) -> + * DOUBLE */ private static DefaultFunctionResolver atan() { - ImmutableList.Builder>> builder = new ImmutableList.Builder<>(); + ImmutableList.Builder< + SerializableFunction>> + builder = new ImmutableList.Builder<>(); for (ExprType type : ExprCoreType.numberTypes()) { - builder.add(impl(nullMissingHandling(x -> new ExprDoubleValue(Math.atan(x.doubleValue()))), - type, DOUBLE)); - builder.add(impl(nullMissingHandling((y, x) -> new ExprDoubleValue(Math.atan2(y.doubleValue(), - x.doubleValue()))), - DOUBLE, type, type)); + builder.add( + impl( + nullMissingHandling(x -> new ExprDoubleValue(Math.atan(x.doubleValue()))), + type, + DOUBLE)); + builder.add( + impl( + nullMissingHandling( + (y, x) -> new ExprDoubleValue(Math.atan2(y.doubleValue(), x.doubleValue()))), + DOUBLE, + type, + type)); } return define(BuiltinFunctionName.ATAN.getName(), builder.build()); } /** - * Definition of atan2(y, x) function. - * Calculates the arc tangent of y / x, except that the signs of both arguments - * are used to determine the quadrant of the result. - * The supported signature of atan2 function is - * (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) -> DOUBLE + * Definition of atan2(y, x) function. Calculates the arc tangent of y / x, except that the signs + * of both arguments are used to determine the quadrant of the result. The supported signature of + * atan2 function is (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE) -> DOUBLE */ private static DefaultFunctionResolver atan2() { - ImmutableList.Builder>> builder = new ImmutableList.Builder<>(); + ImmutableList.Builder< + SerializableFunction>> + builder = new ImmutableList.Builder<>(); for (ExprType type : ExprCoreType.numberTypes()) { - builder.add(impl(nullMissingHandling((y, x) -> new ExprDoubleValue(Math.atan2(y.doubleValue(), - x.doubleValue()))), DOUBLE, type, type)); + builder.add( + impl( + nullMissingHandling( + (y, x) -> new ExprDoubleValue(Math.atan2(y.doubleValue(), x.doubleValue()))), + DOUBLE, + type, + type)); } return define(BuiltinFunctionName.ATAN2.getName(), builder.build()); } /** - * Definition of cos(x) function. - * Calculates the cosine of X, where X is given in radians - * The supported signature of cos function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of cos(x) function. Calculates the cosine of X, where X is given in radians The + * supported signature of cos function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver cos() { - return baseMathFunction(BuiltinFunctionName.COS.getName(), - v -> new ExprDoubleValue(Math.cos(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.COS.getName(), + v -> new ExprDoubleValue(Math.cos(v.doubleValue())), + DOUBLE); } /** - * Definition of cosh(x) function. - * Returns the hyperbolic cosine of x, defined as (((e^x) + (e^(-x))) / 2) - * The supported signature is - * BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of cosh(x) function. Returns the hyperbolic cosine of x, defined as (((e^x) + + * (e^(-x))) / 2) The supported signature is BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver cosh() { - return baseMathFunction(BuiltinFunctionName.COSH.getName(), - v -> new ExprDoubleValue(Math.cosh(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.COSH.getName(), + v -> new ExprDoubleValue(Math.cosh(v.doubleValue())), + DOUBLE); } /** - * Definition of cot(x) function. - * Calculates the cotangent of x - * The supported signature of cot function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of cot(x) function. Calculates the cotangent of x The supported signature of cot + * function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver cot() { - return define(BuiltinFunctionName.COT.getName(), + return define( + BuiltinFunctionName.COT.getName(), ExprCoreType.numberTypes().stream() - .map(type -> impl(nullMissingHandling( - v -> { - Double value = v.doubleValue(); - if (value == 0) { - throw new ArithmeticException( - String.format("Out of range value for cot(%s)", value)); - } - return new ExprDoubleValue(1 / Math.tan(value)); - }), - DOUBLE, type)).collect(Collectors.toList())); - } - - /** - * Definition of degrees(x) function. - * Converts x from radians to degrees - * The supported signature of degrees function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + .map( + type -> + impl( + nullMissingHandling( + v -> { + Double value = v.doubleValue(); + if (value == 0) { + throw new ArithmeticException( + String.format("Out of range value for cot(%s)", value)); + } + return new ExprDoubleValue(1 / Math.tan(value)); + }), + DOUBLE, + type)) + .collect(Collectors.toList())); + } + + /** + * Definition of degrees(x) function. Converts x from radians to degrees The supported signature + * of degrees function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver degrees() { - return baseMathFunction(BuiltinFunctionName.DEGREES.getName(), - v -> new ExprDoubleValue(Math.toDegrees(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.DEGREES.getName(), + v -> new ExprDoubleValue(Math.toDegrees(v.doubleValue())), + DOUBLE); } /** - * Definition of radians(x) function. - * Converts x from degrees to radians - * The supported signature of radians function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of radians(x) function. Converts x from degrees to radians The supported signature + * of radians function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver radians() { - return baseMathFunction(BuiltinFunctionName.RADIANS.getName(), - v -> new ExprDoubleValue(Math.toRadians(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.RADIANS.getName(), + v -> new ExprDoubleValue(Math.toRadians(v.doubleValue())), + DOUBLE); } /** - * Definition of sin(x) function. - * Calculates the sine of x, where x is given in radians - * The supported signature of sin function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of sin(x) function. Calculates the sine of x, where x is given in radians The + * supported signature of sin function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver sin() { - return baseMathFunction(BuiltinFunctionName.SIN.getName(), - v -> new ExprDoubleValue(Math.sin(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.SIN.getName(), + v -> new ExprDoubleValue(Math.sin(v.doubleValue())), + DOUBLE); } /** - * Definition of tan(x) function. - * Calculates the tangent of x, where x is given in radians - * The supported signature of tan function is - * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE + * Definition of tan(x) function. Calculates the tangent of x, where x is given in radians The + * supported signature of tan function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE */ private static DefaultFunctionResolver tan() { - return baseMathFunction(BuiltinFunctionName.TAN.getName(), - v -> new ExprDoubleValue(Math.tan(v.doubleValue())), DOUBLE); + return baseMathFunction( + BuiltinFunctionName.TAN.getName(), + v -> new ExprDoubleValue(Math.tan(v.doubleValue())), + DOUBLE); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/convert/TypeCastOperator.java b/core/src/main/java/org/opensearch/sql/expression/operator/convert/TypeCastOperator.java index d3295a53f07..7c3565f69c2 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/convert/TypeCastOperator.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/convert/TypeCastOperator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.convert; import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN; @@ -46,9 +45,7 @@ @UtilityClass public class TypeCastOperator { - /** - * Register Type Cast Operator. - */ + /** Register Type Cast Operator. */ public static void register(BuiltinFunctionRepository repository) { repository.register(castToString()); repository.register(castToByte()); @@ -64,148 +61,175 @@ public static void register(BuiltinFunctionRepository repository) { repository.register(castToDatetime()); } - private static DefaultFunctionResolver castToString() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_STRING.getName(), + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_STRING.getName(), Stream.concat( - Arrays.asList(BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN, TIME, DATE, - TIMESTAMP, DATETIME).stream() - .map(type -> impl( - nullMissingHandling((v) -> new ExprStringValue(v.value().toString())), - STRING, type)), - Stream.of(impl(nullMissingHandling((v) -> v), STRING, STRING))) - .collect(Collectors.toList()) - ); + Arrays.asList( + BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN, TIME, DATE, TIMESTAMP, + DATETIME) + .stream() + .map( + type -> + impl( + nullMissingHandling( + (v) -> new ExprStringValue(v.value().toString())), + STRING, + type)), + Stream.of(impl(nullMissingHandling((v) -> v), STRING, STRING))) + .collect(Collectors.toList())); } private static DefaultFunctionResolver castToByte() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_BYTE.getName(), - impl(nullMissingHandling( - (v) -> new ExprByteValue(Byte.valueOf(v.stringValue()))), BYTE, STRING), - impl(nullMissingHandling( - (v) -> new ExprByteValue(v.byteValue())), BYTE, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprByteValue(v.booleanValue() ? 1 : 0)), BYTE, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_BYTE.getName(), + impl( + nullMissingHandling((v) -> new ExprByteValue(Byte.valueOf(v.stringValue()))), + BYTE, + STRING), + impl(nullMissingHandling((v) -> new ExprByteValue(v.byteValue())), BYTE, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprByteValue(v.booleanValue() ? 1 : 0)), + BYTE, + BOOLEAN)); } private static DefaultFunctionResolver castToShort() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_SHORT.getName(), - impl(nullMissingHandling( - (v) -> new ExprShortValue(Short.valueOf(v.stringValue()))), SHORT, STRING), - impl(nullMissingHandling( - (v) -> new ExprShortValue(v.shortValue())), SHORT, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprShortValue(v.booleanValue() ? 1 : 0)), SHORT, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_SHORT.getName(), + impl( + nullMissingHandling((v) -> new ExprShortValue(Short.valueOf(v.stringValue()))), + SHORT, + STRING), + impl(nullMissingHandling((v) -> new ExprShortValue(v.shortValue())), SHORT, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprShortValue(v.booleanValue() ? 1 : 0)), + SHORT, + BOOLEAN)); } private static DefaultFunctionResolver castToInt() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_INT.getName(), - impl(nullMissingHandling( - (v) -> new ExprIntegerValue(Integer.valueOf(v.stringValue()))), INTEGER, STRING), - impl(nullMissingHandling( - (v) -> new ExprIntegerValue(v.integerValue())), INTEGER, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprIntegerValue(v.booleanValue() ? 1 : 0)), INTEGER, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_INT.getName(), + impl( + nullMissingHandling((v) -> new ExprIntegerValue(Integer.valueOf(v.stringValue()))), + INTEGER, + STRING), + impl(nullMissingHandling((v) -> new ExprIntegerValue(v.integerValue())), INTEGER, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprIntegerValue(v.booleanValue() ? 1 : 0)), + INTEGER, + BOOLEAN)); } private static DefaultFunctionResolver castToLong() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_LONG.getName(), - impl(nullMissingHandling( - (v) -> new ExprLongValue(Long.valueOf(v.stringValue()))), LONG, STRING), - impl(nullMissingHandling( - (v) -> new ExprLongValue(v.longValue())), LONG, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprLongValue(v.booleanValue() ? 1L : 0L)), LONG, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_LONG.getName(), + impl( + nullMissingHandling((v) -> new ExprLongValue(Long.valueOf(v.stringValue()))), + LONG, + STRING), + impl(nullMissingHandling((v) -> new ExprLongValue(v.longValue())), LONG, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprLongValue(v.booleanValue() ? 1L : 0L)), + LONG, + BOOLEAN)); } private static DefaultFunctionResolver castToFloat() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_FLOAT.getName(), - impl(nullMissingHandling( - (v) -> new ExprFloatValue(Float.valueOf(v.stringValue()))), FLOAT, STRING), - impl(nullMissingHandling( - (v) -> new ExprFloatValue(v.floatValue())), FLOAT, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprFloatValue(v.booleanValue() ? 1f : 0f)), FLOAT, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_FLOAT.getName(), + impl( + nullMissingHandling((v) -> new ExprFloatValue(Float.valueOf(v.stringValue()))), + FLOAT, + STRING), + impl(nullMissingHandling((v) -> new ExprFloatValue(v.floatValue())), FLOAT, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprFloatValue(v.booleanValue() ? 1f : 0f)), + FLOAT, + BOOLEAN)); } private static DefaultFunctionResolver castToDouble() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_DOUBLE.getName(), - impl(nullMissingHandling( - (v) -> new ExprDoubleValue(Double.valueOf(v.stringValue()))), DOUBLE, STRING), - impl(nullMissingHandling( - (v) -> new ExprDoubleValue(v.doubleValue())), DOUBLE, DOUBLE), - impl(nullMissingHandling( - (v) -> new ExprDoubleValue(v.booleanValue() ? 1D : 0D)), DOUBLE, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_DOUBLE.getName(), + impl( + nullMissingHandling((v) -> new ExprDoubleValue(Double.valueOf(v.stringValue()))), + DOUBLE, + STRING), + impl(nullMissingHandling((v) -> new ExprDoubleValue(v.doubleValue())), DOUBLE, DOUBLE), + impl( + nullMissingHandling((v) -> new ExprDoubleValue(v.booleanValue() ? 1D : 0D)), + DOUBLE, + BOOLEAN)); } private static DefaultFunctionResolver castToBoolean() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_BOOLEAN.getName(), - impl(nullMissingHandling( - (v) -> ExprBooleanValue.of(Boolean.valueOf(v.stringValue()))), BOOLEAN, STRING), - impl(nullMissingHandling( - (v) -> ExprBooleanValue.of(v.doubleValue() != 0)), BOOLEAN, DOUBLE), - impl(nullMissingHandling((v) -> v), BOOLEAN, BOOLEAN) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_BOOLEAN.getName(), + impl( + nullMissingHandling((v) -> ExprBooleanValue.of(Boolean.valueOf(v.stringValue()))), + BOOLEAN, + STRING), + impl( + nullMissingHandling((v) -> ExprBooleanValue.of(v.doubleValue() != 0)), BOOLEAN, DOUBLE), + impl(nullMissingHandling((v) -> v), BOOLEAN, BOOLEAN)); } private static DefaultFunctionResolver castToDate() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_DATE.getName(), - impl(nullMissingHandling( - (v) -> new ExprDateValue(v.stringValue())), DATE, STRING), - impl(nullMissingHandling( - (v) -> new ExprDateValue(v.dateValue())), DATE, DATETIME), - impl(nullMissingHandling( - (v) -> new ExprDateValue(v.dateValue())), DATE, TIMESTAMP), - impl(nullMissingHandling((v) -> v), DATE, DATE) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_DATE.getName(), + impl(nullMissingHandling((v) -> new ExprDateValue(v.stringValue())), DATE, STRING), + impl(nullMissingHandling((v) -> new ExprDateValue(v.dateValue())), DATE, DATETIME), + impl(nullMissingHandling((v) -> new ExprDateValue(v.dateValue())), DATE, TIMESTAMP), + impl(nullMissingHandling((v) -> v), DATE, DATE)); } private static DefaultFunctionResolver castToTime() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_TIME.getName(), - impl(nullMissingHandling( - (v) -> new ExprTimeValue(v.stringValue())), TIME, STRING), - impl(nullMissingHandling( - (v) -> new ExprTimeValue(v.timeValue())), TIME, DATETIME), - impl(nullMissingHandling( - (v) -> new ExprTimeValue(v.timeValue())), TIME, TIMESTAMP), - impl(nullMissingHandling((v) -> v), TIME, TIME) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_TIME.getName(), + impl(nullMissingHandling((v) -> new ExprTimeValue(v.stringValue())), TIME, STRING), + impl(nullMissingHandling((v) -> new ExprTimeValue(v.timeValue())), TIME, DATETIME), + impl(nullMissingHandling((v) -> new ExprTimeValue(v.timeValue())), TIME, TIMESTAMP), + impl(nullMissingHandling((v) -> v), TIME, TIME)); } // `DATE`/`TIME`/`DATETIME` -> `DATETIME`/TIMESTAMP` cast tested in BinaryPredicateOperatorTest private static DefaultFunctionResolver castToTimestamp() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_TIMESTAMP.getName(), - impl(nullMissingHandling( - (v) -> new ExprTimestampValue(v.stringValue())), TIMESTAMP, STRING), - impl(nullMissingHandling( - (v) -> new ExprTimestampValue(v.timestampValue())), TIMESTAMP, DATETIME), - impl(nullMissingHandling( - (v) -> new ExprTimestampValue(v.timestampValue())), TIMESTAMP, DATE), - implWithProperties(nullMissingHandlingWithProperties( - (fp, v) -> new ExprTimestampValue(((ExprTimeValue)v).timestampValue(fp))), - TIMESTAMP, TIME), - impl(nullMissingHandling((v) -> v), TIMESTAMP, TIMESTAMP) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_TIMESTAMP.getName(), + impl( + nullMissingHandling((v) -> new ExprTimestampValue(v.stringValue())), TIMESTAMP, STRING), + impl( + nullMissingHandling((v) -> new ExprTimestampValue(v.timestampValue())), + TIMESTAMP, + DATETIME), + impl( + nullMissingHandling((v) -> new ExprTimestampValue(v.timestampValue())), + TIMESTAMP, + DATE), + implWithProperties( + nullMissingHandlingWithProperties( + (fp, v) -> new ExprTimestampValue(((ExprTimeValue) v).timestampValue(fp))), + TIMESTAMP, + TIME), + impl(nullMissingHandling((v) -> v), TIMESTAMP, TIMESTAMP)); } private static DefaultFunctionResolver castToDatetime() { - return FunctionDSL.define(BuiltinFunctionName.CAST_TO_DATETIME.getName(), - impl(nullMissingHandling( - (v) -> new ExprDatetimeValue(v.stringValue())), DATETIME, STRING), - impl(nullMissingHandling( - (v) -> new ExprDatetimeValue(v.datetimeValue())), DATETIME, TIMESTAMP), - impl(nullMissingHandling( - (v) -> new ExprDatetimeValue(v.datetimeValue())), DATETIME, DATE), - implWithProperties(nullMissingHandlingWithProperties( - (fp, v) -> new ExprDatetimeValue(((ExprTimeValue)v).datetimeValue(fp))), - DATETIME, TIME), - impl(nullMissingHandling((v) -> v), DATETIME, DATETIME) - ); + return FunctionDSL.define( + BuiltinFunctionName.CAST_TO_DATETIME.getName(), + impl(nullMissingHandling((v) -> new ExprDatetimeValue(v.stringValue())), DATETIME, STRING), + impl( + nullMissingHandling((v) -> new ExprDatetimeValue(v.datetimeValue())), + DATETIME, + TIMESTAMP), + impl(nullMissingHandling((v) -> new ExprDatetimeValue(v.datetimeValue())), DATETIME, DATE), + implWithProperties( + nullMissingHandlingWithProperties( + (fp, v) -> new ExprDatetimeValue(((ExprTimeValue) v).datetimeValue(fp))), + DATETIME, + TIME), + impl(nullMissingHandling((v) -> v), DATETIME, DATETIME)); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator.java b/core/src/main/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator.java index cc5b47bde15..042a4f3ad47 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.predicate; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; @@ -30,11 +29,10 @@ import org.opensearch.sql.utils.OperatorUtils; /** - * The definition of binary predicate function - * and, Accepts two Boolean values and produces a Boolean. - * or, Accepts two Boolean values and produces a Boolean. - * xor, Accepts two Boolean values and produces a Boolean. - * equalTo, Compare the left expression and right expression and produces a Boolean. + * The definition of binary predicate function and, Accepts two Boolean values and produces a + * Boolean. or, Accepts two Boolean values and produces a Boolean. xor, Accepts two Boolean values + * and produces a Boolean. equalTo, Compare the left expression and right expression and produces a + * Boolean. */ @UtilityClass public class BinaryPredicateOperator { @@ -59,17 +57,8 @@ public static void register(BuiltinFunctionRepository repository) { } /** - * The and logic. - * A B A AND B - * TRUE TRUE TRUE - * TRUE FALSE FALSE - * TRUE NULL NULL - * TRUE MISSING MISSING - * FALSE FALSE FALSE - * FALSE NULL FALSE - * FALSE MISSING FALSE - * NULL NULL NULL - * NULL MISSING MISSING + * The and logic. A B A AND B TRUE TRUE TRUE TRUE FALSE FALSE TRUE NULL NULL TRUE MISSING MISSING + * FALSE FALSE FALSE FALSE NULL FALSE FALSE MISSING FALSE NULL NULL NULL NULL MISSING MISSING * MISSING MISSING MISSING */ private static Table andTable = @@ -87,18 +76,9 @@ public static void register(BuiltinFunctionRepository repository) { .build(); /** - * The or logic. - * A B A AND B - * TRUE TRUE TRUE - * TRUE FALSE TRUE - * TRUE NULL TRUE - * TRUE MISSING TRUE - * FALSE FALSE FALSE - * FALSE NULL NULL - * FALSE MISSING MISSING - * NULL NULL NULL - * NULL MISSING NULL - * MISSING MISSING MISSING + * The or logic. A B A AND B TRUE TRUE TRUE TRUE FALSE TRUE TRUE NULL TRUE TRUE MISSING TRUE FALSE + * FALSE FALSE FALSE NULL NULL FALSE MISSING MISSING NULL NULL NULL NULL MISSING NULL MISSING + * MISSING MISSING */ private static Table orTable = new ImmutableTable.Builder() @@ -115,17 +95,8 @@ public static void register(BuiltinFunctionRepository repository) { .build(); /** - * The xor logic. - * A B A AND B - * TRUE TRUE FALSE - * TRUE FALSE TRUE - * TRUE NULL TRUE - * TRUE MISSING TRUE - * FALSE FALSE FALSE - * FALSE NULL NULL - * FALSE MISSING MISSING - * NULL NULL NULL - * NULL MISSING NULL + * The xor logic. A B A AND B TRUE TRUE FALSE TRUE FALSE TRUE TRUE NULL TRUE TRUE MISSING TRUE + * FALSE FALSE FALSE FALSE NULL NULL FALSE MISSING MISSING NULL NULL NULL NULL MISSING NULL * MISSING MISSING MISSING */ private static Table xorTable = @@ -143,87 +114,132 @@ public static void register(BuiltinFunctionRepository repository) { .build(); private static DefaultFunctionResolver and() { - return define(BuiltinFunctionName.AND.getName(), + return define( + BuiltinFunctionName.AND.getName(), impl((v1, v2) -> lookupTableFunction(v1, v2, andTable), BOOLEAN, BOOLEAN, BOOLEAN)); } private static DefaultFunctionResolver or() { - return define(BuiltinFunctionName.OR.getName(), + return define( + BuiltinFunctionName.OR.getName(), impl((v1, v2) -> lookupTableFunction(v1, v2, orTable), BOOLEAN, BOOLEAN, BOOLEAN)); } private static DefaultFunctionResolver xor() { - return define(BuiltinFunctionName.XOR.getName(), + return define( + BuiltinFunctionName.XOR.getName(), impl((v1, v2) -> lookupTableFunction(v1, v2, xorTable), BOOLEAN, BOOLEAN, BOOLEAN)); } private static DefaultFunctionResolver equal() { - return define(BuiltinFunctionName.EQUAL.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(v1.equals(v2))), - BOOLEAN, type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.EQUAL.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.equals(v2))), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver notEqual() { - return define(BuiltinFunctionName.NOTEQUAL.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(!v1.equals(v2))), - BOOLEAN, type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.NOTEQUAL.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(!v1.equals(v2))), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver less() { - return define(BuiltinFunctionName.LESS.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) < 0)), - BOOLEAN,type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.LESS.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) < 0)), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver lte() { - return define(BuiltinFunctionName.LTE.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) <= 0)), - BOOLEAN, type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.LTE.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) <= 0)), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver greater() { - return define(BuiltinFunctionName.GREATER.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) > 0)), - BOOLEAN, type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.GREATER.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) > 0)), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver gte() { - return define(BuiltinFunctionName.GTE.getName(), ExprCoreType.coreTypes().stream() - .map(type -> impl(nullMissingHandling( - (v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) >= 0)), - BOOLEAN, type, type)) - .collect(Collectors.toList())); + return define( + BuiltinFunctionName.GTE.getName(), + ExprCoreType.coreTypes().stream() + .map( + type -> + impl( + nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) >= 0)), + BOOLEAN, + type, + type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver like() { - return define(BuiltinFunctionName.LIKE.getName(), + return define( + BuiltinFunctionName.LIKE.getName(), impl(nullMissingHandling(OperatorUtils::matches), BOOLEAN, STRING, STRING)); } private static DefaultFunctionResolver regexp() { - return define(BuiltinFunctionName.REGEXP.getName(), + return define( + BuiltinFunctionName.REGEXP.getName(), impl(nullMissingHandling(OperatorUtils::matchesRegexp), INTEGER, STRING, STRING)); } private static DefaultFunctionResolver notLike() { - return define(BuiltinFunctionName.NOT_LIKE.getName(), - impl(nullMissingHandling( - (v1, v2) -> UnaryPredicateOperator.not(OperatorUtils.matches(v1, v2))), - BOOLEAN, STRING, STRING)); + return define( + BuiltinFunctionName.NOT_LIKE.getName(), + impl( + nullMissingHandling( + (v1, v2) -> UnaryPredicateOperator.not(OperatorUtils.matches(v1, v2))), + BOOLEAN, + STRING, + STRING)); } - private static ExprValue lookupTableFunction(ExprValue arg1, ExprValue arg2, - Table table) { + private static ExprValue lookupTableFunction( + ExprValue arg1, ExprValue arg2, Table table) { if (table.contains(arg1, arg2)) { return table.get(arg1, arg2); } else { diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperator.java b/core/src/main/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperator.java index 7d79d9d923c..dbea762ae10 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperator.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperator.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.predicate; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; @@ -28,14 +27,11 @@ import org.opensearch.sql.expression.function.SerializableFunction; /** - * The definition of unary predicate function - * not, Accepts one Boolean value and produces a Boolean. + * The definition of unary predicate function not, Accepts one Boolean value and produces a Boolean. */ @UtilityClass public class UnaryPredicateOperator { - /** - * Register Unary Predicate Function. - */ + /** Register Unary Predicate Function. */ public static void register(BuiltinFunctionRepository repository) { repository.register(not()); repository.register(isNotNull()); @@ -47,18 +43,12 @@ public static void register(BuiltinFunctionRepository repository) { } private static DefaultFunctionResolver not() { - return FunctionDSL.define(BuiltinFunctionName.NOT.getName(), FunctionDSL - .impl(UnaryPredicateOperator::not, BOOLEAN, BOOLEAN)); + return FunctionDSL.define( + BuiltinFunctionName.NOT.getName(), + FunctionDSL.impl(UnaryPredicateOperator::not, BOOLEAN, BOOLEAN)); } - /** - * The not logic. - * A NOT A - * TRUE FALSE - * FALSE TRUE - * NULL NULL - * MISSING MISSING - */ + /** The not logic. A NOT A TRUE FALSE FALSE TRUE NULL NULL MISSING MISSING */ public ExprValue not(ExprValue v) { if (v.isMissing() || v.isNull()) { return v; @@ -68,31 +58,33 @@ public ExprValue not(ExprValue v) { } private static DefaultFunctionResolver isNull(BuiltinFunctionName funcName) { - return FunctionDSL - .define(funcName.getName(), Arrays.stream(ExprCoreType.values()) - .map(type -> FunctionDSL - .impl((v) -> ExprBooleanValue.of(v.isNull()), BOOLEAN, type)) - .collect( - Collectors.toList())); + return FunctionDSL.define( + funcName.getName(), + Arrays.stream(ExprCoreType.values()) + .map(type -> FunctionDSL.impl((v) -> ExprBooleanValue.of(v.isNull()), BOOLEAN, type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver isNotNull() { - return FunctionDSL - .define(BuiltinFunctionName.IS_NOT_NULL.getName(), Arrays.stream(ExprCoreType.values()) - .map(type -> FunctionDSL - .impl((v) -> ExprBooleanValue.of(!v.isNull()), BOOLEAN, type)) - .collect( - Collectors.toList())); + return FunctionDSL.define( + BuiltinFunctionName.IS_NOT_NULL.getName(), + Arrays.stream(ExprCoreType.values()) + .map(type -> FunctionDSL.impl((v) -> ExprBooleanValue.of(!v.isNull()), BOOLEAN, type)) + .collect(Collectors.toList())); } private static DefaultFunctionResolver ifFunction() { FunctionName functionName = BuiltinFunctionName.IF.getName(); List typeList = ExprCoreType.coreTypes(); - List>> functionsOne = typeList.stream().map(v -> - impl((UnaryPredicateOperator::exprIf), v, BOOLEAN, v, v)) - .collect(Collectors.toList()); + List< + SerializableFunction< + FunctionName, + org.apache.commons.lang3.tuple.Pair>> + functionsOne = + typeList.stream() + .map(v -> impl((UnaryPredicateOperator::exprIf), v, BOOLEAN, v, v)) + .collect(Collectors.toList()); DefaultFunctionResolver functionResolver = FunctionDSL.define(functionName, functionsOne); return functionResolver; @@ -102,10 +94,14 @@ private static DefaultFunctionResolver ifNull() { FunctionName functionName = BuiltinFunctionName.IFNULL.getName(); List typeList = ExprCoreType.coreTypes(); - List>> functionsOne = typeList.stream().map(v -> - impl((UnaryPredicateOperator::exprIfNull), v, v, v)) - .collect(Collectors.toList()); + List< + SerializableFunction< + FunctionName, + org.apache.commons.lang3.tuple.Pair>> + functionsOne = + typeList.stream() + .map(v -> impl((UnaryPredicateOperator::exprIfNull), v, v, v)) + .collect(Collectors.toList()); DefaultFunctionResolver functionResolver = FunctionDSL.define(functionName, functionsOne); return functionResolver; @@ -116,14 +112,16 @@ private static DefaultFunctionResolver nullIf() { List typeList = ExprCoreType.coreTypes(); DefaultFunctionResolver functionResolver = - FunctionDSL.define(functionName, - typeList.stream().map(v -> - impl((UnaryPredicateOperator::exprNullIf), v, v, v)) - .collect(Collectors.toList())); + FunctionDSL.define( + functionName, + typeList.stream() + .map(v -> impl((UnaryPredicateOperator::exprNullIf), v, v, v)) + .collect(Collectors.toList())); return functionResolver; } - /** v2 if v1 is null. + /** + * v2 if v1 is null. * * @param v1 varable 1 * @param v2 varable 2 @@ -133,7 +131,8 @@ public static ExprValue exprIfNull(ExprValue v1, ExprValue v2) { return (v1.isNull() || v1.isMissing()) ? v2 : v1; } - /** return null if v1 equls to v2. + /** + * return null if v1 equls to v2. * * @param v1 varable 1 * @param v2 varable 2 @@ -146,5 +145,4 @@ public static ExprValue exprNullIf(ExprValue v1, ExprValue v2) { public static ExprValue exprIf(ExprValue v1, ExprValue v2, ExprValue v3) { return !v1.isNull() && !v1.isMissing() && LITERAL_TRUE.equals(v1) ? v2 : v3; } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/parse/GrokExpression.java b/core/src/main/java/org/opensearch/sql/expression/parse/GrokExpression.java index 9797832f074..748ce5f559f 100644 --- a/core/src/main/java/org/opensearch/sql/expression/parse/GrokExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/parse/GrokExpression.java @@ -20,9 +20,7 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; import org.opensearch.sql.expression.Expression; -/** - * GrokExpression with grok patterns. - */ +/** GrokExpression with grok patterns. */ @EqualsAndHashCode(callSuper = true) @ToString public class GrokExpression extends ParseExpression { @@ -33,15 +31,14 @@ public class GrokExpression extends ParseExpression { grokCompiler.registerDefaultPatterns(); } - @EqualsAndHashCode.Exclude - private final Grok grok; + @EqualsAndHashCode.Exclude private final Grok grok; /** * GrokExpression. * * @param sourceField source text field - * @param pattern pattern used for parsing - * @param identifier derived field + * @param pattern pattern used for parsing + * @param identifier derived field */ public GrokExpression(Expression sourceField, Expression pattern, Expression identifier) { super("grok", sourceField, pattern, identifier); @@ -69,7 +66,9 @@ ExprValue parseValue(ExprValue value) throws ExpressionEvaluationException { */ public static List getNamedGroupCandidates(String pattern) { Grok grok = grokCompiler.compile(pattern); - return grok.namedGroups.stream().map(grok::getNamedRegexCollectionById) - .filter(group -> !group.equals("UNWANTED")).collect(Collectors.toUnmodifiableList()); + return grok.namedGroups.stream() + .map(grok::getNamedRegexCollectionById) + .filter(group -> !group.equals("UNWANTED")) + .collect(Collectors.toUnmodifiableList()); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/parse/ParseExpression.java b/core/src/main/java/org/opensearch/sql/expression/parse/ParseExpression.java index 8d1ebcce081..6e2456ecc28 100644 --- a/core/src/main/java/org/opensearch/sql/expression/parse/ParseExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/parse/ParseExpression.java @@ -21,29 +21,25 @@ import org.opensearch.sql.expression.env.Environment; import org.opensearch.sql.expression.function.FunctionName; -/** - * ParseExpression. - */ +/** ParseExpression. */ @EqualsAndHashCode(callSuper = false) @ToString public abstract class ParseExpression extends FunctionExpression { - @Getter - protected final Expression sourceField; + @Getter protected final Expression sourceField; protected final Expression pattern; - @Getter - protected final Expression identifier; + @Getter protected final Expression identifier; protected final String identifierStr; /** * ParseExpression. * * @param functionName name of function expression - * @param sourceField source text field - * @param pattern pattern used for parsing - * @param identifier derived field + * @param sourceField source text field + * @param pattern pattern used for parsing + * @param identifier derived field */ - public ParseExpression(String functionName, Expression sourceField, Expression pattern, - Expression identifier) { + public ParseExpression( + String functionName, Expression sourceField, Expression pattern, Expression identifier) { super(FunctionName.of(functionName), ImmutableList.of(sourceField, pattern, identifier)); this.sourceField = sourceField; this.pattern = pattern; diff --git a/core/src/main/java/org/opensearch/sql/expression/parse/PatternsExpression.java b/core/src/main/java/org/opensearch/sql/expression/parse/PatternsExpression.java index 67160dad58f..5b92779c352 100644 --- a/core/src/main/java/org/opensearch/sql/expression/parse/PatternsExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/parse/PatternsExpression.java @@ -17,30 +17,28 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; import org.opensearch.sql.expression.Expression; -/** - * PatternsExpression with regex filter. - */ +/** PatternsExpression with regex filter. */ @EqualsAndHashCode(callSuper = true) @ToString public class PatternsExpression extends ParseExpression { - /** - * Default name of the derived field. - */ + /** Default name of the derived field. */ public static final String DEFAULT_NEW_FIELD = "patterns_field"; - private static final ImmutableSet DEFAULT_IGNORED_CHARS = ImmutableSet.copyOf( - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".chars() - .mapToObj(c -> (char) c).toArray(Character[]::new)); + private static final ImmutableSet DEFAULT_IGNORED_CHARS = + ImmutableSet.copyOf( + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + .chars() + .mapToObj(c -> (char) c) + .toArray(Character[]::new)); private final boolean useCustomPattern; - @EqualsAndHashCode.Exclude - private Pattern pattern; + @EqualsAndHashCode.Exclude private Pattern pattern; /** * PatternsExpression. * * @param sourceField source text field - * @param pattern pattern used for parsing - * @param identifier derived field + * @param pattern pattern used for parsing + * @param identifier derived field */ public PatternsExpression(Expression sourceField, Expression pattern, Expression identifier) { super("patterns", sourceField, pattern, identifier); diff --git a/core/src/main/java/org/opensearch/sql/expression/parse/RegexExpression.java b/core/src/main/java/org/opensearch/sql/expression/parse/RegexExpression.java index f3a3ff0b66c..7514c9df69a 100644 --- a/core/src/main/java/org/opensearch/sql/expression/parse/RegexExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/parse/RegexExpression.java @@ -19,24 +19,20 @@ import org.opensearch.sql.exception.ExpressionEvaluationException; import org.opensearch.sql.expression.Expression; -/** - * RegexExpression with regex and named capture group. - */ +/** RegexExpression with regex and named capture group. */ @EqualsAndHashCode(callSuper = true) @ToString public class RegexExpression extends ParseExpression { private static final Logger log = LogManager.getLogger(RegexExpression.class); private static final Pattern GROUP_PATTERN = Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>"); - @Getter - @EqualsAndHashCode.Exclude - private final Pattern regexPattern; + @Getter @EqualsAndHashCode.Exclude private final Pattern regexPattern; /** * RegexExpression. * * @param sourceField source text field - * @param pattern pattern used for parsing - * @param identifier derived field + * @param pattern pattern used for parsing + * @param identifier derived field */ public RegexExpression(Expression sourceField, Expression pattern, Expression identifier) { super("regex", sourceField, pattern, identifier); diff --git a/core/src/main/java/org/opensearch/sql/expression/span/SpanExpression.java b/core/src/main/java/org/opensearch/sql/expression/span/SpanExpression.java index aff114145e1..a4db50944c4 100644 --- a/core/src/main/java/org/opensearch/sql/expression/span/SpanExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/span/SpanExpression.java @@ -24,9 +24,7 @@ public class SpanExpression implements Expression { private final Expression value; private final SpanUnit unit; - /** - * Construct a span expression by field and span interval expression. - */ + /** Construct a span expression by field and span interval expression. */ public SpanExpression(Expression field, Expression value, SpanUnit unit) { this.field = field; this.value = value; @@ -35,18 +33,15 @@ public SpanExpression(Expression field, Expression value, SpanUnit unit) { @Override public ExprValue valueOf(Environment valueEnv) { - Rounding rounding = Rounding.createRounding(this); //TODO: will integrate with WindowAssigner + Rounding rounding = + Rounding.createRounding(this); // TODO: will integrate with WindowAssigner return rounding.round(field.valueOf(valueEnv)); } /** - * Return type follows the following table. - * FIELD VALUE RETURN_TYPE - * int/long integer int/long (field type) - * int/long double double - * float/double integer float/double (field type) - * float/double double float/double (field type) - * other any field type + * Return type follows the following table. FIELD VALUE RETURN_TYPE int/long integer int/long + * (field type) int/long double double float/double integer float/double (field type) float/double + * double float/double (field type) other any field type */ @Override public ExprType type() { diff --git a/core/src/main/java/org/opensearch/sql/expression/system/SystemFunctions.java b/core/src/main/java/org/opensearch/sql/expression/system/SystemFunctions.java index e12bcd0a581..cf071c4f311 100644 --- a/core/src/main/java/org/opensearch/sql/expression/system/SystemFunctions.java +++ b/core/src/main/java/org/opensearch/sql/expression/system/SystemFunctions.java @@ -24,9 +24,7 @@ @UtilityClass public class SystemFunctions { - /** - * Register TypeOf Operator. - */ + /** Register TypeOf Operator. */ public static void register(BuiltinFunctionRepository repository) { repository.register(typeof()); } @@ -37,19 +35,20 @@ private static FunctionResolver typeof() { @Override public Pair resolve( FunctionSignature unresolvedSignature) { - return Pair.of(unresolvedSignature, + return Pair.of( + unresolvedSignature, (functionProperties, arguments) -> new FunctionExpression(BuiltinFunctionName.TYPEOF.getName(), arguments) { - @Override - public ExprValue valueOf(Environment valueEnv) { - return new ExprStringValue(getArguments().get(0).type().legacyTypeName()); - } + @Override + public ExprValue valueOf(Environment valueEnv) { + return new ExprStringValue(getArguments().get(0).type().legacyTypeName()); + } - @Override - public ExprType type() { - return STRING; - } - }); + @Override + public ExprType type() { + return STRING; + } + }); } @Override diff --git a/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java b/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java index 0bbfb651549..1ba5854596a 100644 --- a/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.text; import static org.opensearch.sql.data.type.ExprCoreType.ARRAY; @@ -35,9 +34,8 @@ import org.opensearch.sql.expression.function.SerializableTriFunction; /** - * The definition of text functions. - * 1) have the clear interface for function define. - * 2) the implementation should rely on ExprValue. + * The definition of text functions. 1) have the clear interface for function define. 2) the + * implementation should rely on ExprValue. */ @UtilityClass public class TextFunction { @@ -70,17 +68,20 @@ public void register(BuiltinFunctionRepository repository) { } /** - * Gets substring starting at given point, for optional given length. - * Form of this function using keywords instead of comma delimited variables is not supported. - * Supports following signatures: + * Gets substring starting at given point, for optional given length. Form of this function using + * keywords instead of comma delimited variables is not supported. Supports following signatures: * (STRING, INTEGER)/(STRING, INTEGER, INTEGER) -> STRING */ private DefaultFunctionResolver substringSubstr(FunctionName functionName) { - return define(functionName, - impl(nullMissingHandling(TextFunction::exprSubstrStart), - STRING, STRING, INTEGER), - impl(nullMissingHandling(TextFunction::exprSubstrStartLength), - STRING, STRING, INTEGER, INTEGER)); + return define( + functionName, + impl(nullMissingHandling(TextFunction::exprSubstrStart), STRING, STRING, INTEGER), + impl( + nullMissingHandling(TextFunction::exprSubstrStartLength), + STRING, + STRING, + INTEGER, + INTEGER)); } private DefaultFunctionResolver substring() { @@ -91,223 +92,235 @@ private DefaultFunctionResolver substr() { return substringSubstr(BuiltinFunctionName.SUBSTR.getName()); } - /** - * Removes leading whitespace from string. - * Supports following signatures: - * STRING -> STRING - */ + /** Removes leading whitespace from string. Supports following signatures: STRING -> STRING */ private DefaultFunctionResolver ltrim() { - return define(BuiltinFunctionName.LTRIM.getName(), - impl(nullMissingHandling((v) -> new ExprStringValue(v.stringValue().stripLeading())), - STRING, STRING)); + return define( + BuiltinFunctionName.LTRIM.getName(), + impl( + nullMissingHandling((v) -> new ExprStringValue(v.stringValue().stripLeading())), + STRING, + STRING)); } - /** - * Removes trailing whitespace from string. - * Supports following signatures: - * STRING -> STRING - */ + /** Removes trailing whitespace from string. Supports following signatures: STRING -> STRING */ private DefaultFunctionResolver rtrim() { - return define(BuiltinFunctionName.RTRIM.getName(), - impl(nullMissingHandling((v) -> new ExprStringValue(v.stringValue().stripTrailing())), - STRING, STRING)); + return define( + BuiltinFunctionName.RTRIM.getName(), + impl( + nullMissingHandling((v) -> new ExprStringValue(v.stringValue().stripTrailing())), + STRING, + STRING)); } /** - * Removes leading and trailing whitespace from string. - * Has option to specify a String to trim instead of whitespace but this is not yet supported. - * Supporting String specification requires finding keywords inside TRIM command. - * Supports following signatures: - * STRING -> STRING + * Removes leading and trailing whitespace from string. Has option to specify a String to trim + * instead of whitespace but this is not yet supported. Supporting String specification requires + * finding keywords inside TRIM command. Supports following signatures: STRING -> STRING */ private DefaultFunctionResolver trim() { - return define(BuiltinFunctionName.TRIM.getName(), - impl(nullMissingHandling((v) -> new ExprStringValue(v.stringValue().trim())), - STRING, STRING)); + return define( + BuiltinFunctionName.TRIM.getName(), + impl( + nullMissingHandling((v) -> new ExprStringValue(v.stringValue().trim())), + STRING, + STRING)); } - /** - * Converts String to lowercase. - * Supports following signatures: - * STRING -> STRING - */ + /** Converts String to lowercase. Supports following signatures: STRING -> STRING */ private DefaultFunctionResolver lower() { - return define(BuiltinFunctionName.LOWER.getName(), - impl(nullMissingHandling((v) -> new ExprStringValue((v.stringValue().toLowerCase()))), - STRING, STRING) - ); + return define( + BuiltinFunctionName.LOWER.getName(), + impl( + nullMissingHandling((v) -> new ExprStringValue((v.stringValue().toLowerCase()))), + STRING, + STRING)); } - /** - * Converts String to uppercase. - * Supports following signatures: - * STRING -> STRING - */ + /** Converts String to uppercase. Supports following signatures: STRING -> STRING */ private DefaultFunctionResolver upper() { - return define(BuiltinFunctionName.UPPER.getName(), - impl(nullMissingHandling((v) -> new ExprStringValue((v.stringValue().toUpperCase()))), - STRING, STRING) - ); + return define( + BuiltinFunctionName.UPPER.getName(), + impl( + nullMissingHandling((v) -> new ExprStringValue((v.stringValue().toUpperCase()))), + STRING, + STRING)); } /** - * Concatenates a list of Strings. - * Supports following signatures: - * (STRING, STRING, ...., STRING) -> STRING + * Concatenates a list of Strings. Supports following signatures: (STRING, STRING, ...., STRING) + * -> STRING */ private DefaultFunctionResolver concat() { FunctionName concatFuncName = BuiltinFunctionName.CONCAT.getName(); - return define(concatFuncName, funcName -> + return define( + concatFuncName, + funcName -> Pair.of( - new FunctionSignature(concatFuncName, Collections.singletonList(ARRAY)), - (funcProp, args) -> new FunctionExpression(funcName, args) { + new FunctionSignature(concatFuncName, Collections.singletonList(ARRAY)), + (funcProp, args) -> + new FunctionExpression(funcName, args) { @Override public ExprValue valueOf(Environment valueEnv) { - List exprValues = args.stream() - .map(arg -> arg.valueOf(valueEnv)).collect(Collectors.toList()); - if (exprValues.stream().anyMatch(ExprValue::isMissing)) { - return ExprValueUtils.missingValue(); - } - if (exprValues.stream().anyMatch(ExprValue::isNull)) { - return ExprValueUtils.nullValue(); - } - return new ExprStringValue(exprValues.stream() - .map(ExprValue::stringValue) - .collect(Collectors.joining())); + List exprValues = + args.stream() + .map(arg -> arg.valueOf(valueEnv)) + .collect(Collectors.toList()); + if (exprValues.stream().anyMatch(ExprValue::isMissing)) { + return ExprValueUtils.missingValue(); + } + if (exprValues.stream().anyMatch(ExprValue::isNull)) { + return ExprValueUtils.nullValue(); + } + return new ExprStringValue( + exprValues.stream() + .map(ExprValue::stringValue) + .collect(Collectors.joining())); } @Override public ExprType type() { return STRING; } - } - )); + })); } /** - * TODO: https://github.com/opendistro-for-elasticsearch/sql/issues/710 - * Extend to accept variable argument amounts. - * Concatenates a list of Strings with a separator string. - * Supports following signatures: - * (STRING, STRING, STRING) -> STRING + * TODO: https://github.com/opendistro-for-elasticsearch/sql/issues/710 Extend to accept variable + * argument amounts. Concatenates a list of Strings with a separator string. Supports following + * signatures: (STRING, STRING, STRING) -> STRING */ private DefaultFunctionResolver concat_ws() { - return define(BuiltinFunctionName.CONCAT_WS.getName(), - impl(nullMissingHandling((sep, str1, str2) -> - new ExprStringValue(str1.stringValue() + sep.stringValue() + str2.stringValue())), - STRING, STRING, STRING, STRING)); + return define( + BuiltinFunctionName.CONCAT_WS.getName(), + impl( + nullMissingHandling( + (sep, str1, str2) -> + new ExprStringValue( + str1.stringValue() + sep.stringValue() + str2.stringValue())), + STRING, + STRING, + STRING, + STRING)); } - /** - * Calculates length of String in bytes. - * Supports following signatures: - * STRING -> INTEGER - */ + /** Calculates length of String in bytes. Supports following signatures: STRING -> INTEGER */ private DefaultFunctionResolver length() { - return define(BuiltinFunctionName.LENGTH.getName(), - impl(nullMissingHandling((str) -> - new ExprIntegerValue(str.stringValue().getBytes().length)), INTEGER, STRING)); + return define( + BuiltinFunctionName.LENGTH.getName(), + impl( + nullMissingHandling((str) -> new ExprIntegerValue(str.stringValue().getBytes().length)), + INTEGER, + STRING)); } /** - * Does String comparison of two Strings and returns Integer value. - * Supports following signatures: + * Does String comparison of two Strings and returns Integer value. Supports following signatures: * (STRING, STRING) -> INTEGER */ private DefaultFunctionResolver strcmp() { - return define(BuiltinFunctionName.STRCMP.getName(), - impl(nullMissingHandling((str1, str2) -> - new ExprIntegerValue(Integer.compare( - str1.stringValue().compareTo(str2.stringValue()), 0))), - INTEGER, STRING, STRING)); + return define( + BuiltinFunctionName.STRCMP.getName(), + impl( + nullMissingHandling( + (str1, str2) -> + new ExprIntegerValue( + Integer.compare(str1.stringValue().compareTo(str2.stringValue()), 0))), + INTEGER, + STRING, + STRING)); } /** * Returns the rightmost len characters from the string str, or NULL if any argument is NULL. - * Supports following signatures: - * (STRING, INTEGER) -> STRING + * Supports following signatures: (STRING, INTEGER) -> STRING */ private DefaultFunctionResolver right() { - return define(BuiltinFunctionName.RIGHT.getName(), - impl(nullMissingHandling(TextFunction::exprRight), STRING, STRING, INTEGER)); + return define( + BuiltinFunctionName.RIGHT.getName(), + impl(nullMissingHandling(TextFunction::exprRight), STRING, STRING, INTEGER)); } /** * Returns the leftmost len characters from the string str, or NULL if any argument is NULL. - * Supports following signature: - * (STRING, INTEGER) -> STRING + * Supports following signature: (STRING, INTEGER) -> STRING */ private DefaultFunctionResolver left() { - return define(BuiltinFunctionName.LEFT.getName(), + return define( + BuiltinFunctionName.LEFT.getName(), impl(nullMissingHandling(TextFunction::exprLeft), STRING, STRING, INTEGER)); } /** - * Returns the numeric value of the leftmost character of the string str. - * Returns 0 if str is the empty string. Returns NULL if str is NULL. - * ASCII() works for 8-bit characters. - * Supports following signature: - * STRING -> INTEGER + * Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the + * empty string. Returns NULL if str is NULL. ASCII() works for 8-bit characters. Supports + * following signature: STRING -> INTEGER */ private DefaultFunctionResolver ascii() { - return define(BuiltinFunctionName.ASCII.getName(), + return define( + BuiltinFunctionName.ASCII.getName(), impl(nullMissingHandling(TextFunction::exprAscii), INTEGER, STRING)); } /** - * LOCATE(substr, str) returns the position of the first occurrence of substring substr - * in string str. LOCATE(substr, str, pos) returns the position of the first occurrence - * of substring substr in string str, starting at position pos. - * Returns 0 if substr is not in str. - * Returns NULL if any argument is NULL. - * Supports following signature: - * (STRING, STRING) -> INTEGER - * (STRING, STRING, INTEGER) -> INTEGER + * LOCATE(substr, str) returns the position of the first occurrence of substring substr in string + * str. LOCATE(substr, str, pos) returns the position of the first occurrence of substring substr + * in string str, starting at position pos. Returns 0 if substr is not in str. Returns NULL if any + * argument is NULL. Supports following signature: (STRING, STRING) -> INTEGER (STRING, STRING, + * INTEGER) -> INTEGER */ private DefaultFunctionResolver locate() { - return define(BuiltinFunctionName.LOCATE.getName(), - impl(nullMissingHandling( - (SerializableBiFunction) - TextFunction::exprLocate), INTEGER, STRING, STRING), - impl(nullMissingHandling( - (SerializableTriFunction) - TextFunction::exprLocate), INTEGER, STRING, STRING, INTEGER)); + return define( + BuiltinFunctionName.LOCATE.getName(), + impl( + nullMissingHandling( + (SerializableBiFunction) TextFunction::exprLocate), + INTEGER, + STRING, + STRING), + impl( + nullMissingHandling( + (SerializableTriFunction) + TextFunction::exprLocate), + INTEGER, + STRING, + STRING, + INTEGER)); } /** * Returns the position of the first occurrence of a substring in a string starting from 1. - * Returns 0 if substring is not in string. - * Returns NULL if any argument is NULL. - * Supports following signature: - * (STRING IN STRING) -> INTEGER + * Returns 0 if substring is not in string. Returns NULL if any argument is NULL. Supports + * following signature: (STRING IN STRING) -> INTEGER */ private DefaultFunctionResolver position() { - return define(BuiltinFunctionName.POSITION.getName(), - impl(nullMissingHandling( - (SerializableBiFunction) - TextFunction::exprLocate), INTEGER, STRING, STRING)); + return define( + BuiltinFunctionName.POSITION.getName(), + impl( + nullMissingHandling( + (SerializableBiFunction) TextFunction::exprLocate), + INTEGER, + STRING, + STRING)); } /** - * REPLACE(str, from_str, to_str) returns the string str with all occurrences of - * the string from_str replaced by the string to_str. - * REPLACE() performs a case-sensitive match when searching for from_str. - * Supports following signature: - * (STRING, STRING, STRING) -> STRING + * REPLACE(str, from_str, to_str) returns the string str with all occurrences of the string + * from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when + * searching for from_str. Supports following signature: (STRING, STRING, STRING) -> STRING */ private DefaultFunctionResolver replace() { - return define(BuiltinFunctionName.REPLACE.getName(), + return define( + BuiltinFunctionName.REPLACE.getName(), impl(nullMissingHandling(TextFunction::exprReplace), STRING, STRING, STRING, STRING)); } /** - * REVERSE(str) returns reversed string of the string supplied as an argument - * Returns NULL if the argument is NULL. - * Supports the following signature: - * (STRING) -> STRING + * REVERSE(str) returns reversed string of the string supplied as an argument Returns NULL if the + * argument is NULL. Supports the following signature: (STRING) -> STRING */ private DefaultFunctionResolver reverse() { - return define(BuiltinFunctionName.REVERSE.getName(), + return define( + BuiltinFunctionName.REVERSE.getName(), impl(nullMissingHandling(TextFunction::exprReverse), STRING, STRING)); } @@ -321,7 +334,7 @@ private static ExprValue exprSubstrStart(ExprValue exprValue, ExprValue start) { } private static ExprValue exprSubstrStartLength( - ExprValue exprValue, ExprValue start, ExprValue length) { + ExprValue exprValue, ExprValue start, ExprValue length) { int startIdx = start.integerValue(); int len = length.integerValue(); if ((startIdx == 0) || (len == 0)) { diff --git a/core/src/main/java/org/opensearch/sql/expression/window/WindowDefinition.java b/core/src/main/java/org/opensearch/sql/expression/window/WindowDefinition.java index 24751633de9..2030ce80625 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/WindowDefinition.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/WindowDefinition.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window; import static org.opensearch.sql.ast.tree.Sort.SortOption; @@ -16,9 +15,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.opensearch.sql.expression.Expression; -/** - * Window definition that consists of partition and sort by information for a window. - */ +/** Window definition that consists of partition and sort by information for a window. */ @Data public class WindowDefinition { @@ -27,7 +24,8 @@ public class WindowDefinition { /** * Return all items in partition by and sort list. - * @return all sort items + * + * @return all sort items */ public List> getAllSortItems() { List> allSorts = new ArrayList<>(); @@ -35,5 +33,4 @@ public List> getAllSortItems() { allSorts.addAll(sortList); return allSorts; } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctionExpression.java b/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctionExpression.java index a15919bf03b..f323494307b 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctionExpression.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctionExpression.java @@ -3,27 +3,22 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window; import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.window.frame.WindowFrame; -/** - * Window function abstraction. - */ +/** Window function abstraction. */ public interface WindowFunctionExpression extends Expression { /** - * Create specific window frame based on window definition and what's current window function. - * For now two types of cumulative window frame is returned: - * 1. Ranking window functions: ignore frame definition and always operates on - * previous and current row. - * 2. Aggregate window functions: frame partition into peers and sliding window is not supported. + * Create specific window frame based on window definition and what's current window function. For + * now two types of cumulative window frame is returned: 1. Ranking window functions: ignore frame + * definition and always operates on previous and current row. 2. Aggregate window functions: + * frame partition into peers and sliding window is not supported. * * @param definition window definition - * @return window frame + * @return window frame */ WindowFrame createWindowFrame(WindowDefinition definition); - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctions.java b/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctions.java index 9a9e0c4c863..3df59c52c08 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctions.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/WindowFunctions.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window; import static java.util.Collections.emptyList; @@ -22,16 +21,14 @@ import org.opensearch.sql.expression.window.ranking.RankingWindowFunction; import org.opensearch.sql.expression.window.ranking.RowNumberFunction; -/** - * Window functions that register all window functions in function repository. - */ +/** Window functions that register all window functions in function repository. */ @UtilityClass public class WindowFunctions { /** * Register all window functions to function repository. * - * @param repository function repository + * @param repository function repository */ public void register(BuiltinFunctionRepository repository) { repository.register(rowNumber()); @@ -51,11 +48,11 @@ private DefaultFunctionResolver denseRank() { return rankingFunction(BuiltinFunctionName.DENSE_RANK.getName(), DenseRankFunction::new); } - private DefaultFunctionResolver rankingFunction(FunctionName functionName, - Supplier constructor) { + private DefaultFunctionResolver rankingFunction( + FunctionName functionName, Supplier constructor) { FunctionSignature functionSignature = new FunctionSignature(functionName, emptyList()); FunctionBuilder functionBuilder = (functionProperties, arguments) -> constructor.get(); - return new DefaultFunctionResolver(functionName, - ImmutableMap.of(functionSignature, functionBuilder)); + return new DefaultFunctionResolver( + functionName, ImmutableMap.of(functionSignature, functionBuilder)); } } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunction.java b/core/src/main/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunction.java index 604f65e6ffc..63922ac3fdf 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.aggregation; import java.util.List; @@ -21,9 +20,7 @@ import org.opensearch.sql.expression.window.frame.PeerRowsWindowFrame; import org.opensearch.sql.expression.window.frame.WindowFrame; -/** - * Aggregate function adapter that adapts Aggregator for window operator use. - */ +/** Aggregate function adapter that adapts Aggregator for window operator use. */ @EqualsAndHashCode @RequiredArgsConstructor public class AggregateWindowFunction implements WindowFunctionExpression { @@ -64,5 +61,4 @@ public T accept(ExpressionNodeVisitor visitor, C context) { public String toString() { return aggregator.toString(); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/frame/CurrentRowWindowFrame.java b/core/src/main/java/org/opensearch/sql/expression/window/frame/CurrentRowWindowFrame.java index 06b19a14886..359486a4ef5 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/frame/CurrentRowWindowFrame.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/frame/CurrentRowWindowFrame.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.frame; import com.google.common.collect.PeekingIterator; @@ -21,18 +20,17 @@ import org.opensearch.sql.expression.window.WindowDefinition; /** - * Conceptually, cumulative window frame should hold all seen rows till next partition. - * This class is actually an optimized version that only hold previous and current row. This is - * efficient and sufficient for ranking and aggregate window function support for now, though need - * to add "real" cumulative frame implementation in future as needed. + * Conceptually, cumulative window frame should hold all seen rows till next partition. This class + * is actually an optimized version that only hold previous and current row. This is efficient and + * sufficient for ranking and aggregate window function support for now, though need to add "real" + * cumulative frame implementation in future as needed. */ @EqualsAndHashCode @RequiredArgsConstructor @ToString public class CurrentRowWindowFrame implements WindowFrame { - @Getter - private final WindowDefinition windowDefinition; + @Getter private final WindowDefinition windowDefinition; private ExprValue previous; private ExprValue current; @@ -67,14 +65,12 @@ public ExprValue previous() { private List resolve(List expressions, ExprValue row) { Environment valueEnv = row.bindingTuples(); - return expressions.stream() - .map(expr -> expr.valueOf(valueEnv)) - .collect(Collectors.toList()); + return expressions.stream().map(expr -> expr.valueOf(valueEnv)).collect(Collectors.toList()); } /** - * Current row window frame won't pre-fetch any row ahead. - * So always return false as nothing "cached" in frame. + * Current row window frame won't pre-fetch any row ahead. So always return false as nothing + * "cached" in frame. */ @Override public boolean hasNext() { @@ -85,5 +81,4 @@ public boolean hasNext() { public List next() { return Collections.emptyList(); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrame.java b/core/src/main/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrame.java index a3e8de40c18..0ec2042b1e3 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrame.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrame.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.frame; import com.google.common.collect.PeekingIterator; @@ -19,9 +18,9 @@ import org.opensearch.sql.expression.window.WindowDefinition; /** - * Window frame that only keep peers (tuples with same value of fields specified in sort list - * in window definition). See PeerWindowFrameTest for details about how this window frame - * interacts with window operator and window function. + * Window frame that only keep peers (tuples with same value of fields specified in sort list in + * window definition). See PeerWindowFrameTest for details about how this window frame interacts + * with window operator and window function. */ @RequiredArgsConstructor public class PeerRowsWindowFrame implements WindowFrame { @@ -29,33 +28,26 @@ public class PeerRowsWindowFrame implements WindowFrame { private final WindowDefinition windowDefinition; /** - * All peer rows (peer means rows in a partition that share same sort key - * based on sort list in window definition. + * All peer rows (peer means rows in a partition that share same sort key based on sort list in + * window definition. */ private final List peers = new ArrayList<>(); - /** - * Which row in the peer is currently being enriched by window function. - */ + /** Which row in the peer is currently being enriched by window function. */ private int position; - /** - * Does row at current position represents a new partition. - */ + /** Does row at current position represents a new partition. */ private boolean isNewPartition = true; - /** - * If any more pre-fetched rows not returned to window operator yet. - */ + /** If any more pre-fetched rows not returned to window operator yet. */ @Override public boolean hasNext() { return position < peers.size(); } /** - * Move position and clear new partition flag. - * Note that because all peer rows have same result from window function, - * this is only returned at first time to change window function state. + * Move position and clear new partition flag. Note that because all peer rows have same result + * from window function, this is only returned at first time to change window function state. * Afterwards, empty list is returned to avoid changes until next peer loaded. * * @return all rows for the peer @@ -70,8 +62,9 @@ public List next() { } /** - * Current row at the position. Because rows are pre-fetched here, - * window operator needs to get them from here too. + * Current row at the position. Because rows are pre-fetched here, window operator needs to get + * them from here too. + * * @return row at current position that being enriched by window function */ @Override @@ -82,11 +75,10 @@ public ExprValue current() { /** * Preload all peer rows if last peer rows done. Note that when no more data in peeking iterator, * there must be rows in frame (hasNext()=true), so no need to check it.hasNext() in this method. - * Load until: - * 1. Different peer found (row with different sort key) - * 2. Or new partition (row with different partition key) - * 3. Or no more rows - * @param it rows iterator + * Load until: 1. Different peer found (row with different sort key) 2. Or new partition (row with + * different partition key) 3. Or no more rows + * + * @param it rows iterator */ @Override public void load(PeekingIterator it) { @@ -118,10 +110,7 @@ public boolean isNewPartition() { private boolean isPeer(ExprValue next) { List sortFields = - windowDefinition.getSortList() - .stream() - .map(Pair::getRight) - .collect(Collectors.toList()); + windowDefinition.getSortList().stream().map(Pair::getRight).collect(Collectors.toList()); ExprValue last = peers.get(peers.size() - 1); return resolve(sortFields, last).equals(resolve(sortFields, next)); @@ -139,9 +128,6 @@ private boolean isSamePartition(ExprValue next) { private List resolve(List expressions, ExprValue row) { Environment valueEnv = row.bindingTuples(); - return expressions.stream() - .map(expr -> expr.valueOf(valueEnv)) - .collect(Collectors.toList()); + return expressions.stream().map(expr -> expr.valueOf(valueEnv)).collect(Collectors.toList()); } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/frame/WindowFrame.java b/core/src/main/java/org/opensearch/sql/expression/window/frame/WindowFrame.java index 323656547fa..85bc937969f 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/frame/WindowFrame.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/frame/WindowFrame.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.frame; import com.google.common.collect.PeekingIterator; @@ -14,13 +13,12 @@ import org.opensearch.sql.expression.env.Environment; /** - * Window frame that represents a subset of a window which is all data accessible to - * the window function when calculation. Basically there are 3 types of window frame: - * 1) Entire window frame that holds all data of the window - * 2) Cumulative window frame that accumulates one row by another - * 3) Sliding window frame that maintains a sliding window of fixed size - * Note that which type of window frame is used is determined by both window function itself - * and frame definition in a window definition. + * Window frame that represents a subset of a window which is all data accessible to the window + * function when calculation. Basically there are 3 types of window frame: 1) Entire window frame + * that holds all data of the window 2) Cumulative window frame that accumulates one row by another + * 3) Sliding window frame that maintains a sliding window of fixed size Note that which type of + * window frame is used is determined by both window function itself and frame definition in a + * window definition. */ public interface WindowFrame extends Environment, Iterator> { @@ -31,20 +29,22 @@ default ExprValue resolve(Expression var) { /** * Check is current row the beginning of a new partition according to window definition. - * @return true if a new partition begins here, otherwise false. + * + * @return true if a new partition begins here, otherwise false. */ boolean isNewPartition(); /** * Load one or more rows as window function calculation needed. - * @param iterator peeking iterator that can peek next element without moving iterator + * + * @param iterator peeking iterator that can peek next element without moving iterator */ void load(PeekingIterator iterator); /** * Get current data row for giving window operator chance to get rows preloaded into frame. + * * @return data row */ ExprValue current(); - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/ranking/DenseRankFunction.java b/core/src/main/java/org/opensearch/sql/expression/window/ranking/DenseRankFunction.java index ba6e88d98d3..87506ef63ee 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/ranking/DenseRankFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/ranking/DenseRankFunction.java @@ -3,15 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.ranking; import org.opensearch.sql.expression.function.BuiltinFunctionName; import org.opensearch.sql.expression.window.frame.CurrentRowWindowFrame; /** - * Dense rank window function that assigns a rank number to each row similarly as - * rank function. The difference is there is no gap between rank number assigned. + * Dense rank window function that assigns a rank number to each row similarly as rank function. The + * difference is there is no gap between rank number assigned. */ public class DenseRankFunction extends RankingWindowFunction { @@ -30,5 +29,4 @@ protected int rank(CurrentRowWindowFrame frame) { } return rank; } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankFunction.java b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankFunction.java index c1f33e61377..f72a28cd9af 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankFunction.java @@ -3,22 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.ranking; import org.opensearch.sql.expression.function.BuiltinFunctionName; import org.opensearch.sql.expression.window.frame.CurrentRowWindowFrame; /** - * Rank window function that assigns a rank number to each row based on sort items - * defined in window definition. Use same rank number if sort item values same on - * previous and current row. + * Rank window function that assigns a rank number to each row based on sort items defined in window + * definition. Use same rank number if sort item values same on previous and current row. */ public class RankFunction extends RankingWindowFunction { - /** - * Total number of rows have seen in current partition. - */ + /** Total number of rows have seen in current partition. */ private int total; public RankFunction() { @@ -38,5 +34,4 @@ protected int rank(CurrentRowWindowFrame frame) { } return rank; } - } diff --git a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunction.java b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunction.java index 07a4b42dbd9..c119629cdad 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.ranking; import static java.util.Collections.emptyList; @@ -30,11 +29,9 @@ * such as same return type (integer), same argument list (no arg). */ public abstract class RankingWindowFunction extends FunctionExpression - implements WindowFunctionExpression { + implements WindowFunctionExpression { - /** - * Current rank number assigned. - */ + /** Current rank number assigned. */ protected int rank; public RankingWindowFunction(FunctionName functionName) { @@ -58,26 +55,27 @@ public ExprValue valueOf(Environment valueEnv) { /** * Rank logic that sub-class needs to implement. - * @param frame window frame - * @return rank number + * + * @param frame window frame + * @return rank number */ protected abstract int rank(CurrentRowWindowFrame frame); /** * Check sort field to see if current value is different from previous. - * @param frame window frame - * @return true if different, false if same or no sort list defined + * + * @param frame window frame + * @return true if different, false if same or no sort list defined */ protected boolean isSortFieldValueDifferent(CurrentRowWindowFrame frame) { if (isSortItemsNotDefined(frame)) { return false; } - List sortItems = frame.getWindowDefinition() - .getSortList() - .stream() - .map(Pair::getRight) - .collect(Collectors.toList()); + List sortItems = + frame.getWindowDefinition().getSortList().stream() + .map(Pair::getRight) + .collect(Collectors.toList()); List previous = resolve(frame, sortItems, frame.previous()); List current = resolve(frame, sortItems, frame.current()); @@ -90,9 +88,7 @@ private boolean isSortItemsNotDefined(CurrentRowWindowFrame frame) { private List resolve(WindowFrame frame, List expressions, ExprValue row) { BindingTuple valueEnv = row.bindingTuples(); - return expressions.stream() - .map(expr -> expr.valueOf(valueEnv)) - .collect(Collectors.toList()); + return expressions.stream().map(expr -> expr.valueOf(valueEnv)).collect(Collectors.toList()); } @Override diff --git a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RowNumberFunction.java b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RowNumberFunction.java index 067dfa569d2..90bb2ed8ff9 100644 --- a/core/src/main/java/org/opensearch/sql/expression/window/ranking/RowNumberFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/window/ranking/RowNumberFunction.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.ranking; import org.opensearch.sql.expression.function.BuiltinFunctionName; @@ -25,5 +24,4 @@ protected int rank(CurrentRowWindowFrame frame) { } return rank++; } - } diff --git a/core/src/test/java/org/opensearch/sql/ast/expression/CastTest.java b/core/src/test/java/org/opensearch/sql/ast/expression/CastTest.java index d9386ab122a..741db07eb30 100644 --- a/core/src/test/java/org/opensearch/sql/ast/expression/CastTest.java +++ b/core/src/test/java/org/opensearch/sql/ast/expression/CastTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -23,5 +22,4 @@ void cast_to_int_and_integer_should_convert_to_same_function_impl() { new Cast(booleanLiteral(true), stringLiteral("INT")).convertFunctionName(), new Cast(booleanLiteral(true), stringLiteral("INTEGER")).convertFunctionName()); } - } diff --git a/core/src/test/java/org/opensearch/sql/ast/expression/QualifiedNameTest.java b/core/src/test/java/org/opensearch/sql/ast/expression/QualifiedNameTest.java index b0ab66bc0ee..c211f36ba77 100644 --- a/core/src/test/java/org/opensearch/sql/ast/expression/QualifiedNameTest.java +++ b/core/src/test/java/org/opensearch/sql/ast/expression/QualifiedNameTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.ast.expression; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -36,5 +35,4 @@ void should_return_empty_if_only_single_part() { assertFalse(name.first().isPresent()); assertFalse(name.getPrefix().isPresent()); } - } diff --git a/core/src/test/java/org/opensearch/sql/common/utils/StringUtilsTest.java b/core/src/test/java/org/opensearch/sql/common/utils/StringUtilsTest.java index 06ea404fc29..157b60cbc77 100644 --- a/core/src/test/java/org/opensearch/sql/common/utils/StringUtilsTest.java +++ b/core/src/test/java/org/opensearch/sql/common/utils/StringUtilsTest.java @@ -42,7 +42,5 @@ void unquoteTest() { assertEquals("don't", unquoteText("'don't'")); assertEquals("hello`", unquoteText("`hello``")); assertEquals("don\"t", unquoteText("\"don\"t\"")); - } - } diff --git a/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java b/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java index 7d438c870d6..897347f22d1 100644 --- a/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/ExplainTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.executor; import static java.util.Collections.emptyList; @@ -68,36 +67,30 @@ void can_explain_project_filter_table_scan() { DSL.equal(ref("balance", INTEGER), literal(10000)), DSL.greater(ref("age", INTEGER), literal(30))); NamedExpression[] projectList = { - named("full_name", ref("full_name", STRING), "name"), - named("age", ref("age", INTEGER)) + named("full_name", ref("full_name", STRING), "name"), named("age", ref("age", INTEGER)) }; - PhysicalPlan plan = - project( - filter( - tableScan, - filterExpr), - projectList); + PhysicalPlan plan = project(filter(tableScan, filterExpr), projectList); assertEquals( new ExplainResponse( new ExplainResponseNode( "ProjectOperator", Map.of("fields", "[name, age]"), - singletonList(new ExplainResponseNode( - "FilterOperator", - Map.of("conditions", "and(=(balance, 10000), >(age, 30))"), - singletonList(tableScan.explainNode()))))), + singletonList( + new ExplainResponseNode( + "FilterOperator", + Map.of("conditions", "and(=(balance, 10000), >(age, 30))"), + singletonList(tableScan.explainNode()))))), explain.apply(plan)); } @Test void can_explain_aggregations() { List aggExprs = List.of(ref("balance", DOUBLE)); - List aggList = List.of( - named("avg(balance)", DSL.avg(aggExprs.toArray(new Expression[0])))); - List groupByList = List.of( - named("state", ref("state", STRING))); + List aggList = + List.of(named("avg(balance)", DSL.avg(aggExprs.toArray(new Expression[0])))); + List groupByList = List.of(named("state", ref("state", STRING))); PhysicalPlan plan = agg(new FakeTableScan(), aggList, groupByList); assertEquals( @@ -120,11 +113,7 @@ void can_explain_rare_top_n() { new ExplainResponse( new ExplainResponseNode( "RareTopNOperator", - Map.of( - "commandType", TOP, - "noOfResults", 10, - "fields", "[state]", - "groupBy", "[]"), + Map.of("commandType", TOP, "noOfResults", 10, "fields", "[state]", "groupBy", "[]"), singletonList(tableScan.explainNode()))), explain.apply(plan)); } @@ -132,22 +121,27 @@ void can_explain_rare_top_n() { @Test void can_explain_window() { List partitionByList = List.of(DSL.ref("state", STRING)); - List> sortList = List.of( - ImmutablePair.of(DEFAULT_ASC, ref("age", INTEGER))); + List> sortList = + List.of(ImmutablePair.of(DEFAULT_ASC, ref("age", INTEGER))); - PhysicalPlan plan = window(tableScan, named(DSL.rank()), - new WindowDefinition(partitionByList, sortList)); + PhysicalPlan plan = + window(tableScan, named(DSL.rank()), new WindowDefinition(partitionByList, sortList)); assertEquals( new ExplainResponse( new ExplainResponseNode( "WindowOperator", Map.of( - "function", "rank()", - "definition", Map.of( - "partitionBy", "[state]", - "sortList", Map.of( - "age", Map.of( + "function", + "rank()", + "definition", + Map.of( + "partitionBy", + "[state]", + "sortList", + Map.of( + "age", + Map.of( "sortOrder", "ASC", "nullOrder", "NULL_FIRST")))), singletonList(tableScan.explainNode()))), @@ -157,60 +151,61 @@ void can_explain_window() { @Test void can_explain_other_operators() { ReferenceExpression[] removeList = {ref("state", STRING)}; - Map renameMapping = Map.of( - ref("state", STRING), ref("s", STRING)); - Pair evalExprs = ImmutablePair.of( - ref("age", INTEGER), DSL.add(ref("age", INTEGER), literal(2))); + Map renameMapping = + Map.of(ref("state", STRING), ref("s", STRING)); + Pair evalExprs = + ImmutablePair.of(ref("age", INTEGER), DSL.add(ref("age", INTEGER), literal(2))); Expression[] dedupeList = {ref("age", INTEGER)}; - Pair sortList = ImmutablePair.of( - DEFAULT_ASC, ref("age", INTEGER)); + Pair sortList = ImmutablePair.of(DEFAULT_ASC, ref("age", INTEGER)); List values = List.of(literal("WA"), literal(30)); PhysicalPlan plan = remove( rename( - eval( - dedupe( - sort( - values(values), - sortList), - dedupeList), - evalExprs), - renameMapping), - removeList); + eval(dedupe(sort(values(values), sortList), dedupeList), evalExprs), renameMapping), + removeList); assertEquals( new ExplainResponse( new ExplainResponseNode( "RemoveOperator", Map.of("removeList", "[state]"), - singletonList(new ExplainResponseNode( - "RenameOperator", - Map.of("mapping", Map.of("state", "s")), - singletonList(new ExplainResponseNode( - "EvalOperator", - Map.of("expressions", Map.of("age", "+(age, 2)")), - singletonList(new ExplainResponseNode( - "DedupeOperator", - Map.of( - "dedupeList", "[age]", - "allowedDuplication", 1, - "keepEmpty", false, - "consecutive", false), - singletonList(new ExplainResponseNode( - "SortOperator", - Map.of( - "sortList", Map.of( - "age", Map.of( - "sortOrder", "ASC", - "nullOrder", "NULL_FIRST"))), - singletonList(new ExplainResponseNode( - "ValuesOperator", - Map.of("values", List.of(values)), - emptyList()))))))))))) - ), - explain.apply(plan) - ); + singletonList( + new ExplainResponseNode( + "RenameOperator", + Map.of("mapping", Map.of("state", "s")), + singletonList( + new ExplainResponseNode( + "EvalOperator", + Map.of("expressions", Map.of("age", "+(age, 2)")), + singletonList( + new ExplainResponseNode( + "DedupeOperator", + Map.of( + "dedupeList", + "[age]", + "allowedDuplication", + 1, + "keepEmpty", + false, + "consecutive", + false), + singletonList( + new ExplainResponseNode( + "SortOperator", + Map.of( + "sortList", + Map.of( + "age", + Map.of( + "sortOrder", "ASC", + "nullOrder", "NULL_FIRST"))), + singletonList( + new ExplainResponseNode( + "ValuesOperator", + Map.of("values", List.of(values)), + emptyList())))))))))))), + explain.apply(plan)); } @Test @@ -222,15 +217,13 @@ void can_explain_limit() { "LimitOperator", Map.of("limit", 10, "offset", 5), singletonList(tableScan.explainNode()))), - explain.apply(plan) - ); + explain.apply(plan)); } @Test void can_explain_nested() { Set nestedOperatorArgs = Set.of("message.info", "message"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.info")); + Map> groupedFieldsByPath = Map.of("message", List.of("message.info")); PhysicalPlan plan = nested(tableScan, nestedOperatorArgs, groupedFieldsByPath); assertEquals( @@ -239,8 +232,7 @@ void can_explain_nested() { "NestedOperator", Map.of("nested", Set.of("message.info", "message")), singletonList(tableScan.explainNode()))), - explain.apply(plan) - ); + explain.apply(plan)); } private static class FakeTableScan extends TableScanOperator { @@ -262,14 +254,11 @@ public String toString() { /** Used to ignore table scan which is duplicate but required for each operator test. */ public ExplainResponseNode explainNode() { return new ExplainResponseNode( - "FakeTableScan", - Map.of("request", "Fake DSL request"), - emptyList()); + "FakeTableScan", Map.of("request", "Fake DSL request"), emptyList()); } public String explain() { return "explain"; } } - } diff --git a/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java b/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java index 1510b304e6e..f6b66b4e77d 100644 --- a/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java @@ -37,73 +37,52 @@ class QueryServiceTest { private QueryService queryService; - @Mock - private ExecutionEngine executionEngine; + @Mock private ExecutionEngine executionEngine; - @Mock - private Analyzer analyzer; + @Mock private Analyzer analyzer; - @Mock - private Planner planner; + @Mock private Planner planner; - @Mock - private UnresolvedPlan ast; + @Mock private UnresolvedPlan ast; - @Mock - private LogicalPlan logicalPlan; + @Mock private LogicalPlan logicalPlan; - @Mock - private PhysicalPlan plan; + @Mock private PhysicalPlan plan; - @Mock - private ExecutionEngine.Schema schema; + @Mock private ExecutionEngine.Schema schema; - @Mock - private PlanContext planContext; + @Mock private PlanContext planContext; - @Mock - private Split split; + @Mock private Split split; @Test public void executeWithoutContext() { - queryService() - .executeSuccess() - .handledByOnResponse(); + queryService().executeSuccess().handledByOnResponse(); } @Test public void executeWithContext() { - queryService() - .executeSuccess(split) - .handledByOnResponse(); + queryService().executeSuccess(split).handledByOnResponse(); } @Test public void testExplainShouldPass() { - queryService() - .explainSuccess() - .handledByExplainOnResponse(); + queryService().explainSuccess().handledByExplainOnResponse(); } @Test public void testExecuteWithExceptionShouldBeCaughtByHandler() { - queryService() - .executeFail() - .handledByOnFailure(); + queryService().executeFail().handledByOnFailure(); } @Test public void explainWithIllegalQueryShouldBeCaughtByHandler() { - queryService() - .explainFail() - .handledByExplainOnFailure(); + queryService().explainFail().handledByExplainOnFailure(); } @Test public void analyzeExceptionShouldBeCached() { - queryService() - .analyzeFail() - .handledByOnFailure(); + queryService().analyzeFail().handledByOnFailure(); } Helper queryService() { @@ -130,13 +109,14 @@ Helper executeSuccess() { Helper executeSuccess(Split split) { this.split = Optional.ofNullable(split); doAnswer( - invocation -> { - ResponseListener listener = invocation.getArgument(2); - listener.onResponse( - new ExecutionEngine.QueryResponse(schema, Collections.emptyList(), - Cursor.None)); - return null; - }) + invocation -> { + ResponseListener listener = + invocation.getArgument(2); + listener.onResponse( + new ExecutionEngine.QueryResponse( + schema, Collections.emptyList(), Cursor.None)); + return null; + }) .when(executionEngine) .execute(any(), any(), any()); lenient().when(planContext.getSplit()).thenReturn(this.split); @@ -145,9 +125,7 @@ Helper executeSuccess(Split split) { } Helper analyzeFail() { - doThrow(new IllegalStateException("analyze exception")) - .when(analyzer) - .analyze(any(), any()); + doThrow(new IllegalStateException("analyze exception")).when(analyzer).analyze(any(), any()); return this; } @@ -162,14 +140,14 @@ Helper executeFail() { Helper explainSuccess() { doAnswer( - invocation -> { - ResponseListener listener = - invocation.getArgument(1); - listener.onResponse( - new ExecutionEngine.ExplainResponse( - new ExecutionEngine.ExplainResponseNode("test"))); - return null; - }) + invocation -> { + ResponseListener listener = + invocation.getArgument(1); + listener.onResponse( + new ExecutionEngine.ExplainResponse( + new ExecutionEngine.ExplainResponseNode("test"))); + return null; + }) .when(executionEngine) .explain(any(), any()); @@ -184,36 +162,37 @@ Helper explainFail() { return this; } - void handledByOnResponse() { - ResponseListener responseListener = new ResponseListener<>() { - @Override - public void onResponse(ExecutionEngine.QueryResponse pplQueryResponse) { - assertNotNull(pplQueryResponse); - } - - @Override - public void onFailure(Exception e) { - fail(); - } - }; + ResponseListener responseListener = + new ResponseListener<>() { + @Override + public void onResponse(ExecutionEngine.QueryResponse pplQueryResponse) { + assertNotNull(pplQueryResponse); + } + + @Override + public void onFailure(Exception e) { + fail(); + } + }; split.ifPresentOrElse( split -> queryService.executePlan(logicalPlan, planContext, responseListener), () -> queryService.execute(ast, responseListener)); } void handledByOnFailure() { - ResponseListener responseListener = new ResponseListener<>() { - @Override - public void onResponse(ExecutionEngine.QueryResponse pplQueryResponse) { - fail(); - } - - @Override - public void onFailure(Exception e) { - assertTrue(e instanceof IllegalStateException); - } - }; + ResponseListener responseListener = + new ResponseListener<>() { + @Override + public void onResponse(ExecutionEngine.QueryResponse pplQueryResponse) { + fail(); + } + + @Override + public void onFailure(Exception e) { + assertTrue(e instanceof IllegalStateException); + } + }; split.ifPresentOrElse( split -> queryService.executePlan(logicalPlan, planContext, responseListener), () -> queryService.execute(ast, responseListener)); @@ -250,6 +229,5 @@ public void onFailure(Exception e) { } }); } - } } diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/CommandPlanTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/CommandPlanTest.java index aa300cb0da6..aff5a0d8673 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/CommandPlanTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/CommandPlanTest.java @@ -47,8 +47,7 @@ public void execute_without_error() { public void execute_with_error() { QueryService qs = mock(QueryService.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); ResponseListener listener = mock(ResponseListener.class); - doThrow(new RuntimeException()) - .when(qs).executePlan(any(LogicalPlan.class), any(), any()); + doThrow(new RuntimeException()).when(qs).executePlan(any(LogicalPlan.class), any(), any()); new CommandPlan(QueryId.queryId(), mock(UnresolvedPlan.class), qs, listener).execute(); @@ -62,9 +61,12 @@ public void explain_not_supported() { ResponseListener listener = mock(ResponseListener.class); ResponseListener explainListener = mock(ResponseListener.class); - var exception = assertThrows(Throwable.class, () -> - new CommandPlan(QueryId.queryId(), mock(UnresolvedPlan.class), qs, listener) - .explain(explainListener)); + var exception = + assertThrows( + Throwable.class, + () -> + new CommandPlan(QueryId.queryId(), mock(UnresolvedPlan.class), qs, listener) + .explain(explainListener)); assertEquals("CommandPlan does not support explain", exception.getMessage()); verify(listener, never()).onResponse(any()); diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/ExplainPlanTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/ExplainPlanTest.java index 54b4f24db0f..cdb7f9dcb81 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/ExplainPlanTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/ExplainPlanTest.java @@ -25,14 +25,11 @@ @ExtendWith(MockitoExtension.class) public class ExplainPlanTest { - @Mock - private QueryId queryId; + @Mock private QueryId queryId; - @Mock - private QueryPlan queryPlan; + @Mock private QueryPlan queryPlan; - @Mock - private ResponseListener explainListener; + @Mock private ResponseListener explainListener; @Test public void execute() { diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/IntervalTriggerExecutionTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/IntervalTriggerExecutionTest.java index e0638ba88f1..9eb99d37e3c 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/IntervalTriggerExecutionTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/IntervalTriggerExecutionTest.java @@ -18,16 +18,12 @@ public class IntervalTriggerExecutionTest { @Test void executeTaskWithInterval() { - triggerTask(2) - .taskRun(1) - .aroundInterval(); + triggerTask(2).taskRun(1).aroundInterval(); } @Test void continueExecuteIfTaskRunningLongerThanInterval() { - triggerTask(1) - .taskRun(2) - .aroundTaskRuntime(); + triggerTask(1).taskRun(2).aroundTaskRuntime(); } Helper triggerTask(long interval) { diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanFactoryTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanFactoryTest.java index 2d346e4c2a3..5aed9acc634 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanFactoryTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanFactoryTest.java @@ -41,20 +41,15 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class QueryPlanFactoryTest { - @Mock - private UnresolvedPlan plan; + @Mock private UnresolvedPlan plan; - @Mock - private QueryService queryService; + @Mock private QueryService queryService; - @Mock - private ResponseListener queryListener; + @Mock private ResponseListener queryListener; - @Mock - private ResponseListener explainListener; + @Mock private ResponseListener explainListener; - @Mock - private ExecutionEngine.QueryResponse queryResponse; + @Mock private ExecutionEngine.QueryResponse queryResponse; private QueryPlanFactory factory; @@ -81,14 +76,11 @@ public void create_from_explain_should_success() { @Test public void create_from_cursor_should_success() { - AbstractPlan queryExecution = factory.create("", false, - queryListener, explainListener); - AbstractPlan explainExecution = factory.create("", true, - queryListener, explainListener); + AbstractPlan queryExecution = factory.create("", false, queryListener, explainListener); + AbstractPlan explainExecution = factory.create("", true, queryListener, explainListener); assertAll( () -> assertTrue(queryExecution instanceof QueryPlan), - () -> assertTrue(explainExecution instanceof ExplainPlan) - ); + () -> assertTrue(explainExecution instanceof ExplainPlan)); } @Test @@ -96,8 +88,9 @@ public void create_from_query_without_query_listener_should_throw_exception() { Statement query = new Query(plan, 0); IllegalArgumentException exception = - assertThrows(IllegalArgumentException.class, () -> factory.create( - query, Optional.empty(), Optional.empty())); + assertThrows( + IllegalArgumentException.class, + () -> factory.create(query, Optional.empty(), Optional.empty())); assertEquals("[BUG] query listener must be not null", exception.getMessage()); } @@ -106,8 +99,9 @@ public void create_from_explain_without_explain_listener_should_throw_exception( Statement query = new Explain(new Query(plan, 0)); IllegalArgumentException exception = - assertThrows(IllegalArgumentException.class, () -> factory.create( - query, Optional.empty(), Optional.empty())); + assertThrows( + IllegalArgumentException.class, + () -> factory.create(query, Optional.empty(), Optional.empty())); assertEquals("[BUG] explain listener must be not null", exception.getMessage()); } @@ -143,9 +137,9 @@ public void create_query_with_fetch_size_which_cannot_be_paged() { when(plan.accept(any(CanPaginateVisitor.class), any())).thenReturn(Boolean.FALSE); factory = new QueryPlanFactory(queryService); Statement query = new Query(plan, 10); - assertThrows(UnsupportedCursorRequestException.class, - () -> factory.create(query, - Optional.of(queryListener), Optional.empty())); + assertThrows( + UnsupportedCursorRequestException.class, + () -> factory.create(query, Optional.of(queryListener), Optional.empty())); } @Test diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanTest.java index a0a98e2be72..57ff5c70e9a 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/QueryPlanTest.java @@ -34,20 +34,15 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class QueryPlanTest { - @Mock - private QueryId queryId; + @Mock private QueryId queryId; - @Mock - private UnresolvedPlan plan; + @Mock private UnresolvedPlan plan; - @Mock - private QueryService queryService; + @Mock private QueryService queryService; - @Mock - private ResponseListener explainListener; + @Mock private ResponseListener explainListener; - @Mock - private ResponseListener queryListener; + @Mock private ResponseListener queryListener; @Test public void execute_no_page_size() { @@ -67,53 +62,62 @@ public void explain_no_page_size() { @Test public void can_execute_paginated_plan() { - var listener = new ResponseListener() { - @Override - public void onResponse(ExecutionEngine.QueryResponse response) { - assertNotNull(response); - } - - @Override - public void onFailure(Exception e) { - fail(); - } - }; - var plan = new QueryPlan(QueryId.queryId(), mock(UnresolvedPlan.class), 10, - queryService, listener); + var listener = + new ResponseListener() { + @Override + public void onResponse(ExecutionEngine.QueryResponse response) { + assertNotNull(response); + } + + @Override + public void onFailure(Exception e) { + fail(); + } + }; + var plan = + new QueryPlan(QueryId.queryId(), mock(UnresolvedPlan.class), 10, queryService, listener); plan.execute(); } @Test // Same as previous test, but with incomplete QueryService public void can_handle_error_while_executing_plan() { - var listener = new ResponseListener() { - @Override - public void onResponse(ExecutionEngine.QueryResponse response) { - fail(); - } - - @Override - public void onFailure(Exception e) { - assertNotNull(e); - } - }; - var plan = new QueryPlan(QueryId.queryId(), mock(UnresolvedPlan.class), 10, - new QueryService(null, new DefaultExecutionEngine(), null), listener); + var listener = + new ResponseListener() { + @Override + public void onResponse(ExecutionEngine.QueryResponse response) { + fail(); + } + + @Override + public void onFailure(Exception e) { + assertNotNull(e); + } + }; + var plan = + new QueryPlan( + QueryId.queryId(), + mock(UnresolvedPlan.class), + 10, + new QueryService(null, new DefaultExecutionEngine(), null), + listener); plan.execute(); } @Test public void explain_is_not_supported_for_pagination() { - new QueryPlan(null, null, 0, null, null).explain(new ResponseListener<>() { - @Override - public void onResponse(ExecutionEngine.ExplainResponse response) { - fail(); - } - - @Override - public void onFailure(Exception e) { - assertTrue(e instanceof NotImplementedException); - } - }); + new QueryPlan(null, null, 0, null, null) + .explain( + new ResponseListener<>() { + @Override + public void onResponse(ExecutionEngine.ExplainResponse response) { + fail(); + } + + @Override + public void onFailure(Exception e) { + assertTrue(e instanceof NotImplementedException); + } + }); } } diff --git a/core/src/test/java/org/opensearch/sql/executor/execution/StreamingQueryPlanTest.java b/core/src/test/java/org/opensearch/sql/executor/execution/StreamingQueryPlanTest.java index 7357e99d181..2e8666aea48 100644 --- a/core/src/test/java/org/opensearch/sql/executor/execution/StreamingQueryPlanTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/execution/StreamingQueryPlanTest.java @@ -49,9 +49,7 @@ class StreamingQueryPlanTest { @Test void executionSuccess() throws InterruptedException { - streamingQuery() - .streamingSource() - .shouldSuccess(); + streamingQuery().streamingSource().shouldSuccess(); } @Test @@ -70,9 +68,7 @@ void failIfNoStreamingSource() throws InterruptedException { @Test void taskExecutionShouldNotCallListener() throws InterruptedException { - streamingQuery() - .streamingSource() - .taskExecutionShouldNotCallListener(); + streamingQuery().streamingSource().taskExecutionShouldNotCallListener(); } Helper streamingQuery() { diff --git a/core/src/test/java/org/opensearch/sql/executor/pagination/CanPaginateVisitorTest.java b/core/src/test/java/org/opensearch/sql/executor/pagination/CanPaginateVisitorTest.java index 003967d6aab..5f2ba86c2f7 100644 --- a/core/src/test/java/org/opensearch/sql/executor/pagination/CanPaginateVisitorTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/pagination/CanPaginateVisitorTest.java @@ -121,8 +121,7 @@ public void allow_query_with_select_fields_and_from() { @Test // select x public void allow_query_without_from() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", intLiteral(1))); + var plan = project(values(List.of(intLiteral(1))), alias("1", intLiteral(1))); assertTrue(plan.accept(visitor, null)); } @@ -130,106 +129,124 @@ public void allow_query_without_from() { public void visitField() { // test combinations of acceptable and not acceptable args for coverage assertAll( - () -> assertFalse(project(relation("dummy"), - field(map("1", "2"), argument("name", intLiteral(0)))) - .accept(visitor, null)), - () -> assertFalse(project(relation("dummy"), - field("field", new Argument("", new Literal(1, DataType.INTEGER) { - @Override - public List getChild() { - return List.of(map("1", "2")); - } - }))) - .accept(visitor, null)) - ); + () -> + assertFalse( + project(relation("dummy"), field(map("1", "2"), argument("name", intLiteral(0)))) + .accept(visitor, null)), + () -> + assertFalse( + project( + relation("dummy"), + field( + "field", + new Argument( + "", + new Literal(1, DataType.INTEGER) { + @Override + public List getChild() { + return List.of(map("1", "2")); + } + }))) + .accept(visitor, null))); } @Test public void visitAlias() { // test combinations of acceptable and not acceptable args for coverage assertAll( - () -> assertFalse(project(relation("dummy"), - alias("pew", map("1", "2"), "pew")) - .accept(visitor, null)), - () -> assertFalse(project(relation("dummy"), new Alias("pew", field("pew")) { - @Override - public List getChild() { - return List.of(map("1", "2")); - } - }) - .accept(visitor, null)) - ); + () -> + assertFalse( + project(relation("dummy"), alias("pew", map("1", "2"), "pew")) + .accept(visitor, null)), + () -> + assertFalse( + project( + relation("dummy"), + new Alias("pew", field("pew")) { + @Override + public List getChild() { + return List.of(map("1", "2")); + } + }) + .accept(visitor, null))); } @Test // select a = b public void visitEqualTo() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", equalTo(intLiteral(1), intLiteral(1)))); + var plan = + project(values(List.of(intLiteral(1))), alias("1", equalTo(intLiteral(1), intLiteral(1)))); assertTrue(plan.accept(visitor, null)); } @Test // select interval public void visitInterval() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", intervalLiteral(intLiteral(1), DataType.INTEGER, "days"))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", intervalLiteral(intLiteral(1), DataType.INTEGER, "days"))); assertTrue(plan.accept(visitor, null)); } @Test // select a != b public void visitCompare() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", compare("!=", intLiteral(1), intLiteral(1)))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", compare("!=", intLiteral(1), intLiteral(1)))); assertTrue(plan.accept(visitor, null)); } @Test // select NOT a public void visitNot() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", not(booleanLiteral(true)))); + var plan = project(values(List.of(intLiteral(1))), alias("1", not(booleanLiteral(true)))); assertTrue(plan.accept(visitor, null)); } @Test // select a OR b public void visitOr() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", or(booleanLiteral(true), booleanLiteral(false)))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", or(booleanLiteral(true), booleanLiteral(false)))); assertTrue(plan.accept(visitor, null)); } @Test // select a AND b public void visitAnd() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", and(booleanLiteral(true), booleanLiteral(false)))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", and(booleanLiteral(true), booleanLiteral(false)))); assertTrue(plan.accept(visitor, null)); } @Test // select a XOR b public void visitXor() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", xor(booleanLiteral(true), booleanLiteral(false)))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", xor(booleanLiteral(true), booleanLiteral(false)))); assertTrue(plan.accept(visitor, null)); } @Test // select f() public void visitFunction() { - var plan = project(values(List.of(intLiteral(1))), - function("func")); + var plan = project(values(List.of(intLiteral(1))), function("func")); assertTrue(plan.accept(visitor, null)); } @Test // select nested() ... public void visitNested() { - var plan = project(values(List.of(intLiteral(1))), - function("nested")); + var plan = project(values(List.of(intLiteral(1))), function("nested")); assertFalse(plan.accept(visitor, null)); } @@ -238,30 +255,37 @@ public void visitNested() { public void visitIn() { // test combinations of acceptable and not acceptable args for coverage assertAll( - () -> assertTrue(project(values(List.of(intLiteral(1))), alias("1", in(field("a")))) - .accept(visitor, null)), - () -> assertFalse(project(values(List.of(intLiteral(1))), - alias("1", in(field("a"), map("1", "2")))) - .accept(visitor, null)), - () -> assertFalse(project(values(List.of(intLiteral(1))), - alias("1", in(map("1", "2"), field("a")))) - .accept(visitor, null)) - ); + () -> + assertTrue( + project(values(List.of(intLiteral(1))), alias("1", in(field("a")))) + .accept(visitor, null)), + () -> + assertFalse( + project(values(List.of(intLiteral(1))), alias("1", in(field("a"), map("1", "2")))) + .accept(visitor, null)), + () -> + assertFalse( + project(values(List.of(intLiteral(1))), alias("1", in(map("1", "2"), field("a")))) + .accept(visitor, null))); } @Test // select a BETWEEN 1 AND 2 public void visitBetween() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", between(field("a"), intLiteral(1), intLiteral(2)))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", between(field("a"), intLiteral(1), intLiteral(2)))); assertTrue(plan.accept(visitor, null)); } @Test // select a CASE 1 WHEN 2 public void visitCase() { - var plan = project(values(List.of(intLiteral(1))), - alias("1", caseWhen(intLiteral(1), when(intLiteral(3), intLiteral(4))))); + var plan = + project( + values(List.of(intLiteral(1))), + alias("1", caseWhen(intLiteral(1), when(intLiteral(3), intLiteral(4))))); assertTrue(plan.accept(visitor, null)); } @@ -270,21 +294,33 @@ public void visitCase() { public void visitCast() { // test combinations of acceptable and not acceptable args for coverage assertAll( - () -> assertTrue(project(values(List.of(intLiteral(1))), - alias("1", cast(intLiteral(2), stringLiteral("int")))) - .accept(visitor, null)), - () -> assertFalse(project(values(List.of(intLiteral(1))), - alias("1", cast(intLiteral(2), new Literal(1, DataType.INTEGER) { - @Override - public List getChild() { - return List.of(map("1", "2")); - } - }))) - .accept(visitor, null)), - () -> assertFalse(project(values(List.of(intLiteral(1))), - alias("1", cast(map("1", "2"), stringLiteral("int")))) - .accept(visitor, null)) - ); + () -> + assertTrue( + project( + values(List.of(intLiteral(1))), + alias("1", cast(intLiteral(2), stringLiteral("int")))) + .accept(visitor, null)), + () -> + assertFalse( + project( + values(List.of(intLiteral(1))), + alias( + "1", + cast( + intLiteral(2), + new Literal(1, DataType.INTEGER) { + @Override + public List getChild() { + return List.of(map("1", "2")); + } + }))) + .accept(visitor, null)), + () -> + assertFalse( + project( + values(List.of(intLiteral(1))), + alias("1", cast(map("1", "2"), stringLiteral("int")))) + .accept(visitor, null))); } @Test @@ -305,20 +341,22 @@ public void reject_query_with_eval() { // simple_query_string(["Tags" ^ 1.5, "Title", "Body" 4.2], "taste") // and Tags like "% % %" and Title like "%"; public void accept_query_with_highlight_and_relevance_func() { - var plan = project( - filter( - relation("beer.stackexchange"), - and( + var plan = + project( + filter( + relation("beer.stackexchange"), and( - function("like", qualifiedName("Tags"), stringLiteral("% % %")), - function("like", qualifiedName("Title"), stringLiteral("%"))), - function("simple_query_string", - unresolvedArg("fields", - new RelevanceFieldList(Map.of("Title", 1.0F, "Body", 4.2F, "Tags", 1.5F))), - unresolvedArg("query", - stringLiteral("taste"))))), - alias("highlight(\"Body\")", - highlight(stringLiteral("Body"), Map.of()))); + and( + function("like", qualifiedName("Tags"), stringLiteral("% % %")), + function("like", qualifiedName("Title"), stringLiteral("%"))), + function( + "simple_query_string", + unresolvedArg( + "fields", + new RelevanceFieldList( + Map.of("Title", 1.0F, "Body", 4.2F, "Tags", 1.5F))), + unresolvedArg("query", stringLiteral("taste"))))), + alias("highlight(\"Body\")", highlight(stringLiteral("Body"), Map.of()))); assertTrue(plan.accept(visitor, null)); } @@ -339,12 +377,13 @@ public void reject_query_with_offset() { // test added for coverage only @Test public void visitLimit() { - var visitor = new CanPaginateVisitor() { - @Override - public Boolean visitRelation(Relation node, Object context) { - return Boolean.FALSE; - } - }; + var visitor = + new CanPaginateVisitor() { + @Override + public Boolean visitRelation(Relation node, Object context) { + return Boolean.FALSE; + } + }; var plan = project(limit(relation("dummy"), 0, 0), allFields()); assertFalse(plan.accept(visitor, null)); } @@ -352,8 +391,7 @@ public Boolean visitRelation(Relation node, Object context) { @Test // select * from y where z public void allow_query_with_where() { - var plan = project(filter(relation("dummy"), - booleanLiteral(true)), allFields()); + var plan = project(filter(relation("dummy"), booleanLiteral(true)), allFields()); assertTrue(plan.accept(visitor, null)); } @@ -367,28 +405,29 @@ public void allow_query_with_order_by_with_column_references_only() { @Test // select * from y order by func(z) public void reject_query_with_order_by_with_an_expression() { - var plan = project(sort(relation("dummy"), field(function("func"))), - allFields()); + var plan = project(sort(relation("dummy"), field(function("func"))), allFields()); assertFalse(plan.accept(visitor, null)); } // test added for coverage only @Test public void visitSort() { - CanPaginateVisitor visitor = new CanPaginateVisitor() { - @Override - public Boolean visitRelation(Relation node, Object context) { - return Boolean.FALSE; - } - }; + CanPaginateVisitor visitor = + new CanPaginateVisitor() { + @Override + public Boolean visitRelation(Relation node, Object context) { + return Boolean.FALSE; + } + }; var plan = project(sort(relation("dummy"), field("1")), allFields()); assertFalse(plan.accept(visitor, null)); - visitor = new CanPaginateVisitor() { - @Override - public Boolean visitField(Field node, Object context) { - return Boolean.FALSE; - } - }; + visitor = + new CanPaginateVisitor() { + @Override + public Boolean visitField(Field node, Object context) { + return Boolean.FALSE; + } + }; plan = project(sort(relation("dummy"), field("1")), allFields()); assertFalse(plan.accept(visitor, null)); } @@ -396,31 +435,38 @@ public Boolean visitField(Field node, Object context) { @Test // select * from y group by z public void reject_query_with_group_by() { - var plan = project(agg( - relation("dummy"), List.of(), List.of(), List.of(field("1")), List.of()), - allFields()); + var plan = + project( + agg(relation("dummy"), List.of(), List.of(), List.of(field("1")), List.of()), + allFields()); assertFalse(plan.accept(visitor, null)); } @Test // select agg(x) from y public void reject_query_with_aggregation_function() { - var plan = project(agg( - relation("dummy"), - List.of(alias("agg", aggregate("func", field("pewpew")))), - List.of(), List.of(), List.of()), - allFields()); + var plan = + project( + agg( + relation("dummy"), + List.of(alias("agg", aggregate("func", field("pewpew")))), + List.of(), + List.of(), + List.of()), + allFields()); assertFalse(plan.accept(visitor, null)); } @Test // select window(x) from y public void reject_query_with_window_function() { - var plan = project(relation("dummy"), - alias("pewpew", - window( - aggregate("func", field("pewpew")), - List.of(qualifiedName("1")), List.of()))); + var plan = + project( + relation("dummy"), + alias( + "pewpew", + window( + aggregate("func", field("pewpew")), List.of(qualifiedName("1")), List.of()))); assertFalse(plan.accept(visitor, null)); } @@ -449,12 +495,13 @@ public void reject_project_when_relation_has_child() { // test combinations of acceptable and not acceptable args for coverage public void visitFilter() { assertAll( - () -> assertTrue(project(filter(relation("dummy"), booleanLiteral(true))) - .accept(visitor, null)), - () -> assertFalse(project(filter(relation("dummy"), map("1", "2"))) - .accept(visitor, null)), - () -> assertFalse(project(filter(tableFunction(List.of("1", "2")), booleanLiteral(true))) - .accept(visitor, null)) - ); + () -> + assertTrue( + project(filter(relation("dummy"), booleanLiteral(true))).accept(visitor, null)), + () -> assertFalse(project(filter(relation("dummy"), map("1", "2"))).accept(visitor, null)), + () -> + assertFalse( + project(filter(tableFunction(List.of("1", "2")), booleanLiteral(true))) + .accept(visitor, null))); } } diff --git a/core/src/test/java/org/opensearch/sql/executor/pagination/CursorTest.java b/core/src/test/java/org/opensearch/sql/executor/pagination/CursorTest.java index e3e2c8cf333..fe9590b60be 100644 --- a/core/src/test/java/org/opensearch/sql/executor/pagination/CursorTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/pagination/CursorTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; -import org.opensearch.sql.executor.pagination.Cursor; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class CursorTest { diff --git a/core/src/test/java/org/opensearch/sql/executor/pagination/PlanSerializerTest.java b/core/src/test/java/org/opensearch/sql/executor/pagination/PlanSerializerTest.java index 8211a3bc12f..495a7db80c2 100644 --- a/core/src/test/java/org/opensearch/sql/executor/pagination/PlanSerializerTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/pagination/PlanSerializerTest.java @@ -44,9 +44,14 @@ void setUp() { } @ParameterizedTest - @ValueSource(strings = {"pewpew", "asdkfhashdfjkgakgfwuigfaijkb", "ajdhfgajklghadfjkhgjkadhgad" - + "kadfhgadhjgfjklahdgqheygvskjfbvgsdklgfuirehiluANUIfgauighbahfuasdlhfnhaughsdlfhaughaggf" - + "and_some_other_funny_stuff_which_could_be_generated_while_sleeping_on_the_keyboard"}) + @ValueSource( + strings = { + "pewpew", + "asdkfhashdfjkgakgfwuigfaijkb", + "ajdhfgajklghadfjkhgjkadhgad" + + "kadfhgadhjgfjklahdgqheygvskjfbvgsdklgfuirehiluANUIfgauighbahfuasdlhfnhaughsdlfhaughaggf" + + "and_some_other_funny_stuff_which_could_be_generated_while_sleeping_on_the_keyboard" + }) void serialize_deserialize_str(String input) { var compressed = serialize(input); assertEquals(input, deserialize(compressed)); @@ -98,8 +103,7 @@ void deserialize_throws() { // from gzip - damaged header () -> assertThrows(Throwable.class, () -> deserialize("00")), // from HashCode::fromString - () -> assertThrows(Throwable.class, () -> deserialize("000")) - ); + () -> assertThrows(Throwable.class, () -> deserialize("000"))); } @Test @@ -109,8 +113,7 @@ void convertToCursor_returns_no_cursor_if_cant_serialize() { plan.setThrowNoCursorOnWrite(true); assertAll( () -> assertThrows(NoCursorException.class, () -> serialize(plan)), - () -> assertEquals(Cursor.None, planCache.convertToCursor(plan)) - ); + () -> assertEquals(Cursor.None, planCache.convertToCursor(plan))); } @Test @@ -122,14 +125,14 @@ void convertToCursor_returns_no_cursor_if_plan_is_not_paginate() { @Test void convertToPlan_throws_cursor_has_no_prefix() { - assertThrows(UnsupportedOperationException.class, () -> - planCache.convertToPlan("abc")); + assertThrows(UnsupportedOperationException.class, () -> planCache.convertToPlan("abc")); } @Test void convertToPlan_throws_if_failed_to_deserialize() { - assertThrows(UnsupportedOperationException.class, () -> - planCache.convertToPlan("n:" + serialize(mock(Serializable.class)))); + assertThrows( + UnsupportedOperationException.class, + () -> planCache.convertToPlan("n:" + serialize(mock(Serializable.class)))); } @Test @@ -144,8 +147,8 @@ void serialize_and_deserialize() { @Test void convertToCursor_and_convertToPlan() { var plan = new TestOperator(100500); - var roundTripPlan = (SerializablePlan) - planCache.convertToPlan(planCache.convertToCursor(plan).toString()); + var roundTripPlan = + (SerializablePlan) planCache.convertToPlan(planCache.convertToCursor(plan).toString()); assertEquals(plan, roundTripPlan); assertNotSame(plan, roundTripPlan); } @@ -158,8 +161,8 @@ void resolveObject() { objectOutput.writeObject("Hello, world!"); objectOutput.flush(); - var cds = planCache.getCursorDeserializationStream( - new ByteArrayInputStream(output.toByteArray())); + var cds = + planCache.getCursorDeserializationStream(new ByteArrayInputStream(output.toByteArray())); assertEquals(storageEngine, cds.resolveObject("engine")); var object = new Object(); assertSame(object, cds.resolveObject(object)); diff --git a/core/src/test/java/org/opensearch/sql/executor/streaming/DefaultMetadataLogTest.java b/core/src/test/java/org/opensearch/sql/executor/streaming/DefaultMetadataLogTest.java index 4d8c4f3e937..17ea253e2aa 100644 --- a/core/src/test/java/org/opensearch/sql/executor/streaming/DefaultMetadataLogTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/streaming/DefaultMetadataLogTest.java @@ -8,7 +8,6 @@ package org.opensearch.sql.executor.streaming; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/core/src/test/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecutionTest.java b/core/src/test/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecutionTest.java index f0974db13ea..bd0c8ed3e10 100644 --- a/core/src/test/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecutionTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecutionTest.java @@ -45,19 +45,12 @@ void executedSuccess() { @Test void executedFailed() { - streamingQuery() - .addData() - .executeFailed() - .latestOffsetLogShouldBe(0L) - .noCommittedLog(); + streamingQuery().addData().executeFailed().latestOffsetLogShouldBe(0L).noCommittedLog(); } @Test void noDataInSource() { - streamingQuery() - .neverProcess() - .noOffsetLog() - .noCommittedLog(); + streamingQuery().neverProcess().noOffsetLog().noCommittedLog(); } @Test @@ -170,8 +163,7 @@ Helper executeSuccess(Long... offsets) { ResponseListener listener = invocation.getArgument(2); listener.onResponse( - new ExecutionEngine.QueryResponse(null, Collections.emptyList(), - Cursor.None)); + new ExecutionEngine.QueryResponse(null, Collections.emptyList(), Cursor.None)); PlanContext planContext = invocation.getArgument(1); assertTrue(planContext.getSplit().isPresent()); @@ -230,7 +222,7 @@ Helper latestOffsetLogShouldBe(Long offsetId) { * StreamingSource impl only for testing. * *

initially, offset is -1, getLatestOffset() will return Optional.emtpy(). - * + * *

call addData() add offset by one. */ static class TestStreamingSource implements StreamingSource { @@ -257,8 +249,8 @@ public Optional getLatestOffset() { public Batch getBatch(Optional start, Offset end) { return new Batch( new TestOffsetSplit( - start.map(v -> v.getOffset() + 1).orElse(0L), Long.min(offset.get(), - end.getOffset()))); + start.map(v -> v.getOffset() + 1).orElse(0L), + Long.min(offset.get(), end.getOffset()))); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/ExpressionNodeVisitorTest.java b/core/src/test/java/org/opensearch/sql/expression/ExpressionNodeVisitorTest.java index 47fe9dad0fa..2f3e855430a 100644 --- a/core/src/test/java/org/opensearch/sql/expression/ExpressionNodeVisitorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/ExpressionNodeVisitorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,70 +30,69 @@ class ExpressionNodeVisitorTest { @Test void should_return_null_by_default() { - ExpressionNodeVisitor visitor = new ExpressionNodeVisitor(){}; + ExpressionNodeVisitor visitor = new ExpressionNodeVisitor() {}; assertNull(new HighlightExpression(DSL.literal("Title")).accept(visitor, null)); assertNull(literal(10).accept(visitor, null)); assertNull(ref("name", STRING).accept(visitor, null)); assertNull(named("bool", literal(true)).accept(visitor, null)); assertNull(DSL.abs(literal(-10)).accept(visitor, null)); assertNull(DSL.sum(literal(10)).accept(visitor, null)); - assertNull(named("avg", new AvgAggregator(Collections.singletonList(ref("age", INTEGER)), - INTEGER)).accept(visitor, null)); + assertNull( + named("avg", new AvgAggregator(Collections.singletonList(ref("age", INTEGER)), INTEGER)) + .accept(visitor, null)); assertNull(new CaseClause(ImmutableList.of(), null).accept(visitor, null)); assertNull(new WhenClause(literal("test"), literal(10)).accept(visitor, null)); assertNull(DSL.namedArgument("field", literal("message")).accept(visitor, null)); assertNull(DSL.span(ref("age", INTEGER), literal(1), "").accept(visitor, null)); - assertNull(DSL.regex(ref("name", STRING), DSL.literal("(?\\d+)"), DSL.literal("group")) - .accept(visitor, null)); + assertNull( + DSL.regex(ref("name", STRING), DSL.literal("(?\\d+)"), DSL.literal("group")) + .accept(visitor, null)); } @Test void can_visit_all_types_of_expression_node() { Expression expr = DSL.regex( - DSL.castString( - DSL.sum( - DSL.add( - ref("balance", INTEGER), - literal(10)) - )), + DSL.castString(DSL.sum(DSL.add(ref("balance", INTEGER), literal(10)))), DSL.literal("(?\\d+)"), DSL.literal("group")); - Expression actual = expr.accept(new ExpressionNodeVisitor() { - @Override - public Expression visitLiteral(LiteralExpression node, Object context) { - return node; - } + Expression actual = + expr.accept( + new ExpressionNodeVisitor() { + @Override + public Expression visitLiteral(LiteralExpression node, Object context) { + return node; + } - @Override - public Expression visitReference(ReferenceExpression node, Object context) { - return node; - } + @Override + public Expression visitReference(ReferenceExpression node, Object context) { + return node; + } - @Override - public Expression visitParse(ParseExpression node, Object context) { - return node; - } + @Override + public Expression visitParse(ParseExpression node, Object context) { + return node; + } - @Override - public Expression visitFunction(FunctionExpression node, Object context) { - return DSL.add(visitArguments(node.getArguments(), context)); - } + @Override + public Expression visitFunction(FunctionExpression node, Object context) { + return DSL.add(visitArguments(node.getArguments(), context)); + } - @Override - public Expression visitAggregator(Aggregator node, Object context) { - return DSL.sum(visitArguments(node.getArguments(), context)); - } + @Override + public Expression visitAggregator(Aggregator node, Object context) { + return DSL.sum(visitArguments(node.getArguments(), context)); + } - private Expression[] visitArguments(List arguments, Object context) { - return arguments.stream() - .map(arg -> arg.accept(this, context)) - .toArray(Expression[]::new); - } - }, null); + private Expression[] visitArguments(List arguments, Object context) { + return arguments.stream() + .map(arg -> arg.accept(this, context)) + .toArray(Expression[]::new); + } + }, + null); assertEquals(expr, actual); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java b/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java index 8ce7a523948..fd886cdda36 100644 --- a/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java +++ b/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import static org.opensearch.sql.config.TestConfig.BOOL_TYPE_MISSING_VALUE_FIELD; diff --git a/core/src/test/java/org/opensearch/sql/expression/HighlightExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/HighlightExpressionTest.java index 41f3bad030b..bc6b3628b0a 100644 --- a/core/src/test/java/org/opensearch/sql/expression/HighlightExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/HighlightExpressionTest.java @@ -18,13 +18,13 @@ import org.opensearch.sql.data.model.ExprValueUtils; import org.opensearch.sql.expression.env.Environment; - public class HighlightExpressionTest extends ExpressionTestBase { @Test public void single_highlight_test() { - Environment hlTuple = ExprValueUtils.tupleValue( - ImmutableMap.of("_highlight.Title", "result value")).bindingTuples(); + Environment hlTuple = + ExprValueUtils.tupleValue(ImmutableMap.of("_highlight.Title", "result value")) + .bindingTuples(); HighlightExpression expr = new HighlightExpression(DSL.literal("Title")); ExprValue resultVal = expr.valueOf(hlTuple); @@ -34,8 +34,9 @@ public void single_highlight_test() { @Test public void missing_highlight_test() { - Environment hlTuple = ExprValueUtils.tupleValue( - ImmutableMap.of("_highlight.Title", "result value")).bindingTuples(); + Environment hlTuple = + ExprValueUtils.tupleValue(ImmutableMap.of("_highlight.Title", "result value")) + .bindingTuples(); HighlightExpression expr = new HighlightExpression(DSL.literal("invalid")); ExprValue resultVal = expr.valueOf(hlTuple); @@ -52,8 +53,8 @@ public void missing_highlight_wildcard_test() { builder.put("_highlight", ExprTupleValue.fromExprValueMap(hlBuilder.build())); HighlightExpression hlExpr = new HighlightExpression(DSL.literal("invalid*")); - ExprValue resultVal = hlExpr.valueOf( - ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); + ExprValue resultVal = + hlExpr.valueOf(ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); assertTrue(resultVal.isMissing()); } @@ -67,20 +68,23 @@ public void highlight_all_test() { builder.put("_highlight", ExprTupleValue.fromExprValueMap(hlBuilder.build())); HighlightExpression hlExpr = new HighlightExpression(DSL.literal("T*")); - ExprValue resultVal = hlExpr.valueOf( - ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); + ExprValue resultVal = + hlExpr.valueOf(ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); assertEquals(STRUCT, resultVal.type()); - assertTrue(resultVal.tupleValue().containsValue( - ExprValueUtils.stringValue("correct result value"))); - assertFalse(resultVal.tupleValue().containsValue( - ExprValueUtils.stringValue("secondary correct result value"))); + assertTrue( + resultVal.tupleValue().containsValue(ExprValueUtils.stringValue("correct result value"))); + assertFalse( + resultVal + .tupleValue() + .containsValue(ExprValueUtils.stringValue("secondary correct result value"))); } @Test public void do_nothing_with_missing_value() { - Environment hlTuple = ExprValueUtils.tupleValue( - ImmutableMap.of("NonHighlightField", "ResultValue")).bindingTuples(); + Environment hlTuple = + ExprValueUtils.tupleValue(ImmutableMap.of("NonHighlightField", "ResultValue")) + .bindingTuples(); HighlightExpression expr = new HighlightExpression(DSL.literal("*")); ExprValue resultVal = expr.valueOf(hlTuple); @@ -96,13 +100,13 @@ public void highlight_wildcard_test() { builder.put("_highlight", ExprTupleValue.fromExprValueMap(hlBuilder.build())); HighlightExpression hlExpr = new HighlightExpression(DSL.literal("T*")); - ExprValue resultVal = hlExpr.valueOf( - ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); + ExprValue resultVal = + hlExpr.valueOf(ExprTupleValue.fromExprValueMap(builder.build()).bindingTuples()); assertEquals(STRUCT, resultVal.type()); - assertTrue(resultVal.tupleValue().containsValue( - ExprValueUtils.stringValue("correct result value"))); - assertFalse(resultVal.tupleValue().containsValue( - ExprValueUtils.stringValue("incorrect result value"))); + assertTrue( + resultVal.tupleValue().containsValue(ExprValueUtils.stringValue("correct result value"))); + assertFalse( + resultVal.tupleValue().containsValue(ExprValueUtils.stringValue("incorrect result value"))); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/NamedExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/NamedExpressionTest.java index 38f1ce3ca9d..915952cca80 100644 --- a/core/src/test/java/org/opensearch/sql/expression/NamedExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/NamedExpressionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -55,11 +54,12 @@ void name_a_span_expression() { @Test void name_a_parse_expression() { ParseExpression parse = - DSL.regex(DSL.ref("string_value", STRING), DSL.literal("(?\\w{2})\\w"), + DSL.regex( + DSL.ref("string_value", STRING), + DSL.literal("(?\\w{2})\\w"), DSL.literal("group")); NamedExpression named = DSL.named(parse); assertEquals(parse, named.getDelegated()); assertEquals(parse.getIdentifier().valueOf().stringValue(), named.getName()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/ReferenceExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/ReferenceExpressionTest.java index 46aae069bbd..65ca01a9f61 100644 --- a/core/src/test/java/org/opensearch/sql/expression/ReferenceExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/ReferenceExpressionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -54,13 +53,13 @@ public void resolve_value() { assertEquals(doubleValue(1d), DSL.ref("double_value", DOUBLE).valueOf(valueEnv())); assertEquals(booleanValue(true), DSL.ref("boolean_value", BOOLEAN).valueOf(valueEnv())); assertEquals(stringValue("str"), DSL.ref("string_value", STRING).valueOf(valueEnv())); - assertEquals(tupleValue(ImmutableMap.of("str", 1)), - DSL.ref("struct_value", STRUCT).valueOf(valueEnv())); - assertEquals(collectionValue(ImmutableList.of(1)), - DSL.ref("array_value", ARRAY).valueOf(valueEnv())); + assertEquals( + tupleValue(ImmutableMap.of("str", 1)), DSL.ref("struct_value", STRUCT).valueOf(valueEnv())); + assertEquals( + collectionValue(ImmutableList.of(1)), DSL.ref("array_value", ARRAY).valueOf(valueEnv())); assertEquals(LITERAL_NULL, DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN).valueOf(valueEnv())); - assertEquals(LITERAL_MISSING, - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN).valueOf(valueEnv())); + assertEquals( + LITERAL_MISSING, DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN).valueOf(valueEnv())); } @Test @@ -138,58 +137,33 @@ public void array_with_multiple_path_value() { } /** - * { - * "name": "bob smith" - * "project.year": 1990, - * "project": { - * "year": 2020 - * }, - * "address": { - * "state": "WA", - * "city": "seattle" - * "project.year": 1990 - * }, - * "address.local": { - * "state": "WA", - * }, - * "message": [ - * { "info": "message in array" }, - * { "info": "Only first index of array used" } - * ] - * } + * { "name": "bob smith" "project.year": 1990, "project": { "year": 2020 }, "address": { "state": + * "WA", "city": "seattle" "project.year": 1990 }, "address.local": { "state": "WA", }, "message": + * [ { "info": "message in array" }, { "info": "Only first index of array used" } ] } */ private ExprTupleValue tuple() { ExprValue address = - ExprValueUtils.tupleValue(ImmutableMap.of("state", "WA", "city", "seattle", "project" - + ".year", 1990)); - ExprValue project = - ExprValueUtils.tupleValue(ImmutableMap.of("year", 2020)); - ExprValue addressLocal = - ExprValueUtils.tupleValue(ImmutableMap.of("state", "WA")); + ExprValueUtils.tupleValue( + ImmutableMap.of("state", "WA", "city", "seattle", "project" + ".year", 1990)); + ExprValue project = ExprValueUtils.tupleValue(ImmutableMap.of("year", 2020)); + ExprValue addressLocal = ExprValueUtils.tupleValue(ImmutableMap.of("state", "WA")); ExprValue messageCollectionValue = new ExprCollectionValue( ImmutableList.of( ExprValueUtils.tupleValue( - ImmutableMap.of( - "info", stringValue("First message in array") - ) - ), + ImmutableMap.of("info", stringValue("First message in array"))), ExprValueUtils.tupleValue( - ImmutableMap.of( - "info", stringValue("Only first index of array used") - ) - ) - ) - ); - - ExprTupleValue tuple = ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "name", new ExprStringValue("bob smith"), - "project.year", new ExprIntegerValue(1990), - "project", project, - "address", address, - "address.local", addressLocal, - "message", messageCollectionValue - )); + ImmutableMap.of("info", stringValue("Only first index of array used"))))); + + ExprTupleValue tuple = + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "name", new ExprStringValue("bob smith"), + "project.year", new ExprIntegerValue(1990), + "project", project, + "address", address, + "address.local", addressLocal, + "message", messageCollectionValue)); return tuple; } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/AggregationTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/AggregationTest.java index 7742e6c4d03..f1a3a9d9488 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/AggregationTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/AggregationTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import com.google.common.collect.ImmutableList; @@ -98,26 +97,46 @@ public class AggregationTest extends ExpressionTestBase { protected static List tuples_with_duplicates = Arrays.asList( - ExprValueUtils.tupleValue(ImmutableMap.of( - "integer_value", 1, - "double_value", 4d, - "struct_value", ImmutableMap.of("str", 1), - "array_value", ImmutableList.of(1))), - ExprValueUtils.tupleValue(ImmutableMap.of( - "integer_value", 1, - "double_value", 3d, - "struct_value", ImmutableMap.of("str", 1), - "array_value", ImmutableList.of(1))), - ExprValueUtils.tupleValue(ImmutableMap.of( - "integer_value", 2, - "double_value", 2d, - "struct_value", ImmutableMap.of("str", 2), - "array_value", ImmutableList.of(2))), - ExprValueUtils.tupleValue(ImmutableMap.of( - "integer_value", 3, - "double_value", 1d, - "struct_value", ImmutableMap.of("str1", 1), - "array_value", ImmutableList.of(1, 2)))); + ExprValueUtils.tupleValue( + ImmutableMap.of( + "integer_value", + 1, + "double_value", + 4d, + "struct_value", + ImmutableMap.of("str", 1), + "array_value", + ImmutableList.of(1))), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "integer_value", + 1, + "double_value", + 3d, + "struct_value", + ImmutableMap.of("str", 1), + "array_value", + ImmutableList.of(1))), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "integer_value", + 2, + "double_value", + 2d, + "struct_value", + ImmutableMap.of("str", 2), + "array_value", + ImmutableList.of(2))), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "integer_value", + 3, + "double_value", + 1d, + "struct_value", + ImmutableMap.of("str1", 1), + "array_value", + ImmutableList.of(1, 2)))); protected static List tuples_with_null_and_missing = Arrays.asList( diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/AvgAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/AvgAggregatorTest.java index b3b0052bc32..f465a6477ec 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/AvgAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/AvgAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,16 +36,23 @@ public void avg_field_expression() { @Test public void avg_arithmetic_expression() { - ExprValue result = aggregation(DSL.avg( - DSL.multiply(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))), tuples); + ExprValue result = + aggregation( + DSL.avg( + DSL.multiply( + DSL.ref("integer_value", INTEGER), + DSL.literal(ExprValueUtils.integerValue(10)))), + tuples); assertEquals(25.0, result.value()); } @Test public void filtered_avg() { - ExprValue result = aggregation(DSL.avg(DSL.ref("integer_value", INTEGER)) - .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), tuples); + ExprValue result = + aggregation( + DSL.avg(DSL.ref("integer_value", INTEGER)) + .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), + tuples); assertEquals(3.0, result.value()); } @@ -128,16 +134,17 @@ public void avg_timestamp() { @Test public void valueOf() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.avg(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.avg(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: avg", exception.getMessage()); } @Test public void avg_on_unsupported_type() { var aggregator = new AvgAggregator(List.of(DSL.ref("string", STRING)), STRING); - var exception = assertThrows(IllegalArgumentException.class, - () -> aggregator.create()); + var exception = assertThrows(IllegalArgumentException.class, () -> aggregator.create()); assertEquals("avg aggregation over STRING type is not supported", exception.getMessage()); } @@ -149,9 +156,12 @@ public void test_to_string() { @Test public void test_nested_to_string() { - Aggregator avgAggregator = DSL.avg(DSL.multiply(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))); - assertEquals(String.format("avg(*(%s, %d))", DSL.ref("integer_value", INTEGER), 10), + Aggregator avgAggregator = + DSL.avg( + DSL.multiply( + DSL.ref("integer_value", INTEGER), DSL.literal(ExprValueUtils.integerValue(10)))); + assertEquals( + String.format("avg(*(%s, %d))", DSL.ref("integer_value", INTEGER), 10), avgAggregator.toString()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/CountAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/CountAggregatorTest.java index fd27529a700..50bd3fedfea 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/CountAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/CountAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -72,9 +71,13 @@ public void count_datetime_field_expression() { @Test public void count_arithmetic_expression() { - ExprValue result = aggregation(DSL.count( - DSL.multiply(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))), tuples); + ExprValue result = + aggregation( + DSL.count( + DSL.multiply( + DSL.ref("integer_value", INTEGER), + DSL.literal(ExprValueUtils.integerValue(10)))), + tuples); assertEquals(4, result.value()); } @@ -104,51 +107,56 @@ public void count_array_field_expression() { @Test public void filtered_count() { - ExprValue result = aggregation(DSL.count(DSL.ref("integer_value", INTEGER)) - .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), tuples); + ExprValue result = + aggregation( + DSL.count(DSL.ref("integer_value", INTEGER)) + .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), + tuples); assertEquals(3, result.value()); } @Test public void distinct_count() { - ExprValue result = aggregation(DSL.distinctCount(DSL.ref("integer_value", INTEGER)), - tuples_with_duplicates); + ExprValue result = + aggregation(DSL.distinctCount(DSL.ref("integer_value", INTEGER)), tuples_with_duplicates); assertEquals(3, result.value()); } @Test public void filtered_distinct_count() { - ExprValue result = aggregation(DSL.distinctCount(DSL.ref("integer_value", INTEGER)) - .condition(DSL.greater(DSL.ref("double_value", DOUBLE), DSL.literal(1d))), - tuples_with_duplicates); + ExprValue result = + aggregation( + DSL.distinctCount(DSL.ref("integer_value", INTEGER)) + .condition(DSL.greater(DSL.ref("double_value", DOUBLE), DSL.literal(1d))), + tuples_with_duplicates); assertEquals(2, result.value()); } @Test public void distinct_count_map() { - ExprValue result = aggregation(DSL.distinctCount(DSL.ref("struct_value", STRUCT)), - tuples_with_duplicates); + ExprValue result = + aggregation(DSL.distinctCount(DSL.ref("struct_value", STRUCT)), tuples_with_duplicates); assertEquals(3, result.value()); } @Test public void distinct_count_array() { - ExprValue result = aggregation(DSL.distinctCount(DSL.ref("array_value", ARRAY)), - tuples_with_duplicates); + ExprValue result = + aggregation(DSL.distinctCount(DSL.ref("array_value", ARRAY)), tuples_with_duplicates); assertEquals(3, result.value()); } @Test public void count_with_missing() { - ExprValue result = aggregation(DSL.count(DSL.ref("integer_value", INTEGER)), - tuples_with_null_and_missing); + ExprValue result = + aggregation(DSL.count(DSL.ref("integer_value", INTEGER)), tuples_with_null_and_missing); assertEquals(2, result.value()); } @Test public void count_with_null() { - ExprValue result = aggregation(DSL.count(DSL.ref("double_value", DOUBLE)), - tuples_with_null_and_missing); + ExprValue result = + aggregation(DSL.count(DSL.ref("double_value", DOUBLE)), tuples_with_null_and_missing); assertEquals(2, result.value()); } @@ -166,8 +174,10 @@ public void count_literal_with_null_and_missing() { @Test public void valueOf() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.count(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.count(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: count", exception.getMessage()); } @@ -183,7 +193,8 @@ public void test_to_string() { @Test public void test_nested_to_string() { Aggregator countAggregator = DSL.count(DSL.abs(DSL.ref("integer_value", INTEGER))); - assertEquals(String.format("count(abs(%s))", DSL.ref("integer_value", INTEGER)), + assertEquals( + String.format("count(abs(%s))", DSL.ref("integer_value", INTEGER)), countAggregator.toString()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/MaxAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/MaxAggregatorTest.java index 68866227045..c6cd380ad53 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/MaxAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/MaxAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -83,16 +82,23 @@ public void test_max_timestamp() { @Test public void test_max_arithmetic_expression() { - ExprValue result = aggregation( - DSL.max(DSL.add(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(0)))), tuples); + ExprValue result = + aggregation( + DSL.max( + DSL.add( + DSL.ref("integer_value", INTEGER), + DSL.literal(ExprValueUtils.integerValue(0)))), + tuples); assertEquals(4, result.value()); } @Test public void filtered_max() { - ExprValue result = aggregation(DSL.max(DSL.ref("integer_value", INTEGER)) - .condition(DSL.less(DSL.ref("integer_value", INTEGER), DSL.literal(4))), tuples); + ExprValue result = + aggregation( + DSL.max(DSL.ref("integer_value", INTEGER)) + .condition(DSL.less(DSL.ref("integer_value", INTEGER), DSL.literal(4))), + tuples); assertEquals(3, result.value()); } @@ -119,8 +125,10 @@ public void test_max_all_missing_or_null() { @Test public void test_value_of() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.max(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.max(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: max", exception.getMessage()); } @@ -132,9 +140,12 @@ public void test_to_string() { @Test public void test_nested_to_string() { - Aggregator maxAggregator = DSL.max(DSL.add(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))); - assertEquals(String.format("max(+(%s, %d))", DSL.ref("integer_value", INTEGER), 10), + Aggregator maxAggregator = + DSL.max( + DSL.add( + DSL.ref("integer_value", INTEGER), DSL.literal(ExprValueUtils.integerValue(10)))); + assertEquals( + String.format("max(+(%s, %d))", DSL.ref("integer_value", INTEGER), 10), maxAggregator.toString()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/MinAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/MinAggregatorTest.java index 1437f4dfda5..1aee0f3a6c0 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/MinAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/MinAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -83,16 +82,23 @@ public void test_min_timestamp() { @Test public void test_min_arithmetic_expression() { - ExprValue result = aggregation( - DSL.min(DSL.add(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(0)))), tuples); + ExprValue result = + aggregation( + DSL.min( + DSL.add( + DSL.ref("integer_value", INTEGER), + DSL.literal(ExprValueUtils.integerValue(0)))), + tuples); assertEquals(1, result.value()); } @Test public void filtered_min() { - ExprValue result = aggregation(DSL.min(DSL.ref("integer_value", INTEGER)) - .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), tuples); + ExprValue result = + aggregation( + DSL.min(DSL.ref("integer_value", INTEGER)) + .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), + tuples); assertEquals(2, result.value()); } @@ -119,8 +125,10 @@ public void test_min_all_missing_or_null() { @Test public void test_value_of() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.min(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.min(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: min", exception.getMessage()); } @@ -132,9 +140,12 @@ public void test_to_string() { @Test public void test_nested_to_string() { - Aggregator minAggregator = DSL.min(DSL.add(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))); - assertEquals(String.format("min(+(%s, %d))", DSL.ref("integer_value", INTEGER), 10), + Aggregator minAggregator = + DSL.min( + DSL.add( + DSL.ref("integer_value", INTEGER), DSL.literal(ExprValueUtils.integerValue(10)))); + assertEquals( + String.format("min(+(%s, %d))", DSL.ref("integer_value", INTEGER), 10), minAggregator.toString()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/StdDevAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/StdDevAggregatorTest.java index fe4923d4dfb..ceb76815dc0 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/StdDevAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/StdDevAggregatorTest.java @@ -40,14 +40,11 @@ @ExtendWith(MockitoExtension.class) public class StdDevAggregatorTest extends AggregationTest { - @Mock - Expression expression; + @Mock Expression expression; - @Mock - ExprValue tupleValue; + @Mock ExprValue tupleValue; - @Mock - BindingTuple tuple; + @Mock BindingTuple tuple; @Test public void stddev_sample_field_expression() { diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/SumAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/SumAggregatorTest.java index 676306041ec..eb5a18f248d 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/SumAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/SumAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -53,9 +52,13 @@ public void sum_double_field_expression() { @Test public void sum_arithmetic_expression() { - ExprValue result = aggregation(DSL.sum( - DSL.multiply(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))), tuples); + ExprValue result = + aggregation( + DSL.sum( + DSL.multiply( + DSL.ref("integer_value", INTEGER), + DSL.literal(ExprValueUtils.integerValue(10)))), + tuples); assertEquals(100, result.value()); } @@ -64,19 +67,23 @@ public void sum_string_field_expression() { SumAggregator sumAggregator = new SumAggregator(ImmutableList.of(DSL.ref("string_value", STRING)), ExprCoreType.STRING); SumState sumState = sumAggregator.create(); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> sumAggregator - .iterate( - ExprValueUtils.tupleValue(ImmutableMap.of("string_value", "m")).bindingTuples(), - sumState) - ); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> + sumAggregator.iterate( + ExprValueUtils.tupleValue(ImmutableMap.of("string_value", "m")).bindingTuples(), + sumState)); assertEquals("unexpected type [STRING] in sum aggregation", exception.getMessage()); } @Test public void filtered_sum() { - ExprValue result = aggregation(DSL.sum(DSL.ref("integer_value", INTEGER)) - .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), tuples); + ExprValue result = + aggregation( + DSL.sum(DSL.ref("integer_value", INTEGER)) + .condition(DSL.greater(DSL.ref("integer_value", INTEGER), DSL.literal(1))), + tuples); assertEquals(9, result.value()); } @@ -103,8 +110,10 @@ public void sum_with_all_missing_or_null() { @Test public void valueOf() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.sum(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.sum(DSL.ref("double_value", DOUBLE)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: sum", exception.getMessage()); } @@ -116,9 +125,12 @@ public void test_to_string() { @Test public void test_nested_to_string() { - Aggregator sumAggregator = DSL.sum(DSL.multiply(DSL.ref("integer_value", INTEGER), - DSL.literal(ExprValueUtils.integerValue(10)))); - assertEquals(String.format("sum(*(%s, %d))", DSL.ref("integer_value", INTEGER), 10), + Aggregator sumAggregator = + DSL.sum( + DSL.multiply( + DSL.ref("integer_value", INTEGER), DSL.literal(ExprValueUtils.integerValue(10)))); + assertEquals( + String.format("sum(*(%s, %d))", DSL.ref("integer_value", INTEGER), 10), sumAggregator.toString()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/aggregation/TakeAggregatorTest.java b/core/src/test/java/org/opensearch/sql/expression/aggregation/TakeAggregatorTest.java index 6d9aac4957b..2409ec49d25 100644 --- a/core/src/test/java/org/opensearch/sql/expression/aggregation/TakeAggregatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/aggregation/TakeAggregatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,15 +34,18 @@ public void take_string_field_expression_with_large_size() { @Test public void filtered_take() { ExprValue result = - aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)) - .condition(DSL.equal(DSL.ref("string_value", STRING), DSL.literal("m"))), tuples); + aggregation( + DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)) + .condition(DSL.equal(DSL.ref("string_value", STRING), DSL.literal("m"))), + tuples); assertEquals(ImmutableList.of("m", "m"), result.value()); } @Test public void test_take_null() { ExprValue result = - aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), + aggregation( + DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), tuples_with_null_and_missing); assertEquals(ImmutableList.of("m", "f"), result.value()); } @@ -51,7 +53,8 @@ public void test_take_null() { @Test public void test_take_missing() { ExprValue result = - aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), + aggregation( + DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), tuples_with_null_and_missing); assertEquals(ImmutableList.of("m", "f"), result.value()); } @@ -59,22 +62,27 @@ public void test_take_missing() { @Test public void test_take_all_missing_or_null() { ExprValue result = - aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), + aggregation( + DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)), tuples_with_all_null_or_missing); assertEquals(ImmutableList.of(), result.value()); } @Test public void test_take_with_invalid_size() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, - () -> aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(0)), tuples)); + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + () -> aggregation(DSL.take(DSL.ref("string_value", STRING), DSL.literal(0)), tuples)); assertEquals("size must be greater than 0", exception.getMessage()); } @Test public void test_value_of() { - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)).valueOf(valueEnv())); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.take(DSL.ref("string_value", STRING), DSL.literal(10)).valueOf(valueEnv())); assertEquals("can't evaluate on aggregator: take", exception.getMessage()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/conditional/ConditionalFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/conditional/ConditionalFunctionTest.java index ae8b714dd89..33654a71cfc 100644 --- a/core/src/test/java/org/opensearch/sql/expression/conditional/ConditionalFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/conditional/ConditionalFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.conditional; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,12 +28,9 @@ @ExtendWith(MockitoExtension.class) public class ConditionalFunctionTest extends ExpressionTestBase { - @Mock - Environment env; + @Mock Environment env; - /** - * Arguments for case test. - */ + /** Arguments for case test. */ private static Stream caseArguments() { Stream.Builder builder = Stream.builder(); return builder @@ -46,28 +42,25 @@ private static Stream caseArguments() { @ParameterizedTest(name = "case {0} when {1} then {2} when {3} then {4} else {5}") @MethodSource("caseArguments") - void case_value(int value, - int cond1, int result1, - int cond2, int result2, - int defaultVal) throws Exception { - Callable expect = () -> { - if (cond1 == value) { - return result1; - } else if (cond2 == value) { - return result2; - } else { - return defaultVal; - } - }; + void case_value(int value, int cond1, int result1, int cond2, int result2, int defaultVal) + throws Exception { + Callable expect = + () -> { + if (cond1 == value) { + return result1; + } else if (cond2 == value) { + return result2; + } else { + return defaultVal; + } + }; - Expression cases = DSL.cases( - DSL.literal(defaultVal), - DSL.when(DSL.equal(DSL.literal(cond1), DSL.literal(value)), DSL.literal(result1)), - DSL.when(DSL.equal(DSL.literal(cond2), DSL.literal(value)), DSL.literal(result2))); + Expression cases = + DSL.cases( + DSL.literal(defaultVal), + DSL.when(DSL.equal(DSL.literal(cond1), DSL.literal(value)), DSL.literal(result1)), + DSL.when(DSL.equal(DSL.literal(cond2), DSL.literal(value)), DSL.literal(result2))); - assertEquals( - new ExprIntegerValue(expect.call()), - cases.valueOf(env)); + assertEquals(new ExprIntegerValue(expect.call()), cases.valueOf(env)); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/conditional/cases/CaseClauseTest.java b/core/src/test/java/org/opensearch/sql/expression/conditional/cases/CaseClauseTest.java index 3c95c4f4614..61ff7ef022b 100644 --- a/core/src/test/java/org/opensearch/sql/expression/conditional/cases/CaseClauseTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/conditional/cases/CaseClauseTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.conditional.cases; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,8 +28,7 @@ @ExtendWith(MockitoExtension.class) class CaseClauseTest extends ExpressionTestBase { - @Mock - private WhenClause whenClause; + @Mock private WhenClause whenClause; @Test void should_return_when_clause_result_if_matched() { @@ -93,8 +91,7 @@ void should_return_all_result_types_including_default() { CaseClause caseClause = new CaseClause(ImmutableList.of(whenClause), defaultResult); assertEquals( - ImmutableList.of(ExprCoreType.INTEGER, ExprCoreType.STRING), - caseClause.allResultTypes()); + ImmutableList.of(ExprCoreType.INTEGER, ExprCoreType.STRING), caseClause.allResultTypes()); } @Test @@ -104,9 +101,6 @@ void should_return_all_result_types_excluding_null_result() { when(defaultResult.type()).thenReturn(ExprCoreType.UNDEFINED); CaseClause caseClause = new CaseClause(ImmutableList.of(whenClause), defaultResult); - assertEquals( - ImmutableList.of(), - caseClause.allResultTypes()); + assertEquals(ImmutableList.of(), caseClause.allResultTypes()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/conditional/cases/WhenClauseTest.java b/core/src/test/java/org/opensearch/sql/expression/conditional/cases/WhenClauseTest.java index a13f0725108..e03c8516948 100644 --- a/core/src/test/java/org/opensearch/sql/expression/conditional/cases/WhenClauseTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/conditional/cases/WhenClauseTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.conditional.cases; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -59,5 +58,4 @@ void should_use_result_expression_type() { WhenClause whenClause = new WhenClause(DSL.literal(true), DSL.literal(30)); assertEquals(ExprCoreType.INTEGER, whenClause.type()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/AddTimeAndSubTimeTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/AddTimeAndSubTimeTest.java index e917e2ee620..eed83f4fa9c 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/AddTimeAndSubTimeTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/AddTimeAndSubTimeTest.java @@ -74,38 +74,73 @@ public void time_limited_by_24_hours() { private static Stream getTestData() { return Stream.of( // DATETIME and TIME/DATE/DATETIME/TIMESTAMP - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalTime.of(1, 48), - LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)), - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDate.of(2000, 1, 1), - LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 12, 9, 7)), - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1235, 5, 6, 1, 48), - LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)), - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), Instant.ofEpochSecond(42), - LocalDateTime.of(1961, 4, 12, 9, 7, 42), LocalDateTime.of(1961, 4, 12, 9, 6, 18)), + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalTime.of(1, 48), + LocalDateTime.of(1961, 4, 12, 10, 55), + LocalDateTime.of(1961, 4, 12, 7, 19)), + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalDate.of(2000, 1, 1), + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalDateTime.of(1961, 4, 12, 9, 7)), + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalDateTime.of(1235, 5, 6, 1, 48), + LocalDateTime.of(1961, 4, 12, 10, 55), + LocalDateTime.of(1961, 4, 12, 7, 19)), + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7), + Instant.ofEpochSecond(42), + LocalDateTime.of(1961, 4, 12, 9, 7, 42), + LocalDateTime.of(1961, 4, 12, 9, 6, 18)), // DATE and TIME/DATE/DATETIME/TIMESTAMP - Arguments.of(LocalDate.of(1961, 4, 12), LocalTime.of(9, 7), - LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 11, 14, 53)), - Arguments.of(LocalDate.of(1961, 4, 12), LocalDate.of(2000, 1, 1), - LocalDateTime.of(1961, 4, 12, 0, 0), LocalDateTime.of(1961, 4, 12, 0, 0)), - Arguments.of(LocalDate.of(1961, 4, 12), LocalDateTime.of(1235, 5, 6, 1, 48), - LocalDateTime.of(1961, 4, 12, 1, 48), LocalDateTime.of(1961, 4, 11, 22, 12)), - Arguments.of(LocalDate.of(1961, 4, 12), Instant.ofEpochSecond(42), - LocalDateTime.of(1961, 4, 12, 0, 0, 42), LocalDateTime.of(1961, 4, 11, 23, 59, 18)), + Arguments.of( + LocalDate.of(1961, 4, 12), + LocalTime.of(9, 7), + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalDateTime.of(1961, 4, 11, 14, 53)), + Arguments.of( + LocalDate.of(1961, 4, 12), + LocalDate.of(2000, 1, 1), + LocalDateTime.of(1961, 4, 12, 0, 0), + LocalDateTime.of(1961, 4, 12, 0, 0)), + Arguments.of( + LocalDate.of(1961, 4, 12), + LocalDateTime.of(1235, 5, 6, 1, 48), + LocalDateTime.of(1961, 4, 12, 1, 48), + LocalDateTime.of(1961, 4, 11, 22, 12)), + Arguments.of( + LocalDate.of(1961, 4, 12), + Instant.ofEpochSecond(42), + LocalDateTime.of(1961, 4, 12, 0, 0, 42), + LocalDateTime.of(1961, 4, 11, 23, 59, 18)), // TIMESTAMP and TIME/DATE/DATETIME/TIMESTAMP - Arguments.of(Instant.ofEpochSecond(42), LocalTime.of(9, 7), - LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)), - Arguments.of(Instant.ofEpochSecond(42), LocalDate.of(1961, 4, 12), - LocalDateTime.of(1970, 1, 1, 0, 0, 42), LocalDateTime.of(1970, 1, 1, 0, 0, 42)), - Arguments.of(Instant.ofEpochSecond(42), LocalDateTime.of(1961, 4, 12, 9, 7), - LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)), - Arguments.of(Instant.ofEpochSecond(42), Instant.ofEpochMilli(42), + Arguments.of( + Instant.ofEpochSecond(42), + LocalTime.of(9, 7), + LocalDateTime.of(1970, 1, 1, 9, 7, 42), + LocalDateTime.of(1969, 12, 31, 14, 53, 42)), + Arguments.of( + Instant.ofEpochSecond(42), + LocalDate.of(1961, 4, 12), + LocalDateTime.of(1970, 1, 1, 0, 0, 42), + LocalDateTime.of(1970, 1, 1, 0, 0, 42)), + Arguments.of( + Instant.ofEpochSecond(42), + LocalDateTime.of(1961, 4, 12, 9, 7), + LocalDateTime.of(1970, 1, 1, 9, 7, 42), + LocalDateTime.of(1969, 12, 31, 14, 53, 42)), + Arguments.of( + Instant.ofEpochSecond(42), + Instant.ofEpochMilli(42), LocalDateTime.of(1970, 1, 1, 0, 0, 42, 42000000), - LocalDateTime.of(1970, 1, 1, 0, 0, 41, 958000000)) - ); + LocalDateTime.of(1970, 1, 1, 0, 0, 41, 958000000))); } /** * Check that `ADDTIME` and `SUBTIME` functions result value and type. + * * @param arg1 First argument. * @param arg2 Second argument. * @param addTimeExpectedResult Expected result for `ADDTIME`. @@ -113,9 +148,11 @@ private static Stream getTestData() { */ @ParameterizedTest @MethodSource("getTestData") - public void return_datetime_when_first_arg_is_not_time(Temporal arg1, Temporal arg2, - LocalDateTime addTimeExpectedResult, - LocalDateTime subTimeExpectedResult) { + public void return_datetime_when_first_arg_is_not_time( + Temporal arg1, + Temporal arg2, + LocalDateTime addTimeExpectedResult, + LocalDateTime subTimeExpectedResult) { var res = addtime(arg1, arg2); assertEquals(DATETIME, res.type()); assertEquals(addTimeExpectedResult, res.datetimeValue()); diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java index 7bc788c9a7c..17ff4f67aba 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -20,150 +19,162 @@ class ConvertTZTest extends ExpressionTestBase { @Test public void invalidDate() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2021-04-31 10:00:00")), - DSL.literal("+00:00"), - DSL.literal("+00:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2021-04-31 10:00:00")), + DSL.literal("+00:00"), + DSL.literal("+00:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void conversionFromNoOffset() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:00"), - DSL.literal("+10:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:00"), + DSL.literal("+10:00")); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue("2008-05-16 08:00:00"), expr.valueOf()); } @Test public void conversionToInvalidInput3Over() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:00"), - DSL.literal("+16:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:00"), + DSL.literal("+16:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void conversionToInvalidInput3Under() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:00"), - DSL.literal("-16:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:00"), + DSL.literal("-16:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void conversionFromPositiveToPositive() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+15:00"), - DSL.literal("+01:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+15:00"), + DSL.literal("+01:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidInput2Under() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("-15:00"), - DSL.literal("+01:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("-15:00"), + DSL.literal("+01:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidInput3Over() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("-12:00"), - DSL.literal("+15:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("-12:00"), + DSL.literal("+15:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void conversionToPositiveEdge() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:00"), - DSL.literal("+14:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:00"), + DSL.literal("+14:00")); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue("2008-05-16 12:00:00"), expr.valueOf()); } @Test public void conversionToNegativeEdge() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:01"), - DSL.literal("-13:59")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:01"), + DSL.literal("-13:59")); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue("2008-05-15 08:00:00"), expr.valueOf()); } @Test public void invalidInput2() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+)()"), - DSL.literal("+12:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+)()"), + DSL.literal("+12:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidInput3() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2008-05-15 22:00:00")), - DSL.literal("+00:00"), - DSL.literal("test")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2008-05-15 22:00:00")), + DSL.literal("+00:00"), + DSL.literal("test")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidInput1() { - FunctionExpression expr = DSL.convert_tz( - DSL.literal("test"), - DSL.literal("+00:00"), - DSL.literal("+00:00")); + FunctionExpression expr = + DSL.convert_tz(DSL.literal("test"), DSL.literal("+00:00"), DSL.literal("+00:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidDateFeb30() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2021-02-30 10:00:00")), - DSL.literal("+00:00"), - DSL.literal("+00:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2021-02-30 10:00:00")), + DSL.literal("+00:00"), + DSL.literal("+00:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidDateApril31() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2021-04-31 10:00:00")), - DSL.literal("+00:00"), - DSL.literal("+00:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2021-04-31 10:00:00")), + DSL.literal("+00:00"), + DSL.literal("+00:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void invalidMonth13() { - FunctionExpression expr = DSL.convert_tz(DSL.datetime( - DSL.literal("2021-13-03 10:00:00")), - DSL.literal("+00:00"), - DSL.literal("+00:00")); + FunctionExpression expr = + DSL.convert_tz( + DSL.datetime(DSL.literal("2021-13-03 10:00:00")), + DSL.literal("+00:00"), + DSL.literal("+00:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateAddAndAddDateTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateAddAndAddDateTest.java index 973b168355d..52db0a17e58 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateAddAndAddDateTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateAddAndAddDateTest.java @@ -35,8 +35,8 @@ public void adddate_returns_datetime_when_args_are_time_and_time_interval() { @Test public void date_add_returns_datetime_when_args_are_time_and_time_interval() { - var res = date_add(LocalTime.of(10, 20, 30), - Duration.ofHours(1).plusMinutes(2).plusSeconds(42)); + var res = + date_add(LocalTime.of(10, 20, 30), Duration.ofHours(1).plusMinutes(2).plusSeconds(42)); assertEquals(DATETIME, res.type()); assertEquals(LocalTime.of(11, 23, 12).atDate(today()), res.datetimeValue()); } @@ -50,8 +50,8 @@ public void adddate_time_limited_by_24_hours() { @Test public void date_add_time_limited_by_24_hours() { - var res = date_add(LocalTime.of(10, 20, 30), - Duration.ofHours(20).plusMinutes(50).plusSeconds(7)); + var res = + date_add(LocalTime.of(10, 20, 30), Duration.ofHours(20).plusMinutes(50).plusSeconds(7)); assertEquals(DATETIME, res.type()); assertEquals(LocalTime.of(7, 10, 37), res.datetimeValue().toLocalTime()); } @@ -108,8 +108,9 @@ public void adddate_returns_datetime_when_first_arg_is_datetime() { @Test public void date_add_returns_datetime_when_first_arg_is_timestamp() { - var res = date_add(LocalDateTime.of(1961, 4, 12, 9, 7).toInstant(ZoneOffset.UTC), - Duration.ofMinutes(108)); + var res = + date_add( + LocalDateTime.of(1961, 4, 12, 9, 7).toInstant(ZoneOffset.UTC), Duration.ofMinutes(108)); assertEquals(DATETIME, res.type()); assertEquals(LocalDateTime.of(1961, 4, 12, 10, 55), res.datetimeValue()); } @@ -127,10 +128,13 @@ public void adddate_has_second_signature_but_not_date_add() { var res = adddate(LocalDateTime.of(1961, 4, 12, 9, 7), 100500); assertEquals(DATETIME, res.type()); - var exception = assertThrows(ExpressionEvaluationException.class, - () -> date_add(LocalDateTime.of(1961, 4, 12, 9, 7), 100500)); - assertEquals("date_add function expected {[DATE,INTERVAL],[DATETIME,INTERVAL]," - + "[TIMESTAMP,INTERVAL],[TIME,INTERVAL]}, but get [DATETIME,INTEGER]", + var exception = + assertThrows( + ExpressionEvaluationException.class, + () -> date_add(LocalDateTime.of(1961, 4, 12, 9, 7), 100500)); + assertEquals( + "date_add function expected {[DATE,INTERVAL],[DATETIME,INTERVAL]," + + "[TIMESTAMP,INTERVAL],[TIME,INTERVAL]}, but get [DATETIME,INTEGER]", exception.getMessage()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateDiffTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateDiffTest.java index 72c1ceba03a..a6307584564 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateDiffTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateDiffTest.java @@ -40,33 +40,26 @@ private static Stream getTestData() { Arguments.of(timeSample1, timeSample2, 0L), Arguments.of(timeSample1, dateNow, 0L), Arguments.of(timeSample1, LocalDateTime.now(), 0L), - Arguments.of(timeSample1, - Instant.now().plusMillis(TimeZone.getDefault().getRawOffset()), 0L), - Arguments.of(dateSample1, timeSample1, - -DAYS.between(dateSample1, dateNow)), - Arguments.of(dateSample1, dateSample3, - -DAYS.between(dateSample1, dateSample3)), - Arguments.of(dateSample1, dateTimeSample1, - -DAYS.between(dateSample1, dateSample2)), - Arguments.of(dateSample1, Instant.ofEpochSecond(42), - -DAYS.between(dateSample1, epochStart)), - Arguments.of(dateTimeSample1, LocalTime.now(), - -DAYS.between(dateSample2, dateNow)), - Arguments.of(dateTimeSample1, dateSample3, - -DAYS.between(dateSample2, dateSample3)), - Arguments.of(dateTimeSample1, dateTimeSample2, - -DAYS.between(dateSample2, dateSample3)), - Arguments.of(dateTimeSample1, Instant.ofEpochSecond(0), - -DAYS.between(dateSample2, epochStart)), - Arguments.of(Instant.ofEpochSecond(0), LocalTime.MAX, - -DAYS.between(epochStart, dateNow)), - Arguments.of(Instant.ofEpochSecond(0), dateSample3, - -DAYS.between(epochStart, dateSample3)), - Arguments.of(Instant.ofEpochSecond(0), dateTimeSample2, - -DAYS.between(epochStart, dateSample3)), - Arguments.of(Instant.ofEpochSecond(0), Instant.now(), - -DAYS.between(epochStart, LocalDateTime.now(ZoneId.of("UTC")))) - ); + Arguments.of( + timeSample1, Instant.now().plusMillis(TimeZone.getDefault().getRawOffset()), 0L), + Arguments.of(dateSample1, timeSample1, -DAYS.between(dateSample1, dateNow)), + Arguments.of(dateSample1, dateSample3, -DAYS.between(dateSample1, dateSample3)), + Arguments.of(dateSample1, dateTimeSample1, -DAYS.between(dateSample1, dateSample2)), + Arguments.of( + dateSample1, Instant.ofEpochSecond(42), -DAYS.between(dateSample1, epochStart)), + Arguments.of(dateTimeSample1, LocalTime.now(), -DAYS.between(dateSample2, dateNow)), + Arguments.of(dateTimeSample1, dateSample3, -DAYS.between(dateSample2, dateSample3)), + Arguments.of(dateTimeSample1, dateTimeSample2, -DAYS.between(dateSample2, dateSample3)), + Arguments.of( + dateTimeSample1, Instant.ofEpochSecond(0), -DAYS.between(dateSample2, epochStart)), + Arguments.of(Instant.ofEpochSecond(0), LocalTime.MAX, -DAYS.between(epochStart, dateNow)), + Arguments.of(Instant.ofEpochSecond(0), dateSample3, -DAYS.between(epochStart, dateSample3)), + Arguments.of( + Instant.ofEpochSecond(0), dateTimeSample2, -DAYS.between(epochStart, dateSample3)), + Arguments.of( + Instant.ofEpochSecond(0), + Instant.now(), + -DAYS.between(epochStart, LocalDateTime.now(ZoneId.of("UTC"))))); } @ParameterizedTest diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateSubAndSubDateTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateSubAndSubDateTest.java index 37c62313db3..460e12384b2 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateSubAndSubDateTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateSubAndSubDateTest.java @@ -35,8 +35,8 @@ public void subdate_returns_datetime_when_args_are_time_and_time_interval() { @Test public void date_sub_returns_datetime_when_args_are_time_and_time_interval() { - var res = date_sub(LocalTime.of(10, 20, 30), - Duration.ofHours(1).plusMinutes(2).plusSeconds(42)); + var res = + date_sub(LocalTime.of(10, 20, 30), Duration.ofHours(1).plusMinutes(2).plusSeconds(42)); assertEquals(DATETIME, res.type()); assertEquals(LocalTime.of(9, 17, 48).atDate(today()), res.datetimeValue()); } @@ -50,8 +50,8 @@ public void subdate_time_limited_by_24_hours() { @Test public void date_sub_time_limited_by_24_hours() { - var res = date_sub(LocalTime.of(10, 20, 30), - Duration.ofHours(20).plusMinutes(50).plusSeconds(7)); + var res = + date_sub(LocalTime.of(10, 20, 30), Duration.ofHours(20).plusMinutes(50).plusSeconds(7)); assertEquals(DATETIME, res.type()); assertEquals(LocalTime.of(13, 30, 23), res.datetimeValue().toLocalTime()); } @@ -108,8 +108,9 @@ public void subdate_returns_datetime_when_first_arg_is_datetime() { @Test public void date_sub_returns_datetime_when_first_arg_is_timestamp() { - var res = date_sub(LocalDateTime.of(1961, 4, 12, 9, 7).toInstant(ZoneOffset.UTC), - Duration.ofMinutes(108)); + var res = + date_sub( + LocalDateTime.of(1961, 4, 12, 9, 7).toInstant(ZoneOffset.UTC), Duration.ofMinutes(108)); assertEquals(DATETIME, res.type()); assertEquals(LocalDateTime.of(1961, 4, 12, 7, 19), res.datetimeValue()); } @@ -127,10 +128,13 @@ public void subdate_has_second_signature_but_not_date_sub() { var res = subdate(LocalDateTime.of(1961, 4, 12, 9, 7), 100500); assertEquals(DATETIME, res.type()); - var exception = assertThrows(ExpressionEvaluationException.class, - () -> date_sub(LocalDateTime.of(1961, 4, 12, 9, 7), 100500)); - assertEquals("date_sub function expected {[DATE,INTERVAL],[DATETIME,INTERVAL]," - + "[TIMESTAMP,INTERVAL],[TIME,INTERVAL]}, but get [DATETIME,INTEGER]", + var exception = + assertThrows( + ExpressionEvaluationException.class, + () -> date_sub(LocalDateTime.of(1961, 4, 12, 9, 7), 100500)); + assertEquals( + "date_sub function expected {[DATE,INTERVAL],[DATETIME,INTERVAL]," + + "[TIMESTAMP,INTERVAL],[TIME,INTERVAL]}, but get [DATETIME,INTEGER]", exception.getMessage()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java index 2f650eeda3c..c2a61296260 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; @@ -51,79 +50,81 @@ class DateTimeFunctionTest extends ExpressionTestBase { - final List dateFormatTesters = ImmutableList.of( - new DateFormatTester("1998-01-31 13:14:15.012345", - ImmutableList.of("%H","%I","%k","%l","%i","%p","%r","%S","%T"," %M", - "%W","%D","%Y","%y","%a","%b","%j","%m","%d","%h","%s","%w","%f", - "%q","%"), - ImmutableList.of("13","01","13","1","14","PM","01:14:15 PM","15","13:14:15"," January", - "Saturday","31st","1998","98","Sat","Jan","031","01","31","01","15","6","012345", - "q","%") - ), - new DateFormatTester("1999-12-01", - ImmutableList.of("%D"), - ImmutableList.of("1st") - ), - new DateFormatTester("1999-12-02", - ImmutableList.of("%D"), - ImmutableList.of("2nd") - ), - new DateFormatTester("1999-12-03", - ImmutableList.of("%D"), - ImmutableList.of("3rd") - ), - new DateFormatTester("1999-12-04", - ImmutableList.of("%D"), - ImmutableList.of("4th") - ), - new DateFormatTester("1999-12-11", - ImmutableList.of("%D"), - ImmutableList.of("11th") - ), - new DateFormatTester("1999-12-12", - ImmutableList.of("%D"), - ImmutableList.of("12th") - ), - new DateFormatTester("1999-12-13", - ImmutableList.of("%D"), - ImmutableList.of("13th") - ), - new DateFormatTester("1999-12-31", - ImmutableList.of("%x","%v","%X","%V","%u","%U"), - ImmutableList.of("1999", "52", "1999", "52", "52", "52") - ), - new DateFormatTester("2000-01-01", - ImmutableList.of("%x","%v","%X","%V","%u","%U"), - ImmutableList.of("1999", "52", "1999", "52", "0", "0") - ), - new DateFormatTester("1998-12-31", - ImmutableList.of("%x","%v","%X","%V","%u","%U"), - ImmutableList.of("1998", "52", "1998", "52", "52", "52") - ), - new DateFormatTester("1999-01-01", - ImmutableList.of("%x","%v","%X","%V","%u","%U"), - ImmutableList.of("1998", "52", "1998", "52", "0", "0") - ), - new DateFormatTester("2020-01-04", - ImmutableList.of("%x","%X"), - ImmutableList.of("2020", "2019") - ), - new DateFormatTester("2008-12-31", - ImmutableList.of("%v","%V","%u","%U"), - ImmutableList.of("53","52","53","52") - ), - new DateFormatTester("1998-01-31 13:14:15.012345", - ImmutableList.of("%Y-%m-%dT%TZ"), - ImmutableList.of("1998-01-31T13:14:15Z") - ), - new DateFormatTester("1998-01-31 13:14:15.012345", - ImmutableList.of("%Y-%m-%da %T a"), - ImmutableList.of("1998-01-31PM 13:14:15 PM") - ), - new DateFormatTester("1998-01-31 13:14:15.012345", - ImmutableList.of("%Y-%m-%db %T b"), - ImmutableList.of("1998-01-31b 13:14:15 b")) - ); + final List dateFormatTesters = + ImmutableList.of( + new DateFormatTester( + "1998-01-31 13:14:15.012345", + ImmutableList.of( + "%H", + "%I", "%k", "%l", "%i", "%p", "%r", "%S", "%T", " %M", "%W", "%D", "%Y", "%y", + "%a", "%b", "%j", "%m", "%d", "%h", "%s", "%w", "%f", "%q", "%"), + ImmutableList.of( + "13", + "01", + "13", + "1", + "14", + "PM", + "01:14:15 PM", + "15", + "13:14:15", + " January", + "Saturday", + "31st", + "1998", + "98", + "Sat", + "Jan", + "031", + "01", + "31", + "01", + "15", + "6", + "012345", + "q", + "%")), + new DateFormatTester("1999-12-01", ImmutableList.of("%D"), ImmutableList.of("1st")), + new DateFormatTester("1999-12-02", ImmutableList.of("%D"), ImmutableList.of("2nd")), + new DateFormatTester("1999-12-03", ImmutableList.of("%D"), ImmutableList.of("3rd")), + new DateFormatTester("1999-12-04", ImmutableList.of("%D"), ImmutableList.of("4th")), + new DateFormatTester("1999-12-11", ImmutableList.of("%D"), ImmutableList.of("11th")), + new DateFormatTester("1999-12-12", ImmutableList.of("%D"), ImmutableList.of("12th")), + new DateFormatTester("1999-12-13", ImmutableList.of("%D"), ImmutableList.of("13th")), + new DateFormatTester( + "1999-12-31", + ImmutableList.of("%x", "%v", "%X", "%V", "%u", "%U"), + ImmutableList.of("1999", "52", "1999", "52", "52", "52")), + new DateFormatTester( + "2000-01-01", + ImmutableList.of("%x", "%v", "%X", "%V", "%u", "%U"), + ImmutableList.of("1999", "52", "1999", "52", "0", "0")), + new DateFormatTester( + "1998-12-31", + ImmutableList.of("%x", "%v", "%X", "%V", "%u", "%U"), + ImmutableList.of("1998", "52", "1998", "52", "52", "52")), + new DateFormatTester( + "1999-01-01", + ImmutableList.of("%x", "%v", "%X", "%V", "%u", "%U"), + ImmutableList.of("1998", "52", "1998", "52", "0", "0")), + new DateFormatTester( + "2020-01-04", ImmutableList.of("%x", "%X"), ImmutableList.of("2020", "2019")), + new DateFormatTester( + "2008-12-31", + ImmutableList.of("%v", "%V", "%u", "%U"), + ImmutableList.of("53", "52", "53", "52")), + new DateFormatTester( + "1998-01-31 13:14:15.012345", + ImmutableList.of("%Y-%m-%dT%TZ"), + ImmutableList.of("1998-01-31T13:14:15Z")), + new DateFormatTester( + "1998-01-31 13:14:15.012345", + ImmutableList.of("%Y-%m-%da %T a"), + ImmutableList.of("1998-01-31PM 13:14:15 PM")), + new DateFormatTester( + "1998-01-31 13:14:15.012345", + ImmutableList.of("%Y-%m-%db %T b"), + ImmutableList.of("1998-01-31b 13:14:15 b"))); @AllArgsConstructor private class DateFormatTester { @@ -196,8 +197,8 @@ public void dayName() { @Test public void dayOfMonth() { - FunctionExpression expression = DSL.dayofmonth( - functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression = + DSL.dayofmonth(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); assertEquals(INTEGER, expression.type()); assertEquals("dayofmonth(DATE '2020-08-07')", expression.toString()); assertEquals(integerValue(7), eval(expression)); @@ -215,23 +216,21 @@ private void testDayOfMonthWithUnderscores(FunctionExpression dateExpression, in @Test public void dayOfMonthWithUnderscores() { - FunctionExpression expression1 = DSL.dayofmonth( - functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression1 = + DSL.dayofmonth(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); FunctionExpression expression2 = DSL.dayofmonth(functionProperties, DSL.literal("2020-07-08")); assertAll( () -> testDayOfMonthWithUnderscores(expression1, 7), () -> assertEquals("dayofmonth(DATE '2020-08-07')", expression1.toString()), - () -> testDayOfMonthWithUnderscores(expression2, 8), - () -> assertEquals("dayofmonth(\"2020-07-08\")", expression2.toString()) - ); + () -> assertEquals("dayofmonth(\"2020-07-08\")", expression2.toString())); } @Test public void testDayOfMonthWithTimeType() { - FunctionExpression expression = DSL.day_of_month( - functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); + FunctionExpression expression = + DSL.day_of_month(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); assertEquals(INTEGER, eval(expression).type()); assertEquals( @@ -241,40 +240,35 @@ public void testDayOfMonthWithTimeType() { } private void testInvalidDayOfMonth(String date) { - FunctionExpression expression = DSL.day_of_month( - functionProperties, DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.day_of_month(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void dayOfMonthWithUnderscoresLeapYear() { - //Feb. 29 of a leap year - testDayOfMonthWithUnderscores(DSL.day_of_month( - functionProperties, DSL.literal("2020-02-29")), 29); + // Feb. 29 of a leap year + testDayOfMonthWithUnderscores( + DSL.day_of_month(functionProperties, DSL.literal("2020-02-29")), 29); - //Feb. 29 of a non-leap year + // Feb. 29 of a non-leap year assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-02-29")); } @Test public void dayOfMonthWithUnderscoresInvalidArguments() { assertAll( - //40th day of the month - () -> assertThrows( - SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-02-40")), - //13th month of the year - () -> assertThrows( - SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-13-40")), - //incorrect format - () -> assertThrows( - SemanticCheckException.class, () -> testInvalidDayOfMonth("asdfasdfasdf")) - ); - } - - private void dayOfWeekQuery( - FunctionExpression dateExpression, - int dayOfWeek, - String testExpr) { + // 40th day of the month + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-02-40")), + // 13th month of the year + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-13-40")), + // incorrect format + () -> + assertThrows( + SemanticCheckException.class, () -> testInvalidDayOfMonth("asdfasdfasdf"))); + } + + private void dayOfWeekQuery(FunctionExpression dateExpression, int dayOfWeek, String testExpr) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(dayOfWeek), eval(dateExpression)); assertEquals(testExpr, dateExpression.toString()); @@ -282,34 +276,23 @@ private void dayOfWeekQuery( @Test public void dayOfWeek() { - FunctionExpression expression1 = DSL.dayofweek( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-07"))); - FunctionExpression expression2 = DSL.dayofweek( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-09"))); - FunctionExpression expression3 = DSL.dayofweek( - functionProperties, - DSL.literal("2020-08-09")); - FunctionExpression expression4 = DSL.dayofweek( - functionProperties, - DSL.literal("2020-08-09 01:02:03")); + FunctionExpression expression1 = + DSL.dayofweek(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = + DSL.dayofweek(functionProperties, DSL.literal(new ExprDateValue("2020-08-09"))); + FunctionExpression expression3 = DSL.dayofweek(functionProperties, DSL.literal("2020-08-09")); + FunctionExpression expression4 = + DSL.dayofweek(functionProperties, DSL.literal("2020-08-09 01:02:03")); assertAll( () -> dayOfWeekQuery(expression1, 6, "dayofweek(DATE '2020-08-07')"), - () -> dayOfWeekQuery(expression2, 1, "dayofweek(DATE '2020-08-09')"), - () -> dayOfWeekQuery(expression3, 1, "dayofweek(\"2020-08-09\")"), - - () -> dayOfWeekQuery(expression4, 1, "dayofweek(\"2020-08-09 01:02:03\")") - ); + () -> dayOfWeekQuery(expression4, 1, "dayofweek(\"2020-08-09 01:02:03\")")); } private void dayOfWeekWithUnderscoresQuery( - FunctionExpression dateExpression, - int dayOfWeek, - String testExpr) { + FunctionExpression dateExpression, int dayOfWeek, String testExpr) { assertEquals(INTEGER, dateExpression.type()); assertEquals(integerValue(dayOfWeek), eval(dateExpression)); assertEquals(testExpr, dateExpression.toString()); @@ -317,88 +300,80 @@ private void dayOfWeekWithUnderscoresQuery( @Test public void dayOfWeekWithUnderscores() { - FunctionExpression expression1 = DSL.day_of_week( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-07"))); - FunctionExpression expression2 = DSL.day_of_week( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-09"))); - FunctionExpression expression3 = DSL.day_of_week( - functionProperties, - DSL.literal("2020-08-09")); - FunctionExpression expression4 = DSL.day_of_week( - functionProperties, - DSL.literal("2020-08-09 01:02:03")); + FunctionExpression expression1 = + DSL.day_of_week(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = + DSL.day_of_week(functionProperties, DSL.literal(new ExprDateValue("2020-08-09"))); + FunctionExpression expression3 = DSL.day_of_week(functionProperties, DSL.literal("2020-08-09")); + FunctionExpression expression4 = + DSL.day_of_week(functionProperties, DSL.literal("2020-08-09 01:02:03")); assertAll( () -> dayOfWeekWithUnderscoresQuery(expression1, 6, "day_of_week(DATE '2020-08-07')"), - () -> dayOfWeekWithUnderscoresQuery(expression2, 1, "day_of_week(DATE '2020-08-09')"), - () -> dayOfWeekWithUnderscoresQuery(expression3, 1, "day_of_week(\"2020-08-09\")"), - - () -> dayOfWeekWithUnderscoresQuery( - expression4, 1, "day_of_week(\"2020-08-09 01:02:03\")") - ); + () -> + dayOfWeekWithUnderscoresQuery(expression4, 1, "day_of_week(\"2020-08-09 01:02:03\")")); } @Test public void testDayOfWeekWithTimeType() { - FunctionExpression expression = DSL.day_of_week( - functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); + FunctionExpression expression = + DSL.day_of_week(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); assertAll( () -> assertEquals(INTEGER, eval(expression).type()), - () -> assertEquals(( - LocalDate.now( - functionProperties.getQueryStartClock()).getDayOfWeek().getValue() % 7) + 1, - eval(expression).integerValue()), - () -> assertEquals("day_of_week(TIME '12:23:34')", expression.toString()) - ); + () -> + assertEquals( + (LocalDate.now(functionProperties.getQueryStartClock()).getDayOfWeek().getValue() + % 7) + + 1, + eval(expression).integerValue()), + () -> assertEquals("day_of_week(TIME '12:23:34')", expression.toString())); } private void testInvalidDayOfWeek(String date) { - FunctionExpression expression = DSL.day_of_week( - functionProperties, DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.day_of_week(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void dayOfWeekWithUnderscoresLeapYear() { assertAll( - //Feb. 29 of a leap year - () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( - functionProperties, - DSL.literal("2020-02-29")), 7, "day_of_week(\"2020-02-29\")"), - //day after Feb. 29 of a leap year - () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( - functionProperties, - DSL.literal("2020-03-01")), 1, "day_of_week(\"2020-03-01\")"), - //Feb. 28 of a non-leap year - () -> dayOfWeekWithUnderscoresQuery(DSL.day_of_week( - functionProperties, - DSL.literal("2021-02-28")), 1, "day_of_week(\"2021-02-28\")"), - //Feb. 29 of a non-leap year - () -> assertThrows( - SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29")) - ); + // Feb. 29 of a leap year + () -> + dayOfWeekWithUnderscoresQuery( + DSL.day_of_week(functionProperties, DSL.literal("2020-02-29")), + 7, + "day_of_week(\"2020-02-29\")"), + // day after Feb. 29 of a leap year + () -> + dayOfWeekWithUnderscoresQuery( + DSL.day_of_week(functionProperties, DSL.literal("2020-03-01")), + 1, + "day_of_week(\"2020-03-01\")"), + // Feb. 28 of a non-leap year + () -> + dayOfWeekWithUnderscoresQuery( + DSL.day_of_week(functionProperties, DSL.literal("2021-02-28")), + 1, + "day_of_week(\"2021-02-28\")"), + // Feb. 29 of a non-leap year + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-29"))); } @Test public void dayOfWeekWithUnderscoresInvalidArgument() { assertAll( - //40th day of the month - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidDayOfWeek("2021-02-40")), + // 40th day of the month + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-02-40")), - //13th month of the year - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidDayOfWeek("2021-13-29")), + // 13th month of the year + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("2021-13-29")), - //incorrect format - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidDayOfWeek("asdfasdf")) - ); + // incorrect format + () -> assertThrows(SemanticCheckException.class, () -> testInvalidDayOfWeek("asdfasdf"))); } @Test @@ -421,40 +396,26 @@ public void dayOfYear() { private static Stream getTestDataForDayOfYear() { return Stream.of( - Arguments.of(DSL.literal( - new ExprDateValue("2020-08-07")), - "day_of_year(DATE '2020-08-07')", - 220), - Arguments.of(DSL.literal( - new ExprDatetimeValue("2020-08-07 12:23:34")), + Arguments.of( + DSL.literal(new ExprDateValue("2020-08-07")), "day_of_year(DATE '2020-08-07')", 220), + Arguments.of( + DSL.literal(new ExprDatetimeValue("2020-08-07 12:23:34")), "day_of_year(DATETIME '2020-08-07 12:23:34')", 220), - Arguments.of(DSL.literal( - new ExprTimestampValue("2020-08-07 12:23:34")), + Arguments.of( + DSL.literal(new ExprTimestampValue("2020-08-07 12:23:34")), "day_of_year(TIMESTAMP '2020-08-07 12:23:34')", 220), - Arguments.of(DSL.literal( - "2020-08-07"), - "day_of_year(\"2020-08-07\")", - 220), - Arguments.of(DSL.literal( - "2020-08-07 01:02:03"), - "day_of_year(\"2020-08-07 01:02:03\")", - 220) - ); + Arguments.of(DSL.literal("2020-08-07"), "day_of_year(\"2020-08-07\")", 220), + Arguments.of( + DSL.literal("2020-08-07 01:02:03"), "day_of_year(\"2020-08-07 01:02:03\")", 220)); } @ParameterizedTest(name = "{0}") @MethodSource("getTestDataForDayOfYear") public void dayOfYearWithUnderscores( - LiteralExpression arg, - String expectedString, - int expectedResult) { - validateStringFormat( - DSL.day_of_year(functionProperties, arg), - expectedString, - expectedResult - ); + LiteralExpression arg, String expectedString, int expectedResult) { + validateStringFormat(DSL.day_of_year(functionProperties, arg), expectedString, expectedResult); } @Test @@ -466,100 +427,83 @@ public void testDayOfYearWithTimeType() { } public void dayOfYearWithUnderscoresQuery(String date, int dayOfYear) { - FunctionExpression expression = DSL.day_of_year( - functionProperties, - DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.day_of_year(functionProperties, DSL.literal(new ExprDateValue(date))); assertAll( () -> assertEquals(INTEGER, expression.type()), - () -> assertEquals(integerValue(dayOfYear), eval(expression)) - ); + () -> assertEquals(integerValue(dayOfYear), eval(expression))); } @Test public void dayOfYearWithUnderscoresDifferentArgumentFormats() { - FunctionExpression expression1 = DSL.day_of_year( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-07"))); - FunctionExpression expression2 = DSL.day_of_year( - functionProperties, - DSL.literal("2020-08-07")); - FunctionExpression expression3 = DSL.day_of_year( - functionProperties, - DSL.literal("2020-08-07 01:02:03")); + FunctionExpression expression1 = + DSL.day_of_year(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression2 = DSL.day_of_year(functionProperties, DSL.literal("2020-08-07")); + FunctionExpression expression3 = + DSL.day_of_year(functionProperties, DSL.literal("2020-08-07 01:02:03")); assertAll( () -> dayOfYearWithUnderscoresQuery("2020-08-07", 220), () -> assertEquals("day_of_year(DATE '2020-08-07')", expression1.toString()), - () -> dayOfYearWithUnderscoresQuery("2020-08-07", 220), - () -> assertEquals("day_of_year(\"2020-08-07\")", expression2.toString()), - + () -> assertEquals("day_of_year(\"2020-08-07\")", expression2.toString()), () -> dayOfYearWithUnderscoresQuery("2020-08-07 01:02:03", 220), - () -> assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression3.toString()) - ); + () -> assertEquals("day_of_year(\"2020-08-07 01:02:03\")", expression3.toString())); } @Test public void dayOfYearWithUnderscoresCornerCaseDates() { assertAll( - //31st of December during non leap year (should be 365) + // 31st of December during non leap year (should be 365) () -> dayOfYearWithUnderscoresQuery("2019-12-31", 365), - //Year 1200 + // Year 1200 () -> dayOfYearWithUnderscoresQuery("1200-02-28", 59), - //Year 4000 - () -> dayOfYearWithUnderscoresQuery("4000-02-28", 59) - ); + // Year 4000 + () -> dayOfYearWithUnderscoresQuery("4000-02-28", 59)); } @Test public void dayOfYearWithUnderscoresLeapYear() { assertAll( - //28th of Feb + // 28th of Feb () -> dayOfYearWithUnderscoresQuery("2020-02-28", 59), - //29th of Feb during leap year + // 29th of Feb during leap year () -> dayOfYearWithUnderscoresQuery("2020-02-29 23:59:59", 60), () -> dayOfYearWithUnderscoresQuery("2020-02-29", 60), - //1st of March during leap year + // 1st of March during leap year () -> dayOfYearWithUnderscoresQuery("2020-03-01 00:00:00", 61), () -> dayOfYearWithUnderscoresQuery("2020-03-01", 61), - //1st of March during non leap year + // 1st of March during non leap year () -> dayOfYearWithUnderscoresQuery("2019-03-01", 60), - //31st of December during leap year (should be 366) - () -> dayOfYearWithUnderscoresQuery("2020-12-31", 366) - ); + // 31st of December during leap year (should be 366) + () -> dayOfYearWithUnderscoresQuery("2020-12-31", 366)); } private void invalidDayOfYearQuery(String date) { - FunctionExpression expression = DSL.day_of_year( - functionProperties, - DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.day_of_year(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void invalidDayOfYearArgument() { assertAll( - //29th of Feb non-leapyear - () -> assertThrows( - SemanticCheckException.class, - () -> invalidDayOfYearQuery("2019-02-29")), - - //13th month - () -> assertThrows( - SemanticCheckException.class, - () -> invalidDayOfYearQuery("2019-13-15")), - - //incorrect format for type - () -> assertThrows( - SemanticCheckException.class, - () -> invalidDayOfYearQuery("asdfasdfasdf")) - ); - } - + // 29th of Feb non-leapyear + () -> assertThrows(SemanticCheckException.class, () -> invalidDayOfYearQuery("2019-02-29")), + + // 13th month + () -> assertThrows(SemanticCheckException.class, () -> invalidDayOfYearQuery("2019-13-15")), + + // incorrect format for type + () -> + assertThrows( + SemanticCheckException.class, () -> invalidDayOfYearQuery("asdfasdfasdf"))); + } + @Test public void from_days() { FunctionExpression expression = DSL.from_days(DSL.literal(new ExprLongValue(730669))); @@ -589,13 +533,11 @@ private static Stream getTestDataForGetFormat() { Arguments.of("TIMESTAMP", "JIS", "%Y-%m-%d %H:%i:%s"), Arguments.of("TIMESTAMP", "ISO", "%Y-%m-%d %H:%i:%s"), Arguments.of("TIMESTAMP", "EUR", "%Y-%m-%d %H.%i.%s"), - Arguments.of("TIMESTAMP", "INTERNAL", "%Y%m%d%H%i%s") - ); + Arguments.of("TIMESTAMP", "INTERNAL", "%Y%m%d%H%i%s")); } - private void getFormatQuery(LiteralExpression argType, - LiteralExpression namedFormat, - String expectedResult) { + private void getFormatQuery( + LiteralExpression argType, LiteralExpression namedFormat, String expectedResult) { FunctionExpression expr = DSL.get_format(argType, namedFormat); assertEquals(STRING, expr.type()); assertEquals(expectedResult, eval(expr).stringValue()); @@ -603,20 +545,13 @@ private void getFormatQuery(LiteralExpression argType, @ParameterizedTest(name = "{0}{1}") @MethodSource("getTestDataForGetFormat") - public void testGetFormat(String arg, - String format, - String expectedResult) { - getFormatQuery( - DSL.literal(arg), - DSL.literal(new ExprStringValue(format)), - expectedResult); + public void testGetFormat(String arg, String format, String expectedResult) { + getFormatQuery(DSL.literal(arg), DSL.literal(new ExprStringValue(format)), expectedResult); } @Test public void testGetFormatInvalidFormat() { - FunctionExpression expr = DSL.get_format( - DSL.literal("DATE"), - DSL.literal("1SA")); + FunctionExpression expr = DSL.get_format(DSL.literal("DATE"), DSL.literal("1SA")); assertEquals(nullValue(), eval(expr)); } @@ -655,25 +590,20 @@ private void testInvalidMinuteOfDay(String date) { @Test public void invalidMinuteOfDay() { - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("2022-12-14 12:23:3400")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("2022-12-14 12:2300:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("2022-12-14 1200:23:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("2022-12-1400 12:23:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("2022-1200-14 12:23:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("12:23:3400")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("12:2300:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("1200:23:34")); - assertThrows(SemanticCheckException.class, - () -> testInvalidMinuteOfDay("asdfasdfasdf")); - + assertThrows( + SemanticCheckException.class, () -> testInvalidMinuteOfDay("2022-12-14 12:23:3400")); + assertThrows( + SemanticCheckException.class, () -> testInvalidMinuteOfDay("2022-12-14 12:2300:34")); + assertThrows( + SemanticCheckException.class, () -> testInvalidMinuteOfDay("2022-12-14 1200:23:34")); + assertThrows( + SemanticCheckException.class, () -> testInvalidMinuteOfDay("2022-12-1400 12:23:34")); + assertThrows( + SemanticCheckException.class, () -> testInvalidMinuteOfDay("2022-1200-14 12:23:34")); + assertThrows(SemanticCheckException.class, () -> testInvalidMinuteOfDay("12:23:3400")); + assertThrows(SemanticCheckException.class, () -> testInvalidMinuteOfDay("12:2300:34")); + assertThrows(SemanticCheckException.class, () -> testInvalidMinuteOfDay("1200:23:34")); + assertThrows(SemanticCheckException.class, () -> testInvalidMinuteOfDay("asdfasdfasdf")); } private void hourOfDayQuery(FunctionExpression dateExpression, int hour) { @@ -685,28 +615,23 @@ private void hourOfDayQuery(FunctionExpression dateExpression, int hour) { public void hourOfDay() { FunctionExpression expression1 = DSL.hour_of_day(DSL.literal(new ExprTimeValue("01:02:03"))); FunctionExpression expression2 = DSL.hour_of_day(DSL.literal("01:02:03")); - FunctionExpression expression3 = DSL.hour_of_day( - DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); - FunctionExpression expression4 = DSL.hour_of_day( - DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); + FunctionExpression expression3 = + DSL.hour_of_day(DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03"))); + FunctionExpression expression4 = + DSL.hour_of_day(DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03"))); FunctionExpression expression5 = DSL.hour_of_day(DSL.literal("2020-08-17 01:02:03")); assertAll( () -> hourOfDayQuery(expression1, 1), () -> assertEquals("hour_of_day(TIME '01:02:03')", expression1.toString()), - () -> hourOfDayQuery(expression2, 1), () -> assertEquals("hour_of_day(\"01:02:03\")", expression2.toString()), - () -> hourOfDayQuery(expression3, 1), () -> assertEquals("hour_of_day(TIMESTAMP '2020-08-17 01:02:03')", expression3.toString()), - () -> hourOfDayQuery(expression4, 1), () -> assertEquals("hour_of_day(DATETIME '2020-08-17 01:02:03')", expression4.toString()), - () -> hourOfDayQuery(expression5, 1), - () -> assertEquals("hour_of_day(\"2020-08-17 01:02:03\")", expression5.toString()) - ); + () -> assertEquals("hour_of_day(\"2020-08-17 01:02:03\")", expression5.toString())); } private void invalidHourOfDayQuery(String time) { @@ -717,24 +642,20 @@ private void invalidHourOfDayQuery(String time) { @Test public void hourOfDayInvalidArguments() { assertAll( - //Invalid Seconds + // Invalid Seconds () -> assertThrows(SemanticCheckException.class, () -> invalidHourOfDayQuery("12:23:61")), - //Invalid Minutes + // Invalid Minutes () -> assertThrows(SemanticCheckException.class, () -> invalidHourOfDayQuery("12:61:34")), - //Invalid Hours + // Invalid Hours () -> assertThrows(SemanticCheckException.class, () -> invalidHourOfDayQuery("25:23:34")), - //incorrect format - () -> assertThrows(SemanticCheckException.class, () -> invalidHourOfDayQuery("asdfasdf")) - ); - + // incorrect format + () -> assertThrows(SemanticCheckException.class, () -> invalidHourOfDayQuery("asdfasdf"))); } private void checkForExpectedDay( - FunctionExpression functionExpression, - String expectedDay, - String testExpr) { + FunctionExpression functionExpression, String expectedDay, String testExpr) { assertEquals(DATE, functionExpression.type()); assertEquals(new ExprDateValue(expectedDay), eval(functionExpression)); assertEquals(testExpr, functionExpression.toString()); @@ -743,9 +664,9 @@ private void checkForExpectedDay( private static Stream getTestDataForLastDay() { return Stream.of( Arguments.of(new ExprDateValue("2017-01-20"), "2017-01-31", "last_day(DATE '2017-01-20')"), - //Leap year + // Leap year Arguments.of(new ExprDateValue("2020-02-20"), "2020-02-29", "last_day(DATE '2020-02-20')"), - //Non leap year + // Non leap year Arguments.of(new ExprDateValue("2017-02-20"), "2017-02-28", "last_day(DATE '2017-02-20')"), Arguments.of(new ExprDateValue("2017-03-20"), "2017-03-31", "last_day(DATE '2017-03-20')"), Arguments.of(new ExprDateValue("2017-04-20"), "2017-04-30", "last_day(DATE '2017-04-20')"), @@ -756,8 +677,7 @@ private static Stream getTestDataForLastDay() { Arguments.of(new ExprDateValue("2017-09-20"), "2017-09-30", "last_day(DATE '2017-09-20')"), Arguments.of(new ExprDateValue("2017-10-20"), "2017-10-31", "last_day(DATE '2017-10-20')"), Arguments.of(new ExprDateValue("2017-11-20"), "2017-11-30", "last_day(DATE '2017-11-20')"), - Arguments.of(new ExprDateValue("2017-12-20"), "2017-12-31", "last_day(DATE '2017-12-20')") - ); + Arguments.of(new ExprDateValue("2017-12-20"), "2017-12-31", "last_day(DATE '2017-12-20')")); } @ParameterizedTest(name = "{2}") @@ -766,40 +686,37 @@ public void testLastDay(ExprValue testedDateTime, String expectedResult, String checkForExpectedDay( DSL.last_day(functionProperties, DSL.literal(testedDateTime)), expectedResult, - expectedQuery - ); + expectedQuery); } @Test public void testLastDayWithTimeType() { - FunctionExpression expression = DSL.last_day( - functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); + FunctionExpression expression = + DSL.last_day(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); LocalDate expected = LocalDate.now(functionProperties.getQueryStartClock()); LocalDate result = eval(expression).dateValue(); - assertAll( () -> assertEquals((expected.lengthOfMonth()), result.getDayOfMonth()), - () -> assertEquals("last_day(TIME '12:23:34')", expression.toString()) - ); + () -> assertEquals("last_day(TIME '12:23:34')", expression.toString())); } private void lastDay(String date) { - FunctionExpression expression = DSL.day_of_week( - functionProperties, DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.day_of_week(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void testLastDayInvalidArgument() { - assertThrows(SemanticCheckException.class, () -> lastDay("asdfasdf")); + assertThrows(SemanticCheckException.class, () -> lastDay("asdfasdf")); } @Test public void microsecond() { - FunctionExpression expression = DSL - .microsecond(DSL.literal(new ExprTimeValue("01:02:03.123456"))); + FunctionExpression expression = + DSL.microsecond(DSL.literal(new ExprTimeValue("01:02:03.123456"))); assertEquals(INTEGER, expression.type()); assertEquals(integerValue(123456), eval(expression)); assertEquals("microsecond(TIME '01:02:03.123456')", expression.toString()); @@ -904,20 +821,14 @@ private void minuteOfHourQuery(FunctionExpression dateExpression, int minute, St assertAll( () -> assertEquals(INTEGER, dateExpression.type()), () -> assertEquals(integerValue(minute), eval(dateExpression)), - () -> assertEquals(testExpr, dateExpression.toString()) - ); + () -> assertEquals(testExpr, dateExpression.toString())); } private static Stream getTestDataForMinuteOfHour() { return Stream.of( Arguments.of( - DSL.literal(new ExprTimeValue("01:02:03")), - 2, - "minute_of_hour(TIME '01:02:03')"), - Arguments.of( - DSL.literal("01:02:03"), - 2, - "minute_of_hour(\"01:02:03\")"), + DSL.literal(new ExprTimeValue("01:02:03")), 2, "minute_of_hour(TIME '01:02:03')"), + Arguments.of(DSL.literal("01:02:03"), 2, "minute_of_hour(\"01:02:03\")"), Arguments.of( DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03")), 2, @@ -927,10 +838,7 @@ private static Stream getTestDataForMinuteOfHour() { 2, "minute_of_hour(DATETIME '2020-08-17 01:02:03')"), Arguments.of( - DSL.literal("2020-08-17 01:02:03"), - 2, - "minute_of_hour(\"2020-08-17 01:02:03\")") - ); + DSL.literal("2020-08-17 01:02:03"), 2, "minute_of_hour(\"2020-08-17 01:02:03\")")); } @ParameterizedTest(name = "{2}") @@ -947,29 +855,23 @@ private void invalidMinuteOfHourQuery(String time) { @Test public void minuteOfHourInvalidArguments() { assertAll( - //Invalid Seconds - () -> assertThrows( - SemanticCheckException.class, - () -> invalidMinuteOfHourQuery("12:23:61")), + // Invalid Seconds + () -> + assertThrows(SemanticCheckException.class, () -> invalidMinuteOfHourQuery("12:23:61")), - //Invalid Minutes - () -> assertThrows( - SemanticCheckException.class, - () -> invalidMinuteOfHourQuery("12:61:34")), + // Invalid Minutes + () -> + assertThrows(SemanticCheckException.class, () -> invalidMinuteOfHourQuery("12:61:34")), - //Invalid Hours - () -> assertThrows( - SemanticCheckException.class, - () -> invalidMinuteOfHourQuery("25:23:34")), + // Invalid Hours + () -> + assertThrows(SemanticCheckException.class, () -> invalidMinuteOfHourQuery("25:23:34")), - //incorrect format - () -> assertThrows( - SemanticCheckException.class, - () -> invalidMinuteOfHourQuery("asdfasdf")) - ); + // incorrect format + () -> + assertThrows(SemanticCheckException.class, () -> invalidMinuteOfHourQuery("asdfasdf"))); } - @Test public void month() { FunctionExpression expression = DSL.month(DSL.literal(new ExprDateValue("2020-08-07"))); @@ -991,9 +893,7 @@ public void month() { private static Stream getTestDataForMonthOfYear() { return Stream.of( Arguments.of( - DSL.literal(new ExprDateValue("2020-08-07")), - "month_of_year(DATE '2020-08-07')", - 8), + DSL.literal(new ExprDateValue("2020-08-07")), "month_of_year(DATE '2020-08-07')", 8), Arguments.of( DSL.literal(new ExprDatetimeValue("2020-08-07 12:23:34")), "month_of_year(DATETIME '2020-08-07 12:23:34')", @@ -1002,25 +902,16 @@ private static Stream getTestDataForMonthOfYear() { DSL.literal(new ExprTimestampValue("2020-08-07 12:23:34")), "month_of_year(TIMESTAMP '2020-08-07 12:23:34')", 8), + Arguments.of(DSL.literal("2020-08-07"), "month_of_year(\"2020-08-07\")", 8), Arguments.of( - DSL.literal("2020-08-07"), - "month_of_year(\"2020-08-07\")", - 8), - Arguments.of( - DSL.literal("2020-08-07 01:02:03"), - "month_of_year(\"2020-08-07 01:02:03\")", - 8) - ); + DSL.literal("2020-08-07 01:02:03"), "month_of_year(\"2020-08-07 01:02:03\")", 8)); } @ParameterizedTest(name = "{0}") @MethodSource("getTestDataForMonthOfYear") public void monthOfYear(LiteralExpression arg, String expectedString, int expectedResult) { validateStringFormat( - DSL.month_of_year(functionProperties, arg), - expectedString, - expectedResult - ); + DSL.month_of_year(functionProperties, arg), expectedString, expectedResult); } @Test @@ -1032,41 +923,34 @@ public void testMonthOfYearWithTimeType() { } private void invalidDatesQuery(String date) throws SemanticCheckException { - FunctionExpression expression = DSL.month_of_year( - functionProperties, - DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.month_of_year(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void monthOfYearInvalidDates() { assertAll( - () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-01-50")), - () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-02-29")), - () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-02-31")), - () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-13-05")) - ); + () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-01-50")), + () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-02-29")), + () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-02-31")), + () -> assertThrows(SemanticCheckException.class, () -> invalidDatesQuery("2019-13-05"))); } @Test public void monthOfYearAlternateArgumentSyntaxes() { - FunctionExpression expression = DSL.month_of_year( - functionProperties, - DSL.literal(new ExprDateValue("2020-08-07"))); + FunctionExpression expression = + DSL.month_of_year(functionProperties, DSL.literal(new ExprDateValue("2020-08-07"))); assertEquals(INTEGER, expression.type()); assertEquals("month_of_year(DATE '2020-08-07')", expression.toString()); assertEquals(integerValue(8), eval(expression)); - expression = DSL.month_of_year( - functionProperties, - DSL.literal("2020-08-07")); + expression = DSL.month_of_year(functionProperties, DSL.literal("2020-08-07")); assertEquals(INTEGER, expression.type()); assertEquals("month_of_year(\"2020-08-07\")", expression.toString()); assertEquals(integerValue(8), eval(expression)); - expression = DSL.month_of_year( - functionProperties, - DSL.literal("2020-08-07 01:02:03")); + expression = DSL.month_of_year(functionProperties, DSL.literal("2020-08-07 01:02:03")); assertEquals(INTEGER, expression.type()); assertEquals("month_of_year(\"2020-08-07 01:02:03\")", expression.toString()); assertEquals(integerValue(8), eval(expression)); @@ -1117,15 +1001,13 @@ private static Stream getTestDataForSecToTime() { Arguments.of(-169200, "01:00:00"), Arguments.of(3600, "01:00:00"), Arguments.of(90000, "01:00:00"), - Arguments.of(176400, "01:00:00") - ); + Arguments.of(176400, "01:00:00")); } @ParameterizedTest(name = "{0}") @MethodSource("getTestDataForSecToTime") public void testSecToTime(int seconds, String expected) { - FunctionExpression expr = DSL.sec_to_time( - DSL.literal(new ExprIntegerValue(seconds))); + FunctionExpression expr = DSL.sec_to_time(DSL.literal(new ExprIntegerValue(seconds))); assertEquals(TIME, expr.type()); assertEquals(new ExprTimeValue(expected), eval(expr)); @@ -1137,8 +1019,7 @@ private static Stream getTestDataForSecToTimeWithDecimal() { Arguments.of(1.00123, "00:00:01.00123"), Arguments.of(1.001023, "00:00:01.001023"), Arguments.of(1.000000042, "00:00:01.000000042"), - Arguments.of(3.14, "00:00:03.14") - ); + Arguments.of(3.14, "00:00:03.14")); } @ParameterizedTest(name = "{0}") @@ -1187,28 +1068,18 @@ private void secondOfMinuteQuery(FunctionExpression dateExpression, int second, private static Stream getTestDataForSecondOfMinute() { return Stream.of( Arguments.of( - DSL.literal(new ExprTimeValue("01:02:03")), - 3, - "second_of_minute(TIME '01:02:03')"), + DSL.literal(new ExprTimeValue("01:02:03")), 3, "second_of_minute(TIME '01:02:03')"), + Arguments.of(DSL.literal("01:02:03"), 3, "second_of_minute(\"01:02:03\")"), Arguments.of( - DSL.literal("01:02:03"), - 3, - "second_of_minute(\"01:02:03\")"), + DSL.literal("2020-08-17 01:02:03"), 3, "second_of_minute(\"2020-08-17 01:02:03\")"), Arguments.of( - DSL.literal("2020-08-17 01:02:03"), - 3, - "second_of_minute(\"2020-08-17 01:02:03\")"), - Arguments.of( - DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03")), 3, "second_of_minute(TIMESTAMP '2020-08-17 01:02:03')"), Arguments.of( - DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03")), 3, - "second_of_minute(DATETIME '2020-08-17 01:02:03')") - ); + "second_of_minute(DATETIME '2020-08-17 01:02:03')")); } @ParameterizedTest(name = "{2}") @@ -1225,19 +1096,22 @@ private void invalidSecondOfMinuteQuery(String time) { @Test public void secondOfMinuteInvalidArguments() { assertAll( - //Invalid Seconds - () -> assertThrows(SemanticCheckException.class, - () -> invalidSecondOfMinuteQuery("12:23:61")), - //Invalid Minutes - () -> assertThrows(SemanticCheckException.class, - () -> invalidSecondOfMinuteQuery("12:61:34")), - //Invalid Hours - () -> assertThrows(SemanticCheckException.class, - () -> invalidSecondOfMinuteQuery("25:23:34")), - //incorrect format - () -> assertThrows(SemanticCheckException.class, - () -> invalidSecondOfMinuteQuery("asdfasdf")) - ); + // Invalid Seconds + () -> + assertThrows( + SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:23:61")), + // Invalid Minutes + () -> + assertThrows( + SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("12:61:34")), + // Invalid Hours + () -> + assertThrows( + SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("25:23:34")), + // incorrect format + () -> + assertThrows( + SemanticCheckException.class, () -> invalidSecondOfMinuteQuery("asdfasdf"))); } @Test @@ -1304,35 +1178,32 @@ public void timestamp() { } private void weekQuery(String date, int mode, int expectedResult) { - FunctionExpression expression = DSL - .week(functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); + FunctionExpression expression = + DSL.week(functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); assertEquals(INTEGER, expression.type()); assertEquals(String.format("week(DATE '%s', %d)", date, mode), expression.toString()); assertEquals(integerValue(expectedResult), eval(expression)); } private void weekOfYearUnderscoresQuery(String date, int mode, int expectedResult) { - FunctionExpression expression = DSL - .week_of_year( - functionProperties, - DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); + FunctionExpression expression = + DSL.week_of_year( + functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); assertEquals(INTEGER, expression.type()); assertEquals(String.format("week_of_year(DATE '%s', %d)", date, mode), expression.toString()); assertEquals(integerValue(expectedResult), eval(expression)); } private void weekOfYearQuery(String date, int mode, int expectedResult) { - FunctionExpression expression = DSL - .weekofyear( - functionProperties, - DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); + FunctionExpression expression = + DSL.weekofyear(functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); assertEquals(INTEGER, expression.type()); assertEquals(String.format("weekofyear(DATE '%s', %d)", date, mode), expression.toString()); assertEquals(integerValue(expectedResult), eval(expression)); } private static Stream getTestDataForWeek() { - //Test the behavior of different modes passed into the 'week_of_year' function + // Test the behavior of different modes passed into the 'week_of_year' function return Stream.of( Arguments.of("2019-01-05", 0, 0), Arguments.of("2019-01-05", 1, 1), @@ -1342,7 +1213,6 @@ private static Stream getTestDataForWeek() { Arguments.of("2019-01-05", 5, 0), Arguments.of("2019-01-05", 6, 1), Arguments.of("2019-01-05", 7, 53), - Arguments.of("2019-01-06", 0, 1), Arguments.of("2019-01-06", 1, 1), Arguments.of("2019-01-06", 2, 1), @@ -1351,7 +1221,6 @@ private static Stream getTestDataForWeek() { Arguments.of("2019-01-06", 5, 0), Arguments.of("2019-01-06", 6, 2), Arguments.of("2019-01-06", 7, 53), - Arguments.of("2019-01-07", 0, 1), Arguments.of("2019-01-07", 1, 2), Arguments.of("2019-01-07", 2, 1), @@ -1360,11 +1229,9 @@ private static Stream getTestDataForWeek() { Arguments.of("2019-01-07", 5, 1), Arguments.of("2019-01-07", 6, 2), Arguments.of("2019-01-07", 7, 1), - Arguments.of("2000-01-01", 0, 0), Arguments.of("2000-01-01", 2, 52), - Arguments.of("1999-12-31", 0, 52) - ); + Arguments.of("1999-12-31", 0, 52)); } @ParameterizedTest(name = "{1}{2}") @@ -1376,145 +1243,130 @@ public void testWeek(String date, int mode, int expected) { } private void validateStringFormat( - FunctionExpression expr, - String expectedString, - int expectedResult) { + FunctionExpression expr, String expectedString, int expectedResult) { assertAll( () -> assertEquals(INTEGER, expr.type()), () -> assertEquals(expectedString, expr.toString()), - () -> assertEquals(integerValue(expectedResult), eval(expr)) - ); + () -> assertEquals(integerValue(expectedResult), eval(expr))); } private static Stream getTestDataForWeekFormats() { return Stream.of( - Arguments.of(DSL.literal(new ExprDateValue("2019-01-05")), - "DATE '2019-01-05'", - 0), - Arguments.of(DSL.literal(new ExprDatetimeValue("2019-01-05 01:02:03")), + Arguments.of(DSL.literal(new ExprDateValue("2019-01-05")), "DATE '2019-01-05'", 0), + Arguments.of( + DSL.literal(new ExprDatetimeValue("2019-01-05 01:02:03")), "DATETIME '2019-01-05 01:02:03'", 0), - Arguments.of(DSL.literal(new ExprTimestampValue("2019-01-05 01:02:03")), - "TIMESTAMP '2019-01-05 01:02:03'", - 0), Arguments.of( - DSL.literal("2019-01-05"), - "\"2019-01-05\"", + DSL.literal(new ExprTimestampValue("2019-01-05 01:02:03")), + "TIMESTAMP '2019-01-05 01:02:03'", 0), - Arguments.of( - DSL.literal("2019-01-05 00:01:00"), - "\"2019-01-05 00:01:00\"", - 0) - ); + Arguments.of(DSL.literal("2019-01-05"), "\"2019-01-05\"", 0), + Arguments.of(DSL.literal("2019-01-05 00:01:00"), "\"2019-01-05 00:01:00\"", 0)); } @ParameterizedTest(name = "{0}") @MethodSource("getTestDataForWeekFormats") public void testWeekFormats( - LiteralExpression arg, - String expectedString, - Integer expectedInteger) { + LiteralExpression arg, String expectedString, Integer expectedInteger) { validateStringFormat( DSL.week(functionProperties, arg), - String.format("week(%s)", expectedString), expectedInteger); + String.format("week(%s)", expectedString), + expectedInteger); validateStringFormat( DSL.week_of_year(functionProperties, arg), - String.format("week_of_year(%s)", expectedString), expectedInteger); + String.format("week_of_year(%s)", expectedString), + expectedInteger); validateStringFormat( DSL.weekofyear(functionProperties, arg), - String.format("weekofyear(%s)", expectedString), expectedInteger); + String.format("weekofyear(%s)", expectedString), + expectedInteger); } @Test public void testWeekOfYearWithTimeType() { assertAll( - () -> validateStringFormat( - DSL.week( - functionProperties, - DSL.literal(new ExprTimeValue("12:23:34")), - DSL.literal(0)), - "week(TIME '12:23:34', 0)", - LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)), - - () -> validateStringFormat( - DSL.week_of_year(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))), - "week_of_year(TIME '12:23:34')", - LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)), - - () -> validateStringFormat( - DSL.weekofyear(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))), - "weekofyear(TIME '12:23:34')", - LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)) - ); + () -> + validateStringFormat( + DSL.week( + functionProperties, DSL.literal(new ExprTimeValue("12:23:34")), DSL.literal(0)), + "week(TIME '12:23:34', 0)", + LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)), + () -> + validateStringFormat( + DSL.week_of_year(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))), + "week_of_year(TIME '12:23:34')", + LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)), + () -> + validateStringFormat( + DSL.weekofyear(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))), + "weekofyear(TIME '12:23:34')", + LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR))); } @Test public void modeInUnsupportedFormat() { - FunctionExpression expression1 = DSL - .week(functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(8)); + FunctionExpression expression1 = + DSL.week(functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(8)); SemanticCheckException exception = assertThrows(SemanticCheckException.class, () -> eval(expression1)); - assertEquals("mode:8 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:8 is invalid, please use mode value between 0-7", exception.getMessage()); - FunctionExpression expression2 = DSL - .week(functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(-1)); + FunctionExpression expression2 = + DSL.week(functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(-1)); exception = assertThrows(SemanticCheckException.class, () -> eval(expression2)); - assertEquals("mode:-1 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:-1 is invalid, please use mode value between 0-7", exception.getMessage()); } @Test public void testInvalidWeekOfYear() { assertAll( - //Test for WeekOfYear - //test invalid month - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearQuery("2019-13-05 01:02:03", 0, 0)), - //test invalid day - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearQuery("2019-01-50 01:02:03", 0, 0)), - //test invalid leap year - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearQuery("2019-02-29 01:02:03", 0, 0)), - - //Test for Week_Of_Year - //test invalid month - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearUnderscoresQuery("2019-13-05 01:02:03", 0, 0)), - //test invalid day - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearUnderscoresQuery("2019-01-50 01:02:03", 0, 0)), - //test invalid leap year - () -> assertThrows( - SemanticCheckException.class, - () -> weekOfYearUnderscoresQuery("2019-02-29 01:02:03", 0, 0)) - ); + // Test for WeekOfYear + // test invalid month + () -> + assertThrows( + SemanticCheckException.class, () -> weekOfYearQuery("2019-13-05 01:02:03", 0, 0)), + // test invalid day + () -> + assertThrows( + SemanticCheckException.class, () -> weekOfYearQuery("2019-01-50 01:02:03", 0, 0)), + // test invalid leap year + () -> + assertThrows( + SemanticCheckException.class, () -> weekOfYearQuery("2019-02-29 01:02:03", 0, 0)), + + // Test for Week_Of_Year + // test invalid month + () -> + assertThrows( + SemanticCheckException.class, + () -> weekOfYearUnderscoresQuery("2019-13-05 01:02:03", 0, 0)), + // test invalid day + () -> + assertThrows( + SemanticCheckException.class, + () -> weekOfYearUnderscoresQuery("2019-01-50 01:02:03", 0, 0)), + // test invalid leap year + () -> + assertThrows( + SemanticCheckException.class, + () -> weekOfYearUnderscoresQuery("2019-02-29 01:02:03", 0, 0))); } @Test public void weekOfYearModeInUnsupportedFormat() { - FunctionExpression expression1 = DSL - .week_of_year( - functionProperties, - DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(8)); + FunctionExpression expression1 = + DSL.week_of_year( + functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(8)); SemanticCheckException exception = assertThrows(SemanticCheckException.class, () -> eval(expression1)); - assertEquals("mode:8 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:8 is invalid, please use mode value between 0-7", exception.getMessage()); - FunctionExpression expression2 = DSL - .week_of_year( - functionProperties, - DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(-1)); + FunctionExpression expression2 = + DSL.week_of_year( + functionProperties, DSL.literal(new ExprDateValue("2019-01-05")), DSL.literal(-1)); exception = assertThrows(SemanticCheckException.class, () -> eval(expression2)); - assertEquals("mode:-1 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:-1 is invalid, please use mode value between 0-7", exception.getMessage()); } @Test @@ -1557,15 +1409,14 @@ public void year() { public void date_format() { dateFormatTesters.forEach(this::testDateFormat); String timestamp = "1998-01-31 13:14:15.012345"; - String timestampFormat = "%a %b %c %D %d %e %f %H %h %I %i %j %k %l %M " - + "%m %p %r %S %s %T %% %P"; - String timestampFormatted = "Sat Jan 01 31st 31 31 012345 13 01 01 14 031 13 1 " - + "January 01 PM 01:14:15 PM 15 15 13:14:15 % P"; - - FunctionExpression expr = DSL.date_format( - functionProperties, - DSL.literal(timestamp), - DSL.literal(timestampFormat)); + String timestampFormat = + "%a %b %c %D %d %e %f %H %h %I %i %j %k %l %M " + "%m %p %r %S %s %T %% %P"; + String timestampFormatted = + "Sat Jan 01 31st 31 31 012345 13 01 01 14 031 13 1 " + + "January 01 PM 01:14:15 PM 15 15 13:14:15 % P"; + + FunctionExpression expr = + DSL.date_format(functionProperties, DSL.literal(timestamp), DSL.literal(timestampFormat)); assertEquals(STRING, expr.type()); assertEquals(timestampFormatted, eval(expr).stringValue()); } @@ -1578,108 +1429,55 @@ void testDateFormat(DateFormatTester dft) { @Test public void testDateFormatWithTimeType() { - FunctionExpression expr = DSL.date_format( - functionProperties, - DSL.literal(new ExprTimeValue("12:23:34")), - DSL.literal(new ExprStringValue("%m %d"))); + FunctionExpression expr = + DSL.date_format( + functionProperties, + DSL.literal(new ExprTimeValue("12:23:34")), + DSL.literal(new ExprStringValue("%m %d"))); + assertEquals(expr.toString(), "date_format(TIME '12:23:34', \"%m %d\")"); assertEquals( - expr.toString(), - "date_format(TIME '12:23:34', \"%m %d\")" - ); - assertEquals( - LocalDateTime.now( - functionProperties.getQueryStartClock()).format( - DateTimeFormatter.ofPattern("\"MM dd\"")), - eval(expr).toString() - ); + LocalDateTime.now(functionProperties.getQueryStartClock()) + .format(DateTimeFormatter.ofPattern("\"MM dd\"")), + eval(expr).toString()); } @Test public void testTimeFormatWithDateType() { - FunctionExpression expr = DSL.time_format( - functionProperties, - DSL.literal(new ExprDateValue("2023-01-16")), - DSL.literal(new ExprStringValue("%h %s"))); + FunctionExpression expr = + DSL.time_format( + functionProperties, + DSL.literal(new ExprDateValue("2023-01-16")), + DSL.literal(new ExprStringValue("%h %s"))); - assertEquals( - expr.toString(), - "time_format(DATE '2023-01-16', \"%h %s\")" - ); - assertEquals( - "\"12 00\"", - eval(expr).toString() - ); + assertEquals(expr.toString(), "time_format(DATE '2023-01-16', \"%h %s\")"); + assertEquals("\"12 00\"", eval(expr).toString()); } private static Stream getTestDataForTimeFormat() { return Stream.of( - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%f"), - "012345"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.002345"), - DSL.literal("%f"), - "002345"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012300"), - DSL.literal("%f"), - "012300"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%H"), - "13"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%h"), - "01"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%I"), - "01"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%i"), - "14"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%k"), - "13"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%l"), - "1"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%p"), - "PM"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%r"), - "01:14:15 PM"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%S"), - "15"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%s"), - "15"), - Arguments.of( - DSL.literal("1998-01-31 13:14:15.012345"), - DSL.literal("%T"), - "13:14:15"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%f"), "012345"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.002345"), DSL.literal("%f"), "002345"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012300"), DSL.literal("%f"), "012300"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%H"), "13"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%h"), "01"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%I"), "01"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%i"), "14"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%k"), "13"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%l"), "1"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%p"), "PM"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%r"), "01:14:15 PM"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%S"), "15"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%s"), "15"), + Arguments.of(DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%T"), "13:14:15"), Arguments.of( DSL.literal("1998-01-31 13:14:15.012345"), DSL.literal("%f %H %h %I %i %k %l %p %r %S %s %T"), - "012345 13 01 01 14 13 1 PM 01:14:15 PM 15 15 13:14:15") - ); + "012345 13 01 01 14 13 1 PM 01:14:15 PM 15 15 13:14:15")); } - private void timeFormatQuery(LiteralExpression arg, - LiteralExpression format, - String expectedResult) { + private void timeFormatQuery( + LiteralExpression arg, LiteralExpression format, String expectedResult) { FunctionExpression expr = DSL.time_format(functionProperties, arg, format); assertEquals(STRING, expr.type()); assertEquals(expectedResult, eval(expr).stringValue()); @@ -1687,32 +1485,20 @@ private void timeFormatQuery(LiteralExpression arg, @ParameterizedTest(name = "{0}{1}") @MethodSource("getTestDataForTimeFormat") - public void testTimeFormat(LiteralExpression arg, - LiteralExpression format, - String expectedResult) { + public void testTimeFormat( + LiteralExpression arg, LiteralExpression format, String expectedResult) { timeFormatQuery(arg, format, expectedResult); } private static Stream getInvalidTestDataForTimeFormat() { return Stream.of( - Arguments.of( - DSL.literal("asdfasdf"), - DSL.literal("%f")), - Arguments.of( - DSL.literal("12345"), - DSL.literal("%h")), - Arguments.of( - DSL.literal("10:11:61"), - DSL.literal("%h")), - Arguments.of( - DSL.literal("10:61:12"), - DSL.literal("%h")), - Arguments.of( - DSL.literal("61:11:12"), - DSL.literal("%h")) - ); + Arguments.of(DSL.literal("asdfasdf"), DSL.literal("%f")), + Arguments.of(DSL.literal("12345"), DSL.literal("%h")), + Arguments.of(DSL.literal("10:11:61"), DSL.literal("%h")), + Arguments.of(DSL.literal("10:61:12"), DSL.literal("%h")), + Arguments.of(DSL.literal("61:11:12"), DSL.literal("%h"))); } - + @ParameterizedTest(name = "{0}{1}") @MethodSource("getInvalidTestDataForTimeFormat") public void testInvalidTimeFormat(LiteralExpression arg, LiteralExpression format) { @@ -1734,41 +1520,39 @@ private static Stream getInvalidTimeFormatHandlers() { Arguments.of("%V"), Arguments.of("%v"), Arguments.of("%X"), - Arguments.of("%x") - ); + Arguments.of("%x")); } @ParameterizedTest(name = "{0}") @MethodSource("getInvalidTimeFormatHandlers") public void testTimeFormatWithInvalidHandlers(String handler) { - FunctionExpression expr = DSL.time_format( - functionProperties, - DSL.literal("12:23:34"), - DSL.literal(handler)); + FunctionExpression expr = + DSL.time_format(functionProperties, DSL.literal("12:23:34"), DSL.literal(handler)); assertEquals(ExprNullValue.of(), eval(expr)); } @Test public void testTimeFormatWithDateHandlers() { - FunctionExpression expr = DSL.time_format( - functionProperties, - DSL.literal(new ExprDateValue("2023-01-17")), - DSL.literal("%c %d %e %m %Y %y")); - assertEquals( - "0 00 0 00 0000 00", - eval(expr).stringValue()); + FunctionExpression expr = + DSL.time_format( + functionProperties, + DSL.literal(new ExprDateValue("2023-01-17")), + DSL.literal("%c %d %e %m %Y %y")); + assertEquals("0 00 0 00 0000 00", eval(expr).stringValue()); } @Test public void testTimeFormatAndDateFormatReturnSameResult() { - FunctionExpression timeFormatExpr = DSL.time_format( - functionProperties, - DSL.literal(new ExprDateValue("1998-01-31 13:14:15.012345")), - DSL.literal("%f %H %h %I %i %k %l %p %r %S %s %T")); - FunctionExpression dateFormatExpr = DSL.date_format( - functionProperties, - DSL.literal(new ExprDateValue("1998-01-31 13:14:15.012345")), - DSL.literal("%f %H %h %I %i %k %l %p %r %S %s %T")); + FunctionExpression timeFormatExpr = + DSL.time_format( + functionProperties, + DSL.literal(new ExprDateValue("1998-01-31 13:14:15.012345")), + DSL.literal("%f %H %h %I %i %k %l %p %r %S %s %T")); + FunctionExpression dateFormatExpr = + DSL.date_format( + functionProperties, + DSL.literal(new ExprDateValue("1998-01-31 13:14:15.012345")), + DSL.literal("%f %H %h %I %i %k %l %p %r %S %s %T")); assertEquals(eval(dateFormatExpr), eval(timeFormatExpr)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTest.java index 22c3571acae..d8571225349 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,8 +38,8 @@ public void positiveTimeZoneNoField2() { @Test public void positiveField1WrittenField2() { - FunctionExpression expr = DSL.datetime(DSL.literal("2008-05-15 22:00:00+01:00"), - DSL.literal("America/Los_Angeles")); + FunctionExpression expr = + DSL.datetime(DSL.literal("2008-05-15 22:00:00+01:00"), DSL.literal("America/Los_Angeles")); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue("2008-05-15 14:00:00"), expr.valueOf()); } @@ -53,42 +52,43 @@ public void localDateTimeConversion() { String dt = "2008-05-15 22:00:00"; String timeZone = "America/Los_Angeles"; LocalDateTime timeConverted = LocalDateTime.parse(dt, formatter); - ZonedDateTime timeZoneLocal = timeConverted.atZone(ZoneId.of(TimeZone.getDefault().getID())) - .withZoneSameInstant(ZoneId.of(timeZone)); - FunctionExpression expr = DSL.datetime(DSL.literal(dt), - DSL.literal(timeZone)); + ZonedDateTime timeZoneLocal = + timeConverted + .atZone(ZoneId.of(TimeZone.getDefault().getID())) + .withZoneSameInstant(ZoneId.of(timeZone)); + FunctionExpression expr = DSL.datetime(DSL.literal(dt), DSL.literal(timeZone)); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue(timeZoneLocal.toLocalDateTime()), expr.valueOf()); } @Test public void negativeField1WrittenField2() { - FunctionExpression expr = DSL.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), - DSL.literal("America/Los_Angeles")); + FunctionExpression expr = + DSL.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), DSL.literal("America/Los_Angeles")); assertEquals(DATETIME, expr.type()); assertEquals(new ExprDatetimeValue("2008-05-16 02:00:00"), expr.valueOf()); } @Test public void negativeField1PositiveField2() { - FunctionExpression expr = DSL.datetime(DSL.literal("2008-05-15 22:00:00-12:00"), - DSL.literal("+15:00")); + FunctionExpression expr = + DSL.datetime(DSL.literal("2008-05-15 22:00:00-12:00"), DSL.literal("+15:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void twentyFourHourDifference() { - FunctionExpression expr = DSL.datetime(DSL.literal("2008-05-15 22:00:00-14:00"), - DSL.literal("+10:00")); + FunctionExpression expr = + DSL.datetime(DSL.literal("2008-05-15 22:00:00-14:00"), DSL.literal("+10:00")); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } @Test public void negativeToNull() { - FunctionExpression expr = DSL.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), - DSL.literal(nullValue())); + FunctionExpression expr = + DSL.datetime(DSL.literal("2008-05-15 22:00:00-11:00"), DSL.literal(nullValue())); assertEquals(DATETIME, expr.type()); assertEquals(nullValue(), expr.valueOf()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java index 4c8d42e8f97..023a3574aa7 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/DateTimeTestBase.java @@ -27,16 +27,17 @@ public class DateTimeTestBase extends ExpressionTestBase { - protected final BuiltinFunctionRepository functionRepository - = BuiltinFunctionRepository.getInstance(); + protected final BuiltinFunctionRepository functionRepository = + BuiltinFunctionRepository.getInstance(); protected ExprValue eval(Expression expression) { return expression.valueOf(); } protected FunctionExpression adddate(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.ADDDATE.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.ADDDATE.getName(), List.of(date, interval)); } protected ExprValue adddate(Object first, Object interval) { @@ -45,8 +46,9 @@ protected ExprValue adddate(Object first, Object interval) { } protected FunctionExpression addtime(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.ADDTIME.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.ADDTIME.getName(), List.of(date, interval)); } protected ExprValue addtime(Temporal first, Temporal second) { @@ -55,8 +57,9 @@ protected ExprValue addtime(Temporal first, Temporal second) { } protected FunctionExpression date_add(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.DATE_ADD.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.DATE_ADD.getName(), List.of(date, interval)); } protected ExprValue date_add(Object first, Object second) { @@ -65,8 +68,9 @@ protected ExprValue date_add(Object first, Object second) { } protected FunctionExpression date_sub(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.DATE_SUB.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.DATE_SUB.getName(), List.of(date, interval)); } protected ExprValue date_sub(Object first, Object second) { @@ -75,13 +79,15 @@ protected ExprValue date_sub(Object first, Object second) { } protected FunctionExpression datediff(Expression first, Expression second) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.DATEDIFF.getName(), List.of(first, second)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.DATEDIFF.getName(), List.of(first, second)); } protected Long datediff(Temporal first, Temporal second) { return datediff(DSL.literal(fromObjectValue(first)), DSL.literal(fromObjectValue(second))) - .valueOf().longValue(); + .valueOf() + .longValue(); } protected LocalDateTime fromUnixTime(Double value) { @@ -89,13 +95,17 @@ protected LocalDateTime fromUnixTime(Double value) { } protected FunctionExpression fromUnixTime(Expression value) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.FROM_UNIXTIME.getName(), List.of(value)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.FROM_UNIXTIME.getName(), List.of(value)); } protected FunctionExpression fromUnixTime(Expression value, Expression format) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.FROM_UNIXTIME.getName(), List.of(value, format)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, + BuiltinFunctionName.FROM_UNIXTIME.getName(), + List.of(value, format)); } protected LocalDateTime fromUnixTime(Long value) { @@ -103,28 +113,31 @@ protected LocalDateTime fromUnixTime(Long value) { } protected String fromUnixTime(Long value, String format) { - return fromUnixTime(DSL.literal(value), DSL.literal(format)) - .valueOf().stringValue(); + return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf().stringValue(); } protected String fromUnixTime(Double value, String format) { - return fromUnixTime(DSL.literal(value), DSL.literal(format)) - .valueOf().stringValue(); + return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf().stringValue(); } protected FunctionExpression maketime(Expression hour, Expression minute, Expression second) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.MAKETIME.getName(), List.of(hour, minute, second)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, + BuiltinFunctionName.MAKETIME.getName(), + List.of(hour, minute, second)); } protected LocalTime maketime(Double hour, Double minute, Double second) { return maketime(DSL.literal(hour), DSL.literal(minute), DSL.literal(second)) - .valueOf().timeValue(); + .valueOf() + .timeValue(); } protected FunctionExpression makedate(Expression year, Expression dayOfYear) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.MAKEDATE.getName(), List.of(year, dayOfYear)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.MAKEDATE.getName(), List.of(year, dayOfYear)); } protected LocalDate makedate(double year, double dayOfYear) { @@ -132,29 +145,29 @@ protected LocalDate makedate(double year, double dayOfYear) { } protected FunctionExpression period_add(Expression period, Expression months) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.PERIOD_ADD.getName(), List.of(period, months)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.PERIOD_ADD.getName(), List.of(period, months)); } protected Integer period_add(Integer period, Integer months) { - return period_add(DSL.literal(period), DSL.literal(months)) - .valueOf().integerValue(); + return period_add(DSL.literal(period), DSL.literal(months)).valueOf().integerValue(); } protected FunctionExpression period_diff(Expression first, Expression second) { - return (FunctionExpression) functionRepository.compile( - functionProperties, - BuiltinFunctionName.PERIOD_DIFF.getName(), List.of(first, second)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.PERIOD_DIFF.getName(), List.of(first, second)); } protected Integer period_diff(Integer first, Integer second) { - return period_diff(DSL.literal(first), DSL.literal(second)) - .valueOf().integerValue(); + return period_diff(DSL.literal(first), DSL.literal(second)).valueOf().integerValue(); } protected FunctionExpression subdate(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.SUBDATE.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.SUBDATE.getName(), List.of(date, interval)); } protected ExprValue subdate(Object first, Object interval) { @@ -163,8 +176,9 @@ protected ExprValue subdate(Object first, Object interval) { } protected FunctionExpression subtime(Expression date, Expression interval) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.SUBTIME.getName(), List.of(date, interval)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.SUBTIME.getName(), List.of(date, interval)); } protected ExprValue subtime(Temporal first, Temporal second) { @@ -173,18 +187,21 @@ protected ExprValue subtime(Temporal first, Temporal second) { } protected FunctionExpression timediff(Expression first, Expression second) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.TIMEDIFF.getName(), List.of(first, second)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.TIMEDIFF.getName(), List.of(first, second)); } protected LocalTime timediff(LocalTime first, LocalTime second) { return timediff(DSL.literal(new ExprTimeValue(first)), DSL.literal(new ExprTimeValue(second))) - .valueOf().timeValue(); + .valueOf() + .timeValue(); } protected FunctionExpression unixTimeStampExpr() { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.UNIX_TIMESTAMP.getName(), List.of()); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.UNIX_TIMESTAMP.getName(), List.of()); } protected Long unixTimeStamp() { @@ -192,8 +209,9 @@ protected Long unixTimeStamp() { } protected FunctionExpression unixTimeStampOf(Expression value) { - return (FunctionExpression) functionRepository.compile(functionProperties, - BuiltinFunctionName.UNIX_TIMESTAMP.getName(), List.of(value)); + return (FunctionExpression) + functionRepository.compile( + functionProperties, BuiltinFunctionName.UNIX_TIMESTAMP.getName(), List.of(value)); } protected Double unixTimeStampOf(Double value) { @@ -201,17 +219,14 @@ protected Double unixTimeStampOf(Double value) { } protected Double unixTimeStampOf(LocalDate value) { - return unixTimeStampOf(DSL.literal(new ExprDateValue(value))) - .valueOf().doubleValue(); + return unixTimeStampOf(DSL.literal(new ExprDateValue(value))).valueOf().doubleValue(); } protected Double unixTimeStampOf(LocalDateTime value) { - return unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value))) - .valueOf().doubleValue(); + return unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value))).valueOf().doubleValue(); } protected Double unixTimeStampOf(Instant value) { - return unixTimeStampOf(DSL.literal(new ExprTimestampValue(value))) - .valueOf().doubleValue(); + return unixTimeStampOf(DSL.literal(new ExprTimestampValue(value))).valueOf().doubleValue(); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java index 338933333a2..820158b7224 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/ExtractTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; @@ -38,8 +37,7 @@ private static Stream getDatetimeResultsForExtractFunction() { Arguments.of("DAY_MICROSECOND", 11101112123000L), Arguments.of("DAY_SECOND", 11101112), Arguments.of("DAY_MINUTE", 111011), - Arguments.of("DAY_HOUR", 1110) - ); + Arguments.of("DAY_HOUR", 1110)); } private static Stream getTimeResultsForExtractFunction() { @@ -53,8 +51,7 @@ private static Stream getTimeResultsForExtractFunction() { Arguments.of("MINUTE_SECOND", 1112), Arguments.of("HOUR_MICROSECOND", 101112123000L), Arguments.of("HOUR_SECOND", 101112), - Arguments.of("HOUR_MINUTE", 1011) - ); + Arguments.of("HOUR_MINUTE", 1011)); } private static Stream getDateResultsForExtractFunction() { @@ -64,19 +61,18 @@ private static Stream getDateResultsForExtractFunction() { Arguments.of("MONTH", 2), Arguments.of("QUARTER", 1), Arguments.of("YEAR", 2023), - Arguments.of("YEAR_MONTH", 202302) - ); + Arguments.of("YEAR_MONTH", 202302)); } @ParameterizedTest(name = "{0}") @MethodSource({ - "getDatetimeResultsForExtractFunction", - "getTimeResultsForExtractFunction", - "getDateResultsForExtractFunction"}) + "getDatetimeResultsForExtractFunction", + "getTimeResultsForExtractFunction", + "getDateResultsForExtractFunction" + }) public void testExtractWithDatetime(String part, long expected) { - FunctionExpression datetimeExpression = DSL.extract( - DSL.literal(part), - DSL.literal(new ExprDatetimeValue(datetimeInput))); + FunctionExpression datetimeExpression = + DSL.extract(DSL.literal(part), DSL.literal(new ExprDatetimeValue(datetimeInput))); assertEquals(LONG, datetimeExpression.type()); assertEquals(expected, eval(datetimeExpression).longValue()); @@ -87,23 +83,17 @@ public void testExtractWithDatetime(String part, long expected) { private void datePartWithTimeArgQuery(String part, String time, long expected) { ExprTimeValue timeValue = new ExprTimeValue(time); - FunctionExpression datetimeExpression = DSL.extract( - functionProperties, - DSL.literal(part), - DSL.literal(timeValue)); + FunctionExpression datetimeExpression = + DSL.extract(functionProperties, DSL.literal(part), DSL.literal(timeValue)); assertEquals(LONG, datetimeExpression.type()); - assertEquals(expected, - eval(datetimeExpression).longValue()); + assertEquals(expected, eval(datetimeExpression).longValue()); } - @Test public void testExtractDatePartWithTimeType() { datePartWithTimeArgQuery( - "DAY", - timeInput, - LocalDate.now(functionProperties.getQueryStartClock()).getDayOfMonth()); + "DAY", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getDayOfMonth()); datePartWithTimeArgQuery( "WEEK", @@ -111,43 +101,35 @@ public void testExtractDatePartWithTimeType() { LocalDate.now(functionProperties.getQueryStartClock()).get(ALIGNED_WEEK_OF_YEAR)); datePartWithTimeArgQuery( - "MONTH", - timeInput, - LocalDate.now(functionProperties.getQueryStartClock()).getMonthValue()); + "MONTH", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getMonthValue()); datePartWithTimeArgQuery( - "YEAR", - timeInput, - LocalDate.now(functionProperties.getQueryStartClock()).getYear()); + "YEAR", timeInput, LocalDate.now(functionProperties.getQueryStartClock()).getYear()); } @ParameterizedTest(name = "{0}") @MethodSource("getDateResultsForExtractFunction") public void testExtractWithDate(String part, long expected) { - FunctionExpression datetimeExpression = DSL.extract( - DSL.literal(part), - DSL.literal(new ExprDateValue(dateInput))); + FunctionExpression datetimeExpression = + DSL.extract(DSL.literal(part), DSL.literal(new ExprDateValue(dateInput))); assertEquals(LONG, datetimeExpression.type()); assertEquals(expected, eval(datetimeExpression).longValue()); assertEquals( - String.format("extract(\"%s\", DATE '2023-02-11')", part), - datetimeExpression.toString()); + String.format("extract(\"%s\", DATE '2023-02-11')", part), datetimeExpression.toString()); } @ParameterizedTest(name = "{0}") @MethodSource("getTimeResultsForExtractFunction") public void testExtractWithTime(String part, long expected) { - FunctionExpression datetimeExpression = DSL.extract( - functionProperties, - DSL.literal(part), - DSL.literal(new ExprTimeValue(timeInput))); + FunctionExpression datetimeExpression = + DSL.extract( + functionProperties, DSL.literal(part), DSL.literal(new ExprTimeValue(timeInput))); assertEquals(LONG, datetimeExpression.type()); assertEquals(expected, eval(datetimeExpression).longValue()); assertEquals( - String.format("extract(\"%s\", TIME '10:11:12.123')", part), - datetimeExpression.toString()); + String.format("extract(\"%s\", TIME '10:11:12.123')", part), datetimeExpression.toString()); } private ExprValue eval(Expression expression) { diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/FromUnixTimeTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/FromUnixTimeTest.java index 58387ef04f7..8fcc6904b2a 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/FromUnixTimeTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/FromUnixTimeTest.java @@ -31,20 +31,21 @@ private static Stream getLongSamples() { Arguments.of(1L), Arguments.of(1447430881L), Arguments.of(2147483647L), - Arguments.of(1662577241L) - ); + Arguments.of(1662577241L)); } /** * Test processing different Long values. + * * @param value a value */ @ParameterizedTest @MethodSource("getLongSamples") public void checkOfLong(Long value) { - assertEquals(LocalDateTime.of(1970, 1, 1, 0, 0, 0).plus(value, ChronoUnit.SECONDS), - fromUnixTime(value)); - assertEquals(LocalDateTime.of(1970, 1, 1, 0, 0, 0).plus(value, ChronoUnit.SECONDS), + assertEquals( + LocalDateTime.of(1970, 1, 1, 0, 0, 0).plus(value, ChronoUnit.SECONDS), fromUnixTime(value)); + assertEquals( + LocalDateTime.of(1970, 1, 1, 0, 0, 0).plus(value, ChronoUnit.SECONDS), eval(fromUnixTime(DSL.literal(new ExprLongValue(value)))).datetimeValue()); } @@ -54,12 +55,12 @@ private static Stream getDoubleSamples() { Arguments.of(100500.100500d), Arguments.of(1447430881.564d), Arguments.of(2147483647.451232d), - Arguments.of(1662577241.d) - ); + Arguments.of(1662577241.d)); } /** * Test processing different Double values. + * * @param value a value */ @ParameterizedTest @@ -70,11 +71,11 @@ public void checkOfDouble(Double value) { var valueAsString = new DecimalFormat("0.#").format(value); assertEquals( - LocalDateTime.ofEpochSecond(intPart, (int)Math.round(fracPart * 1E9), ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(intPart, (int) Math.round(fracPart * 1E9), ZoneOffset.UTC), fromUnixTime(value), valueAsString); assertEquals( - LocalDateTime.ofEpochSecond(intPart, (int)Math.round(fracPart * 1E9), ZoneOffset.UTC), + LocalDateTime.ofEpochSecond(intPart, (int) Math.round(fracPart * 1E9), ZoneOffset.UTC), eval(fromUnixTime(DSL.literal(new ExprDoubleValue(value)))).datetimeValue(), valueAsString); } @@ -88,11 +89,12 @@ private static Stream getLongSamplesWithFormat() { Arguments.of(1447430881L, "%s", "01"), // 2015-11-13 16:08:01, %s - second Arguments.of(2147483647L, "%T", "03:14:07"), // 2038-01-19 03:14:07, %T - time Arguments.of(1662577241L, "%d", "07") // 1662577241, %d - day of the month - ); + ); } /** * Test processing different Long values with format. + * * @param value a value * @param format a format * @param expected expected result @@ -101,8 +103,11 @@ private static Stream getLongSamplesWithFormat() { @MethodSource("getLongSamplesWithFormat") public void checkOfLongWithFormat(Long value, String format, String expected) { assertEquals(expected, fromUnixTime(value, format)); - assertEquals(expected, eval(fromUnixTime(DSL.literal(new ExprLongValue(value)), - DSL.literal(new ExprStringValue(format)))).stringValue()); + assertEquals( + expected, + eval(fromUnixTime( + DSL.literal(new ExprLongValue(value)), DSL.literal(new ExprStringValue(format)))) + .stringValue()); } private static Stream getDoubleSamplesWithFormat() { @@ -112,11 +117,12 @@ private static Stream getDoubleSamplesWithFormat() { Arguments.of(1447430881.56d, "%M", "November"), // 2015-11-13 16:08:01.56, %M - Month name Arguments.of(2147483647.42d, "%j", "019"), // 2038-01-19 03:14:07.42, %j - day of the year Arguments.of(1662577241.d, "%l", "7") // 2022-09-07 19:00:41, %l - 12 hour clock, no 0 pad - ); + ); } /** * Test processing different Double values with format. + * * @param value a value * @param format a format * @param expected expected result @@ -125,16 +131,18 @@ private static Stream getDoubleSamplesWithFormat() { @MethodSource("getDoubleSamplesWithFormat") public void checkOfDoubleWithFormat(Double value, String format, String expected) { assertEquals(expected, fromUnixTime(value, format)); - assertEquals(expected, eval(fromUnixTime(DSL.literal(new ExprDoubleValue(value)), - DSL.literal(new ExprStringValue(format)))).stringValue()); + assertEquals( + expected, + eval(fromUnixTime( + DSL.literal(new ExprDoubleValue(value)), DSL.literal(new ExprStringValue(format)))) + .stringValue()); } @Test public void checkInvalidFormat() { - assertEquals(new ExprStringValue("q"), - fromUnixTime(DSL.literal(0L), DSL.literal("%q")).valueOf()); - assertEquals(new ExprStringValue(""), - fromUnixTime(DSL.literal(0L), DSL.literal("")).valueOf()); + assertEquals( + new ExprStringValue("q"), fromUnixTime(DSL.literal(0L), DSL.literal("%q")).valueOf()); + assertEquals(new ExprStringValue(""), fromUnixTime(DSL.literal(0L), DSL.literal("")).valueOf()); } @Test @@ -153,33 +161,33 @@ public void checkInsideTheRangeWithoutFormat() { @Test public void checkValueOutsideOfTheRangeWithFormat() { - assertEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(32536771200L), DSL.literal("%d")).valueOf()); - assertEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(32536771200d), DSL.literal("%d")).valueOf()); + assertEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200L), DSL.literal("%d")).valueOf()); + assertEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200d), DSL.literal("%d")).valueOf()); } @Test public void checkInsideTheRangeWithFormat() { - assertNotEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(32536771199L), DSL.literal("%d")).valueOf()); - assertNotEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(32536771199d), DSL.literal("%d")).valueOf()); + assertNotEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199L), DSL.literal("%d")).valueOf()); + assertNotEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199d), DSL.literal("%d")).valueOf()); } @Test public void checkNullOrNegativeValues() { assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(ExprNullValue.of())).valueOf()); - assertEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(-1L), DSL.literal("%d")).valueOf()); - assertEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(-1.5d), DSL.literal("%d")).valueOf()); - assertEquals(ExprNullValue.of(), + assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1L), DSL.literal("%d")).valueOf()); + assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1.5d), DSL.literal("%d")).valueOf()); + assertEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(42L), DSL.literal(ExprNullValue.of())).valueOf()); - assertEquals(ExprNullValue.of(), + assertEquals( + ExprNullValue.of(), fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal("%d")).valueOf()); - assertEquals(ExprNullValue.of(), - fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal(ExprNullValue.of())) - .valueOf()); + assertEquals( + ExprNullValue.of(), + fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal(ExprNullValue.of())).valueOf()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/IntervalClauseTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/IntervalClauseTest.java index e63acc94c06..d89e5f978ac 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/IntervalClauseTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/IntervalClauseTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,14 +30,11 @@ @ExtendWith(MockitoExtension.class) public class IntervalClauseTest extends ExpressionTestBase { - @Mock - Environment env; + @Mock Environment env; - @Mock - Expression nullRef; + @Mock Expression nullRef; - @Mock - Expression missingRef; + @Mock Expression missingRef; @Test public void microsecond() { @@ -106,7 +102,9 @@ public void year() { @Test public void unsupported_unit() { FunctionExpression expr = DSL.interval(DSL.literal(1), DSL.literal("year_month")); - assertThrows(ExpressionEvaluationException.class, () -> expr.valueOf(env), + assertThrows( + ExpressionEvaluationException.class, + () -> expr.valueOf(env), "interval unit year_month is not supported"); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/MakeDateTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/MakeDateTest.java index fbd7e61c8d9..b607d907db8 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/MakeDateTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/MakeDateTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -22,21 +21,32 @@ public class MakeDateTest extends DateTimeTestBase { @Test public void checkEdgeCases() { - assertEquals(LocalDate.ofYearDay(2002, 1), makedate(2001., 366.), + assertEquals( + LocalDate.ofYearDay(2002, 1), + makedate(2001., 366.), "No switch to the next year on getting 366th day of a non-leap year"); - assertEquals(LocalDate.ofYearDay(2005, 1), makedate(2004., 367.), + assertEquals( + LocalDate.ofYearDay(2005, 1), + makedate(2004., 367.), "No switch to the next year on getting 367th day of a leap year"); - assertEquals(LocalDate.ofYearDay(2000, 42), makedate(0., 42.), + assertEquals( + LocalDate.ofYearDay(2000, 42), + makedate(0., 42.), "0 year is not interpreted as 2000 as in MySQL"); - assertEquals(nullValue(), eval(makedate(DSL.literal(-1.), DSL.literal(42.))), + assertEquals( + nullValue(), + eval(makedate(DSL.literal(-1.), DSL.literal(42.))), "Negative year doesn't produce NULL"); - assertEquals(nullValue(), eval(makedate(DSL.literal(42.), DSL.literal(-1.))), + assertEquals( + nullValue(), + eval(makedate(DSL.literal(42.), DSL.literal(-1.))), "Negative dayOfYear doesn't produce NULL"); - assertEquals(nullValue(), eval(makedate(DSL.literal(42.), DSL.literal(0.))), + assertEquals( + nullValue(), + eval(makedate(DSL.literal(42.), DSL.literal(0.))), "Zero dayOfYear doesn't produce NULL"); - assertEquals(LocalDate.of(1999, 3, 1), makedate(1999., 60.), - "Got Feb 29th of a non-lear year"); + assertEquals(LocalDate.of(1999, 3, 1), makedate(1999., 60.), "Got Feb 29th of a non-lear year"); assertEquals(LocalDate.of(1999, 12, 31), makedate(1999., 365.)); assertEquals(LocalDate.of(2004, 12, 31), makedate(2004., 366.)); } @@ -73,12 +83,12 @@ private static Stream getTestData() { Arguments.of(3617.452566, 619.795467), Arguments.of(2210.322073, 106.914268), Arguments.of(675.757974, 147.702828), - Arguments.of(1101.801820, 40.055318) - ); + Arguments.of(1101.801820, 40.055318)); } /** * Test function with given pseudo-random values. + * * @param year year * @param dayOfYear day of year */ @@ -88,20 +98,20 @@ public void checkRandomValues(double year, double dayOfYear) { LocalDate actual = makedate(year, dayOfYear); LocalDate expected = getReferenceValue(year, dayOfYear); - assertEquals(expected, actual, - String.format("year = %f, dayOfYear = %f", year, dayOfYear)); + assertEquals(expected, actual, String.format("year = %f, dayOfYear = %f", year, dayOfYear)); } /** - * Using another algorithm to get reference value. - * We should go to the next year until remaining @dayOfYear is bigger than 365/366. + * Using another algorithm to get reference value. We should go to the next year until + * remaining @dayOfYear is bigger than 365/366. + * * @param year Year. * @param dayOfYear Day of the year. * @return The calculated date. */ private LocalDate getReferenceValue(double year, double dayOfYear) { - var yearL = (int)Math.round(year); - var dayL = (int)Math.round(dayOfYear); + var yearL = (int) Math.round(year); + var dayL = (int) Math.round(dayOfYear); while (true) { int daysInYear = Year.isLeap(yearL) ? 366 : 365; if (dayL > daysInYear) { diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/MakeTimeTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/MakeTimeTest.java index 2f8e45b4c35..ff6896647d1 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/MakeTimeTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/MakeTimeTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -24,18 +23,27 @@ public class MakeTimeTest extends DateTimeTestBase { @Test public void checkEdgeCases() { - assertEquals(nullValue(), eval(maketime(DSL.literal(-1.), DSL.literal(42.), DSL.literal(42.))), + assertEquals( + nullValue(), + eval(maketime(DSL.literal(-1.), DSL.literal(42.), DSL.literal(42.))), "Negative hour doesn't produce NULL"); - assertEquals(nullValue(), eval(maketime(DSL.literal(42.), DSL.literal(-1.), DSL.literal(42.))), + assertEquals( + nullValue(), + eval(maketime(DSL.literal(42.), DSL.literal(-1.), DSL.literal(42.))), "Negative minute doesn't produce NULL"); - assertEquals(nullValue(), eval(maketime(DSL.literal(12.), DSL.literal(42.), DSL.literal(-1.))), + assertEquals( + nullValue(), + eval(maketime(DSL.literal(12.), DSL.literal(42.), DSL.literal(-1.))), "Negative second doesn't produce NULL"); - assertThrows(DateTimeParseException.class, + assertThrows( + DateTimeParseException.class, () -> eval(maketime(DSL.literal(24.), DSL.literal(42.), DSL.literal(42.)))); - assertThrows(DateTimeParseException.class, + assertThrows( + DateTimeParseException.class, () -> eval(maketime(DSL.literal(12.), DSL.literal(60.), DSL.literal(42.)))); - assertThrows(DateTimeParseException.class, + assertThrows( + DateTimeParseException.class, () -> eval(maketime(DSL.literal(12.), DSL.literal(42.), DSL.literal(60.)))); assertEquals(LocalTime.of(23, 59, 59), maketime(23., 59., 59.)); @@ -81,12 +89,12 @@ private static Stream getTestData() { Arguments.of(7.494112, 9.761983, 17.444988), Arguments.of(17.867756, 10.313120, 36.391815), Arguments.of(19.712155, 3.197562, 6.607233), - Arguments.of(2.385090, 41.761568, 33.342590) - ); + Arguments.of(2.385090, 41.761568, 33.342590)); } /** * Test function with given pseudo-random values. + * * @param hour hour * @param minute minute * @param second second @@ -95,11 +103,15 @@ private static Stream getTestData() { @MethodSource("getTestData") public void checkRandomValues(double hour, double minute, double second) { // results could have 1 nanosec diff because of rounding FP - var expected = LocalTime.of((int)Math.round(hour), (int)Math.round(minute), - // pick fraction second part as nanos - (int)Math.floor(second)).withNano((int)((second % 1) * 1E9)); + var expected = + LocalTime.of( + (int) Math.round(hour), + (int) Math.round(minute), + // pick fraction second part as nanos + (int) Math.floor(second)) + .withNano((int) ((second % 1) * 1E9)); var delta = Duration.between(expected, maketime(hour, minute, second)).getNano(); - assertEquals(0, delta, 1, - String.format("hour = %f, minute = %f, second = %f", hour, minute, second)); + assertEquals( + 0, delta, 1, String.format("hour = %f, minute = %f, second = %f", hour, minute, second)); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java index b67f4efc215..0e5c00084fb 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/NowLikeFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.hamcrest.MatcherAssert.assertThat; @@ -41,11 +40,11 @@ import org.opensearch.sql.expression.FunctionExpression; import org.opensearch.sql.expression.function.FunctionProperties; - class NowLikeFunctionTest extends ExpressionTestBase { @Test void now() { - test_now_like_functions(DSL::now, + test_now_like_functions( + DSL::now, DATETIME, false, () -> LocalDateTime.now(functionProperties.getQueryStartClock())); @@ -53,19 +52,28 @@ void now() { @Test void current_timestamp() { - test_now_like_functions(DSL::current_timestamp, DATETIME, false, + test_now_like_functions( + DSL::current_timestamp, + DATETIME, + false, () -> LocalDateTime.now(functionProperties.getQueryStartClock())); } @Test void localtimestamp() { - test_now_like_functions(DSL::localtimestamp, DATETIME, false, + test_now_like_functions( + DSL::localtimestamp, + DATETIME, + false, () -> LocalDateTime.now(functionProperties.getQueryStartClock())); } @Test void localtime() { - test_now_like_functions(DSL::localtime, DATETIME, false, + test_now_like_functions( + DSL::localtime, + DATETIME, + false, () -> LocalDateTime.now(functionProperties.getQueryStartClock())); } @@ -76,21 +84,21 @@ void sysdate() { @Test void curtime() { - test_now_like_functions(DSL::curtime, TIME, false, - () -> LocalTime.now(functionProperties.getQueryStartClock())); + test_now_like_functions( + DSL::curtime, TIME, false, () -> LocalTime.now(functionProperties.getQueryStartClock())); } @Test void currdate() { - test_now_like_functions(DSL::curdate, - DATE, false, - () -> LocalDate.now(functionProperties.getQueryStartClock())); + test_now_like_functions( + DSL::curdate, DATE, false, () -> LocalDate.now(functionProperties.getQueryStartClock())); } @Test void current_time() { - test_now_like_functions(DSL::current_time, + test_now_like_functions( + DSL::current_time, TIME, false, () -> LocalTime.now(functionProperties.getQueryStartClock())); @@ -98,26 +106,29 @@ void current_time() { @Test void current_date() { - test_now_like_functions(DSL::current_date, DATE, false, + test_now_like_functions( + DSL::current_date, + DATE, + false, () -> LocalDate.now(functionProperties.getQueryStartClock())); } @Test void utc_date() { - test_now_like_functions(DSL::utc_date, DATE, false, - () -> utcDateTimeNow(functionProperties).toLocalDate()); + test_now_like_functions( + DSL::utc_date, DATE, false, () -> utcDateTimeNow(functionProperties).toLocalDate()); } @Test void utc_time() { - test_now_like_functions(DSL::utc_time, TIME, false, - () -> utcDateTimeNow(functionProperties).toLocalTime()); + test_now_like_functions( + DSL::utc_time, TIME, false, () -> utcDateTimeNow(functionProperties).toLocalTime()); } @Test void utc_timestamp() { - test_now_like_functions(DSL::utc_timestamp, DATETIME, false, - () -> utcDateTimeNow(functionProperties)); + test_now_like_functions( + DSL::utc_timestamp, DATETIME, false, () -> utcDateTimeNow(functionProperties)); } private static LocalDateTime utcDateTimeNow(FunctionProperties functionProperties) { @@ -130,16 +141,16 @@ private static LocalDateTime utcDateTimeNow(FunctionProperties functionPropertie /** * Check how NOW-like functions are processed. * - * @param function Function - * @param resType Return type - * @param hasFsp Whether function has fsp argument + * @param function Function + * @param resType Return type + * @param hasFsp Whether function has fsp argument * @param referenceGetter A callback to get reference value */ void test_now_like_functions( BiFunction function, - ExprCoreType resType, - Boolean hasFsp, - Supplier referenceGetter) { + ExprCoreType resType, + Boolean hasFsp, + Supplier referenceGetter) { // Check return types: // `func()` FunctionExpression expr = function.apply(functionProperties, new Expression[] {}); @@ -153,10 +164,15 @@ void test_now_like_functions( assertEquals(resType, expr.type()); for (var wrongFspValue : List.of(-1, 10)) { - var exception = assertThrows(IllegalArgumentException.class, - () -> function.apply(functionProperties, - new Expression[] {DSL.literal(wrongFspValue)}).valueOf()); - assertEquals(String.format("Invalid `fsp` value: %d, allowed 0 to 6", wrongFspValue), + var exception = + assertThrows( + IllegalArgumentException.class, + () -> + function + .apply(functionProperties, new Expression[] {DSL.literal(wrongFspValue)}) + .valueOf()); + assertEquals( + String.format("Invalid `fsp` value: %d, allowed 0 to 6", wrongFspValue), exception.getMessage()); } } @@ -170,11 +186,9 @@ void test_now_like_functions( assertThat(sample, isCloseTo(reference, maxDiff, unit)); if (hasFsp) { // `func(fsp)` - Temporal value = extractValue(function.apply(functionProperties, - new Expression[] {DSL.literal(0)})); - assertThat(referenceGetter.get(), - isCloseTo(value, maxDiff, unit)); - + Temporal value = + extractValue(function.apply(functionProperties, new Expression[] {DSL.literal(0)})); + assertThat(referenceGetter.get(), isCloseTo(value, maxDiff, unit)); } } @@ -182,7 +196,8 @@ static Matcher isCloseTo(Temporal reference, long maxDiff, TemporalUni return new BaseMatcher<>() { @Override public void describeTo(Description description) { - description.appendText("value between ") + description + .appendText("value between ") .appendValue(reference.minus(maxDiff, units)) .appendText(" and ") .appendValue(reference.plus(maxDiff, units)); @@ -197,24 +212,21 @@ public boolean matches(Object value) { } return false; } - - }; } @TestFactory Stream constantValueTestFactory() { - BiFunction, DynamicTest> buildTest - = (name, action) -> - DynamicTest.dynamicTest( - String.format("multiple_invocations_same_value_test[%s]", name), - () -> { - var v1 = extractValue(action.apply(functionProperties)); - Thread.sleep(1000); - var v2 = extractValue(action.apply(functionProperties)); - assertEquals(v1, v2); - } - ); + BiFunction, DynamicTest> buildTest = + (name, action) -> + DynamicTest.dynamicTest( + String.format("multiple_invocations_same_value_test[%s]", name), + () -> { + var v1 = extractValue(action.apply(functionProperties)); + Thread.sleep(1000); + var v2 = extractValue(action.apply(functionProperties)); + assertEquals(v1, v2); + }); return Stream.of( buildTest.apply("now", DSL::now), buildTest.apply("current_timestamp", DSL::current_timestamp), @@ -222,8 +234,7 @@ Stream constantValueTestFactory() { buildTest.apply("curdate", DSL::curdate), buildTest.apply("curtime", DSL::curtime), buildTest.apply("localtimestamp", DSL::localtimestamp), - buildTest.apply("localtime", DSL::localtime) - ); + buildTest.apply("localtime", DSL::localtime)); } @Test @@ -232,7 +243,6 @@ void sysdate_multiple_invocations_differ() throws InterruptedException { Thread.sleep(1000); var v2 = extractValue(DSL.sysdate(functionProperties)); assertThat(v1, IsNot.not(isCloseTo(v2, 1, ChronoUnit.NANOS))); - } private Temporal extractValue(FunctionExpression func) { @@ -243,7 +253,7 @@ private Temporal extractValue(FunctionExpression func) { return func.valueOf().datetimeValue(); case TIME: return func.valueOf().timeValue(); - // unreachable code + // unreachable code default: throw new IllegalArgumentException(String.format("%s", func.type())); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/PeriodFunctionsTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/PeriodFunctionsTest.java index bf228dd5091..c6a6392c9b7 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/PeriodFunctionsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/PeriodFunctionsTest.java @@ -18,6 +18,7 @@ public class PeriodFunctionsTest extends DateTimeTestBase { /** * Generate sample data for `PERIOD_ADD` function. + * * @return A data set. */ public static Stream getTestDataForPeriodAdd() { @@ -27,8 +28,7 @@ public static Stream getTestDataForPeriodAdd() { Arguments.of(3, -1, 200002), // Mar 2000 - 1 Arguments.of(12, 0, 200012), // Dec 2000 + 0 Arguments.of(6104, 100, 206908), // Apr 2061 + 100m (8y4m) - Arguments.of(201510, 14, 201612) - ); + Arguments.of(201510, 14, 201612)); } @ParameterizedTest @@ -39,6 +39,7 @@ public void period_add_with_different_data(int period, int months, int expected) /** * Generate sample data for `PERIOD_DIFF` function. + * * @return A data set. */ public static Stream getTestDataForPeriodDiff() { @@ -51,7 +52,7 @@ public static Stream getTestDataForPeriodDiff() { Arguments.of(200505, 7505, 360), // May 2005 - May 1975 Arguments.of(6104, 8509, 907), // Apr 2061 - Sep 1985 Arguments.of(207707, 7707, 1200) // Jul 2077 - Jul 1977 - ); + ); } @ParameterizedTest @@ -68,29 +69,30 @@ public void two_way_conversion(int period1, int period2, int expected) { /** * Generate invalid sample data for test. + * * @return A data set. */ public static Stream getInvalidTestData() { return Stream.of( - Arguments.of(0), - Arguments.of(123), - Arguments.of(100), - Arguments.of(1234), - Arguments.of(1000), - Arguments.of(2020), - Arguments.of(12345), - Arguments.of(123456), - Arguments.of(1234567), - Arguments.of(200213), - Arguments.of(200300), - Arguments.of(-1), - Arguments.of(-1234), - Arguments.of(-123401) - ); + Arguments.of(0), + Arguments.of(123), + Arguments.of(100), + Arguments.of(1234), + Arguments.of(1000), + Arguments.of(2020), + Arguments.of(12345), + Arguments.of(123456), + Arguments.of(1234567), + Arguments.of(200213), + Arguments.of(200300), + Arguments.of(-1), + Arguments.of(-1234), + Arguments.of(-123401)); } /** * Check that `PERIOD_ADD` and `PERIOD_DIFF` return NULL on invalid input. + * * @param period An invalid data. */ @ParameterizedTest diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/StrToDateTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/StrToDateTest.java index b758331a71b..42d4aab1f66 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/StrToDateTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/StrToDateTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -15,11 +14,9 @@ import java.time.LocalTime; import java.util.stream.Stream; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.sql.data.model.ExprDatetimeValue; import org.opensearch.sql.data.model.ExprNullValue; import org.opensearch.sql.data.model.ExprStringValue; @@ -35,89 +32,46 @@ class StrToDateTest extends ExpressionTestBase { private static Stream getTestDataForStrToDate() { return Stream.of( - //Date arguments + // Date arguments Arguments.of( - "01,5,2013", - "%d,%m,%Y", - new ExprDatetimeValue("2013-05-01 00:00:00"), - DATETIME), + "01,5,2013", "%d,%m,%Y", new ExprDatetimeValue("2013-05-01 00:00:00"), DATETIME), Arguments.of( - "May 1, 2013", - "%M %d, %Y", - new ExprDatetimeValue("2013-05-01 00:00:00"), - DATETIME), + "May 1, 2013", "%M %d, %Y", new ExprDatetimeValue("2013-05-01 00:00:00"), DATETIME), Arguments.of( "May 1, 2013 - 9,23,11", "%M %d, %Y - %h,%i,%s", new ExprDatetimeValue("2013-05-01 09:23:11"), DATETIME), Arguments.of( - "2000,1,1", - "%Y,%m,%d", - new ExprDatetimeValue("2000-01-01 00:00:00"), - DATETIME), + "2000,1,1", "%Y,%m,%d", new ExprDatetimeValue("2000-01-01 00:00:00"), DATETIME), Arguments.of( - "2000,1,1,10", - "%Y,%m,%d,%h", - new ExprDatetimeValue("2000-01-01 10:00:00"), - DATETIME), + "2000,1,1,10", "%Y,%m,%d,%h", new ExprDatetimeValue("2000-01-01 10:00:00"), DATETIME), Arguments.of( "2000,1,1,10,11", "%Y,%m,%d,%h,%i", new ExprDatetimeValue("2000-01-01 10:11:00"), DATETIME), - //Invalid Arguments (should return null) - Arguments.of( - "a09:30:17", - "a%h:%i:%s", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "abc", - "abc", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "2000,1", - "%Y,%m", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "2000,1,10", - "%Y,%m,%h", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "2000,1,10,11", - "%Y,%m,%h,%i", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "9", - "%m", - ExprNullValue.of(), - UNDEFINED), - Arguments.of( - "9", - "%s", - ExprNullValue.of(), - UNDEFINED) - ); + // Invalid Arguments (should return null) + Arguments.of("a09:30:17", "a%h:%i:%s", ExprNullValue.of(), UNDEFINED), + Arguments.of("abc", "abc", ExprNullValue.of(), UNDEFINED), + Arguments.of("2000,1", "%Y,%m", ExprNullValue.of(), UNDEFINED), + Arguments.of("2000,1,10", "%Y,%m,%h", ExprNullValue.of(), UNDEFINED), + Arguments.of("2000,1,10,11", "%Y,%m,%h,%i", ExprNullValue.of(), UNDEFINED), + Arguments.of("9", "%m", ExprNullValue.of(), UNDEFINED), + Arguments.of("9", "%s", ExprNullValue.of(), UNDEFINED)); } @ParameterizedTest(name = "{0} | {1}") @MethodSource("getTestDataForStrToDate") public void test_str_to_date( - String datetime, - String format, - ExprValue expectedResult, - ExprCoreType expectedType) { + String datetime, String format, ExprValue expectedResult, ExprCoreType expectedType) { - FunctionExpression expression = DSL.str_to_date( - functionProperties, - DSL.literal(new ExprStringValue(datetime)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression expression = + DSL.str_to_date( + functionProperties, + DSL.literal(new ExprStringValue(datetime)), + DSL.literal(new ExprStringValue(format))); ExprValue result = eval(expression); @@ -132,26 +86,25 @@ private static LocalDateTime getExpectedTimeResult(int hour, int minute, int sec LocalDate.now().getDayOfMonth(), hour, minute, - seconds - ); + seconds); } private static Stream getTestDataForStrToDateWithTime() { return Stream.of( Arguments.of("9,23,11", "%h,%i,%s"), Arguments.of("2000,9,23,11", "%Y,%h,%i,%s"), - Arguments.of("2000,3,9,23,11", "%Y,%m,%h,%i,%s") - ); + Arguments.of("2000,3,9,23,11", "%Y,%m,%h,%i,%s")); } @ParameterizedTest(name = "{1}") @MethodSource("getTestDataForStrToDateWithTime") public void test_str_to_date_with_time_type(String parsed, String format) { - FunctionExpression expression = DSL.str_to_date( - functionProperties, - DSL.literal(new ExprStringValue(parsed)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression expression = + DSL.str_to_date( + functionProperties, + DSL.literal(new ExprStringValue(parsed)), + DSL.literal(new ExprStringValue(format))); ExprValue result = eval(expression); @@ -162,19 +115,21 @@ public void test_str_to_date_with_time_type(String parsed, String format) { @Test public void test_str_to_date_with_date_format() { - LocalDateTime arg = LocalDateTime.of(2023, 2, 27, 10, 11,12); + LocalDateTime arg = LocalDateTime.of(2023, 2, 27, 10, 11, 12); String format = "%Y,%m,%d %h,%i,%s"; - FunctionExpression dateFormatExpr = DSL.date_format( - functionProperties, - DSL.literal(new ExprDatetimeValue(arg)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression dateFormatExpr = + DSL.date_format( + functionProperties, + DSL.literal(new ExprDatetimeValue(arg)), + DSL.literal(new ExprStringValue(format))); String dateFormatResult = eval(dateFormatExpr).stringValue(); - FunctionExpression strToDateExpr = DSL.str_to_date( - functionProperties, - DSL.literal(new ExprStringValue(dateFormatResult)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression strToDateExpr = + DSL.str_to_date( + functionProperties, + DSL.literal(new ExprStringValue(dateFormatResult)), + DSL.literal(new ExprStringValue(format))); LocalDateTime strToDateResult = eval(strToDateExpr).datetimeValue(); assertEquals(arg, strToDateResult); @@ -186,24 +141,24 @@ public void test_str_to_date_with_time_format() { final int MINUTES = 11; final int SECONDS = 12; - LocalTime arg = LocalTime.of(HOURS, MINUTES,SECONDS); + LocalTime arg = LocalTime.of(HOURS, MINUTES, SECONDS); String format = "%h,%i,%s"; - FunctionExpression dateFormatExpr = DSL.time_format( - functionProperties, - DSL.literal(new ExprTimeValue(arg)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression dateFormatExpr = + DSL.time_format( + functionProperties, + DSL.literal(new ExprTimeValue(arg)), + DSL.literal(new ExprStringValue(format))); String timeFormatResult = eval(dateFormatExpr).stringValue(); - FunctionExpression strToDateExpr = DSL.str_to_date( - functionProperties, - DSL.literal(new ExprStringValue(timeFormatResult)), - DSL.literal(new ExprStringValue(format))); + FunctionExpression strToDateExpr = + DSL.str_to_date( + functionProperties, + DSL.literal(new ExprStringValue(timeFormatResult)), + DSL.literal(new ExprStringValue(format))); LocalDateTime strToDateResult = eval(strToDateExpr).datetimeValue(); - assertEquals( - getExpectedTimeResult(HOURS, MINUTES, SECONDS), - strToDateResult); + assertEquals(getExpectedTimeResult(HOURS, MINUTES, SECONDS), strToDateResult); } private ExprValue eval(Expression expression) { diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeDiffTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeDiffTest.java index 8bfb09bd491..88218bc63c7 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeDiffTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeDiffTest.java @@ -22,12 +22,12 @@ private static Stream getTestData() { Arguments.of(LocalTime.of(12, 42), LocalTime.of(7, 40), LocalTime.of(5, 2)), Arguments.of(LocalTime.of(7, 40), LocalTime.of(12, 42), LocalTime.of(18, 58)), Arguments.of(LocalTime.of(7, 40), LocalTime.of(7, 40), LocalTime.of(0, 0)), - Arguments.of(LocalTime.MAX, LocalTime.MIN, LocalTime.MAX) - ); + Arguments.of(LocalTime.MAX, LocalTime.MIN, LocalTime.MAX)); } /** * Test `TIME_DIFF` function with different data. + * * @param arg1 First argument. * @param arg2 Second argument. * @param expectedResult Expected result. @@ -36,7 +36,9 @@ private static Stream getTestData() { @MethodSource("getTestData") public void try_different_data(LocalTime arg1, LocalTime arg2, LocalTime expectedResult) { assertEquals(expectedResult, timediff(arg1, arg2)); - assertEquals(expectedResult, eval(timediff(DSL.literal(new ExprTimeValue(arg1)), - DSL.literal(new ExprTimeValue(arg2)))).timeValue()); + assertEquals( + expectedResult, + eval(timediff(DSL.literal(new ExprTimeValue(arg1)), DSL.literal(new ExprTimeValue(arg2)))) + .timeValue()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampAddTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampAddTest.java index 63514ab352f..243eb6bb7bc 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampAddTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampAddTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertAll; @@ -36,99 +35,90 @@ class TimeStampAddTest extends ExpressionTestBase { private static Stream getTestDataForTimestampAdd() { return Stream.of( - Arguments.of("MINUTE", 1, new ExprStringValue("2003-01-02 00:00:00"), - "2003-01-02 00:01:00"), - Arguments.of("WEEK", 1, new ExprStringValue("2003-01-02 00:00:00"), - "2003-01-09 00:00:00"), - - //Date - Arguments.of("MINUTE", 1, new ExprDateValue("2003-01-02"), - "2003-01-02 00:01:00"), - Arguments.of("WEEK", 1, new ExprDateValue("2003-01-02"), - "2003-01-09 00:00:00"), - - //Datetime - Arguments.of("MINUTE", 1, new ExprDatetimeValue("2003-01-02 00:00:00"), - "2003-01-02 00:01:00"), - Arguments.of("WEEK", 1, new ExprDatetimeValue("2003-01-02 00:00:00"), - "2003-01-09 00:00:00"), - - //Timestamp - Arguments.of("MINUTE", 1, new ExprTimestampValue("2003-01-02 00:00:00"), - "2003-01-02 00:01:00"), - Arguments.of("WEEK", 1, new ExprTimestampValue("2003-01-02 00:00:00"), - "2003-01-09 00:00:00"), - - //Cases surrounding leap year - Arguments.of("SECOND", 1, new ExprTimestampValue("2020-02-28 23:59:59"), - "2020-02-29 00:00:00"), - Arguments.of("MINUTE", 1, new ExprTimestampValue("2020-02-28 23:59:59"), - "2020-02-29 00:00:59"), - Arguments.of("HOUR", 1, new ExprTimestampValue("2020-02-28 23:59:59"), - "2020-02-29 00:59:59"), - Arguments.of("DAY", 1, new ExprTimestampValue("2020-02-28 23:59:59"), - "2020-02-29 23:59:59"), - Arguments.of("WEEK", 1, new ExprTimestampValue("2020-02-28 23:59:59"), - "2020-03-06 23:59:59"), - - //Cases surrounding end-of-year - Arguments.of("SECOND", 1, new ExprTimestampValue("2020-12-31 23:59:59"), - "2021-01-01 00:00:00"), - Arguments.of("MINUTE", 1, new ExprTimestampValue("2020-12-31 23:59:59"), - "2021-01-01 00:00:59"), - Arguments.of("HOUR", 1, new ExprTimestampValue("2020-12-31 23:59:59"), - "2021-01-01 00:59:59"), - Arguments.of("DAY", 1, new ExprTimestampValue("2020-12-31 23:59:59"), - "2021-01-01 23:59:59"), - Arguments.of("WEEK", 1, new ExprTimestampValue("2020-12-31 23:59:59"), - "2021-01-07 23:59:59"), - - //Test adding a month (including special cases) - Arguments.of("MONTH", 1, new ExprStringValue("2003-01-02 00:00:00"), - "2003-02-02 00:00:00"), - Arguments.of("MONTH", 1, new ExprDateValue("2024-03-30"), - "2024-04-30 00:00:00"), - Arguments.of("MONTH", 1, new ExprDateValue("2024-03-31"), - "2024-04-30 00:00:00"), - - //Test remaining interval types - Arguments.of("MICROSECOND", 123, new ExprStringValue("2003-01-02 00:00:00"), + Arguments.of( + "MINUTE", 1, new ExprStringValue("2003-01-02 00:00:00"), "2003-01-02 00:01:00"), + Arguments.of("WEEK", 1, new ExprStringValue("2003-01-02 00:00:00"), "2003-01-09 00:00:00"), + + // Date + Arguments.of("MINUTE", 1, new ExprDateValue("2003-01-02"), "2003-01-02 00:01:00"), + Arguments.of("WEEK", 1, new ExprDateValue("2003-01-02"), "2003-01-09 00:00:00"), + + // Datetime + Arguments.of( + "MINUTE", 1, new ExprDatetimeValue("2003-01-02 00:00:00"), "2003-01-02 00:01:00"), + Arguments.of( + "WEEK", 1, new ExprDatetimeValue("2003-01-02 00:00:00"), "2003-01-09 00:00:00"), + + // Timestamp + Arguments.of( + "MINUTE", 1, new ExprTimestampValue("2003-01-02 00:00:00"), "2003-01-02 00:01:00"), + Arguments.of( + "WEEK", 1, new ExprTimestampValue("2003-01-02 00:00:00"), "2003-01-09 00:00:00"), + + // Cases surrounding leap year + Arguments.of( + "SECOND", 1, new ExprTimestampValue("2020-02-28 23:59:59"), "2020-02-29 00:00:00"), + Arguments.of( + "MINUTE", 1, new ExprTimestampValue("2020-02-28 23:59:59"), "2020-02-29 00:00:59"), + Arguments.of( + "HOUR", 1, new ExprTimestampValue("2020-02-28 23:59:59"), "2020-02-29 00:59:59"), + Arguments.of( + "DAY", 1, new ExprTimestampValue("2020-02-28 23:59:59"), "2020-02-29 23:59:59"), + Arguments.of( + "WEEK", 1, new ExprTimestampValue("2020-02-28 23:59:59"), "2020-03-06 23:59:59"), + + // Cases surrounding end-of-year + Arguments.of( + "SECOND", 1, new ExprTimestampValue("2020-12-31 23:59:59"), "2021-01-01 00:00:00"), + Arguments.of( + "MINUTE", 1, new ExprTimestampValue("2020-12-31 23:59:59"), "2021-01-01 00:00:59"), + Arguments.of( + "HOUR", 1, new ExprTimestampValue("2020-12-31 23:59:59"), "2021-01-01 00:59:59"), + Arguments.of( + "DAY", 1, new ExprTimestampValue("2020-12-31 23:59:59"), "2021-01-01 23:59:59"), + Arguments.of( + "WEEK", 1, new ExprTimestampValue("2020-12-31 23:59:59"), "2021-01-07 23:59:59"), + + // Test adding a month (including special cases) + Arguments.of("MONTH", 1, new ExprStringValue("2003-01-02 00:00:00"), "2003-02-02 00:00:00"), + Arguments.of("MONTH", 1, new ExprDateValue("2024-03-30"), "2024-04-30 00:00:00"), + Arguments.of("MONTH", 1, new ExprDateValue("2024-03-31"), "2024-04-30 00:00:00"), + + // Test remaining interval types + Arguments.of( + "MICROSECOND", + 123, + new ExprStringValue("2003-01-02 00:00:00"), "2003-01-02 00:00:00.000123"), - Arguments.of("QUARTER", 1, new ExprStringValue("2003-01-02 00:00:00"), - "2003-04-02 00:00:00"), - Arguments.of("YEAR", 1, new ExprStringValue("2003-01-02 00:00:00"), - "2004-01-02 00:00:00"), - - //Test negative value for amount (Test for all intervals) - Arguments.of("MICROSECOND", -1, new ExprStringValue("2000-01-01 00:00:00"), + Arguments.of( + "QUARTER", 1, new ExprStringValue("2003-01-02 00:00:00"), "2003-04-02 00:00:00"), + Arguments.of("YEAR", 1, new ExprStringValue("2003-01-02 00:00:00"), "2004-01-02 00:00:00"), + + // Test negative value for amount (Test for all intervals) + Arguments.of( + "MICROSECOND", + -1, + new ExprStringValue("2000-01-01 00:00:00"), "1999-12-31 23:59:59.999999"), - Arguments.of("SECOND", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-31 23:59:59"), - Arguments.of("MINUTE", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-31 23:59:00"), - Arguments.of("HOUR", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-31 23:00:00"), - Arguments.of("DAY", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-31 00:00:00"), - Arguments.of("WEEK", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-25 00:00:00"), - Arguments.of("MONTH", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-12-01 00:00:00"), - Arguments.of("QUARTER", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-10-01 00:00:00"), - Arguments.of("YEAR", -1, new ExprStringValue("2000-01-01 00:00:00"), - "1999-01-01 00:00:00") - ); + Arguments.of( + "SECOND", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-31 23:59:59"), + Arguments.of( + "MINUTE", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-31 23:59:00"), + Arguments.of("HOUR", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-31 23:00:00"), + Arguments.of("DAY", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-31 00:00:00"), + Arguments.of("WEEK", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-25 00:00:00"), + Arguments.of( + "MONTH", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-12-01 00:00:00"), + Arguments.of( + "QUARTER", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-10-01 00:00:00"), + Arguments.of( + "YEAR", -1, new ExprStringValue("2000-01-01 00:00:00"), "1999-01-01 00:00:00")); } - private static FunctionExpression timestampaddQuery(String unit, - int amount, - ExprValue datetimeExpr) { + private static FunctionExpression timestampaddQuery( + String unit, int amount, ExprValue datetimeExpr) { return DSL.timestampadd( - DSL.literal(unit), - DSL.literal(new ExprIntegerValue(amount)), - DSL.literal(datetimeExpr) - ); + DSL.literal(unit), DSL.literal(new ExprIntegerValue(amount)), DSL.literal(datetimeExpr)); } @ParameterizedTest @@ -159,22 +149,19 @@ private static Stream getTestDataForTestAddingDatePartToTime() { Arguments.of("YEAR", 1, "10:11:12", LocalDate.now().plusYears(1)), Arguments.of("YEAR", 5, "10:11:12", LocalDate.now().plusYears(5)), Arguments.of("YEAR", 10, "10:11:12", LocalDate.now().plusYears(10)), - Arguments.of("YEAR", -10, "10:11:12", LocalDate.now().plusYears(-10)) - ); + Arguments.of("YEAR", -10, "10:11:12", LocalDate.now().plusYears(-10))); } @ParameterizedTest @MethodSource("getTestDataForTestAddingDatePartToTime") - public void testAddingDatePartToTime(String interval, - int addedInterval, - String timeArg, - LocalDate expectedDate) { - FunctionExpression expr = DSL.timestampadd( - functionProperties, - DSL.literal(interval), - DSL.literal(new ExprIntegerValue(addedInterval)), - DSL.literal(new ExprTimeValue(timeArg)) - ); + public void testAddingDatePartToTime( + String interval, int addedInterval, String timeArg, LocalDate expectedDate) { + FunctionExpression expr = + DSL.timestampadd( + functionProperties, + DSL.literal(interval), + DSL.literal(new ExprIntegerValue(addedInterval)), + DSL.literal(new ExprTimeValue(timeArg))); LocalDateTime expected1 = LocalDateTime.of(expectedDate, LocalTime.parse(timeArg)); @@ -187,16 +174,15 @@ public void testAddingTimePartToTime() { int addedInterval = 1; String timeArg = "10:11:12"; - FunctionExpression expr = DSL.timestampadd( - functionProperties, - DSL.literal(interval), - DSL.literal(new ExprIntegerValue(addedInterval)), - DSL.literal(new ExprTimeValue(timeArg)) - ); + FunctionExpression expr = + DSL.timestampadd( + functionProperties, + DSL.literal(interval), + DSL.literal(new ExprIntegerValue(addedInterval)), + DSL.literal(new ExprTimeValue(timeArg))); - LocalDateTime expected = LocalDateTime.of( - LocalDate.now(), - LocalTime.parse(timeArg).plusMinutes(addedInterval)); + LocalDateTime expected = + LocalDateTime.of(LocalDate.now(), LocalTime.parse(timeArg).plusMinutes(addedInterval)); assertEquals(new ExprDatetimeValue(expected), eval(expr)); } @@ -205,38 +191,27 @@ public void testAddingTimePartToTime() { public void testDifferentInputTypesHaveSameResult() { String part = "SECOND"; int amount = 1; - FunctionExpression dateExpr = timestampaddQuery( - part, - amount, - new ExprDateValue("2000-01-01")); - - FunctionExpression stringExpr = timestampaddQuery( - part, - amount, - new ExprStringValue("2000-01-01 00:00:00")); - - FunctionExpression datetimeExpr = timestampaddQuery( - part, - amount, - new ExprDatetimeValue("2000-01-01 00:00:00")); - - FunctionExpression timestampExpr = timestampaddQuery( - part, - amount, - new ExprTimestampValue("2000-01-01 00:00:00")); + FunctionExpression dateExpr = timestampaddQuery(part, amount, new ExprDateValue("2000-01-01")); + + FunctionExpression stringExpr = + timestampaddQuery(part, amount, new ExprStringValue("2000-01-01 00:00:00")); + + FunctionExpression datetimeExpr = + timestampaddQuery(part, amount, new ExprDatetimeValue("2000-01-01 00:00:00")); + + FunctionExpression timestampExpr = + timestampaddQuery(part, amount, new ExprTimestampValue("2000-01-01 00:00:00")); assertAll( () -> assertEquals(eval(dateExpr), eval(stringExpr)), () -> assertEquals(eval(dateExpr), eval(datetimeExpr)), - () -> assertEquals(eval(dateExpr), eval(timestampExpr)) - ); + () -> assertEquals(eval(dateExpr), eval(timestampExpr))); } private static Stream getInvalidTestDataForTimestampAdd() { return Stream.of( Arguments.of("WEEK", 1, new ExprStringValue("2000-13-01")), - Arguments.of("WEEK", 1, new ExprStringValue("2000-01-40")) - ); + Arguments.of("WEEK", 1, new ExprStringValue("2000-01-40"))); } @ParameterizedTest diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampDiffTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampDiffTest.java index b00792e0489..061420ceee2 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampDiffTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/TimeStampDiffTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertAll; @@ -37,11 +36,9 @@ class TimeStampDiffTest extends ExpressionTestBase { - //Helper function to create an argument based on a passed in interval type - private static ExprValue generateArg(String intervalType, - String argType, - LocalDateTime base, - int added) { + // Helper function to create an argument based on a passed in interval type + private static ExprValue generateArg( + String intervalType, String argType, LocalDateTime base, int added) { LocalDateTime arg; switch (intervalType) { case "MICROSECOND": @@ -72,9 +69,8 @@ private static ExprValue generateArg(String intervalType, arg = base.plusYears(added); break; default: - throw new SemanticCheckException(String.format( - "%s is not a valid interval type.", - intervalType)); + throw new SemanticCheckException( + String.format("%s is not a valid interval type.", intervalType)); } switch (argType) { @@ -87,92 +83,69 @@ private static ExprValue generateArg(String intervalType, case "DATETIME": return new ExprDatetimeValue(arg); case "STRING": - return new ExprStringValue(String.format( - "%04d-%02d-%02d %02d:%02d:%02d.%06d", - arg.getYear(), - arg.getMonthValue(), - arg.getDayOfMonth(), - arg.getHour(), - arg.getMinute(), - arg.getSecond(), - arg.getNano() / 1000)); + return new ExprStringValue( + String.format( + "%04d-%02d-%02d %02d:%02d:%02d.%06d", + arg.getYear(), + arg.getMonthValue(), + arg.getDayOfMonth(), + arg.getHour(), + arg.getMinute(), + arg.getSecond(), + arg.getNano() / 1000)); default: - throw new SemanticCheckException(String.format( - "%s is not a valid ExprCoreValueType.", - argType)); + throw new SemanticCheckException( + String.format("%s is not a valid ExprCoreValueType.", argType)); } } - //Generate test data to test all permutations for args (intervalType, arg1, arg2) + // Generate test data to test all permutations for args (intervalType, arg1, arg2) private static Stream getGeneralTestDataForTimestampDiff() { - //Needs to be initialized with a value to prevent a null pointer exception. - Stream testData = Stream.of(Arguments.of( - "DAY", - new ExprDateValue("2000-01-01 00:00:00"), - new ExprDateValue("2000-01-01"), - 0)); - - final String[] timeIntervalTypes = { - "MICROSECOND", - "SECOND", - "MINUTE", - "HOUR" - }; - - final String[] dateIntervalTypes = { - "DAY", - "WEEK", - "MONTH", - "QUARTER", - "YEAR" - }; + // Needs to be initialized with a value to prevent a null pointer exception. + Stream testData = + Stream.of( + Arguments.of( + "DAY", + new ExprDateValue("2000-01-01 00:00:00"), + new ExprDateValue("2000-01-01"), + 0)); + + final String[] timeIntervalTypes = {"MICROSECOND", "SECOND", "MINUTE", "HOUR"}; + final String[] dateIntervalTypes = {"DAY", "WEEK", "MONTH", "QUARTER", "YEAR"}; final String[] intervalTypes = ArrayUtils.addAll(timeIntervalTypes, dateIntervalTypes); - //TIME type not included here as it is a special case handled by a different test - final String[] expressionTypes = { - "DATE", - "DATETIME", - "TIMESTAMP", - "STRING" - }; + // TIME type not included here as it is a special case handled by a different test + final String[] expressionTypes = {"DATE", "DATETIME", "TIMESTAMP", "STRING"}; final LocalDateTime baseDateTime = LocalDateTime.of(2000, 1, 1, 0, 0, 0); final int intervalDifference = 5; - //Iterate through each permutation of argument + // Iterate through each permutation of argument for (String intervalType : intervalTypes) { for (String firstArgExpressionType : expressionTypes) { for (String secondArgExpressionType : expressionTypes) { ExprValue firstArg = generateArg(intervalType, firstArgExpressionType, baseDateTime, 0); - ExprValue secondArg = generateArg( - intervalType, - secondArgExpressionType, - baseDateTime, - intervalDifference); - - //If second arg is a DATE and you are using a unit of TIME to measure then expected is 0. - //The second arg is equal to baseDatetime in this case. - int expected = ( - secondArgExpressionType == "DATE" - && Arrays.asList(timeIntervalTypes).contains(intervalType)) - ? 0 : intervalDifference; - - testData = Stream.concat(testData, Stream.of( - Arguments.of( - intervalType, - firstArg, - secondArg, - expected), - Arguments.of( - intervalType, - secondArg, - firstArg, - -expected) - )); + ExprValue secondArg = + generateArg(intervalType, secondArgExpressionType, baseDateTime, intervalDifference); + + // If second arg is a DATE and you are using a unit of TIME to measure then expected is 0. + // The second arg is equal to baseDatetime in this case. + int expected = + (secondArgExpressionType == "DATE" + && Arrays.asList(timeIntervalTypes).contains(intervalType)) + ? 0 + : intervalDifference; + + testData = + Stream.concat( + testData, + Stream.of( + Arguments.of(intervalType, firstArg, secondArg, expected), + Arguments.of(intervalType, secondArg, firstArg, -expected))); } } } @@ -183,7 +156,7 @@ private static Stream getGeneralTestDataForTimestampDiff() { private static Stream getCornerCaseTestDataForTimestampDiff() { return Stream.of( - //Test around Leap Year + // Test around Leap Year Arguments.of( "DAY", new ExprDatetimeValue("2019-02-28 00:00:00"), @@ -195,7 +168,7 @@ private static Stream getCornerCaseTestDataForTimestampDiff() { new ExprDatetimeValue("2020-03-01 00:00:00"), 2), - //Test around year change + // Test around year change Arguments.of( "SECOND", new ExprDatetimeValue("2019-12-31 23:59:59"), @@ -210,33 +183,27 @@ private static Stream getCornerCaseTestDataForTimestampDiff() { "DAY", new ExprDatetimeValue("2019-12-31 00:00:00"), new ExprDatetimeValue("2020-01-01 00:00:00"), - 1) - ); + 1)); } - private static FunctionExpression timestampdiffQuery(FunctionProperties functionProperties, - String unit, - ExprValue datetimeExpr1, - ExprValue datetimeExpr2) { + private static FunctionExpression timestampdiffQuery( + FunctionProperties functionProperties, + String unit, + ExprValue datetimeExpr1, + ExprValue datetimeExpr2) { return DSL.timestampdiff( functionProperties, DSL.literal(unit), DSL.literal(datetimeExpr1), - DSL.literal(datetimeExpr2) - ); + DSL.literal(datetimeExpr2)); } @ParameterizedTest @MethodSource({"getGeneralTestDataForTimestampDiff", "getCornerCaseTestDataForTimestampDiff"}) - public void testTimestampdiff(String unit, - ExprValue datetimeExpr1, - ExprValue datetimeExpr2, - long expected) { - FunctionExpression expr = timestampdiffQuery( - functionProperties, - unit, - datetimeExpr1, - datetimeExpr2); + public void testTimestampdiff( + String unit, ExprValue datetimeExpr1, ExprValue datetimeExpr2, long expected) { + FunctionExpression expr = + timestampdiffQuery(functionProperties, unit, datetimeExpr1, datetimeExpr2); assertEquals(expected, eval(expr).longValue()); } @@ -250,11 +217,10 @@ private static Stream getUnits() { Arguments.of("WEEK"), Arguments.of("MONTH"), Arguments.of("QUARTER"), - Arguments.of("YEAR") - ); + Arguments.of("YEAR")); } - //Test that Time arg uses today's date with all interval/part arguments + // Test that Time arg uses today's date with all interval/part arguments @ParameterizedTest @MethodSource("getUnits") public void testTimestampDiffWithTimeType(String unit) { @@ -266,16 +232,11 @@ public void testTimestampDiffWithTimeType(String unit) { ExprValue datetimeExpr = generateArg(unit, "TIMESTAMP", base, 0); ExprValue stringExpr = generateArg(unit, "TIMESTAMP", base, 0); - ExprValue[] expressions = {timeExpr, timestampExpr, dateExpr, datetimeExpr,stringExpr}; + ExprValue[] expressions = {timeExpr, timestampExpr, dateExpr, datetimeExpr, stringExpr}; for (ExprValue arg1 : expressions) { for (ExprValue arg2 : expressions) { - FunctionExpression funcExpr = timestampdiffQuery( - functionProperties, - unit, - arg1, - arg2 - ); + FunctionExpression funcExpr = timestampdiffQuery(functionProperties, unit, arg1, arg2); assertEquals(0L, eval(funcExpr).longValue()); } @@ -293,66 +254,65 @@ private static Stream getTimestampDiffInvalidArgs() { Arguments.of("SECOND", "2023-01-01 10:11:12", "2000-01-40 10:11:12"), Arguments.of("SECOND", "2023-01-01 10:11:12", "2000-01-01 25:11:12"), Arguments.of("SECOND", "2023-01-01 10:11:12", "2000-01-01 10:70:12"), - Arguments.of("SECOND", "2023-01-01 10:11:12", "2000-01-01 10:11:70") - ); + Arguments.of("SECOND", "2023-01-01 10:11:12", "2000-01-01 10:11:70")); } @ParameterizedTest @MethodSource("getTimestampDiffInvalidArgs") public void testTimestampDiffWithInvalidTimeArgs(String unit, String arg1, String arg2) { - FunctionExpression expr = timestampdiffQuery( - functionProperties, - unit, - new ExprStringValue(arg1), - new ExprStringValue(arg2) - ); + FunctionExpression expr = + timestampdiffQuery( + functionProperties, unit, new ExprStringValue(arg1), new ExprStringValue(arg2)); assertThrows(SemanticCheckException.class, () -> eval(expr)); } @Test public void testTimestampDiffWithInvalidPartReturnsNull() { - FunctionExpression expr = timestampdiffQuery( - functionProperties, + FunctionExpression expr = + timestampdiffQuery( + functionProperties, "INVALID", - new ExprStringValue("2023-01-01 10:11:12"), - new ExprStringValue("2000-01-01 10:11:12") - ); + new ExprStringValue("2023-01-01 10:11:12"), + new ExprStringValue("2000-01-01 10:11:12")); assertEquals(ExprNullValue.of(), eval(expr)); } - //Test that different input types have the same result + // Test that different input types have the same result @Test public void testDifferentInputTypesHaveSameResult() { String part = "SECOND"; - FunctionExpression dateExpr = timestampdiffQuery( - functionProperties, - part, - new ExprDateValue("2000-01-01"), - new ExprDateValue("2000-01-02")); + FunctionExpression dateExpr = + timestampdiffQuery( + functionProperties, + part, + new ExprDateValue("2000-01-01"), + new ExprDateValue("2000-01-02")); - FunctionExpression stringExpr = timestampdiffQuery( - functionProperties, - part, - new ExprStringValue("2000-01-01 00:00:00"), - new ExprStringValue("2000-01-02 00:00:00")); + FunctionExpression stringExpr = + timestampdiffQuery( + functionProperties, + part, + new ExprStringValue("2000-01-01 00:00:00"), + new ExprStringValue("2000-01-02 00:00:00")); - FunctionExpression datetimeExpr = timestampdiffQuery( - functionProperties, - part, - new ExprDatetimeValue("2000-01-01 00:00:00"), - new ExprDatetimeValue("2000-01-02 00:00:00")); + FunctionExpression datetimeExpr = + timestampdiffQuery( + functionProperties, + part, + new ExprDatetimeValue("2000-01-01 00:00:00"), + new ExprDatetimeValue("2000-01-02 00:00:00")); - FunctionExpression timestampExpr = timestampdiffQuery( - functionProperties, - part, - new ExprTimestampValue("2000-01-01 00:00:00"), - new ExprTimestampValue("2000-01-02 00:00:00")); + FunctionExpression timestampExpr = + timestampdiffQuery( + functionProperties, + part, + new ExprTimestampValue("2000-01-01 00:00:00"), + new ExprTimestampValue("2000-01-02 00:00:00")); assertAll( () -> assertEquals(eval(dateExpr), eval(stringExpr)), () -> assertEquals(eval(dateExpr), eval(datetimeExpr)), - () -> assertEquals(eval(dateExpr), eval(timestampExpr)) - ); + () -> assertEquals(eval(dateExpr), eval(timestampExpr))); } private ExprValue eval(Expression expression) { diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/TimestampTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/TimestampTest.java index 957dac5c741..2017a640d2c 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/TimestampTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/TimestampTest.java @@ -39,35 +39,39 @@ public void timestamp_one_arg_string() { expr = DSL.timestamp(functionProperties, DSL.literal("1961-04-12 09:07:00.123456")); assertEquals(TIMESTAMP, expr.type()); - assertEquals(LocalDateTime.of(1961, 4, 12, 9, 7, 0, 123456000), - expr.valueOf().datetimeValue()); + assertEquals(LocalDateTime.of(1961, 4, 12, 9, 7, 0, 123456000), expr.valueOf().datetimeValue()); } /** * Check that `TIMESTAMP` function throws an exception on incorrect string input. + * * @param value A value. * @param testName A test name. */ @ParameterizedTest(name = "{1}") @CsvSource({ - "1984-02-30 12:20:42, Feb 30th", - "1984-02-10 24:00:00, 24:00:00", - "84-02-10 12:20:42, 2 digit year" + "1984-02-30 12:20:42, Feb 30th", + "1984-02-10 24:00:00, 24:00:00", + "84-02-10 12:20:42, 2 digit year" }) public void timestamp_one_arg_string_invalid_format(String value, String testName) { // exception thrown from ExprTimestampValue(String) CTOR - var exception = assertThrows(SemanticCheckException.class, - () -> DSL.timestamp(functionProperties, DSL.literal(value)).valueOf()); - assertEquals(String.format("timestamp:%s in unsupported format, please " - + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", value), exception.getMessage()); + var exception = + assertThrows( + SemanticCheckException.class, + () -> DSL.timestamp(functionProperties, DSL.literal(value)).valueOf()); + assertEquals( + String.format( + "timestamp:%s in unsupported format, please " + "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", + value), + exception.getMessage()); } @Test public void timestamp_one_arg_time() { var expr = DSL.timestamp(functionProperties, DSL.time(DSL.literal("22:33:44"))); assertEquals(TIMESTAMP, expr.type()); - var refValue = LocalDate.now().atTime(LocalTime.of(22, 33, 44)) - .atZone(UTC_ZONE_ID).toInstant(); + var refValue = LocalDate.now().atTime(LocalTime.of(22, 33, 44)).atZone(UTC_ZONE_ID).toInstant(); assertEquals(new ExprTimestampValue(refValue), expr.valueOf()); } @@ -75,8 +79,7 @@ public void timestamp_one_arg_time() { public void timestamp_one_arg_date() { var expr = DSL.timestamp(functionProperties, DSL.date(DSL.literal("2077-12-15"))); assertEquals(TIMESTAMP, expr.type()); - var refValue = LocalDate.of(2077, 12, 15).atStartOfDay() - .atZone(UTC_ZONE_ID).toInstant(); + var refValue = LocalDate.of(2077, 12, 15).atStartOfDay().atZone(UTC_ZONE_ID).toInstant(); assertEquals(new ExprTimestampValue(refValue), expr.valueOf()); } @@ -90,8 +93,8 @@ public void timestamp_one_arg_datetime() { @Test public void timestamp_one_arg_timestamp() { var refValue = new ExprTimestampValue(Instant.ofEpochSecond(10050042)); - var expr = DSL.timestamp(functionProperties, - DSL.timestamp(functionProperties, DSL.literal(refValue))); + var expr = + DSL.timestamp(functionProperties, DSL.timestamp(functionProperties, DSL.literal(refValue))); assertEquals(TIMESTAMP, expr.type()); assertEquals(refValue, expr.valueOf()); } @@ -109,71 +112,115 @@ private static Stream getTestData() { // First argument of `TIMESTAMP` function, second argument and expected result value return Stream.of( // STRING and STRING/DATE/TIME/DATETIME/TIMESTAMP - Arguments.of("1961-04-12 09:07:00", "2077-12-15 01:48:00", + Arguments.of( + "1961-04-12 09:07:00", + "2077-12-15 01:48:00", dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of("1984-02-10 12:20:42", LocalDate.of(2077, 12, 21), + Arguments.of( + "1984-02-10 12:20:42", + LocalDate.of(2077, 12, 21), dateTime2ExprTs(LocalDateTime.of(1984, 2, 10, 12, 20, 42))), - Arguments.of("1961-04-12 09:07:00", LocalTime.of(1, 48), + Arguments.of( + "1961-04-12 09:07:00", + LocalTime.of(1, 48), dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of("2020-12-31 17:30:00", LocalDateTime.of(2077, 12, 21, 12, 20, 42), + Arguments.of( + "2020-12-31 17:30:00", + LocalDateTime.of(2077, 12, 21, 12, 20, 42), dateTime2ExprTs(LocalDateTime.of(2021, 1, 1, 5, 50, 42))), - Arguments.of("2020-12-31 17:30:00", Instant.ofEpochSecond(42), + Arguments.of( + "2020-12-31 17:30:00", + Instant.ofEpochSecond(42), dateTime2ExprTs(LocalDateTime.of(2020, 12, 31, 17, 30, 42))), // DATE and STRING/DATE/TIME/DATETIME/TIMESTAMP - Arguments.of(LocalDate.of(2077, 12, 21), "2077-12-15 01:48:00", + Arguments.of( + LocalDate.of(2077, 12, 21), + "2077-12-15 01:48:00", dateTime2ExprTs(LocalDateTime.of(2077, 12, 21, 1, 48, 0))), - Arguments.of(LocalDate.of(2077, 12, 21), LocalDate.of(1984, 2, 3), + Arguments.of( + LocalDate.of(2077, 12, 21), + LocalDate.of(1984, 2, 3), dateTime2ExprTs(LocalDateTime.of(2077, 12, 21, 0, 0, 0))), - Arguments.of(LocalDate.of(2077, 12, 21), LocalTime.of(22, 33, 44), + Arguments.of( + LocalDate.of(2077, 12, 21), + LocalTime.of(22, 33, 44), dateTime2ExprTs(LocalDateTime.of(2077, 12, 21, 22, 33, 44))), - Arguments.of(LocalDate.of(2077, 12, 21), LocalDateTime.of(1999, 9, 9, 22, 33, 44), + Arguments.of( + LocalDate.of(2077, 12, 21), + LocalDateTime.of(1999, 9, 9, 22, 33, 44), dateTime2ExprTs(LocalDateTime.of(2077, 12, 21, 22, 33, 44))), - Arguments.of(LocalDate.of(2077, 12, 21), Instant.ofEpochSecond(42), + Arguments.of( + LocalDate.of(2077, 12, 21), + Instant.ofEpochSecond(42), dateTime2ExprTs(LocalDateTime.of(2077, 12, 21, 0, 0, 42))), // TIME and STRING/DATE/TIME/DATETIME/TIMESTAMP - Arguments.of(LocalTime.of(9, 7, 0), "2077-12-15 01:48:00", + Arguments.of( + LocalTime.of(9, 7, 0), + "2077-12-15 01:48:00", dateTime2ExprTs(today.atTime(LocalTime.of(10, 55, 0)))), - Arguments.of(LocalTime.of(12, 20, 42), LocalDate.of(2077, 12, 21), + Arguments.of( + LocalTime.of(12, 20, 42), + LocalDate.of(2077, 12, 21), dateTime2ExprTs(today.atTime(LocalTime.of(12, 20, 42)))), - Arguments.of(LocalTime.of(9, 7, 0), LocalTime.of(1, 48), + Arguments.of( + LocalTime.of(9, 7, 0), + LocalTime.of(1, 48), dateTime2ExprTs(today.atTime(LocalTime.of(10, 55, 0)))), - Arguments.of(LocalTime.of(17, 30, 0), LocalDateTime.of(2077, 12, 21, 12, 20, 42), + Arguments.of( + LocalTime.of(17, 30, 0), + LocalDateTime.of(2077, 12, 21, 12, 20, 42), dateTime2ExprTs(today.plusDays(1).atTime(LocalTime.of(5, 50, 42)))), - Arguments.of(LocalTime.of(17, 30, 0), Instant.ofEpochSecond(42), + Arguments.of( + LocalTime.of(17, 30, 0), + Instant.ofEpochSecond(42), dateTime2ExprTs(today.atTime(LocalTime.of(17, 30, 42)))), // DATETIME and STRING/DATE/TIME/DATETIME/TIMESTAMP - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7, 0), "2077-12-15 01:48:00", + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7, 0), + "2077-12-15 01:48:00", dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of(LocalDateTime.of(1984, 2, 10, 12, 20, 42), LocalDate.of(2077, 12, 21), + Arguments.of( + LocalDateTime.of(1984, 2, 10, 12, 20, 42), + LocalDate.of(2077, 12, 21), dateTime2ExprTs(LocalDateTime.of(1984, 2, 10, 12, 20, 42))), - Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7, 0), LocalTime.of(1, 48), + Arguments.of( + LocalDateTime.of(1961, 4, 12, 9, 7, 0), + LocalTime.of(1, 48), dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of(LocalDateTime.of(2020, 12, 31, 17, 30, 0), + Arguments.of( + LocalDateTime.of(2020, 12, 31, 17, 30, 0), LocalDateTime.of(2077, 12, 21, 12, 20, 42), dateTime2ExprTs(LocalDateTime.of(2021, 1, 1, 5, 50, 42))), - Arguments.of(LocalDateTime.of(2020, 12, 31, 17, 30, 0), Instant.ofEpochSecond(42), + Arguments.of( + LocalDateTime.of(2020, 12, 31, 17, 30, 0), + Instant.ofEpochSecond(42), dateTime2ExprTs(LocalDateTime.of(2020, 12, 31, 17, 30, 42))), // TIMESTAMP and STRING/DATE/TIME/DATETIME/TIMESTAMP - Arguments.of(dateTime2Instant(LocalDateTime.of(1961, 4, 12, 9, 7, 0)), + Arguments.of( + dateTime2Instant(LocalDateTime.of(1961, 4, 12, 9, 7, 0)), "2077-12-15 01:48:00", dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of(dateTime2Instant(LocalDateTime.of(1984, 2, 10, 12, 20, 42)), + Arguments.of( + dateTime2Instant(LocalDateTime.of(1984, 2, 10, 12, 20, 42)), LocalDate.of(2077, 12, 21), dateTime2ExprTs(LocalDateTime.of(1984, 2, 10, 12, 20, 42))), - Arguments.of(dateTime2Instant(LocalDateTime.of(1961, 4, 12, 9, 7, 0)), + Arguments.of( + dateTime2Instant(LocalDateTime.of(1961, 4, 12, 9, 7, 0)), LocalTime.of(1, 48), dateTime2ExprTs(LocalDateTime.of(1961, 4, 12, 10, 55, 0))), - Arguments.of(dateTime2Instant(LocalDateTime.of(2020, 12, 31, 17, 30, 0)), + Arguments.of( + dateTime2Instant(LocalDateTime.of(2020, 12, 31, 17, 30, 0)), LocalDateTime.of(2077, 12, 21, 12, 20, 42), dateTime2ExprTs(LocalDateTime.of(2021, 1, 1, 5, 50, 42))), - Arguments.of(dateTime2Instant(LocalDateTime.of(2020, 12, 31, 17, 30, 0)), + Arguments.of( + dateTime2Instant(LocalDateTime.of(2020, 12, 31, 17, 30, 0)), Instant.ofEpochSecond(42), - dateTime2ExprTs(LocalDateTime.of(2020, 12, 31, 17, 30, 42))) - ); + dateTime2ExprTs(LocalDateTime.of(2020, 12, 31, 17, 30, 42)))); } /** * Test `TIMESTAMP` function which takes 2 arguments with input of different types. + * * @param arg1 First argument to be passed to `TIMESTAMP` function. * @param arg2 Second argument to be passed to `TIMESTAMP` function. * @param expected The expected result. @@ -181,9 +228,11 @@ private static Stream getTestData() { @ParameterizedTest @MethodSource("getTestData") public void timestamp_with_two_args(Object arg1, Object arg2, ExprTimestampValue expected) { - var expr = DSL.timestamp(functionProperties, - DSL.literal(ExprValueUtils.fromObjectValue(arg1)), - DSL.literal(ExprValueUtils.fromObjectValue(arg2))); + var expr = + DSL.timestamp( + functionProperties, + DSL.literal(ExprValueUtils.fromObjectValue(arg1)), + DSL.literal(ExprValueUtils.fromObjectValue(arg2))); assertEquals(TIMESTAMP, expr.type()); assertEquals(expected, expr.valueOf()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/ToSecondsTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/ToSecondsTest.java index 1e89659de72..7aa824e61de 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/ToSecondsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/ToSecondsTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,7 +34,6 @@ import org.opensearch.sql.expression.ExpressionTestBase; import org.opensearch.sql.expression.FunctionExpression; - class ToSecondsTest extends ExpressionTestBase { private static final long SECONDS_FROM_0001_01_01_TO_EPOCH_START = 62167219200L; @@ -54,11 +52,9 @@ private static Stream getTestDataForToSeconds() { Arguments.of(new ExprStringValue("2009-11-29 00:00:00"), new ExprLongValue(63426672000L)), Arguments.of(new ExprStringValue("2009-11-29 13:43:32"), new ExprLongValue(63426721412L)), Arguments.of(new ExprDateValue("2009-11-29"), new ExprLongValue(63426672000L)), - Arguments.of(new ExprDatetimeValue("2009-11-29 13:43:32"), - new ExprLongValue(63426721412L)), - Arguments.of(new ExprTimestampValue("2009-11-29 13:43:32"), - new ExprLongValue(63426721412L)) - ); + Arguments.of(new ExprDatetimeValue("2009-11-29 13:43:32"), new ExprLongValue(63426721412L)), + Arguments.of( + new ExprTimestampValue("2009-11-29 13:43:32"), new ExprLongValue(63426721412L))); } @ParameterizedTest @@ -71,12 +67,13 @@ public void testToSeconds(ExprValue arg, ExprValue expected) { @Test public void testToSecondsWithTimeType() { - FunctionExpression expr = DSL.to_seconds(functionProperties, - DSL.literal(new ExprTimeValue("10:11:12"))); + FunctionExpression expr = + DSL.to_seconds(functionProperties, DSL.literal(new ExprTimeValue("10:11:12"))); - long expected = SECONDS_FROM_0001_01_01_TO_EPOCH_START - + LocalDate.now(functionProperties.getQueryStartClock()) - .toEpochSecond(LocalTime.parse("10:11:12"), ZoneOffset.UTC); + long expected = + SECONDS_FROM_0001_01_01_TO_EPOCH_START + + LocalDate.now(functionProperties.getQueryStartClock()) + .toEpochSecond(LocalTime.parse("10:11:12"), ZoneOffset.UTC); assertEquals(expected, eval(expr).longValue()); } @@ -88,8 +85,7 @@ private static Stream getInvalidTestDataForToSeconds() { Arguments.of(new ExprStringValue("2000-10-45")), Arguments.of(new ExprStringValue("2000-10-10 70:00:00")), Arguments.of(new ExprStringValue("2000-10-10 00:70:00")), - Arguments.of(new ExprStringValue("2000-10-10 00:00:70")) - ); + Arguments.of(new ExprStringValue("2000-10-10 00:00:70"))); } @ParameterizedTest @@ -106,9 +102,10 @@ public void testToSecondsWithDateAdd() { long addedSeconds = SECONDS_PER_DAY; long expected = eval(dateExpr).longValue() + addedSeconds; - FunctionExpression dateAddExpr = DSL.date_add( - DSL.literal(new ExprDateValue(date)), - DSL.literal(new ExprIntervalValue(Duration.ofSeconds(addedSeconds)))); + FunctionExpression dateAddExpr = + DSL.date_add( + DSL.literal(new ExprDateValue(date)), + DSL.literal(new ExprIntervalValue(Duration.ofSeconds(addedSeconds)))); long result = eval(DSL.to_seconds(DSL.literal(eval(dateAddExpr)))).longValue(); diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTimeStampTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTimeStampTest.java index f6e24f4e279..c979b683021 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTimeStampTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTimeStampTest.java @@ -43,19 +43,20 @@ private static Stream getDateSamples() { Arguments.of(LocalDate.of(1999, 12, 31)), Arguments.of(LocalDate.of(2004, 2, 29)), Arguments.of(LocalDate.of(2100, 2, 28)), - Arguments.of(LocalDate.of(2012, 2, 21)) - ); + Arguments.of(LocalDate.of(2012, 2, 21))); } /** * Check processing valid values of type LocalDate. + * * @param value a value */ @ParameterizedTest @MethodSource("getDateSamples") public void checkOfDate(LocalDate value) { assertEquals(value.getLong(ChronoField.EPOCH_DAY) * 24 * 3600, unixTimeStampOf(value)); - assertEquals(value.getLong(ChronoField.EPOCH_DAY) * 24 * 3600, + assertEquals( + value.getLong(ChronoField.EPOCH_DAY) * 24 * 3600, eval(unixTimeStampOf(DSL.literal(new ExprDateValue(value)))).longValue()); } @@ -66,36 +67,39 @@ private static Stream getDateTimeSamples() { Arguments.of(LocalDateTime.of(1999, 12, 31, 23, 59)), Arguments.of(LocalDateTime.of(2004, 2, 29, 7, 40)), Arguments.of(LocalDateTime.of(2100, 2, 28, 13, 14)), - Arguments.of(LocalDateTime.of(2012, 2, 21, 0, 0)) - ); + Arguments.of(LocalDateTime.of(2012, 2, 21, 0, 0))); } /** * Check processing valid values of type LocalDateTime. + * * @param value a value */ @ParameterizedTest @MethodSource("getDateTimeSamples") public void checkOfDateTime(LocalDateTime value) { assertEquals(value.toEpochSecond(ZoneOffset.UTC), unixTimeStampOf(value)); - assertEquals(value.toEpochSecond(ZoneOffset.UTC), + assertEquals( + value.toEpochSecond(ZoneOffset.UTC), eval(unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value)))).longValue()); } private static Stream getInstantSamples() { return getDateTimeSamples() - .map(v -> Arguments.of(((LocalDateTime)v.get()[0]).toInstant(ZoneOffset.UTC))); + .map(v -> Arguments.of(((LocalDateTime) v.get()[0]).toInstant(ZoneOffset.UTC))); } /** * Check processing valid values of type Instant. + * * @param value a value */ @ParameterizedTest @MethodSource("getInstantSamples") public void checkOfInstant(Instant value) { assertEquals(value.getEpochSecond(), unixTimeStampOf(value)); - assertEquals(value.getEpochSecond(), + assertEquals( + value.getEpochSecond(), eval(unixTimeStampOf(DSL.literal(new ExprTimestampValue(value)))).longValue()); } @@ -104,17 +108,17 @@ public void checkOfInstant(Instant value) { private static Stream getDoubleSamples() { return Stream.of( Arguments.of(840101d, LocalDateTime.of(1984, 1, 1, 0, 0, 0)), - Arguments.of(840101112233d, LocalDateTime.of(1984, 1, 1, 11,22,33)), + Arguments.of(840101112233d, LocalDateTime.of(1984, 1, 1, 11, 22, 33)), Arguments.of(840101112233.123456, LocalDateTime.of(1984, 1, 1, 11, 22, 33, 123456000)), Arguments.of(19840101d, LocalDateTime.of(1984, 1, 1, 0, 0, 0)), Arguments.of(19840101000000d, LocalDateTime.of(1984, 1, 1, 0, 0, 0)), - Arguments.of(19840101112233d, LocalDateTime.of(1984, 1, 1, 11,22,33)), - Arguments.of(19840101112233.123456, LocalDateTime.of(1984, 1, 1, 11, 22, 33, 123456000)) - ); + Arguments.of(19840101112233d, LocalDateTime.of(1984, 1, 1, 11, 22, 33)), + Arguments.of(19840101112233.123456, LocalDateTime.of(1984, 1, 1, 11, 22, 33, 123456000))); } /** * Check processing valid Double values. + * * @param valueAsDouble a value * @param valueAsLDT the value as LocalDateTime */ @@ -122,11 +126,13 @@ private static Stream getDoubleSamples() { @MethodSource("getDoubleSamples") public void checkOfDoubleFormats(Double valueAsDouble, LocalDateTime valueAsLDT) { var valueAsStr = new DecimalFormat("0.#").format(valueAsDouble); - assertEquals(valueAsLDT.toEpochSecond(ZoneOffset.UTC), - unixTimeStampOf(valueAsDouble), 1d, valueAsStr); - assertEquals(valueAsLDT.toEpochSecond(ZoneOffset.UTC), + assertEquals( + valueAsLDT.toEpochSecond(ZoneOffset.UTC), unixTimeStampOf(valueAsDouble), 1d, valueAsStr); + assertEquals( + valueAsLDT.toEpochSecond(ZoneOffset.UTC), eval(unixTimeStampOf(DSL.literal(new ExprDoubleValue(valueAsDouble)))).longValue(), - 1d, valueAsStr); + 1d, + valueAsStr); } @Test @@ -165,7 +171,7 @@ public void checkMaxValue() { private static Stream getInvalidDoubleSamples() { return Stream.of( - //invalid dates + // invalid dates Arguments.of(19990231.d), Arguments.of(19991320.d), Arguments.of(19991232.d), @@ -181,7 +187,7 @@ private static Stream getInvalidDoubleSamples() { Arguments.of(9912.d), Arguments.of(199912.d), Arguments.of(1999102.d), - //same as above, but with valid time + // same as above, but with valid time Arguments.of(19990231112233.d), Arguments.of(19991320112233.d), Arguments.of(19991232112233.d), @@ -197,7 +203,7 @@ private static Stream getInvalidDoubleSamples() { Arguments.of(9912112233.d), Arguments.of(199912112233.d), Arguments.of(1999102112233.d), - //invalid time + // invalid time Arguments.of(19840101242233.d), Arguments.of(19840101116033.d), Arguments.of(19840101112260.d), @@ -206,7 +212,7 @@ private static Stream getInvalidDoubleSamples() { Arguments.of(19840101123.d), Arguments.of(1984010113.d), Arguments.of(198401011.d), - //same, but with short date + // same, but with short date Arguments.of(840101242233.d), Arguments.of(840101116033.d), Arguments.of(840101112260.d), @@ -214,23 +220,24 @@ private static Stream getInvalidDoubleSamples() { Arguments.of(8401011123.d), Arguments.of(840101123.d), Arguments.of(8401011.d), - //misc + // misc Arguments.of(0d), Arguments.of(-1d), Arguments.of(42d), - //too many digits - Arguments.of(199902201122330d) - ); + // too many digits + Arguments.of(199902201122330d)); } /** * Check processing invalid Double values. + * * @param value a value */ @ParameterizedTest @MethodSource("getInvalidDoubleSamples") public void checkInvalidDoubleCausesNull(Double value) { - assertEquals(ExprNullValue.of(), + assertEquals( + ExprNullValue.of(), unixTimeStampOf(DSL.literal(new ExprDoubleValue(value))).valueOf(), new DecimalFormat("0.#").format(value)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTwoWayConversionTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTwoWayConversionTest.java index 70fe299e972..c74b062fba1 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTwoWayConversionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/UnixTwoWayConversionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -33,8 +32,7 @@ public void checkConvertNow_with_eval() { } private LocalDateTime getExpectedNow() { - return LocalDateTime.now( - functionProperties.getQueryStartClock().withZone(UTC_ZONE_ID)) + return LocalDateTime.now(functionProperties.getQueryStartClock().withZone(UTC_ZONE_ID)) .withNano(0); } @@ -44,23 +42,25 @@ private static Stream getDoubleSamples() { Arguments.of(100500.100500d), Arguments.of(1447430881.564d), Arguments.of(2147483647.451232d), - Arguments.of(1662577241.d) - ); + Arguments.of(1662577241.d)); } /** * Test converting valid Double values EpochTime -> DateTime -> EpochTime. + * * @param value a value */ @ParameterizedTest @MethodSource("getDoubleSamples") public void convertEpoch2DateTime2Epoch(Double value) { assertEquals(value, unixTimeStampOf(fromUnixTime(value))); - assertEquals(value, + assertEquals( + value, eval(unixTimeStampOf(fromUnixTime(DSL.literal(new ExprDoubleValue(value))))).doubleValue()); assertEquals(Math.round(value) + 0d, unixTimeStampOf(fromUnixTime(Math.round(value)))); - assertEquals(Math.round(value) + 0d, + assertEquals( + Math.round(value) + 0d, eval(unixTimeStampOf(fromUnixTime(DSL.literal(new ExprLongValue(Math.round(value)))))) .doubleValue()); } @@ -72,19 +72,20 @@ private static Stream getDateTimeSamples() { Arguments.of(LocalDateTime.of(1999, 12, 31, 23, 59, 59)), Arguments.of(LocalDateTime.of(2004, 2, 29, 7, 40)), Arguments.of(LocalDateTime.of(2100, 2, 28, 13, 14, 15)), - Arguments.of(LocalDateTime.of(2012, 2, 21, 0, 0, 17)) - ); + Arguments.of(LocalDateTime.of(2012, 2, 21, 0, 0, 17))); } /** * Test converting valid values DateTime -> EpochTime -> DateTime. + * * @param value a value */ @ParameterizedTest @MethodSource("getDateTimeSamples") public void convertDateTime2Epoch2DateTime(LocalDateTime value) { assertEquals(value, fromUnixTime(unixTimeStampOf(value))); - assertEquals(value, + assertEquals( + value, eval(fromUnixTime(unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value))))) .datetimeValue()); } diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/WeekdayTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/WeekdayTest.java index 4b97639996b..d2a63945034 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/WeekdayTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/WeekdayTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static org.junit.jupiter.api.Assertions.assertAll; @@ -28,109 +27,89 @@ import org.opensearch.sql.expression.FunctionExpression; import org.opensearch.sql.expression.LiteralExpression; - class WeekdayTest extends ExpressionTestBase { - private void weekdayQuery( - FunctionExpression dateExpression, - int dayOfWeek, - String testExpr) { + private void weekdayQuery(FunctionExpression dateExpression, int dayOfWeek, String testExpr) { assertAll( () -> assertEquals(INTEGER, dateExpression.type()), () -> assertEquals(integerValue(dayOfWeek), eval(dateExpression)), - () -> assertEquals(testExpr, dateExpression.toString()) - ); + () -> assertEquals(testExpr, dateExpression.toString())); } private static Stream getTestDataForWeekday() { return Stream.of( - Arguments.of( - DSL.literal(new ExprDateValue("2020-08-07")), - 4, - "weekday(DATE '2020-08-07')"), - Arguments.of( - DSL.literal(new ExprDateValue("2020-08-09")), - 6, - "weekday(DATE '2020-08-09')"), - Arguments.of( - DSL.literal("2020-08-09"), - 6, - "weekday(\"2020-08-09\")"), - Arguments.of( - DSL.literal("2020-08-09 01:02:03"), - 6, - "weekday(\"2020-08-09 01:02:03\")") - ); + Arguments.of(DSL.literal(new ExprDateValue("2020-08-07")), 4, "weekday(DATE '2020-08-07')"), + Arguments.of(DSL.literal(new ExprDateValue("2020-08-09")), 6, "weekday(DATE '2020-08-09')"), + Arguments.of(DSL.literal("2020-08-09"), 6, "weekday(\"2020-08-09\")"), + Arguments.of(DSL.literal("2020-08-09 01:02:03"), 6, "weekday(\"2020-08-09 01:02:03\")")); } @MethodSource("getTestDataForWeekday") @ParameterizedTest public void weekday(LiteralExpression arg, int expectedInt, String expectedString) { - FunctionExpression expression = DSL.weekday( - functionProperties, - arg); + FunctionExpression expression = DSL.weekday(functionProperties, arg); weekdayQuery(expression, expectedInt, expectedString); } @Test public void testWeekdayWithTimeType() { - FunctionExpression expression = DSL.weekday( - functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); + FunctionExpression expression = + DSL.weekday(functionProperties, DSL.literal(new ExprTimeValue("12:23:34"))); assertAll( () -> assertEquals(INTEGER, eval(expression).type()), - () -> assertEquals(( - LocalDate.now( - functionProperties.getQueryStartClock()).getDayOfWeek().getValue() - 1), - eval(expression).integerValue()), - () -> assertEquals("weekday(TIME '12:23:34')", expression.toString()) - ); + () -> + assertEquals( + (LocalDate.now(functionProperties.getQueryStartClock()).getDayOfWeek().getValue() + - 1), + eval(expression).integerValue()), + () -> assertEquals("weekday(TIME '12:23:34')", expression.toString())); } private void testInvalidWeekday(String date) { - FunctionExpression expression = DSL.weekday( - functionProperties, DSL.literal(new ExprDateValue(date))); + FunctionExpression expression = + DSL.weekday(functionProperties, DSL.literal(new ExprDateValue(date))); eval(expression); } @Test public void weekdayLeapYear() { assertAll( - //Feb. 29 of a leap year - () -> weekdayQuery(DSL.weekday( - functionProperties, - DSL.literal("2020-02-29")), 5, "weekday(\"2020-02-29\")"), - //day after Feb. 29 of a leap year - () -> weekdayQuery(DSL.weekday( - functionProperties, - DSL.literal("2020-03-01")), 6, "weekday(\"2020-03-01\")"), - //Feb. 28 of a non-leap year - () -> weekdayQuery(DSL.weekday( - functionProperties, - DSL.literal("2021-02-28")), 6, "weekday(\"2021-02-28\")"), - //Feb. 29 of a non-leap year - () -> assertThrows( - SemanticCheckException.class, () -> testInvalidWeekday("2021-02-29")) - ); + // Feb. 29 of a leap year + () -> + weekdayQuery( + DSL.weekday(functionProperties, DSL.literal("2020-02-29")), + 5, + "weekday(\"2020-02-29\")"), + // day after Feb. 29 of a leap year + () -> + weekdayQuery( + DSL.weekday(functionProperties, DSL.literal("2020-03-01")), + 6, + "weekday(\"2020-03-01\")"), + // Feb. 28 of a non-leap year + () -> + weekdayQuery( + DSL.weekday(functionProperties, DSL.literal("2021-02-28")), + 6, + "weekday(\"2021-02-28\")"), + // Feb. 29 of a non-leap year + () -> assertThrows(SemanticCheckException.class, () -> testInvalidWeekday("2021-02-29"))); } @Test public void weekdayInvalidArgument() { assertAll( - //40th day of the month - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidWeekday("2021-02-40")), - - //13th month of the year - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidWeekday("2021-13-29")), - - //incorrect format - () -> assertThrows(SemanticCheckException.class, - () -> testInvalidWeekday("asdfasdf")) - ); + // 40th day of the month + () -> assertThrows(SemanticCheckException.class, () -> testInvalidWeekday("2021-02-40")), + + // 13th month of the year + () -> assertThrows(SemanticCheckException.class, () -> testInvalidWeekday("2021-13-29")), + + // incorrect format + () -> assertThrows(SemanticCheckException.class, () -> testInvalidWeekday("asdfasdf"))); } private ExprValue eval(Expression expression) { diff --git a/core/src/test/java/org/opensearch/sql/expression/datetime/YearweekTest.java b/core/src/test/java/org/opensearch/sql/expression/datetime/YearweekTest.java index 7517c5e8bf1..4f7208d141f 100644 --- a/core/src/test/java/org/opensearch/sql/expression/datetime/YearweekTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/datetime/YearweekTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.datetime; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; @@ -32,20 +31,18 @@ class YearweekTest extends ExpressionTestBase { private void yearweekQuery(String date, int mode, int expectedResult) { - FunctionExpression expression = DSL - .yearweek( - functionProperties, - DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); + FunctionExpression expression = + DSL.yearweek(functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(mode)); assertAll( () -> assertEquals(INTEGER, expression.type()), - () -> assertEquals( - String.format("yearweek(DATE '%s', %d)", date, mode), expression.toString()), - () -> assertEquals(integerValue(expectedResult), eval(expression)) - ); + () -> + assertEquals( + String.format("yearweek(DATE '%s', %d)", date, mode), expression.toString()), + () -> assertEquals(integerValue(expectedResult), eval(expression))); } private static Stream getTestDataForYearweek() { - //Test the behavior of different modes passed into the 'yearweek' function + // Test the behavior of different modes passed into the 'yearweek' function return Stream.of( Arguments.of("2019-01-05", 0, 201852), Arguments.of("2019-01-05", 1, 201901), @@ -78,8 +75,7 @@ private static Stream getTestDataForYearweek() { Arguments.of("1999-01-01", 1, 199852), Arguments.of("1999-01-01", 4, 199852), Arguments.of("1999-01-01", 5, 199852), - Arguments.of("1999-01-01", 6, 199852) - ); + Arguments.of("1999-01-01", 6, 199852)); } @ParameterizedTest(name = "{0} | {1}") @@ -90,17 +86,13 @@ public void testYearweak(String date, int mode, int expected) { @Test public void testYearweekWithoutMode() { - LocalDate date = LocalDate.of(2019,1,05); + LocalDate date = LocalDate.of(2019, 1, 05); - FunctionExpression expression = DSL - .yearweek( - functionProperties, - DSL.literal(new ExprDateValue(date)), DSL.literal(0)); + FunctionExpression expression = + DSL.yearweek(functionProperties, DSL.literal(new ExprDateValue(date)), DSL.literal(0)); - FunctionExpression expressionWithoutMode = DSL - .yearweek( - functionProperties, - DSL.literal(new ExprDateValue(date))); + FunctionExpression expressionWithoutMode = + DSL.yearweek(functionProperties, DSL.literal(new ExprDateValue(date))); assertEquals(eval(expression), eval(expressionWithoutMode)); } @@ -111,58 +103,53 @@ public void testYearweekWithTimeType() { int year = LocalDate.now(functionProperties.getQueryStartClock()).getYear(); int expected = Integer.parseInt(String.format("%d%02d", year, week)); - FunctionExpression expression = DSL - .yearweek( - functionProperties, - DSL.literal(new ExprTimeValue("10:11:12")), DSL.literal(0)); + FunctionExpression expression = + DSL.yearweek( + functionProperties, DSL.literal(new ExprTimeValue("10:11:12")), DSL.literal(0)); - FunctionExpression expressionWithoutMode = DSL - .yearweek( - functionProperties, - DSL.literal(new ExprTimeValue("10:11:12"))); + FunctionExpression expressionWithoutMode = + DSL.yearweek(functionProperties, DSL.literal(new ExprTimeValue("10:11:12"))); assertAll( () -> assertEquals(expected, eval(expression).integerValue()), - () -> assertEquals(expected, eval(expressionWithoutMode).integerValue()) - ); + () -> assertEquals(expected, eval(expressionWithoutMode).integerValue())); } @Test public void testInvalidYearWeek() { assertAll( - //test invalid month - () -> assertThrows( - SemanticCheckException.class, - () -> yearweekQuery("2019-13-05 01:02:03", 0, 0)), - //test invalid day - () -> assertThrows( - SemanticCheckException.class, - () -> yearweekQuery("2019-01-50 01:02:03", 0, 0)), - //test invalid leap year - () -> assertThrows( - SemanticCheckException.class, - () -> yearweekQuery("2019-02-29 01:02:03", 0, 0)) - ); + // test invalid month + () -> + assertThrows( + SemanticCheckException.class, () -> yearweekQuery("2019-13-05 01:02:03", 0, 0)), + // test invalid day + () -> + assertThrows( + SemanticCheckException.class, () -> yearweekQuery("2019-01-50 01:02:03", 0, 0)), + // test invalid leap year + () -> + assertThrows( + SemanticCheckException.class, () -> yearweekQuery("2019-02-29 01:02:03", 0, 0))); } @Test public void yearweekModeInUnsupportedFormat() { - FunctionExpression expression1 = DSL - .yearweek( + FunctionExpression expression1 = + DSL.yearweek( functionProperties, - DSL.literal(new ExprDatetimeValue("2019-01-05 10:11:12")), DSL.literal(8)); + DSL.literal(new ExprDatetimeValue("2019-01-05 10:11:12")), + DSL.literal(8)); SemanticCheckException exception = assertThrows(SemanticCheckException.class, () -> eval(expression1)); - assertEquals("mode:8 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:8 is invalid, please use mode value between 0-7", exception.getMessage()); - FunctionExpression expression2 = DSL - .yearweek( + FunctionExpression expression2 = + DSL.yearweek( functionProperties, - DSL.literal(new ExprDatetimeValue("2019-01-05 10:11:12")), DSL.literal(-1)); + DSL.literal(new ExprDatetimeValue("2019-01-05 10:11:12")), + DSL.literal(-1)); exception = assertThrows(SemanticCheckException.class, () -> eval(expression2)); - assertEquals("mode:-1 is invalid, please use mode value between 0-7", - exception.getMessage()); + assertEquals("mode:-1 is invalid, please use mode value between 0-7", exception.getMessage()); } private ExprValue eval(Expression expression) { diff --git a/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionNameTest.java b/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionNameTest.java index 075d8095447..774c471b7d7 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionNameTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionNameTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -20,8 +19,7 @@ class BuiltinFunctionNameTest { private static Stream ofArguments() { Stream.Builder builder = Stream.builder(); - return Arrays.asList(BuiltinFunctionName.values()) - .stream() + return Arrays.asList(BuiltinFunctionName.values()).stream() .map(functionName -> Arguments.of(functionName.getName().getFunctionName(), functionName)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionRepositoryTest.java b/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionRepositoryTest.java index 98a4c32e2e5..3ee12f59d49 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionRepositoryTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/BuiltinFunctionRepositoryTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -47,20 +46,13 @@ @ExtendWith(MockitoExtension.class) class BuiltinFunctionRepositoryTest { - @Mock - private DefaultFunctionResolver mockfunctionResolver; - @Mock - private Map mockMap; - @Mock - FunctionProperties functionProperties; - @Mock - private FunctionName mockFunctionName; - @Mock - private FunctionBuilder functionExpressionBuilder; - @Mock - private FunctionSignature functionSignature; - @Mock - private Expression mockExpression; + @Mock private DefaultFunctionResolver mockfunctionResolver; + @Mock private Map mockMap; + @Mock FunctionProperties functionProperties; + @Mock private FunctionName mockFunctionName; + @Mock private FunctionBuilder functionExpressionBuilder; + @Mock private FunctionSignature functionSignature; + @Mock private Expression mockExpression; private BuiltinFunctionRepository repo; @@ -83,16 +75,15 @@ void compile() { when(mockExpression.type()).thenReturn(UNDEFINED); when(functionSignature.getParamTypeList()).thenReturn(Arrays.asList(UNDEFINED)); when(mockfunctionResolver.getFunctionName()).thenReturn(mockFunctionName); - when(mockfunctionResolver.resolve(any())).thenReturn( - Pair.of(functionSignature, functionExpressionBuilder)); + when(mockfunctionResolver.resolve(any())) + .thenReturn(Pair.of(functionSignature, functionExpressionBuilder)); when(mockMap.containsKey(mockFunctionName)).thenReturn(true); when(mockMap.get(mockFunctionName)).thenReturn(mockfunctionResolver); BuiltinFunctionRepository repo = new BuiltinFunctionRepository(mockMap); repo.register(mockfunctionResolver); repo.compile(functionProperties, mockFunctionName, Arrays.asList(mockExpression)); - verify(functionExpressionBuilder, times(1)) - .apply(eq(functionProperties), any()); + verify(functionExpressionBuilder, times(1)).apply(eq(functionProperties), any()); } @Test @@ -101,8 +92,8 @@ void compile_datasource_defined_function() { when(mockExpression.type()).thenReturn(UNDEFINED); when(functionSignature.getParamTypeList()).thenReturn(Arrays.asList(UNDEFINED)); when(dataSourceFunctionResolver.getFunctionName()).thenReturn(mockFunctionName); - when(dataSourceFunctionResolver.resolve(any())).thenReturn( - Pair.of(functionSignature, functionExpressionBuilder)); + when(dataSourceFunctionResolver.resolve(any())) + .thenReturn(Pair.of(functionSignature, functionExpressionBuilder)); BuiltinFunctionRepository repo = new BuiltinFunctionRepository(Map.of()); repo.compile( @@ -110,8 +101,7 @@ void compile_datasource_defined_function() { Collections.singletonList(dataSourceFunctionResolver), mockFunctionName, Arrays.asList(mockExpression)); - verify(functionExpressionBuilder, times(1)) - .apply(eq(functionProperties), any()); + verify(functionExpressionBuilder, times(1)).apply(eq(functionProperties), any()); } @Test @@ -119,22 +109,23 @@ void compile_datasource_defined_function() { void resolve() { when(functionSignature.getFunctionName()).thenReturn(mockFunctionName); when(mockfunctionResolver.getFunctionName()).thenReturn(mockFunctionName); - when(mockfunctionResolver.resolve(functionSignature)).thenReturn( - Pair.of(functionSignature, functionExpressionBuilder)); + when(mockfunctionResolver.resolve(functionSignature)) + .thenReturn(Pair.of(functionSignature, functionExpressionBuilder)); when(mockMap.containsKey(mockFunctionName)).thenReturn(true); when(mockMap.get(mockFunctionName)).thenReturn(mockfunctionResolver); BuiltinFunctionRepository repo = new BuiltinFunctionRepository(mockMap); repo.register(mockfunctionResolver); - assertEquals(functionExpressionBuilder, repo.resolve( - Collections.emptyList(), functionSignature)); + assertEquals( + functionExpressionBuilder, repo.resolve(Collections.emptyList(), functionSignature)); } @Test void resolve_should_not_cast_arguments_in_cast_function() { when(mockExpression.toString()).thenReturn("string"); FunctionImplementation function = - repo.resolve(Collections.emptyList(), + repo.resolve( + Collections.emptyList(), registerFunctionResolver(CAST_TO_BOOLEAN.getName(), DATETIME, BOOLEAN)) .apply(functionProperties, ImmutableList.of(mockExpression)); assertEquals("cast_to_boolean(string)", function.toString()); @@ -145,8 +136,8 @@ void resolve_should_not_cast_arguments_if_same_type() { when(mockFunctionName.getFunctionName()).thenReturn("mock"); when(mockExpression.toString()).thenReturn("string"); FunctionImplementation function = - repo.resolve(Collections.emptyList(), - registerFunctionResolver(mockFunctionName, STRING, STRING)) + repo.resolve( + Collections.emptyList(), registerFunctionResolver(mockFunctionName, STRING, STRING)) .apply(functionProperties, ImmutableList.of(mockExpression)); assertEquals("mock(string)", function.toString()); } @@ -156,8 +147,8 @@ void resolve_should_not_cast_arguments_if_both_numbers() { when(mockFunctionName.getFunctionName()).thenReturn("mock"); when(mockExpression.toString()).thenReturn("byte"); FunctionImplementation function = - repo.resolve(Collections.emptyList(), - registerFunctionResolver(mockFunctionName, BYTE, INTEGER)) + repo.resolve( + Collections.emptyList(), registerFunctionResolver(mockFunctionName, BYTE, INTEGER)) .apply(functionProperties, ImmutableList.of(mockExpression)); assertEquals("mock(byte)", function.toString()); } @@ -168,8 +159,7 @@ void resolve_should_cast_arguments() { when(mockExpression.toString()).thenReturn("string"); when(mockExpression.type()).thenReturn(STRING); - FunctionSignature signature = - registerFunctionResolver(mockFunctionName, STRING, BOOLEAN); + FunctionSignature signature = registerFunctionResolver(mockFunctionName, STRING, BOOLEAN); registerFunctionResolver(CAST_TO_BOOLEAN.getName(), STRING, STRING); FunctionImplementation function = @@ -181,10 +171,13 @@ void resolve_should_cast_arguments() { @Test void resolve_should_throw_exception_for_unsupported_conversion() { ExpressionEvaluationException error = - assertThrows(ExpressionEvaluationException.class, () -> - repo.resolve(Collections.emptyList(), - registerFunctionResolver(mockFunctionName, BYTE, STRUCT)) - .apply(functionProperties, ImmutableList.of(mockExpression))); + assertThrows( + ExpressionEvaluationException.class, + () -> + repo.resolve( + Collections.emptyList(), + registerFunctionResolver(mockFunctionName, BYTE, STRUCT)) + .apply(functionProperties, ImmutableList.of(mockExpression))); assertEquals(error.getMessage(), "Type conversion to type STRUCT is not supported"); } @@ -195,33 +188,37 @@ void resolve_unregistered() { BuiltinFunctionRepository repo = new BuiltinFunctionRepository(mockMap); repo.register(mockfunctionResolver); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> repo.resolve(Collections.emptyList(), - new FunctionSignature(FunctionName.of("unknown"), List.of()))); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, + () -> + repo.resolve( + Collections.emptyList(), + new FunctionSignature(FunctionName.of("unknown"), List.of()))); assertEquals("unsupported function name: unknown", exception.getMessage()); } - private FunctionSignature registerFunctionResolver(FunctionName funcName, - ExprType sourceType, - ExprType targetType) { - FunctionSignature unresolvedSignature = new FunctionSignature( - funcName, ImmutableList.of(sourceType)); - FunctionSignature resolvedSignature = new FunctionSignature( - funcName, ImmutableList.of(targetType)); + private FunctionSignature registerFunctionResolver( + FunctionName funcName, ExprType sourceType, ExprType targetType) { + FunctionSignature unresolvedSignature = + new FunctionSignature(funcName, ImmutableList.of(sourceType)); + FunctionSignature resolvedSignature = + new FunctionSignature(funcName, ImmutableList.of(targetType)); DefaultFunctionResolver funcResolver = mock(DefaultFunctionResolver.class); FunctionBuilder funcBuilder = mock(FunctionBuilder.class); when(mockMap.containsKey(eq(funcName))).thenReturn(true); when(mockMap.get(eq(funcName))).thenReturn(funcResolver); - when(funcResolver.resolve(eq(unresolvedSignature))).thenReturn( - Pair.of(resolvedSignature, funcBuilder)); + when(funcResolver.resolve(eq(unresolvedSignature))) + .thenReturn(Pair.of(resolvedSignature, funcBuilder)); repo.register(funcResolver); // Relax unnecessary stubbing check because error case test doesn't call this - lenient().doAnswer(invocation -> - new FakeFunctionExpression(funcName, invocation.getArgument(1)) - ).when(funcBuilder).apply(eq(functionProperties), any()); + lenient() + .doAnswer(invocation -> new FakeFunctionExpression(funcName, invocation.getArgument(1))) + .when(funcBuilder) + .apply(eq(functionProperties), any()); return unresolvedSignature; } @@ -244,8 +241,9 @@ public ExprType type() { @Override public String toString() { return getFunctionName().getFunctionName() - + "(" + StringUtils.join(getArguments(), ", ") + ")"; + + "(" + + StringUtils.join(getArguments(), ", ") + + ")"; } } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/DefaultFunctionResolverTest.java b/core/src/test/java/org/opensearch/sql/expression/function/DefaultFunctionResolverTest.java index 202c1bd0aa9..ad9e8a66614 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/DefaultFunctionResolverTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/DefaultFunctionResolverTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -27,32 +26,24 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @ExtendWith(MockitoExtension.class) class DefaultFunctionResolverTest { - @Mock - private FunctionSignature exactlyMatchFS; - @Mock - private FunctionSignature bestMatchFS; - @Mock - private FunctionSignature leastMatchFS; - @Mock - private FunctionSignature notMatchFS; - @Mock - private FunctionSignature functionSignature; - @Mock - private FunctionBuilder exactlyMatchBuilder; - @Mock - private FunctionBuilder bestMatchBuilder; - @Mock - private FunctionBuilder leastMatchBuilder; - @Mock - private FunctionBuilder notMatchBuilder; + @Mock private FunctionSignature exactlyMatchFS; + @Mock private FunctionSignature bestMatchFS; + @Mock private FunctionSignature leastMatchFS; + @Mock private FunctionSignature notMatchFS; + @Mock private FunctionSignature functionSignature; + @Mock private FunctionBuilder exactlyMatchBuilder; + @Mock private FunctionBuilder bestMatchBuilder; + @Mock private FunctionBuilder leastMatchBuilder; + @Mock private FunctionBuilder notMatchBuilder; private FunctionName functionName = FunctionName.of("add"); @Test void resolve_function_signature_exactly_match() { when(functionSignature.match(exactlyMatchFS)).thenReturn(WideningTypeRule.TYPE_EQUAL); - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(exactlyMatchFS, exactlyMatchBuilder)); + DefaultFunctionResolver resolver = + new DefaultFunctionResolver( + functionName, ImmutableMap.of(exactlyMatchFS, exactlyMatchBuilder)); assertEquals(exactlyMatchBuilder, resolver.resolve(functionSignature).getValue()); } @@ -61,8 +52,10 @@ void resolve_function_signature_exactly_match() { void resolve_function_signature_best_match() { when(functionSignature.match(bestMatchFS)).thenReturn(1); when(functionSignature.match(leastMatchFS)).thenReturn(2); - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(bestMatchFS, bestMatchBuilder, leastMatchFS, leastMatchBuilder)); + DefaultFunctionResolver resolver = + new DefaultFunctionResolver( + functionName, + ImmutableMap.of(bestMatchFS, bestMatchBuilder, leastMatchFS, leastMatchBuilder)); assertEquals(bestMatchBuilder, resolver.resolve(functionSignature).getValue()); } @@ -72,12 +65,14 @@ void resolve_function_not_match() { when(functionSignature.match(notMatchFS)).thenReturn(WideningTypeRule.IMPOSSIBLE_WIDENING); when(notMatchFS.formatTypes()).thenReturn("[INTEGER,INTEGER]"); when(functionSignature.formatTypes()).thenReturn("[BOOLEAN,BOOLEAN]"); - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(notMatchFS, notMatchBuilder)); - - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> resolver.resolve(functionSignature)); - assertEquals("add function expected {[INTEGER,INTEGER]}, but get [BOOLEAN,BOOLEAN]", + DefaultFunctionResolver resolver = + new DefaultFunctionResolver(functionName, ImmutableMap.of(notMatchFS, notMatchBuilder)); + + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, () -> resolver.resolve(functionSignature)); + assertEquals( + "add function expected {[INTEGER,INTEGER]}, but get [BOOLEAN,BOOLEAN]", exception.getMessage()); } @@ -88,8 +83,8 @@ void resolve_varargs_function_signature_match() { when(functionSignature.getParamTypeList()).thenReturn(ImmutableList.of(STRING)); when(bestMatchFS.getParamTypeList()).thenReturn(ImmutableList.of(ARRAY)); - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(bestMatchFS, bestMatchBuilder)); + DefaultFunctionResolver resolver = + new DefaultFunctionResolver(functionName, ImmutableMap.of(bestMatchFS, bestMatchBuilder)); assertEquals(bestMatchBuilder, resolver.resolve(functionSignature).getValue()); } @@ -102,13 +97,13 @@ void resolve_varargs_no_args_function_signature_not_match() { // Concat function with no arguments when(functionSignature.getParamTypeList()).thenReturn(Collections.emptyList()); - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(bestMatchFS, bestMatchBuilder)); + DefaultFunctionResolver resolver = + new DefaultFunctionResolver(functionName, ImmutableMap.of(bestMatchFS, bestMatchBuilder)); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> resolver.resolve(functionSignature)); - assertEquals("concat function expected 1-9 arguments, but got 0", - exception.getMessage()); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, () -> resolver.resolve(functionSignature)); + assertEquals("concat function expected 1-9 arguments, but got 0", exception.getMessage()); } @Test @@ -117,16 +112,17 @@ void resolve_varargs_too_many_args_function_signature_not_match() { when(functionSignature.match(bestMatchFS)).thenReturn(WideningTypeRule.TYPE_EQUAL); when(bestMatchFS.getParamTypeList()).thenReturn(ImmutableList.of(ARRAY)); // Concat function with more than 9 arguments - when(functionSignature.getParamTypeList()).thenReturn(ImmutableList - .of(STRING, STRING, STRING, STRING, STRING, - STRING, STRING, STRING, STRING, STRING)); - - DefaultFunctionResolver resolver = new DefaultFunctionResolver(functionName, - ImmutableMap.of(bestMatchFS, bestMatchBuilder)); - - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> resolver.resolve(functionSignature)); - assertEquals("concat function expected 1-9 arguments, but got 10", - exception.getMessage()); + when(functionSignature.getParamTypeList()) + .thenReturn( + ImmutableList.of( + STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING, STRING)); + + DefaultFunctionResolver resolver = + new DefaultFunctionResolver(functionName, ImmutableMap.of(bestMatchFS, bestMatchBuilder)); + + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, () -> resolver.resolve(functionSignature)); + assertEquals("concat function expected 1-9 arguments, but got 10", exception.getMessage()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLDefineTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLDefineTest.java index 8bf4d7ba244..670605c5a0e 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLDefineTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLDefineTest.java @@ -52,16 +52,15 @@ void define_name_test() { static class SampleFunctionBuilder implements FunctionBuilder { @Override - public FunctionImplementation apply(FunctionProperties functionProperties, - List arguments) { + public FunctionImplementation apply( + FunctionProperties functionProperties, List arguments) { return new SampleFunctionImplementation(arguments); } } @RequiredArgsConstructor static class SampleFunctionImplementation implements FunctionImplementation { - @Getter - private final List arguments; + @Getter private final List arguments; @Override public FunctionName getFunctionName() { diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLTestBase.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLTestBase.java index f5f41284515..a8b9e8da96b 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLTestBase.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLTestBase.java @@ -11,40 +11,39 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.sql.data.model.ExprMissingValue; import org.opensearch.sql.data.model.ExprNullValue; -import org.opensearch.sql.data.model.ExprTupleValue; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; @ExtendWith(MockitoExtension.class) public class FunctionDSLTestBase { - @Mock - FunctionProperties functionProperties; + @Mock FunctionProperties functionProperties; public static final ExprNullValue NULL = ExprNullValue.of(); public static final ExprMissingValue MISSING = ExprMissingValue.of(); protected static final ExprType ANY_TYPE = () -> "ANY"; - protected static final ExprValue ANY = new ExprValue() { - @Override - public Object value() { - throw new RuntimeException(); - } + protected static final ExprValue ANY = + new ExprValue() { + @Override + public Object value() { + throw new RuntimeException(); + } - @Override - public ExprType type() { - return ANY_TYPE; - } + @Override + public ExprType type() { + return ANY_TYPE; + } - @Override - public String toString() { - return "ANY"; - } + @Override + public String toString() { + return "ANY"; + } - @Override - public int compareTo(ExprValue o) { - throw new RuntimeException(); - } - }; + @Override + public int compareTo(ExprValue o) { + throw new RuntimeException(); + } + }; static final FunctionName SAMPLE_NAME = FunctionName.of("sample"); static final FunctionSignature SAMPLE_SIGNATURE_A = new FunctionSignature(SAMPLE_NAME, List.of(ExprCoreType.UNDEFINED)); @@ -55,18 +54,16 @@ public int compareTo(ExprValue o) { static final SerializableTriFunction twoArgWithProperties = (functionProperties, v1, v2) -> ANY; - static final SerializableQuadFunction - + static final SerializableQuadFunction< + FunctionProperties, ExprValue, ExprValue, ExprValue, ExprValue> threeArgsWithProperties = (functionProperties, v1, v2, v3) -> ANY; - static final SerializableBiFunction - twoArgs = (v1, v2) -> ANY; - static final SerializableTriFunction - threeArgs = (v1, v2, v3) -> ANY; + static final SerializableBiFunction twoArgs = (v1, v2) -> ANY; + static final SerializableTriFunction threeArgs = + (v1, v2, v3) -> ANY; static final SerializableQuadFunction fourArgs = (v1, v2, v3, v4) -> ANY; - @Mock - FunctionProperties mockProperties; + @Mock FunctionProperties mockProperties; } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplNoArgTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplNoArgTest.java index 5d970803edd..907cd49f26e 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplNoArgTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplNoArgTest.java @@ -9,7 +9,6 @@ import java.util.List; import org.apache.commons.lang3.tuple.Pair; -import org.junit.jupiter.api.BeforeEach; import org.opensearch.sql.expression.Expression; class FunctionDSLimplNoArgTest extends FunctionDSLimplTestBase { diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTestBase.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTestBase.java index 8f494c01c33..a76e738e2dd 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTestBase.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTestBase.java @@ -42,16 +42,16 @@ void implementation_valid_functionBuilder() { @Test void implementation_functionBuilder_return_functionExpression() { - FunctionImplementation executable = getImplementation().getValue() - .apply(functionProperties, getSampleArguments()); + FunctionImplementation executable = + getImplementation().getValue().apply(functionProperties, getSampleArguments()); assertTrue(executable instanceof FunctionExpression); } @Test void implementation_functionExpression_valueOf() { FunctionExpression executable = - (FunctionExpression) getImplementation().getValue() - .apply(functionProperties, getSampleArguments()); + (FunctionExpression) + getImplementation().getValue().apply(functionProperties, getSampleArguments()); assertEquals(ANY, executable.valueOf(null)); } @@ -59,23 +59,20 @@ void implementation_functionExpression_valueOf() { @Test void implementation_functionExpression_type() { FunctionExpression executable = - (FunctionExpression) getImplementation().getValue() - .apply(functionProperties, getSampleArguments()); + (FunctionExpression) + getImplementation().getValue().apply(functionProperties, getSampleArguments()); assertEquals(ANY_TYPE, executable.type()); } @Test void implementation_functionExpression_toString() { FunctionExpression executable = - (FunctionExpression) getImplementation().getValue() - .apply(functionProperties, getSampleArguments()); + (FunctionExpression) + getImplementation().getValue().apply(functionProperties, getSampleArguments()); assertEquals(getExpected_toString(), executable.toString()); } - /** - * A lambda that takes a function name and returns an implementation - * of the function. - */ + /** A lambda that takes a function name and returns an implementation of the function. */ abstract SerializableFunction> getImplementationGenerator(); diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTwoArgTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTwoArgTest.java index 87d097c9eb3..4e2626b9c37 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTwoArgTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplTwoArgTest.java @@ -19,7 +19,6 @@ class FunctionDSLimplTwoArgTest extends FunctionDSLimplTestBase { return impl(twoArgs, ANY_TYPE, ANY_TYPE, ANY_TYPE); } - @Override List getSampleArguments() { return List.of(DSL.literal(ANY), DSL.literal(ANY)); diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesNoArgsTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesNoArgsTest.java index c3c41b6c0c4..f4b8d0090eb 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesNoArgsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesNoArgsTest.java @@ -9,7 +9,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.opensearch.sql.expression.Expression; -class FunctionDSLimplWithPropertiesNoArgsTest extends FunctionDSLimplTestBase { +class FunctionDSLimplWithPropertiesNoArgsTest extends FunctionDSLimplTestBase { @Override SerializableFunction> diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesOneArgTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesOneArgTest.java index 4a05326c0a8..57960b5c1cd 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesOneArgTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesOneArgTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.expression.function; - import java.util.List; import org.apache.commons.lang3.tuple.Pair; import org.opensearch.sql.data.model.ExprValue; @@ -17,8 +16,8 @@ class FunctionDSLimplWithPropertiesOneArgTest extends FunctionDSLimplTestBase { @Override SerializableFunction> getImplementationGenerator() { - SerializableBiFunction functionBody - = (fp, arg) -> ANY; + SerializableBiFunction functionBody = + (fp, arg) -> ANY; return FunctionDSL.implWithProperties(functionBody, ANY_TYPE, ANY_TYPE); } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgTest.java index 18444a476ec..3c42b412c8e 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgTest.java @@ -16,8 +16,8 @@ class FunctionDSLimplWithPropertiesTwoArgTest extends FunctionDSLimplTestBase { @Override SerializableFunction> getImplementationGenerator() { - SerializableTriFunction functionBody - = (fp, arg1, arg2) -> ANY; + SerializableTriFunction functionBody = + (fp, arg1, arg2) -> ANY; return FunctionDSL.implWithProperties(functionBody, ANY_TYPE, ANY_TYPE, ANY_TYPE); } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgsTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgsTest.java index f6904858012..748d7343730 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLimplWithPropertiesTwoArgsTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.expression.function; - import java.util.List; import org.apache.commons.lang3.tuple.Pair; import org.opensearch.sql.data.model.ExprValue; @@ -17,8 +16,8 @@ class FunctionDSLimplWithPropertiesTwoArgsTest extends FunctionDSLimplTestBase { @Override SerializableFunction> getImplementationGenerator() { - SerializableTriFunction functionBody - = (fp, arg1, arg2) -> ANY; + SerializableTriFunction functionBody = + (fp, arg1, arg2) -> ANY; return FunctionDSL.implWithProperties(functionBody, ANY_TYPE, ANY_TYPE, ANY_TYPE); } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLnullMissingHandlingTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLnullMissingHandlingTest.java index 0cea2228435..a1cc3050035 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLnullMissingHandlingTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionDSLnullMissingHandlingTest.java @@ -28,106 +28,119 @@ void nullMissingHandling_oneArg_apply() { assertEquals(ANY, nullMissingHandling(oneArg).apply(ANY)); } - @Test void nullMissingHandling_oneArg_FunctionProperties_nullValue() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(oneArgWithProperties).apply(functionProperties, NULL)); } @Test void nullMissingHandling_oneArg_FunctionProperties_missingValue() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(oneArgWithProperties).apply(functionProperties, MISSING)); } @Test void nullMissingHandling_oneArg_FunctionProperties_apply() { - assertEquals(ANY, + assertEquals( + ANY, nullMissingHandlingWithProperties(oneArgWithProperties).apply(functionProperties, ANY)); } @Test void nullMissingHandling_twoArgs_FunctionProperties_nullValue_firstArg() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(twoArgWithProperties) .apply(functionProperties, NULL, ANY)); } @Test void nullMissingHandling_twoArgs_FunctionProperties_nullValue_secondArg() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(twoArgWithProperties) .apply(functionProperties, ANY, NULL)); } @Test void nullMissingHandling_twoArgs_FunctionProperties_missingValue_firstArg() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(twoArgWithProperties) .apply(functionProperties, MISSING, ANY)); } @Test void nullMissingHandling_twoArgs_FunctionProperties_missingValue_secondArg() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(twoArgWithProperties) .apply(functionProperties, ANY, MISSING)); } @Test void nullMissingHandling_twoArgs_FunctionProperties_apply() { - assertEquals(ANY, + assertEquals( + ANY, nullMissingHandlingWithProperties(twoArgWithProperties) .apply(functionProperties, ANY, ANY)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_nullValue_firstArg() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, NULL, ANY, ANY)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_nullValue_secondArg() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, ANY, NULL, ANY)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_nullValue_thirdArg() { - assertEquals(NULL, + assertEquals( + NULL, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, ANY, ANY, NULL)); } - @Test void nullMissingHandling_threeArgs_FunctionProperties_missingValue_firstArg() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, MISSING, ANY, ANY)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_missingValue_secondArg() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, ANY, MISSING, ANY)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_missingValue_thirdArg() { - assertEquals(MISSING, + assertEquals( + MISSING, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, ANY, ANY, MISSING)); } @Test void nullMissingHandling_threeArgs_FunctionProperties_apply() { - assertEquals(ANY, + assertEquals( + ANY, nullMissingHandlingWithProperties(threeArgsWithProperties) .apply(functionProperties, ANY, ANY, ANY)); } @@ -142,7 +155,6 @@ void nullMissingHandling_twoArgs_secondArg_nullValue() { assertEquals(NULL, nullMissingHandling(twoArgs).apply(ANY, NULL)); } - @Test void nullMissingHandling_twoArgs_firstArg_missingValue() { assertEquals(MISSING, nullMissingHandling(twoArgs).apply(MISSING, ANY)); @@ -158,7 +170,6 @@ void nullMissingHandling_twoArgs_apply() { assertEquals(ANY, nullMissingHandling(twoArgs).apply(ANY, ANY)); } - @Test void nullMissingHandling_threeArgs_firstArg_nullValue() { assertEquals(NULL, nullMissingHandling(threeArgs).apply(NULL, ANY, ANY)); @@ -174,7 +185,6 @@ void nullMissingHandling_threeArgs_thirdArg_nullValue() { assertEquals(NULL, nullMissingHandling(threeArgs).apply(ANY, ANY, NULL)); } - @Test void nullMissingHandling_threeArgs_firstArg_missingValue() { assertEquals(MISSING, nullMissingHandling(threeArgs).apply(MISSING, ANY, ANY)); diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTest.java index 64ec21e7e17..b531d049fd6 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTest.java @@ -17,9 +17,7 @@ import java.time.Clock; import java.time.Instant; import java.time.ZoneId; -import java.util.concurrent.Callable; import java.util.function.Consumer; -import java.util.function.Function; import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DynamicTest; @@ -77,16 +75,17 @@ void functionProperties_can_be_deserialized() throws IOException, ClassNotFoundE @TestFactory Stream functionProperties_none_throws_on_access() { - Consumer tb = tc -> { - RuntimeException e = assertThrows(FunctionProperties.UnexpectedCallException.class, tc); - assertEquals("FunctionProperties.None is a null object and not meant to be accessed.", - e.getMessage()); - }; + Consumer tb = + tc -> { + RuntimeException e = assertThrows(FunctionProperties.UnexpectedCallException.class, tc); + assertEquals( + "FunctionProperties.None is a null object and not meant to be accessed.", + e.getMessage()); + }; return Stream.of( - DynamicTest.dynamicTest("getQueryStartClock", - () -> tb.accept(FunctionProperties.None::getQueryStartClock)), - DynamicTest.dynamicTest("getSystemClock", - () -> tb.accept(FunctionProperties.None::getSystemClock)) - ); + DynamicTest.dynamicTest( + "getQueryStartClock", () -> tb.accept(FunctionProperties.None::getQueryStartClock)), + DynamicTest.dynamicTest( + "getSystemClock", () -> tb.accept(FunctionProperties.None::getSystemClock))); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionSignatureTest.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionSignatureTest.java index cc658bff984..2fb5dc468ed 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionSignatureTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/FunctionSignatureTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -25,10 +24,8 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @ExtendWith(MockitoExtension.class) class FunctionSignatureTest { - @Mock - private FunctionSignature funcSignature; - @Mock - private List funcParamTypeList; + @Mock private FunctionSignature funcSignature; + @Mock private List funcParamTypeList; private FunctionName unresolvedFuncName = FunctionName.of("add"); private List unresolvedParamTypeList = diff --git a/core/src/test/java/org/opensearch/sql/expression/function/OpenSearchFunctionsTest.java b/core/src/test/java/org/opensearch/sql/expression/function/OpenSearchFunctionsTest.java index d90d8295c43..168b73acc43 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/OpenSearchFunctionsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/OpenSearchFunctionsTest.java @@ -24,44 +24,48 @@ import org.opensearch.sql.expression.NamedArgumentExpression; import org.opensearch.sql.expression.env.Environment; - public class OpenSearchFunctionsTest extends ExpressionTestBase { - private final NamedArgumentExpression field = new NamedArgumentExpression( - "field", DSL.literal("message")); - private final NamedArgumentExpression fields = new NamedArgumentExpression( - "fields", DSL.literal(new ExprTupleValue(new LinkedHashMap<>(Map.of( - "title", ExprValueUtils.floatValue(1.F), - "body", ExprValueUtils.floatValue(.3F)))))); - private final NamedArgumentExpression query = new NamedArgumentExpression( - "query", DSL.literal("search query")); - private final NamedArgumentExpression analyzer = new NamedArgumentExpression( - "analyzer", DSL.literal("keyword")); - private final NamedArgumentExpression autoGenerateSynonymsPhrase = new NamedArgumentExpression( - "auto_generate_synonyms_phrase", DSL.literal("true")); - private final NamedArgumentExpression fuzziness = new NamedArgumentExpression( - "fuzziness", DSL.literal("AUTO")); - private final NamedArgumentExpression maxExpansions = new NamedArgumentExpression( - "max_expansions", DSL.literal("10")); - private final NamedArgumentExpression prefixLength = new NamedArgumentExpression( - "prefix_length", DSL.literal("1")); - private final NamedArgumentExpression fuzzyTranspositions = new NamedArgumentExpression( - "fuzzy_transpositions", DSL.literal("false")); - private final NamedArgumentExpression fuzzyRewrite = new NamedArgumentExpression( - "fuzzy_rewrite", DSL.literal("rewrite method")); - private final NamedArgumentExpression lenient = new NamedArgumentExpression( - "lenient", DSL.literal("true")); - private final NamedArgumentExpression operator = new NamedArgumentExpression( - "operator", DSL.literal("OR")); - private final NamedArgumentExpression minimumShouldMatch = new NamedArgumentExpression( - "minimum_should_match", DSL.literal("1")); - private final NamedArgumentExpression zeroTermsQueryAll = new NamedArgumentExpression( - "zero_terms_query", DSL.literal("ALL")); - private final NamedArgumentExpression zeroTermsQueryNone = new NamedArgumentExpression( - "zero_terms_query", DSL.literal("None")); - private final NamedArgumentExpression boost = new NamedArgumentExpression( - "boost", DSL.literal("2.0")); - private final NamedArgumentExpression slop = new NamedArgumentExpression( - "slop", DSL.literal("3")); + private final NamedArgumentExpression field = + new NamedArgumentExpression("field", DSL.literal("message")); + private final NamedArgumentExpression fields = + new NamedArgumentExpression( + "fields", + DSL.literal( + new ExprTupleValue( + new LinkedHashMap<>( + Map.of( + "title", ExprValueUtils.floatValue(1.F), + "body", ExprValueUtils.floatValue(.3F)))))); + private final NamedArgumentExpression query = + new NamedArgumentExpression("query", DSL.literal("search query")); + private final NamedArgumentExpression analyzer = + new NamedArgumentExpression("analyzer", DSL.literal("keyword")); + private final NamedArgumentExpression autoGenerateSynonymsPhrase = + new NamedArgumentExpression("auto_generate_synonyms_phrase", DSL.literal("true")); + private final NamedArgumentExpression fuzziness = + new NamedArgumentExpression("fuzziness", DSL.literal("AUTO")); + private final NamedArgumentExpression maxExpansions = + new NamedArgumentExpression("max_expansions", DSL.literal("10")); + private final NamedArgumentExpression prefixLength = + new NamedArgumentExpression("prefix_length", DSL.literal("1")); + private final NamedArgumentExpression fuzzyTranspositions = + new NamedArgumentExpression("fuzzy_transpositions", DSL.literal("false")); + private final NamedArgumentExpression fuzzyRewrite = + new NamedArgumentExpression("fuzzy_rewrite", DSL.literal("rewrite method")); + private final NamedArgumentExpression lenient = + new NamedArgumentExpression("lenient", DSL.literal("true")); + private final NamedArgumentExpression operator = + new NamedArgumentExpression("operator", DSL.literal("OR")); + private final NamedArgumentExpression minimumShouldMatch = + new NamedArgumentExpression("minimum_should_match", DSL.literal("1")); + private final NamedArgumentExpression zeroTermsQueryAll = + new NamedArgumentExpression("zero_terms_query", DSL.literal("ALL")); + private final NamedArgumentExpression zeroTermsQueryNone = + new NamedArgumentExpression("zero_terms_query", DSL.literal("None")); + private final NamedArgumentExpression boost = + new NamedArgumentExpression("boost", DSL.literal("2.0")); + private final NamedArgumentExpression slop = + new NamedArgumentExpression("slop", DSL.literal("3")); @Test void match() { @@ -80,50 +84,135 @@ void match() { expr = DSL.match(field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient, operator); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient, + operator); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient, operator); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient, + operator); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient, operator, minimumShouldMatch); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient, + operator, + minimumShouldMatch); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient, operator, minimumShouldMatch, - zeroTermsQueryAll); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient, + operator, + minimumShouldMatch, + zeroTermsQueryAll); assertEquals(BOOLEAN, expr.type()); - expr = DSL.match( - field, query, analyzer, autoGenerateSynonymsPhrase, fuzziness, maxExpansions, prefixLength, - fuzzyTranspositions, fuzzyRewrite, lenient, operator, minimumShouldMatch, - zeroTermsQueryNone, boost); + expr = + DSL.match( + field, + query, + analyzer, + autoGenerateSynonymsPhrase, + fuzziness, + maxExpansions, + prefixLength, + fuzzyTranspositions, + fuzzyRewrite, + lenient, + operator, + minimumShouldMatch, + zeroTermsQueryNone, + boost); assertEquals(BOOLEAN, expr.type()); } @@ -134,20 +223,16 @@ void match_phrase() { } } - List match_phrase_dsl_expressions() { return List.of( - DSL.match_phrase(field, query), - DSL.match_phrase(field, query, analyzer), - DSL.match_phrase(field, query, analyzer, zeroTermsQueryAll), - DSL.match_phrase(field, query, analyzer, zeroTermsQueryNone, slop) - ); + DSL.match_phrase(field, query), + DSL.match_phrase(field, query, analyzer), + DSL.match_phrase(field, query, analyzer, zeroTermsQueryAll), + DSL.match_phrase(field, query, analyzer, zeroTermsQueryNone, slop)); } List match_phrase_prefix_dsl_expressions() { - return List.of( - DSL.match_phrase_prefix(field, query) - ); + return List.of(DSL.match_phrase_prefix(field, query)); } @Test @@ -160,7 +245,8 @@ public void match_phrase_prefix() { @Test void match_in_memory() { FunctionExpression expr = DSL.match(field, query); - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> expr.valueOf(valueEnv()), "OpenSearch defined function [match] is only supported in WHERE and HAVING clause."); } @@ -174,50 +260,52 @@ void match_to_string() { @Test void multi_match() { FunctionExpression expr = DSL.multi_match(fields, query); - assertEquals(String.format("multi_match(fields=%s, query=%s)", - fields.getValue(), query.getValue()), + assertEquals( + String.format("multi_match(fields=%s, query=%s)", fields.getValue(), query.getValue()), expr.toString()); } @Test void simple_query_string() { FunctionExpression expr = DSL.simple_query_string(fields, query); - assertEquals(String.format("simple_query_string(fields=%s, query=%s)", - fields.getValue(), query.getValue()), + assertEquals( + String.format( + "simple_query_string(fields=%s, query=%s)", fields.getValue(), query.getValue()), expr.toString()); } @Test void query() { FunctionExpression expr = DSL.query(query); - assertEquals(String.format("query(query=%s)", query.getValue()), - expr.toString()); + assertEquals(String.format("query(query=%s)", query.getValue()), expr.toString()); } @Test void query_string() { FunctionExpression expr = DSL.query_string(fields, query); - assertEquals(String.format("query_string(fields=%s, query=%s)", - fields.getValue(), query.getValue()), + assertEquals( + String.format("query_string(fields=%s, query=%s)", fields.getValue(), query.getValue()), expr.toString()); } @Test void wildcard_query() { FunctionExpression expr = DSL.wildcard_query(field, query); - assertEquals(String.format("wildcard_query(field=%s, query=%s)", - field.getValue(), query.getValue()), + assertEquals( + String.format("wildcard_query(field=%s, query=%s)", field.getValue(), query.getValue()), expr.toString()); } @Test void nested_query() { FunctionExpression expr = DSL.nested(DSL.ref("message.info", STRING)); - assertEquals(String.format("FunctionExpression(functionName=%s, arguments=[message.info])", - BuiltinFunctionName.NESTED.getName()), + assertEquals( + String.format( + "FunctionExpression(functionName=%s, arguments=[message.info])", + BuiltinFunctionName.NESTED.getName()), expr.toString()); - Environment nestedTuple = ExprValueUtils.tupleValue( - Map.of("message", Map.of("info", "result"))).bindingTuples(); + Environment nestedTuple = + ExprValueUtils.tupleValue(Map.of("message", Map.of("info", "result"))).bindingTuples(); assertEquals(expr.valueOf(nestedTuple), ExprValueUtils.stringValue("result")); assertEquals(expr.type(), STRING); } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/RelevanceFunctionResolverTest.java b/core/src/test/java/org/opensearch/sql/expression/function/RelevanceFunctionResolverTest.java index deba721481c..c678ac6eb41 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/RelevanceFunctionResolverTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/RelevanceFunctionResolverTest.java @@ -6,9 +6,7 @@ package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import static org.opensearch.sql.data.type.ExprCoreType.STRING; @@ -38,18 +36,15 @@ void resolve_correct_name_test() { void resolve_invalid_name_test() { var wrongFuncName = FunctionName.of("wrong_func"); var sig = new FunctionSignature(wrongFuncName, List.of(STRING)); - Exception exception = assertThrows(SemanticCheckException.class, - () -> resolver.resolve(sig)); - assertEquals("Expected 'sample_function' but got 'wrong_func'", - exception.getMessage()); + Exception exception = assertThrows(SemanticCheckException.class, () -> resolver.resolve(sig)); + assertEquals("Expected 'sample_function' but got 'wrong_func'", exception.getMessage()); } @Test void resolve_invalid_third_param_type_test() { var sig = new FunctionSignature(sampleFuncName, List.of(STRING, STRING, INTEGER, STRING)); - Exception exception = assertThrows(SemanticCheckException.class, - () -> resolver.resolve(sig)); - assertEquals("Expected type STRING instead of INTEGER for parameter #3", - exception.getMessage()); + Exception exception = assertThrows(SemanticCheckException.class, () -> resolver.resolve(sig)); + assertEquals( + "Expected type STRING instead of INTEGER for parameter #3", exception.getMessage()); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/WideningTypeRuleTest.java b/core/src/test/java/org/opensearch/sql/expression/function/WideningTypeRuleTest.java index 27b36a0fecf..3b6e5f7586c 100644 --- a/core/src/test/java/org/opensearch/sql/expression/function/WideningTypeRuleTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/function/WideningTypeRuleTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,8 +38,7 @@ class WideningTypeRuleTest { private static Table numberWidenRule = - new ImmutableTable.Builder() + new ImmutableTable.Builder() .put(BYTE, SHORT, 1) .put(BYTE, INTEGER, 2) .put(BYTE, LONG, 3) @@ -77,35 +75,37 @@ class WideningTypeRuleTest { private static Stream distanceArguments() { List exprTypes = ExprCoreType.coreTypes(); return Lists.cartesianProduct(exprTypes, exprTypes).stream() - .map(list -> { - ExprCoreType type1 = list.get(0); - ExprCoreType type2 = list.get(1); - if (type1 == type2) { - return Arguments.of(type1, type2, TYPE_EQUAL); - } else if (numberWidenRule.contains(type1, type2)) { - return Arguments.of(type1, type2, numberWidenRule.get(type1, type2)); - } else { - return Arguments.of(type1, type2, IMPOSSIBLE_WIDENING); - } - }); + .map( + list -> { + ExprCoreType type1 = list.get(0); + ExprCoreType type2 = list.get(1); + if (type1 == type2) { + return Arguments.of(type1, type2, TYPE_EQUAL); + } else if (numberWidenRule.contains(type1, type2)) { + return Arguments.of(type1, type2, numberWidenRule.get(type1, type2)); + } else { + return Arguments.of(type1, type2, IMPOSSIBLE_WIDENING); + } + }); } private static Stream validMaxTypes() { List exprTypes = ExprCoreType.coreTypes(); return Lists.cartesianProduct(exprTypes, exprTypes).stream() - .map(list -> { - ExprCoreType type1 = list.get(0); - ExprCoreType type2 = list.get(1); - if (type1 == type2) { - return Arguments.of(type1, type2, type1); - } else if (numberWidenRule.contains(type1, type2)) { - return Arguments.of(type1, type2, type2); - } else if (numberWidenRule.contains(type2, type1)) { - return Arguments.of(type1, type2, type1); - } else { - return Arguments.of(type1, type2, null); - } - }); + .map( + list -> { + ExprCoreType type1 = list.get(0); + ExprCoreType type2 = list.get(1); + if (type1 == type2) { + return Arguments.of(type1, type2, type1); + } else if (numberWidenRule.contains(type1, type2)) { + return Arguments.of(type1, type2, type2); + } else if (numberWidenRule.contains(type2, type1)) { + return Arguments.of(type1, type2, type1); + } else { + return Arguments.of(type1, type2, null); + } + }); } @ParameterizedTest @@ -118,8 +118,8 @@ public void distance(ExprCoreType v1, ExprCoreType v2, Integer expected) { @MethodSource("validMaxTypes") public void max(ExprCoreType v1, ExprCoreType v2, ExprCoreType expected) { if (null == expected) { - ExpressionEvaluationException exception = assertThrows( - ExpressionEvaluationException.class, () -> WideningTypeRule.max(v1, v2)); + ExpressionEvaluationException exception = + assertThrows(ExpressionEvaluationException.class, () -> WideningTypeRule.max(v1, v2)); assertEquals(String.format("no max type of %s and %s ", v1, v2), exception.getMessage()); } else { assertEquals(expected, WideningTypeRule.max(v1, v2)); @@ -128,10 +128,9 @@ public void max(ExprCoreType v1, ExprCoreType v2, ExprCoreType expected) { @Test public void maxOfUndefinedAndOthersShouldBeTheOtherType() { - ExprCoreType.coreTypes().forEach(type -> - assertEquals(type, WideningTypeRule.max(type, UNDEFINED))); - ExprCoreType.coreTypes().forEach(type -> - assertEquals(type, WideningTypeRule.max(UNDEFINED, type))); + ExprCoreType.coreTypes() + .forEach(type -> assertEquals(type, WideningTypeRule.max(type, UNDEFINED))); + ExprCoreType.coreTypes() + .forEach(type -> assertEquals(type, WideningTypeRule.max(UNDEFINED, type))); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunctionTest.java index 028ace62313..24c0d0decfe 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/ArithmeticFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.arthmetic; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -40,21 +39,34 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class ArithmeticFunctionTest extends ExpressionTestBase { private static Stream arithmeticFunctionArguments() { - List numberOp1 = Arrays.asList(new ExprByteValue(3), new ExprShortValue(3), - new ExprIntegerValue(3), new ExprLongValue(3L), new ExprFloatValue(3f), - new ExprDoubleValue(3D)); + List numberOp1 = + Arrays.asList( + new ExprByteValue(3), + new ExprShortValue(3), + new ExprIntegerValue(3), + new ExprLongValue(3L), + new ExprFloatValue(3f), + new ExprDoubleValue(3D)); List numberOp2 = - Arrays.asList(new ExprByteValue(2), new ExprShortValue(2), new ExprIntegerValue(2), + Arrays.asList( + new ExprByteValue(2), + new ExprShortValue(2), + new ExprIntegerValue(2), new ExprLongValue(3L), - new ExprFloatValue(2f), new ExprDoubleValue(2D)); + new ExprFloatValue(2f), + new ExprDoubleValue(2D)); return Lists.cartesianProduct(numberOp1, numberOp2).stream() .map(list -> Arguments.of(list.get(0), list.get(1))); } private static Stream arithmeticOperatorArguments() { - return Stream - .of(BuiltinFunctionName.ADD, BuiltinFunctionName.SUBTRACT, BuiltinFunctionName.MULTIPLY, - BuiltinFunctionName.DIVIDE, BuiltinFunctionName.DIVIDE).map(Arguments::of); + return Stream.of( + BuiltinFunctionName.ADD, + BuiltinFunctionName.SUBTRACT, + BuiltinFunctionName.MULTIPLY, + BuiltinFunctionName.DIVIDE, + BuiltinFunctionName.DIVIDE) + .map(Arguments::of); } @ParameterizedTest(name = "add({1}, {2})") @@ -73,10 +85,9 @@ public void addFunction(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.addFunction(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.ADDFUNCTION, - expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("add(%s, %s)", - op1.toString(), op2.toString()), expression.toString()); + assertValueEqual(BuiltinFunctionName.ADDFUNCTION, expectedType, op1, op2, expression.valueOf()); + assertEquals( + String.format("add(%s, %s)", op1.toString(), op2.toString()), expression.toString()); } @ParameterizedTest(name = "subtract({1}, {2})") @@ -85,10 +96,8 @@ public void subtract(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.subtract(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.SUBTRACT, expectedType, op1, op2, - expression.valueOf()); - assertEquals(String.format("-(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual(BuiltinFunctionName.SUBTRACT, expectedType, op1, op2, expression.valueOf()); + assertEquals(String.format("-(%s, %s)", op1.toString(), op2.toString()), expression.toString()); } @ParameterizedTest(name = "subtractFunction({1}, {2})") @@ -97,10 +106,10 @@ public void subtractFunction(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.subtractFunction(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.SUBTRACTFUNCTION, expectedType, op1, op2, - expression.valueOf()); - assertEquals(String.format("subtract(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual( + BuiltinFunctionName.SUBTRACTFUNCTION, expectedType, op1, op2, expression.valueOf()); + assertEquals( + String.format("subtract(%s, %s)", op1.toString(), op2.toString()), expression.toString()); } @ParameterizedTest(name = "mod({1}, {2})") @@ -110,8 +119,8 @@ public void mod(ExprValue op1, ExprValue op2) { ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); assertValueEqual(BuiltinFunctionName.MOD, expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("mod(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertEquals( + String.format("mod(%s, %s)", op1.toString(), op2.toString()), expression.toString()); expression = DSL.mod(literal(op1), literal(new ExprByteValue(0))); assertTrue(expression.valueOf(valueEnv()).isNull()); @@ -125,8 +134,8 @@ public void modulus(ExprValue op1, ExprValue op2) { ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); assertValueEqual(BuiltinFunctionName.MODULUS, expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("%%(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertEquals( + String.format("%%(%s, %s)", op1.toString(), op2.toString()), expression.toString()); expression = DSL.modulus(literal(op1), literal(new ExprByteValue(0))); assertTrue(expression.valueOf(valueEnv()).isNull()); @@ -139,10 +148,10 @@ public void modulusFunction(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.modulusFunction(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.MODULUSFUNCTION, - expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("modulus(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual( + BuiltinFunctionName.MODULUSFUNCTION, expectedType, op1, op2, expression.valueOf()); + assertEquals( + String.format("modulus(%s, %s)", op1.toString(), op2.toString()), expression.toString()); expression = DSL.modulusFunction(literal(op1), literal(new ExprByteValue(0))); assertTrue(expression.valueOf(valueEnv()).isNull()); @@ -155,10 +164,8 @@ public void multiply(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.multiply(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.MULTIPLY, expectedType, op1, op2, - expression.valueOf()); - assertEquals(String.format("*(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual(BuiltinFunctionName.MULTIPLY, expectedType, op1, op2, expression.valueOf()); + assertEquals(String.format("*(%s, %s)", op1.toString(), op2.toString()), expression.toString()); } @ParameterizedTest(name = "multiplyFunction({1}, {2})") @@ -167,10 +174,10 @@ public void multiplyFunction(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.multiplyFunction(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.MULTIPLYFUNCTION, expectedType, op1, op2, - expression.valueOf()); - assertEquals(String.format("multiply(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual( + BuiltinFunctionName.MULTIPLYFUNCTION, expectedType, op1, op2, expression.valueOf()); + assertEquals( + String.format("multiply(%s, %s)", op1.toString(), op2.toString()), expression.toString()); } @ParameterizedTest(name = "divide({1}, {2})") @@ -180,8 +187,7 @@ public void divide(ExprValue op1, ExprValue op2) { ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); assertValueEqual(BuiltinFunctionName.DIVIDE, expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("/(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertEquals(String.format("/(%s, %s)", op1.toString(), op2.toString()), expression.toString()); expression = DSL.divide(literal(op1), literal(new ExprByteValue(0))); assertTrue(expression.valueOf(valueEnv()).isNull()); @@ -194,10 +200,10 @@ public void divideFunction(ExprValue op1, ExprValue op2) { FunctionExpression expression = DSL.divideFunction(literal(op1), literal(op2)); ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type()); assertEquals(expectedType, expression.type()); - assertValueEqual(BuiltinFunctionName.DIVIDEFUNCTION, - expectedType, op1, op2, expression.valueOf()); - assertEquals(String.format("divide(%s, %s)", op1.toString(), op2.toString()), - expression.toString()); + assertValueEqual( + BuiltinFunctionName.DIVIDEFUNCTION, expectedType, op1, op2, expression.valueOf()); + assertEquals( + String.format("divide(%s, %s)", op1.toString(), op2.toString()), expression.toString()); expression = DSL.divideFunction(literal(op1), literal(new ExprByteValue(0))); assertTrue(expression.valueOf(valueEnv()).isNull()); @@ -207,38 +213,51 @@ public void divideFunction(ExprValue op1, ExprValue op2) { @ParameterizedTest(name = "multipleParameters({1},{2})") @MethodSource("arithmeticFunctionArguments") public void multipleParameters(ExprValue op1) { - assertThrows(ExpressionEvaluationException.class, - () -> DSL.add(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.addFunction(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.add(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.addFunction(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.subtract(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.subtractFunction(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.subtract(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.subtractFunction(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.multiply(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.multiplyFunction(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.multiply(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.multiplyFunction(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.divide(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.divideFunction(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.divide(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.divideFunction(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.mod(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.modulus(literal(op1), literal(op1), literal(op1))); - assertThrows(ExpressionEvaluationException.class, - () -> DSL.modulusFunction(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.mod(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.modulus(literal(op1), literal(op1), literal(op1))); + assertThrows( + ExpressionEvaluationException.class, + () -> DSL.modulusFunction(literal(op1), literal(op1), literal(op1))); } - protected void assertValueEqual(BuiltinFunctionName builtinFunctionName, ExprType type, - ExprValue op1, - ExprValue op2, - ExprValue actual) { + protected void assertValueEqual( + BuiltinFunctionName builtinFunctionName, + ExprType type, + ExprValue op1, + ExprValue op2, + ExprValue actual) { switch ((ExprCoreType) type) { case BYTE: Byte vb1 = op1.byteValue(); diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java index d9f95bc67a3..e084bf1d53c 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.arthmetic; import static org.hamcrest.MatcherAssert.assertThat; @@ -76,28 +75,30 @@ private static Stream testLogDoubleArguments() { } private static Stream testLogInvalidDoubleArguments() { - return Stream.of(Arguments.of(0D, -2D), - Arguments.of(0D, 2D), - Arguments.of(2D, 0D)); + return Stream.of(Arguments.of(0D, -2D), Arguments.of(0D, 2D), Arguments.of(2D, 0D)); } private static Stream trigonometricArguments() { Stream.Builder builder = Stream.builder(); return builder - .add(Arguments.of(1)).add(Arguments.of(1L)).add(Arguments.of(1F)).add(Arguments.of(1D)) + .add(Arguments.of(1)) + .add(Arguments.of(1L)) + .add(Arguments.of(1F)) + .add(Arguments.of(1D)) .build(); } private static Stream trigonometricDoubleArguments() { Stream.Builder builder = Stream.builder(); return builder - .add(Arguments.of(1, 2)).add(Arguments.of(1L, 2L)).add(Arguments.of(1F, 2F)) - .add(Arguments.of(1D, 2D)).build(); + .add(Arguments.of(1, 2)) + .add(Arguments.of(1L, 2L)) + .add(Arguments.of(1F, 2F)) + .add(Arguments.of(1D, 2D)) + .build(); } - /** - * Test abs with byte value. - */ + /** Test abs with byte value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(bytes = {-2, 2}) public void abs_byte_value(Byte value) { @@ -106,61 +107,43 @@ public void abs_byte_value(Byte value) { assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test abs with integer value. - */ + /** Test abs with integer value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(ints = {-2, 2}) public void abs_int_value(Integer value) { FunctionExpression abs = DSL.abs(DSL.literal(value)); - assertThat( - abs.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue(Math.abs(value)))); + assertThat(abs.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue(Math.abs(value)))); assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test abs with long value. - */ + /** Test abs with long value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(longs = {-2L, 2L}) public void abs_long_value(Long value) { FunctionExpression abs = DSL.abs(DSL.literal(value)); - assertThat( - abs.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue(Math.abs(value)))); + assertThat(abs.valueOf(valueEnv()), allOf(hasType(LONG), hasValue(Math.abs(value)))); assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test abs with float value. - */ + /** Test abs with float value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(floats = {-2f, 2f}) public void abs_float_value(Float value) { FunctionExpression abs = DSL.abs(DSL.literal(value)); - assertThat( - abs.valueOf(valueEnv()), - allOf(hasType(FLOAT), hasValue(Math.abs(value)))); + assertThat(abs.valueOf(valueEnv()), allOf(hasType(FLOAT), hasValue(Math.abs(value)))); assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test abs with double value. - */ + /** Test abs with double value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(doubles = {-2L, 2L}) public void abs_double_value(Double value) { FunctionExpression abs = DSL.abs(DSL.literal(value)); - assertThat( - abs.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.abs(value)))); + assertThat(abs.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.abs(value)))); assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test abs with short value. - */ + /** Test abs with short value. */ @ParameterizedTest(name = "abs({0})") @ValueSource(shorts = {-2, 2}) public void abs_short_value(Short value) { @@ -171,16 +154,12 @@ public void abs_short_value(Short value) { assertEquals(String.format("abs(%s)", value.toString()), abs.toString()); } - /** - * Test ceil/ceiling with integer value. - */ + /** Test ceil/ceiling with integer value. */ @ParameterizedTest(name = "ceil({0})") @ValueSource(ints = {2, -2}) public void ceil_int_value(Integer value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); - assertThat( - ceil.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); + assertThat(ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); @@ -189,15 +168,12 @@ public void ceil_int_value(Integer value) { assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); } - /** - * Test ceil/ceiling with long value. - */ + /** Test ceil/ceiling with long value. */ @ParameterizedTest(name = "ceil({0})") @ValueSource(longs = {2L, -2L}) public void ceil_long_value(Long value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); - assertThat( - ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); + assertThat(ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); @@ -206,15 +182,12 @@ public void ceil_long_value(Long value) { assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); } - /** - * Test ceil/ceiling with long value. - */ + /** Test ceil/ceiling with long value. */ @ParameterizedTest(name = "ceil({0})") @ValueSource(longs = {9223372036854775805L, -9223372036854775805L}) public void ceil_long_value_long(Long value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); - assertThat( - ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); + assertThat(ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); @@ -223,15 +196,12 @@ public void ceil_long_value_long(Long value) { assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); } - /** - * Test ceil/ceiling with float value. - */ + /** Test ceil/ceiling with float value. */ @ParameterizedTest(name = "ceil({0})") @ValueSource(floats = {2F, -2F}) public void ceil_float_value(Float value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); - assertThat( - ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); + assertThat(ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); @@ -240,15 +210,12 @@ public void ceil_float_value(Float value) { assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); } - /** - * Test ceil/ceiling with double value. - */ + /** Test ceil/ceiling with double value. */ @ParameterizedTest(name = "ceil({0})") @ValueSource(doubles = {-2L, 2L}) public void ceil_double_value(Double value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); - assertThat( - ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); + assertThat(ceil.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.ceil(value)))); assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); @@ -257,9 +224,7 @@ public void ceil_double_value(Double value) { assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); } - /** - * Test conv from decimal base with string as a number. - */ + /** Test conv from decimal base with string as a number. */ @ParameterizedTest(name = "conv({0})") @ValueSource(strings = {"1", "0", "-1"}) public void conv_from_decimal(String value) { @@ -282,34 +247,27 @@ public void conv_from_decimal(String value) { assertEquals(String.format("conv(\"%s\", 10, 16)", value), conv.toString()); } - /** - * Test conv from decimal base with integer as a number. - */ + /** Test conv from decimal base with integer as a number. */ @ParameterizedTest(name = "conv({0})") @ValueSource(ints = {1, 0, -1}) public void conv_from_decimal(Integer value) { FunctionExpression conv = DSL.conv(DSL.literal(value), DSL.literal(10), DSL.literal(2)); assertThat( - conv.valueOf(valueEnv()), - allOf(hasType(STRING), hasValue(Integer.toString(value, 2)))); + conv.valueOf(valueEnv()), allOf(hasType(STRING), hasValue(Integer.toString(value, 2)))); assertEquals(String.format("conv(%s, 10, 2)", value), conv.toString()); conv = DSL.conv(DSL.literal(value), DSL.literal(10), DSL.literal(8)); assertThat( - conv.valueOf(valueEnv()), - allOf(hasType(STRING), hasValue(Integer.toString(value, 8)))); + conv.valueOf(valueEnv()), allOf(hasType(STRING), hasValue(Integer.toString(value, 8)))); assertEquals(String.format("conv(%s, 10, 8)", value), conv.toString()); conv = DSL.conv(DSL.literal(value), DSL.literal(10), DSL.literal(16)); assertThat( - conv.valueOf(valueEnv()), - allOf(hasType(STRING), hasValue(Integer.toString(value, 16)))); + conv.valueOf(valueEnv()), allOf(hasType(STRING), hasValue(Integer.toString(value, 16)))); assertEquals(String.format("conv(%s, 10, 16)", value), conv.toString()); } - /** - * Test conv to decimal base with string as a number. - */ + /** Test conv to decimal base with string as a number. */ @ParameterizedTest(name = "conv({0})") @ValueSource(strings = {"11", "0", "11111"}) public void conv_to_decimal(String value) { @@ -332,9 +290,7 @@ public void conv_to_decimal(String value) { assertEquals(String.format("conv(\"%s\", 16, 10)", value), conv.toString()); } - /** - * Test conv to decimal base with integer as a number. - */ + /** Test conv to decimal base with integer as a number. */ @ParameterizedTest(name = "conv({0})") @ValueSource(ints = {11, 0, 11111}) public void conv_to_decimal(Integer value) { @@ -357,273 +313,187 @@ public void conv_to_decimal(Integer value) { assertEquals(String.format("conv(%s, 16, 10)", value), conv.toString()); } - /** - * Test crc32 with string value. - */ + /** Test crc32 with string value. */ @ParameterizedTest(name = "crc({0})") @ValueSource(strings = {"odfe", "sql"}) public void crc32_string_value(String value) { FunctionExpression crc = DSL.crc32(DSL.literal(value)); CRC32 crc32 = new CRC32(); crc32.update(value.getBytes()); - assertThat( - crc.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue(crc32.getValue()))); + assertThat(crc.valueOf(valueEnv()), allOf(hasType(LONG), hasValue(crc32.getValue()))); assertEquals(String.format("crc32(\"%s\")", value), crc.toString()); } - /** - * Test constant e. - */ + /** Test constant e. */ @Test public void test_e() { FunctionExpression e = DSL.euler(); assertThat(e.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.E))); } - /** - * Test exp with integer value. - */ + /** Test exp with integer value. */ @ParameterizedTest(name = "exp({0})") @ValueSource(ints = {-2, 2}) public void exp_int_value(Integer value) { FunctionExpression exp = DSL.exp(DSL.literal(value)); - assertThat( - exp.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); + assertThat(exp.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); assertEquals(String.format("exp(%s)", value.toString()), exp.toString()); } - /** - * Test exp with long value. - */ + /** Test exp with long value. */ @ParameterizedTest(name = "exp({0})") @ValueSource(longs = {-2L, 2L}) public void exp_long_value(Long value) { FunctionExpression exp = DSL.exp(DSL.literal(value)); - assertThat( - exp.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); + assertThat(exp.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); assertEquals(String.format("exp(%s)", value.toString()), exp.toString()); } - /** - * Test exp with float value. - */ + /** Test exp with float value. */ @ParameterizedTest(name = "exp({0})") @ValueSource(floats = {-2F, 2F}) public void exp_float_value(Float value) { FunctionExpression exp = DSL.exp(DSL.literal(value)); - assertThat( - exp.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); + assertThat(exp.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); assertEquals(String.format("exp(%s)", value.toString()), exp.toString()); } - /** - * Test exp with double value. - */ + /** Test exp with double value. */ @ParameterizedTest(name = "exp({0})") @ValueSource(doubles = {-2D, 2D}) public void exp_double_value(Double value) { FunctionExpression exp = DSL.exp(DSL.literal(value)); - assertThat( - exp.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); + assertThat(exp.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.exp(value)))); assertEquals(String.format("exp(%s)", value.toString()), exp.toString()); } - /** - * Test expm1 with integer value. - */ + /** Test expm1 with integer value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(ints = { - -1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE}) + @ValueSource(ints = {-1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE}) public void expm1_int_value(Integer value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test expm1 with long value. - */ + /** Test expm1 with long value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(longs = { - -1L, 0L, 1L, Long.MAX_VALUE, Long.MIN_VALUE}) + @ValueSource(longs = {-1L, 0L, 1L, Long.MAX_VALUE, Long.MIN_VALUE}) public void expm1_long_value(Long value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test expm1 with float value. - */ + /** Test expm1 with float value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(floats = { - -1.5F, -1F, 0F, 1F, 1.5F, Float.MAX_VALUE, Float.MIN_VALUE}) + @ValueSource(floats = {-1.5F, -1F, 0F, 1F, 1.5F, Float.MAX_VALUE, Float.MIN_VALUE}) public void expm1_float_value(Float value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test expm1 with double value. - */ + /** Test expm1 with double value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(doubles = { - -1.5D, -1D, 0D, 1D, 1.5D, Double.MAX_VALUE, Double.MIN_VALUE}) + @ValueSource(doubles = {-1.5D, -1D, 0D, 1D, 1.5D, Double.MAX_VALUE, Double.MIN_VALUE}) public void expm1_double_value(Double value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test expm1 with short value. - */ + /** Test expm1 with short value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(shorts = { - -1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE}) + @ValueSource(shorts = {-1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE}) public void expm1_short_value(Short value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test expm1 with byte value. - */ + /** Test expm1 with byte value. */ @ParameterizedTest(name = "expm1({0})") - @ValueSource(bytes = { - -1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE}) + @ValueSource(bytes = {-1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE}) public void expm1_byte_value(Byte value) { FunctionExpression expm1 = DSL.expm1(DSL.literal(value)); - assertThat( - expm1.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); + assertThat(expm1.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.expm1(value)))); assertEquals(String.format("expm1(%s)", value), expm1.toString()); } - /** - * Test floor with integer value. - */ + /** Test floor with integer value. */ @ParameterizedTest(name = "floor({0})") @ValueSource(ints = {-2, 2}) public void floor_int_value(Integer value) { FunctionExpression floor = DSL.floor(DSL.literal(value)); - assertThat( - floor.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.floor(value)))); + assertThat(floor.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.floor(value)))); assertEquals(String.format("floor(%s)", value), floor.toString()); } - /** - * Test floor with long value. - */ + /** Test floor with long value. */ @ParameterizedTest(name = "floor({0})") @ValueSource(longs = {-2L, 2L}) public void floor_long_value(Long value) { FunctionExpression floor = DSL.floor(DSL.literal(value)); - assertThat( - floor.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.floor(value)))); + assertThat(floor.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.floor(value)))); assertEquals(String.format("floor(%s)", value.toString()), floor.toString()); } - /** - * Test floor with float value. - */ + /** Test floor with float value. */ @ParameterizedTest(name = "floor({0})") @ValueSource(floats = {-2F, 2F}) public void floor_float_value(Float value) { FunctionExpression floor = DSL.floor(DSL.literal(value)); - assertThat( - floor.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.floor(value)))); + assertThat(floor.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.floor(value)))); assertEquals(String.format("floor(%s)", value.toString()), floor.toString()); } - /** - * Test floor with double value. - */ + /** Test floor with double value. */ @ParameterizedTest(name = "floor({0})") @ValueSource(doubles = {-2D, 2D}) public void floor_double_value(Double value) { FunctionExpression floor = DSL.floor(DSL.literal(value)); - assertThat( - floor.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.floor(value)))); + assertThat(floor.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.floor(value)))); assertEquals(String.format("floor(%s)", value.toString()), floor.toString()); } - /** - * Test ln with integer value. - */ + /** Test ln with integer value. */ @ParameterizedTest(name = "ln({0})") @ValueSource(ints = {2, 3}) public void ln_int_value(Integer value) { FunctionExpression ln = DSL.ln(DSL.literal(value)); - assertThat( - ln.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.log(value)))); + assertThat(ln.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.log(value)))); assertEquals(String.format("ln(%s)", value.toString()), ln.toString()); } - /** - * Test ln with long value. - */ + /** Test ln with long value. */ @ParameterizedTest(name = "ln({0})") @ValueSource(longs = {2L, 3L}) public void ln_long_value(Long value) { FunctionExpression ln = DSL.ln(DSL.literal(value)); - assertThat( - ln.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.log(value)))); + assertThat(ln.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.log(value)))); assertEquals(String.format("ln(%s)", value.toString()), ln.toString()); } - /** - * Test ln with float value. - */ + /** Test ln with float value. */ @ParameterizedTest(name = "ln({0})") @ValueSource(floats = {2F, 3F}) public void ln_float_value(Float value) { FunctionExpression ln = DSL.ln(DSL.literal(value)); - assertThat( - ln.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.log(value)))); + assertThat(ln.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.log(value)))); assertEquals(String.format("ln(%s)", value.toString()), ln.toString()); } - /** - * Test ln with double value. - */ + /** Test ln with double value. */ @ParameterizedTest(name = "ln({0})") @ValueSource(doubles = {2D, 3D}) public void ln_double_value(Double value) { FunctionExpression ln = DSL.ln(DSL.literal(value)); - assertThat( - ln.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.log(value)))); + assertThat(ln.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.log(value)))); assertEquals(String.format("ln(%s)", value.toString()), ln.toString()); } - /** - * Test ln with invalid value. - */ + /** Test ln with invalid value. */ @ParameterizedTest(name = "ln({0})") @ValueSource(doubles = {0D, -3D}) public void ln_invalid_value(Double value) { @@ -632,69 +502,47 @@ public void ln_invalid_value(Double value) { assertTrue(ln.valueOf(valueEnv()).isNull()); } - /** - * Test log with 1 int argument. - */ + /** Test log with 1 int argument. */ @ParameterizedTest(name = "log({0})") @ValueSource(ints = {2, 3}) public void log_int_value(Integer v) { FunctionExpression log = DSL.log(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v), 0.0001)); assertEquals(String.format("log(%s)", v.toString()), log.toString()); } - /** - * Test log with 1 long argument. - */ + /** Test log with 1 long argument. */ @ParameterizedTest(name = "log({0})") @ValueSource(longs = {2L, 3L}) public void log_int_value(Long v) { FunctionExpression log = DSL.log(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v), 0.0001)); assertEquals(String.format("log(%s)", v.toString()), log.toString()); } - /** - * Test log with 1 float argument. - */ + /** Test log with 1 float argument. */ @ParameterizedTest(name = "log({0})") @ValueSource(floats = {2F, 3F}) public void log_float_value(Float v) { FunctionExpression log = DSL.log(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v), 0.0001)); assertEquals(String.format("log(%s)", v.toString()), log.toString()); } - /** - * Test log with 1 double argument. - */ + /** Test log with 1 double argument. */ @ParameterizedTest(name = "log({0})") @ValueSource(doubles = {2D, 3D}) public void log_double_value(Double v) { FunctionExpression log = DSL.log(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v), 0.0001)); assertEquals(String.format("log(%s)", v.toString()), log.toString()); } - /** - * Test log with 1 invalid value. - */ + /** Test log with 1 invalid value. */ @ParameterizedTest(name = "log({0})") @ValueSource(doubles = {0D, -3D}) public void log_invalid_value(Double value) { @@ -703,65 +551,51 @@ public void log_invalid_value(Double value) { assertTrue(log.valueOf(valueEnv()).isNull()); } - /** - * Test log with 2 int arguments. - */ + /** Test log with 2 int arguments. */ @ParameterizedTest(name = "log({0}, {1})") @MethodSource("testLogIntegerArguments") public void log_two_int_value(Integer v1, Integer v2) { FunctionExpression log = DSL.log(DSL.literal(v1), DSL.literal(v2)); assertEquals(log.type(), DOUBLE); assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v2) / Math.log(v1), 0.0001)); + getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v2) / Math.log(v1), 0.0001)); assertEquals(String.format("log(%s, %s)", v1.toString(), v2.toString()), log.toString()); } - /** - * Test log with 2 long arguments. - */ + /** Test log with 2 long arguments. */ @ParameterizedTest(name = "log({0}, {1})") @MethodSource("testLogLongArguments") public void log_two_long_value(Long v1, Long v2) { FunctionExpression log = DSL.log(DSL.literal(v1), DSL.literal(v2)); assertEquals(log.type(), DOUBLE); assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v2) / Math.log(v1), 0.0001)); + getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v2) / Math.log(v1), 0.0001)); assertEquals(String.format("log(%s, %s)", v1.toString(), v2.toString()), log.toString()); } - /** - * Test log with 2 float arguments. - */ + /** Test log with 2 float arguments. */ @ParameterizedTest(name = "log({0}, {1})") @MethodSource("testLogFloatArguments") public void log_two_double_value(Float v1, Float v2) { FunctionExpression log = DSL.log(DSL.literal(v1), DSL.literal(v2)); assertEquals(log.type(), DOUBLE); assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v2) / Math.log(v1), 0.0001)); + getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v2) / Math.log(v1), 0.0001)); assertEquals(String.format("log(%s, %s)", v1.toString(), v2.toString()), log.toString()); } - /** - * Test log with 2 double arguments. - */ + /** Test log with 2 double arguments. */ @ParameterizedTest(name = "log({0}, {1})") @MethodSource("testLogDoubleArguments") public void log_two_double_value(Double v1, Double v2) { FunctionExpression log = DSL.log(DSL.literal(v1), DSL.literal(v2)); assertEquals(log.type(), DOUBLE); assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v2) / Math.log(v1), 0.0001)); + getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v2) / Math.log(v1), 0.0001)); assertEquals(String.format("log(%s, %s)", v1.toString(), v2.toString()), log.toString()); } - /** - * Test log with 2 invalid double arguments. - */ + /** Test log with 2 invalid double arguments. */ @ParameterizedTest(name = "log({0}, {2})") @MethodSource("testLogInvalidDoubleArguments") public void log_two_invalid_double_value(Double v1, Double v2) { @@ -770,69 +604,47 @@ public void log_two_invalid_double_value(Double v1, Double v2) { assertTrue(log.valueOf(valueEnv()).isNull()); } - /** - * Test log10 with int value. - */ + /** Test log10 with int value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(ints = {2, 3}) public void log10_int_value(Integer v) { FunctionExpression log = DSL.log10(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log10(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log10(v), 0.0001)); assertEquals(String.format("log10(%s)", v.toString()), log.toString()); } - /** - * Test log10 with long value. - */ + /** Test log10 with long value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(longs = {2L, 3L}) public void log10_long_value(Long v) { FunctionExpression log = DSL.log10(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log10(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log10(v), 0.0001)); assertEquals(String.format("log10(%s)", v.toString()), log.toString()); } - /** - * Test log10 with float value. - */ + /** Test log10 with float value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(floats = {2F, 3F}) public void log10_float_value(Float v) { FunctionExpression log = DSL.log10(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log10(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log10(v), 0.0001)); assertEquals(String.format("log10(%s)", v.toString()), log.toString()); } - /** - * Test log10 with int value. - */ + /** Test log10 with int value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(doubles = {2D, 3D}) public void log10_double_value(Double v) { FunctionExpression log = DSL.log10(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log10(v), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log10(v), 0.0001)); assertEquals(String.format("log10(%s)", v.toString()), log.toString()); } - /** - * Test log10 with 1 invalid double argument. - */ + /** Test log10 with 1 invalid double argument. */ @ParameterizedTest(name = "log10({0})") @ValueSource(doubles = {0D, -3D}) public void log10_two_invalid_value(Double v) { @@ -841,69 +653,47 @@ public void log10_two_invalid_value(Double v) { assertTrue(log.valueOf(valueEnv()).isNull()); } - /** - * Test log2 with int value. - */ + /** Test log2 with int value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(ints = {2, 3}) public void log2_int_value(Integer v) { FunctionExpression log = DSL.log2(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v) / Math.log(2), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v) / Math.log(2), 0.0001)); assertEquals(String.format("log2(%s)", v.toString()), log.toString()); } - /** - * Test log2 with long value. - */ + /** Test log2 with long value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(longs = {2L, 3L}) public void log2_long_value(Long v) { FunctionExpression log = DSL.log2(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v) / Math.log(2), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v) / Math.log(2), 0.0001)); assertEquals(String.format("log2(%s)", v.toString()), log.toString()); } - /** - * Test log2 with float value. - */ + /** Test log2 with float value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(floats = {2F, 3F}) public void log2_float_value(Float v) { FunctionExpression log = DSL.log2(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v) / Math.log(2), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v) / Math.log(2), 0.0001)); assertEquals(String.format("log2(%s)", v.toString()), log.toString()); } - /** - * Test log2 with double value. - */ + /** Test log2 with double value. */ @ParameterizedTest(name = "log10({0})") @ValueSource(doubles = {2D, 3D}) public void log2_double_value(Double v) { FunctionExpression log = DSL.log2(DSL.literal(v)); assertEquals(log.type(), DOUBLE); - assertThat( - getDoubleValue(log.valueOf(valueEnv())), - closeTo(Math.log(v) / Math.log(2), 0.0001) - ); + assertThat(getDoubleValue(log.valueOf(valueEnv())), closeTo(Math.log(v) / Math.log(2), 0.0001)); assertEquals(String.format("log2(%s)", v.toString()), log.toString()); } - /** - * Test log2 with an invalid double value. - */ + /** Test log2 with an invalid double value. */ @ParameterizedTest(name = "log2({0})") @ValueSource(doubles = {0D, -2D}) public void log2_invalid_double_value(Double v) { @@ -912,9 +702,7 @@ public void log2_invalid_double_value(Double v) { assertTrue(log.valueOf(valueEnv()).isNull()); } - /** - * Test mod with byte value. - */ + /** Test mod with byte value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogByteArguments") public void mod_byte_value(Byte v1, Byte v2) { @@ -930,9 +718,7 @@ public void mod_byte_value(Byte v1, Byte v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test mod with short value. - */ + /** Test mod with short value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogShortArguments") public void mod_short_value(Short v1, Short v2) { @@ -940,8 +726,7 @@ public void mod_short_value(Short v1, Short v2) { assertThat( mod.valueOf(valueEnv()), - allOf(hasType(SHORT), - hasValue(Integer.valueOf(v1 % v2).shortValue()))); + allOf(hasType(SHORT), hasValue(Integer.valueOf(v1 % v2).shortValue()))); assertEquals(String.format("mod(%s, %s)", v1, v2), mod.toString()); mod = DSL.mod(DSL.literal(v1), DSL.literal(new ExprShortValue(0))); @@ -949,16 +734,12 @@ public void mod_short_value(Short v1, Short v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test mod with integer value. - */ + /** Test mod with integer value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogIntegerArguments") public void mod_int_value(Integer v1, Integer v2) { FunctionExpression mod = DSL.mod(DSL.literal(v1), DSL.literal(v2)); - assertThat( - mod.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue(v1 % v2))); + assertThat(mod.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue(v1 % v2))); assertEquals(String.format("mod(%s, %s)", v1, v2), mod.toString()); mod = DSL.mod(DSL.literal(v1), DSL.literal(0)); @@ -966,16 +747,12 @@ public void mod_int_value(Integer v1, Integer v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test mod with long value. - */ + /** Test mod with long value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogLongArguments") public void mod_long_value(Long v1, Long v2) { FunctionExpression mod = DSL.mod(DSL.literal(v1), DSL.literal(v2)); - assertThat( - mod.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue(v1 % v2))); + assertThat(mod.valueOf(valueEnv()), allOf(hasType(LONG), hasValue(v1 % v2))); assertEquals(String.format("mod(%s, %s)", v1, v2), mod.toString()); mod = DSL.mod(DSL.literal(v1), DSL.literal(0)); @@ -983,16 +760,12 @@ public void mod_long_value(Long v1, Long v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test mod with long value. - */ + /** Test mod with long value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogFloatArguments") public void mod_float_value(Float v1, Float v2) { FunctionExpression mod = DSL.mod(DSL.literal(v1), DSL.literal(v2)); - assertThat( - mod.valueOf(valueEnv()), - allOf(hasType(FLOAT), hasValue(v1 % v2))); + assertThat(mod.valueOf(valueEnv()), allOf(hasType(FLOAT), hasValue(v1 % v2))); assertEquals(String.format("mod(%s, %s)", v1, v2), mod.toString()); mod = DSL.mod(DSL.literal(v1), DSL.literal(0)); @@ -1000,16 +773,12 @@ public void mod_float_value(Float v1, Float v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test mod with double value. - */ + /** Test mod with double value. */ @ParameterizedTest(name = "mod({0}, {1})") @MethodSource("testLogDoubleArguments") public void mod_double_value(Double v1, Double v2) { FunctionExpression mod = DSL.mod(DSL.literal(v1), DSL.literal(v2)); - assertThat( - mod.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(v1 % v2))); + assertThat(mod.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(v1 % v2))); assertEquals(String.format("mod(%s, %s)", v1, v2), mod.toString()); mod = DSL.mod(DSL.literal(v1), DSL.literal(0)); @@ -1017,105 +786,72 @@ public void mod_double_value(Double v1, Double v2) { assertTrue(mod.valueOf(valueEnv()).isNull()); } - /** - * Test pow/power with short value. - */ + /** Test pow/power with short value. */ @ParameterizedTest(name = "pow({0}, {1}") @MethodSource("testLogShortArguments") public void pow_short_value(Short v1, Short v2) { FunctionExpression pow = DSL.pow(DSL.literal(v1), DSL.literal(v2)); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); - FunctionExpression power = - DSL.power(DSL.literal(v1), DSL.literal(v2)); - assertThat( - power.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + FunctionExpression power = DSL.power(DSL.literal(v1), DSL.literal(v2)); + assertThat(power.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); } - /** - * Test pow/power with integer value. - */ + /** Test pow/power with integer value. */ @ParameterizedTest(name = "pow({0}, {1}") @MethodSource("testLogIntegerArguments") public void pow_int_value(Integer v1, Integer v2) { FunctionExpression pow = DSL.pow(DSL.literal(v1), DSL.literal(v2)); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); FunctionExpression power = DSL.power(DSL.literal(v1), DSL.literal(v2)); - assertThat( - power.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(power.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); } - /** - * Test pow/power with long value. - */ + /** Test pow/power with long value. */ @ParameterizedTest(name = "pow({0}, {1}") @MethodSource("testLogLongArguments") public void pow_long_value(Long v1, Long v2) { FunctionExpression pow = DSL.pow(DSL.literal(v1), DSL.literal(v2)); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); FunctionExpression power = DSL.power(DSL.literal(v1), DSL.literal(v2)); - assertThat( - power.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(power.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); } - /** - * Test pow/power with float value. - */ + /** Test pow/power with float value. */ @ParameterizedTest(name = "pow({0}, {1}") @MethodSource("testLogFloatArguments") public void pow_float_value(Float v1, Float v2) { FunctionExpression pow = DSL.pow(DSL.literal(v1), DSL.literal(v2)); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); FunctionExpression power = DSL.power(DSL.literal(v1), DSL.literal(v2)); - assertThat( - power.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(power.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); } - /** - * Test pow/power with double value. - */ + /** Test pow/power with double value. */ @ParameterizedTest(name = "pow({0}, {1}") @MethodSource("testLogDoubleArguments") public void pow_double_value(Double v1, Double v2) { FunctionExpression pow = DSL.pow(DSL.literal(v1), DSL.literal(v2)); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); FunctionExpression power = DSL.power(DSL.literal(v1), DSL.literal(v2)); - assertThat( - power.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); + assertThat(power.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(v1, v2)))); assertEquals(String.format("pow(%s, %s)", v1, v2), pow.toString()); } - /** - * Test pow/power with null output. - */ + /** Test pow/power with null output. */ @Test public void pow_null_output() { FunctionExpression pow = DSL.pow(DSL.literal((double) -2), DSL.literal(1.5)); @@ -1129,367 +865,318 @@ public void pow_null_output() { assertTrue(pow.valueOf(valueEnv()).isNull()); } - /** - * Test pow/power with edge cases. - */ + /** Test pow/power with edge cases. */ @Test public void pow_edge_cases() { FunctionExpression pow = DSL.pow(DSL.literal((double) -2), DSL.literal((double) 2)); assertEquals(pow.type(), DOUBLE); - assertEquals(String.format("pow(%s, %s)",(double) -2, (double) 2), pow.toString()); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(-2, 2)))); + assertEquals(String.format("pow(%s, %s)", (double) -2, (double) 2), pow.toString()); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(-2, 2)))); pow = DSL.pow(DSL.literal((double) 2), DSL.literal((double) 1.5)); assertEquals(pow.type(), DOUBLE); assertEquals(String.format("pow(%s, %s)", (double) 2, (double) 1.5), pow.toString()); - assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow(2, 1.5)))); + assertThat(pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow(2, 1.5)))); pow = DSL.pow(DSL.literal((float) -2), DSL.literal((float) 2)); assertEquals(pow.type(), DOUBLE); assertEquals(String.format("pow(%s, %s)", (float) -2, (float) 2), pow.toString()); assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow((float) -2, (float) 2)))); + pow.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.pow((float) -2, (float) 2)))); pow = DSL.pow(DSL.literal((float) 2), DSL.literal((float) 1.5)); assertEquals(pow.type(), DOUBLE); assertEquals(String.format("pow(%s, %s)", (float) 2, (float) 1.5), pow.toString()); assertThat( - pow.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.pow((float) 2, (float) 1.5)))); + pow.valueOf(valueEnv()), + allOf(hasType(DOUBLE), hasValue(Math.pow((float) 2, (float) 1.5)))); } - /** - * Test rint with byte value. - */ + /** Test rint with byte value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(bytes = { - -1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE}) + @ValueSource(bytes = {-1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE}) public void rint_byte_value(Byte value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test rint with short value. - */ + /** Test rint with short value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(shorts = { - -1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE}) + @ValueSource(shorts = {-1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE}) public void rint_short_value(Short value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test rint with integer value. - */ + /** Test rint with integer value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(ints = { - -1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE}) + @ValueSource(ints = {-1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE}) public void rint_int_value(Integer value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test rint with long value. - */ + /** Test rint with long value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(longs = { - -1L, 0L, 1L, Long.MAX_VALUE, Long.MIN_VALUE}) + @ValueSource(longs = {-1L, 0L, 1L, Long.MAX_VALUE, Long.MIN_VALUE}) public void rint_long_value(Long value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test rint with float value. - */ + /** Test rint with float value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(floats = { - -1F, -0.75F, -0.5F, 0F, 0.5F, 0.500000001F, - 0.75F, 1F, 1.9999F, 42.42F, Float.MAX_VALUE, Float.MIN_VALUE}) + @ValueSource( + floats = { + -1F, + -0.75F, + -0.5F, + 0F, + 0.5F, + 0.500000001F, + 0.75F, + 1F, + 1.9999F, + 42.42F, + Float.MAX_VALUE, + Float.MIN_VALUE + }) public void rint_float_value(Float value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test rint with double value. - */ + /** Test rint with double value. */ @ParameterizedTest(name = "rint({0})") - @ValueSource(doubles = { - -1F, -0.75F, -0.5F, 0F, 0.5F, 0.500000001F, - 0.75F, 1F, 1.9999F, 42.42F, Double.MAX_VALUE, Double.MIN_VALUE}) + @ValueSource( + doubles = { + -1F, + -0.75F, + -0.5F, + 0F, + 0.5F, + 0.500000001F, + 0.75F, + 1F, + 1.9999F, + 42.42F, + Double.MAX_VALUE, + Double.MIN_VALUE + }) public void rint_double_value(Double value) { FunctionExpression rint = DSL.rint(DSL.literal(value)); - assertThat( - rint.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); + assertThat(rint.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.rint(value)))); assertEquals(String.format("rint(%s)", value), rint.toString()); } - /** - * Test round with integer value. - */ + /** Test round with integer value. */ @ParameterizedTest(name = "round({0}") @ValueSource(ints = {21, -21}) public void round_int_value(Integer value) { FunctionExpression round = DSL.round(DSL.literal(value)); - assertThat( - round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.round(value)))); + assertThat(round.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.round(value)))); assertEquals(String.format("round(%s)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue( - new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).longValue()))); + allOf( + hasType(LONG), + hasValue(new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).longValue()))); assertEquals(String.format("round(%s, 1)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(-1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue( - new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).longValue()))); + allOf( + hasType(LONG), + hasValue(new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).longValue()))); assertEquals(String.format("round(%s, -1)", value), round.toString()); } - /** - * Test round with long value. - */ + /** Test round with long value. */ @ParameterizedTest(name = "round({0}") @ValueSource(longs = {21L, -21L}) public void round_long_value(Long value) { FunctionExpression round = DSL.round(DSL.literal(value)); - assertThat( - round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue((long) Math.round(value)))); + assertThat(round.valueOf(valueEnv()), allOf(hasType(LONG), hasValue((long) Math.round(value)))); assertEquals(String.format("round(%s)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue( - new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).longValue()))); + allOf( + hasType(LONG), + hasValue(new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).longValue()))); assertEquals(String.format("round(%s, 1)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(-1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(LONG), hasValue( - new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).longValue()))); + allOf( + hasType(LONG), + hasValue(new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).longValue()))); assertEquals(String.format("round(%s, -1)", value), round.toString()); } - /** - * Test round with float value. - */ + /** Test round with float value. */ @ParameterizedTest(name = "round({0}") @ValueSource(floats = {21F, -21F}) public void round_float_value(Float value) { FunctionExpression round = DSL.round(DSL.literal(value)); assertThat( - round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue((double) Math.round(value)))); + round.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue((double) Math.round(value)))); assertEquals(String.format("round(%s)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue( - new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue()))); + allOf( + hasType(DOUBLE), + hasValue(new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue()))); assertEquals(String.format("round(%s, 1)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(-1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue( - new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).doubleValue()))); + allOf( + hasType(DOUBLE), + hasValue(new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).doubleValue()))); assertEquals(String.format("round(%s, -1)", value), round.toString()); } - /** - * Test round with double value. - */ + /** Test round with double value. */ @ParameterizedTest(name = "round({0}") @ValueSource(doubles = {21D, -21D}) public void round_double_value(Double value) { FunctionExpression round = DSL.round(DSL.literal(value)); assertThat( - round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue((double) Math.round(value)))); + round.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue((double) Math.round(value)))); assertEquals(String.format("round(%s)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue( - new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue()))); + allOf( + hasType(DOUBLE), + hasValue(new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue()))); assertEquals(String.format("round(%s, 1)", value), round.toString()); round = DSL.round(DSL.literal(value), DSL.literal(-1)); assertThat( round.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue( - new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).doubleValue()))); + allOf( + hasType(DOUBLE), + hasValue(new BigDecimal(value).setScale(-1, RoundingMode.HALF_UP).doubleValue()))); assertEquals(String.format("round(%s, -1)", value), round.toString()); } - /** - * Test sign with integer value. - */ + /** Test sign with integer value. */ @ParameterizedTest(name = "sign({0})") @ValueSource(ints = {2, -2}) public void sign_int_value(Integer value) { FunctionExpression sign = DSL.sign(DSL.literal(value)); assertThat( - sign.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + sign.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("sign(%s)", value), sign.toString()); } - /** - * Test sign with long value. - */ + /** Test sign with long value. */ @ParameterizedTest(name = "sign({0})") @ValueSource(longs = {2L, -2L}) public void sign_long_value(Long value) { FunctionExpression sign = DSL.sign(DSL.literal(value)); assertThat( - sign.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + sign.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("sign(%s)", value), sign.toString()); } - /** - * Test sign with float value. - */ + /** Test sign with float value. */ @ParameterizedTest(name = "sign({0})") @ValueSource(floats = {2F, -2F}) public void sign_float_value(Float value) { FunctionExpression sign = DSL.sign(DSL.literal(value)); assertThat( - sign.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + sign.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("sign(%s)", value), sign.toString()); } - /** - * Test sign with double value. - */ + /** Test sign with double value. */ @ParameterizedTest(name = "sign({0})") @ValueSource(doubles = {2, -2}) public void sign_double_value(Double value) { FunctionExpression sign = DSL.sign(DSL.literal(value)); assertThat( - sign.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + sign.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("sign(%s)", value), sign.toString()); } - /** - * Test signum with byte value. - */ + /** Test signum with byte value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(bytes = {2, 0, -2}) public void signum_bytes_value(Byte value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); } - /** - * Test signum with short value. - */ + /** Test signum with short value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(shorts = {2, 0, -2}) public void signum_short_value(Short value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); } - /** - * Test signum with integer value. - */ + /** Test signum with integer value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(ints = {2, 0, -2}) public void signum_int_value(Integer value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); } - /** - * Test signum with long value. - */ + /** Test signum with long value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(longs = {2L, 0L, -2L}) public void signum_long_value(Long value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); } - /** - * Test signum with float value. - */ + /** Test signum with float value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(floats = {2F, 0F, -2F}) public void signum_float_value(Float value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); } - /** - * Test signum with double value. - */ + /** Test signum with double value. */ @ParameterizedTest(name = "signum({0})") @ValueSource(doubles = {2, 0, -2}) public void signum_double_value(Double value) { FunctionExpression signum = DSL.signum(DSL.literal(value)); assertThat( - signum.valueOf(valueEnv()), - allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); + signum.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.signum(value)))); assertEquals(String.format("signum(%s)", value), signum.toString()); - } + } - /** - * Test sinh with byte value. - */ + /** Test sinh with byte value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(bytes = {-1, 1, 2, Byte.MAX_VALUE, Byte.MIN_VALUE}) public void sinh_byte_value(Byte value) { @@ -1498,9 +1185,7 @@ public void sinh_byte_value(Byte value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sinh with short value. - */ + /** Test sinh with short value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(shorts = {-1, 1, 2, Short.MAX_VALUE, Short.MIN_VALUE}) public void sinh_short_value(Short value) { @@ -1509,9 +1194,7 @@ public void sinh_short_value(Short value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sinh with integer value. - */ + /** Test sinh with integer value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(ints = {-1, 1, 2, Integer.MAX_VALUE, Integer.MIN_VALUE}) public void sinh_int_value(Integer value) { @@ -1520,9 +1203,7 @@ public void sinh_int_value(Integer value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sinh with long value. - */ + /** Test sinh with long value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(longs = {-1L, 1L, 2L, Long.MAX_VALUE, Long.MIN_VALUE}) public void sinh_long_value(Long value) { @@ -1531,9 +1212,7 @@ public void sinh_long_value(Long value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sinh with float value. - */ + /** Test sinh with float value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(floats = {-1.5F, -1F, 1F, 1.5F, 2F, 2.7F, Float.MAX_VALUE, Float.MIN_VALUE}) public void sinh_float_value(Float value) { @@ -1542,9 +1221,7 @@ public void sinh_float_value(Float value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sinh with double value. - */ + /** Test sinh with double value. */ @ParameterizedTest(name = "sinh({0})") @ValueSource(doubles = {-1.5, -1D, 1D, 1.5D, 2D, 2.7D, Double.MAX_VALUE, Double.MIN_VALUE}) public void sinh_double_value(Double value) { @@ -1553,9 +1230,7 @@ public void sinh_double_value(Double value) { assertEquals(String.format("sinh(%s)", value), sinh.toString()); } - /** - * Test sqrt with int value. - */ + /** Test sqrt with int value. */ @ParameterizedTest(name = "sqrt({0})") @ValueSource(ints = {1, 2}) public void sqrt_int_value(Integer value) { @@ -1564,9 +1239,7 @@ public void sqrt_int_value(Integer value) { assertEquals(String.format("sqrt(%s)", value), sqrt.toString()); } - /** - * Test sqrt with long value. - */ + /** Test sqrt with long value. */ @ParameterizedTest(name = "sqrt({0})") @ValueSource(longs = {1L, 2L}) public void sqrt_long_value(Long value) { @@ -1575,9 +1248,7 @@ public void sqrt_long_value(Long value) { assertEquals(String.format("sqrt(%s)", value), sqrt.toString()); } - /** - * Test sqrt with float value. - */ + /** Test sqrt with float value. */ @ParameterizedTest(name = "sqrt({0})") @ValueSource(floats = {1F, 2F}) public void sqrt_float_value(Float value) { @@ -1586,9 +1257,7 @@ public void sqrt_float_value(Float value) { assertEquals(String.format("sqrt(%s)", value), sqrt.toString()); } - /** - * Test sqrt with double value. - */ + /** Test sqrt with double value. */ @ParameterizedTest(name = "sqrt({0})") @ValueSource(doubles = {1D, 2D}) public void sqrt_double_value(Double value) { @@ -1597,9 +1266,7 @@ public void sqrt_double_value(Double value) { assertEquals(String.format("sqrt(%s)", value), sqrt.toString()); } - /** - * Test sqrt with negative value. - */ + /** Test sqrt with negative value. */ @ParameterizedTest(name = "sqrt({0})") @ValueSource(doubles = {-1D, -2D}) public void sqrt_negative_value(Double value) { @@ -1608,73 +1275,90 @@ public void sqrt_negative_value(Double value) { assertTrue(sqrt.valueOf(valueEnv()).isNull()); } - /** - * Test truncate with integer value. - */ + /** Test truncate with integer value. */ @ParameterizedTest(name = "truncate({0}, {1})") @ValueSource(ints = {2, -2, Integer.MAX_VALUE, Integer.MIN_VALUE}) public void truncate_int_value(Integer value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( - truncate.valueOf(valueEnv()), allOf(hasType(LONG), + truncate.valueOf(valueEnv()), + allOf( + hasType(LONG), hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } - /** - * Test truncate with long value. - */ + /** Test truncate with long value. */ @ParameterizedTest(name = "truncate({0}, {1})") @ValueSource(longs = {2L, -2L, Long.MAX_VALUE, Long.MIN_VALUE}) public void truncate_long_value(Long value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( - truncate.valueOf(valueEnv()), allOf(hasType(LONG), + truncate.valueOf(valueEnv()), + allOf( + hasType(LONG), hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } - /** - * Test truncate with float value. - */ + /** Test truncate with float value. */ @ParameterizedTest(name = "truncate({0}, {1})") @ValueSource(floats = {2F, -2F, Float.MAX_VALUE, Float.MIN_VALUE}) public void truncate_float_value(Float value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( - truncate.valueOf(valueEnv()), allOf(hasType(DOUBLE), + truncate.valueOf(valueEnv()), + allOf( + hasType(DOUBLE), hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } - /** - * Test truncate with double value. - */ + /** Test truncate with double value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(doubles = {2D, -9.223372036854776e+18D, -2147483649.0D, -2147483648.0D, - -32769.0D, -32768.0D, -34.84D, -2.0D, -1.2D, -1.0D, 0.0D, 1.0D, - 1.3D, 2.0D, 1004.3D, 32767.0D, 32768.0D, 2147483647.0D, 2147483648.0D, - 9.223372036854776e+18D, Double.MAX_VALUE, Double.MIN_VALUE}) + @ValueSource( + doubles = { + 2D, + -9.223372036854776e+18D, + -2147483649.0D, + -2147483648.0D, + -32769.0D, + -32768.0D, + -34.84D, + -2.0D, + -1.2D, + -1.0D, + 0.0D, + 1.0D, + 1.3D, + 2.0D, + 1004.3D, + 32767.0D, + 32768.0D, + 2147483647.0D, + 2147483648.0D, + 9.223372036854776e+18D, + Double.MAX_VALUE, + Double.MIN_VALUE + }) public void truncate_double_value(Double value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( - truncate.valueOf(valueEnv()), allOf(hasType(DOUBLE), + truncate.valueOf(valueEnv()), + allOf( + hasType(DOUBLE), hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } - /** - * Test constant pi. - */ + /** Test constant pi. */ @Test public void test_pi() { FunctionExpression pi = DSL.pi(); assertThat(pi.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.PI))); } - /** - * Test rand with no argument. - */ + /** Test rand with no argument. */ @Test public void rand_no_arg() { FunctionExpression rand = DSL.rand(); @@ -1685,9 +1369,7 @@ public void rand_no_arg() { assertEquals("rand()", rand.toString()); } - /** - * Test rand with integer value. - */ + /** Test rand with integer value. */ @ParameterizedTest(name = "rand({0})") @ValueSource(ints = {2, 3}) public void rand_int_value(Integer n) { @@ -1695,27 +1377,22 @@ public void rand_int_value(Integer n) { assertEquals(FLOAT, rand.type()); assertTrue( getFloatValue(rand.valueOf(valueEnv())) >= 0 - && getFloatValue(rand.valueOf(valueEnv())) < 1); + && getFloatValue(rand.valueOf(valueEnv())) < 1); assertEquals(getFloatValue(rand.valueOf(valueEnv())), new Random(n).nextFloat()); assertEquals(String.format("rand(%s)", n), rand.toString()); } - /** - * Test acos with integer, long, float, double values. - */ + /** Test acos with integer, long, float, double values. */ @ParameterizedTest(name = "acos({0})") @MethodSource("trigonometricArguments") public void test_acos(Number value) { FunctionExpression acos = DSL.acos(DSL.literal(value)); assertThat( - acos.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.acos(value.doubleValue())))); + acos.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.acos(value.doubleValue())))); assertEquals(String.format("acos(%s)", value), acos.toString()); } - /** - * Test acos with illegal values. - */ + /** Test acos with illegal values. */ @ParameterizedTest(name = "acos({0})") @ValueSource(doubles = {2D, -2D}) public void acos_with_illegal_value(Number value) { @@ -1724,22 +1401,17 @@ public void acos_with_illegal_value(Number value) { assertTrue(acos.valueOf(valueEnv()).isNull()); } - /** - * Test asin with integer, long, float, double values. - */ + /** Test asin with integer, long, float, double values. */ @ParameterizedTest(name = "asin({0})") @MethodSource("trigonometricArguments") public void test_asin(Number value) { FunctionExpression asin = DSL.asin(DSL.literal(value)); assertThat( - asin.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.asin(value.doubleValue())))); + asin.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.asin(value.doubleValue())))); assertEquals(String.format("asin(%s)", value), asin.toString()); } - /** - * Test asin with illegal value. - */ + /** Test asin with illegal value. */ @ParameterizedTest(name = "asin({0})") @ValueSource(doubles = {2D, -2D}) public void asin_with_illegal_value(Number value) { @@ -1748,36 +1420,28 @@ public void asin_with_illegal_value(Number value) { assertTrue(asin.valueOf(valueEnv()).isNull()); } - /** - * Test atan with one argument integer, long, float, double values. - */ + /** Test atan with one argument integer, long, float, double values. */ @ParameterizedTest(name = "atan({0})") @MethodSource("trigonometricArguments") public void atan_one_arg(Number value) { FunctionExpression atan = DSL.atan(DSL.literal(value)); assertThat( - atan.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.atan(value.doubleValue())))); + atan.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.atan(value.doubleValue())))); assertEquals(String.format("atan(%s)", value), atan.toString()); } - /** - * Test atan with two arguments of integer, long, float, double values. - */ + /** Test atan with two arguments of integer, long, float, double values. */ @ParameterizedTest(name = "atan({0}, {1})") @MethodSource("trigonometricDoubleArguments") public void atan_two_args(Number v1, Number v2) { - FunctionExpression atan = - DSL.atan(DSL.literal(v1), DSL.literal(v2)); + FunctionExpression atan = DSL.atan(DSL.literal(v1), DSL.literal(v2)); assertThat( atan.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.atan2(v1.doubleValue(), v2.doubleValue())))); assertEquals(String.format("atan(%s, %s)", v1, v2), atan.toString()); } - /** - * Test atan2 with integer, long, float, double values. - */ + /** Test atan2 with integer, long, float, double values. */ @ParameterizedTest(name = "atan2({0}, {1})") @MethodSource("trigonometricDoubleArguments") public void test_atan2(Number v1, Number v2) { @@ -1788,22 +1452,17 @@ public void test_atan2(Number v1, Number v2) { assertEquals(String.format("atan2(%s, %s)", v1, v2), atan2.toString()); } - /** - * Test cos with integer, long, float, double values. - */ + /** Test cos with integer, long, float, double values. */ @ParameterizedTest(name = "cos({0})") @MethodSource("trigonometricArguments") public void test_cos(Number value) { FunctionExpression cos = DSL.cos(DSL.literal(value)); assertThat( - cos.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.cos(value.doubleValue())))); + cos.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.cos(value.doubleValue())))); assertEquals(String.format("cos(%s)", value), cos.toString()); } - /** - * Test cosh with byte value. - */ + /** Test cosh with byte value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(bytes = {-1, 1, 2}) public void cosh_byte_value(Byte value) { @@ -1812,9 +1471,7 @@ public void cosh_byte_value(Byte value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cosh with short value. - */ + /** Test cosh with short value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(shorts = {-1, 1, 2}) public void cosh_short_value(Short value) { @@ -1823,9 +1480,7 @@ public void cosh_short_value(Short value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cosh with integer value. - */ + /** Test cosh with integer value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(ints = {-1, 1, 2}) public void cosh_int_value(Integer value) { @@ -1834,9 +1489,7 @@ public void cosh_int_value(Integer value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cosh with long value. - */ + /** Test cosh with long value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(longs = {-1L, 1L, 2L}) public void cosh_long_value(Long value) { @@ -1845,9 +1498,7 @@ public void cosh_long_value(Long value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cosh with float value. - */ + /** Test cosh with float value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(floats = {-1F, 1F, 2F}) public void cosh_float_value(Float value) { @@ -1856,9 +1507,7 @@ public void cosh_float_value(Float value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cosh with double value. - */ + /** Test cosh with double value. */ @ParameterizedTest(name = "cosh({0})") @ValueSource(doubles = {-1D, 1D, 2D}) public void cosh_double_value(Double value) { @@ -1867,9 +1516,7 @@ public void cosh_double_value(Double value) { assertEquals(String.format("cosh(%s)", value), cosh.toString()); } - /** - * Test cot with integer, long, float, double values. - */ + /** Test cot with integer, long, float, double values. */ @ParameterizedTest(name = "cot({0})") @MethodSource("trigonometricArguments") public void test_cot(Number value) { @@ -1880,21 +1527,18 @@ public void test_cot(Number value) { assertEquals(String.format("cot(%s)", value), cot.toString()); } - /** - * Test cot with out-of-range value 0. - */ + /** Test cot with out-of-range value 0. */ @ParameterizedTest(name = "cot({0})") @ValueSource(doubles = {0}) public void cot_with_zero(Number value) { FunctionExpression cot = DSL.cot(DSL.literal(value)); assertThrows( - ArithmeticException.class, () -> cot.valueOf(valueEnv()), + ArithmeticException.class, + () -> cot.valueOf(valueEnv()), String.format("Out of range value for cot(%s)", value)); } - /** - * Test degrees with integer, long, float, double values. - */ + /** Test degrees with integer, long, float, double values. */ @ParameterizedTest(name = "degrees({0})") @MethodSource("trigonometricArguments") public void test_degrees(Number value) { @@ -1905,9 +1549,7 @@ public void test_degrees(Number value) { assertEquals(String.format("degrees(%s)", value), degrees.toString()); } - /** - * Test radians with integer, long, float, double values. - */ + /** Test radians with integer, long, float, double values. */ @ParameterizedTest(name = "radians({0})") @MethodSource("trigonometricArguments") public void test_radians(Number value) { @@ -1918,35 +1560,27 @@ public void test_radians(Number value) { assertEquals(String.format("radians(%s)", value), radians.toString()); } - /** - * Test sin with integer, long, float, double values. - */ + /** Test sin with integer, long, float, double values. */ @ParameterizedTest(name = "sin({0})") @MethodSource("trigonometricArguments") public void test_sin(Number value) { FunctionExpression sin = DSL.sin(DSL.literal(value)); assertThat( - sin.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.sin(value.doubleValue())))); + sin.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.sin(value.doubleValue())))); assertEquals(String.format("sin(%s)", value), sin.toString()); } - /** - * Test tan with integer, long, float, double values. - */ + /** Test tan with integer, long, float, double values. */ @ParameterizedTest(name = "tan({0})") @MethodSource("trigonometricArguments") public void test_tan(Number value) { FunctionExpression tan = DSL.tan(DSL.literal(value)); assertThat( - tan.valueOf(valueEnv()), - allOf(hasType(DOUBLE), hasValue(Math.tan(value.doubleValue())))); + tan.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.tan(value.doubleValue())))); assertEquals(String.format("tan(%s)", value), tan.toString()); } - /** - * Test cbrt with int value. - */ + /** Test cbrt with int value. */ @ParameterizedTest(name = "cbrt({0})") @ValueSource(ints = {1, 2}) public void cbrt_int_value(Integer value) { @@ -1955,9 +1589,7 @@ public void cbrt_int_value(Integer value) { assertEquals(String.format("cbrt(%s)", value), cbrt.toString()); } - /** - * Test cbrt with long value. - */ + /** Test cbrt with long value. */ @ParameterizedTest(name = "cbrt({0})") @ValueSource(longs = {1L, 2L}) public void cbrt_long_value(Long value) { @@ -1966,9 +1598,7 @@ public void cbrt_long_value(Long value) { assertEquals(String.format("cbrt(%s)", value), cbrt.toString()); } - /** - * Test cbrt with float value. - */ + /** Test cbrt with float value. */ @ParameterizedTest(name = "cbrt({0})") @ValueSource(floats = {1F, 2F}) public void cbrt_float_value(Float value) { @@ -1977,9 +1607,7 @@ public void cbrt_float_value(Float value) { assertEquals(String.format("cbrt(%s)", value), cbrt.toString()); } - /** - * Test cbrt with double value. - */ + /** Test cbrt with double value. */ @ParameterizedTest(name = "cbrt({0})") @ValueSource(doubles = {1D, 2D, Double.MAX_VALUE, Double.MIN_VALUE}) public void cbrt_double_value(Double value) { @@ -1988,9 +1616,7 @@ public void cbrt_double_value(Double value) { assertEquals(String.format("cbrt(%s)", value), cbrt.toString()); } - /** - * Test cbrt with negative value. - */ + /** Test cbrt with negative value. */ @ParameterizedTest(name = "cbrt({0})") @ValueSource(doubles = {-1D, -2D}) public void cbrt_negative_value(Double value) { diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/convert/TypeCastOperatorTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/convert/TypeCastOperatorTest.java index 742313922ac..7803a4dbca6 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/convert/TypeCastOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/convert/TypeCastOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.convert; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -44,8 +43,12 @@ class TypeCastOperatorTest { private static Stream numberData() { - return Stream.of(new ExprByteValue(3), new ExprShortValue(3), - new ExprIntegerValue(3), new ExprLongValue(3L), new ExprFloatValue(3.14f), + return Stream.of( + new ExprByteValue(3), + new ExprShortValue(3), + new ExprIntegerValue(3), + new ExprLongValue(3L), + new ExprFloatValue(3.14f), new ExprDoubleValue(3.1415D)); } @@ -357,5 +360,4 @@ void castToDatetime() { assertEquals(DATETIME, expression.type()); assertEquals(new ExprDatetimeValue("2012-08-07 00:00:00"), expression.valueOf()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperatorTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperatorTest.java index ad07dbc9755..e6290553cec 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.predicate; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -11,10 +10,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.opensearch.sql.config.TestConfig.BOOL_TYPE_MISSING_VALUE_FIELD; import static org.opensearch.sql.config.TestConfig.BOOL_TYPE_NULL_VALUE_FIELD; -import static org.opensearch.sql.config.TestConfig.INT_TYPE_MISSING_VALUE_FIELD; -import static org.opensearch.sql.config.TestConfig.INT_TYPE_NULL_VALUE_FIELD; -import static org.opensearch.sql.config.TestConfig.STRING_TYPE_MISSING_VALUE_FIELD; -import static org.opensearch.sql.config.TestConfig.STRING_TYPE_NULL_VALUE_FIELD; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_MISSING; import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; @@ -25,7 +20,6 @@ import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN; import static org.opensearch.sql.data.type.ExprCoreType.DATETIME; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; -import static org.opensearch.sql.data.type.ExprCoreType.STRING; import static org.opensearch.sql.data.type.ExprCoreType.STRUCT; import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP; import static org.opensearch.sql.utils.ComparisonUtil.compare; @@ -64,7 +58,8 @@ class BinaryPredicateOperatorTest extends ExpressionTestBase { - private static List STRING_PATTERN_PAIRS = ImmutableList.of( + private static List STRING_PATTERN_PAIRS = + ImmutableList.of( new StringPatternPair("Michael!", ".*"), new StringPatternPair("new*\\n*line", "new\\\\*.\\\\*line"), new StringPatternPair("a", "^[a-d]"), @@ -107,8 +102,7 @@ private static List> getValuesForComparisonTests() { List.of(Instant.ofEpochSecond(100500), LocalDate.of(1961, 4, 12)), List.of(Instant.ofEpochSecond(100500), LocalTime.of(7, 40, 0)), List.of(LocalTime.of(7, 40, 0), LocalDateTime.of(1984, 10, 25, 7, 40)), - List.of(Instant.ofEpochSecond(42), LocalDateTime.of(1984, 10, 25, 7, 40)) - ); + List.of(Instant.ofEpochSecond(42), LocalDateTime.of(1984, 10, 25, 7, 40))); } private static Stream testEqualArguments() { @@ -117,22 +111,33 @@ private static Stream testEqualArguments() { builder.add(Arguments.of(fromObjectValue(argPair.get(0)), fromObjectValue(argPair.get(0)))); builder.add(Arguments.of(fromObjectValue(argPair.get(1)), fromObjectValue(argPair.get(1)))); } - builder.add(Arguments.of(fromObjectValue(LocalTime.of(7, 40, 0)), - fromObjectValue(LocalTime.of(7, 40, 0).atDate(LocalDate.now())))); - builder.add(Arguments.of(fromObjectValue(LocalDateTime.of(1970, 1, 1, 0, 0, 42)), - fromObjectValue(Instant.ofEpochSecond(42)))); - builder.add(Arguments.of(fromObjectValue(LocalDate.of(1970, 1, 1)), - fromObjectValue(Instant.ofEpochSecond(0)))); - builder.add(Arguments.of(fromObjectValue(LocalDate.of(1984, 10, 25)), - fromObjectValue(LocalDateTime.of(1984, 10, 25, 0, 0)))); - builder.add(Arguments.of(fromObjectValue(LocalTime.of(0, 0, 0)), - fromObjectValue(LocalDate.now()))); - builder.add(Arguments.of(fromObjectValue(LocalTime.of(0, 0, 0)), - fromObjectValue(LocalDate.now().atStartOfDay(ZoneId.of("UTC")).toInstant()))); - builder.add(Arguments.of(fromObjectValue(ImmutableList.of(1)), - fromObjectValue(ImmutableList.of(1)))); - builder.add(Arguments.of(fromObjectValue(ImmutableMap.of("str", 1)), - fromObjectValue(ImmutableMap.of("str", 1)))); + builder.add( + Arguments.of( + fromObjectValue(LocalTime.of(7, 40, 0)), + fromObjectValue(LocalTime.of(7, 40, 0).atDate(LocalDate.now())))); + builder.add( + Arguments.of( + fromObjectValue(LocalDateTime.of(1970, 1, 1, 0, 0, 42)), + fromObjectValue(Instant.ofEpochSecond(42)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDate.of(1970, 1, 1)), fromObjectValue(Instant.ofEpochSecond(0)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDate.of(1984, 10, 25)), + fromObjectValue(LocalDateTime.of(1984, 10, 25, 0, 0)))); + builder.add( + Arguments.of(fromObjectValue(LocalTime.of(0, 0, 0)), fromObjectValue(LocalDate.now()))); + builder.add( + Arguments.of( + fromObjectValue(LocalTime.of(0, 0, 0)), + fromObjectValue(LocalDate.now().atStartOfDay(ZoneId.of("UTC")).toInstant()))); + builder.add( + Arguments.of(fromObjectValue(ImmutableList.of(1)), fromObjectValue(ImmutableList.of(1)))); + builder.add( + Arguments.of( + fromObjectValue(ImmutableMap.of("str", 1)), + fromObjectValue(ImmutableMap.of("str", 1)))); return builder.build(); } @@ -142,24 +147,37 @@ private static Stream testNotEqualArguments() { builder.add(Arguments.of(fromObjectValue(argPair.get(0)), fromObjectValue(argPair.get(1)))); builder.add(Arguments.of(fromObjectValue(argPair.get(1)), fromObjectValue(argPair.get(0)))); } - builder.add(Arguments.of(fromObjectValue(LocalTime.of(7, 40, 0)), - fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40, 0)))); - builder.add(Arguments.of(fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40, 0)), - fromObjectValue(Instant.ofEpochSecond(42)))); - builder.add(Arguments.of(fromObjectValue(LocalDate.of(1984, 10, 25)), - fromObjectValue(Instant.ofEpochSecond(42)))); - builder.add(Arguments.of(fromObjectValue(LocalTime.of(7, 40, 0)), - fromObjectValue(Instant.ofEpochSecond(42)))); - builder.add(Arguments.of(fromObjectValue(LocalDate.of(1984, 10, 25)), - fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40)))); - builder.add(Arguments.of(fromObjectValue(LocalDate.of(1984, 10, 25)), - fromObjectValue(LocalTime.of(7, 40, 0)))); - builder.add(Arguments.of(fromObjectValue(ImmutableList.of(1)), - fromObjectValue(ImmutableList.of(1, 2)))); - builder.add(Arguments.of(fromObjectValue(ImmutableList.of(1)), - fromObjectValue(ImmutableList.of(2)))); - builder.add(Arguments.of(fromObjectValue(ImmutableMap.of("str", 1)), - fromObjectValue(ImmutableMap.of("str2", 2)))); + builder.add( + Arguments.of( + fromObjectValue(LocalTime.of(7, 40, 0)), + fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40, 0)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40, 0)), + fromObjectValue(Instant.ofEpochSecond(42)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDate.of(1984, 10, 25)), + fromObjectValue(Instant.ofEpochSecond(42)))); + builder.add( + Arguments.of( + fromObjectValue(LocalTime.of(7, 40, 0)), fromObjectValue(Instant.ofEpochSecond(42)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDate.of(1984, 10, 25)), + fromObjectValue(LocalDateTime.of(1984, 10, 25, 7, 40)))); + builder.add( + Arguments.of( + fromObjectValue(LocalDate.of(1984, 10, 25)), fromObjectValue(LocalTime.of(7, 40, 0)))); + builder.add( + Arguments.of( + fromObjectValue(ImmutableList.of(1)), fromObjectValue(ImmutableList.of(1, 2)))); + builder.add( + Arguments.of(fromObjectValue(ImmutableList.of(1)), fromObjectValue(ImmutableList.of(2)))); + builder.add( + Arguments.of( + fromObjectValue(ImmutableMap.of("str", 1)), + fromObjectValue(ImmutableMap.of("str2", 2)))); return builder.build(); } @@ -174,13 +192,17 @@ private static Stream testCompareValueArguments() { } private static Stream testLikeArguments() { - List> arguments = Arrays.asList( - Arrays.asList("foo", "foo"), Arrays.asList("notFoo", "foo"), - Arrays.asList("foobar", "%bar"), Arrays.asList("bar", "%bar"), - Arrays.asList("foo", "fo_"), Arrays.asList("foo", "foo_"), - Arrays.asList("foorbar", "%o_ar"), Arrays.asList("foobar", "%o_a%"), - Arrays.asList("fooba%_\\^$.*[]()|+r", "%\\%\\_\\\\\\^\\$\\.\\*\\[\\]\\(\\)\\|\\+_") - ); + List> arguments = + Arrays.asList( + Arrays.asList("foo", "foo"), + Arrays.asList("notFoo", "foo"), + Arrays.asList("foobar", "%bar"), + Arrays.asList("bar", "%bar"), + Arrays.asList("foo", "fo_"), + Arrays.asList("foo", "foo_"), + Arrays.asList("foorbar", "%o_ar"), + Arrays.asList("foobar", "%o_a%"), + Arrays.asList("fooba%_\\^$.*[]()|+r", "%\\%\\_\\\\\\^\\$\\.\\*\\[\\]\\(\\)\\|\\+_")); Stream.Builder builder = Stream.builder(); for (List argPair : arguments) { builder.add(Arguments.of(fromObjectValue(argPair.get(0)), fromObjectValue(argPair.get(1)))); @@ -191,8 +213,7 @@ private static Stream testLikeArguments() { @ParameterizedTest(name = "and({0}, {1})") @MethodSource("binaryPredicateArguments") public void test_and(Boolean v1, Boolean v2) { - FunctionExpression and = - DSL.and(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); + FunctionExpression and = DSL.and(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); assertEquals(BOOLEAN, and.type()); assertEquals(v1 && v2, ExprValueUtils.getBooleanValue(and.valueOf(valueEnv()))); assertEquals(String.format("and(%s, %s)", v1.toString(), v2.toString()), and.toString()); @@ -240,23 +261,31 @@ public void test_boolean_and_missing() { @Test public void test_null_and_missing() { - FunctionExpression and = DSL.and(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + FunctionExpression and = + DSL.and( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, and.type()); assertEquals(LITERAL_MISSING, and.valueOf(valueEnv())); - and = DSL.and(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); + and = + DSL.and( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, and.type()); assertEquals(LITERAL_NULL, and.valueOf(valueEnv())); - and = DSL.and(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + and = + DSL.and( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, and.type()); assertEquals(LITERAL_MISSING, and.valueOf(valueEnv())); - and = DSL.and(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); + and = + DSL.and( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, and.type()); assertEquals(LITERAL_MISSING, and.valueOf(valueEnv())); } @@ -264,8 +293,7 @@ public void test_null_and_missing() { @ParameterizedTest(name = "or({0}, {1})") @MethodSource("binaryPredicateArguments") public void test_or(Boolean v1, Boolean v2) { - FunctionExpression or = - DSL.or(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); + FunctionExpression or = DSL.or(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); assertEquals(BOOLEAN, or.type()); assertEquals(v1 || v2, ExprValueUtils.getBooleanValue(or.valueOf(valueEnv()))); assertEquals(String.format("or(%s, %s)", v1.toString(), v2.toString()), or.toString()); @@ -313,34 +341,39 @@ public void test_boolean_or_missing() { @Test public void test_null_or_missing() { - FunctionExpression or = DSL.or(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + FunctionExpression or = + DSL.or( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, or.type()); assertEquals(LITERAL_MISSING, or.valueOf(valueEnv())); or = - DSL.or(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.or( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, or.type()); assertEquals(LITERAL_NULL, or.valueOf(valueEnv())); - or = DSL.or(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + or = + DSL.or( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, or.type()); assertEquals(LITERAL_NULL, or.valueOf(valueEnv())); - or = DSL.or(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); + or = + DSL.or( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, or.type()); assertEquals(LITERAL_NULL, or.valueOf(valueEnv())); } - @ParameterizedTest(name = "xor({0}, {1})") @MethodSource("binaryPredicateArguments") public void test_xor(Boolean v1, Boolean v2) { - FunctionExpression xor = - DSL.xor(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); + FunctionExpression xor = DSL.xor(DSL.literal(booleanValue(v1)), DSL.literal(booleanValue(v2))); assertEquals(BOOLEAN, xor.type()); assertEquals(v1 ^ v2, ExprValueUtils.getBooleanValue(xor.valueOf(valueEnv()))); assertEquals(String.format("xor(%s, %s)", v1.toString(), v2.toString()), xor.toString()); @@ -388,23 +421,31 @@ public void test_boolean_xor_missing() { @Test public void test_null_xor_missing() { - FunctionExpression xor = DSL.xor(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + FunctionExpression xor = + DSL.xor( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, xor.type()); assertEquals(LITERAL_MISSING, xor.valueOf(valueEnv())); - xor = DSL.xor(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); + xor = + DSL.xor( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, xor.type()); assertEquals(LITERAL_NULL, xor.valueOf(valueEnv())); - xor = DSL.xor(DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); + xor = + DSL.xor( + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, xor.type()); assertEquals(LITERAL_NULL, xor.valueOf(valueEnv())); - xor = DSL.xor(DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), - DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); + xor = + DSL.xor( + DSL.ref(BOOL_TYPE_MISSING_VALUE_FIELD, BOOLEAN), + DSL.ref(BOOL_TYPE_NULL_VALUE_FIELD, BOOLEAN)); assertEquals(BOOLEAN, xor.type()); assertEquals(LITERAL_NULL, xor.valueOf(valueEnv())); } @@ -415,24 +456,28 @@ public void test_equal(ExprValue v1, ExprValue v2) { FunctionExpression equal = DSL.equal(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, equal.type()); if (v1.type() == v2.type()) { - assertEquals(v1.value().equals(v2.value()), - ExprValueUtils.getBooleanValue(equal.valueOf(valueEnv()))); + assertEquals( + v1.value().equals(v2.value()), ExprValueUtils.getBooleanValue(equal.valueOf(valueEnv()))); } if (v1.type() != STRUCT && v1.type() != ARRAY) { - assertEquals(0 == compare(functionProperties, v1, v2), + assertEquals( + 0 == compare(functionProperties, v1, v2), ExprValueUtils.getBooleanValue(equal.valueOf(valueEnv()))); } assertStringRepr(v1, v2, "=", equal); } - private void assertStringRepr(ExprValue v1, ExprValue v2, String function, - FunctionExpression functionExpression) { + private void assertStringRepr( + ExprValue v1, ExprValue v2, String function, FunctionExpression functionExpression) { if (v1.type() == v2.type()) { assertEquals(String.format("%s(%s, %s)", function, v1, v2), functionExpression.toString()); } else { var widerType = v1.type() == TIMESTAMP || v2.type() == TIMESTAMP ? TIMESTAMP : DATETIME; - assertEquals(String.format("%s(%s, %s)", function, getExpectedStringRepr(widerType, v1), - getExpectedStringRepr(widerType, v2)), functionExpression.toString()); + assertEquals( + String.format( + "%s(%s, %s)", + function, getExpectedStringRepr(widerType, v1), getExpectedStringRepr(widerType, v2)), + functionExpression.toString()); } } @@ -446,15 +491,17 @@ private String getExpectedStringRepr(ExprType widerType, ExprValue value) { @ParameterizedTest(name = "equal({0}, {1})") @MethodSource({"testEqualArguments", "testNotEqualArguments"}) public void test_notequal(ExprValue v1, ExprValue v2) { - FunctionExpression notequal = DSL.notequal(functionProperties, - DSL.literal(v1), DSL.literal(v2)); + FunctionExpression notequal = + DSL.notequal(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, notequal.type()); if (v1.type() == v2.type()) { - assertEquals(!v1.value().equals(v2.value()), + assertEquals( + !v1.value().equals(v2.value()), ExprValueUtils.getBooleanValue(notequal.valueOf(valueEnv()))); } if (v1.type() != STRUCT && v1.type() != ARRAY) { - assertEquals(0 != compare(functionProperties, v1, v2), + assertEquals( + 0 != compare(functionProperties, v1, v2), ExprValueUtils.getBooleanValue(notequal.valueOf(valueEnv()))); } assertStringRepr(v1, v2, "!=", notequal); @@ -465,7 +512,8 @@ public void test_notequal(ExprValue v1, ExprValue v2) { public void test_less(ExprValue v1, ExprValue v2) { FunctionExpression less = DSL.less(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, less.type()); - assertEquals(compare(functionProperties, v1, v2) < 0, + assertEquals( + compare(functionProperties, v1, v2) < 0, ExprValueUtils.getBooleanValue(less.valueOf(valueEnv()))); assertStringRepr(v1, v2, "<", less); } @@ -475,7 +523,8 @@ public void test_less(ExprValue v1, ExprValue v2) { public void test_lte(ExprValue v1, ExprValue v2) { FunctionExpression lte = DSL.lte(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, lte.type()); - assertEquals(compare(functionProperties, v1, v2) <= 0, + assertEquals( + compare(functionProperties, v1, v2) <= 0, ExprValueUtils.getBooleanValue(lte.valueOf(valueEnv()))); assertStringRepr(v1, v2, "<=", lte); } @@ -485,7 +534,8 @@ public void test_lte(ExprValue v1, ExprValue v2) { public void test_greater(ExprValue v1, ExprValue v2) { FunctionExpression greater = DSL.greater(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, greater.type()); - assertEquals(compare(functionProperties, v1, v2) > 0, + assertEquals( + compare(functionProperties, v1, v2) > 0, ExprValueUtils.getBooleanValue(greater.valueOf(valueEnv()))); assertStringRepr(v1, v2, ">", greater); } @@ -495,7 +545,8 @@ public void test_greater(ExprValue v1, ExprValue v2) { public void test_gte(ExprValue v1, ExprValue v2) { FunctionExpression gte = DSL.gte(functionProperties, DSL.literal(v1), DSL.literal(v2)); assertEquals(BOOLEAN, gte.type()); - assertEquals(compare(functionProperties, v1, v2) >= 0, + assertEquals( + compare(functionProperties, v1, v2) >= 0, ExprValueUtils.getBooleanValue(gte.valueOf(valueEnv()))); assertStringRepr(v1, v2, ">=", gte); } @@ -527,17 +578,15 @@ void test_regexp() { } void testRegexpString(StringPatternPair stringPatternPair) { - FunctionExpression expression = DSL.regexp( + FunctionExpression expression = + DSL.regexp( DSL.literal(new ExprStringValue(stringPatternPair.getStr())), DSL.literal(new ExprStringValue(stringPatternPair.getPatt()))); assertEquals(INTEGER, expression.type()); - assertEquals(stringPatternPair.regExpTest(), expression - .valueOf(valueEnv()).integerValue()); + assertEquals(stringPatternPair.regExpTest(), expression.valueOf(valueEnv()).integerValue()); } - /** - * Todo. remove this test cases after script serilization implemented. - */ + /** Todo. remove this test cases after script serilization implemented. */ @Test public void serializationTest() throws Exception { Expression expression = DSL.equal(DSL.literal("v1"), DSL.literal("v2")); diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperatorTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperatorTest.java index de3f5a3e7e3..f7a1a7008af 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/predicate/UnaryPredicateOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.operator.predicate; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -50,15 +49,15 @@ private static Stream isNullArguments() { expressions.add(DSL.literal(LITERAL_NULL)); return Lists.cartesianProduct(expressions, expressions).stream() - .map(list -> { - Expression e1 = list.get(0); - if (e1.valueOf(valueEnv()).isNull() - || e1.valueOf(valueEnv()).isMissing()) { - return Arguments.of(e1, DSL.literal(LITERAL_TRUE)); - } else { - return Arguments.of(e1, DSL.literal(LITERAL_FALSE)); - } - }); + .map( + list -> { + Expression e1 = list.get(0); + if (e1.valueOf(valueEnv()).isNull() || e1.valueOf(valueEnv()).isMissing()) { + return Arguments.of(e1, DSL.literal(LITERAL_TRUE)); + } else { + return Arguments.of(e1, DSL.literal(LITERAL_FALSE)); + } + }); } private static Stream ifNullArguments() { @@ -69,16 +68,17 @@ private static Stream ifNullArguments() { exprValueArrayList.add(DSL.literal(LITERAL_NULL)); return Lists.cartesianProduct(exprValueArrayList, exprValueArrayList).stream() - .map(list -> { - Expression e1 = list.get(0); - Expression e2 = list.get(1); - if (e1.valueOf(valueEnv()).value() == LITERAL_NULL.value() + .map( + list -> { + Expression e1 = list.get(0); + Expression e2 = list.get(1); + if (e1.valueOf(valueEnv()).value() == LITERAL_NULL.value() || e1.valueOf(valueEnv()).value() == LITERAL_MISSING) { - return Arguments.of(e1, e2, e2); - } else { - return Arguments.of(e1, e2, e1); - } - }); + return Arguments.of(e1, e2, e2); + } else { + return Arguments.of(e1, e2, e1); + } + }); } private static Stream nullIfArguments() { @@ -87,16 +87,17 @@ private static Stream nullIfArguments() { exprValueArrayList.add(DSL.literal(321)); return Lists.cartesianProduct(exprValueArrayList, exprValueArrayList).stream() - .map(list -> { - Expression e1 = list.get(0); - Expression e2 = list.get(1); + .map( + list -> { + Expression e1 = list.get(0); + Expression e2 = list.get(1); - if (e1.equals(e2)) { - return Arguments.of(e1, e2, DSL.literal(LITERAL_NULL)); - } else { - return Arguments.of(e1, e2, e1); - } - }); + if (e1.equals(e2)) { + return Arguments.of(e1, e2, DSL.literal(LITERAL_NULL)); + } else { + return Arguments.of(e1, e2, e1); + } + }); } private static Stream ifArguments() { @@ -107,14 +108,15 @@ private static Stream ifArguments() { exprValueArrayList.add(DSL.literal(LITERAL_MISSING)); return Lists.cartesianProduct(exprValueArrayList, exprValueArrayList).stream() - .map(list -> { - Expression e1 = list.get(0); - if (e1.valueOf(valueEnv()).value() == LITERAL_TRUE.value()) { - return Arguments.of(e1, DSL.literal("123"), DSL.literal("321"), DSL.literal("123")); - } else { - return Arguments.of(e1, DSL.literal("123"), DSL.literal("321"), DSL.literal("321")); - } - }); + .map( + list -> { + Expression e1 = list.get(0); + if (e1.valueOf(valueEnv()).value() == LITERAL_TRUE.value()) { + return Arguments.of(e1, DSL.literal("123"), DSL.literal("321"), DSL.literal("123")); + } else { + return Arguments.of(e1, DSL.literal("123"), DSL.literal("321"), DSL.literal("321")); + } + }); } private static Stream exprIfNullArguments() { @@ -125,15 +127,16 @@ private static Stream exprIfNullArguments() { exprValues.add(ExprValueUtils.integerValue(456)); return Lists.cartesianProduct(exprValues, exprValues).stream() - .map(list -> { - ExprValue e1 = list.get(0); - ExprValue e2 = list.get(1); - if (e1.isNull() || e1.isMissing()) { - return Arguments.of(e1, e2, e2); - } else { - return Arguments.of(e1, e2, e1); - } - }); + .map( + list -> { + ExprValue e1 = list.get(0); + ExprValue e2 = list.get(1); + if (e1.isNull() || e1.isMissing()) { + return Arguments.of(e1, e2, e2); + } else { + return Arguments.of(e1, e2, e1); + } + }); } private static Stream exprNullIfArguments() { @@ -143,15 +146,16 @@ private static Stream exprNullIfArguments() { exprValues.add(ExprValueUtils.integerValue(123)); return Lists.cartesianProduct(exprValues, exprValues).stream() - .map(list -> { - ExprValue e1 = list.get(0); - ExprValue e2 = list.get(1); - if (e1.equals(e2)) { - return Arguments.of(e1, e2, LITERAL_NULL); - } else { - return Arguments.of(e1, e2, e1); - } - }); + .map( + list -> { + ExprValue e1 = list.get(0); + ExprValue e2 = list.get(1); + if (e1.equals(e2)) { + return Arguments.of(e1, e2, LITERAL_NULL); + } else { + return Arguments.of(e1, e2, e1); + } + }); } @Test @@ -225,5 +229,4 @@ public void test_exprIfNull_predicate(ExprValue v1, ExprValue v2, ExprValue expe public void test_exprNullIf_predicate(ExprValue v1, ExprValue v2, ExprValue expected) { assertEquals(expected.value(), UnaryPredicateOperator.exprNullIf(v1, v2).value()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/parse/GrokExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/parse/GrokExpressionTest.java index 28bd3467fd8..fec851c1ed5 100644 --- a/core/src/test/java/org/opensearch/sql/expression/parse/GrokExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/parse/GrokExpressionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.parse; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,70 +36,100 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class GrokExpressionTest extends ExpressionTestBase { - @Mock - Environment env; + @Mock Environment env; @Test public void resolve_grok_groups_and_parsed_values() { - when(DSL.ref("log_value", STRING).valueOf(env)).thenReturn(stringValue( - "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] \"GET /deliverables HTTP/2.0\" 501 2721")); + when(DSL.ref("log_value", STRING).valueOf(env)) + .thenReturn( + stringValue( + "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] \"GET /deliverables HTTP/2.0\" 501" + + " 2721")); String rawPattern = "%{COMMONAPACHELOG}"; - Map expected = ImmutableMap.builder() - .put("COMMONAPACHELOG", "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] " - + "\"GET /deliverables HTTP/2.0\" 501 2721") - .put("clientip", "145.128.75.121") - .put("ident", "-") - .put("auth", "-") - .put("timestamp", "29/Aug/2022:13:26:44 -0700") - .put("MONTHDAY", "29") - .put("MONTH", "Aug") - .put("YEAR", "2022") - .put("TIME", "13:26:44") - .put("HOUR", "13") - .put("MINUTE", "26") - .put("SECOND", "44") - .put("INT", "-0700") - .put("verb", "GET") - .put("request", "/deliverables") - .put("httpversion", "2.0") - .put("rawrequest", "") - .put("response", "501") - .put("bytes", "2721") - .build(); + Map expected = + ImmutableMap.builder() + .put( + "COMMONAPACHELOG", + "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] " + + "\"GET /deliverables HTTP/2.0\" 501 2721") + .put("clientip", "145.128.75.121") + .put("ident", "-") + .put("auth", "-") + .put("timestamp", "29/Aug/2022:13:26:44 -0700") + .put("MONTHDAY", "29") + .put("MONTH", "Aug") + .put("YEAR", "2022") + .put("TIME", "13:26:44") + .put("HOUR", "13") + .put("MINUTE", "26") + .put("SECOND", "44") + .put("INT", "-0700") + .put("verb", "GET") + .put("request", "/deliverables") + .put("httpversion", "2.0") + .put("rawrequest", "") + .put("response", "501") + .put("bytes", "2721") + .build(); List identifiers = new ArrayList<>(expected.keySet()); assertEquals(identifiers, GrokExpression.getNamedGroupCandidates(rawPattern)); - identifiers.forEach(identifier -> assertEquals(stringValue(expected.get(identifier)), - DSL.grok(DSL.ref("log_value", STRING), DSL.literal(rawPattern), DSL.literal(identifier)) - .valueOf(env))); + identifiers.forEach( + identifier -> + assertEquals( + stringValue(expected.get(identifier)), + DSL.grok( + DSL.ref("log_value", STRING), + DSL.literal(rawPattern), + DSL.literal(identifier)) + .valueOf(env))); } @Test public void resolve_null_and_empty_values() { - assertEquals(stringValue(""), - DSL.grok(DSL.ref("string_value", STRING), DSL.literal("%{COMMONAPACHELOG}"), - DSL.literal("request")).valueOf(valueEnv())); - assertEquals(LITERAL_NULL, - DSL.grok(DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), - DSL.literal("%{COMMONAPACHELOG}"), DSL.literal("request")).valueOf(valueEnv())); - assertEquals(LITERAL_NULL, - DSL.grok(DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), - DSL.literal("p%{COMMONAPACHELOG}"), DSL.literal("request")).valueOf(valueEnv())); + assertEquals( + stringValue(""), + DSL.grok( + DSL.ref("string_value", STRING), + DSL.literal("%{COMMONAPACHELOG}"), + DSL.literal("request")) + .valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.grok( + DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), + DSL.literal("%{COMMONAPACHELOG}"), + DSL.literal("request")) + .valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.grok( + DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), + DSL.literal("p%{COMMONAPACHELOG}"), + DSL.literal("request")) + .valueOf(valueEnv())); } @Test public void resolve_type() { - assertEquals(STRING, - DSL.grok(DSL.ref("string_value", STRING), DSL.literal("%{COMMONAPACHELOG}"), - DSL.literal("request")).type()); + assertEquals( + STRING, + DSL.grok( + DSL.ref("string_value", STRING), + DSL.literal("%{COMMONAPACHELOG}"), + DSL.literal("request")) + .type()); } @Test public void throws_semantic_exception_if_value_type_is_not_string() { assertThrows( SemanticCheckException.class, - () -> DSL.grok(DSL.ref("boolean_value", BOOLEAN), DSL.literal("%{COMMONAPACHELOG}"), - DSL.literal("request")) - .valueOf(valueEnv())); + () -> + DSL.grok( + DSL.ref("boolean_value", BOOLEAN), + DSL.literal("%{COMMONAPACHELOG}"), + DSL.literal("request")) + .valueOf(valueEnv())); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/parse/PatternsExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/parse/PatternsExpressionTest.java index 1593e94a8a5..7237f0673b4 100644 --- a/core/src/test/java/org/opensearch/sql/expression/parse/PatternsExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/parse/PatternsExpressionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.parse; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -33,46 +32,58 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class PatternsExpressionTest extends ExpressionTestBase { - @Mock - Environment env; + @Mock Environment env; @Test public void resolve_value() { - when(DSL.ref("log_value", STRING).valueOf(env)).thenReturn(stringValue( - "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] \"GET /deliverables HTTP/2.0\" 501 2721")); - assertEquals(stringValue("... - - [//::: -] \" / /.\" "), - DSL.patterns(DSL.ref("log_value", STRING), DSL.literal(""), - DSL.literal("punct_field")).valueOf(env)); - assertEquals(stringValue("... - - [/Aug/::: -] \"GET /deliverables HTTP/.\" "), - DSL.patterns(DSL.ref("log_value", STRING), DSL.literal("[0-9]"), - DSL.literal("regex_field")).valueOf(env)); + when(DSL.ref("log_value", STRING).valueOf(env)) + .thenReturn( + stringValue( + "145.128.75.121 - - [29/Aug/2022:13:26:44 -0700] \"GET /deliverables HTTP/2.0\" 501" + + " 2721")); + assertEquals( + stringValue("... - - [//::: -] \" / /.\" "), + DSL.patterns(DSL.ref("log_value", STRING), DSL.literal(""), DSL.literal("punct_field")) + .valueOf(env)); + assertEquals( + stringValue("... - - [/Aug/::: -] \"GET /deliverables HTTP/.\" "), + DSL.patterns(DSL.ref("log_value", STRING), DSL.literal("[0-9]"), DSL.literal("regex_field")) + .valueOf(env)); } @Test public void resolve_null_and_missing_values() { - assertEquals(LITERAL_NULL, - DSL.patterns(DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), - DSL.literal("pattern"), DSL.literal("patterns_field")).valueOf(valueEnv())); - assertEquals(LITERAL_NULL, - DSL.patterns(DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), - DSL.literal("pattern"), DSL.literal("patterns_field")).valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.patterns( + DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), + DSL.literal("pattern"), + DSL.literal("patterns_field")) + .valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.patterns( + DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), + DSL.literal("pattern"), + DSL.literal("patterns_field")) + .valueOf(valueEnv())); } @Test public void resolve_type() { - assertEquals(STRING, - DSL.patterns(DSL.ref("string_value", STRING), - DSL.literal("pattern"), - DSL.literal("group")).type()); + assertEquals( + STRING, + DSL.patterns(DSL.ref("string_value", STRING), DSL.literal("pattern"), DSL.literal("group")) + .type()); } @Test public void throws_semantic_exception_if_value_type_is_not_string() { assertThrows( SemanticCheckException.class, - () -> DSL.patterns(DSL.ref("boolean_value", BOOLEAN), - DSL.literal("pattern"), - DSL.literal("group")) - .valueOf(valueEnv())); + () -> + DSL.patterns( + DSL.ref("boolean_value", BOOLEAN), DSL.literal("pattern"), DSL.literal("group")) + .valueOf(valueEnv())); } } diff --git a/core/src/test/java/org/opensearch/sql/expression/parse/RegexExpressionTest.java b/core/src/test/java/org/opensearch/sql/expression/parse/RegexExpressionTest.java index 95d836042ec..846aff69117 100644 --- a/core/src/test/java/org/opensearch/sql/expression/parse/RegexExpressionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/parse/RegexExpressionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.parse; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,62 +36,97 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class RegexExpressionTest extends ExpressionTestBase { - @Mock - Environment env; + @Mock Environment env; @Test public void resolve_regex_groups_and_parsed_values() { - when(DSL.ref("log_value", STRING).valueOf(env)).thenReturn(stringValue( - "130.246.123.197 - - [2018-07-22T03:26:21.326Z] \"GET /beats/metricbeat_1 HTTP/1.1\" " - + "200 6850 \"-\" \"Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 " - + "Firefox/6.0a1\"")); + when(DSL.ref("log_value", STRING).valueOf(env)) + .thenReturn( + stringValue( + "130.246.123.197 - - [2018-07-22T03:26:21.326Z] \"GET /beats/metricbeat_1" + + " HTTP/1.1\" 200 6850 \"-\" \"Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1)" + + " Gecko/20110421 Firefox/6.0a1\"")); String rawPattern = "(?(\\d{1,3}\\.){3}\\d{1,3}) - - \\[(?\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:" + "[0-5]\\d:[0-5]\\d\\.\\d+([+-][0-2]\\d:[0-5]\\d|Z))] \"(?[^\"]+)\" " + "(?\\d+) (?\\d+) \"-\" \"(?[^\"]+)\""; Map expected = - ImmutableMap.of("ip", "130.246.123.197", "date", "2018-07-22T03:26:21.326Z", "request", - "GET /beats/metricbeat_1 HTTP/1.1", "status", "200", "bytes", "6850", "userAgent", + ImmutableMap.of( + "ip", + "130.246.123.197", + "date", + "2018-07-22T03:26:21.326Z", + "request", + "GET /beats/metricbeat_1 HTTP/1.1", + "status", + "200", + "bytes", + "6850", + "userAgent", "Mozilla/5.0 (X11; Linux x86_64; rv:6.0a1) Gecko/20110421 Firefox/6.0a1"); List identifiers = new ArrayList<>(expected.keySet()); assertEquals(identifiers, RegexExpression.getNamedGroupCandidates(rawPattern)); - identifiers.forEach(identifier -> assertEquals(stringValue(expected.get(identifier)), - DSL.regex(DSL.ref("log_value", STRING), DSL.literal(rawPattern), DSL.literal(identifier)) - .valueOf(env))); + identifiers.forEach( + identifier -> + assertEquals( + stringValue(expected.get(identifier)), + DSL.regex( + DSL.ref("log_value", STRING), + DSL.literal(rawPattern), + DSL.literal(identifier)) + .valueOf(env))); } @Test public void resolve_not_parsable_inputs_as_empty_string() { - assertEquals(stringValue(""), - DSL.regex(DSL.ref("string_value", STRING), DSL.literal("(?not-matching)"), - DSL.literal("group")).valueOf(valueEnv())); + assertEquals( + stringValue(""), + DSL.regex( + DSL.ref("string_value", STRING), + DSL.literal("(?not-matching)"), + DSL.literal("group")) + .valueOf(valueEnv())); } @Test public void resolve_null_and_missing_values() { - assertEquals(LITERAL_NULL, - DSL.regex(DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), - DSL.literal("(?\\w{2})\\w"), DSL.literal("group")).valueOf(valueEnv())); - assertEquals(LITERAL_NULL, - DSL.regex(DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), - DSL.literal("(?\\w{2})\\w"), DSL.literal("group")).valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.regex( + DSL.ref(STRING_TYPE_NULL_VALUE_FIELD, STRING), + DSL.literal("(?\\w{2})\\w"), + DSL.literal("group")) + .valueOf(valueEnv())); + assertEquals( + LITERAL_NULL, + DSL.regex( + DSL.ref(STRING_TYPE_MISSING_VALUE_FIELD, STRING), + DSL.literal("(?\\w{2})\\w"), + DSL.literal("group")) + .valueOf(valueEnv())); } @Test public void resolve_type() { - assertEquals(STRING, - DSL.regex(DSL.ref("string_value", STRING), DSL.literal("(?\\w{2})\\w"), - DSL.literal("group")).type()); + assertEquals( + STRING, + DSL.regex( + DSL.ref("string_value", STRING), + DSL.literal("(?\\w{2})\\w"), + DSL.literal("group")) + .type()); } @Test public void throws_semantic_exception_if_value_type_is_not_string() { assertThrows( SemanticCheckException.class, - () -> DSL.regex(DSL.ref("boolean_value", BOOLEAN), DSL.literal("(?\\w{2})\\w"), - DSL.literal("group")) - .valueOf(valueEnv())); + () -> + DSL.regex( + DSL.ref("boolean_value", BOOLEAN), + DSL.literal("(?\\w{2})\\w"), + DSL.literal("group")) + .valueOf(valueEnv())); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/system/SystemFunctionsTest.java b/core/src/test/java/org/opensearch/sql/expression/system/SystemFunctionsTest.java index 62d219e5767..ac4153f59f3 100644 --- a/core/src/test/java/org/opensearch/sql/expression/system/SystemFunctionsTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/system/SystemFunctionsTest.java @@ -62,27 +62,30 @@ void typeof() { assertEquals("TIMESTAMP", typeofGetValue(new ExprTimestampValue(Instant.now()))); assertEquals("UNDEFINED", typeofGetValue(ExprNullValue.of())); assertEquals("UNDEFINED", typeofGetValue(ExprMissingValue.of())); - assertEquals("UNKNOWN", typeofGetValue(new AbstractExprValue() { - @Override - public int compare(ExprValue other) { - return 0; - } + assertEquals( + "UNKNOWN", + typeofGetValue( + new AbstractExprValue() { + @Override + public int compare(ExprValue other) { + return 0; + } - @Override - public boolean equal(ExprValue other) { - return false; - } + @Override + public boolean equal(ExprValue other) { + return false; + } - @Override - public Object value() { - return null; - } + @Override + public Object value() { + return null; + } - @Override - public ExprType type() { - return ExprCoreType.UNKNOWN; - } - })); + @Override + public ExprType type() { + return ExprCoreType.UNKNOWN; + } + })); } private String typeofGetValue(ExprValue input) { diff --git a/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java index 54d2e5c400b..84ae0b844fe 100644 --- a/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.text; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -36,45 +35,44 @@ @ExtendWith(MockitoExtension.class) public class TextFunctionTest extends ExpressionTestBase { - @Mock - Environment env; - - @Mock - Expression nullRef; - - @Mock - Expression missingRef; - - - private static List SUBSTRING_STRINGS = ImmutableList.of( - new SubstringInfo("", 1, 1, ""), - new SubstringInfo("Quadratically", 5, null, "ratically"), - new SubstringInfo("foobarbar", 4, null, "barbar"), - new SubstringInfo("Quadratically", 5, 6, "ratica"), - new SubstringInfo("Quadratically", 5, 600, "ratically"), - new SubstringInfo("Quadratically", 500, 1, ""), - new SubstringInfo("Quadratically", 500, null, ""), - new SubstringInfo("Sakila", -3, null, "ila"), - new SubstringInfo("Sakila", -5, 3, "aki"), - new SubstringInfo("Sakila", -4, 2, "ki"), - new SubstringInfo("Quadratically", 0, null, ""), - new SubstringInfo("Sakila", 0, 2, ""), - new SubstringInfo("Sakila", 2, 0, ""), - new SubstringInfo("Sakila", 0, 0, "")); - private static List UPPER_LOWER_STRINGS = ImmutableList.of( - "test", " test", "test ", " test ", "TesT", "TEST", " TEST", "TEST ", " TEST ", " ", ""); - private static List STRING_PATTERN_PAIRS = ImmutableList.of( - new StringPatternPair("Michael!", "Michael!"), - new StringPatternPair("hello", "world"), - new StringPatternPair("world", "hello")); - private static List TRIM_STRINGS = ImmutableList.of( - " test", " test", "test ", "test", " test ", "", " "); - private static List> CONCAT_STRING_LISTS = ImmutableList.of( - ImmutableList.of("hello", "world"), - ImmutableList.of("123", "5325")); - private static List> CONCAT_STRING_LISTS_WITH_MANY_STRINGS = ImmutableList.of( - ImmutableList.of("he", "llo", "wo", "rld", "!"), - ImmutableList.of("0", "123", "53", "25", "7")); + @Mock Environment env; + + @Mock Expression nullRef; + + @Mock Expression missingRef; + + private static List SUBSTRING_STRINGS = + ImmutableList.of( + new SubstringInfo("", 1, 1, ""), + new SubstringInfo("Quadratically", 5, null, "ratically"), + new SubstringInfo("foobarbar", 4, null, "barbar"), + new SubstringInfo("Quadratically", 5, 6, "ratica"), + new SubstringInfo("Quadratically", 5, 600, "ratically"), + new SubstringInfo("Quadratically", 500, 1, ""), + new SubstringInfo("Quadratically", 500, null, ""), + new SubstringInfo("Sakila", -3, null, "ila"), + new SubstringInfo("Sakila", -5, 3, "aki"), + new SubstringInfo("Sakila", -4, 2, "ki"), + new SubstringInfo("Quadratically", 0, null, ""), + new SubstringInfo("Sakila", 0, 2, ""), + new SubstringInfo("Sakila", 2, 0, ""), + new SubstringInfo("Sakila", 0, 0, "")); + private static List UPPER_LOWER_STRINGS = + ImmutableList.of( + "test", " test", "test ", " test ", "TesT", "TEST", " TEST", "TEST ", " TEST ", " ", ""); + private static List STRING_PATTERN_PAIRS = + ImmutableList.of( + new StringPatternPair("Michael!", "Michael!"), + new StringPatternPair("hello", "world"), + new StringPatternPair("world", "hello")); + private static List TRIM_STRINGS = + ImmutableList.of(" test", " test", "test ", "test", " test ", "", " "); + private static List> CONCAT_STRING_LISTS = + ImmutableList.of(ImmutableList.of("hello", "world"), ImmutableList.of("123", "5325")); + private static List> CONCAT_STRING_LISTS_WITH_MANY_STRINGS = + ImmutableList.of( + ImmutableList.of("he", "llo", "wo", "rld", "!"), + ImmutableList.of("0", "123", "53", "25", "7")); interface SubstrSubstring { FunctionExpression getFunction(SubstringInfo strInfo); @@ -86,9 +84,11 @@ public FunctionExpression getFunction(SubstringInfo strInfo) { if (strInfo.getLen() == null) { expr = DSL.substr(DSL.literal(strInfo.getExpr()), DSL.literal(strInfo.getStart())); } else { - expr = DSL.substr(DSL.literal(strInfo.getExpr()), - DSL.literal(strInfo.getStart()), - DSL.literal(strInfo.getLen())); + expr = + DSL.substr( + DSL.literal(strInfo.getExpr()), + DSL.literal(strInfo.getStart()), + DSL.literal(strInfo.getLen())); } return expr; } @@ -100,9 +100,11 @@ public FunctionExpression getFunction(SubstringInfo strInfo) { if (strInfo.getLen() == null) { expr = DSL.substring(DSL.literal(strInfo.getExpr()), DSL.literal(strInfo.getStart())); } else { - expr = DSL.substring(DSL.literal(strInfo.getExpr()), - DSL.literal(strInfo.getStart()), - DSL.literal(strInfo.getLen())); + expr = + DSL.substring( + DSL.literal(strInfo.getExpr()), + DSL.literal(strInfo.getStart()), + DSL.literal(strInfo.getLen())); } return expr; } @@ -235,15 +237,11 @@ void concat() { when(nullRef.type()).thenReturn(STRING); when(missingRef.type()).thenReturn(STRING); - assertEquals(missingValue(), eval( - DSL.concat(missingRef, DSL.literal("1")))); + assertEquals(missingValue(), eval(DSL.concat(missingRef, DSL.literal("1")))); // If any of the expressions is a NULL value, it returns NULL. - assertEquals(nullValue(), eval( - DSL.concat(nullRef, DSL.literal("1")))); - assertEquals(missingValue(), eval( - DSL.concat(DSL.literal("1"), missingRef))); - assertEquals(nullValue(), eval( - DSL.concat(DSL.literal("1"), nullRef))); + assertEquals(nullValue(), eval(DSL.concat(nullRef, DSL.literal("1")))); + assertEquals(missingValue(), eval(DSL.concat(DSL.literal("1"), missingRef))); + assertEquals(nullValue(), eval(DSL.concat(DSL.literal("1"), nullRef))); } @Test @@ -252,18 +250,15 @@ void concat_ws() { when(nullRef.type()).thenReturn(STRING); when(missingRef.type()).thenReturn(STRING); - assertEquals(missingValue(), eval( - DSL.concat_ws(missingRef, DSL.literal("1"), DSL.literal("1")))); - assertEquals(nullValue(), eval( - DSL.concat_ws(nullRef, DSL.literal("1"), DSL.literal("1")))); - assertEquals(missingValue(), eval( - DSL.concat_ws(DSL.literal("1"), missingRef, DSL.literal("1")))); - assertEquals(nullValue(), eval( - DSL.concat_ws(DSL.literal("1"), nullRef, DSL.literal("1")))); - assertEquals(missingValue(), eval( - DSL.concat_ws(DSL.literal("1"), DSL.literal("1"), missingRef))); - assertEquals(nullValue(), eval( - DSL.concat_ws(DSL.literal("1"), DSL.literal("1"), nullRef))); + assertEquals( + missingValue(), eval(DSL.concat_ws(missingRef, DSL.literal("1"), DSL.literal("1")))); + assertEquals(nullValue(), eval(DSL.concat_ws(nullRef, DSL.literal("1"), DSL.literal("1")))); + assertEquals( + missingValue(), eval(DSL.concat_ws(DSL.literal("1"), missingRef, DSL.literal("1")))); + assertEquals(nullValue(), eval(DSL.concat_ws(DSL.literal("1"), nullRef, DSL.literal("1")))); + assertEquals( + missingValue(), eval(DSL.concat_ws(DSL.literal("1"), DSL.literal("1"), missingRef))); + assertEquals(nullValue(), eval(DSL.concat_ws(DSL.literal("1"), DSL.literal("1"), nullRef))); } @Test @@ -290,9 +285,9 @@ void strcmp() { @Test void right() { - FunctionExpression expression = DSL.right( - DSL.literal(new ExprStringValue("foobarbar")), - DSL.literal(new ExprIntegerValue(4))); + FunctionExpression expression = + DSL.right( + DSL.literal(new ExprStringValue("foobarbar")), DSL.literal(new ExprIntegerValue(4))); assertEquals(STRING, expression.type()); assertEquals("rbar", eval(expression).stringValue()); @@ -319,9 +314,9 @@ void right() { @Test void left() { - FunctionExpression expression = DSL.left( - DSL.literal(new ExprStringValue("helloworld")), - DSL.literal(new ExprIntegerValue(5))); + FunctionExpression expression = + DSL.left( + DSL.literal(new ExprStringValue("helloworld")), DSL.literal(new ExprIntegerValue(5))); assertEquals(STRING, expression.type()); assertEquals("hello", eval(expression).stringValue()); @@ -360,16 +355,11 @@ void ascii() { @Test void locate() { - FunctionExpression expression = DSL.locate( - DSL.literal("world"), - DSL.literal("helloworld")); + FunctionExpression expression = DSL.locate(DSL.literal("world"), DSL.literal("helloworld")); assertEquals(INTEGER, expression.type()); assertEquals(6, eval(expression).integerValue()); - expression = DSL.locate( - DSL.literal("world"), - DSL.literal("helloworldworld"), - DSL.literal(7)); + expression = DSL.locate(DSL.literal("world"), DSL.literal("helloworldworld"), DSL.literal(7)); assertEquals(INTEGER, expression.type()); assertEquals(11, eval(expression).integerValue()); @@ -378,21 +368,18 @@ void locate() { assertEquals(nullValue(), eval(DSL.locate(nullRef, DSL.literal("hello"), DSL.literal(1)))); when(missingRef.type()).thenReturn(STRING); assertEquals(missingValue(), eval(DSL.locate(missingRef, DSL.literal("hello")))); - assertEquals(missingValue(), eval( - DSL.locate(missingRef, DSL.literal("hello"), DSL.literal(1)))); + assertEquals( + missingValue(), eval(DSL.locate(missingRef, DSL.literal("hello"), DSL.literal(1)))); } @Test void position() { - FunctionExpression expression = DSL.position( - DSL.literal("world"), - DSL.literal("helloworldworld")); + FunctionExpression expression = + DSL.position(DSL.literal("world"), DSL.literal("helloworldworld")); assertEquals(INTEGER, expression.type()); assertEquals(6, eval(expression).integerValue()); - expression = DSL.position( - DSL.literal("abc"), - DSL.literal("hello world")); + expression = DSL.position(DSL.literal("abc"), DSL.literal("hello world")); assertEquals(INTEGER, expression.type()); assertEquals(0, eval(expression).integerValue()); @@ -404,10 +391,8 @@ void position() { @Test void replace() { - FunctionExpression expression = DSL.replace( - DSL.literal("helloworld"), - DSL.literal("world"), - DSL.literal("opensearch")); + FunctionExpression expression = + DSL.replace(DSL.literal("helloworld"), DSL.literal("world"), DSL.literal("opensearch")); assertEquals(STRING, expression.type()); assertEquals("helloopensearch", eval(expression).stringValue()); @@ -435,18 +420,17 @@ void testConcatString(List strings) { expected = String.join("", strings); } - FunctionExpression expression = DSL.concat( - DSL.literal(strings.get(0)), DSL.literal(strings.get(1))); + FunctionExpression expression = + DSL.concat(DSL.literal(strings.get(0)), DSL.literal(strings.get(1))); assertEquals(STRING, expression.type()); assertEquals(expected, eval(expression).stringValue()); } void testConcatString(List strings, String delim) { - String expected = strings.stream() - .filter(Objects::nonNull).collect(Collectors.joining(",")); + String expected = strings.stream().filter(Objects::nonNull).collect(Collectors.joining(",")); - FunctionExpression expression = DSL.concat_ws( - DSL.literal(delim), DSL.literal(strings.get(0)), DSL.literal(strings.get(1))); + FunctionExpression expression = + DSL.concat_ws(DSL.literal(delim), DSL.literal(strings.get(0)), DSL.literal(strings.get(1))); assertEquals(STRING, expression.type()); assertEquals(expected, eval(expression).stringValue()); } @@ -457,7 +441,8 @@ void testConcatMultipleString(List strings) { expected = String.join("", strings); } - FunctionExpression expression = DSL.concat( + FunctionExpression expression = + DSL.concat( DSL.literal(strings.get(0)), DSL.literal(strings.get(1)), DSL.literal(strings.get(2)), @@ -474,7 +459,8 @@ void testLengthString(String str) { } void testStcmpString(StringPatternPair stringPatternPair) { - FunctionExpression expression = DSL.strcmp( + FunctionExpression expression = + DSL.strcmp( DSL.literal(new ExprStringValue(stringPatternPair.getStr())), DSL.literal(new ExprStringValue(stringPatternPair.getPatt()))); assertEquals(INTEGER, expression.type()); diff --git a/core/src/test/java/org/opensearch/sql/expression/window/CurrentRowWindowFrameTest.java b/core/src/test/java/org/opensearch/sql/expression/window/CurrentRowWindowFrameTest.java index f2e54f96544..3964f36fb7d 100644 --- a/core/src/test/java/org/opensearch/sql/expression/window/CurrentRowWindowFrameTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/window/CurrentRowWindowFrameTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,10 +28,11 @@ class CurrentRowWindowFrameTest { - private final CurrentRowWindowFrame windowFrame = new CurrentRowWindowFrame( - new WindowDefinition( - ImmutableList.of(DSL.ref("state", STRING)), - ImmutableList.of(ImmutablePair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); + private final CurrentRowWindowFrame windowFrame = + new CurrentRowWindowFrame( + new WindowDefinition( + ImmutableList.of(DSL.ref("state", STRING)), + ImmutableList.of(ImmutablePair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); @Test void test_iterator_methods() { @@ -42,17 +42,21 @@ void test_iterator_methods() { @Test void should_return_new_partition_if_partition_by_field_value_changed() { - PeekingIterator iterator = Iterators.peekingIterator( - Iterators.forArray( - ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), - "age", new ExprIntegerValue(20))), - ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), - "age", new ExprIntegerValue(30))), - ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("CA"), - "age", new ExprIntegerValue(18))))); + PeekingIterator iterator = + Iterators.peekingIterator( + Iterators.forArray( + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), + "age", new ExprIntegerValue(20))), + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), + "age", new ExprIntegerValue(30))), + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("CA"), + "age", new ExprIntegerValue(18))))); windowFrame.load(iterator); assertTrue(windowFrame.isNewPartition()); @@ -66,24 +70,28 @@ void should_return_new_partition_if_partition_by_field_value_changed() { @Test void can_resolve_single_expression_value() { - windowFrame.load(Iterators.peekingIterator( - Iterators.singletonIterator( - ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), - "age", new ExprIntegerValue(20)))))); - assertEquals( - new ExprIntegerValue(20), - windowFrame.resolve(DSL.ref("age", INTEGER))); + windowFrame.load( + Iterators.peekingIterator( + Iterators.singletonIterator( + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), + "age", new ExprIntegerValue(20)))))); + assertEquals(new ExprIntegerValue(20), windowFrame.resolve(DSL.ref("age", INTEGER))); } @Test void can_return_previous_and_current_row() { - ExprValue row1 = ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), - "age", new ExprIntegerValue(20))); - ExprValue row2 = ExprTupleValue.fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), - "age", new ExprIntegerValue(30))); + ExprValue row1 = + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), + "age", new ExprIntegerValue(20))); + ExprValue row2 = + ExprTupleValue.fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), + "age", new ExprIntegerValue(30))); PeekingIterator iterator = Iterators.peekingIterator(Iterators.forArray(row1, row2)); windowFrame.load(iterator); @@ -94,5 +102,4 @@ void can_return_previous_and_current_row() { assertEquals(row1, windowFrame.previous()); assertEquals(row2, windowFrame.current()); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunctionTest.java index d7062d4e479..b728a413ba4 100644 --- a/core/src/test/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/window/aggregation/AggregateWindowFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.aggregation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -27,9 +26,7 @@ import org.opensearch.sql.expression.aggregation.Aggregator; import org.opensearch.sql.expression.window.frame.PeerRowsWindowFrame; -/** - * Aggregate window function test collection. - */ +/** Aggregate window function test collection. */ @SuppressWarnings("unchecked") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @ExtendWith(MockitoExtension.class) @@ -56,15 +53,17 @@ void should_accumulate_all_peer_values_and_not_reset_state_if_same_partition() { new AggregateWindowFunction(DSL.sum(DSL.ref("age", INTEGER))); when(windowFrame.isNewPartition()).thenReturn(true); - when(windowFrame.next()).thenReturn(ImmutableList.of( - fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(10))), - fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(20))))); + when(windowFrame.next()) + .thenReturn( + ImmutableList.of( + fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(10))), + fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(20))))); assertEquals(new ExprIntegerValue(30), windowFunction.valueOf(windowFrame)); when(windowFrame.isNewPartition()).thenReturn(false); - when(windowFrame.next()).thenReturn(ImmutableList.of( - fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(30))))); + when(windowFrame.next()) + .thenReturn( + ImmutableList.of(fromExprValueMap(ImmutableMap.of("age", new ExprIntegerValue(30))))); assertEquals(new ExprIntegerValue(60), windowFunction.valueOf(windowFrame)); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrameTest.java b/core/src/test/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrameTest.java index 26f6bb2f5eb..1da2e503227 100644 --- a/core/src/test/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrameTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/window/frame/PeerRowsWindowFrameTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.frame; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,15 +33,16 @@ @ExtendWith(MockitoExtension.class) class PeerRowsWindowFrameTest { - private final PeerRowsWindowFrame windowFrame = new PeerRowsWindowFrame( - new WindowDefinition( - ImmutableList.of(DSL.ref("state", STRING)), - ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); + private final PeerRowsWindowFrame windowFrame = + new PeerRowsWindowFrame( + new WindowDefinition( + ImmutableList.of(DSL.ref("state", STRING)), + ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); @Test void test_single_row() { - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.singletonIterator(tuple("WA", 10, 100))); + PeekingIterator tuples = + Iterators.peekingIterator(Iterators.singletonIterator(tuple("WA", 10, 100))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); @@ -50,11 +50,9 @@ void test_single_row() { @Test void test_single_partition_with_no_more_rows_after_peers() { - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("WA", 20, 200), - tuple("WA", 20, 50))); + PeekingIterator tuples = + Iterators.peekingIterator( + Iterators.forArray(tuple("WA", 10, 100), tuple("WA", 20, 200), tuple("WA", 20, 50))); // Here we simulate how WindowFrame interacts with WindowOperator which calls load() // and WindowFunction which calls isNewPartition() and move() @@ -64,9 +62,7 @@ void test_single_partition_with_no_more_rows_after_peers() { windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of(tuple("WA", 20, 200), tuple("WA", 20, 50)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 20, 200), tuple("WA", 20, 50)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); @@ -75,180 +71,124 @@ void test_single_partition_with_no_more_rows_after_peers() { @Test void test_single_partition_with_more_rows_after_peers() { - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("WA", 20, 200), - tuple("WA", 20, 50), - tuple("WA", 35, 150))); + PeekingIterator tuples = + Iterators.peekingIterator( + Iterators.forArray( + tuple("WA", 10, 100), + tuple("WA", 20, 200), + tuple("WA", 20, 50), + tuple("WA", 35, 150))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 20, 200), - tuple("WA", 20, 50)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 20, 200), tuple("WA", 20, 50)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of(), - windowFrame.next()); + assertEquals(ImmutableList.of(), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 35, 150)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 35, 150)), windowFrame.next()); } @Test void test_two_partitions_with_all_same_peers_in_second_partition() { - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("CA", 18, 150), - tuple("CA", 18, 100))); + PeekingIterator tuples = + Iterators.peekingIterator( + Iterators.forArray(tuple("WA", 10, 100), tuple("CA", 18, 150), tuple("CA", 18, 100))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("CA", 18, 150), - tuple("CA", 18, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("CA", 18, 150), tuple("CA", 18, 100)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of(), - windowFrame.next()); + assertEquals(ImmutableList.of(), windowFrame.next()); } @Test void test_two_partitions_with_single_row_in_each_partition() { - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("CA", 30, 200))); + PeekingIterator tuples = + Iterators.peekingIterator(Iterators.forArray(tuple("WA", 10, 100), tuple("CA", 30, 200))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("CA", 30, 200)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("CA", 30, 200)), windowFrame.next()); } @Test void test_window_definition_with_no_partition_by() { - PeerRowsWindowFrame windowFrame = new PeerRowsWindowFrame( - new WindowDefinition( - ImmutableList.of(), - ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); + PeerRowsWindowFrame windowFrame = + new PeerRowsWindowFrame( + new WindowDefinition( + ImmutableList.of(), + ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("CA", 30, 200))); + PeekingIterator tuples = + Iterators.peekingIterator(Iterators.forArray(tuple("WA", 10, 100), tuple("CA", 30, 200))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("CA", 30, 200)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("CA", 30, 200)), windowFrame.next()); } @Test void test_window_definition_with_no_order_by() { - PeerRowsWindowFrame windowFrame = new PeerRowsWindowFrame( - new WindowDefinition( - ImmutableList.of(DSL.ref("state", STRING)), - ImmutableList.of())); + PeerRowsWindowFrame windowFrame = + new PeerRowsWindowFrame( + new WindowDefinition(ImmutableList.of(DSL.ref("state", STRING)), ImmutableList.of())); - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("CA", 30, 200))); + PeekingIterator tuples = + Iterators.peekingIterator(Iterators.forArray(tuple("WA", 10, 100), tuple("CA", 30, 200))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100)), windowFrame.next()); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("CA", 30, 200)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("CA", 30, 200)), windowFrame.next()); } @Test void test_window_definition_with_no_partition_by_and_order_by() { - PeerRowsWindowFrame windowFrame = new PeerRowsWindowFrame( - new WindowDefinition( - ImmutableList.of(), - ImmutableList.of())); + PeerRowsWindowFrame windowFrame = + new PeerRowsWindowFrame(new WindowDefinition(ImmutableList.of(), ImmutableList.of())); - PeekingIterator tuples = Iterators.peekingIterator( - Iterators.forArray( - tuple("WA", 10, 100), - tuple("CA", 30, 200))); + PeekingIterator tuples = + Iterators.peekingIterator(Iterators.forArray(tuple("WA", 10, 100), tuple("CA", 30, 200))); windowFrame.load(tuples); assertTrue(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of( - tuple("WA", 10, 100), - tuple("CA", 30, 200)), - windowFrame.next()); + assertEquals(ImmutableList.of(tuple("WA", 10, 100), tuple("CA", 30, 200)), windowFrame.next()); windowFrame.load(tuples); assertFalse(windowFrame.isNewPartition()); - assertEquals( - ImmutableList.of(), - windowFrame.next()); + assertEquals(ImmutableList.of(), windowFrame.next()); } private ExprValue tuple(String state, int age, int balance) { - return fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue(state), - "age", new ExprIntegerValue(age), - "balance", new ExprIntegerValue(balance))); + return fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue(state), + "age", new ExprIntegerValue(age), + "balance", new ExprIntegerValue(balance))); } - } diff --git a/core/src/test/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunctionTest.java index 7b2b5a42f2e..dc2f8fd147e 100644 --- a/core/src/test/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/window/ranking/RankingWindowFunctionTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.expression.window.ranking; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,57 +30,72 @@ import org.opensearch.sql.expression.window.WindowDefinition; import org.opensearch.sql.expression.window.frame.CurrentRowWindowFrame; -/** - * Rank window function test collection. - */ +/** Rank window function test collection. */ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @ExtendWith(MockitoExtension.class) class RankingWindowFunctionTest extends ExpressionTestBase { - private final CurrentRowWindowFrame windowFrame1 = new CurrentRowWindowFrame( - new WindowDefinition( - ImmutableList.of(DSL.ref("state", STRING)), - ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); + private final CurrentRowWindowFrame windowFrame1 = + new CurrentRowWindowFrame( + new WindowDefinition( + ImmutableList.of(DSL.ref("state", STRING)), + ImmutableList.of(Pair.of(DEFAULT_ASC, DSL.ref("age", INTEGER))))); - private final CurrentRowWindowFrame windowFrame2 = new CurrentRowWindowFrame( - new WindowDefinition( - ImmutableList.of(DSL.ref("state", STRING)), - ImmutableList.of())); // No sort items defined + private final CurrentRowWindowFrame windowFrame2 = + new CurrentRowWindowFrame( + new WindowDefinition( + ImmutableList.of(DSL.ref("state", STRING)), + ImmutableList.of())); // No sort items defined private PeekingIterator iterator1; private PeekingIterator iterator2; @BeforeEach void set_up() { - iterator1 = Iterators.peekingIterator(Iterators.forArray( - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(40))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(20))))); - - iterator2 = Iterators.peekingIterator(Iterators.forArray( - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(50))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(55))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(15))))); + iterator1 = + Iterators.peekingIterator( + Iterators.forArray( + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(40))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(20))))); + + iterator2 = + Iterators.peekingIterator( + Iterators.forArray( + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(50))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(55))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(15))))); } @Test void test_value_of() { - PeekingIterator iterator = Iterators.peekingIterator( - Iterators.singletonIterator( - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))))); + PeekingIterator iterator = + Iterators.peekingIterator( + Iterators.singletonIterator( + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))))); RankingWindowFunction rowNumber = DSL.rowNumber(); @@ -165,18 +179,24 @@ void row_number_should_work_if_no_sort_items_defined() { @Test void rank_should_always_return_1_if_no_sort_items_defined() { - PeekingIterator iterator = Iterators.peekingIterator( - Iterators.forArray( - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(50))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(55))), - fromExprValueMap(ImmutableMap.of( - "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(15))))); + PeekingIterator iterator = + Iterators.peekingIterator( + Iterators.forArray( + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(30))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(50))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("WA"), "age", new ExprIntegerValue(55))), + fromExprValueMap( + ImmutableMap.of( + "state", new ExprStringValue("CA"), "age", new ExprIntegerValue(15))))); RankingWindowFunction rank = DSL.rank(); @@ -215,5 +235,4 @@ void dense_rank_should_always_return_1_if_no_sort_items_defined() { windowFrame2.load(iterator2); assertEquals(1, denseRank.rank(windowFrame2)); } - } diff --git a/core/src/test/java/org/opensearch/sql/monitor/AlwaysHealthyMonitorTest.java b/core/src/test/java/org/opensearch/sql/monitor/AlwaysHealthyMonitorTest.java index 676f59a44b5..9eb7a816a76 100644 --- a/core/src/test/java/org/opensearch/sql/monitor/AlwaysHealthyMonitorTest.java +++ b/core/src/test/java/org/opensearch/sql/monitor/AlwaysHealthyMonitorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.monitor; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/core/src/test/java/org/opensearch/sql/planner/DefaultImplementorTest.java b/core/src/test/java/org/opensearch/sql/planner/DefaultImplementorTest.java index 2d233e9a6f1..45d8f6c03c5 100644 --- a/core/src/test/java/org/opensearch/sql/planner/DefaultImplementorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/DefaultImplementorTest.java @@ -81,8 +81,7 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class DefaultImplementorTest { - @Mock - private Table table; + @Mock private Table table; private final DefaultImplementor implementor = new DefaultImplementor<>(); @@ -107,50 +106,43 @@ public void visit_should_return_default_physical_operator() { ImmutablePair.of(Sort.SortOption.DEFAULT_ASC, ref("name1", STRING)); Integer limit = 1; Integer offset = 1; - List> nestedArgs = List.of( - Map.of( - "field", new ReferenceExpression("message.info", STRING), - "path", new ReferenceExpression("message", STRING) - ) - ); + List> nestedArgs = + List.of( + Map.of( + "field", new ReferenceExpression("message.info", STRING), + "path", new ReferenceExpression("message", STRING))); List nestedProjectList = List.of( - new NamedExpression( - "message.info", - DSL.nested(DSL.ref("message.info", STRING)), - null - ) - ); + new NamedExpression("message.info", DSL.nested(DSL.ref("message.info", STRING)), null)); Set nestedOperatorArgs = Set.of("message.info"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.info")); - + Map> groupedFieldsByPath = Map.of("message", List.of("message.info")); LogicalPlan plan = project( nested( - limit( - LogicalPlanDSL.dedupe( - rareTopN( - sort( - eval( - remove( - rename( - aggregation( - filter(values(emptyList()), filterExpr), - aggregators, - groupByExprs), - mappings), - exclude), - newEvalField), - sortField), - CommandType.TOP, - topByExprs, - rareTopNField), - dedupeField), - limit, - offset), - nestedArgs, nestedProjectList), + limit( + LogicalPlanDSL.dedupe( + rareTopN( + sort( + eval( + remove( + rename( + aggregation( + filter(values(emptyList()), filterExpr), + aggregators, + groupByExprs), + mappings), + exclude), + newEvalField), + sortField), + CommandType.TOP, + topByExprs, + rareTopNField), + dedupeField), + limit, + offset), + nestedArgs, + nestedProjectList), include); PhysicalPlan actual = plan.accept(implementor, null); @@ -158,37 +150,39 @@ public void visit_should_return_default_physical_operator() { assertEquals( PhysicalPlanDSL.project( PhysicalPlanDSL.nested( - PhysicalPlanDSL.limit( - PhysicalPlanDSL.dedupe( - PhysicalPlanDSL.rareTopN( - PhysicalPlanDSL.sort( - PhysicalPlanDSL.eval( - PhysicalPlanDSL.remove( - PhysicalPlanDSL.rename( - PhysicalPlanDSL.agg( - PhysicalPlanDSL.filter( - PhysicalPlanDSL.values(emptyList()), - filterExpr), - aggregators, - groupByExprs), - mappings), - exclude), - newEvalField), - sortField), - CommandType.TOP, - topByExprs, - rareTopNField), - dedupeField), - limit, - offset), - nestedOperatorArgs, groupedFieldsByPath), + PhysicalPlanDSL.limit( + PhysicalPlanDSL.dedupe( + PhysicalPlanDSL.rareTopN( + PhysicalPlanDSL.sort( + PhysicalPlanDSL.eval( + PhysicalPlanDSL.remove( + PhysicalPlanDSL.rename( + PhysicalPlanDSL.agg( + PhysicalPlanDSL.filter( + PhysicalPlanDSL.values(emptyList()), + filterExpr), + aggregators, + groupByExprs), + mappings), + exclude), + newEvalField), + sortField), + CommandType.TOP, + topByExprs, + rareTopNField), + dedupeField), + limit, + offset), + nestedOperatorArgs, + groupedFieldsByPath), include), actual); } @Test public void visitRelation_should_throw_an_exception() { - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> new LogicalRelation("test", table).accept(implementor, null)); } @@ -196,36 +190,27 @@ public void visitRelation_should_throw_an_exception() { @Test public void visitWindowOperator_should_return_PhysicalWindowOperator() { NamedExpression windowFunction = named(new RowNumberFunction()); - WindowDefinition windowDefinition = new WindowDefinition( - Collections.singletonList(ref("state", STRING)), - Collections.singletonList( - ImmutablePair.of(Sort.SortOption.DEFAULT_DESC, ref("age", INTEGER)))); + WindowDefinition windowDefinition = + new WindowDefinition( + Collections.singletonList(ref("state", STRING)), + Collections.singletonList( + ImmutablePair.of(Sort.SortOption.DEFAULT_DESC, ref("age", INTEGER)))); NamedExpression[] projectList = { - named("state", ref("state", STRING)), - named("row_number", ref("row_number", INTEGER)) + named("state", ref("state", STRING)), named("row_number", ref("row_number", INTEGER)) }; Pair[] sortList = { - ImmutablePair.of(Sort.SortOption.DEFAULT_ASC, ref("state", STRING)), - ImmutablePair.of(Sort.SortOption.DEFAULT_DESC, ref("age", STRING)) + ImmutablePair.of(Sort.SortOption.DEFAULT_ASC, ref("state", STRING)), + ImmutablePair.of(Sort.SortOption.DEFAULT_DESC, ref("age", STRING)) }; LogicalPlan logicalPlan = - project( - window( - sort( - values(), - sortList), - windowFunction, - windowDefinition), - projectList); + project(window(sort(values(), sortList), windowFunction, windowDefinition), projectList); PhysicalPlan physicalPlan = PhysicalPlanDSL.project( PhysicalPlanDSL.window( - PhysicalPlanDSL.sort( - PhysicalPlanDSL.values(), - sortList), + PhysicalPlanDSL.sort(PhysicalPlanDSL.values(), sortList), windowFunction, windowDefinition), projectList); @@ -238,20 +223,22 @@ void visitLogicalCursor_deserializes_it() { var engine = mock(StorageEngine.class); var physicalPlan = new TestOperator(); - var logicalPlan = LogicalPlanDSL.fetchCursor(new PlanSerializer(engine) - .convertToCursor(physicalPlan).toString(), engine); + var logicalPlan = + LogicalPlanDSL.fetchCursor( + new PlanSerializer(engine).convertToCursor(physicalPlan).toString(), engine); assertEquals(physicalPlan, logicalPlan.accept(implementor, null)); } @Test public void visitTableScanBuilder_should_build_TableScanOperator() { TableScanOperator tableScanOperator = mock(TableScanOperator.class); - TableScanBuilder tableScanBuilder = new TableScanBuilder() { - @Override - public TableScanOperator build() { - return tableScanOperator; - } - }; + TableScanBuilder tableScanBuilder = + new TableScanBuilder() { + @Override + public TableScanOperator build() { + return tableScanOperator; + } + }; assertEquals(tableScanOperator, tableScanBuilder.accept(implementor, null)); } @@ -259,12 +246,13 @@ public TableScanOperator build() { public void visitTableWriteBuilder_should_build_TableWriteOperator() { LogicalPlan child = values(); TableWriteOperator tableWriteOperator = mock(TableWriteOperator.class); - TableWriteBuilder logicalPlan = new TableWriteBuilder(child) { - @Override - public TableWriteOperator build(PhysicalPlan child) { - return tableWriteOperator; - } - }; + TableWriteBuilder logicalPlan = + new TableWriteBuilder(child) { + @Override + public TableWriteOperator build(PhysicalPlan child) { + return tableWriteOperator; + } + }; assertEquals(tableWriteOperator, logicalPlan.accept(implementor, null)); } @@ -281,11 +269,13 @@ public void visitCloseCursor_should_build_CursorCloseOperator() { @Test public void visitPaginate_should_remove_it_from_tree() { - var logicalPlanTree = new LogicalPaginate(42, List.of( - new LogicalProject( - new LogicalValues(List.of(List.of())), List.of(), List.of()))); - var physicalPlanTree = new ProjectOperator( - new ValuesOperator(List.of(List.of())), List.of(), List.of()); + var logicalPlanTree = + new LogicalPaginate( + 42, + List.of( + new LogicalProject(new LogicalValues(List.of(List.of())), List.of(), List.of()))); + var physicalPlanTree = + new ProjectOperator(new ValuesOperator(List.of(List.of())), List.of(), List.of()); assertEquals(physicalPlanTree, logicalPlanTree.accept(implementor, null)); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/PlanContextTest.java b/core/src/test/java/org/opensearch/sql/planner/PlanContextTest.java index 77ae78f77e3..76ccb6613a9 100644 --- a/core/src/test/java/org/opensearch/sql/planner/PlanContextTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/PlanContextTest.java @@ -16,8 +16,7 @@ @ExtendWith(MockitoExtension.class) class PlanContextTest { - @Mock - private Split split; + @Mock private Split split; @Test void createEmptyPlanContext() { diff --git a/core/src/test/java/org/opensearch/sql/planner/PlannerTest.java b/core/src/test/java/org/opensearch/sql/planner/PlannerTest.java index 64498f76cd8..6ad44c303b1 100644 --- a/core/src/test/java/org/opensearch/sql/planner/PlannerTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/PlannerTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner; import static java.util.Collections.emptyList; @@ -47,14 +46,11 @@ @ExtendWith(MockitoExtension.class) public class PlannerTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan scan; + @Mock private PhysicalPlan scan; - @Mock - private StorageEngine storageEngine; + @Mock private StorageEngine storageEngine; - @Mock - private LogicalPlanOptimizer optimizer; + @Mock private LogicalPlanOptimizer optimizer; @BeforeEach public void setUp() { @@ -68,29 +64,22 @@ public void planner_test() { PhysicalPlanDSL.rename( PhysicalPlanDSL.agg( PhysicalPlanDSL.filter( - scan, - DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10)) - ), + scan, DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10))), ImmutableList.of(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), - ImmutableList.of() - ), - ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE)) - ), + ImmutableList.of()), + ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE))), LogicalPlanDSL.rename( LogicalPlanDSL.aggregation( LogicalPlanDSL.filter( - LogicalPlanDSL.relation("schema", + LogicalPlanDSL.relation( + "schema", storageEngine.getTable( new DataSourceSchemaName(DEFAULT_DATASOURCE_NAME, "default"), - "schema")), - DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10)) - ), + "schema")), + DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10))), ImmutableList.of(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), - ImmutableList.of() - ), - ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE)) - ) - ); + ImmutableList.of()), + ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE)))); } @Test @@ -103,15 +92,12 @@ public void plan_a_query_without_relation_involved() { PhysicalPlanDSL.values(emptyList()), DSL.named("123", DSL.literal(123)), DSL.named("hello", DSL.literal("hello")), - DSL.named("false", DSL.literal(false)) - ), + DSL.named("false", DSL.literal(false))), LogicalPlanDSL.project( LogicalPlanDSL.values(emptyList()), DSL.named("123", DSL.literal(123)), DSL.named("hello", DSL.literal("hello")), - DSL.named("false", DSL.literal(false)) - ) - ); + DSL.named("false", DSL.literal(false)))); } protected void assertPhysicalPlan(PhysicalPlan expected, LogicalPlan logicalPlan) { @@ -156,15 +142,15 @@ public PhysicalPlan visitFilter(LogicalFilter plan, Object context) { @Override public PhysicalPlan visitAggregation(LogicalAggregation plan, Object context) { - return new AggregationOperator(plan.getChild().get(0).accept(this, context), - plan.getAggregatorList(), plan.getGroupByList() - ); + return new AggregationOperator( + plan.getChild().get(0).accept(this, context), + plan.getAggregatorList(), + plan.getGroupByList()); } @Override public PhysicalPlan visitRename(LogicalRename plan, Object context) { - return new RenameOperator(plan.getChild().get(0).accept(this, context), - plan.getRenameMap()); + return new RenameOperator(plan.getChild().get(0).accept(this, context), plan.getRenameMap()); } } } diff --git a/core/src/test/java/org/opensearch/sql/planner/SerializablePlanTest.java b/core/src/test/java/org/opensearch/sql/planner/SerializablePlanTest.java index 8073445dc08..f00ef61ee1b 100644 --- a/core/src/test/java/org/opensearch/sql/planner/SerializablePlanTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/SerializablePlanTest.java @@ -6,7 +6,6 @@ package org.opensearch.sql.planner; import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Answers.CALLS_REAL_METHODS; import org.junit.jupiter.api.DisplayNameGeneration; diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java index fe751506ce8..d128885a6ac 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.logical; import static org.opensearch.sql.ast.dsl.AstDSL.argument; @@ -30,10 +29,7 @@ public void analyze_dedup_with_two_field_with_default_option() { DSL.ref("integer_value", INTEGER), DSL.ref("double_value", DOUBLE)), dedupe( - relation("schema"), - defaultDedupArgs(), - field("integer_value"), field("double_value") - )); + relation("schema"), defaultDedupArgs(), field("integer_value"), field("double_value"))); } @Test @@ -41,7 +37,9 @@ public void analyze_dedup_with_one_field_with_customize_option() { assertAnalyzeEqual( LogicalPlanDSL.dedupe( LogicalPlanDSL.relation("schema", table), - 3, false, true, + 3, + false, + true, DSL.ref("integer_value", INTEGER), DSL.ref("double_value", DOUBLE)), dedupe( @@ -49,9 +47,8 @@ public void analyze_dedup_with_one_field_with_customize_option() { exprList( argument("number", intLiteral(3)), argument("keepempty", booleanLiteral(false)), - argument("consecutive", booleanLiteral(true)) - ), - field("integer_value"), field("double_value") - )); + argument("consecutive", booleanLiteral(true))), + field("integer_value"), + field("double_value"))); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java index 55bc7930456..746441ef4aa 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.logical; import static org.opensearch.sql.ast.dsl.AstDSL.field; @@ -25,8 +24,8 @@ public void analyze_eval_with_one_field() { assertAnalyzeEqual( LogicalPlanDSL.eval( LogicalPlanDSL.relation("schema", table), - ImmutablePair - .of(DSL.ref("absValue", INTEGER), DSL.abs(DSL.ref("integer_value", INTEGER)))), + ImmutablePair.of( + DSL.ref("absValue", INTEGER), DSL.abs(DSL.ref("integer_value", INTEGER)))), AstDSL.eval( AstDSL.relation("schema"), AstDSL.let(AstDSL.field("absValue"), AstDSL.function("abs", field("integer_value"))))); @@ -37,8 +36,8 @@ public void analyze_eval_with_two_field() { assertAnalyzeEqual( LogicalPlanDSL.eval( LogicalPlanDSL.relation("schema", table), - ImmutablePair - .of(DSL.ref("absValue", INTEGER), DSL.abs(DSL.ref("integer_value", INTEGER))), + ImmutablePair.of( + DSL.ref("absValue", INTEGER), DSL.abs(DSL.ref("integer_value", INTEGER))), ImmutablePair.of(DSL.ref("iValue", INTEGER), DSL.abs(DSL.ref("absValue", INTEGER)))), AstDSL.eval( AstDSL.relation("schema"), diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalPlanNodeVisitorTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalPlanNodeVisitorTest.java index d4d5c89c9bf..df8baab55de 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalPlanNodeVisitorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalPlanNodeVisitorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.logical; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -44,9 +43,7 @@ import org.opensearch.sql.storage.write.TableWriteBuilder; import org.opensearch.sql.storage.write.TableWriteOperator; -/** - * Todo. Temporary added for UT coverage, Will be removed. - */ +/** Todo. Temporary added for UT coverage, Will be removed. */ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class LogicalPlanNodeVisitorTest { @@ -84,50 +81,59 @@ public void logical_plan_should_be_traversable() { @SuppressWarnings("unchecked") private static Stream getLogicalPlansForVisitorTest() { LogicalPlan relation = LogicalPlanDSL.relation("schema", table); - LogicalPlan tableScanBuilder = new TableScanBuilder() { - @Override - public TableScanOperator build() { - return null; - } - }; - TableWriteBuilder tableWriteBuilder = new TableWriteBuilder(null) { - @Override - public TableWriteOperator build(PhysicalPlan child) { - return null; - } - }; + LogicalPlan tableScanBuilder = + new TableScanBuilder() { + @Override + public TableScanOperator build() { + return null; + } + }; + TableWriteBuilder tableWriteBuilder = + new TableWriteBuilder(null) { + @Override + public TableWriteOperator build(PhysicalPlan child) { + return null; + } + }; LogicalPlan write = LogicalPlanDSL.write(null, table, Collections.emptyList()); LogicalPlan filter = LogicalPlanDSL.filter(relation, expression); - LogicalPlan aggregation = LogicalPlanDSL.aggregation( - filter, ImmutableList.of(DSL.named("avg", aggregator)), ImmutableList.of(DSL.named( - "group", expression))); + LogicalPlan aggregation = + LogicalPlanDSL.aggregation( + filter, + ImmutableList.of(DSL.named("avg", aggregator)), + ImmutableList.of(DSL.named("group", expression))); LogicalPlan rename = LogicalPlanDSL.rename(aggregation, ImmutableMap.of(ref, ref)); LogicalPlan project = LogicalPlanDSL.project(relation, named("ref", ref)); LogicalPlan remove = LogicalPlanDSL.remove(relation, ref); LogicalPlan eval = LogicalPlanDSL.eval(relation, Pair.of(ref, expression)); LogicalPlan sort = LogicalPlanDSL.sort(relation, Pair.of(SortOption.DEFAULT_ASC, expression)); LogicalPlan dedup = LogicalPlanDSL.dedupe(relation, 1, false, false, expression); - LogicalPlan window = LogicalPlanDSL.window(relation, named(expression), new WindowDefinition( - ImmutableList.of(ref), ImmutableList.of(Pair.of(SortOption.DEFAULT_ASC, expression)))); - LogicalPlan rareTopN = LogicalPlanDSL.rareTopN( - relation, CommandType.TOP, ImmutableList.of(expression), expression); - LogicalPlan highlight = new LogicalHighlight(filter, - new LiteralExpression(ExprValueUtils.stringValue("fieldA")), Map.of()); + LogicalPlan window = + LogicalPlanDSL.window( + relation, + named(expression), + new WindowDefinition( + ImmutableList.of(ref), + ImmutableList.of(Pair.of(SortOption.DEFAULT_ASC, expression)))); + LogicalPlan rareTopN = + LogicalPlanDSL.rareTopN( + relation, CommandType.TOP, ImmutableList.of(expression), expression); + LogicalPlan highlight = + new LogicalHighlight( + filter, new LiteralExpression(ExprValueUtils.stringValue("fieldA")), Map.of()); LogicalPlan mlCommons = new LogicalMLCommons(relation, "kmeans", Map.of()); LogicalPlan ad = new LogicalAD(relation, Map.of()); LogicalPlan ml = new LogicalML(relation, Map.of()); LogicalPlan paginate = new LogicalPaginate(42, List.of(relation)); - List> nestedArgs = List.of( - Map.of( - "field", new ReferenceExpression("message.info", STRING), - "path", new ReferenceExpression("message", STRING) - ) - ); + List> nestedArgs = + List.of( + Map.of( + "field", new ReferenceExpression("message.info", STRING), + "path", new ReferenceExpression("message", STRING))); List projectList = List.of( - new NamedExpression("message.info", DSL.nested(DSL.ref("message.info", STRING)), null) - ); + new NamedExpression("message.info", DSL.nested(DSL.ref("message.info", STRING)), null)); LogicalNested nested = new LogicalNested(null, nestedArgs, projectList); @@ -136,20 +142,37 @@ public TableWriteOperator build(PhysicalPlan child) { LogicalCloseCursor closeCursor = new LogicalCloseCursor(cursor); return Stream.of( - relation, tableScanBuilder, write, tableWriteBuilder, filter, aggregation, rename, project, - remove, eval, sort, dedup, window, rareTopN, highlight, mlCommons, ad, ml, paginate, nested, - cursor, closeCursor - ).map(Arguments::of); + relation, + tableScanBuilder, + write, + tableWriteBuilder, + filter, + aggregation, + rename, + project, + remove, + eval, + sort, + dedup, + window, + rareTopN, + highlight, + mlCommons, + ad, + ml, + paginate, + nested, + cursor, + closeCursor) + .map(Arguments::of); } @ParameterizedTest @MethodSource("getLogicalPlansForVisitorTest") public void abstract_plan_node_visitor_should_return_null(LogicalPlan plan) { - assertNull(plan.accept(new LogicalPlanNodeVisitor() { - }, null)); + assertNull(plan.accept(new LogicalPlanNodeVisitor() {}, null)); } - private static class NodesCount extends LogicalPlanNodeVisitor { @Override public Integer visitRelation(LogicalRelation plan, Object context) { @@ -160,28 +183,36 @@ public Integer visitRelation(LogicalRelation plan, Object context) { public Integer visitFilter(LogicalFilter plan, Object context) { return 1 + plan.getChild().stream() - .map(child -> child.accept(this, context)).mapToInt(Integer::intValue).sum(); + .map(child -> child.accept(this, context)) + .mapToInt(Integer::intValue) + .sum(); } @Override public Integer visitAggregation(LogicalAggregation plan, Object context) { return 1 + plan.getChild().stream() - .map(child -> child.accept(this, context)).mapToInt(Integer::intValue).sum(); + .map(child -> child.accept(this, context)) + .mapToInt(Integer::intValue) + .sum(); } @Override public Integer visitRename(LogicalRename plan, Object context) { return 1 + plan.getChild().stream() - .map(child -> child.accept(this, context)).mapToInt(Integer::intValue).sum(); + .map(child -> child.accept(this, context)) + .mapToInt(Integer::intValue) + .sum(); } @Override public Integer visitRareTopN(LogicalRareTopN plan, Object context) { return 1 + plan.getChild().stream() - .map(child -> child.accept(this, context)).mapToInt(Integer::intValue).sum(); + .map(child -> child.accept(this, context)) + .mapToInt(Integer::intValue) + .sum(); } } } diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalRelationTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalRelationTest.java index 6426ebb63f9..7181c91da92 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalRelationTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalRelationTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.logical; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -12,14 +11,12 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.opensearch.sql.expression.Expression; import org.opensearch.sql.storage.Table; @ExtendWith(MockitoExtension.class) class LogicalRelationTest { - @Mock - Table table; + @Mock Table table; @Test public void logicalRelationHasNoInput() { @@ -32,5 +29,4 @@ public void logicalRelationWithDataSourceHasNoInput() { LogicalPlan relation = LogicalPlanDSL.relation("prometheus.index", table); assertEquals(0, relation.getChild().size()); } - } diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java index 1a337950095..8757473d243 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.logical; import static org.opensearch.sql.ast.dsl.AstDSL.argument; diff --git a/core/src/test/java/org/opensearch/sql/planner/optimizer/LogicalPlanOptimizerTest.java b/core/src/test/java/org/opensearch/sql/planner/optimizer/LogicalPlanOptimizerTest.java index faedb881113..2cdcb76e717 100644 --- a/core/src/test/java/org/opensearch/sql/planner/optimizer/LogicalPlanOptimizerTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/optimizer/LogicalPlanOptimizerTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.optimizer; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -61,117 +60,85 @@ @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class LogicalPlanOptimizerTest { - @Mock - private Table table; + @Mock private Table table; - @Spy - private TableScanBuilder tableScanBuilder; + @Spy private TableScanBuilder tableScanBuilder; @BeforeEach void setUp() { lenient().when(table.createScanBuilder()).thenReturn(tableScanBuilder); } - /** - * Filter - Filter --> Filter. - */ + /** Filter - Filter --> Filter. */ @Test void filter_merge_filter() { assertEquals( filter( tableScanBuilder, - DSL.and(DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(2))), - DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(1)))) - ), + DSL.and( + DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(2))), + DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(1))))), optimize( filter( filter( relation("schema", table), - DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(1))) - ), - DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(2))) - ) - ) - ); + DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(1)))), + DSL.equal(DSL.ref("integer_value", INTEGER), DSL.literal(integerValue(2)))))); } - /** - * Filter - Sort --> Sort - Filter. - */ + /** Filter - Sort --> Sort - Filter. */ @Test void push_filter_under_sort() { assertEquals( sort( filter( tableScanBuilder, - DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))) - ), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG)) - ), + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))), + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG))), optimize( filter( sort( relation("schema", table), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG)) - ), - DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))) - ) - ) - ); + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG))), + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))))); } - /** - * Filter - Sort --> Sort - Filter. - */ + /** Filter - Sort --> Sort - Filter. */ @Test void multiple_filter_should_eventually_be_merged() { assertEquals( sort( filter( tableScanBuilder, - DSL.and(DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))), - DSL.less(DSL.ref("longV", INTEGER), DSL.literal(longValue(1L)))) - ), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG)) - ), + DSL.and( + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))), + DSL.less(DSL.ref("longV", INTEGER), DSL.literal(longValue(1L))))), + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG))), optimize( filter( sort( filter( relation("schema", table), - DSL.less(DSL.ref("longV", INTEGER), DSL.literal(longValue(1L))) - ), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG)) - ), - DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))) - ) - ) - ); + DSL.less(DSL.ref("longV", INTEGER), DSL.literal(longValue(1L)))), + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("longV", LONG))), + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))))); } @Test void default_table_scan_builder_should_not_push_down_anything() { LogicalPlan[] plans = { - project( - relation("schema", table), - DSL.named("i", DSL.ref("intV", INTEGER)) - ), - filter( - relation("schema", table), - DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1))) - ), - aggregation( - relation("schema", table), - ImmutableList - .of(DSL.named("AVG(intV)", - DSL.avg(DSL.ref("intV", INTEGER)))), - ImmutableList.of(DSL.named("longV", DSL.ref("longV", LONG)))), - sort( - relation("schema", table), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("intV", INTEGER))), - limit( - relation("schema", table), - 1, 1) + project(relation("schema", table), DSL.named("i", DSL.ref("intV", INTEGER))), + filter( + relation("schema", table), + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))), + aggregation( + relation("schema", table), + ImmutableList.of(DSL.named("AVG(intV)", DSL.avg(DSL.ref("intV", INTEGER)))), + ImmutableList.of(DSL.named("longV", DSL.ref("longV", LONG)))), + sort( + relation("schema", table), + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("intV", INTEGER))), + limit(relation("schema", table), 1, 1) }; for (LogicalPlan plan : plans) { @@ -185,12 +152,7 @@ void table_scan_builder_support_project_push_down_can_apply_its_rule() { assertEquals( tableScanBuilder, - optimize( - project( - relation("schema", table), - DSL.named("i", DSL.ref("intV", INTEGER))) - ) - ); + optimize(project(relation("schema", table), DSL.named("i", DSL.ref("intV", INTEGER))))); } @Test @@ -202,9 +164,7 @@ void table_scan_builder_support_filter_push_down_can_apply_its_rule() { optimize( filter( relation("schema", table), - DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))) - ) - ); + DSL.equal(DSL.ref("intV", INTEGER), DSL.literal(integerValue(1)))))); } @Test @@ -216,12 +176,8 @@ void table_scan_builder_support_aggregation_push_down_can_apply_its_rule() { optimize( aggregation( relation("schema", table), - ImmutableList - .of(DSL.named("AVG(intV)", - DSL.avg(DSL.ref("intV", INTEGER)))), - ImmutableList.of(DSL.named("longV", DSL.ref("longV", LONG)))) - ) - ); + ImmutableList.of(DSL.named("AVG(intV)", DSL.avg(DSL.ref("intV", INTEGER)))), + ImmutableList.of(DSL.named("longV", DSL.ref("longV", LONG)))))); } @Test @@ -233,23 +189,14 @@ void table_scan_builder_support_sort_push_down_can_apply_its_rule() { optimize( sort( relation("schema", table), - Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("intV", INTEGER))) - ) - ); + Pair.of(Sort.SortOption.DEFAULT_ASC, DSL.ref("intV", INTEGER))))); } @Test void table_scan_builder_support_limit_push_down_can_apply_its_rule() { when(tableScanBuilder.pushDownLimit(any())).thenReturn(true); - assertEquals( - tableScanBuilder, - optimize( - limit( - relation("schema", table), - 1, 1) - ) - ); + assertEquals(tableScanBuilder, optimize(limit(relation("schema", table), 1, 1))); } @Test @@ -258,13 +205,7 @@ void table_scan_builder_support_highlight_push_down_can_apply_its_rule() { assertEquals( tableScanBuilder, - optimize( - highlight( - relation("schema", table), - DSL.literal("*"), - Collections.emptyMap()) - ) - ); + optimize(highlight(relation("schema", table), DSL.literal("*"), Collections.emptyMap()))); } @Test @@ -277,33 +218,27 @@ void table_scan_builder_support_nested_push_down_can_apply_its_rule() { nested( relation("schema", table), List.of(Map.of("field", new ReferenceExpression("message.info", STRING))), - List.of(new NamedExpression( - "message.info", - DSL.nested(DSL.ref("message.info", STRING)), - null)) - ) - ) - ); + List.of( + new NamedExpression( + "message.info", DSL.nested(DSL.ref("message.info", STRING)), null))))); } @Test void table_not_support_scan_builder_should_not_be_impact() { - Table table = new Table() { - @Override - public Map getFieldTypes() { - return null; - } - - @Override - public PhysicalPlan implement(LogicalPlan plan) { - return null; - } - }; - - assertEquals( - relation("schema", table), - optimize(relation("schema", table)) - ); + Table table = + new Table() { + @Override + public Map getFieldTypes() { + return null; + } + + @Override + public PhysicalPlan implement(LogicalPlan plan) { + return null; + } + }; + + assertEquals(relation("schema", table), optimize(relation("schema", table))); } @Test @@ -311,28 +246,25 @@ void table_support_write_builder_should_be_replaced() { TableWriteBuilder writeBuilder = Mockito.mock(TableWriteBuilder.class); when(table.createWriteBuilder(any())).thenReturn(writeBuilder); - assertEquals( - writeBuilder, - optimize(write(values(), table, Collections.emptyList())) - ); + assertEquals(writeBuilder, optimize(write(values(), table, Collections.emptyList()))); } @Test void table_not_support_write_builder_should_report_error() { - Table table = new Table() { - @Override - public Map getFieldTypes() { - return null; - } - - @Override - public PhysicalPlan implement(LogicalPlan plan) { - return null; - } - }; - - assertThrows(UnsupportedOperationException.class, - () -> table.createWriteBuilder(null)); + Table table = + new Table() { + @Override + public Map getFieldTypes() { + return null; + } + + @Override + public PhysicalPlan implement(LogicalPlan plan) { + return null; + } + }; + + assertThrows(UnsupportedOperationException.class, () -> table.createWriteBuilder(null)); } @Test @@ -340,8 +272,7 @@ void paged_table_scan_builder_support_project_push_down_can_apply_its_rule() { when(tableScanBuilder.pushDownPageSize(any())).thenReturn(true); var relation = relation("schema", table); - var optimized = LogicalPlanOptimizer.create() - .optimize(paginate(project(relation), 4)); + var optimized = LogicalPlanOptimizer.create().optimize(paginate(project(relation), 4)); verify(tableScanBuilder).pushDownPageSize(any()); assertEquals(project(tableScanBuilder), optimized); @@ -350,30 +281,29 @@ void paged_table_scan_builder_support_project_push_down_can_apply_its_rule() { @Test void push_down_page_size_multiple_children() { var relation = relation("schema", table); - var twoChildrenPlan = new LogicalPlan(List.of(relation, relation)) { - @Override - public R accept(LogicalPlanNodeVisitor visitor, C context) { - return null; - } - }; + var twoChildrenPlan = + new LogicalPlan(List.of(relation, relation)) { + @Override + public R accept(LogicalPlanNodeVisitor visitor, C context) { + return null; + } + }; var queryPlan = paginate(twoChildrenPlan, 4); var optimizer = LogicalPlanOptimizer.create(); - final var exception = assertThrows(UnsupportedOperationException.class, - () -> optimizer.optimize(queryPlan)); - assertEquals("Unsupported plan: relation operator cannot have siblings", - exception.getMessage()); + final var exception = + assertThrows(UnsupportedOperationException.class, () -> optimizer.optimize(queryPlan)); + assertEquals( + "Unsupported plan: relation operator cannot have siblings", exception.getMessage()); } @Test void push_down_page_size_push_failed() { when(tableScanBuilder.pushDownPageSize(any())).thenReturn(false); - var queryPlan = paginate( - project( - relation("schema", table)), 4); + var queryPlan = paginate(project(relation("schema", table)), 4); var optimizer = LogicalPlanOptimizer.create(); - final var exception = assertThrows(IllegalStateException.class, - () -> optimizer.optimize(queryPlan)); + final var exception = + assertThrows(IllegalStateException.class, () -> optimizer.optimize(queryPlan)); assertEquals("Failed to push down LogicalPaginate", exception.getMessage()); } @@ -386,8 +316,7 @@ void push_page_size_noop_if_no_relation() { @Test void push_page_size_noop_if_no_sub_plans() { var paginate = new LogicalPaginate(42, List.of()); - assertEquals(paginate, - LogicalPlanOptimizer.create().optimize(paginate)); + assertEquals(paginate, LogicalPlanOptimizer.create().optimize(paginate)); } @Test @@ -395,8 +324,8 @@ void table_scan_builder_support_offset_push_down_can_apply_its_rule() { when(tableScanBuilder.pushDownPageSize(any())).thenReturn(true); var relation = new LogicalRelation("schema", table); - var optimized = LogicalPlanOptimizer.create() - .optimize(new LogicalPaginate(42, List.of(project(relation)))); + var optimized = + LogicalPlanOptimizer.create().optimize(new LogicalPaginate(42, List.of(project(relation)))); // `optimized` structure: LogicalProject -> TableScanBuilder // LogicalRelation replaced by a TableScanBuilder instance assertEquals(project(tableScanBuilder), optimized); diff --git a/core/src/test/java/org/opensearch/sql/planner/optimizer/pattern/PatternsTest.java b/core/src/test/java/org/opensearch/sql/planner/optimizer/pattern/PatternsTest.java index ef310e3b0e0..d1e1a03521f 100644 --- a/core/src/test/java/org/opensearch/sql/planner/optimizer/pattern/PatternsTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/optimizer/pattern/PatternsTest.java @@ -3,12 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.optimizer.pattern; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -17,7 +15,6 @@ import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; import org.opensearch.sql.planner.logical.LogicalFilter; -import org.opensearch.sql.planner.logical.LogicalPaginate; import org.opensearch.sql.planner.logical.LogicalPlan; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @@ -29,8 +26,8 @@ void source_is_empty() { when(plan.getChild()).thenReturn(Collections.emptyList()); assertAll( () -> assertFalse(Patterns.source().getFunction().apply(plan).isPresent()), - () -> assertFalse(Patterns.source(null).getProperty().getFunction().apply(plan).isPresent()) - ); + () -> + assertFalse(Patterns.source(null).getProperty().getFunction().apply(plan).isPresent())); } @Test @@ -38,7 +35,6 @@ void table_is_empty() { var plan = mock(LogicalFilter.class); assertAll( () -> assertFalse(Patterns.table().getFunction().apply(plan).isPresent()), - () -> assertFalse(Patterns.writeTable().getFunction().apply(plan).isPresent()) - ); + () -> assertFalse(Patterns.writeTable().getFunction().apply(plan).isPresent())); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/AggregationOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/AggregationOperatorTest.java index 2585a21d7ab..0f3f4bd61f7 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/AggregationOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/AggregationOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -38,563 +37,733 @@ class AggregationOperatorTest extends PhysicalPlanTestBase { @Test public void sum_without_groups() { - PhysicalPlan plan = new AggregationOperator(new TestScan(), - Collections - .singletonList(DSL.named("sum(response)", DSL.sum(DSL.ref("response", INTEGER)))), - Collections.emptyList()); + PhysicalPlan plan = + new AggregationOperator( + new TestScan(), + Collections.singletonList( + DSL.named("sum(response)", DSL.sum(DSL.ref("response", INTEGER)))), + Collections.emptyList()); List result = execute(plan); assertEquals(1, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("sum(response)", 1504d)) - )); + assertThat( + result, + containsInAnyOrder(ExprValueUtils.tupleValue(ImmutableMap.of("sum(response)", 1504d)))); } @Test public void avg_with_one_groups() { - PhysicalPlan plan = new AggregationOperator(new TestScan(), - Collections - .singletonList(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), - Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))); + PhysicalPlan plan = + new AggregationOperator( + new TestScan(), + Collections.singletonList( + DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), + Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "avg(response)", 268d)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "avg(response)", 350d)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "avg(response)", 268d)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "avg(response)", 350d)))); } @Test public void avg_with_two_groups() { - PhysicalPlan plan = new AggregationOperator(new TestScan(), - Collections - .singletonList(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), - Arrays.asList(DSL.named("action", DSL.ref("action", STRING)), - DSL.named("ip", DSL.ref("ip", STRING)))); + PhysicalPlan plan = + new AggregationOperator( + new TestScan(), + Collections.singletonList( + DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), + Arrays.asList( + DSL.named("action", DSL.ref("action", STRING)), + DSL.named("ip", DSL.ref("ip", STRING)))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue( - ImmutableMap.of("action", "GET", "ip", "209.160.24.63", "avg(response)", 302d)), - ExprValueUtils.tupleValue( - ImmutableMap.of("action", "GET", "ip", "112.111.162.4", "avg(response)", 200d)), - ExprValueUtils.tupleValue( - ImmutableMap.of("action", "POST", "ip", "74.125.19.106", "avg(response)", 350d)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("action", "GET", "ip", "209.160.24.63", "avg(response)", 302d)), + ExprValueUtils.tupleValue( + ImmutableMap.of("action", "GET", "ip", "112.111.162.4", "avg(response)", 200d)), + ExprValueUtils.tupleValue( + ImmutableMap.of("action", "POST", "ip", "74.125.19.106", "avg(response)", 350d)))); } @Test public void sum_with_one_groups() { - PhysicalPlan plan = new AggregationOperator(new TestScan(), - Collections - .singletonList(DSL.named("sum(response)", DSL.sum(DSL.ref("response", INTEGER)))), - Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))); + PhysicalPlan plan = + new AggregationOperator( + new TestScan(), + Collections.singletonList( + DSL.named("sum(response)", DSL.sum(DSL.ref("response", INTEGER)))), + Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "sum(response)", 804)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "sum(response)", 700)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "sum(response)", 804)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "sum(response)", 700)))); } @Test public void millisecond_span() { - PhysicalPlan plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6 * 1000), "ms")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), + Collections.singletonList( + DSL.named( + "span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6 * 1000), "ms")))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2021-01-01 00:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2021-01-01 00:00:12"), "count", 3)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("2021-01-01 00:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprTimestampValue("2021-01-01 00:00:12"), "count", 3)))); } @Test public void second_span() { - PhysicalPlan plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6), "s")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6), "s")))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2021-01-01 00:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2021-01-01 00:00:12"), "count", 3)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("2021-01-01 00:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprTimestampValue("2021-01-01 00:00:12"), "count", 3)))); } @Test public void minute_span() { - PhysicalPlan plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("minute", DATETIME)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("minute", DATETIME), DSL.literal(5), "m")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("minute", DATETIME)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("minute", DATETIME), DSL.literal(5), "m")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2020-12-31 23:50:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2021-01-01 00:05:00"), "count", 1)) - )); - - plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("hour", TIME)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("hour", TIME), DSL.literal(30), "m")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2020-12-31 23:50:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDatetimeValue("2021-01-01 00:05:00"), "count", 1)))); + + plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("hour", TIME)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("hour", TIME), DSL.literal(30), "m")))); result = execute(plan); assertEquals(4, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("17:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("18:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("18:30:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("19:00:00"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("17:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("18:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("18:30:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("19:00:00"), "count", 1)))); } @Test public void hour_span() { - PhysicalPlan plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("hour", TIME)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("hour", TIME), DSL.literal(1), "h")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("hour", TIME)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("hour", TIME), DSL.literal(1), "h")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("17:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("18:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimeValue("19:00:00"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("17:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("18:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimeValue("19:00:00"), "count", 1)))); } @Test public void day_span() { - PhysicalPlan plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count(day)", DSL.count(DSL.ref("day", DATE)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count(day)", DSL.count(DSL.ref("day", DATE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")))); List result = execute(plan); assertEquals(4, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-01-01"), "count(day)", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-01-02"), "count(day)", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-01-03"), "count(day)", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-01-04"), "count(day)", 1)) - )); - - plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("month", DATE)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(30), "d")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-01-01"), "count(day)", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-01-02"), "count(day)", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-01-03"), "count(day)", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-01-04"), "count(day)", 1)))); + + plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("month", DATE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(30), "d")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2020-12-04"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-02-02"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-03-04"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2020-12-04"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-02-02"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-03-04"), "count", 1)))); } @Test public void week_span() { - PhysicalPlan plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("month", DATE)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(5), "w")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("month", DATE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(5), "w")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2020-11-16"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-01-25"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-03-01"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2020-11-16"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-01-25"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-03-01"), "count", 1)))); } @Test public void month_span() { - PhysicalPlan plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("month", DATE)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(1), "M")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("month", DATE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("month", DATE), DSL.literal(1), "M")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2020-12-01"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-02-01"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDateValue("2021-03-01"), "count", 1)) - )); - - plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("quarter", DATETIME)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("quarter", DATETIME), DSL.literal(2), "M")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2020-12-01"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-02-01"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDateValue("2021-03-01"), "count", 1)))); + + plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("quarter", DATETIME)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("quarter", DATETIME), DSL.literal(2), "M")))); result = execute(plan); assertEquals(4, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2020-09-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2020-11-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2021-05-01 00:00:00"), "count", 2)) - )); - - plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10 * 12), "M")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2020-09-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2020-11-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDatetimeValue("2021-05-01 00:00:00"), "count", 2)))); + + plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), + Collections.singletonList( + DSL.named( + "span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10 * 12), "M")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)) - )); - + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)))); } @Test public void quarter_span() { - PhysicalPlan plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("quarter", DATETIME)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("quarter", DATETIME), DSL.literal(2), "q")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("quarter", DATETIME)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("quarter", DATETIME), DSL.literal(2), "q")))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2020-07-01 00:00:00"), "count", 2)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 3)) - )); - - plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10 * 4), "q")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprDatetimeValue("2020-07-01 00:00:00"), "count", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDatetimeValue("2021-01-01 00:00:00"), "count", 3)))); + + plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10 * 4), "q")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)))); } @Test public void year_span() { - PhysicalPlan plan = new AggregationOperator(testScan(dateInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10), "y")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(dateInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("year", TIMESTAMP)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("year", TIMESTAMP), DSL.literal(10), "y")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), - ExprValueUtils.tupleValue(ImmutableMap - .of("span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("1990-01-01 00:00:00"), "count", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of("span", new ExprTimestampValue("2010-01-01 00:00:00"), "count", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprTimestampValue("2020-01-01 00:00:00"), "count", 1)))); } @Test public void integer_field() { - PhysicalPlan plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("integer", INTEGER)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("integer", INTEGER), DSL - .literal(1), "")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("integer", INTEGER)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("integer", INTEGER), DSL.literal(1), "")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 2, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 5, "count", 1)))); - - plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("integer", INTEGER)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("integer", INTEGER), DSL - .literal(1.5), "")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 2, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 5, "count", 1)))); + + plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("integer", INTEGER)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("integer", INTEGER), DSL.literal(1.5), "")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); } @Test public void long_field() { - PhysicalPlan plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("long", LONG)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("long", LONG), DSL - .literal(1), "")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("long", LONG)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("long", LONG), DSL.literal(1), "")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1L, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 2L, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 5L, "count", 1)))); - - plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("long", LONG)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("long", LONG), DSL - .literal(1.5), "")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1L, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 2L, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 5L, "count", 1)))); + + plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("long", LONG)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("long", LONG), DSL.literal(1.5), "")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); } @Test public void float_field() { - PhysicalPlan plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("float", FLOAT)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("float", FLOAT), DSL - .literal(1), "")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("float", FLOAT)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("float", FLOAT), DSL.literal(1), "")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1F, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 2F, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 5F, "count", 1)))); - - plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("float", FLOAT)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("float", FLOAT), DSL - .literal(1.5), "")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1F, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 2F, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 5F, "count", 1)))); + + plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("float", FLOAT)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("float", FLOAT), DSL.literal(1.5), "")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); } @Test public void double_field() { - PhysicalPlan plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("double", DOUBLE)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("double", DOUBLE), DSL - .literal(1), "")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("double", DOUBLE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("double", DOUBLE), DSL.literal(1), "")))); List result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 2D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 5D, "count", 1)))); - - plan = new AggregationOperator(testScan(numericInputs), - Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("double", DOUBLE)))), - Collections.singletonList(DSL.named("span", DSL.span(DSL.ref("double", DOUBLE), DSL - .literal(1.5), "")))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 2D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 5D, "count", 1)))); + + plan = + new AggregationOperator( + testScan(numericInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("double", DOUBLE)))), + Collections.singletonList( + DSL.named("span", DSL.span(DSL.ref("double", DOUBLE), DSL.literal(1.5), "")))); result = execute(plan); assertEquals(3, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("span", 0D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 1.5D, "count", 1)), + ExprValueUtils.tupleValue(ImmutableMap.of("span", 4.5D, "count", 1)))); } @Test public void twoBucketsSpanAndLong() { - PhysicalPlan plan = new AggregationOperator(testScan(compoundInputs), - Collections.singletonList(DSL.named("max", DSL.max(DSL.ref("errors", INTEGER)))), - Arrays.asList( - DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), - DSL.named("region", DSL.ref("region", STRING))) - ); + PhysicalPlan plan = + new AggregationOperator( + testScan(compoundInputs), + Collections.singletonList(DSL.named("max", DSL.max(DSL.ref("errors", INTEGER)))), + Arrays.asList( + DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), + DSL.named("region", DSL.ref("region", STRING)))); List result = execute(plan); assertEquals(4, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-03"), "region","iad", "max", 3)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-04"), "region","iad", "max", 10)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-06"), "region","iad", "max", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-07"), "region","iad", "max", 8)) - )); - - plan = new AggregationOperator(testScan(compoundInputs), - Collections.singletonList(DSL.named("max", DSL.max(DSL.ref("errors", INTEGER)))), - Arrays.asList( - DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), - DSL.named("region", DSL.ref("region", STRING)), - DSL.named("host", DSL.ref("host", STRING))) - ); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDateValue("2021-01-03"), "region", "iad", "max", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDateValue("2021-01-04"), "region", "iad", "max", 10)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDateValue("2021-01-06"), "region", "iad", "max", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", new ExprDateValue("2021-01-07"), "region", "iad", "max", 8)))); + + plan = + new AggregationOperator( + testScan(compoundInputs), + Collections.singletonList(DSL.named("max", DSL.max(DSL.ref("errors", INTEGER)))), + Arrays.asList( + DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), + DSL.named("region", DSL.ref("region", STRING)), + DSL.named("host", DSL.ref("host", STRING)))); result = execute(plan); assertEquals(7, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-03"), "region","iad", "host", "h1", "max", 2)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-03"), "region","iad", "host", "h2", "max", 3)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-04"), "region","iad", "host", "h1", "max", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-04"), "region","iad", "host", "h2", "max", 10)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-06"), "region","iad", "host", "h1", "max", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-07"), "region","iad", "host", "h1", "max", 6)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "span", new ExprDateValue("2021-01-07"), "region","iad", "host", "h2", "max", 8)) - )); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-03"), + "region", + "iad", + "host", + "h1", + "max", + 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-03"), + "region", + "iad", + "host", + "h2", + "max", + 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-04"), + "region", + "iad", + "host", + "h1", + "max", + 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-04"), + "region", + "iad", + "host", + "h2", + "max", + 10)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-06"), + "region", + "iad", + "host", + "h1", + "max", + 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-07"), + "region", + "iad", + "host", + "h1", + "max", + 6)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "span", + new ExprDateValue("2021-01-07"), + "region", + "iad", + "host", + "h2", + "max", + 8)))); } @Test public void aggregate_with_two_groups_with_windowing() { - PhysicalPlan plan = new AggregationOperator(testScan(compoundInputs), - Collections.singletonList(DSL.named("sum", DSL.sum(DSL.ref("errors", INTEGER)))), - Arrays.asList( - DSL.named("host", DSL.ref("host", STRING)), - DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")))); + PhysicalPlan plan = + new AggregationOperator( + testScan(compoundInputs), + Collections.singletonList(DSL.named("sum", DSL.sum(DSL.ref("errors", INTEGER)))), + Arrays.asList( + DSL.named("host", DSL.ref("host", STRING)), + DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")))); List result = execute(plan); assertEquals(7, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-03"), - "sum", 2)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-04"), - "sum", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-06"), - "sum", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-07"), - "sum", 6)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-03"), - "sum", 3)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-04"), - "sum", 10)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-07"), - "sum", 8)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-03"), + "sum", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-04"), + "sum", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-06"), + "sum", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-07"), + "sum", 6)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-03"), + "sum", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-04"), + "sum", 10)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-07"), + "sum", 8)))); } @Test public void aggregate_with_three_groups_with_windowing() { - PhysicalPlan plan = new AggregationOperator(testScan(compoundInputs), - Collections.singletonList(DSL.named("sum", DSL.sum(DSL.ref("errors", INTEGER)))), - Arrays.asList( - DSL.named("host", DSL.ref("host", STRING)), - DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), - DSL.named("region", DSL.ref("region", STRING)))); + PhysicalPlan plan = + new AggregationOperator( + testScan(compoundInputs), + Collections.singletonList(DSL.named("sum", DSL.sum(DSL.ref("errors", INTEGER)))), + Arrays.asList( + DSL.named("host", DSL.ref("host", STRING)), + DSL.named("span", DSL.span(DSL.ref("day", DATE), DSL.literal(1), "d")), + DSL.named("region", DSL.ref("region", STRING)))); List result = execute(plan); assertEquals(7, result.size()); - assertThat(result, containsInRelativeOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-03"), - "region", new ExprStringValue("iad"), - "sum", 2)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-04"), - "region", new ExprStringValue("iad"), - "sum", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-06"), - "region", new ExprStringValue("iad"), - "sum", 1)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h1"), - "span", new ExprDateValue("2021-01-07"), - "region", new ExprStringValue("iad"), - "sum", 6)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-03"), - "region", new ExprStringValue("iad"), - "sum", 3)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-04"), - "region", new ExprStringValue("iad"), - "sum", 10)), - ExprValueUtils.tupleValue(ImmutableMap.of( - "host", new ExprStringValue("h2"), - "span", new ExprDateValue("2021-01-07"), - "region", new ExprStringValue("iad"), - "sum", 8)))); + assertThat( + result, + containsInRelativeOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-03"), + "region", new ExprStringValue("iad"), + "sum", 2)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-04"), + "region", new ExprStringValue("iad"), + "sum", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-06"), + "region", new ExprStringValue("iad"), + "sum", 1)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h1"), + "span", new ExprDateValue("2021-01-07"), + "region", new ExprStringValue("iad"), + "sum", 6)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-03"), + "region", new ExprStringValue("iad"), + "sum", 3)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-04"), + "region", new ExprStringValue("iad"), + "sum", 10)), + ExprValueUtils.tupleValue( + ImmutableMap.of( + "host", new ExprStringValue("h2"), + "span", new ExprDateValue("2021-01-07"), + "region", new ExprStringValue("iad"), + "sum", 8)))); } @Test public void copyOfAggregationOperatorShouldSame() { - AggregationOperator plan = new AggregationOperator(testScan(datetimeInputs), - Collections.singletonList(DSL - .named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), - Collections.singletonList(DSL - .named("span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6 * 1000), "ms")))); - AggregationOperator copy = new AggregationOperator(plan.getInput(), plan.getAggregatorList(), - plan.getGroupByExprList()); + AggregationOperator plan = + new AggregationOperator( + testScan(datetimeInputs), + Collections.singletonList(DSL.named("count", DSL.count(DSL.ref("second", TIMESTAMP)))), + Collections.singletonList( + DSL.named( + "span", DSL.span(DSL.ref("second", TIMESTAMP), DSL.literal(6 * 1000), "ms")))); + AggregationOperator copy = + new AggregationOperator( + plan.getInput(), plan.getAggregatorList(), plan.getGroupByExprList()); assertEquals(plan, copy); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/DedupeOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/DedupeOperatorTest.java index 13f35f98439..f6308aed53b 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/DedupeOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/DedupeOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -26,8 +25,7 @@ @ExtendWith(MockitoExtension.class) class DedupeOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; /** * construct the map which contain null value, because {@link ImmutableMap} doesn't support null @@ -182,8 +180,7 @@ public void dedupe_one_field_with_missing_value() { tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "POST", "response", 200))) - .thenReturn( - tupleValue(ImmutableMap.of("action", "POST", "response", 200))) + .thenReturn(tupleValue(ImmutableMap.of("action", "POST", "response", 200))) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))); @@ -201,8 +198,7 @@ public void dedupe_one_field_with_missing_value_keep_empty() { tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "POST", "response", 200))) - .thenReturn( - tupleValue(ImmutableMap.of("action", "POST", "response", 200))) + .thenReturn(tupleValue(ImmutableMap.of("action", "POST", "response", 200))) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))); @@ -221,8 +217,7 @@ public void dedupe_one_field_with_null_value() { tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "POST", "response", 200))) - .thenReturn( - tupleValue(NULL_MAP)) + .thenReturn(tupleValue(NULL_MAP)) .thenReturn( tupleValue(ImmutableMap.of("region", "us-east-1", "action", "GET", "response", 200))); diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/EvalOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/EvalOperatorTest.java index d32622834b9..6f10cc435b7 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/EvalOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/EvalOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -29,8 +28,7 @@ @ExtendWith(MockitoExtension.class) class EvalOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; @Test public void create_new_field_that_contain_the_result_of_a_calculation() { @@ -63,8 +61,8 @@ public void create_multiple_field_using_field_defined_in_input_tuple() { eval( inputPlan, ImmutablePair.of( - DSL.ref("velocity", DOUBLE), DSL.divide(DSL.ref("distance", INTEGER), DSL.ref( - "time", INTEGER))), + DSL.ref("velocity", DOUBLE), + DSL.divide(DSL.ref("distance", INTEGER), DSL.ref("time", INTEGER))), ImmutablePair.of( DSL.ref("doubleDistance", INTEGER), DSL.multiply(DSL.ref("distance", INTEGER), DSL.literal(2)))); @@ -88,8 +86,8 @@ public void create_multiple_filed_using_field_defined_in_eval_operator() { eval( inputPlan, ImmutablePair.of( - DSL.ref("velocity", INTEGER), DSL.divide(DSL.ref("distance", INTEGER), DSL.ref( - "time", INTEGER))), + DSL.ref("velocity", INTEGER), + DSL.divide(DSL.ref("distance", INTEGER), DSL.ref("time", INTEGER))), ImmutablePair.of( DSL.ref("doubleVelocity", INTEGER), DSL.multiply(DSL.ref("velocity", INTEGER), DSL.literal(2)))); @@ -126,8 +124,8 @@ public void replace_existing_field() { public void do_nothing_with_none_tuple_value() { when(inputPlan.hasNext()).thenReturn(true, false); when(inputPlan.next()).thenReturn(ExprValueUtils.integerValue(1)); - PhysicalPlan plan = eval(inputPlan, ImmutablePair.of(DSL.ref("response", INTEGER), - DSL.ref("referer", STRING))); + PhysicalPlan plan = + eval(inputPlan, ImmutablePair.of(DSL.ref("response", INTEGER), DSL.ref("referer", STRING))); List result = execute(plan); assertThat(result, allOf(iterableWithSize(1), hasItems(ExprValueUtils.integerValue(1)))); diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/FilterOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/FilterOperatorTest.java index 6a8bcad2034..bfe3b323c4c 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/FilterOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/FilterOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -17,17 +16,12 @@ import com.google.common.collect.ImmutableMap; import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.AdditionalAnswers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.opensearch.sql.data.model.ExprIntegerValue; import org.opensearch.sql.data.model.ExprTupleValue; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.model.ExprValueUtils; @@ -36,20 +30,31 @@ @ExtendWith(MockitoExtension.class) @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class FilterOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; @Test public void filter_test() { - FilterOperator plan = new FilterOperator(new TestScan(), - DSL.and(DSL.notequal(DSL.ref("response", INTEGER), DSL.literal(200)), + FilterOperator plan = + new FilterOperator( + new TestScan(), + DSL.and( + DSL.notequal(DSL.ref("response", INTEGER), DSL.literal(200)), DSL.notequal(DSL.ref("response", INTEGER), DSL.literal(500)))); List result = execute(plan); assertEquals(1, result.size()); - assertThat(result, containsInAnyOrder(ExprValueUtils - .tupleValue(ImmutableMap - .of("ip", "209.160.24.63", "action", "GET", "response", 404, "referer", - "www.amazon.com")))); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com")))); } @Test @@ -59,8 +64,8 @@ public void null_value_should_been_ignored() { when(inputPlan.hasNext()).thenReturn(true, false); when(inputPlan.next()).thenReturn(new ExprTupleValue(value)); - FilterOperator plan = new FilterOperator(inputPlan, - DSL.equal(DSL.ref("response", INTEGER), DSL.literal(404))); + FilterOperator plan = + new FilterOperator(inputPlan, DSL.equal(DSL.ref("response", INTEGER), DSL.literal(404))); List result = execute(plan); assertEquals(0, result.size()); } @@ -72,8 +77,8 @@ public void missing_value_should_been_ignored() { when(inputPlan.hasNext()).thenReturn(true, false); when(inputPlan.next()).thenReturn(new ExprTupleValue(value)); - FilterOperator plan = new FilterOperator(inputPlan, - DSL.equal(DSL.ref("response", INTEGER), DSL.literal(404))); + FilterOperator plan = + new FilterOperator(inputPlan, DSL.equal(DSL.ref("response", INTEGER), DSL.literal(404))); List result = execute(plan); assertEquals(0, result.size()); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/LimitOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/LimitOperatorTest.java index a9796bb11a5..fa3788cc2d2 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/LimitOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/LimitOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -23,10 +22,19 @@ public void limit() { PhysicalPlan plan = new LimitOperator(new TestScan(), 1, 0); List result = execute(plan); assertEquals(1, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 200, "referer", "www.amazon.com")) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com")))); } @Test @@ -34,15 +42,24 @@ public void limit_and_offset() { PhysicalPlan plan = new LimitOperator(new TestScan(), 1, 1); List result = execute(plan); assertEquals(1, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 404, "referer", "www.amazon.com")) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com")))); } @Test public void offset_exceeds_row_number() { - PhysicalPlan plan = new LimitOperator(new TestScan(),1, 6); + PhysicalPlan plan = new LimitOperator(new TestScan(), 1, 6); List result = execute(plan); assertEquals(0, result.size()); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/NestedOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/NestedOperatorTest.java index 5f8bf99b0df..cf98183eb32 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/NestedOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/NestedOperatorTest.java @@ -8,7 +8,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; import static org.opensearch.sql.data.model.ExprValueUtils.collectionValue; import static org.opensearch.sql.data.model.ExprValueUtils.tupleValue; @@ -29,68 +28,40 @@ @ExtendWith(MockitoExtension.class) class NestedOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; - private final ExprValue testData = tupleValue( - Map.of( - "message", - collectionValue( - List.of( - Map.of("info", "a"), - Map.of("info", "b"), - Map.of("info", "c") - ) - ), - "comment", - collectionValue( - List.of( - Map.of("data", "1"), - Map.of("data", "2"), - Map.of("data", "3") - ) - ) - ) - ); + private final ExprValue testData = + tupleValue( + Map.of( + "message", + collectionValue( + List.of(Map.of("info", "a"), Map.of("info", "b"), Map.of("info", "c"))), + "comment", + collectionValue( + List.of(Map.of("data", "1"), Map.of("data", "2"), Map.of("data", "3"))))); + private final ExprValue testDataWithSamePath = + tupleValue( + Map.of( + "message", + collectionValue( + List.of( + Map.of("info", "a", "id", "1"), + Map.of("info", "b", "id", "2"), + Map.of("info", "c", "id", "3"))))); - private final ExprValue testDataWithSamePath = tupleValue( - Map.of( - "message", - collectionValue( - List.of( - Map.of("info", "a", "id", "1"), - Map.of("info", "b", "id", "2"), - Map.of("info", "c", "id", "3") - ) - ) - ) - ); + private final ExprValue nonNestedTestData = tupleValue(Map.of("message", "val")); - private final ExprValue nonNestedTestData = tupleValue( - Map.of( - "message", "val" - ) - ); - - private final ExprValue missingArrayData = tupleValue( - Map.of( - "missing", - collectionValue( - List.of("value") - ) - ) - ); + private final ExprValue missingArrayData = + tupleValue(Map.of("missing", collectionValue(List.of("value")))); @Test public void nested_one_nested_field() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(testData); + when(inputPlan.next()).thenReturn(testData); Set fields = Set.of("message.info"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.info")); + Map> groupedFieldsByPath = Map.of("message", List.of("message.info")); var nested = new NestedOperator(inputPlan, fields, groupedFieldsByPath); @@ -98,77 +69,104 @@ public void nested_one_nested_field() { execute(nested), contains( tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "a"); - put("comment", collectionValue( - new ArrayList<>() {{ - add(new LinkedHashMap<>() {{ - put("data", "1"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "2"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "3"); - }} - ); - }} - )); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "a"); + put( + "comment", + collectionValue( + new ArrayList<>() { + { + add( + new LinkedHashMap<>() { + { + put("data", "1"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "2"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "3"); + } + }); + } + })); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "b"); - put("comment", collectionValue( - new ArrayList<>() {{ - add(new LinkedHashMap<>() {{ - put("data", "1"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "2"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "3"); - }} - ); - }} - )); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "b"); + put( + "comment", + collectionValue( + new ArrayList<>() { + { + add( + new LinkedHashMap<>() { + { + put("data", "1"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "2"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "3"); + } + }); + } + })); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "c"); - put("comment", collectionValue( - new ArrayList<>() {{ - add(new LinkedHashMap<>() {{ - put("data", "1"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "2"); - }} - ); - add(new LinkedHashMap<>() {{ - put("data", "3"); - }} - ); - }} - )); - }} - ) - ) - ); + new LinkedHashMap<>() { + { + put("message.info", "c"); + put( + "comment", + collectionValue( + new ArrayList<>() { + { + add( + new LinkedHashMap<>() { + { + put("data", "1"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "2"); + } + }); + add( + new LinkedHashMap<>() { + { + put("data", "3"); + } + }); + } + })); + } + }))); } @Test public void nested_two_nested_field() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(testData); + when(inputPlan.next()).thenReturn(testData); List> fields = List.of( @@ -177,76 +175,81 @@ public void nested_two_nested_field() { "path", new ReferenceExpression("message", STRING)), Map.of( "field", new ReferenceExpression("comment.data", STRING), - "path", new ReferenceExpression("comment", STRING)) - ); + "path", new ReferenceExpression("comment", STRING))); var nested = new NestedOperator(inputPlan, fields); assertThat( execute(nested), contains( tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "a"); - put("comment.data", "1"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "a"); + put("comment.data", "1"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "a"); - put("comment.data", "2"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "a"); + put("comment.data", "2"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "a"); - put("comment.data", "3"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "a"); + put("comment.data", "3"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "b"); - put("comment.data", "1"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "b"); + put("comment.data", "1"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "b"); - put("comment.data", "2"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "b"); + put("comment.data", "2"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "b"); - put("comment.data", "3"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "b"); + put("comment.data", "3"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "c"); - put("comment.data", "1"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "c"); + put("comment.data", "1"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "c"); - put("comment.data", "2"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "c"); + put("comment.data", "2"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "c"); - put("comment.data", "3"); - }} - ) - ) - ); + new LinkedHashMap<>() { + { + put("message.info", "c"); + put("comment.data", "3"); + } + }))); } @Test public void nested_two_nested_fields_with_same_path() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(testDataWithSamePath); + when(inputPlan.next()).thenReturn(testDataWithSamePath); List> fields = List.of( @@ -255,85 +258,69 @@ public void nested_two_nested_fields_with_same_path() { "path", new ReferenceExpression("message", STRING)), Map.of( "field", new ReferenceExpression("message.id", STRING), - "path", new ReferenceExpression("message", STRING)) - ); + "path", new ReferenceExpression("message", STRING))); var nested = new NestedOperator(inputPlan, fields); assertThat( execute(nested), contains( tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "a"); - put("message.id", "1"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "a"); + put("message.id", "1"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "b"); - put("message.id", "2"); - }} - ), + new LinkedHashMap<>() { + { + put("message.info", "b"); + put("message.id", "2"); + } + }), tupleValue( - new LinkedHashMap<>() {{ - put("message.info", "c"); - put("message.id", "3"); - }} - ) - ) - ); + new LinkedHashMap<>() { + { + put("message.info", "c"); + put("message.id", "3"); + } + }))); } @Test public void non_nested_field_tests() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(nonNestedTestData); + when(inputPlan.next()).thenReturn(nonNestedTestData); Set fields = Set.of("message"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.info")); + Map> groupedFieldsByPath = Map.of("message", List.of("message.info")); var nested = new NestedOperator(inputPlan, fields, groupedFieldsByPath); assertThat( - execute(nested), - contains( - tupleValue(new LinkedHashMap<>(Map.of("message", "val"))) - ) - ); + execute(nested), contains(tupleValue(new LinkedHashMap<>(Map.of("message", "val"))))); } @Test public void nested_missing_tuple_field() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(tupleValue(Map.of())); + when(inputPlan.next()).thenReturn(tupleValue(Map.of())); Set fields = Set.of("message.val"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.val")); + Map> groupedFieldsByPath = Map.of("message", List.of("message.val")); var nested = new NestedOperator(inputPlan, fields, groupedFieldsByPath); assertThat( execute(nested), - contains( - tupleValue(new LinkedHashMap<>(Map.of("message.val", ExprNullValue.of()))) - ) - ); + contains(tupleValue(new LinkedHashMap<>(Map.of("message.val", ExprNullValue.of()))))); } @Test public void nested_missing_array_field() { when(inputPlan.hasNext()).thenReturn(true, false); - when(inputPlan.next()) - .thenReturn(missingArrayData); + when(inputPlan.next()).thenReturn(missingArrayData); Set fields = Set.of("missing.data"); - Map> groupedFieldsByPath = - Map.of("message", List.of("message.data")); + Map> groupedFieldsByPath = Map.of("message", List.of("message.data")); var nested = new NestedOperator(inputPlan, fields, groupedFieldsByPath); - assertEquals(0, execute(nested) - .get(0) - .tupleValue() - .size()); + assertEquals(0, execute(nested).get(0).tupleValue().size()); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanNodeVisitorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanNodeVisitorTest.java index 8ed4881d333..c91ae8787ca 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanNodeVisitorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanNodeVisitorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static java.util.Collections.emptyList; @@ -49,16 +48,12 @@ import org.opensearch.sql.expression.ReferenceExpression; import org.opensearch.sql.expression.window.WindowDefinition; -/** - * Todo, testing purpose, delete later. - */ +/** Todo, testing purpose, delete later. */ @ExtendWith(MockitoExtension.class) @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class PhysicalPlanNodeVisitorTest extends PhysicalPlanTestBase { - @Mock - PhysicalPlan plan; - @Mock - ReferenceExpression ref; + @Mock PhysicalPlan plan; + @Mock ReferenceExpression ref; @Test public void print_physical_plan() { @@ -69,16 +64,13 @@ public void print_physical_plan() { agg( rareTopN( filter( - limit( - new TestScan(), - 1, 1 - ), + limit(new TestScan(), 1, 1), DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10))), CommandType.TOP, ImmutableList.of(), DSL.ref("response", INTEGER)), - ImmutableList - .of(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), + ImmutableList.of( + DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), ImmutableList.of()), ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE))), named("ref", ref)), @@ -104,17 +96,20 @@ public static Stream getPhysicalPlanForTest() { filter(new TestScan(), DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10))); PhysicalPlan aggregation = - agg(filter, ImmutableList.of(DSL.named("avg(response)", - DSL.avg(DSL.ref("response", INTEGER)))), ImmutableList.of()); + agg( + filter, + ImmutableList.of(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), + ImmutableList.of()); PhysicalPlan rename = - rename(aggregation, ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", - DOUBLE))); + rename( + aggregation, + ImmutableMap.of(DSL.ref("ivalue", INTEGER), DSL.ref("avg(response)", DOUBLE))); PhysicalPlan project = project(plan, named("ref", ref)); - PhysicalPlan window = window(plan, named(DSL.rowNumber()), - new WindowDefinition(emptyList(), emptyList())); + PhysicalPlan window = + window(plan, named(DSL.rowNumber()), new WindowDefinition(emptyList(), emptyList())); PhysicalPlan remove = remove(plan, ref); @@ -136,26 +131,33 @@ public static Stream getPhysicalPlanForTest() { PhysicalPlan cursorClose = new CursorCloseOperator(plan); - return Stream.of(Arguments.of(filter, "filter"), Arguments.of(aggregation, "aggregation"), - Arguments.of(rename, "rename"), Arguments.of(project, "project"), - Arguments.of(window, "window"), Arguments.of(remove, "remove"), - Arguments.of(eval, "eval"), Arguments.of(sort, "sort"), Arguments.of(dedupe, "dedupe"), - Arguments.of(values, "values"), Arguments.of(rareTopN, "rareTopN"), - Arguments.of(limit, "limit"), Arguments.of(nested, "nested"), + return Stream.of( + Arguments.of(filter, "filter"), + Arguments.of(aggregation, "aggregation"), + Arguments.of(rename, "rename"), + Arguments.of(project, "project"), + Arguments.of(window, "window"), + Arguments.of(remove, "remove"), + Arguments.of(eval, "eval"), + Arguments.of(sort, "sort"), + Arguments.of(dedupe, "dedupe"), + Arguments.of(values, "values"), + Arguments.of(rareTopN, "rareTopN"), + Arguments.of(limit, "limit"), + Arguments.of(nested, "nested"), Arguments.of(cursorClose, "cursorClose")); } @ParameterizedTest(name = "{1}") @MethodSource("getPhysicalPlanForTest") public void test_PhysicalPlanVisitor_should_return_null(PhysicalPlan plan, String name) { - assertNull(plan.accept(new PhysicalPlanNodeVisitor() { - }, null)); + assertNull(plan.accept(new PhysicalPlanNodeVisitor() {}, null)); } @Test public void test_visitMLCommons() { PhysicalPlanNodeVisitor physicalPlanNodeVisitor = - new PhysicalPlanNodeVisitor() {}; + new PhysicalPlanNodeVisitor() {}; assertNull(physicalPlanNodeVisitor.visitMLCommons(plan, null)); } @@ -163,7 +165,7 @@ public void test_visitMLCommons() { @Test public void test_visitAD() { PhysicalPlanNodeVisitor physicalPlanNodeVisitor = - new PhysicalPlanNodeVisitor() {}; + new PhysicalPlanNodeVisitor() {}; assertNull(physicalPlanNodeVisitor.visitAD(plan, null)); } @@ -171,7 +173,7 @@ public void test_visitAD() { @Test public void test_visitML() { PhysicalPlanNodeVisitor physicalPlanNodeVisitor = - new PhysicalPlanNodeVisitor() {}; + new PhysicalPlanNodeVisitor() {}; assertNull(physicalPlanNodeVisitor.visitML(plan, null)); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTest.java index ab3f0ef36d5..d63ab357738 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTest.java @@ -5,15 +5,7 @@ package org.opensearch.sql.planner.physical; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.CALLS_REAL_METHODS; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; @@ -28,33 +20,32 @@ @ExtendWith(MockitoExtension.class) @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class PhysicalPlanTest { - @Mock - Split split; - - @Mock - PhysicalPlan child; - - private PhysicalPlan testPlan = new PhysicalPlan() { - @Override - public R accept(PhysicalPlanNodeVisitor visitor, C context) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasNext() { - throw new UnsupportedOperationException(); - } - - @Override - public ExprValue next() { - throw new UnsupportedOperationException(); - } - - @Override - public List getChild() { - return List.of(child); - } - }; + @Mock Split split; + + @Mock PhysicalPlan child; + + private PhysicalPlan testPlan = + new PhysicalPlan() { + @Override + public R accept(PhysicalPlanNodeVisitor visitor, C context) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasNext() { + throw new UnsupportedOperationException(); + } + + @Override + public ExprValue next() { + throw new UnsupportedOperationException(); + } + + @Override + public List getChild() { + return List.of(child); + } + }; @Test void add_split_to_child_by_default() { diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java index 60d97f159ac..003e59959f0 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import com.google.common.collect.ImmutableList; @@ -29,47 +28,71 @@ public class PhysicalPlanTestBase { - protected static final List countTestInputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 1, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 2, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 3, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 4, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 5, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 6, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 7, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 8, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 9, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 10, "testString", "asdf"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("id", 11, "testString", "asdf"))) + protected static final List countTestInputs = + new ImmutableList.Builder() + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 1, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 2, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 3, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 4, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 5, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 6, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 7, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 8, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 9, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 10, "testString", "asdf"))) + .add(ExprValueUtils.tupleValue(ImmutableMap.of("id", 11, "testString", "asdf"))) .build(); - protected static final List inputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("ip", "209.160.24.63", "action", "GET", "response", 200, "referer", - "www.amazon.com"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("ip", "209.160.24.63", "action", "GET", "response", 404, "referer", - "www.amazon.com"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("ip", "112.111.162.4", "action", "GET", "response", 200, "referer", - "www.amazon.com"))) - .add(ExprValueUtils.tupleValue(ImmutableMap - .of("ip", "74.125.19.106", "action", "POST", "response", 200, "referer", - "www.google.com"))) - .add(ExprValueUtils - .tupleValue(ImmutableMap.of("ip", "74.125.19.106", "action", "POST", "response", 500))) - .build(); + protected static final List inputs = + new ImmutableList.Builder() + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com"))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com"))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "112.111.162.4", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com"))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "ip", + "74.125.19.106", + "action", + "POST", + "response", + 200, + "referer", + "www.google.com"))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of("ip", "74.125.19.106", "action", "POST", "response", 500))) + .build(); private static Map typeMapping = new ImmutableMap.Builder() @@ -79,112 +102,172 @@ public class PhysicalPlanTestBase { .put("referer", ExprCoreType.STRING) .build(); - protected static final List dateInputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-03"), - "month", new ExprDateValue("2021-02-04"), - "quarter", new ExprDatetimeValue("2021-01-01 12:25:02"), - "year", new ExprTimestampValue("2013-01-01 12:25:02")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-01"), - "month", new ExprDateValue("2021-03-17"), - "quarter", new ExprDatetimeValue("2021-05-17 12:25:01"), - "year", new ExprTimestampValue("2021-01-01 12:25:02")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-04"), - "month", new ExprDateValue("2021-02-08"), - "quarter", new ExprDatetimeValue("2021-06-08 12:25:02"), - "year", new ExprTimestampValue("2016-01-01 12:25:02")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-02"), - "month", new ExprDateValue("2020-12-12"), - "quarter", new ExprDatetimeValue("2020-12-12 12:25:03"), - "year", new ExprTimestampValue("1999-01-01 12:25:02")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-01"), - "month", new ExprDateValue("2021-02-28"), - "quarter", new ExprDatetimeValue("2020-09-28 12:25:01"), - "year", new ExprTimestampValue("2018-01-01 12:25:02")))) - .build(); + protected static final List dateInputs = + new ImmutableList.Builder() + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", new ExprDateValue("2021-01-03"), + "month", new ExprDateValue("2021-02-04"), + "quarter", new ExprDatetimeValue("2021-01-01 12:25:02"), + "year", new ExprTimestampValue("2013-01-01 12:25:02")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", new ExprDateValue("2021-01-01"), + "month", new ExprDateValue("2021-03-17"), + "quarter", new ExprDatetimeValue("2021-05-17 12:25:01"), + "year", new ExprTimestampValue("2021-01-01 12:25:02")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", new ExprDateValue("2021-01-04"), + "month", new ExprDateValue("2021-02-08"), + "quarter", new ExprDatetimeValue("2021-06-08 12:25:02"), + "year", new ExprTimestampValue("2016-01-01 12:25:02")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", new ExprDateValue("2021-01-02"), + "month", new ExprDateValue("2020-12-12"), + "quarter", new ExprDatetimeValue("2020-12-12 12:25:03"), + "year", new ExprTimestampValue("1999-01-01 12:25:02")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", new ExprDateValue("2021-01-01"), + "month", new ExprDateValue("2021-02-28"), + "quarter", new ExprDatetimeValue("2020-09-28 12:25:01"), + "year", new ExprTimestampValue("2018-01-01 12:25:02")))) + .build(); - protected static final List datetimeInputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "hour", new ExprTimeValue("17:17:00"), - "minute", new ExprDatetimeValue("2020-12-31 23:54:12"), - "second", new ExprTimestampValue("2021-01-01 00:00:05")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "hour", new ExprTimeValue("18:17:00"), - "minute", new ExprDatetimeValue("2021-01-01 00:05:12"), - "second", new ExprTimestampValue("2021-01-01 00:00:12")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "hour", new ExprTimeValue("17:15:00"), - "minute", new ExprDatetimeValue("2021-01-01 00:03:12"), - "second", new ExprTimestampValue("2021-01-01 00:00:17")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "hour", new ExprTimeValue("19:01:00"), - "minute", new ExprDatetimeValue("2021-01-01 00:02:12"), - "second", new ExprTimestampValue("2021-01-01 00:00:03")))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "hour", new ExprTimeValue("18:50:00"), - "minute", new ExprDatetimeValue("2021-01-01 00:00:12"), - "second", new ExprTimestampValue("2021-01-01 00:00:13")))) - .build(); + protected static final List datetimeInputs = + new ImmutableList.Builder() + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "hour", new ExprTimeValue("17:17:00"), + "minute", new ExprDatetimeValue("2020-12-31 23:54:12"), + "second", new ExprTimestampValue("2021-01-01 00:00:05")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "hour", new ExprTimeValue("18:17:00"), + "minute", new ExprDatetimeValue("2021-01-01 00:05:12"), + "second", new ExprTimestampValue("2021-01-01 00:00:12")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "hour", new ExprTimeValue("17:15:00"), + "minute", new ExprDatetimeValue("2021-01-01 00:03:12"), + "second", new ExprTimestampValue("2021-01-01 00:00:17")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "hour", new ExprTimeValue("19:01:00"), + "minute", new ExprDatetimeValue("2021-01-01 00:02:12"), + "second", new ExprTimestampValue("2021-01-01 00:00:03")))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "hour", new ExprTimeValue("18:50:00"), + "minute", new ExprDatetimeValue("2021-01-01 00:00:12"), + "second", new ExprTimestampValue("2021-01-01 00:00:13")))) + .build(); - protected static final List numericInputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "integer", 2, - "long", 2L, - "float", 2F, - "double", 2D))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "integer", 1, - "long", 1L, - "float", 1F, - "double", 1D))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "integer", 5, - "long", 5L, - "float", 5F, - "double", 5D))) - .build(); + protected static final List numericInputs = + new ImmutableList.Builder() + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of("integer", 2, "long", 2L, "float", 2F, "double", 2D))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of("integer", 1, "long", 1L, "float", 1F, "double", 1D))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of("integer", 5, "long", 5L, "float", 5F, "double", 5D))) + .build(); - protected static final List compoundInputs = new ImmutableList.Builder() - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-03"), - "region", "iad", - "host", "h1", - "errors", 2))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-03"), - "region", "iad", - "host", "h2", - "errors", 3))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-04"), - "region", "iad", - "host", "h1", - "errors", 1))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-04"), - "region", "iad", - "host", "h2", - "errors", 10))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-06"), - "region", "iad", - "host", "h1", - "errors", 1))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-07"), - "region", "iad", - "host", "h1", - "errors", 6))) - .add(ExprValueUtils.tupleValue(ImmutableMap.of( - "day", new ExprDateValue("2021-01-07"), - "region", "iad", - "host", "h2", - "errors", 8))) - .build(); + protected static final List compoundInputs = + new ImmutableList.Builder() + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-03"), + "region", + "iad", + "host", + "h1", + "errors", + 2))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-03"), + "region", + "iad", + "host", + "h2", + "errors", + 3))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-04"), + "region", + "iad", + "host", + "h1", + "errors", + 1))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-04"), + "region", + "iad", + "host", + "h2", + "errors", + 10))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-06"), + "region", + "iad", + "host", + "h1", + "errors", + 1))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-07"), + "region", + "iad", + "host", + "h1", + "errors", + 6))) + .add( + ExprValueUtils.tupleValue( + ImmutableMap.of( + "day", + new ExprDateValue("2021-01-07"), + "region", + "iad", + "host", + "h2", + "errors", + 8))) + .build(); protected Environment typeEnv() { return var -> { @@ -244,12 +327,10 @@ public ExprValue next() { } @Override - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - } + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} @Override - public void writeExternal(ObjectOutput out) throws IOException { - } + public void writeExternal(ObjectOutput out) throws IOException {} public boolean equals(final Object o) { return o == this || o.hashCode() == hashCode(); diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/ProjectOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/ProjectOperatorTest.java index f5ecf76bd09..ded8605cf05 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/ProjectOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/ProjectOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -64,9 +63,11 @@ public void project_two_field_follow_the_project_order() { when(inputPlan.hasNext()).thenReturn(true, false); when(inputPlan.next()) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200))); - PhysicalPlan plan = project(inputPlan, - DSL.named("response", DSL.ref("response", INTEGER)), - DSL.named("action", DSL.ref("action", STRING))); + PhysicalPlan plan = + project( + inputPlan, + DSL.named("response", DSL.ref("response", INTEGER)), + DSL.named("action", DSL.ref("action", STRING))); List result = execute(plan); assertThat( @@ -83,9 +84,11 @@ public void project_keep_missing_value() { when(inputPlan.next()) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200))) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST"))); - PhysicalPlan plan = project(inputPlan, - DSL.named("response", DSL.ref("response", INTEGER)), - DSL.named("action", DSL.ref("action", STRING))); + PhysicalPlan plan = + project( + inputPlan, + DSL.named("response", DSL.ref("response", INTEGER)), + DSL.named("action", DSL.ref("action", STRING))); List result = execute(plan); assertThat( @@ -94,21 +97,23 @@ public void project_keep_missing_value() { iterableWithSize(2), hasItems( ExprValueUtils.tupleValue(ImmutableMap.of("response", 200, "action", "GET")), - ExprTupleValue.fromExprValueMap(ImmutableMap.of("response", - LITERAL_MISSING, - "action", stringValue("POST")))))); + ExprTupleValue.fromExprValueMap( + ImmutableMap.of("response", LITERAL_MISSING, "action", stringValue("POST")))))); } @Test public void project_schema() { - PhysicalPlan project = project(inputPlan, - DSL.named("response", DSL.ref("response", INTEGER)), - DSL.named("action", DSL.ref("action", STRING), "act")); - - assertThat(project.schema().getColumns(), contains( - new ExecutionEngine.Schema.Column("response", null, INTEGER), - new ExecutionEngine.Schema.Column("action", "act", STRING) - )); + PhysicalPlan project = + project( + inputPlan, + DSL.named("response", DSL.ref("response", INTEGER)), + DSL.named("action", DSL.ref("action", STRING), "act")); + + assertThat( + project.schema().getColumns(), + contains( + new ExecutionEngine.Schema.Column("response", null, INTEGER), + new ExecutionEngine.Schema.Column("action", "act", STRING))); } @Test @@ -117,16 +122,24 @@ public void project_fields_with_parse_expressions() { when(inputPlan.next()) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("response", "GET 200"))); PhysicalPlan plan = - project(inputPlan, ImmutableList.of(DSL.named("action", DSL.ref("action", STRING)), + project( + inputPlan, + ImmutableList.of( + DSL.named("action", DSL.ref("action", STRING)), DSL.named("response", DSL.ref("response", STRING))), - ImmutableList.of(DSL.named("action", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("action"))), DSL.named("response", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("response")))) - ); + ImmutableList.of( + DSL.named( + "action", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("action"))), + DSL.named( + "response", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("response"))))); List result = execute(plan); assertThat( @@ -143,20 +156,23 @@ public void project_fields_with_unused_parse_expressions() { when(inputPlan.next()) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("response", "GET 200"))); PhysicalPlan plan = - project(inputPlan, ImmutableList.of(DSL.named("response", DSL.ref("response", STRING))), - ImmutableList.of(DSL.named("ignored", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("ignored")))) - ); + project( + inputPlan, + ImmutableList.of(DSL.named("response", DSL.ref("response", STRING))), + ImmutableList.of( + DSL.named( + "ignored", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("ignored"))))); List result = execute(plan); assertThat( result, allOf( iterableWithSize(1), - hasItems( - ExprValueUtils.tupleValue(ImmutableMap.of("response", "GET 200"))))); + hasItems(ExprValueUtils.tupleValue(ImmutableMap.of("response", "GET 200"))))); } @Test @@ -166,14 +182,19 @@ public void project_fields_with_parse_expressions_and_runtime_fields() { .thenReturn( ExprValueUtils.tupleValue(ImmutableMap.of("response", "GET 200", "eval_field", 1))); PhysicalPlan plan = - project(inputPlan, ImmutableList.of(DSL.named("response", DSL.ref("response", STRING)), + project( + inputPlan, + ImmutableList.of( + DSL.named("response", DSL.ref("response", STRING)), DSL.named("action", DSL.ref("action", STRING)), DSL.named("eval_field", DSL.ref("eval_field", INTEGER))), - ImmutableList.of(DSL.named("action", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("action")))) - ); + ImmutableList.of( + DSL.named( + "action", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("action"))))); List result = execute(plan); assertThat( @@ -193,16 +214,24 @@ public void project_parse_missing_will_fallback() { ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", "GET 200"))) .thenReturn(ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST"))); PhysicalPlan plan = - project(inputPlan, ImmutableList.of(DSL.named("action", DSL.ref("action", STRING)), + project( + inputPlan, + ImmutableList.of( + DSL.named("action", DSL.ref("action", STRING)), DSL.named("response", DSL.ref("response", STRING))), - ImmutableList.of(DSL.named("action", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("action"))), DSL.named("response", - DSL.regex(DSL.ref("response", STRING), - DSL.literal("(?\\w+) (?\\d+)"), - DSL.literal("response")))) - ); + ImmutableList.of( + DSL.named( + "action", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("action"))), + DSL.named( + "response", + DSL.regex( + DSL.ref("response", STRING), + DSL.literal("(?\\w+) (?\\d+)"), + DSL.literal("response"))))); List result = execute(plan); assertThat( @@ -225,8 +254,8 @@ public void serializable() { objectOutput.writeObject(project); objectOutput.flush(); - ObjectInputStream objectInput = new ObjectInputStream( - new ByteArrayInputStream(output.toByteArray())); + ObjectInputStream objectInput = + new ObjectInputStream(new ByteArrayInputStream(output.toByteArray())); var roundTripPlan = (ProjectOperator) objectInput.readObject(); assertEquals(project, roundTripPlan); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/RareTopNOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/RareTopNOperatorTest.java index 5a77bf7f66c..e252c41d1fa 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/RareTopNOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/RareTopNOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -24,74 +23,87 @@ public class RareTopNOperatorTest extends PhysicalPlanTestBase { @Test public void rare_without_group() { - PhysicalPlan plan = new RareTopNOperator(new TestScan(), - CommandType.RARE, - Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), - Collections.emptyList()); + PhysicalPlan plan = + new RareTopNOperator( + new TestScan(), + CommandType.RARE, + Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), + Collections.emptyList()); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST")), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST")), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")))); } @Test public void rare_with_group() { - PhysicalPlan plan = new RareTopNOperator(new TestScan(), - CommandType.RARE, - Collections.singletonList(DSL.ref("response", ExprCoreType.INTEGER)), - Collections.singletonList(DSL.ref("action", ExprCoreType.STRING))); + PhysicalPlan plan = + new RareTopNOperator( + new TestScan(), + CommandType.RARE, + Collections.singletonList(DSL.ref("response", ExprCoreType.INTEGER)), + Collections.singletonList(DSL.ref("action", ExprCoreType.STRING))); List result = execute(plan); assertEquals(4, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 200)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 500)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 404)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 200)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 500)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 404)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200)))); } @Test public void top_without_group() { - PhysicalPlan plan = new RareTopNOperator(new TestScan(), - CommandType.TOP, - Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), - Collections.emptyList()); + PhysicalPlan plan = + new RareTopNOperator( + new TestScan(), + CommandType.TOP, + Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), + Collections.emptyList()); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST")) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST")))); } @Test public void top_n_without_group() { - PhysicalPlan plan = new RareTopNOperator(new TestScan(), - CommandType.TOP, - 1, - Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), - Collections.emptyList()); + PhysicalPlan plan = + new RareTopNOperator( + new TestScan(), + CommandType.TOP, + 1, + Collections.singletonList(DSL.ref("action", ExprCoreType.STRING)), + Collections.emptyList()); List result = execute(plan); assertEquals(1, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")) - )); + assertThat( + result, containsInAnyOrder(ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET")))); } @Test public void top_n_with_group() { - PhysicalPlan plan = new RareTopNOperator(new TestScan(), - CommandType.TOP, - 1, - Collections.singletonList(DSL.ref("response", ExprCoreType.INTEGER)), - Collections.singletonList(DSL.ref("action", ExprCoreType.STRING))); + PhysicalPlan plan = + new RareTopNOperator( + new TestScan(), + CommandType.TOP, + 1, + Collections.singletonList(DSL.ref("response", ExprCoreType.INTEGER)), + Collections.singletonList(DSL.ref("action", ExprCoreType.STRING))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 200)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "response", 200)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "response", 200)))); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/RemoveOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/RemoveOperatorTest.java index ec950e6016b..6ba5d3fba85 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/RemoveOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/RemoveOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -29,8 +28,7 @@ @ExtendWith(MockitoExtension.class) class RemoveOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; @Test public void remove_one_field() { diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/RenameOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/RenameOperatorTest.java index a0da659a382..807d8b8836b 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/RenameOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/RenameOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,34 +26,34 @@ @ExtendWith(MockitoExtension.class) public class RenameOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; @Test public void avg_aggregation_rename() { - PhysicalPlan plan = new RenameOperator( - new AggregationOperator(new TestScan(), - Collections - .singletonList(DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), - Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))), - ImmutableMap.of(DSL.ref("avg(response)", DOUBLE), DSL.ref("avg", DOUBLE)) - ); + PhysicalPlan plan = + new RenameOperator( + new AggregationOperator( + new TestScan(), + Collections.singletonList( + DSL.named("avg(response)", DSL.avg(DSL.ref("response", INTEGER)))), + Collections.singletonList(DSL.named("action", DSL.ref("action", STRING)))), + ImmutableMap.of(DSL.ref("avg(response)", DOUBLE), DSL.ref("avg", DOUBLE))); List result = execute(plan); assertEquals(2, result.size()); - assertThat(result, containsInAnyOrder( - ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "avg", 268d)), - ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "avg", 350d)) - )); + assertThat( + result, + containsInAnyOrder( + ExprValueUtils.tupleValue(ImmutableMap.of("action", "GET", "avg", 268d)), + ExprValueUtils.tupleValue(ImmutableMap.of("action", "POST", "avg", 350d)))); } @Test public void rename_int_value() { when(inputPlan.hasNext()).thenReturn(true, false); when(inputPlan.next()).thenReturn(ExprValueUtils.integerValue(1)); - PhysicalPlan plan = new RenameOperator( - inputPlan, - ImmutableMap.of(DSL.ref("avg(response)", DOUBLE), DSL.ref("avg", DOUBLE)) - ); + PhysicalPlan plan = + new RenameOperator( + inputPlan, ImmutableMap.of(DSL.ref("avg(response)", DOUBLE), DSL.ref("avg", DOUBLE))); List result = execute(plan); assertEquals(1, result.size()); assertThat(result, containsInAnyOrder(ExprValueUtils.integerValue(1))); diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/SortOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/SortOperatorTest.java index ef9bdfbca4c..f9a44be3abb 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/SortOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/SortOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,8 +26,7 @@ @ExtendWith(MockitoExtension.class) class SortOperatorTest extends PhysicalPlanTestBase { - @Mock - private PhysicalPlan inputPlan; + @Mock private PhysicalPlan inputPlan; /** * construct the map which contain null value, because {@link ImmutableMap} doesn't support null @@ -264,7 +262,6 @@ public void sort_one_field_without_input() { assertEquals( 0, - execute(sort(inputPlan, - Pair.of(SortOption.DEFAULT_ASC, ref("response", INTEGER)))).size()); + execute(sort(inputPlan, Pair.of(SortOption.DEFAULT_ASC, ref("response", INTEGER)))).size()); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/ValuesOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/ValuesOperatorTest.java index 9acab03d2bd..ddf4014b7e0 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/ValuesOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/ValuesOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.hamcrest.MatcherAssert.assertThat; @@ -26,10 +25,7 @@ class ValuesOperatorTest { @Test public void shouldHaveNoChild() { ValuesOperator values = values(ImmutableList.of(literal(1))); - assertThat( - values.getChild(), - is(empty()) - ); + assertThat(values.getChild(), is(empty())); } @Test @@ -40,10 +36,6 @@ public void iterateSingleRow() { results.add(values.next()); } - assertThat( - results, - contains(collectionValue(Arrays.asList(1, "abc"))) - ); + assertThat(results, contains(collectionValue(Arrays.asList(1, "abc")))); } - } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/WindowOperatorTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/WindowOperatorTest.java index 7f727d391b6..3bd145ff91c 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/WindowOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/WindowOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.planner.physical; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -43,21 +42,56 @@ void test_ranking_window_function() { window(DSL.rank()) .partitionBy(ref("action", STRING)) .sortBy(DEFAULT_ASC, ref("response", INTEGER)) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 200, "referer", "www.amazon.com", - "rank()", 1)) - .expectNext(ImmutableMap.of( - "ip", "112.111.162.4", "action", "GET", "response", 200, "referer", "www.amazon.com", - "rank()", 1)) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 404, "referer", "www.amazon.com", - "rank()", 3)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 200, "referer", "www.google.com", - "rank()", 1)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 500, - "rank()", 2)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "rank()", + 1)) + .expectNext( + ImmutableMap.of( + "ip", + "112.111.162.4", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "rank()", + 1)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com", + "rank()", + 3)) + .expectNext( + ImmutableMap.of( + "ip", + "74.125.19.106", + "action", + "POST", + "response", + 200, + "referer", + "www.google.com", + "rank()", + 1)) + .expectNext( + ImmutableMap.of("ip", "74.125.19.106", "action", "POST", "response", 500, "rank()", 2)) .done(); } @@ -67,21 +101,57 @@ void test_aggregate_window_function() { window(new AggregateWindowFunction(DSL.sum(ref("response", INTEGER)))) .partitionBy(ref("action", STRING)) .sortBy(DEFAULT_ASC, ref("response", INTEGER)) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 200, "referer", "www.amazon.com", - "sum(response)", 400)) - .expectNext(ImmutableMap.of( - "ip", "112.111.162.4", "action", "GET", "response", 200, "referer", "www.amazon.com", - "sum(response)", 400)) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 404, "referer", "www.amazon.com", - "sum(response)", 804)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 200, "referer", "www.google.com", - "sum(response)", 200)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 500, - "sum(response)", 700)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "sum(response)", + 400)) + .expectNext( + ImmutableMap.of( + "ip", + "112.111.162.4", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "sum(response)", + 400)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com", + "sum(response)", + 804)) + .expectNext( + ImmutableMap.of( + "ip", + "74.125.19.106", + "action", + "POST", + "response", + 200, + "referer", + "www.google.com", + "sum(response)", + 200)) + .expectNext( + ImmutableMap.of( + "ip", "74.125.19.106", "action", "POST", "response", 500, "sum(response)", 700)) .done(); } @@ -89,21 +159,57 @@ void test_aggregate_window_function() { @Test void test_aggregate_window_function_without_sort_key() { window(new AggregateWindowFunction(DSL.sum(ref("response", INTEGER)))) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 200, "referer", "www.amazon.com", - "sum(response)", 1504)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 500, - "sum(response)", 1504)) - .expectNext(ImmutableMap.of( - "ip", "74.125.19.106", "action", "POST", "response", 200, "referer", "www.google.com", - "sum(response)", 1504)) - .expectNext(ImmutableMap.of( - "ip", "112.111.162.4", "action", "GET", "response", 200, "referer", "www.amazon.com", - "sum(response)", 1504)) - .expectNext(ImmutableMap.of( - "ip", "209.160.24.63", "action", "GET", "response", 404, "referer", "www.amazon.com", - "sum(response)", 1504)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "sum(response)", + 1504)) + .expectNext( + ImmutableMap.of( + "ip", "74.125.19.106", "action", "POST", "response", 500, "sum(response)", 1504)) + .expectNext( + ImmutableMap.of( + "ip", + "74.125.19.106", + "action", + "POST", + "response", + 200, + "referer", + "www.google.com", + "sum(response)", + 1504)) + .expectNext( + ImmutableMap.of( + "ip", + "112.111.162.4", + "action", + "GET", + "response", + 200, + "referer", + "www.amazon.com", + "sum(response)", + 1504)) + .expectNext( + ImmutableMap.of( + "ip", + "209.160.24.63", + "action", + "GET", + "response", + 404, + "referer", + "www.amazon.com", + "sum(response)", + 1504)) .done(); } @@ -136,10 +242,11 @@ WindowOperatorAssertion sortBy(SortOption option, Expression expr) { WindowOperatorAssertion expectNext(Map expected) { if (windowOperator == null) { WindowDefinition definition = new WindowDefinition(partitionByList, sortList); - windowOperator = new WindowOperator( - new SortOperator(new TestScan(), definition.getAllSortItems()), - windowFunction, - definition); + windowOperator = + new WindowOperator( + new SortOperator(new TestScan(), definition.getAllSortItems()), + windowFunction, + definition); windowOperator.open(); } @@ -153,7 +260,5 @@ void done() { assertFalse(windowOperator.hasNext()); windowOperator.close(); } - } - } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/collector/RoundingTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/collector/RoundingTest.java index f40e5c058b1..3a2601a874d 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/collector/RoundingTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/collector/RoundingTest.java @@ -22,8 +22,8 @@ public class RoundingTest { void time_rounding_illegal_span() { SpanExpression span = DSL.span(DSL.ref("time", TIME), DSL.literal(1), "d"); Rounding rounding = Rounding.createRounding(span); - assertThrows(ExpressionEvaluationException.class, - () -> rounding.round(new ExprTimeValue("23:30:00"))); + assertThrows( + ExpressionEvaluationException.class, () -> rounding.round(new ExprTimeValue("23:30:00"))); } @Test @@ -36,7 +36,8 @@ void round_unknown_type() { @Test void resolve() { String illegalUnit = "illegal"; - assertThrows(IllegalArgumentException.class, + assertThrows( + IllegalArgumentException.class, () -> Rounding.DateTimeUnit.resolve(illegalUnit), "Unable to resolve unit " + illegalUnit); } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableScanTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableScanTest.java index 93c02def86c..28851f2454c 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableScanTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableScanTest.java @@ -35,11 +35,9 @@ @ExtendWith(MockitoExtension.class) public class DataSourceTableScanTest { - @Mock - private DataSourceService dataSourceService; + @Mock private DataSourceService dataSourceService; - @Mock - private StorageEngine storageEngine; + @Mock private StorageEngine storageEngine; private DataSourceTableScan dataSourceTableScan; @@ -58,10 +56,16 @@ void testIterator() { Set dataSourceSet = new HashSet<>(); dataSourceSet.add(new DataSource("prometheus", DataSourceType.PROMETHEUS, storageEngine)); dataSourceSet.add(new DataSource("opensearch", DataSourceType.OPENSEARCH, storageEngine)); - Set dataSourceMetadata = dataSourceSet.stream() - .map(dataSource -> new DataSourceMetadata(dataSource.getName(), - dataSource.getConnectorType(), Collections.emptyList(), ImmutableMap.of())) - .collect(Collectors.toSet()); + Set dataSourceMetadata = + dataSourceSet.stream() + .map( + dataSource -> + new DataSourceMetadata( + dataSource.getName(), + dataSource.getConnectorType(), + Collections.emptyList(), + ImmutableMap.of())) + .collect(Collectors.toSet()); when(dataSourceService.getDataSourceMetadata(true)).thenReturn(dataSourceMetadata); assertFalse(dataSourceTableScan.hasNext()); @@ -74,11 +78,14 @@ void testIterator() { Set expectedExprTupleValues = new HashSet<>(); for (DataSource dataSource : dataSourceSet) { - expectedExprTupleValues.add(new ExprTupleValue(new LinkedHashMap<>(ImmutableMap.of( - "DATASOURCE_NAME", ExprValueUtils.stringValue(dataSource.getName()), - "CONNECTOR_TYPE", ExprValueUtils.stringValue(dataSource.getConnectorType().name()))))); + expectedExprTupleValues.add( + new ExprTupleValue( + new LinkedHashMap<>( + ImmutableMap.of( + "DATASOURCE_NAME", ExprValueUtils.stringValue(dataSource.getName()), + "CONNECTOR_TYPE", + ExprValueUtils.stringValue(dataSource.getConnectorType().name()))))); } assertEquals(expectedExprTupleValues, exprTupleValues); } - } diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableTest.java b/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableTest.java index c82a042770c..75f21c3e52a 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/datasource/DataSourceTableTest.java @@ -27,13 +27,12 @@ @ExtendWith(MockitoExtension.class) public class DataSourceTableTest { - @Mock - private DataSourceService dataSourceService; + @Mock private DataSourceService dataSourceService; @Test void testGetFieldTypes() { DataSourceTable dataSourceTable = new DataSourceTable(dataSourceService); - Map fieldTypes = dataSourceTable.getFieldTypes(); + Map fieldTypes = dataSourceTable.getFieldTypes(); Map expectedTypes = new HashMap<>(); expectedTypes.put("DATASOURCE_NAME", ExprCoreType.STRING); expectedTypes.put("CONNECTOR_TYPE", ExprCoreType.STRING); @@ -43,9 +42,8 @@ void testGetFieldTypes() { @Test void testImplement() { DataSourceTable dataSourceTable = new DataSourceTable(dataSourceService); - PhysicalPlan physicalPlan - = dataSourceTable.implement( - LogicalPlanDSL.relation(DATASOURCES_TABLE_NAME, dataSourceTable)); + PhysicalPlan physicalPlan = + dataSourceTable.implement(LogicalPlanDSL.relation(DATASOURCES_TABLE_NAME, dataSourceTable)); assertTrue(physicalPlan instanceof DataSourceTableScan); } @@ -53,7 +51,8 @@ void testImplement() { @Test void testExist() { UnsupportedOperationException exception = - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> new DataSourceTable(dataSourceService).exists()); assertEquals("Unsupported Operation", exception.getMessage()); } @@ -62,7 +61,8 @@ void testExist() { @Test void testCreateTable() { UnsupportedOperationException exception = - assertThrows(UnsupportedOperationException.class, + assertThrows( + UnsupportedOperationException.class, () -> new DataSourceTable(dataSourceService).create(new HashMap<>())); assertEquals("Unsupported Operation", exception.getMessage()); } diff --git a/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssignerTest.java b/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssignerTest.java index 886537e0889..0c63f594d90 100644 --- a/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssignerTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssignerTest.java @@ -20,33 +20,21 @@ void testAssignWindows() { long slideSize = 500; SlidingWindowAssigner assigner = new SlidingWindowAssigner(windowSize, slideSize); - assertEquals( - List.of( - new Window(0, 1000), - new Window(500, 1500)), - assigner.assign(500)); - - assertEquals( - List.of( - new Window(0, 1000), - new Window(500, 1500)), - assigner.assign(999)); - - assertEquals( - List.of( - new Window(500, 1500), - new Window(1000, 2000)), - assigner.assign(1000)); + assertEquals(List.of(new Window(0, 1000), new Window(500, 1500)), assigner.assign(500)); + + assertEquals(List.of(new Window(0, 1000), new Window(500, 1500)), assigner.assign(999)); + + assertEquals(List.of(new Window(500, 1500), new Window(1000, 2000)), assigner.assign(1000)); } @Test void testConstructWithIllegalArguments() { - IllegalArgumentException error1 = assertThrows(IllegalArgumentException.class, - () -> new SlidingWindowAssigner(-1, 100)); + IllegalArgumentException error1 = + assertThrows(IllegalArgumentException.class, () -> new SlidingWindowAssigner(-1, 100)); assertEquals("Window size [-1] must be positive number", error1.getMessage()); - IllegalArgumentException error2 = assertThrows(IllegalArgumentException.class, - () -> new SlidingWindowAssigner(1000, 0)); + IllegalArgumentException error2 = + assertThrows(IllegalArgumentException.class, () -> new SlidingWindowAssigner(1000, 0)); assertEquals("Slide size [0] must be positive number", error2.getMessage()); } } diff --git a/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/TumblingWindowAssignerTest.java b/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/TumblingWindowAssignerTest.java index 55a8750c117..a8ab0487017 100644 --- a/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/TumblingWindowAssignerTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/streaming/windowing/assigner/TumblingWindowAssignerTest.java @@ -19,21 +19,15 @@ void testAssignWindow() { long windowSize = 1000; TumblingWindowAssigner assigner = new TumblingWindowAssigner(windowSize); - assertEquals( - Collections.singletonList(new Window(0, 1000)), - assigner.assign(500)); - assertEquals( - Collections.singletonList(new Window(1000, 2000)), - assigner.assign(1999)); - assertEquals( - Collections.singletonList(new Window(2000, 3000)), - assigner.assign(2000)); + assertEquals(Collections.singletonList(new Window(0, 1000)), assigner.assign(500)); + assertEquals(Collections.singletonList(new Window(1000, 2000)), assigner.assign(1999)); + assertEquals(Collections.singletonList(new Window(2000, 3000)), assigner.assign(2000)); } @Test void testConstructWithIllegalWindowSize() { - IllegalArgumentException error = assertThrows(IllegalArgumentException.class, - () -> new TumblingWindowAssigner(-1)); + IllegalArgumentException error = + assertThrows(IllegalArgumentException.class, () -> new TumblingWindowAssigner(-1)); assertEquals("Window size [-1] must be positive number", error.getMessage()); } } diff --git a/core/src/test/java/org/opensearch/sql/storage/TableScanOperatorTest.java b/core/src/test/java/org/opensearch/sql/storage/TableScanOperatorTest.java index f2f556a9575..81680455cc5 100644 --- a/core/src/test/java/org/opensearch/sql/storage/TableScanOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/storage/TableScanOperatorTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.storage; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -15,36 +14,40 @@ class TableScanOperatorTest { - private final TableScanOperator tableScan = new TableScanOperator() { - @Override - public String explain() { - return "explain"; - } + private final TableScanOperator tableScan = + new TableScanOperator() { + @Override + public String explain() { + return "explain"; + } - @Override - public boolean hasNext() { - return false; - } + @Override + public boolean hasNext() { + return false; + } - @Override - public ExprValue next() { - return null; - } - }; + @Override + public ExprValue next() { + return null; + } + }; @Test public void accept() { - Boolean isVisited = tableScan.accept(new PhysicalPlanNodeVisitor() { - @Override - protected Boolean visitNode(PhysicalPlan node, Object context) { - return (node instanceof TableScanOperator); - } - - @Override - public Boolean visitTableScan(TableScanOperator node, Object context) { - return super.visitTableScan(node, context); - } - }, null); + Boolean isVisited = + tableScan.accept( + new PhysicalPlanNodeVisitor() { + @Override + protected Boolean visitNode(PhysicalPlan node, Object context) { + return (node instanceof TableScanOperator); + } + + @Override + public Boolean visitTableScan(TableScanOperator node, Object context) { + return super.visitTableScan(node, context); + } + }, + null); assertTrue(isVisited); } @@ -53,5 +56,4 @@ public Boolean visitTableScan(TableScanOperator node, Object context) { public void getChild() { assertTrue(tableScan.getChild().isEmpty()); } - } diff --git a/core/src/test/java/org/opensearch/sql/storage/bindingtuple/BindingTupleTest.java b/core/src/test/java/org/opensearch/sql/storage/bindingtuple/BindingTupleTest.java index 780d1cd2ddb..89d23727072 100644 --- a/core/src/test/java/org/opensearch/sql/storage/bindingtuple/BindingTupleTest.java +++ b/core/src/test/java/org/opensearch/sql/storage/bindingtuple/BindingTupleTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.storage.bindingtuple; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -21,30 +20,31 @@ class BindingTupleTest { public void resolve_ref_expression() { BindingTuple bindingTuple = ExprValueUtils.tupleValue(ImmutableMap.of("ip", "209.160.24.63")).bindingTuples(); - assertEquals(ExprValueUtils.stringValue("209.160.24.63"), - bindingTuple.resolve(DSL.ref("ip", STRING))); + assertEquals( + ExprValueUtils.stringValue("209.160.24.63"), bindingTuple.resolve(DSL.ref("ip", STRING))); } @Test public void resolve_missing_expression() { BindingTuple bindingTuple = ExprValueUtils.tupleValue(ImmutableMap.of("ip", "209.160.24.63")).bindingTuples(); - assertEquals(ExprValueUtils.LITERAL_MISSING, - bindingTuple.resolve(DSL.ref("ip_missing", STRING))); + assertEquals( + ExprValueUtils.LITERAL_MISSING, bindingTuple.resolve(DSL.ref("ip_missing", STRING))); } @Test public void resolve_from_empty_tuple() { - assertEquals(ExprValueUtils.LITERAL_MISSING, - BindingTuple.EMPTY.resolve(DSL.ref("ip_missing", STRING))); + assertEquals( + ExprValueUtils.LITERAL_MISSING, BindingTuple.EMPTY.resolve(DSL.ref("ip_missing", STRING))); } @Test public void resolve_literal_expression_throw_exception() { BindingTuple bindingTuple = ExprValueUtils.tupleValue(ImmutableMap.of("ip", "209.160.24.63")).bindingTuples(); - ExpressionEvaluationException exception = assertThrows(ExpressionEvaluationException.class, - () -> bindingTuple.resolve(DSL.literal(1))); + ExpressionEvaluationException exception = + assertThrows( + ExpressionEvaluationException.class, () -> bindingTuple.resolve(DSL.literal(1))); assertEquals("can resolve expression: 1", exception.getMessage()); } } diff --git a/core/src/test/java/org/opensearch/sql/storage/write/TableWriteOperatorTest.java b/core/src/test/java/org/opensearch/sql/storage/write/TableWriteOperatorTest.java index 112192bde0c..e5b2c9f61a0 100644 --- a/core/src/test/java/org/opensearch/sql/storage/write/TableWriteOperatorTest.java +++ b/core/src/test/java/org/opensearch/sql/storage/write/TableWriteOperatorTest.java @@ -21,44 +21,47 @@ @ExtendWith(MockitoExtension.class) class TableWriteOperatorTest { - @Mock - private PhysicalPlan child; + @Mock private PhysicalPlan child; private TableWriteOperator tableWrite; @BeforeEach void setUp() { - tableWrite = new TableWriteOperator(child) { - @Override - public String explain() { - return "explain"; - } + tableWrite = + new TableWriteOperator(child) { + @Override + public String explain() { + return "explain"; + } - @Override - public boolean hasNext() { - return false; - } + @Override + public boolean hasNext() { + return false; + } - @Override - public ExprValue next() { - return null; - } - }; + @Override + public ExprValue next() { + return null; + } + }; } @Test void testAccept() { - Boolean isVisited = tableWrite.accept(new PhysicalPlanNodeVisitor<>() { - @Override - protected Boolean visitNode(PhysicalPlan node, Object context) { - return (node instanceof TableWriteOperator); - } + Boolean isVisited = + tableWrite.accept( + new PhysicalPlanNodeVisitor<>() { + @Override + protected Boolean visitNode(PhysicalPlan node, Object context) { + return (node instanceof TableWriteOperator); + } - @Override - public Boolean visitTableWrite(TableWriteOperator node, Object context) { - return super.visitTableWrite(node, context); - } - }, null); + @Override + public Boolean visitTableWrite(TableWriteOperator node, Object context) { + return super.visitTableWrite(node, context); + } + }, + null); assertTrue(isVisited); } diff --git a/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java b/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java index d6f0ef02cd9..b25f4d1053d 100644 --- a/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java +++ b/core/src/test/java/org/opensearch/sql/utils/ComparisonUtil.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.utils; import static org.opensearch.sql.data.model.ExprValueUtils.getDoubleValue; @@ -21,8 +20,8 @@ public class ComparisonUtil { /** - * Util to compare the object (integer, long, float, double, string) values. - * Allows comparing different datetime types and requires `FunctionProperties` object for that. + * Util to compare the object (integer, long, float, double, string) values. Allows comparing + * different datetime types and requires `FunctionProperties` object for that. */ public static int compare(FunctionProperties functionProperties, ExprValue v1, ExprValue v2) { if (v1.isMissing() || v2.isMissing()) { @@ -36,9 +35,7 @@ public static int compare(FunctionProperties functionProperties, ExprValue v1, E return compare(v1, v2); } - /** - * Util to compare the object (integer, long, float, double, string) values. - */ + /** Util to compare the object (integer, long, float, double, string) values. */ public static int compare(ExprValue v1, ExprValue v2) { if (v1.isMissing() || v2.isMissing()) { throw new ExpressionEvaluationException("invalid to call compare operation on missing value"); @@ -49,21 +46,34 @@ public static int compare(ExprValue v1, ExprValue v2) { "invalid to call compare operation on values of different types"); } - switch ((ExprCoreType)v1.type()) { - case BYTE: return v1.byteValue().compareTo(v2.byteValue()); - case SHORT: return v1.shortValue().compareTo(v2.shortValue()); - case INTEGER: return getIntegerValue(v1).compareTo(getIntegerValue(v2)); - case LONG: return getLongValue(v1).compareTo(getLongValue(v2)); - case FLOAT: return getFloatValue(v1).compareTo(getFloatValue(v2)); - case DOUBLE: return getDoubleValue(v1).compareTo(getDoubleValue(v2)); - case STRING: return getStringValue(v1).compareTo(getStringValue(v2)); - case BOOLEAN: return v1.booleanValue().compareTo(v2.booleanValue()); - case TIME: return v1.timeValue().compareTo(v2.timeValue()); - case DATE: return v1.dateValue().compareTo(v2.dateValue()); - case DATETIME: return v1.datetimeValue().compareTo(v2.datetimeValue()); - case TIMESTAMP: return v1.timestampValue().compareTo(v2.timestampValue()); - default: throw new ExpressionEvaluationException( - String.format("%s instances are not comparable", v1.getClass().getSimpleName())); + switch ((ExprCoreType) v1.type()) { + case BYTE: + return v1.byteValue().compareTo(v2.byteValue()); + case SHORT: + return v1.shortValue().compareTo(v2.shortValue()); + case INTEGER: + return getIntegerValue(v1).compareTo(getIntegerValue(v2)); + case LONG: + return getLongValue(v1).compareTo(getLongValue(v2)); + case FLOAT: + return getFloatValue(v1).compareTo(getFloatValue(v2)); + case DOUBLE: + return getDoubleValue(v1).compareTo(getDoubleValue(v2)); + case STRING: + return getStringValue(v1).compareTo(getStringValue(v2)); + case BOOLEAN: + return v1.booleanValue().compareTo(v2.booleanValue()); + case TIME: + return v1.timeValue().compareTo(v2.timeValue()); + case DATE: + return v1.dateValue().compareTo(v2.dateValue()); + case DATETIME: + return v1.datetimeValue().compareTo(v2.datetimeValue()); + case TIMESTAMP: + return v1.timestampValue().compareTo(v2.timestampValue()); + default: + throw new ExpressionEvaluationException( + String.format("%s instances are not comparable", v1.getClass().getSimpleName())); } } } diff --git a/core/src/test/java/org/opensearch/sql/utils/DateTimeUtilsTest.java b/core/src/test/java/org/opensearch/sql/utils/DateTimeUtilsTest.java index 9bf0a53ff6e..989759681f8 100644 --- a/core/src/test/java/org/opensearch/sql/utils/DateTimeUtilsTest.java +++ b/core/src/test/java/org/opensearch/sql/utils/DateTimeUtilsTest.java @@ -16,11 +16,16 @@ public class DateTimeUtilsTest { @Test void round() { - long actual = LocalDateTime.parse("2021-09-28T23:40:00").atZone(ZoneId.systemDefault()) - .toInstant().toEpochMilli(); + long actual = + LocalDateTime.parse("2021-09-28T23:40:00") + .atZone(ZoneId.systemDefault()) + .toInstant() + .toEpochMilli(); long rounded = DateTimeUtils.roundFloor(actual, TimeUnit.HOURS.toMillis(1)); assertEquals( - LocalDateTime.parse("2021-09-28T23:00:00").atZone(ZoneId.systemDefault()).toInstant() + LocalDateTime.parse("2021-09-28T23:00:00") + .atZone(ZoneId.systemDefault()) + .toInstant() .toEpochMilli(), Instant.ofEpochMilli(rounded).toEpochMilli()); } diff --git a/core/src/test/java/org/opensearch/sql/utils/MatcherUtils.java b/core/src/test/java/org/opensearch/sql/utils/MatcherUtils.java index 8b1a3dda2ed..206f05a38a6 100644 --- a/core/src/test/java/org/opensearch/sql/utils/MatcherUtils.java +++ b/core/src/test/java/org/opensearch/sql/utils/MatcherUtils.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.utils; import org.hamcrest.Description; @@ -11,13 +10,9 @@ import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprCoreType; -/** - * Matcher Utils. - */ +/** Matcher Utils. */ public class MatcherUtils { - /** - * Check {@link ExprValue} type equal to {@link ExprCoreType}. - */ + /** Check {@link ExprValue} type equal to {@link ExprCoreType}. */ public static TypeSafeMatcher hasType(ExprCoreType type) { return new TypeSafeMatcher() { @Override @@ -32,9 +27,7 @@ protected boolean matchesSafely(ExprValue value) { }; } - /** - * Check {@link ExprValue} value equal to {@link Object}. - */ + /** Check {@link ExprValue} value equal to {@link Object}. */ public static TypeSafeMatcher hasValue(Object object) { return new TypeSafeMatcher() { @Override diff --git a/core/src/test/java/org/opensearch/sql/utils/SystemIndexUtilsTest.java b/core/src/test/java/org/opensearch/sql/utils/SystemIndexUtilsTest.java index 81d28f40db5..903077ca295 100644 --- a/core/src/test/java/org/opensearch/sql/utils/SystemIndexUtilsTest.java +++ b/core/src/test/java/org/opensearch/sql/utils/SystemIndexUtilsTest.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.utils; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -49,8 +48,8 @@ void test_mapping_info_table() { @Test void test_mapping_info_table_with_special_index_name() { - final SystemIndexUtils.SystemTable table - = systemTable("logs-2021.01.11.MAPPINGS_ODFE_SYS_TABLE"); + final SystemIndexUtils.SystemTable table = + systemTable("logs-2021.01.11.MAPPINGS_ODFE_SYS_TABLE"); assertTrue(table.isMetaInfoTable()); assertFalse(table.isSystemInfoTable()); assertEquals("logs-2021.01.11", table.getTableName()); diff --git a/core/src/test/java/org/opensearch/sql/utils/TestOperator.java b/core/src/test/java/org/opensearch/sql/utils/TestOperator.java index 584cf6f3fd0..d356f2f866b 100644 --- a/core/src/test/java/org/opensearch/sql/utils/TestOperator.java +++ b/core/src/test/java/org/opensearch/sql/utils/TestOperator.java @@ -18,13 +18,10 @@ public class TestOperator extends PhysicalPlan implements SerializablePlan { private int field; - @Setter - private boolean throwNoCursorOnWrite = false; - @Setter - private boolean throwIoOnWrite = false; + @Setter private boolean throwNoCursorOnWrite = false; + @Setter private boolean throwIoOnWrite = false; - public TestOperator() { - } + public TestOperator() {} public TestOperator(int value) { field = value; diff --git a/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultExecutionEngine.java b/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultExecutionEngine.java index db72498a1d3..61219d46378 100644 --- a/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultExecutionEngine.java +++ b/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultExecutionEngine.java @@ -12,9 +12,7 @@ import org.opensearch.sql.executor.pagination.Cursor; import org.opensearch.sql.planner.physical.PhysicalPlan; -/** - * Used for testing purpose. - */ +/** Used for testing purpose. */ public class DefaultExecutionEngine implements ExecutionEngine { @Override public void execute(PhysicalPlan plan, ResponseListener listener) { @@ -33,8 +31,8 @@ public void execute( while (plan.hasNext()) { result.add(plan.next()); } - QueryResponse response = new QueryResponse(new Schema(new ArrayList<>()), new ArrayList<>(), - Cursor.None); + QueryResponse response = + new QueryResponse(new Schema(new ArrayList<>()), new ArrayList<>(), Cursor.None); listener.onResponse(response); } catch (Exception e) { listener.onFailure(e); diff --git a/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultQueryManager.java b/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultQueryManager.java index 41888a0581d..3167d4843a6 100644 --- a/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultQueryManager.java +++ b/core/src/testFixtures/java/org/opensearch/sql/executor/DefaultQueryManager.java @@ -13,9 +13,7 @@ import java.util.concurrent.TimeUnit; import org.opensearch.sql.executor.execution.AbstractPlan; -/** - * Default QueryManager implementation which execute {@link AbstractPlan} on caller thread. - */ +/** Default QueryManager implementation which execute {@link AbstractPlan} on caller thread. */ public class DefaultQueryManager implements QueryManager { private final ExecutorService executorService; From 6a14bbd0e7c851e93b15a11c3abaf52492730eca Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Tue, 22 Aug 2023 12:05:16 -0700 Subject: [PATCH 4/6] Reverted string utils Signed-off-by: Mitchell Gale --- .../sql/common/utils/StringUtils.java | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java index 4b7752a9de5..07c09c73c4e 100644 --- a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java +++ b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ + package org.opensearch.sql.common.utils; import com.google.common.base.Strings; @@ -11,38 +12,52 @@ public class StringUtils { /** - * Unquote Identifier which has " or ' as mark. Strings quoted by ' or " with two of these quotes - * appearing next to each other in the quote acts as an escape
+ * Unquote any string with mark specified. + * @param text string + * @param mark quotation mark + * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been + * removed + */ + public static String unquote(String text, String mark) { + if (isQuoted(text, mark)) { + return text.substring(mark.length(), text.length() - mark.length()); + } + return text; + } + + /** + * Unquote Identifier which has " or ' or ` as mark. + * Strings quoted by ' or " with two of these quotes appearing next to each other in the quote + * acts as an escape * Example: 'Test''s' will result in 'Test's', similar with those single quotes being replaced - * with double quote. Supports escaping quotes (single/double) and escape characters using the `\` - * characters. - * + * with double. * @param text string - * @return An unquoted string whose outer pair of (single/double) quotes have been removed + * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been + * removed */ public static String unquoteText(String text) { + if (text.length() < 2) { return text; } - char enclosingQuote = 0; + char enclosingQuote; char firstChar = text.charAt(0); char lastChar = text.charAt(text.length() - 1); - if (firstChar != lastChar) { + if (firstChar == lastChar + && (firstChar == '\'' + || firstChar == '"' + || firstChar == '`')) { + enclosingQuote = firstChar; + } else { return text; } - if (firstChar == '`') { + if (enclosingQuote == '`') { return text.substring(1, text.length() - 1); } - if (firstChar == lastChar && (firstChar == '\'' || firstChar == '"')) { - enclosingQuote = firstChar; - } else { - return text; - } - char currentChar; char nextChar; @@ -52,22 +67,21 @@ public static String unquoteText(String text) { for (int chIndex = 1; chIndex < text.length() - 1; chIndex++) { currentChar = text.charAt(chIndex); nextChar = text.charAt(chIndex + 1); - - if ((currentChar == '\\' && (nextChar == '"' || nextChar == '\\' || nextChar == '\'')) - || (currentChar == nextChar && currentChar == enclosingQuote)) { + if (currentChar == enclosingQuote + && nextChar == currentChar) { chIndex++; - currentChar = nextChar; } textSB.append(currentChar); } + return textSB.toString(); } /** * Unquote Identifier which has ` as mark. - * * @param identifier identifier that possibly enclosed by double quotes or back ticks - * @return An unquoted string whose outer pair of (double/back-tick) quotes have been removed + * @return An unquoted string whose outer pair of (double/back-tick) quotes have been + * removed */ public static String unquoteIdentifier(String identifier) { if (isQuoted(identifier, "`")) { @@ -78,15 +92,16 @@ public static String unquoteIdentifier(String identifier) { } /** - * Returns a formatted string using the specified format string and arguments, as well as the - * {@link Locale#ROOT} locale. + * Returns a formatted string using the specified format string and + * arguments, as well as the {@link Locale#ROOT} locale. * * @param format format string - * @param args arguments referenced by the format specifiers in the format string + * @param args arguments referenced by the format specifiers in the format string * @return A formatted string * @throws IllegalFormatException If a format string contains an illegal syntax, a format - * specifier that is incompatible with the given arguments, insufficient arguments given the - * format string, or other illegal conditions. + * specifier that is incompatible with the given arguments, + * insufficient arguments given the format string, or other + * illegal conditions. * @see java.lang.String#format(Locale, String, Object...) */ public static String format(final String format, Object... args) { @@ -96,4 +111,4 @@ public static String format(final String format, Object... args) { private static boolean isQuoted(String text, String mark) { return !Strings.isNullOrEmpty(text) && text.startsWith(mark) && text.endsWith(mark); } -} +} \ No newline at end of file From c7c41cc0f7b8faaa7461737285ce3b208c97141e Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Tue, 22 Aug 2023 12:05:50 -0700 Subject: [PATCH 5/6] Spotless apply Signed-off-by: Mitchell Gale --- .../sql/common/utils/StringUtils.java | 36 ++++++++----------- .../arthmetic/MathematicalFunction.java | 1 - 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java index 07c09c73c4e..c81f56ef634 100644 --- a/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java +++ b/common/src/main/java/org/opensearch/sql/common/utils/StringUtils.java @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ - package org.opensearch.sql.common.utils; import com.google.common.base.Strings; @@ -13,6 +12,7 @@ public class StringUtils { /** * Unquote any string with mark specified. + * * @param text string * @param mark quotation mark * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been @@ -26,11 +26,10 @@ public static String unquote(String text, String mark) { } /** - * Unquote Identifier which has " or ' or ` as mark. - * Strings quoted by ' or " with two of these quotes appearing next to each other in the quote - * acts as an escape - * Example: 'Test''s' will result in 'Test's', similar with those single quotes being replaced - * with double. + * Unquote Identifier which has " or ' or ` as mark. Strings quoted by ' or " with two of these + * quotes appearing next to each other in the quote acts as an escape Example: 'Test''s' will + * result in 'Test's', similar with those single quotes being replaced with double. + * * @param text string * @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been * removed @@ -45,10 +44,7 @@ public static String unquoteText(String text) { char firstChar = text.charAt(0); char lastChar = text.charAt(text.length() - 1); - if (firstChar == lastChar - && (firstChar == '\'' - || firstChar == '"' - || firstChar == '`')) { + if (firstChar == lastChar && (firstChar == '\'' || firstChar == '"' || firstChar == '`')) { enclosingQuote = firstChar; } else { return text; @@ -67,8 +63,7 @@ public static String unquoteText(String text) { for (int chIndex = 1; chIndex < text.length() - 1; chIndex++) { currentChar = text.charAt(chIndex); nextChar = text.charAt(chIndex + 1); - if (currentChar == enclosingQuote - && nextChar == currentChar) { + if (currentChar == enclosingQuote && nextChar == currentChar) { chIndex++; } textSB.append(currentChar); @@ -79,9 +74,9 @@ public static String unquoteText(String text) { /** * Unquote Identifier which has ` as mark. + * * @param identifier identifier that possibly enclosed by double quotes or back ticks - * @return An unquoted string whose outer pair of (double/back-tick) quotes have been - * removed + * @return An unquoted string whose outer pair of (double/back-tick) quotes have been removed */ public static String unquoteIdentifier(String identifier) { if (isQuoted(identifier, "`")) { @@ -92,16 +87,15 @@ public static String unquoteIdentifier(String identifier) { } /** - * Returns a formatted string using the specified format string and - * arguments, as well as the {@link Locale#ROOT} locale. + * Returns a formatted string using the specified format string and arguments, as well as the + * {@link Locale#ROOT} locale. * * @param format format string - * @param args arguments referenced by the format specifiers in the format string + * @param args arguments referenced by the format specifiers in the format string * @return A formatted string * @throws IllegalFormatException If a format string contains an illegal syntax, a format - * specifier that is incompatible with the given arguments, - * insufficient arguments given the format string, or other - * illegal conditions. + * specifier that is incompatible with the given arguments, insufficient arguments given the + * format string, or other illegal conditions. * @see java.lang.String#format(Locale, String, Object...) */ public static String format(final String format, Object... args) { @@ -111,4 +105,4 @@ public static String format(final String format, Object... args) { private static boolean isQuoted(String text, String mark) { return !Strings.isNullOrEmpty(text) && text.startsWith(mark) && text.endsWith(mark); } -} \ No newline at end of file +} diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java index 0ba434c26d7..22f4b765732 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java @@ -416,7 +416,6 @@ private static DefaultFunctionResolver pi() { } /** - * Definition of pow(x, y)/power(x, y) function.
* Calculate the value of x raised to the power of y
* The supported signature of pow/power function is
From 9b4c112ecaf2d7afcb47942c18b5d47b52000b79 Mon Sep 17 00:00:00 2001 From: Mitchell Gale Date: Tue, 22 Aug 2023 13:42:35 -0700 Subject: [PATCH 6/6] spotless apply Signed-off-by: Mitchell Gale --- .../main/java/org/opensearch/sql/ast/dsl/AstDSL.java | 4 ++++ .../opensearch/sql/ast/expression/QualifiedName.java | 10 +++------- .../java/org/opensearch/sql/ast/tree/FetchCursor.java | 1 - .../org/opensearch/sql/ast/tree/TableFunction.java | 1 - .../opensearch/sql/data/model/AbstractExprValue.java | 4 ++-- .../org/opensearch/sql/data/model/ExprTimeValue.java | 3 +-- .../org/opensearch/sql/data/type/WideningTypeRule.java | 5 +++-- 7 files changed, 13 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java index 9b983c19f33..4f3056b0f7f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java +++ b/core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java @@ -247,6 +247,8 @@ public static Function function(String funcName, UnresolvedExpression... funcArg } /** + * + * *
    * CASE
    *    WHEN search_condition THEN result_expr
@@ -260,6 +262,8 @@ public UnresolvedExpression caseWhen(UnresolvedExpression elseClause, When... wh } /** + * + * *
    * CASE case_value_expr
    *     WHEN compare_expr THEN result_expr
diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java b/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java
index 73c6e3782a5..4f2d7eabf05 100644
--- a/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java
+++ b/core/src/main/java/org/opensearch/sql/ast/expression/QualifiedName.java
@@ -78,13 +78,9 @@ public Optional first() {
    * Get rest parts of the qualified name. Assume that there must be remaining parts so caller is
    * responsible for the check (first() or size() must be called first).
* For example:
- * {@code
- *   QualifiedName name = ...
- *   Optional first = name.first();
- *   if (first.isPresent()) {
- *   name.rest() ...
- *   }
- * } + * {@code
  QualifiedName name = ...
  Optional first = + * name.first();
  if (first.isPresent()) {
  name.rest() ...
  }
} + * * @return rest part(s) */ public QualifiedName rest() { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java b/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java index d1e2279aadb..d6a260ad0be 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/FetchCursor.java @@ -11,7 +11,6 @@ import org.opensearch.sql.ast.AbstractNodeVisitor; /** An unresolved plan that represents fetching the next batch in paginated plan. */ - @RequiredArgsConstructor @EqualsAndHashCode(callSuper = false) public class FetchCursor extends UnresolvedPlan { diff --git a/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java b/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java index 331547a23e2..711a8a0ec5f 100644 --- a/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java +++ b/core/src/main/java/org/opensearch/sql/ast/tree/TableFunction.java @@ -15,7 +15,6 @@ import org.opensearch.sql.ast.expression.QualifiedName; import org.opensearch.sql.ast.expression.UnresolvedExpression; - /** ASTNode for Table Function. */ @ToString @EqualsAndHashCode(callSuper = false) diff --git a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java index f5ac4d493bb..fd2cc902979 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java @@ -29,8 +29,8 @@ public int compareTo(ExprValue other) { } /** - * The customize equals logic. - * The table below list the NULL and MISSING handling logic. + * The customize equals logic. The table below list the NULL and MISSING handling logic. + * * * * diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java index c22b423c7d9..d808af49b19 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java @@ -34,8 +34,7 @@ public ExprTimeValue(String time) { this.time = LocalTime.parse(time, DATE_TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL); } catch (DateTimeParseException e) { throw new SemanticCheckException( - String.format( - "time:%s in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'", time)); + String.format("time:%s in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'", time)); } } diff --git a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java index 3c8a6031245..723d09956d1 100644 --- a/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java +++ b/core/src/main/java/org/opensearch/sql/data/type/WideningTypeRule.java @@ -12,6 +12,7 @@ /** * The definition of widening type rule for expression value. + * *
A
* * @@ -56,8 +57,8 @@ private static int distance(ExprType type1, ExprType type2, int distance) { /** * The max type among two types. The max is defined as follow if type1 could widen to type2, then - * max is type2, vice versa if type1 couldn't widen to type2 and type2 could't widen to type1, then - * throw {@link ExpressionEvaluationException}. + * max is type2, vice versa if type1 couldn't widen to type2 and type2 could't widen to type1, + * then throw {@link ExpressionEvaluationException}. * * @param type1 type1 * @param type2 type2
ExprTypeWidens to data types
INTEGERLONG, FLOAT, DOUBLE