Skip to content

Commit d64c291

Browse files
authored
Forbid some interface and add new interface in Record (#16892)
1 parent 0d64d51 commit d64c291

File tree

10 files changed

+197
-43
lines changed

10 files changed

+197
-43
lines changed

integration-test/src/test/java/org/apache/iotdb/relational/it/query/object/IoTDBObjectQueryIT.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@
3333
import org.apache.tsfile.enums.TSDataType;
3434
import org.apache.tsfile.read.common.Field;
3535
import org.apache.tsfile.read.common.RowRecord;
36-
import org.apache.tsfile.utils.Binary;
3736
import org.junit.AfterClass;
3837
import org.junit.BeforeClass;
3938
import org.junit.Test;
4039
import org.junit.experimental.categories.Category;
4140
import org.junit.runner.RunWith;
4241

43-
import java.sql.Blob;
4442
import java.sql.Connection;
4543
import java.sql.ResultSet;
4644
import java.sql.SQLException;
4745
import java.sql.Statement;
4846

4947
import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
50-
import static org.junit.Assert.assertArrayEquals;
48+
import static org.apache.iotdb.jdbc.IoTDBJDBCResultSet.OBJECT_ERR_MSG;
5149
import static org.junit.Assert.assertEquals;
5250
import static org.junit.Assert.assertTrue;
5351
import static org.junit.Assert.fail;
@@ -104,10 +102,20 @@ public void jdbcTest() {
104102
int cnt = 0;
105103
while (resultSet.next()) {
106104
cnt++;
107-
Blob blob = resultSet.getBlob(3);
108-
byte[] bytes = resultSet.getBytes("o1");
109-
assertArrayEquals(blob.getBytes(1, (int) blob.length()), bytes);
110-
assertTrue(new String(bytes).endsWith(String.format("%d.bin", cnt)));
105+
try {
106+
resultSet.getBlob(3);
107+
fail();
108+
} catch (SQLException e) {
109+
assertEquals(OBJECT_ERR_MSG, e.getMessage());
110+
}
111+
112+
try {
113+
resultSet.getBytes("o1");
114+
fail();
115+
} catch (SQLException e) {
116+
assertEquals(OBJECT_ERR_MSG, e.getMessage());
117+
}
118+
111119
String s = resultSet.getString(3);
112120
assertEquals("(Object) 5 B", s);
113121
}
@@ -150,13 +158,15 @@ public void sessionTest() {
150158
String s = field.getStringValue();
151159
assertEquals("(Object) 5 B", s);
152160
Object blob = field.getObjectValue(TSDataType.OBJECT);
153-
assertTrue(blob instanceof Binary);
154-
assertTrue(
155-
new String(((Binary) blob).getValues()).endsWith(String.format("%d.bin", cnt)));
156-
157-
Binary binary = field.getBinaryV();
158-
assertArrayEquals(binary.getValues(), ((Binary) blob).getValues());
159-
assertTrue(new String(binary.getValues()).endsWith(String.format("%d.bin", cnt)));
161+
assertTrue(blob instanceof String);
162+
assertEquals("(Object) 5 B", blob);
163+
164+
try {
165+
field.getBinaryV();
166+
fail();
167+
} catch (UnsupportedOperationException e) {
168+
assertEquals("OBJECT Type only support getStringValue", e.getMessage());
169+
}
160170
}
161171
assertEquals(4, cnt);
162172
}
@@ -174,8 +184,12 @@ public void sessionTest() {
174184
assertEquals("(Object) 5 B", o);
175185
String s = iterator.getString("o1");
176186
assertEquals("(Object) 5 B", s);
177-
Binary blob = iterator.getBlob(3);
178-
assertTrue(new String(blob.getValues()).endsWith(String.format("%d.bin", cnt)));
187+
try {
188+
iterator.getBlob(3);
189+
fail();
190+
} catch (StatementExecutionException e) {
191+
assertEquals("OBJECT Type only support getString", e.getMessage());
192+
}
179193
}
180194
assertEquals(4, cnt);
181195
}

iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/access/Record.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import org.apache.tsfile.utils.Binary;
2525

26+
import java.io.File;
2627
import java.time.LocalDate;
28+
import java.util.Optional;
2729

