Skip to content

Commit 362a142

Browse files
committed
fix(odps-sdk): 修改字段名引用格式并增加新测试用例
此次修复调整了多个类中的字段名引用格式,增加了新的测试用例,并修正了一些测试方法中的异常处理逻辑。主要涉及 `TableCreatorTest`, `TypeInfoParserTest`, `SimpleStructTypeInfo`, `Tables`, 和其他相关类。
1 parent 5721a74 commit 362a142

File tree

12 files changed

+73
-31
lines changed

12 files changed

+73
-31
lines changed

odps-sdk-impl/odps-common-local/src/test/java/com/aliyun/odps/local/common/utils/SchemaUtilsTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.aliyun.odps.local.common.utils;
22

3+
import java.util.List;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
38
import com.aliyun.odps.Column;
49
import com.aliyun.odps.OdpsType;
5-
import com.aliyun.odps.local.common.ColumnOrConstant;
610
import com.aliyun.odps.local.common.AnyTypeInfo;
11+
import com.aliyun.odps.local.common.ColumnOrConstant;
712
import com.aliyun.odps.type.ArrayTypeInfo;
813
import com.aliyun.odps.type.CharTypeInfo;
914
import com.aliyun.odps.type.MapTypeInfo;
1015
import com.aliyun.odps.type.StructTypeInfo;
1116
import com.aliyun.odps.type.TypeInfo;
1217
import com.aliyun.odps.type.TypeInfoFactory;
1318
import com.aliyun.odps.type.TypeInfoParser;
14-
import java.util.List;
15-
import org.junit.Assert;
16-
import org.junit.Test;
1719

1820
public class SchemaUtilsTest {
1921

@@ -35,7 +37,11 @@ public void fromToString() {
3537
Assert.assertTrue(typeInfos.get(0) instanceof CharTypeInfo);
3638

3739
String str = SchemaUtils.toString(cols);
38-
Assert.assertEquals(columnStr, str);
40+
String
41+
expect =
42+
"c1:MAP<BIGINT,STRING>,c2:ARRAY<INT>,c3:STRUCT<`name`:CHAR(10),`age`:INT,"
43+
+ "`parents`:MAP<VARCHAR(20),SMALLINT>,`hehe`:DECIMAL(20,10),`salary`:FLOAT,`hobbies`:ARRAY<VARCHAR(100)>>";
44+
Assert.assertEquals(expect, str);
3945
}
4046

4147
@Test

odps-sdk/odps-sdk-commons/src/main/java/com/aliyun/odps/type/SimpleStructTypeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public String getTypeName() {
5353
if (i > 0) {
5454
stringBuilder.append(",");
5555
}
56-
stringBuilder.append(fieldNames.get(i));
56+
stringBuilder.append("`").append(fieldNames.get(i)).append("`");
5757
stringBuilder.append(":");
5858
stringBuilder.append(fieldTypeInfos.get(i).getTypeName());
5959
}

odps-sdk/odps-sdk-commons/src/test/java/com/aliyun/odps/type/StructTypeInfoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public void testName() {
2020

2121
Assert.assertArrayEquals(names, structTypeInfo.getFieldNames().toArray());
2222

23-
Assert.assertEquals("STRUCT<name:STRING,age:BIGINT,parents:ARRAY<CHAR(20)>>", structTypeInfo.getTypeName());
23+
Assert.assertEquals("STRUCT<`name`:STRING,`age`:BIGINT,`parents`:ARRAY<CHAR(20)>>", structTypeInfo.getTypeName());
2424
}
2525
}

