Skip to content

Commit 673434b

Browse files
authored
Fix quoted tree path parsing in table views (#18884)
1 parent d77a815 commit 673434b

3 files changed

Lines changed: 84 additions & 8 deletions

File tree

‎integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBTableIT.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,33 @@ public void testConcurrentAutoCreateAndDropColumn() throws Exception {
886886
}
887887
}
888888

889+
@Test
890+
public void testCreateViewFromTreePathWithSpaces() throws Exception {
891+
try (final Connection connection = EnvFactory.getEnv().getConnection();
892+
final Statement statement = connection.createStatement()) {
893+
statement.execute("CREATE DATABASE root.tv143_cmp_3");
894+
statement.execute("CREATE TIMESERIES root.`tv143_cmp_3`.`a b`.`d1`.s1 INT32");
895+
statement.execute("INSERT INTO root.`tv143_cmp_3`.`a b`.`d1`(time,s1) VALUES (1,10)");
896+
}
897+
898+
try (final Connection connection =
899+
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
900+
final Statement statement = connection.createStatement()) {
901+
statement.execute("CREATE DATABASE tv143_cmp_3");
902+
statement.execute("USE tv143_cmp_3");
903+
statement.execute(
904+
"CREATE VIEW v (device STRING TAG, value INT32 FIELD FROM s1) WITH (ttl='INF') "
905+
+ "AS root.\"`tv143_cmp_3`\".\"`a b`\".**");
906+
907+
try (final ResultSet resultSet =
908+
statement.executeQuery("SELECT value FROM v ORDER BY time")) {
909+
assertTrue(resultSet.next());
910+
assertEquals(10, resultSet.getInt("value"));
911+
assertFalse(resultSet.next());
912+
}
913+
}
914+
}
915+
889916
@Test
890917
public void testTreeViewTable() throws Exception {
891918
try (final Connection connection = EnvFactory.getEnv().getConnection();

‎iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,16 @@ private PartialPath parsePrefixPath(final RelationalSqlParser.PrefixPathContext
769769
final StringBuilder builder = new StringBuilder("root.");
770770
for (int i = 0; i < nodeNames.size(); i++) {
771771
if (nodeNames.get(i).nodeNameWithoutWildcard() != null) {
772-
builder
773-
.append("`")
774-
.append(
775-
parseNodeString(
776-
((Identifier) visit(nodeNames.get(i).nodeNameWithoutWildcard().identifier()))
777-
.getValue()))
778-
.append("`")
779-
.append(i == nodeNames.size() - 1 ? "" : ".");
772+
final String nodeName =
773+
parseNodeString(
774+
((Identifier) visit(nodeNames.get(i).nodeNameWithoutWildcard().identifier()))
775+
.getValue());
776+
if (nodeName.startsWith("`") && nodeName.endsWith("`")) {
777+
builder.append(nodeName);
778+
} else {
779+
builder.append("`").append(nodeName).append("`");
780+
}
781+
builder.append(i == nodeNames.size() - 1 ? "" : ".");
780782
} else {
781783
builder.append(nodeNames.get(i).getText());
782784
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.db.queryengine.plan.relational.sql.parser;
21+
22+
import org.apache.iotdb.commons.path.PartialPath;
23+
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Statement;
24+
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.CreateView;
25+
26+
import org.junit.Test;
27+
28+
import java.time.ZoneId;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
public class CreateViewPrefixPathTest {
33+
34+
@Test
35+
public void testQuotedTreePathSegment() throws Exception {
36+
final Statement statement =
37+
new SqlParser()
38+
.createStatement(
39+
"CREATE VIEW tv143_cmp_3.v (device STRING TAG, value INT32 FIELD FROM s1) "
40+
+ "WITH (ttl='INF') AS root.\"`tv143_cmp_3`\".\"`a b`\".**",
41+
ZoneId.systemDefault(),
42+
null);
43+
44+
assertEquals(
45+
new PartialPath("root.tv143_cmp_3.`a b`.**"), ((CreateView) statement).getPrefixPath());
46+
}
47+
}

0 commit comments

Comments
 (0)