Skip to content

Commit 5795b26

Browse files
authored
[Fix][StarRocks] Fix NPE when upstream catalogtable table path only have table name part (#6540)
1 parent 505c125 commit 5795b26

File tree

3 files changed

+145
-5
lines changed

3 files changed

+145
-5
lines changed

Diff for: seatunnel-connectors-v2/connector-starrocks/src/main/java/org/apache/seatunnel/connectors/seatunnel/starrocks/sink/StarRocksSinkFactory.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ public TableSink createSink(TableSinkFactoryContext context) {
8989
String sinkDatabaseName = sinkConfig.getDatabase();
9090
String sinkTableName = sinkConfig.getTable();
9191
// to replace
92-
String finalDatabaseName =
93-
sinkDatabaseName.replace(REPLACE_DATABASE_NAME_KEY, sourceDatabaseName);
92+
sinkDatabaseName =
93+
sinkDatabaseName.replace(
94+
REPLACE_DATABASE_NAME_KEY,
95+
sourceDatabaseName != null ? sourceDatabaseName : "");
9496
String finalTableName = this.replaceFullTableName(sinkTableName, tableId);
9597
// rebuild TableIdentifier and catalogTable
9698
TableIdentifier newTableId =
9799
TableIdentifier.of(
98-
tableId.getCatalogName(), finalDatabaseName, null, finalTableName);
100+
tableId.getCatalogName(), sinkDatabaseName, null, finalTableName);
99101
catalogTable =
100102
CatalogTable.of(
101103
newTableId,
@@ -107,7 +109,7 @@ public TableSink createSink(TableSinkFactoryContext context) {
107109
CatalogTable finalCatalogTable = catalogTable;
108110
// reset
109111
sinkConfig.setTable(finalTableName);
110-
sinkConfig.setDatabase(finalDatabaseName);
112+
sinkConfig.setDatabase(sinkDatabaseName);
111113
return () -> new StarRocksSink(sinkConfig, finalCatalogTable, context.getOptions());
112114
}
113115

Diff for: seatunnel-e2e/seatunnel-connector-v2-e2e/connector-starrocks-e2e/src/test/java/org/apache/seatunnel/e2e/connector/starrocks/StarRocksIT.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ public class StarRocksIT extends TestSuiteBase implements TestResource {
110110
+ "\"storage_format\" = \"DEFAULT\""
111111
+ ")";
112112

113+
private static final String DDL_FAKE_SINK_TABLE =
114+
"create table "
115+
+ DATABASE
116+
+ "."
117+
+ "fake_table_sink"
118+
+ " (\n"
119+
+ " id BIGINT,\n"
120+
+ " c_string STRING,\n"
121+
+ " c_boolean BOOLEAN,\n"
122+
+ " c_tinyint TINYINT,\n"
123+
+ " c_int INT,\n"
124+
+ " c_bigint BIGINT,\n"
125+
+ " c_float FLOAT,\n"
126+
+ " c_double DOUBLE,\n"
127+
+ " c_decimal Decimal(2, 1),\n"
128+
+ " c_date DATE\n"
129+
+ ")ENGINE=OLAP\n"
130+
+ "DUPLICATE KEY(`id`)\n"
131+
+ "DISTRIBUTED BY HASH(`id`) BUCKETS 1\n"
132+
+ "PROPERTIES (\n"
133+
+ "\"replication_num\" = \"1\",\n"
134+
+ "\"in_memory\" = \"false\","
135+
+ "\"storage_format\" = \"DEFAULT\""
136+
+ ")";
137+
113138
private static final String INIT_DATA_SQL =
114139
"insert into "
115140
+ DATABASE
@@ -253,6 +278,13 @@ public void testStarRocksSink(TestContainer container)
253278
}
254279
}
255280

281+
@TestTemplate
282+
public void testSinkWithCatalogTableNameOnly(TestContainer container)
283+
throws IOException, InterruptedException {
284+
Container.ExecResult execResult = container.executeJob("/fake-to-starrocks.conf");
285+
Assertions.assertEquals(0, execResult.getExitCode(), execResult.getStderr());
286+
}
287+
256288
private void initializeJdbcConnection()
257289
throws SQLException, ClassNotFoundException, MalformedURLException,
258290
InstantiationException, IllegalAccessException {
@@ -274,7 +306,7 @@ private void initializeJdbcTable() {
274306
// create source table
275307
statement.execute(DDL_SOURCE);
276308
// create sink table
277-
// statement.execute(DDL_SINK);
309+
statement.execute(DDL_FAKE_SINK_TABLE);
278310
} catch (SQLException e) {
279311
throw new RuntimeException("Initializing table failed!", e);
280312
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
env {
19+
parallelism = 1
20+
job.mode = "BATCH"
21+
}
22+
23+
source {
24+
FakeSource {
25+
parallelism = 1
26+
result_table_name = "fake"
27+
row.num = 100
28+
schema {
29+
table = "FakeTable"
30+
columns = [
31+
{
32+
name = id
33+
type = bigint
34+
nullable = false
35+
defaultValue = 0
36+
},
37+
{
38+
name = c_string
39+
type = string
40+
nullable = true
41+
},
42+
{
43+
name = c_boolean
44+
type = boolean
45+
nullable = true
46+
},
47+
{
48+
name = c_tinyint
49+
type = tinyint
50+
nullable = true
51+
},
52+
{
53+
name = c_int
54+
type = int
55+
nullable = true
56+
},
57+
{
58+
name = c_bigint
59+
type = bigint
60+
nullable = true
61+
},
62+
{
63+
name = c_float
64+
type = float
65+
nullable = true
66+
},
67+
{
68+
name = c_double
69+
type = double
70+
nullable = true
71+
},
72+
{
73+
name = c_decimal
74+
type = "decimal(2, 1)"
75+
nullable = true
76+
},
77+
{
78+
name = c_date
79+
type = date
80+
nullable = true
81+
}
82+
]
83+
}
84+
}
85+
}
86+
87+
transform {
88+
}
89+
90+
sink {
91+
StarRocks {
92+
source_table_name = "fake"
93+
nodeUrls = ["starrocks_e2e:8030"]
94+
username = root
95+
password = ""
96+
database = "test"
97+
table = "fake_table_sink"
98+
batch_max_rows = 100
99+
max_retries = 3
100+
base-url="jdbc:mysql://starrocks_e2e:9030/test"
101+
starrocks.config = {
102+
format = "JSON"
103+
strip_outer_array = true
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)