This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JDBC response for delete query (#337)
* Fix JDBC response for Delete queries * Add integration tests * Add unit tests
- Loading branch information
Showing
5 changed files
with
220 additions
and
18 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/com/amazon/opendistroforelasticsearch/sql/executor/format/DeleteResultSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.executor.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.domain.Delete; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.index.reindex.BulkByScrollResponse; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DeleteResultSet extends ResultSet { | ||
private Delete query; | ||
private Object queryResult; | ||
|
||
public static final String DELETED = "deleted_rows"; | ||
|
||
public DeleteResultSet(Client client, Delete query, Object queryResult) { | ||
this.client = client; | ||
this.query = query; | ||
this.queryResult = queryResult; | ||
this.schema = new Schema(loadColumns()); | ||
this.dataRows = new DataRows(loadRows()); | ||
} | ||
|
||
private List<Schema.Column> loadColumns() { | ||
return Collections.singletonList(new Schema.Column(DELETED, null, Schema.Type.LONG)); | ||
} | ||
|
||
private List<DataRows.Row> loadRows() { | ||
return Collections.singletonList(new DataRows.Row(loadDeletedData())); | ||
} | ||
|
||
private Map<String, Object> loadDeletedData(){ | ||
return Collections.singletonMap(DELETED, ((BulkByScrollResponse) queryResult).getDeleted()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...java/com/amazon/opendistroforelasticsearch/sql/unittest/executor/DeleteResultSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.unittest.executor; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.domain.Delete; | ||
import com.amazon.opendistroforelasticsearch.sql.executor.format.DataRows; | ||
import com.amazon.opendistroforelasticsearch.sql.executor.format.DeleteResultSet; | ||
import com.amazon.opendistroforelasticsearch.sql.executor.format.Schema; | ||
import org.apache.lucene.search.TotalHits; | ||
import org.elasticsearch.client.node.NodeClient; | ||
|
||
import org.elasticsearch.common.xcontent.DeprecationHandler; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
|
||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.index.reindex.BulkByScrollResponse; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
|
||
public class DeleteResultSetTest { | ||
|
||
@Mock | ||
NodeClient client; | ||
|
||
@Mock | ||
Delete deleteQuery; | ||
|
||
@Test | ||
public void testDeleteResponseForJdbcFormat() throws IOException { | ||
|
||
String jsonDeleteResponse = "{\n" + | ||
" \"took\" : 73,\n" + | ||
" \"timed_out\" : false,\n" + | ||
" \"total\" : 1,\n" + | ||
" \"updated\" : 0,\n" + | ||
" \"created\" : 0,\n" + | ||
" \"deleted\" : 10,\n" + | ||
" \"batches\" : 1,\n" + | ||
" \"version_conflicts\" : 0,\n" + | ||
" \"noops\" : 0,\n" + | ||
" \"retries\" : {\n" + | ||
" \"bulk\" : 0,\n" + | ||
" \"search\" : 0\n" + | ||
" },\n" + | ||
" \"throttled_millis\" : 0,\n" + | ||
" \"requests_per_second\" : -1.0,\n" + | ||
" \"throttled_until_millis\" : 0,\n" + | ||
" \"failures\" : [ ]\n" + | ||
"}\n"; | ||
|
||
XContentType xContentType = XContentType.JSON; | ||
XContentParser parser = xContentType.xContent().createParser( | ||
NamedXContentRegistry.EMPTY, | ||
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, | ||
jsonDeleteResponse | ||
); | ||
|
||
BulkByScrollResponse deleteResponse = BulkByScrollResponse.fromXContent(parser); | ||
DeleteResultSet deleteResultSet = new DeleteResultSet(client, deleteQuery, deleteResponse); | ||
Schema schema = deleteResultSet.getSchema(); | ||
DataRows dataRows = deleteResultSet.getDataRows(); | ||
|
||
assertThat(schema.getHeaders().size(), equalTo(1)); | ||
assertThat(dataRows.getSize(), equalTo(1L)); | ||
assertThat(dataRows.iterator().next().getData(DeleteResultSet.DELETED), equalTo(10L)); | ||
} | ||
|
||
} |