This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new 9252e9682ee [Fix](create-table-like)Fix create table like error, the
converted table field COMMENT contains special characters (#25833)
9252e9682ee is described below
commit 9252e9682eed3a2e80f9a691147f8a738fb46ed7
Author: Calvin Kirs <[email protected]>
AuthorDate: Wed Oct 25 18:51:45 2023 +0800
[Fix](create-table-like)Fix create table like error, the converted table
field COMMENT contains special characters (#25833)
When parsing String syntax, ' and " are both legal in 2.0.2. We will remove
the outer boundary symbols, but you can add ' inside "", such as "I don't", but
2.0.2 does not escape the middle ' and returns it by default. will be wrapped
into ', so it becomes 'I don't'. This syntax analysis is wrong. Now we have to
escape all the symbols inside and it becomes 'I don/'t'.
CREATE TABLE IF NOT EXISTS a2 (
`c1` int NOT NULL DEFAULT "1" comment "sd",
`c2` int NOT NULL DEFAULT "4" comment "'nll'"
) ENGINE=OLAP
UNIQUE KEY(`c1`)
COMMENT "'llll'll'"
PARTITION BY LIST (`c1`)
(
PARTITION p1 VALUES IN ("1","2","3"),
PARTITION p2 VALUES IN ("4","5","6")
)
DISTRIBUTED BY HASH(`c1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
);
detailMessage = Failed to execute CREATE TABLE LIKE test_create_table_like.
Reason: errCode = 2, detailMessage = Syntax error in line 2:
...` date NOT NULL COMMENT ''a' is a date, but it's not a...
test
kris> CREATE TABLE test_create_table_like (
a DATEV2 NOT NULL COMMENT "'a' is a date, but it's not
a date/n,/r/n/t/n/'",
b VARCHAR(96) NOT NULL COMMENT ' /"a/" is a date,
/n/,/r/n/t/n',
c VARCHAR(96) NOT NULL COMMENT 'c',
d VARCHAR(96) COMMENT '',
e bigint NOT NULL )
DISTRIBUTED BY HASH(e) BUCKETS 1
PROPERTIES( 'replication_num' = '1')
[2023-10-24 16:50:52] completed in 504 ms
kris> create table tctl_1 like test_create_table_like
[2023-10-24 16:51:20] completed in 430 ms
kris> create table tctl_2 like tctl_1
[2023-10-24 16:51:30] completed in 429 ms
kris> create table tctl_3 like tctl_2
[2023-10-24 16:51:42] completed in 408 ms
kris> CREATE TABLE a1 (
a DATEV2 NOT NULL COMMENT "'a' is a date, but it's not
a date/n,/r/n/t/n/'",
b VARCHAR(96) NOT NULL COMMENT ' /"a/" is a date,
/n/,/r/n/t/n',
c VARCHAR(96) NOT NULL COMMENT 'c',
d VARCHAR(96) COMMENT '',
e bigint NOT NULL )
COMMENT "'a' is a date, but it's not a date/n,/r/n/t/n/'"
DISTRIBUTED BY HASH(e) BUCKETS 1
PROPERTIES( 'replication_num' = '1')
[2023-10-24 16:58:18] completed in 526 ms
kris> create table a2 like a1
[2023-10-24 16:58:32] completed in 389 ms
kris> create table a3 like a2
[2023-10-24 16:58:41] completed in 363 ms
kris> show create table a3
---
.../org/apache/doris/common/util/SqlUtils.java | 47 ++++++++++++++++++++--
.../suites/table_p0/create_table_like_p0.groovy | 36 +++++++++++++++++
2 files changed, 79 insertions(+), 4 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/common/util/SqlUtils.java
b/fe/fe-core/src/main/java/org/apache/doris/common/util/SqlUtils.java
index 096f94a4aff..93fad7773e9 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/common/util/SqlUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/common/util/SqlUtils.java
@@ -20,7 +20,6 @@ package org.apache.doris.common.util;
import org.apache.doris.parser.DorisSqlSeparatorLexer;
import org.apache.doris.parser.DorisSqlSeparatorParser;
-import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
@@ -29,6 +28,7 @@ import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ParseTree;
+import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
@@ -51,11 +51,50 @@ public class SqlUtils {
return sb.toString();
}
+ /**
+ * add escape characters for string
+ * @param str
+ * eg: "a'b" -> "a\'b"
+ * @return escaped string
+ */
public static String escapeQuota(String str) {
- if (Strings.isNullOrEmpty(str)) {
- return str;
+ StringWriter writer = new StringWriter();
+ int strLen = str.length();
+ for (int i = 0; i < strLen; ++i) {
+ char c = str.charAt(i);
+ switch (c) {
+ case '\n':
+ writer.append("\\n");
+ break;
+ case '\t':
+ writer.append("\\t");
+ break;
+ case '\r':
+ writer.append("\\r");
+ break;
+ case '\b':
+ writer.append("\\b");
+ break;
+ case '\0':
+ writer.append("\\0");
+ break;
+ case '\032':
+ writer.append("\\Z");
+ break;
+ case '\'':
+ writer.append("\\'");
+ break;
+ case '\"':
+ writer.append("\\\"");
+ break;
+
+ default:
+ writer.append(c);
+ break;
+ }
}
- return str.replaceAll("\"", "\\\\\"");
+
+ return writer.toString();
}
public static List<String> splitMultiStmts(String sql) {
diff --git a/regression-test/suites/table_p0/create_table_like_p0.groovy
b/regression-test/suites/table_p0/create_table_like_p0.groovy
new file mode 100644
index 00000000000..0a3197aa1db
--- /dev/null
+++ b/regression-test/suites/table_p0/create_table_like_p0.groovy
@@ -0,0 +1,36 @@
+// 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.
+
+suite("create_table_like_p0") {
+ sql """DROP TABLE IF EXISTS test_create_table_like"""
+ sql """
+ CREATE TABLE test_create_table_like (
+ a DATEV2 NOT NULL COMMENT "'a' is a date, but it's not a
date/n,/r/n/t/n/'",
+ b VARCHAR(96) NOT NULL COMMENT ' /"a/" is a date, /n/,/r/n/t/n',
+ c VARCHAR(96) NOT NULL COMMENT 'c',
+ d VARCHAR(96) COMMENT '',
+ e bigint NOT NULL )
+ DISTRIBUTED BY HASH(e) BUCKETS 1
+ PROPERTIES( 'replication_num' = '1');
+ """
+ sql """DROP TABLE IF EXISTS tctl_2"""
+ sql """DROP TABLE IF EXISTS tctl_3"""
+ sql """DROP TABLE IF EXISTS tctl_1"""
+ sql """create table tctl_1 like test_create_table_like"""
+ sql """create table tctl_2 like tctl_1"""
+ sql """create table tctl_3 like tctl_2"""
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]