Skip to content

Commit 9d3414e

Browse files
committed
Add IT for session and jdbc query
1 parent 1b2d3ff commit 9d3414e

File tree

4 files changed

+202
-9
lines changed

4 files changed

+202
-9
lines changed

integration-test/src/main/java/org/apache/iotdb/itbase/runtime/ClusterTestResultSet.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,12 @@ public Ref getRef(int columnIndex) {
781781
}
782782

783783
@Override
784-
public Blob getBlob(int columnIndex) {
785-
throw new UnsupportedOperationException();
784+
public Blob getBlob(int columnIndex) throws SQLException {
785+
RequestDelegate<Blob> delegate = createLocalRequestDelegate();
786+
for (ResultSet rs : resultSets) {
787+
delegate.addRequest(() -> rs.getBlob(columnIndex));
788+
}
789+
return delegate.requestAllAndCompare();
786790
}
787791

788792
@Override
@@ -806,8 +810,12 @@ public Ref getRef(String columnLabel) {
806810
}
807811

808812
@Override
809-
public Blob getBlob(String columnLabel) {
810-
throw new UnsupportedOperationException();
813+
public Blob getBlob(String columnLabel) throws SQLException {
814+
RequestDelegate<Blob> delegate = createLocalRequestDelegate();
815+
for (ResultSet rs : resultSets) {
816+
delegate.addRequest(() -> rs.getBlob(columnLabel));
817+
}
818+
return delegate.requestAllAndCompare();
811819
}
812820

813821
@Override
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.relational.it.query.object;
21+
22+
import org.apache.iotdb.isession.ITableSession;
23+
import org.apache.iotdb.isession.SessionConfig;
24+
import org.apache.iotdb.isession.SessionDataSet;
25+
import org.apache.iotdb.it.env.EnvFactory;
26+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
27+
import org.apache.iotdb.itbase.category.TableClusterIT;
28+
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
29+
import org.apache.iotdb.itbase.env.BaseEnv;
30+
import org.apache.iotdb.rpc.IoTDBConnectionException;
31+
import org.apache.iotdb.rpc.StatementExecutionException;
32+
33+
import org.apache.tsfile.enums.TSDataType;
34+
import org.apache.tsfile.read.common.Field;
35+
import org.apache.tsfile.read.common.RowRecord;
36+
import org.apache.tsfile.utils.Binary;
37+
import org.junit.AfterClass;
38+
import org.junit.BeforeClass;
39+
import org.junit.Test;
40+
import org.junit.experimental.categories.Category;
41+
import org.junit.runner.RunWith;
42+
43+
import java.sql.Blob;
44+
import java.sql.Connection;
45+
import java.sql.ResultSet;
46+
import java.sql.SQLException;
47+
import java.sql.Statement;
48+
49+
import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
50+
import static org.junit.Assert.assertArrayEquals;
51+
import static org.junit.Assert.assertEquals;
52+
import static org.junit.Assert.assertTrue;
53+
import static org.junit.Assert.fail;
54+
55+
@RunWith(IoTDBTestRunner.class)
56+
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
57+
public class IoTDBObjectQueryIT {
58+
59+
private static final String DATABASE_NAME = "test_db";
60+
61+
private static final String TIME_ZONE = "+00:00";
62+
63+
private static final String[] createSqls =
64+
new String[] {
65+
"CREATE DATABASE " + DATABASE_NAME,
66+
"USE " + DATABASE_NAME,
67+
"CREATE TABLE t1(device_id STRING TAG, o1 OBJECT, b1 BLOB, s1 STRING)",
68+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(1, 'd1', X'cafebabe01', to_object(true, 0, X'cafebabe01'), 'cafebabe01')",
69+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(2, 'd1', X'cafebabe0202', to_object(true, 0, X'cafebabe02'), 'cafebabe02')",
70+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(3, 'd1', X'cafebabe0303', to_object(true, 0, X'cafebabe03'), 'cafebabe03')",
71+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(4, 'd1', X'cafebabe04', to_object(true, 0, X'cafebabe04'), 'cafebabe04')",
72+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(1, 'd2', X'cafebade01', to_object(true, 0, X'cafebade01'), 'cafebade01')",
73+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(2, 'd2', X'cafebade0202', to_object(true, 0, X'cafebade02'), 'cafebade02')",
74+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(3, 'd2', X'cafebade0302', to_object(true, 0, X'cafebade03'), 'cafebade03')",
75+
"INSERT INTO t1(time, device_id, b1, o1, s1) VALUES(4, 'd2', X'cafebade04', to_object(true, 0, X'cafebade04'), 'cafebade04')",
76+
"FLUSH",
77+
};
78+
79+
@BeforeClass
80+
public static void classSetUp() {
81+
EnvFactory.getEnv().initClusterEnvironment();
82+
prepareTableData(createSqls);
83+
}
84+
85+
@AfterClass
86+
public static void classTearDown() {
87+
EnvFactory.getEnv().cleanClusterEnvironment();
88+
}
89+
90+
@Test
91+
public void jdbcTest() {
92+
try (Connection connection =
93+
EnvFactory.getEnv()
94+
.getConnection(
95+
SessionConfig.DEFAULT_USER,
96+
SessionConfig.DEFAULT_PASSWORD,
97+
BaseEnv.TABLE_SQL_DIALECT)) {
98+
connection.setClientInfo("time_zone", TIME_ZONE);
99+
try (Statement statement = connection.createStatement()) {
100+
statement.execute("use " + DATABASE_NAME);
101+
try (ResultSet resultSet =
102+
statement.executeQuery(
103+
"SELECT time, b1, o1, s1 FROM t1 WHERE device_id = 'd1' ORDER BY time")) {
104+
int cnt = 0;
105+
while (resultSet.next()) {
106+
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)));
111+
String s = resultSet.getString(3);
112+
assertEquals("(Object) 5 B", s);
113+
}
114+
assertEquals(4, cnt);
115+
}
116+
117+
try (ResultSet resultSet =
118+
statement.executeQuery(
119+
"SELECT time, b1, READ_OBJECT(o1), s1 FROM t1 WHERE device_id = 'd2' AND READ_OBJECT(o1)=b1 ORDER BY time")) {
120+
int cnt = 0;
121+
String[] ans = {"0xcafebade01", "0xcafebade04"};
122+
while (resultSet.next()) {
123+
String s = resultSet.getString(3);
124+
assertEquals(ans[cnt], s);
125+
cnt++;
126+
}
127+
assertEquals(2, cnt);
128+
}
129+
}
130+
} catch (SQLException e) {
131+
e.printStackTrace();
132+
fail(e.getMessage());
133+
}
134+
}
135+
136+
@Test
137+
public void sessionTest() {
138+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
139+
session.executeNonQueryStatement("USE " + DATABASE_NAME);
140+
141+
// SessionDataSet
142+
try (SessionDataSet dataSet =
143+
session.executeQueryStatement(
144+
"SELECT time, b1, o1, s1 FROM t1 WHERE device_id = 'd1' ORDER BY time")) {
145+
int cnt = 0;
146+
while (dataSet.hasNext()) {
147+
cnt++;
148+
RowRecord rowRecord = dataSet.next();
149+
Field field = rowRecord.getField(2);
150+
String s = field.getStringValue();
151+
assertEquals("(Object) 5 B", s);
152+
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)));
160+
}
161+
assertEquals(4, cnt);
162+
}
163+
164+
// SessionDataSet.DataIterator
165+
try (SessionDataSet dataSet =
166+
session.executeQueryStatement(
167+
"SELECT time, b1, o1, s1 FROM t1 WHERE device_id = 'd2' ORDER BY time")) {
168+
SessionDataSet.DataIterator iterator = dataSet.iterator();
169+
int cnt = 0;
170+
while (iterator.next()) {
171+
cnt++;
172+
Object o = iterator.getObject(3);
173+
assertTrue(o instanceof String);
174+
assertEquals("(Object) 5 B", o);
175+
String s = iterator.getString("o1");
176+
assertEquals("(Object) 5 B", s);
177+
Binary blob = iterator.getBlob(3);
178+
assertTrue(new String(blob.getValues()).endsWith(String.format("%d.bin", cnt)));
179+
}
180+
assertEquals(4, cnt);
181+
}
182+
} catch (IoTDBConnectionException | StatementExecutionException e) {
183+
fail(e.getMessage());
184+
}
185+
}
186+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public byte[] getBytes(String columnName) throws SQLException {
380380
return null;
381381
}
382382

