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

Preserve the name of column after unnest #11193

Closed
wants to merge 1 commit into from
Closed
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 @@ -700,8 +700,8 @@ protected Scope visitUnnest(Unnest node, Optional<Scope> scope)
if (expressionType instanceof ArrayType) {
Type elementType = ((ArrayType) expressionType).getElementType();
if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
elementType.getTypeParameters().stream()
.map(type -> Field.newUnqualified(Optional.empty(), type))
((RowType) elementType).getFields().stream()
.map(field -> Field.newUnqualified(field.getName(), field.getType()))
.forEach(outputFields::add);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,116 @@
*/
package com.facebook.presto.sql.query;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestUnnest
{
private QueryAssertions assertions;

@BeforeClass
public void init()
{
assertions = new QueryAssertions();
}

@AfterClass(alwaysRun = true)
public void teardown()
{
assertions.close();
assertions = null;
}

@Test
public void testUnnestArrayRows()
{
try (QueryAssertions assertions = new QueryAssertions()) {
assertions.assertQuery(
"SELECT * FROM UNNEST(ARRAY[ROW(1, 1.1), ROW(3, 3.3)], ARRAY[ROW('a', true), ROW('b', false)])",
"VALUES (1, 1.1, 'a', true), (3, 3.3, 'b', false)");
assertions.assertQuery(
"SELECT x, y FROM (VALUES (ARRAY[ROW(1.0, 2), ROW(3, 4.123)])) AS t(a) CROSS JOIN UNNEST(a) t(x, y)",
"VALUES (1.0, 2), (3, 4.123)");
assertions.assertQuery(
"SELECT x, y, z FROM (VALUES (ARRAY[ROW(1, 2), ROW(3, 4)])) t(a) CROSS JOIN (VALUES (1), (2)) s(z) CROSS JOIN UNNEST(a) t(x, y)",
"VALUES (1, 2, 1), (1, 2, 2), (3, 4, 1), (3, 4, 2)");
}
assertions.assertQuery(
"SELECT * FROM UNNEST(ARRAY[ROW(1, 1.1), ROW(3, 3.3)], ARRAY[ROW('a', true), ROW('b', false)])",
"VALUES (1, 1.1, 'a', true), (3, 3.3, 'b', false)");
assertions.assertQuery(
"SELECT x, y FROM (VALUES (ARRAY[ROW(1.0, 2), ROW(3, 4.123)])) AS t(a) CROSS JOIN UNNEST(a) t(x, y)",
"VALUES (1.0, 2), (3, 4.123)");
assertions.assertQuery(
"SELECT x, y, z FROM (VALUES (ARRAY[ROW(1, 2), ROW(3, 4)])) t(a) CROSS JOIN (VALUES (1), (2)) s(z) CROSS JOIN UNNEST(a) t(x, y)",
"VALUES (1, 2, 1), (1, 2, 2), (3, 4, 1), (3, 4, 2)");
}

@Test
public void testUnnestPreserveColumnName()
{
assertions.assertQuery(
"SELECT x FROM UNNEST(array[" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately obvious that array(cast(...) as row(x int, y varchar), cast(...) as row (x int, y varchar))) is of type
array(row(x int, y varchar)).

A better form would be: cast(array[row(1, 'a'), row(2, 'b')] as array(row(x int, y varchar)))

" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
"])",
"VALUES (1), (2)");

assertions.assertFails(
"SELECT x FROM" +
"(VALUES (3)) AS t(x)" +
"CROSS JOIN UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
"])",
".*Column 'x' is ambiguous.*");

assertions.assertQuery(
"SELECT t.x FROM" +
"(VALUES (3)) AS t(x)" +
"CROSS JOIN UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
"])",
"VALUES (3), (3)");

assertions.assertQuery(
"SELECT u.x FROM" +
"(VALUES (3)) AS t(x)" +
"CROSS JOIN UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
"]) u",
"VALUES (1), (2)");
}

@Test
public void testUnnestMultiExpr()
{
assertions.assertFails(
"SELECT x" +
" FROM UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
" ]," +
" array[" +
" CAST(ROW(3, 'c') AS ROW(x int, y varchar))," +
" CAST(ROW(4, 'd') AS ROW(x int, y varchar))" +
" ])",
".*Column 'x' is ambiguous.*");

assertions.assertQuery(
"SELECT t3" +
" FROM UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(x int, y varchar))," +
" CAST(ROW(2, 'b') AS ROW(x int, y varchar))" +
" ]," +
" array[" +
" CAST(ROW(3, 'c') AS ROW(x int, y varchar))," +
" CAST(ROW(4, 'd') AS ROW(x int, y varchar))" +
" ]) t(t1,t2,t3,t4)",
"VALUES (3), (4)");

assertions.assertQuery(
"SELECT x" +
" FROM UNNEST(array[" +
" CAST(ROW(1, 'a') AS ROW(a int, b varchar))," +
" CAST(ROW(2, 'b') AS ROW(a int, b varchar))" +
" ]," +
" array[" +
" CAST(ROW(3, 'c') AS ROW(x int, y varchar))," +
" CAST(ROW(4, 'd') AS ROW(x int, y varchar))" +
" ])",
"VALUES (3), (4)");
}
}