Skip to content
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 @@ -51,6 +51,7 @@
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -59,6 +60,7 @@
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.google.common.base.MoreObjects.toStringHelper;
Expand All @@ -82,6 +84,7 @@
import static io.trino.type.JsonType.JSON;
import static java.lang.Float.floatToRawIntBits;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

public class MaterializedResult
Expand Down Expand Up @@ -226,6 +229,53 @@ public String toString()
.toString();
}

public MaterializedResult exceptColumns(String... columnNamesToExclude)
{
validateIfColumnsPresent(columnNamesToExclude);
checkArgument(columnNamesToExclude.length > 0, "At least one column must be excluded");
checkArgument(columnNamesToExclude.length < getColumnNames().size(), "All columns cannot be excluded");
return projected(((Predicate<String>) Set.of(columnNamesToExclude)::contains).negate());
}

public MaterializedResult project(String... columnNamesToInclude)
{
validateIfColumnsPresent(columnNamesToInclude);
checkArgument(columnNamesToInclude.length > 0, "At least one column must be projected");
return projected(Set.of(columnNamesToInclude)::contains);
}

private void validateIfColumnsPresent(String... columns)
{
Set<String> columnNames = ImmutableSet.copyOf(getColumnNames());
for (String column : columns) {
checkArgument(columnNames.contains(column), "[%s] column is not present in %s".formatted(column, columnNames));
}
}

private MaterializedResult projected(Predicate<String> columnFilter)
{
List<String> columnNames = getColumnNames();
Map<Integer, String> columnsIndexToNameMap = new HashMap<>();
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.get(i);
if (columnFilter.test(columnName)) {
columnsIndexToNameMap.put(i, columnName);
}
}

return new MaterializedResult(
getMaterializedRows().stream()
.map(row -> new MaterializedRow(
row.getPrecision(),
columnsIndexToNameMap.keySet().stream()
.map(row::getField)
.collect(toList()))) // values are nullable
.collect(toImmutableList()),
columnsIndexToNameMap.keySet().stream()
.map(getTypes()::get)
.collect(toImmutableList()));
}

public Stream<Object> getOnlyColumn()
{
checkState(types.size() == 1, "result set must have exactly one column");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import io.trino.Session;
import io.trino.execution.warnings.WarningCollector;
Expand Down Expand Up @@ -52,15 +51,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
import static io.trino.cost.StatsCalculator.noopStatsCalculator;
import static io.trino.execution.querystats.PlanOptimizersStatsCollector.createPlanOptimizersStatsCollector;
Expand Down Expand Up @@ -329,57 +324,28 @@ private QueryAssert(

public QueryAssert exceptColumns(String... columnNamesToExclude)
{
validateIfColumnsPresent(columnNamesToExclude);
checkArgument(columnNamesToExclude.length > 0, "At least one column must be excluded");
checkArgument(columnNamesToExclude.length < actual.getColumnNames().size(), "All columns cannot be excluded");
return projected(((Predicate<String>) Set.of(columnNamesToExclude)::contains).negate());
return new QueryAssert(
runner,
session,
format("%s except columns %s", query, Arrays.toString(columnNamesToExclude)),
actual.exceptColumns(columnNamesToExclude),
ordered,
skipTypesCheck,
skipResultsCorrectnessCheckForPushdown);
}

public QueryAssert projected(String... columnNamesToInclude)
{
validateIfColumnsPresent(columnNamesToInclude);
checkArgument(columnNamesToInclude.length > 0, "At least one column must be projected");
return projected(Set.of(columnNamesToInclude)::contains);
}

private QueryAssert projected(Predicate<String> columnFilter)
{
List<String> columnNames = actual.getColumnNames();
Map<Integer, String> columnsIndexToNameMap = new HashMap<>();
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.get(i);
if (columnFilter.test(columnName)) {
columnsIndexToNameMap.put(i, columnName);
}
}

return new QueryAssert(
runner,
session,
format("%s projected with %s", query, columnsIndexToNameMap.values()),
new MaterializedResult(
actual.getMaterializedRows().stream()
.map(row -> new MaterializedRow(
row.getPrecision(),
columnsIndexToNameMap.keySet().stream()
.map(row::getField)
.collect(toList()))) // values are nullable
.collect(toImmutableList()),
columnsIndexToNameMap.keySet().stream()
.map(actual.getTypes()::get)
.collect(toImmutableList())),
format("%s projected with %s", query, Arrays.toString(columnNamesToInclude)),
actual.project(columnNamesToInclude),
ordered,
skipTypesCheck,
skipResultsCorrectnessCheckForPushdown);
}

private void validateIfColumnsPresent(String... columns)
{
Set<String> columnNames = ImmutableSet.copyOf(actual.getColumnNames());
Arrays.stream(columns)
.forEach(column -> checkArgument(columnNames.contains(column), "[%s] column is not present in %s".formatted(column, columnNames)));
}

public QueryAssert matches(BiFunction<Session, QueryRunner, MaterializedResult> evaluator)
{
MaterializedResult expected = evaluator.apply(session, runner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,11 @@ public void testView()
String schemaName = getSession().getSchema().orElseThrow();
String testView = "test_view_" + randomNameSuffix();
String testViewWithComment = "test_view_with_comment_" + randomNameSuffix();
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet()) // prime the cache, if any
.doesNotContain(testView);
assertUpdate("CREATE VIEW " + testView + " AS SELECT 123 x");
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet())
.contains(testView);
assertUpdate("CREATE OR REPLACE VIEW " + testView + " AS " + query);

assertUpdate("CREATE VIEW " + testViewWithComment + " COMMENT 'orders' AS SELECT 123 x");
Expand Down Expand Up @@ -951,6 +955,8 @@ public void testView()
"CROSS JOIN UNNEST(ARRAY['orderkey', 'orderstatus', 'half'])");

