Skip to content

Commit 6d6fb16

Browse files
authored
Merge pull request #301 from civitaspo/flexible-temp-table-suffix
Make temp tables suffix length flexible
2 parents 7a433dc + 89a63aa commit 6d6fb16

File tree

6 files changed

+90
-12
lines changed

6 files changed

+90
-12
lines changed

embulk-output-jdbc/src/main/java/org/embulk/output/jdbc/AbstractJdbcOutputPlugin.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.embulk.util.retryhelper.RetryExecutor;
6060
import org.embulk.util.retryhelper.RetryGiveupException;
6161
import org.embulk.util.retryhelper.Retryable;
62+
import org.msgpack.core.annotations.VisibleForTesting;
6263
import org.embulk.util.config.Config;
6364
import org.embulk.util.config.ConfigDefault;
6465
import org.embulk.util.config.ConfigMapper;
@@ -693,10 +694,10 @@ public List<TableIdentifier> call() throws Exception
693694
{
694695
intermTables = new ArrayList<>();
695696
if (task.getMode().tempTablePerTask()) {
696-
String namePrefix = generateIntermediateTableNamePrefix(task.getActualTable().getTableName(), con, 3,
697+
String tableNameFormat = generateIntermediateTableNameFormat(task.getActualTable().getTableName(), con, taskCount,
697698
task.getFeatures().getMaxTableNameLength(), task.getFeatures().getTableNameLengthSemantics());
698699
for (int taskIndex = 0; taskIndex < taskCount; taskIndex++) {
699-
String tableName = namePrefix + String.format("%03d", taskIndex % 1000);
700+
String tableName = String.format(tableNameFormat, taskIndex);
700701
table = buildIntermediateTableId(con, task, tableName);
701702
// if table already exists, SQLException will be thrown
702703
con.createTable(table, newTableSchema, task.getCreateTableConstraint(), task.getCreateTableOption());
@@ -760,6 +761,25 @@ private void dropTables() throws SQLException
760761
}
761762
}
762763

764+
@VisibleForTesting
765+
static int calculateSuffixLength(int taskCount)
766+
{
767+
assert(taskCount >= 0);
768+
// NOTE: for backward compatibility
769+
// See. https://github.com/embulk/embulk-output-jdbc/pull/301
770+
int minimumLength = 3;
771+
return Math.max(minimumLength, String.valueOf(taskCount - 1).length());
772+
}
773+
774+
protected String generateIntermediateTableNameFormat(String baseTableName, JdbcOutputConnection con,
775+
int taskCount, int maxLength, LengthSemantics lengthSemantics) throws SQLException
776+
{
777+
int suffixLength = calculateSuffixLength(taskCount);
778+
String prefix = generateIntermediateTableNamePrefix(baseTableName, con, suffixLength, maxLength, lengthSemantics);
779+
String suffixFormat = "%0" + suffixLength + "d";
780+
return prefix + suffixFormat;
781+
}
782+
763783
protected String generateIntermediateTableNamePrefix(String baseTableName, JdbcOutputConnection con,
764784
int suffixLength, int maxLength, LengthSemantics lengthSemantics) throws SQLException
765785
{

embulk-output-jdbc/src/test/java/org/embulk/output/TestJdbcOutputPlugin.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

embulk-output-jdbc/src/test/java/org/embulk/output/AbstractJdbcOutputPluginTest.java renamed to embulk-output-jdbc/src/test/java/org/embulk/output/jdbc/AbstractJdbcOutputPluginTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.embulk.output;
1+
package org.embulk.output.jdbc;
22

33
import java.io.File;
44
import java.io.FileInputStream;
@@ -24,8 +24,8 @@
2424

2525
import org.embulk.config.ConfigException;
2626
import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
27-
import org.embulk.output.tester.EmbulkPluginTester;
28-
import org.embulk.output.tester.EmbulkPluginTester.PluginDefinition;
27+
import org.embulk.output.jdbc.tester.EmbulkPluginTester;
28+
import org.embulk.output.jdbc.tester.EmbulkPluginTester.PluginDefinition;
2929
import org.yaml.snakeyaml.Yaml;
3030

3131
import static java.util.Locale.ENGLISH;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.embulk.output.jdbc;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class TestJdbcOutputPlugin
8+
{
9+
@Test
10+
public void testCalculateSuffixLength()
11+
{
12+
{
13+
try {
14+
AbstractJdbcOutputPlugin.calculateSuffixLength(-1);
15+
} catch (java.lang.AssertionError e) {
16+
assert(true);
17+
}
18+
}
19+
{
20+
try {
21+
AbstractJdbcOutputPlugin.calculateSuffixLength(0);
22+
} catch (java.lang.AssertionError e) {
23+
assert(true);
24+
}
25+
}
26+
{
27+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(1);
28+
assertEquals(3, i);
29+
}
30+
{
31+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(10);
32+
assertEquals(3, i);
33+
}
34+
{
35+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(11);
36+
assertEquals(3, i);
37+
}
38+
{
39+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(100);
40+
assertEquals(3, i);
41+
}
42+
{
43+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(101);
44+
assertEquals(3, i);
45+
}
46+
{
47+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(1000);
48+
assertEquals(3, i);
49+
}
50+
{
51+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(1001);
52+
assertEquals(4, i);
53+
}
54+
{
55+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(10000);
56+
assertEquals(4, i);
57+
}
58+
{
59+
int i = AbstractJdbcOutputPlugin.calculateSuffixLength(10001);
60+
assertEquals(5, i);
61+
}
62+
}
63+
}

embulk-output-jdbc/src/test/java/org/embulk/output/TimestampFormatTest.java renamed to embulk-output-jdbc/src/test/java/org/embulk/output/jdbc/TimestampFormatTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.embulk.output;
1+
package org.embulk.output.jdbc;
22

33
import java.sql.Timestamp;
44
import java.text.ParseException;

embulk-output-jdbc/src/test/java/org/embulk/output/tester/EmbulkPluginTester.java renamed to embulk-output-jdbc/src/test/java/org/embulk/output/jdbc/tester/EmbulkPluginTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.embulk.output.tester;
1+
package org.embulk.output.jdbc.tester;
22

33
import java.util.ArrayList;
44
import java.util.Collections;

0 commit comments

Comments
 (0)