383-
if (dataType.equals(TSDataType.BLOB)) {
383+
if (dataType.equals(TSDataType.BLOB) || dataType.equals(TSDataType.OBJECT)) {
384384
Binary binary = ioTDBRpcDataSet.getBinary(columnName);
385385
return binary == null ? null : binary.getValues();
386386
} else {

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/relational/ColumnTransformerBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@
243243
import static org.apache.tsfile.read.common.type.FloatType.FLOAT;
244244
import static org.apache.tsfile.read.common.type.IntType.INT32;
245245
import static org.apache.tsfile.read.common.type.LongType.INT64;
246-
import static org.apache.tsfile.read.common.type.ObjectType.OBJECT;
247246
import static org.apache.tsfile.read.common.type.StringType.STRING;
248247

249248
public class ColumnTransformerBuilder
@@ -1456,10 +1455,10 @@ private ColumnTransformer getFunctionColumnTransformer(
14561455
.equalsIgnoreCase(functionName)) {
14571456
ColumnTransformer first = this.process(children.get(0), context);
14581457
if (children.size() == 1) {
1459-
return new ReadObjectColumnTransformer(OBJECT, first, context.fragmentInstanceContext);
1458+
return new ReadObjectColumnTransformer(BLOB, first, context.fragmentInstanceContext);
14601459
} else if (children.size() == 2) {
14611460
return new ReadObjectColumnTransformer(
1462-
OBJECT,
1461+
BLOB,
14631462
((LongLiteral) children.get(1)).getParsedValue(),
14641463
first,
14651464
context.fragmentInstanceContext);
@@ -1468,7 +1467,7 @@ private ColumnTransformer getFunctionColumnTransformer(
14681467
long length = ((LongLiteral) children.get(2)).getParsedValue();
14691468
checkArgument(offset >= 0 && length >= 0);
14701469
return new ReadObjectColumnTransformer(
1471-
OBJECT,
1470+
BLOB,
14721471
((LongLiteral) children.get(1)).getParsedValue(),
14731472
((LongLiteral) children.get(2)).getParsedValue(),
14741473
first,

0 commit comments

Comments
 (0)