assertUpdate("DROP VIEW " + testView);
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet())
.doesNotContain(testView);
}

@Test
Expand Down Expand Up @@ -2864,13 +2870,19 @@ public void testCreateTable()
return;
}

assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet()) // prime the cache, if any
.doesNotContain(tableName);
assertUpdate("CREATE TABLE " + tableName + " (a bigint, b double, c varchar(50))");
assertTrue(getQueryRunner().tableExists(getSession(), tableName));
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet())
.contains(tableName);
assertTableColumnNames(tableName, "a", "b", "c");
assertNull(getTableComment(getSession().getCatalog().orElseThrow(), getSession().getSchema().orElseThrow(), tableName));

assertUpdate("DROP TABLE " + tableName);
assertFalse(getQueryRunner().tableExists(getSession(), tableName));
Comment thread
hashhar marked this conversation as resolved.
Outdated
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet())
.doesNotContain(tableName);

assertQueryFails("CREATE TABLE " + tableName + " (a bad_type)", ".* Unknown type 'bad_type' for column 'a'");
assertFalse(getQueryRunner().tableExists(getSession(), tableName));
Expand Down Expand Up @@ -3519,14 +3531,16 @@ public void testCommentTable()
String catalogName = getSession().getCatalog().orElseThrow();
String schemaName = getSession().getSchema().orElseThrow();
try (TestTable table = new TestTable(getQueryRunner()::execute, "test_comment_", "(a integer)")) {
// comment initially not set
assertThat(getTableComment(catalogName, schemaName, table.getName())).isEqualTo(null);

// comment set
assertUpdate("COMMENT ON TABLE " + table.getName() + " IS 'new comment'");
assertThat((String) computeScalar("SHOW CREATE TABLE " + table.getName())).contains("COMMENT 'new comment'");
assertThat(getTableComment(catalogName, schemaName, table.getName())).isEqualTo("new comment");
assertThat(query(
"SELECT table_name, comment FROM system.metadata.table_comments " +
"WHERE catalog_name = '" + catalogName + "' AND " +
"schema_name = '" + schemaName + "'"))
"WHERE catalog_name = '" + catalogName + "' AND schema_name = '" + schemaName + "'")) // without table_name filter
.skippingTypesCheck()
.containsAll("VALUES ('" + table.getName() + "', 'new comment')");

Expand Down