Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Added "datetime_format" URL parameter to handle jdbc date and time fo…
Browse files Browse the repository at this point in the history
…rmatting with new engine
  • Loading branch information
cip999 committed Aug 22, 2021
1 parent 8c99a03 commit e892bda
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package com.amazon.opendistroforelasticsearch.sql.protocol.response;

import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprTimestampValue.INCLUDE_TIME_WHEN_NONZERO;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.tupleValue;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.STRING;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.TIMESTAMP;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprTimestampValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprTupleValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils;
import com.amazon.opendistroforelasticsearch.sql.executor.ExecutionEngine;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;

import org.junit.jupiter.api.Test;

class QueryResultTest {
Expand All @@ -36,6 +44,10 @@ class QueryResultTest {
new ExecutionEngine.Schema.Column("name", null, STRING),
new ExecutionEngine.Schema.Column("age", null, INTEGER)));

private ExecutionEngine.Schema schemaTimestamp = new ExecutionEngine.Schema(ImmutableList.of(
new ExecutionEngine.Schema.Column("name", null, STRING),
new ExecutionEngine.Schema.Column("birthdate", null, TIMESTAMP)));


@Test
void size() {
Expand Down Expand Up @@ -125,4 +137,36 @@ void iterate() {
}
}

@Test
void setDateTimeFormatTest() {
LinkedHashMap<String, ExprValue> valueMap1 = new LinkedHashMap<>();
valueMap1.put("name", ExprValueUtils.fromObjectValue("John"));
valueMap1.put("birthdate", ExprValueUtils.fromObjectValue("1987-10-23 00:00:00", TIMESTAMP));

LinkedHashMap<String, ExprValue> valueMap2 = new LinkedHashMap<>();
valueMap2.put("name", ExprValueUtils.fromObjectValue("Andrea"));
valueMap2.put("birthdate", ExprValueUtils.fromObjectValue("1999-03-14 03:30:00", TIMESTAMP));

QueryResult response = new QueryResult(
schemaTimestamp,
Arrays.asList(
new ExprTupleValue(valueMap1),
new ExprTupleValue(valueMap2)
));

response.setDatetimeFormat(INCLUDE_TIME_WHEN_NONZERO);

int i = 0;
for (Object[] objects : response) {
if (i == 0) {
assertArrayEquals(new Object[] {"John", "1987-10-23"}, objects);
} else if (i == 1) {
assertArrayEquals(new Object[] {"Andrea", "1999-03-14 03:30:00"}, objects);
} else {
fail("More rows returned than expected");
}
i++;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.amazon.opendistroforelasticsearch.sql.protocol.response.format.Format;
import com.google.common.collect.ImmutableMap;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
Expand All @@ -44,6 +46,18 @@ public void shouldSupportQueryWithJDBCFormat() {
assertEquals(request.format(), Format.JDBC);
}

@Test
public void shouldSupportQueryDatetimeFormat() {
Map<String, String> params = new HashMap<>();
params.put("format", "jdbc");
params.put("datetime_format", "never_include_time");

SQLQueryRequest request = SQLQueryRequestBuilder.request("SELECT 1")
.params(params)
.build();
assertEquals("never_include_time", request.getDatetimeFormat());
}

@Test
public void shouldSupportQueryWithQueryFieldOnly() {
SQLQueryRequest request =
Expand Down

0 comments on commit e892bda

Please sign in to comment.