Skip to content

Commit

Permalink
Populate TestTable with multi-row INSERT
Browse files Browse the repository at this point in the history
Use single INSERT instead of multiple single-row inserts. Should be
faster, especially e.g. for Trino.
  • Loading branch information
findepi committed Aug 30, 2023
1 parent f9483bb commit f12bf10
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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 @@ -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 @@ -24,6 +24,7 @@
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 @@ -49,9 +50,17 @@ 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

0 comments on commit f12bf10

Please sign in to comment.