SqlResource: Fix incorrect labeling of aliased columns. (#3829)

This commit is contained in:
Gian Merlino 2017-01-07 12:33:53 -08:00 committed by Fangjin Yang
parent 97ce006719
commit 3c012305d1
4 changed files with 75 additions and 3 deletions

View File

@ -106,7 +106,7 @@ public class SqlResource
value = resultSet.getObject(i + 1); value = resultSet.getObject(i + 1);
} }
jsonGenerator.writeObjectField(metaData.getColumnName(i + 1), value); jsonGenerator.writeObjectField(metaData.getColumnLabel(i + 1), value);
} }
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
} }

View File

@ -124,6 +124,21 @@ public class DruidAvaticaHandlerTest
); );
} }
@Test
public void testFieldAliasingSelect() throws Exception
{
final ResultSet resultSet = client.createStatement().executeQuery(
"SELECT dim2 AS \"x\", dim2 AS \"y\" FROM druid.foo LIMIT 1"
);
final List<Map<String, Object>> rows = getRows(resultSet);
Assert.assertEquals(
ImmutableList.of(
ImmutableMap.of("x", "a", "y", "a")
),
rows
);
}
@Test @Test
public void testExplainSelectCount() throws Exception public void testExplainSelectCount() throws Exception
{ {
@ -244,8 +259,8 @@ public class DruidAvaticaHandlerTest
while (resultSet.next()) { while (resultSet.next()) {
final Map<String, Object> row = Maps.newHashMap(); final Map<String, Object> row = Maps.newHashMap();
for (int i = 0; i < metaData.getColumnCount(); i++) { for (int i = 0; i < metaData.getColumnCount(); i++) {
if (returnKeys == null || returnKeys.contains(metaData.getColumnName(i + 1))) { if (returnKeys == null || returnKeys.contains(metaData.getColumnLabel(i + 1))) {
row.put(metaData.getColumnName(i + 1), resultSet.getObject(i + 1)); row.put(metaData.getColumnLabel(i + 1), resultSet.getObject(i + 1));
} }
} }
rows.add(row); rows.add(row);

View File

@ -328,6 +328,31 @@ public class CalciteQueryTest
); );
} }
@Test
public void testSelectSingleColumnTwice() throws Exception
{
testQuery(
"SELECT dim2 x, dim2 y FROM druid.foo LIMIT 2",
ImmutableList.<Query>of(
Druids.newSelectQueryBuilder()
.dataSource(CalciteTests.DATASOURCE)
.intervals(QSS(Filtration.eternity()))
.dimensionSpecs(DIMS(
new DefaultDimensionSpec("dim2", "d1"),
new DefaultDimensionSpec("dim2", "d2")
))
.granularity(QueryGranularities.ALL)
.descending(false)
.pagingSpec(FIRST_PAGING_SPEC)
.build()
),
ImmutableList.of(
new Object[]{"a", "a"},
new Object[]{"", ""}
)
);
}
@Test @Test
public void testSelectSingleColumnWithLimitDescending() throws Exception public void testSelectSingleColumnWithLimitDescending() throws Exception
{ {

View File

@ -109,6 +109,38 @@ public class SqlResourceTest
); );
} }
@Test
public void testFieldAliasingSelect() throws Exception
{
final List<Map<String, Object>> rows = doPost(
new SqlQuery("SELECT dim2 \"x\", dim2 \"y\" FROM druid.foo LIMIT 1")
);
Assert.assertEquals(
ImmutableList.of(
ImmutableMap.of("x", "a", "y", "a")
),
rows
);
}
@Test
public void testFieldAliasingGroupBy() throws Exception
{
final List<Map<String, Object>> rows = doPost(
new SqlQuery("SELECT dim2 \"x\", dim2 \"y\" FROM druid.foo GROUP BY dim2")
);
Assert.assertEquals(
ImmutableList.of(
ImmutableMap.of("x", "", "y", ""),
ImmutableMap.of("x", "a", "y", "a"),
ImmutableMap.of("x", "abc", "y", "abc")
),
rows
);
}
@Test @Test
public void testExplainCountStar() throws Exception public void testExplainCountStar() throws Exception
{ {