|
| 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 | +} |
0 commit comments