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

Populate TestTable with multi-row INSERT #18693

Merged
merged 10 commits into from
Aug 30, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,24 @@
package io.trino.plugin.bigquery;

import io.trino.testing.sql.SqlExecutor;
import io.trino.testing.sql.TestTable;

import java.util.List;
import io.trino.testing.sql.TemporaryRelation;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class BigQueryTestView
extends TestTable
implements TemporaryRelation
{
private final TestTable table;
private final SqlExecutor sqlExecutor;
private final TemporaryRelation relation;
private final String viewName;

public BigQueryTestView(SqlExecutor sqlExecutor, TestTable table)
{
super(sqlExecutor, table.getName(), null);
this.table = requireNonNull(table, "table is null");
this.viewName = table.getName() + "_view";
}

@Override
public void createAndInsert(List<String> rowsToInsert) {}

public void createView()
public BigQueryTestView(SqlExecutor sqlExecutor, TemporaryRelation relation)
{
sqlExecutor.execute(format("CREATE VIEW %s AS SELECT * FROM %s", viewName, table.getName()));
this.sqlExecutor = requireNonNull(sqlExecutor, "sqlExecutor is null");
this.relation = requireNonNull(relation, "relation is null");
this.viewName = relation.getName() + "_view";
sqlExecutor.execute(format("CREATE VIEW %s AS SELECT * FROM %s", viewName, relation.getName()));
}

@Override
Expand All @@ -51,7 +43,8 @@ public String getName()
@Override
public void close()
{
sqlExecutor.execute("DROP TABLE " + table.getName());
sqlExecutor.execute("DROP VIEW " + viewName);
try (relation) {
sqlExecutor.execute("DROP VIEW " + viewName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import io.trino.testing.datatype.ColumnSetup;
import io.trino.testing.datatype.CreateAndInsertDataSetup;
import io.trino.testing.sql.SqlExecutor;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TemporaryRelation;

import java.util.List;

import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static java.util.Objects.requireNonNull;

public class BigQueryViewCreateAndInsertDataSetup
Expand All @@ -34,11 +35,15 @@ public BigQueryViewCreateAndInsertDataSetup(SqlExecutor sqlExecutor, String tabl
}

@Override
public TestTable setupTemporaryRelation(List<ColumnSetup> inputs)
public TemporaryRelation setupTemporaryRelation(List<ColumnSetup> inputs)
{
TestTable table = super.setupTemporaryRelation(inputs);
BigQueryTestView view = new BigQueryTestView(sqlExecutor, table);
view.createView();
return view;
TemporaryRelation table = super.setupTemporaryRelation(inputs);
try {
return new BigQueryTestView(sqlExecutor, table);
}
catch (Throwable e) {
closeAllSuppress(e, table);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2321,8 +2321,11 @@ public void testPartitionFilterRequiredAndTableChanges()
try (TestTable table = new TestTable(
getQueryRunner()::execute,
"test_partition_filter_table_changes",
"(x integer, part integer) WITH (partitioned_by = ARRAY['part'], change_data_feed_enabled = true)",
ImmutableList.of("(1, 11)", "(2, 22)", "(3, 33)"))) {
"(x integer, part integer) WITH (partitioned_by = ARRAY['part'], change_data_feed_enabled = true)")) {
assertUpdate("INSERT INTO " + table.getName() + " VALUES (1, 11)", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (2, 22)", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (3, 33)", 1);

@Language("RegExp")
String expectedMessageRegExp = "Filter required on test_schema\\." + table.getName() + " for at least one partition column: part";

Expand Down Expand Up @@ -2367,8 +2370,11 @@ public void testPartitionFilterRequiredAndHistoryTable()
try (TestTable table = new TestTable(
getQueryRunner()::execute,
"test_partition_filter_table_changes",
"(x integer, part integer) WITH (partitioned_by = ARRAY['part'], change_data_feed_enabled = true)",
ImmutableList.of("(1, 11)", "(2, 22)", "(3, 33)"))) {
"(x integer, part integer) WITH (partitioned_by = ARRAY['part'], change_data_feed_enabled = true)")) {
assertUpdate("INSERT INTO " + table.getName() + " VALUES (1, 11)", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (2, 22)", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (3, 33)", 1);

@Language("RegExp")
String expectedMessageRegExp = "Filter required on test_schema\\." + table.getName() + " for at least one partition column: part";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,11 @@ public void testMetadataTables()
try (TestTable table = new TestTable(
getQueryRunner()::execute,
"test_metadata_tables",
"(id int, part varchar) WITH (partitioning = ARRAY['part'])",
ImmutableList.of("1, 'p1'", "2, 'p1'", "3, 'p2'"))) {
"(id int, part varchar) WITH (partitioning = ARRAY['part'])")) {
assertUpdate("INSERT INTO " + table.getName() + " VALUES (1, 'p1')", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (2, 'p1')", 1);
assertUpdate("INSERT INTO " + table.getName() + " VALUES (3, 'p2')", 1);

List<Long> snapshotIds = computeActual("SELECT snapshot_id FROM \"" + table.getName() + "$snapshots\" ORDER BY committed_at DESC")
.getOnlyColumn()
.map(Long.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.trino.testing.datatype.ColumnSetup;
import io.trino.testing.datatype.DataSetup;
import io.trino.testing.sql.TemporaryRelation;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TrinoSqlExecutor;
import org.bson.Document;

Expand Down Expand Up @@ -46,7 +45,7 @@ public MongoCreateAndInsertDataSetup(TrinoSqlExecutor trinoSqlExecutor, MongoCli
@Override
public TemporaryRelation setupTemporaryRelation(List<ColumnSetup> inputs)
{
TestTable testTable = new MongoTestTable(trinoSqlExecutor, tableNamePrefix);
MongoTestTable testTable = new MongoTestTable(trinoSqlExecutor, tableNamePrefix);
try {
insertRows(testTable, inputs);
}
Expand All @@ -57,7 +56,7 @@ public TemporaryRelation setupTemporaryRelation(List<ColumnSetup> inputs)
return testTable;
}

private void insertRows(TestTable testTable, List<ColumnSetup> inputs)
private void insertRows(MongoTestTable testTable, List<ColumnSetup> inputs)
{
int i = 0;
StringBuilder json = new StringBuilder("{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,32 @@
package io.trino.plugin.mongodb;

import io.trino.testing.sql.SqlExecutor;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TemporaryRelation;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.util.Objects.requireNonNull;

public class MongoTestTable
extends TestTable
implements TemporaryRelation
{
private final SqlExecutor sqlExecutor;
private final String name;

public MongoTestTable(SqlExecutor sqlExecutor, String namePrefix)
{
super(sqlExecutor, namePrefix, null);
this.sqlExecutor = requireNonNull(sqlExecutor, "sqlExecutor is null");
this.name = requireNonNull(namePrefix, "namePrefix is null") + randomNameSuffix();
}

@Override
public String getName()
{
return name;
}

@Override
public void createAndInsert(List<String> rowsToInsert)
public void close()
{
checkArgument(rowsToInsert.isEmpty(), "rowsToInsert must be empty");
sqlExecutor.execute("DROP TABLE " + name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ private String getLongInClause(int start, int length)
@Override
protected SqlExecutor onRemoteDatabase()
{
return oracleServer::execute;
return new SqlExecutor() {
@Override
public boolean supportsMultiRowInsert()
{
return false;
}

@Override
public void execute(String sql)
{
oracleServer.execute(sql);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public PhoenixSqlExecutor(String jdbcUrl, Properties jdbcProperties)
this.jdbcProperties.putAll(requireNonNull(jdbcProperties, "jdbcProperties is null"));
}

@Override
public boolean supportsMultiRowInsert()
{
return false;
}

@Override
public void execute(String sql)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public PhoenixTestTable(SqlExecutor sqlExecutor, String namePrefix, String table
}

@Override
public void createAndInsert(List<String> rowsToInsert)
protected void createAndInsert(List<String> rowsToInsert)
{
sqlExecutor.execute(format("CREATE TABLE %s %s", name, tableDefinition));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.testing.datatype;

import io.trino.testing.sql.SqlExecutor;
import io.trino.testing.sql.TemporaryRelation;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TrinoSqlExecutor;

Expand All @@ -38,7 +39,7 @@ public CreateAndInsertDataSetup(SqlExecutor sqlExecutor, String tableNamePrefix)
}

@Override
public TestTable setupTemporaryRelation(List<ColumnSetup> inputs)
public TemporaryRelation setupTemporaryRelation(List<ColumnSetup> inputs)
{
TestTable testTable = createTestTable(inputs);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@

public interface SqlExecutor
{
default boolean supportsMultiRowInsert()
{
return true;
}

void execute(String sql);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

public class TestTable
implements TemporaryRelation
Expand All @@ -38,19 +40,27 @@ public TestTable(SqlExecutor sqlExecutor, String namePrefix, String tableDefinit

public TestTable(SqlExecutor sqlExecutor, String namePrefix, String tableDefinition, List<String> rowsToInsert)
{
this.sqlExecutor = sqlExecutor;
this.name = namePrefix + randomNameSuffix();
this.tableDefinition = tableDefinition;
this.sqlExecutor = requireNonNull(sqlExecutor, "sqlExecutor is null");
this.name = requireNonNull(namePrefix, "namePrefix is null") + randomNameSuffix();
this.tableDefinition = requireNonNull(tableDefinition, "tableDefinition is null");
createAndInsert(rowsToInsert);
}

public void createAndInsert(List<String> rowsToInsert)
protected void createAndInsert(List<String> rowsToInsert)
{
sqlExecutor.execute(format("CREATE TABLE %s %s", name, tableDefinition));
try {
for (String row : rowsToInsert) {
// some databases do not support multi value insert statement
sqlExecutor.execute(format("INSERT INTO %s VALUES (%s)", name, row));
if (!rowsToInsert.isEmpty()) {
if (sqlExecutor.supportsMultiRowInsert()) {
sqlExecutor.execute(format("INSERT INTO %s VALUES %s", name, rowsToInsert.stream()
.map("(%s)"::formatted)
.collect(joining(", "))));
}
else {
for (String row : rowsToInsert) {
sqlExecutor.execute(format("INSERT INTO %s VALUES (%s)", name, row));
}
}
}
}
catch (Exception e) {
Expand Down