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

Fix failure when rewriting distance functions in PostgreSQL #23165

Merged
merged 1 commit into from
Aug 29, 2024
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 @@ -124,12 +124,13 @@ public static Optional<ParameterizedExpression> rewrite(ConnectorExpression expr
if (expression instanceof Call call && call.getFunctionName().equals(CAST_FUNCTION_NAME)) {
ConnectorExpression argument = getOnlyElement(call.getArguments());
if (argument instanceof Variable variable) {
JdbcTypeHandle typeHandle = ((JdbcColumnHandle) context.getAssignment(variable.getName())).getJdbcTypeHandle();
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(variable.getName());
JdbcTypeHandle typeHandle = columnHandle.getJdbcTypeHandle();
// TODO type.equals("vector") should be improved to support pushdown on vector type which is installed in other schemas
if (!typeHandle.jdbcTypeName().map(type -> type.equals("vector")).orElse(false)) {
return Optional.empty();
}
return Optional.of(new ParameterizedExpression(quoted(variable.getName()), ImmutableList.of()));
return Optional.of(new ParameterizedExpression(quoted(columnHandle.getColumnName()), ImmutableList.of()));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.trino.testing.QueryRunner;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TestView;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import java.util.stream.IntStream;
Expand Down Expand Up @@ -378,4 +379,18 @@ void testPgVectorUnsupportedCosineDistance()
.hasMessageContaining("invalid input");
}
}

@RepeatedTest(10) // Regression test for https://github.com/trinodb/trino/issues/23152
void testDuplicateColumnWithUnion()
{
try (TestTable table = new TestTable(postgreSqlServer::execute, "test_union", "(id int, v vector(3))")) {
postgreSqlServer.execute("INSERT INTO " + table.getName() + " VALUES (1, '[1,2,3]'), (2, '[4,5,6]')");

assertThat(query("" +
"SELECT id FROM " + table.getName() +
" UNION ALL " +
"(SELECT id FROM " + table.getName() + " ORDER BY cosine_distance(v, ARRAY[4,5,6]) LIMIT 1)"))
.matches("VALUES 1, 2, 2");
}
}
}
Loading