Skip to content

Commit 3a154a2

Browse files
[fix](nereids) fix create view use null literal (#49881)
Related PR: #32743 Problem Summary: problem before this pr: 1. null type problem the type of NULL is null_type, this will lead to error when select this view through jdbc catalog. ```sql mysql> CREATE VIEW test_null COMMENT '测试null类型' AS SELECT NULL AS `col1`; Query OK, 0 rows affected (0.02 sec) mysql> desc test_null; +-------+-----------+------+-------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-------+---------+-------+ | col1 | null_type | Yes | false | NULL | | +-------+-----------+------+-------+---------+-------+ 1 row in set (0.00 sec) ``` after this pr, the type is changed to tinyint. mysql> CREATE VIEW test_null2 COMMENT '测试null类型' AS SELECT NULL AS `col1`; Query OK, 0 rows affected (0.03 sec) ```sql mysql> desc test_null2; +-------+---------+------+-------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-------+---------+-------+ | col1 | tinyint | Yes | false | NULL | | +-------+---------+------+-------+---------+-------+ 1 row in set (0.01 sec) ``` 2. nullable problem Modify the internal elements of nested types returned by JDBC to always be null, in order to maintain consistency with Doris' internal implementation ### Release note
1 parent 01725e5 commit 3a154a2

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

fe/be-java-extensions/java-common/src/main/java/org/apache/doris/common/jni/vec/VectorColumn.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ public void appendArray(List<Object>[] batch, boolean isNullable) {
12361236
}
12371237
}
12381238
}
1239-
childColumns[0].appendObjectColumn(nested, isNullable);
1239+
childColumns[0].appendObjectColumn(nested, true);
12401240
}
12411241

12421242
public ArrayList<Object> getArray(int rowId) {
@@ -1300,8 +1300,8 @@ public void appendMap(Map<Object, Object>[] batch, boolean isNullable) {
13001300
}
13011301
}
13021302
}
1303-
childColumns[0].appendObjectColumn(keys, isNullable);
1304-
childColumns[1].appendObjectColumn(values, isNullable);
1303+
childColumns[0].appendObjectColumn(keys, true);
1304+
childColumns[1].appendObjectColumn(values, true);
13051305
}
13061306

13071307
public HashMap<Object, Object> getMap(int rowId) {
@@ -1363,7 +1363,7 @@ public void appendStruct(Map<String, Object>[] batch, boolean isNullable) {
13631363
appendIndex++;
13641364
}
13651365
for (int j = 0; j < childColumns.length; ++j) {
1366-
childColumns[j].appendObjectColumn(columnData[j], isNullable);
1366+
childColumns[j].appendObjectColumn(columnData[j], true);
13671367
}
13681368
}
13691369

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/BaseViewInfo.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
import org.apache.doris.nereids.trees.plans.logical.LogicalView;
6464
import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
6565
import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;
66+
import org.apache.doris.nereids.types.DataType;
67+
import org.apache.doris.nereids.types.NullType;
68+
import org.apache.doris.nereids.types.TinyIntType;
69+
import org.apache.doris.nereids.util.TypeCoercionUtils;
6670
import org.apache.doris.nereids.util.Utils;
6771
import org.apache.doris.qe.ConnectContext;
6872

@@ -162,17 +166,21 @@ protected String rewriteProjectsToUserDefineAlias(String resSql) {
162166
protected void createFinalCols(List<Slot> outputs) throws org.apache.doris.common.AnalysisException {
163167
if (simpleColumnDefinitions.isEmpty()) {
164168
for (Slot output : outputs) {
165-
Column column = new Column(output.getName(), output.getDataType().toCatalogDataType(),
166-
output.nullable());
169+
DataType dataType = TypeCoercionUtils.replaceSpecifiedType(output.getDataType(), NullType.class,
170+
TinyIntType.INSTANCE);
171+
Column column = new Column(output.getName(), dataType.toCatalogDataType(), output.nullable());
167172
finalCols.add(column);
168173
}
169174
} else {
170175
if (outputs.size() != simpleColumnDefinitions.size()) {
171176
ErrorReport.reportAnalysisException(ErrorCode.ERR_VIEW_WRONG_LIST);
172177
}
173178
for (int i = 0; i < simpleColumnDefinitions.size(); ++i) {
179+
Slot output = outputs.get(i);
180+
DataType dataType = TypeCoercionUtils.replaceSpecifiedType(output.getDataType(), NullType.class,
181+
TinyIntType.INSTANCE);
174182
Column column = new Column(simpleColumnDefinitions.get(i).getName(),
175-
outputs.get(i).getDataType().toCatalogDataType(), outputs.get(i).nullable());
183+
dataType.toCatalogDataType(), output.nullable());
176184
column.setComment(simpleColumnDefinitions.get(i).getComment());
177185
finalCols.add(column);
178186
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !test_null --
3+
\N
4+
5+
-- !test_null_array --
6+
[null, null]
7+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("create_view_nereids_fix_null") {
19+
sql "drop view if exists test_null"
20+
sql "CREATE VIEW test_null COMMENT '测试null类型' AS SELECT NULL AS `col1`; "
21+
def res = sql "desc test_null"
22+
mustContain(res[0][1], "tinyint")
23+
24+
sql "drop view if exists test_null_array"
25+
sql "CREATE VIEW test_null_array COMMENT '测试null类型' AS SELECT [NULL, NULL] AS `col1`; "
26+
def res2 = sql "desc test_null_array"
27+
mustContain(res2[0][1], "array<tinyint>")
28+
mustContain(res2[0][2], "No")
29+
30+
String s3_endpoint = getS3Endpoint()
31+
logger.info("s3_endpoint: " + s3_endpoint)
32+
String bucket = getS3BucketName()
33+
logger.info("bucket: " + bucket)
34+
String driver_url = "https://${bucket}.${s3_endpoint}/regression/jdbc_driver/mysql-connector-java-8.0.25.jar"
35+
String dbname = context.config.getDbNameByFile(context.file)
36+
String jdbcUrl = context.config.jdbcUrl
37+
String jdbcUser = context.config.jdbcUser
38+
logger.info("jdbcUser: " + jdbcUser)
39+
String jdbcPassword = context.config.jdbcPassword
40+
logger.info("jdbcPassword: " + jdbcPassword)
41+
sql "drop catalog if exists create_view_nereids_fix_null_catalog"
42+
sql """
43+
CREATE CATALOG create_view_nereids_fix_null_catalog PROPERTIES (
44+
"type"="jdbc",
45+
"user"="${jdbcUser}",
46+
"password"="${jdbcPassword}",
47+
"jdbc_url"="${jdbcUrl}",
48+
"driver_url"="${driver_url}",
49+
"driver_class"="com.mysql.cj.jdbc.Driver"
50+
);
51+
"""
52+
sql "switch create_view_nereids_fix_null_catalog"
53+
sql "use ${dbname}"
54+
qt_test_null "select * from test_null"
55+
qt_test_null_array "select * from test_null_array"
56+
sql "drop catalog create_view_nereids_fix_null_catalog;"
57+
}

0 commit comments

Comments
 (0)