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

Use TestTable in faker #24088

Merged
merged 2 commits into from
Nov 11, 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
7 changes: 7 additions & 0 deletions plugin/trino-faker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-main</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-memory</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_PROPERTY;
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_REFERENCE;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.StandardErrorCode.SCHEMA_ALREADY_EXISTS;
import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_FOUND;
Expand Down Expand Up @@ -263,7 +264,7 @@ public synchronized void setColumnComment(ConnectorSession session, ConnectorTab
.map(columnInfo -> {
if (columnInfo.handle().equals(column)) {
if (ROW_ID_COLUMN_NAME.equals(columnInfo.handle().name())) {
throw new IllegalArgumentException(String.format("Cannot set comment for %s column", ROW_ID_COLUMN_NAME));
throw new TrinoException(INVALID_COLUMN_REFERENCE, "Cannot set comment for %s column".formatted(ROW_ID_COLUMN_NAME));
}
return columnInfo.withComment(comment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import io.trino.testing.AbstractTestQueryFramework;
import io.trino.testing.QueryRunner;
import io.trino.testing.sql.TestTable;
import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;

import static io.trino.plugin.faker.FakerSplitManager.MAX_ROWS_PER_SPLIT;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_REFERENCE;
import static org.assertj.core.api.Assertions.assertThat;

final class TestFakerQueries
extends AbstractTestQueryFramework
Expand All @@ -42,26 +44,21 @@ void testShowTables()
@Test
void testColumnComment()
{
assertUpdate("CREATE TABLE faker.default.comments (id INTEGER, name VARCHAR)");
assertUpdate("COMMENT ON COLUMN comments.name IS 'comment text'");

assertTableColumnNames("faker.default.comments", "id", "name");

assertUpdate("DROP TABLE faker.default.comments");
try (TestTable table = new TestTable(getQueryRunner()::execute, "comment", "(id INTEGER, name VARCHAR)")) {
assertUpdate("COMMENT ON COLUMN %s.name IS 'comment text'".formatted(table.getName()));
assertQuery("SHOW COLUMNS FROM " + table.getName(), "VALUES ('id', 'integer', '', ''), ('name', 'varchar', '', 'comment text')");
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}
}

@Test
void testCannotCommentRowId()
{
assertUpdate("CREATE TABLE faker.default.cannot_comment (id INTEGER, name VARCHAR)");
@Language("SQL")
String testQuery = "COMMENT ON COLUMN \"cannot_comment\".\"$row_id\" IS 'comment text'";

assertThatThrownBy(() -> getQueryRunner().execute(testQuery))
.as("Query: " + testQuery)
.hasMessageContaining("Cannot set comment for $row_id column");

assertUpdate("DROP TABLE faker.default.cannot_comment");
try (TestTable table = new TestTable(getQueryRunner()::execute, "cannot_comment", "(id INTEGER, name VARCHAR)")) {
assertThat(query("COMMENT ON COLUMN \"%s\".\"$row_id\" IS 'comment text'".formatted(table.getName())))
.failure()
.hasErrorCode(INVALID_COLUMN_REFERENCE)
.hasMessageContaining("Cannot set comment for $row_id column");
}
}

@Test
Expand Down Expand Up @@ -212,90 +209,67 @@ rnd_nchar char(1000) NOT NULL,
@Test
void testSelectLimit()
{
assertUpdate("CREATE TABLE faker.default.single_column (rnd_bigint bigint NOT NULL)");

assertQuery("SELECT count(rnd_bigint) FROM (SELECT rnd_bigint FROM single_column LIMIT 5) a",
"VALUES (5)");

assertQuery("""
SELECT count(rnd_bigint)
FROM (SELECT rnd_bigint FROM single_column LIMIT %d) a""".formatted(2 * MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(2 * MAX_ROWS_PER_SPLIT));

assertQuery("SELECT count(distinct rnd_bigint) FROM single_column LIMIT 5",
"VALUES (1000)");

assertQuery("""
SELECT count(rnd_bigint)
FROM (SELECT rnd_bigint FROM single_column LIMIT %d) a""".formatted(MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(MAX_ROWS_PER_SPLIT));

// generating data should be deterministic
String testQuery = """
SELECT to_hex(checksum(rnd_bigint))
FROM (SELECT rnd_bigint FROM single_column LIMIT %d) a""".formatted(3 * MAX_ROWS_PER_SPLIT);
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");

// there should be no overlap between data generated from different splits
assertQuery("""
SELECT count(1)
FROM (SELECT rnd_bigint FROM single_column LIMIT %d) a
JOIN (SELECT rnd_bigint FROM single_column LIMIT %d) b ON a.rnd_bigint = b.rnd_bigint""".formatted(2 * MAX_ROWS_PER_SPLIT, 5 * MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(2 * MAX_ROWS_PER_SPLIT));

assertUpdate("DROP TABLE faker.default.single_column");
try (TestTable table = new TestTable(getQueryRunner()::execute, "single_column", "(rnd_bigint bigint NOT NULL)")) {
assertQuery("SELECT count(rnd_bigint) FROM (SELECT rnd_bigint FROM %s LIMIT 5) a".formatted(table.getName()),
"VALUES (5)");

assertQuery("""
SELECT count(rnd_bigint)
FROM (SELECT rnd_bigint FROM %s LIMIT %d) a""".formatted(table.getName(), 2 * MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(2 * MAX_ROWS_PER_SPLIT));

assertQuery("SELECT count(distinct rnd_bigint) FROM %s LIMIT 5".formatted(table.getName()),
"VALUES (1000)");

assertQuery("""
SELECT count(rnd_bigint)
FROM (SELECT rnd_bigint FROM %s LIMIT %d) a""".formatted(table.getName(), MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(MAX_ROWS_PER_SPLIT));

// generating data should be deterministic
String testQuery = """
SELECT to_hex(checksum(rnd_bigint))
FROM (SELECT rnd_bigint FROM %s LIMIT %d) a""".formatted(table.getName(), 3 * MAX_ROWS_PER_SPLIT);
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");
assertQuery(testQuery, "VALUES ('1FB3289AC3A44EEA')");

// there should be no overlap between data generated from different splits
assertQuery("""
SELECT count(1)
FROM (SELECT rnd_bigint FROM %s LIMIT %d) a
JOIN (SELECT rnd_bigint FROM %s LIMIT %d) b ON a.rnd_bigint = b.rnd_bigint""".formatted(table.getName(), 2 * MAX_ROWS_PER_SPLIT, table.getName(), 5 * MAX_ROWS_PER_SPLIT),
"VALUES (%d)".formatted(2 * MAX_ROWS_PER_SPLIT));
}
}

@Test
void testSelectDefaultTableLimit()
{
@Language("SQL")
String tableQuery = "CREATE TABLE faker.default.default_table_limit (rnd_bigint bigint NOT NULL) WITH (default_limit = 100)";
assertUpdate(tableQuery);

@Language("SQL")
String testQuery = "SELECT count(distinct rnd_bigint) FROM default_table_limit";
assertQuery(testQuery, "VALUES (100)");

assertUpdate("DROP TABLE faker.default.default_table_limit");
try (TestTable table = new TestTable(getQueryRunner()::execute, "default_table_limit", "(rnd_bigint bigint NOT NULL) WITH (default_limit = 100)")) {
assertQuery("SELECT count(distinct rnd_bigint) FROM " + table.getName(), "VALUES (100)");
}
}

@Test
public void selectOnlyNulls()
{
@Language("SQL")
String tableQuery = "CREATE TABLE faker.default.only_nulls (rnd_bigint bigint) WITH (null_probability = 1.0)";
assertUpdate(tableQuery);
tableQuery = "CREATE TABLE faker.default.only_nulls_column (rnd_bigint bigint WITH (null_probability = 1.0))";
assertUpdate(tableQuery);

@Language("SQL")
String testQuery = "SELECT count(distinct rnd_bigint) FROM only_nulls";
assertQuery(testQuery, "VALUES (0)");
testQuery = "SELECT count(distinct rnd_bigint) FROM only_nulls_column";
assertQuery(testQuery, "VALUES (0)");

assertUpdate("DROP TABLE faker.default.only_nulls");
assertUpdate("DROP TABLE faker.default.only_nulls_column");
try (TestTable table = new TestTable(getQueryRunner()::execute, "only_nulls", "(rnd_bigint bigint) WITH (null_probability = 1.0)")) {
assertQuery("SELECT count(distinct rnd_bigint) FROM " + table.getName(), "VALUES (0)");
}
}

@Test
void testSelectGenerator()
{
@Language("SQL")
String tableQuery = "CREATE TABLE faker.default.generators (" +
"name VARCHAR NOT NULL WITH (generator = '#{Name.first_name} #{Name.last_name}'), " +
"age_years INTEGER NOT NULL" +
")";
assertUpdate(tableQuery);

@Language("SQL")
String testQuery = "SELECT count(name) FILTER (WHERE LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) = 1) FROM generators";
assertQuery(testQuery, "VALUES (1000)");

assertUpdate("DROP TABLE faker.default.generators");
try (TestTable table = new TestTable(getQueryRunner()::execute, "generators", """
(
name VARCHAR NOT NULL WITH (generator = '#{Name.first_name} #{Name.last_name}'),
age_years INTEGER NOT NULL
)
""")) {
assertQuery("SELECT count(name) FILTER (WHERE LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) = 1) FROM " + table.getName(), "VALUES (1000)");
}
}

@Test
Expand Down