Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Backport 2.x] Bug Fix, handle DESC TABLE response #2213

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static LinkedHashMap<String, ExprValue> extractRow(
linkedHashMap.put(
column.getName(), new ExprTimestampValue(row.getString(column.getName())));
} else if (type == ExprCoreType.STRING) {
linkedHashMap.put(column.getName(), new ExprStringValue(row.getString(column.getName())));
linkedHashMap.put(column.getName(), new ExprStringValue(jsonString(row, column.getName())));
} else {
throw new RuntimeException("Result contains invalid data type");
}
Expand Down Expand Up @@ -137,6 +137,10 @@ private ExprCoreType getDataType(String sparkDataType) {
}
}

private static String jsonString(JSONObject jsonObject, String key) {
return jsonObject.has(key) ? jsonObject.getString(key) : "";
}

@Override
public boolean hasNext() {
return responseIterator.hasNext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.opensearch.sql.data.model.ExprValueUtils.stringValue;
import static org.opensearch.sql.spark.constants.TestConstants.QUERY;
import static org.opensearch.sql.spark.utils.TestUtils.getJson;

Expand Down Expand Up @@ -173,4 +174,60 @@ void testQuerySchema() {
ExecutionEngine.Schema expectedSchema = new ExecutionEngine.Schema(columns);
assertEquals(expectedSchema, sparkSqlFunctionTableScanOperator.schema());
}

/** https://github.com/opensearch-project/sql/issues/2210. */
@Test
@SneakyThrows
void issue2210() {
SparkQueryRequest sparkQueryRequest = new SparkQueryRequest();
sparkQueryRequest.setSql(QUERY);

SparkSqlFunctionTableScanOperator sparkSqlFunctionTableScanOperator =
new SparkSqlFunctionTableScanOperator(sparkClient, sparkQueryRequest);

when(sparkClient.sql(any())).thenReturn(new JSONObject(getJson("issue2210.json")));
sparkSqlFunctionTableScanOperator.open();
assertTrue(sparkSqlFunctionTableScanOperator.hasNext());
assertEquals(
new ExprTupleValue(
new LinkedHashMap<>() {
{
put("col_name", stringValue("day"));
put("data_type", stringValue("int"));
put("comment", stringValue(""));
}
}),
sparkSqlFunctionTableScanOperator.next());
assertEquals(
new ExprTupleValue(
new LinkedHashMap<>() {
{
put("col_name", stringValue("# Partition Information"));
put("data_type", stringValue(""));
put("comment", stringValue(""));
}
}),
sparkSqlFunctionTableScanOperator.next());
assertEquals(
new ExprTupleValue(
new LinkedHashMap<>() {
{
put("col_name", stringValue("# col_name"));
put("data_type", stringValue("data_type"));
put("comment", stringValue("comment"));
}
}),
sparkSqlFunctionTableScanOperator.next());
assertEquals(
new ExprTupleValue(
new LinkedHashMap<>() {
{
put("col_name", stringValue("day"));
put("data_type", stringValue("int"));
put("comment", stringValue(""));
}
}),
sparkSqlFunctionTableScanOperator.next());
Assertions.assertFalse(sparkSqlFunctionTableScanOperator.hasNext());
}
}
17 changes: 17 additions & 0 deletions spark/src/test/resources/issue2210.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"data": {
"result": [
"{'col_name':'day','data_type':'int'}",
"{'col_name':'# Partition Information','data_type':'','comment':''}",
"{'col_name':'# col_name','data_type':'data_type','comment':'comment'}",
"{'col_name':'day','data_type':'int'}"
],
"schema": [
"{'column_name':'col_name','data_type':'string'}",
"{'column_name':'data_type','data_type':'string'}",
"{'column_name':'comment','data_type':'string'}"
],
"stepId": "s-123456789",
"applicationId": "application-abc"
}
}
Loading