diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBAlignByDeviceTableViewIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBAlignByDeviceTableViewIT.java index 4f33aa49a0d2d..ca92a1e1de6b6 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBAlignByDeviceTableViewIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBAlignByDeviceTableViewIT.java @@ -47,7 +47,7 @@ public class IoTDBAlignByDeviceTableViewIT { new String[] { "CREATE DATABASE " + DATABASE_NAME, "USE " + DATABASE_NAME, - "create view vehicle(device_id STRING TAG, s0 INT32 FIELD, s1 INT64 FIELD, s2 FLOAT FIELD, s3 STRING FIELD, s4 BOOLEAN FIELD) as root.vehicle.**" + "create view vehicle(device_id STRING TAG, s0 INT32 FIELD, s1 INT64 FIELD, s2 FLOAT FIELD, s3 TEXT FIELD, s4 BOOLEAN FIELD) as root.vehicle.**" }; private static final String[] sqls = diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBOrderByWithAlignByDeviceTableViewIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBOrderByWithAlignByDeviceTableViewIT.java index d8bca2b4522d8..8dbf6b892f01e 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBOrderByWithAlignByDeviceTableViewIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/old/alignbydevice/IoTDBOrderByWithAlignByDeviceTableViewIT.java @@ -190,7 +190,7 @@ protected static void insertData() { "create view weather(city STRING TAG, precipitation INT64 FIELD, temperature DOUBLE FIELD) as root.weather.**"); statement.execute( - "create view optimize(plant_id STRING TAG, device_id STRING TAG, temperature DOUBLE FIELD, status BOOLEAN FIELD, hardware STRING FIELD) as root.ln.**"); + "create view optimize(plant_id STRING TAG, device_id STRING TAG, temperature DOUBLE FIELD, status BOOLEAN FIELD, hardware TEXT FIELD) as root.ln.**"); } catch (Exception e) { e.printStackTrace(); diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBTableViewQueryWithNotMatchedDataTypeIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBTableViewQueryWithNotMatchedDataTypeIT.java new file mode 100644 index 0000000000000..1e342a13a4a45 --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/view/recent/IoTDBTableViewQueryWithNotMatchedDataTypeIT.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.relational.it.query.view.recent; + +import org.apache.iotdb.isession.ITableSession; +import org.apache.iotdb.isession.SessionDataSet; +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.itbase.category.TableClusterIT; +import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; +import org.apache.iotdb.rpc.IoTDBConnectionException; +import org.apache.iotdb.rpc.StatementExecutionException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +import static org.apache.iotdb.db.it.utils.TestUtils.prepareData; +import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData; + +@RunWith(Parameterized.class) +@Category({TableLocalStandaloneIT.class, TableClusterIT.class}) +public class IoTDBTableViewQueryWithNotMatchedDataTypeIT { + + protected static final String DATABASE_NAME = "test"; + + private final boolean aligned; + + protected static final String[] createTreeAlignedDataSqls = + new String[] { + "CREATE ALIGNED TIMESERIES root.db.battery.b1(current FLOAT)", + "INSERT INTO root.db.battery.b1(time, current) aligned values (1, 1)", + }; + + protected static final String[] createTreeNonAlignedDataSqls = + new String[] { + "CREATE TIMESERIES root.db.battery.b1.current FLOAT", + "INSERT INTO root.db.battery.b1(time, current) values (1, 1)", + }; + + protected static String[] createTableSqls = { + "create database " + DATABASE_NAME, + "use " + DATABASE_NAME, + "CREATE VIEW view1 (battery TAG, current BLOB FIELD) as root.db.battery.**", + }; + + public IoTDBTableViewQueryWithNotMatchedDataTypeIT(boolean aligned) { + this.aligned = aligned; + } + + @Parameterized.Parameters(name = "aligned={0}") + public static Collection data() { + return Arrays.asList(new Object[][] {{true}, {false}}); + } + + @Before + public void setUp() throws Exception { + EnvFactory.getEnv().getConfig().getCommonConfig().setSortBufferSize(128 * 1024); + EnvFactory.getEnv().getConfig().getCommonConfig().setMaxTsBlockSizeInByte(4 * 1024); + EnvFactory.getEnv().initClusterEnvironment(); + prepareData(aligned ? createTreeAlignedDataSqls : createTreeNonAlignedDataSqls); + prepareTableData(createTableSqls); + } + + @After + public void tearDown() throws Exception { + EnvFactory.getEnv().cleanClusterEnvironment(); + } + + @Test + public void test() throws IoTDBConnectionException, StatementExecutionException { + try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) { + session.executeNonQueryStatement("USE " + DATABASE_NAME); + SessionDataSet sessionDataSet = session.executeQueryStatement("select * from view1"); + Assert.assertFalse(sessionDataSet.hasNext()); + sessionDataSet.close(); + } + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/utils/ResourceByPathUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/utils/ResourceByPathUtils.java index b0c4daa85c28e..e5288adc76d28 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/utils/ResourceByPathUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/utils/ResourceByPathUtils.java @@ -66,6 +66,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -317,6 +318,18 @@ public ReadOnlyMemChunk getReadOnlyMemChunkFromMemTable( } AlignedWritableMemChunk alignedMemChunk = ((AlignedWritableMemChunkGroup) memTableMap.get(deviceID)).getAlignedMemChunk(); + + // check If data type matches + Map dataTypeMap = new HashMap<>(alignedMemChunk.getSchemaList().size()); + for (IMeasurementSchema schema : alignedMemChunk.getSchemaList()) { + dataTypeMap.put(schema.getMeasurementName(), schema.getType()); + } + for (IMeasurementSchema schema : alignedFullPath.getSchemaList()) { + TSDataType dataTypeInMemChunk = dataTypeMap.get(schema.getMeasurementName()); + if (dataTypeInMemChunk != null && dataTypeInMemChunk != schema.getType()) { + return null; + } + } // only need to do this check for tree model if (context.isIgnoreAllNullRows()) { boolean containsMeasurement = false; @@ -514,6 +527,10 @@ public ReadOnlyMemChunk getReadOnlyMemChunkFromMemTable( } IWritableMemChunk memChunk = memTableMap.get(deviceID).getMemChunkMap().get(fullPath.getMeasurement()); + // check If data type matches + if (memChunk.getSchema().getType() != fullPath.getMeasurementSchema().getType()) { + return null; + } // prepare TVList for query. It should clone TVList if necessary. Map tvListQueryMap = prepareTvListMapForQuery(context, memChunk, modsToMemtable == null, globalTimeFilter);