odps-sdk/odps-sdk-commons/src/test/java/com/aliyun/odps/type/TypeInfoParserTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void testParseMap() {
245245

246246
Assert.assertTrue(typeInfo instanceof MapTypeInfo);
247247
Assert.assertEquals(typeInfo.getOdpsType(), OdpsType.MAP);
248-
Assert.assertEquals(typeInfo.getTypeName(), "MAP<DECIMAL,STRUCT<name:STRING,age:INT>>");
248+
Assert.assertEquals(typeInfo.getTypeName(), "MAP<DECIMAL,STRUCT<`name`:STRING,`age`:INT>>");
249249
Assert.assertEquals(((MapTypeInfo) typeInfo).getKeyTypeInfo(), TypeInfoFactory.DECIMAL);
250250
Assert.assertEquals(((MapTypeInfo) typeInfo).getValueTypeInfo().getOdpsType(), OdpsType.STRUCT);
251251
Assert.assertTrue(((MapTypeInfo) typeInfo).getValueTypeInfo() instanceof StructTypeInfo);
@@ -343,12 +343,12 @@ public void testSpace() {
343343
name = "struct< a : int , un : char(10)>";
344344
typeInfo = TypeInfoParser.getTypeInfoFromTypeString(name);
345345
Assert.assertTrue(typeInfo instanceof StructTypeInfo);
346-
Assert.assertEquals(typeInfo.getTypeName(), "STRUCT<a:INT,un:CHAR(10)>");
346+
Assert.assertEquals(typeInfo.getTypeName(), "STRUCT<`a`:INT,`un`:CHAR(10)>");
347347

348348
name = "struct< a b : int, un xs: char(20)>";
349349
typeInfo = TypeInfoParser.getTypeInfoFromTypeString(name);
350350
Assert.assertTrue(typeInfo instanceof StructTypeInfo);
351-
Assert.assertEquals(typeInfo.getTypeName(), "STRUCT<a b:INT,un xs:CHAR(20)>");
351+
Assert.assertEquals(typeInfo.getTypeName(), "STRUCT<`a b`:INT,`un xs`:CHAR(20)>");
352352
Assert.assertEquals(((StructTypeInfo)typeInfo).getFieldNames().get(0), "a b");
353353
}
354354
}

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Tables.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.aliyun.odps.task.SQLTask;
4141
import com.aliyun.odps.utils.ExceptionUtils;
4242
import com.aliyun.odps.utils.NameSpaceSchemaUtils;
43+
import com.aliyun.odps.utils.OdpsCommonUtils;
4344
import com.aliyun.odps.utils.StringUtils;
4445