2830
public interface Record {
2931
/**
@@ -83,7 +85,7 @@ public interface Record {
8385
* Returns the Binary value at the specified column in this row.
8486
*
8587
* <p>Users need to ensure that the data type of the specified column is {@code TSDataType.TEXT},
86-
* {@code TSDataType.STRING} or {@code TSDataType.BLOB} or {@code TSDataType.OBJECT}.
88+
* {@code TSDataType.STRING} or {@code TSDataType.BLOB}.
8789
*
8890
* @param columnIndex index of the specified column
8991
* @return the Binary value at the specified column in this row
@@ -113,6 +115,15 @@ public interface Record {
113115

114116
Object getObject(int columnIndex);
115117

118+
/**
119+
* Returns the OBJECT value's real file path in current node at the specified column in this row.
120+
*
121+
* @param columnIndex index of the specified column
122+
* @return Optional.empty() if current node doesn't have the real file storing the object content,
123+
* otherwise the File referring to the OBJECT value's real file path
124+
*/
125+
Optional<File> getObjectFile(int columnIndex);
126+
116127
/**
117128
* Returns the Binary representation of an object stored at the specified column in this row.
118129
*

iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionDataSet.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,26 @@ public LocalDate getDate(String columnName) throws StatementExecutionException {
327327
}
328328

329329
public Binary getBlob(int columnIndex) throws StatementExecutionException {
330+
final TSDataType dataType = ioTDBRpcDataSet.getDataType(columnIndex);
331+
if (dataType == null) {
332+
return null;
333+
}
334+
335+
if (dataType.equals(TSDataType.OBJECT)) {
336+
throw new StatementExecutionException("OBJECT Type only support getString");
337+
}
330338
return ioTDBRpcDataSet.getBinary(columnIndex);
331339
}
332340

333341
public Binary getBlob(String columnName) throws StatementExecutionException {
342+
final TSDataType dataType = ioTDBRpcDataSet.getDataType(columnName);
343+
if (dataType == null) {
344+
return null;
345+
}
346+
347+
if (dataType.equals(TSDataType.OBJECT)) {
348+
throw new StatementExecutionException("OBJECT Type only support getString");
349+
}
334350
return ioTDBRpcDataSet.getBinary(columnName);
335351
}
336352

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070

7171
public class IoTDBJDBCResultSet implements ResultSet {
7272

73+
public static final String OBJECT_ERR_MSG = "OBJECT Type only support getString";
74+
7375
private static final Logger LOGGER = LoggerFactory.getLogger(IoTDBJDBCResultSet.class);
7476

7577
protected IoTDBStatement statement;
@@ -301,6 +303,15 @@ public InputStream getBinaryStream(String arg0) throws SQLException {
301303
@Override
302304
public Blob getBlob(int arg0) throws SQLException {
303305
try {
306+
final TSDataType dataType = ioTDBRpcDataSet.getDataType(arg0);
307+
if (dataType == null) {
308+
return null;
309+
}
310+
311+
if (dataType.equals(TSDataType.OBJECT)) {
312+
throw new SQLException(OBJECT_ERR_MSG);
313+
}
314+
304315
Binary binary = ioTDBRpcDataSet.getBinary(arg0);
305316
if (ObjectUtils.isNotEmpty(binary)) {
306317
return new SerialBlob(binary.getValues());
@@ -314,6 +325,15 @@ public Blob getBlob(int arg0) throws SQLException {
314325
@Override
315326
public Blob getBlob(String arg0) throws SQLException {
316327
try {
328+
final TSDataType dataType = ioTDBRpcDataSet.getDataType(arg0);
329+
if (dataType == null) {
330+
return null;
331+
}
332+
333+
if (dataType.equals(TSDataType.OBJECT)) {
334+
throw new SQLException(OBJECT_ERR_MSG);
335+
}
336+
317337
Binary binary = ioTDBRpcDataSet.getBinary(arg0);
318338
if (ObjectUtils.isNotEmpty(binary)) {
319339
return new SerialBlob(binary.getValues());
@@ -360,7 +380,9 @@ public byte[] getBytes(int columnIndex) throws SQLException {
360380
return null;
361381
}
362382

363-
if (dataType.equals(TSDataType.BLOB) || dataType.equals(TSDataType.OBJECT)) {
383+
if (dataType.equals(TSDataType.OBJECT)) {
384+
throw new SQLException(OBJECT_ERR_MSG);
385+
} else if (dataType.equals(TSDataType.BLOB)) {
364386
Binary binary = ioTDBRpcDataSet.getBinary(columnIndex);
365387
return binary == null ? null : binary.getValues();
366388
} else {
@@ -379,8 +401,9 @@ public byte[] getBytes(String columnName) throws SQLException {
379401
if (dataType == null) {
380402
return null;
381403
}
382-
383-
if (dataType.equals(TSDataType.BLOB) || dataType.equals(TSDataType.OBJECT)) {
404+
if (dataType.equals(TSDataType.OBJECT)) {
405+
throw new SQLException(OBJECT_ERR_MSG);
406+
} else if (dataType.equals(TSDataType.BLOB)) {
384407
Binary binary = ioTDBRpcDataSet.getBinary(columnName);
385408
return binary == null ? null : binary.getValues();
386409
} else {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/partition/Slice.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
import org.apache.tsfile.utils.BytesUtils;
3030
import org.apache.tsfile.utils.DateUtils;
3131

32+
import java.io.File;
3233
import java.time.LocalDate;
3334
import java.util.Arrays;
3435
import java.util.HashSet;
3536
import java.util.Iterator;
3637
import java.util.List;
38+
import java.util.Optional;
3739
import java.util.Set;
3840
import java.util.stream.Collectors;
3941

42+
import static org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.RecordIterator.OBJECT_ERR_MSG;
4043
import static org.apache.iotdb.udf.api.type.Type.OBJECT;
4144

4245
/** Parts of partition. */
@@ -170,6 +173,14 @@ public boolean getBoolean(int columnIndex) {
170173

171174
@Override
172175
public Binary getBinary(int columnIndex) {
176+
Type type = dataTypes.get(columnIndex);
177+
if (type == OBJECT) {
178+
throw new UnsupportedOperationException(OBJECT_ERR_MSG);
179+
}
180+
return getBinarySafely(columnIndex);
181+
}
182+
183+
public Binary getBinarySafely(int columnIndex) {
173184
return originalColumns[columnIndex].getBinary(offset);
174185
}
175186

@@ -193,15 +204,24 @@ public LocalDate getLocalDate(int columnIndex) {
193204

194205
@Override
195206
public Object getObject(int columnIndex) {
207+
Type type = dataTypes.get(columnIndex);
208+
if (type == OBJECT) {
209+
throw new UnsupportedOperationException(OBJECT_ERR_MSG);
210+
}
196211
return originalColumns[columnIndex].getObject(offset);
197212
}
198213

199214
@Override
200-
public Binary readObject(int columnIndex, long offset, int length) {
215+
public Optional<File> getObjectFile(int columnIndex) {
201216
if (getDataType(columnIndex) != Type.OBJECT) {
202217
throw new UnsupportedOperationException("current column is not object column");
203218
}
204-
Binary binary = getBinary(columnIndex);
219+
return ObjectTypeUtils.getObjectPathFromBinary(getBinarySafely(columnIndex));
220+
}
221+
222+
@Override
223+
public Binary readObject(int columnIndex, long offset, int length) {
224+
Binary binary = getBinarySafely(columnIndex);
205225
return new Binary(ObjectTypeUtils.readObjectContent(binary, offset, length, true).array());
206226
}
207227

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/RecordIterator.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@
3131
import org.apache.tsfile.utils.BytesUtils;
3232
import org.apache.tsfile.utils.DateUtils;
3333

34+
import java.io.File;
3435
import java.time.LocalDate;
3536
import java.util.Iterator;
3637
import java.util.List;
38+
import java.util.Optional;
3739

3840
import static org.apache.tsfile.read.common.type.BlobType.BLOB;
3941

4042
public class RecordIterator implements Iterator<Record> {
4143

44+
public static final String OBJECT_ERR_MSG =
45+
"OBJECT Type only support getString, getObjectFile, objectLength and readObject";
46+
4247
private final List<Column> childrenColumns;
4348
private final List<org.apache.tsfile.read.common.type.Type> dataTypes;
4449
private final int positionCount;
@@ -113,6 +118,14 @@ public boolean getBoolean(int columnIndex) {
113118

114119
@Override
115120
public Binary getBinary(int columnIndex) {
121+
org.apache.tsfile.read.common.type.Type type = dataTypes.get(columnIndex);
122+
if (type == ObjectType.OBJECT) {
123+
throw new UnsupportedOperationException(OBJECT_ERR_MSG);
124+
}
125+
return getBinarySafely(columnIndex);
126+
}
127+
128+
private Binary getBinarySafely(int columnIndex) {
116129
return childrenColumns.get(columnIndex).getBinary(index);
117130
}
118131

@@ -136,15 +149,24 @@ public LocalDate getLocalDate(int columnIndex) {
136149

137150
@Override
138151
public Object getObject(int columnIndex) {
152+
org.apache.tsfile.read.common.type.Type type = dataTypes.get(columnIndex);
153+
if (type == ObjectType.OBJECT) {
154+
throw new UnsupportedOperationException(OBJECT_ERR_MSG);
155+
}
139156
return childrenColumns.get(columnIndex).getObject(index);
140157
}
141158

142159
@Override
143-
public Binary readObject(int columnIndex, long offset, int length) {
160+
public Optional<File> getObjectFile(int columnIndex) {
144161
if (getDataType(columnIndex) != Type.OBJECT) {
145162
throw new UnsupportedOperationException("current column is not object column");
146163
}
147-
Binary binary = getBinary(columnIndex);
164+
return ObjectTypeUtils.getObjectPathFromBinary(getBinarySafely(columnIndex));
165+
}
166+
167+
@Override
168+
public Binary readObject(int columnIndex, long offset, int length) {
169+
Binary binary = getBinarySafely(columnIndex);
148170
return new Binary(ObjectTypeUtils.readObjectContent(binary, offset, length, true).array());
149171
}
150172

0 commit comments

Comments
 (0)