Skip to content

Commit

Permalink
SqlResource: Fix incorrect labeling of aliased columns. (apache#3829)
Browse files Browse the repository at this point in the history
  • Loading branch information
gianm authored and fjy committed Jan 7, 2017
1 parent 97ce006 commit 3c01230
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sql/src/main/java/io/druid/sql/http/SqlResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void write(final OutputStream outputStream) throws IOException, WebApplic
value = resultSet.getObject(i + 1);
}

jsonGenerator.writeObjectField(metaData.getColumnName(i + 1), value);
jsonGenerator.writeObjectField(metaData.getColumnLabel(i + 1), value);
}
jsonGenerator.writeEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ public void testSelectCount() throws Exception
);
}

@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
public void testExplainSelectCount() throws Exception
{
Expand Down Expand Up @@ -244,8 +259,8 @@ private static List<Map<String, Object>> getRows(final ResultSet resultSet, fina
while (resultSet.next()) {
final Map<String, Object> row = Maps.newHashMap();
for (int i = 0; i < metaData.getColumnCount(); i++) {
if (returnKeys == null || returnKeys.contains(metaData.getColumnName(i + 1))) {
row.put(metaData.getColumnName(i + 1), resultSet.getObject(i + 1));
if (returnKeys == null || returnKeys.contains(metaData.getColumnLabel(i + 1))) {
row.put(metaData.getColumnLabel(i + 1), resultSet.getObject(i + 1));
}
}
rows.add(row);
Expand Down
25 changes: 25 additions & 0 deletions sql/src/test/java/io/druid/sql/calcite/CalciteQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,31 @@ public void testSelectStarWithLimitDescending() throws Exception
);
}

@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
public void testSelectSingleColumnWithLimitDescending() throws Exception
{
Expand Down
32 changes: 32 additions & 0 deletions sql/src/test/java/io/druid/sql/calcite/http/SqlResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ public void testTimestampsInResponse() throws Exception
);
}

@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
public void testExplainCountStar() throws Exception
{
Expand Down

0 comments on commit 3c01230

Please sign in to comment.