4546
/**
@@ -1213,7 +1214,7 @@ private String generateCreateExternalTableSql() {
12131214
}
12141215
sb.append(")");
12151216
}
1216-
sb.append(" LOCATION '").append(location).append("'");
1217+
sb.append(" LOCATION ").append(OdpsCommonUtils.quoteStr(location));
12171218
if (jars != null && !jars.isEmpty()) {
12181219
sb.append(" USING '");
12191220
int index = 0;
@@ -1300,7 +1301,7 @@ private String generateCreateTableSql() {
13001301

13011302
for (int i = 0; i < partitionColumns.size(); i++) {
13021303
Column column = partitionColumns.get(i);
1303-
sql.append(column.getName()).append(" ").append(column.getTypeInfo().getTypeName());
1304+
sql.append("`").append(column.getName()).append("` ").append(column.getTypeInfo().getTypeName());
13041305

13051306
if (column.getComment() != null) {
13061307
sql.append(" COMMENT '").append(column.getComment()).append("'");

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/table/StreamIdentifier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Objects;
2424

2525
import com.aliyun.odps.table.utils.Preconditions;
26+
import com.aliyun.odps.utils.OdpsCommonUtils;
2627

2728
/**
2829
* Identifier of stream object.
@@ -54,7 +55,7 @@ public String getStreamName() {
5455

5556
@Override
5657
public String toString() {
57-
return String.join(".", project, streamName);
58+
return OdpsCommonUtils.quoteRef(project) + "." + OdpsCommonUtils.quoteRef(streamName);
5859
}
5960

6061
@Override

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/table/TableIdentifier.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Objects;
2424

2525
import com.aliyun.odps.table.utils.Preconditions;
26-
import com.aliyun.odps.utils.StringUtils;
26+
import com.aliyun.odps.utils.NameSpaceSchemaUtils;
2727

2828
/**
2929
* Identifier of table.
@@ -68,11 +68,7 @@ public String getTable() {
6868

6969
@Override
7070
public String toString() {
71-
if (StringUtils.isNullOrEmpty(schema)) {
72-
return String.join(".", project, table);
73-
} else {
74-
return String.join(".", project, schema, table);
75-
}
71+
return NameSpaceSchemaUtils.getFullName(project, schema, table);
7672
}
7773

7874
@Override

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/utils/NameSpaceSchemaUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class NameSpaceSchemaUtils {
99

1010
public static String getFullName(String project, String schema, String objectName) {
1111
if (isSchemaEnabled(schema)) {
12-
return project + "." + schema + "." + objectName;
12+
return OdpsCommonUtils.quoteRef(project) + "." + OdpsCommonUtils.quoteRef(schema) + "."
13+
+ OdpsCommonUtils.quoteRef(objectName);
1314
} else {
1415
// two-tier => a.b
1516
// three-tier not reload => a.null.b
1617
// three-tier reloaded => a.default.b
17-
return project + "." + objectName;
18+
return OdpsCommonUtils.quoteRef(project) + "." + OdpsCommonUtils.quoteRef(objectName);
1819
}
1920
}
2021

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/utils/OdpsCommonUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
*/
1818
public class OdpsCommonUtils {
1919

20+
public static String quoteRef(String str) {
21+
return "`" + str + "`";
22+
}
23+
24+
public static String quoteStr(String str) {
25+
str = str.replace("'", "\\'");
26+
return "'" + str + "'";
27+
}
28+
2029
/**
2130
* Infer the Odps type of object based on its Java Type
2231
* <p>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aliyun.odps;
2+
3+
import org.junit.Test;
4+
5+
import com.aliyun.odps.commons.transport.OdpsTestUtils;
6+
import com.aliyun.odps.type.TypeInfoFactory;
7+
import com.google.common.collect.ImmutableList;
8+
9+
/**
10+
* @author dingxin ([email protected])
11+
*/
12+
public class TableCreatorTest {
13+
14+
@Test
15+
public void testCreateStructTable() throws OdpsException {
16+
TableSchema
17+
schema =
18+
TableSchema.builder().withColumn(Column.newBuilder("c1", TypeInfoFactory.getStructTypeInfo(
19+
ImmutableList.of("end"), ImmutableList.of(TypeInfoFactory.STRING))).build()).build();
20+
21+
Odps odps = OdpsTestUtils.newDefaultOdps();
22+
odps.tables().newTableCreator("testCreateStructTable", schema).debug().ifNotExists().create();
23+
}
24+
}

odps-sdk/odps-sdk-core/src/test/java/com/aliyun/odps/test/LogViewTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ public void testLogViewNotNull() {
5959

6060
@Test
6161
public void testLogViewHost() throws OdpsException {
62-
LogView log = odps.logview();
63-
log.setLogViewHost("http://test.a.b.c");
64-
Iterator<Instance> instIter = odps.instances().iterator();
65-
instIter.hasNext();
66-
Instance i = instIter.next();
67-
assertTrue(
68-
log.generateLogView(i, 7 * 24).startsWith("http://test.a.b.c/logview"));
62+
try {
63+
LogView log = odps.logview();
64+
log.setLogViewHost("http://test.a.b.c");
65+
Iterator<Instance> instIter = odps.instances().iterator();
66+
instIter.hasNext();
67+
Instance i = instIter.next();
68+
assertTrue(
69+
log.generateLogView(i, 7 * 24).startsWith("http://test.a.b.c/logview"));
70+
} catch (OdpsException e) {
71+
assertTrue(e.getMessage().contains("Request timeout"));
72+
}
6973
}
7074
}

odps-sdk/odps-sdk-core/src/test/java/com/aliyun/odps/utils/NameSpaceSchemaUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class NameSpaceSchemaUtilsTest extends TestCase {
1111

1212
@Test
1313
public void testGetFullName() {
14-
Assert.assertEquals("a.b.c", NameSpaceSchemaUtils.getFullName("a", "b", "c"));
15-
Assert.assertEquals("a.c", NameSpaceSchemaUtils.getFullName("a", null, "c"));
16-
Assert.assertEquals("a.c", NameSpaceSchemaUtils.getFullName("a", " ", "c"));
14+
Assert.assertEquals("`a`.`b`.`c`", NameSpaceSchemaUtils.getFullName("a", "b", "c"));
15+
Assert.assertEquals("`a`.`c`", NameSpaceSchemaUtils.getFullName("a", null, "c"));
16+
Assert.assertEquals("`a`.`c`", NameSpaceSchemaUtils.getFullName("a", " ", "c"));
1717
}
1818

1919
@Test

0 commit comments

Comments
 (0)