Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX](struct) fix struct be nested when read will make core #26270

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix struct be nested when read will make core
  • Loading branch information
amorynan committed Nov 2, 2023
commit 1f906237f0227757519e95c2cd3583b63b062ad1
1 change: 1 addition & 0 deletions be/src/olap/rowset/segment_v2/column_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB&
case FieldType::OLAP_FIELD_TYPE_STRUCT: {
// not support empty struct
DCHECK(meta.children_columns_size() >= 1);
num_rows = meta.children_columns(0).num_rows();
// create struct column reader
std::unique_ptr<ColumnReader> struct_reader(
new ColumnReader(opts, meta, num_rows, file_reader));
Expand Down
56 changes: 54 additions & 2 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -787,9 +788,49 @@ public static Type getAssignmentCompatibleType(Type t1, Type t2, boolean strict)
}

return new ArrayType(itemCompatibleType, arrayType1.getContainsNull() || arrayType2.getContainsNull());
} else if (t1.isArrayType() && t2.isNull()) {
} else if (t1.isMapType() && t2.isMapType()) {
MapType mapType1 = (MapType) t1;
MapType mapType2 = (MapType) t2;
Type keyCompatibleType = Type.getAssignmentCompatibleType(mapType1.getKeyType(),
mapType2.getKeyType(), strict);
if (keyCompatibleType.isInvalid()) {
return keyCompatibleType;
}
Type valCompatibleType = Type.getAssignmentCompatibleType(mapType1.getValueType(),
mapType2.getValueType(), strict);
if (valCompatibleType.isInvalid()) {
return valCompatibleType;
}

return new MapType(keyCompatibleType, valCompatibleType,
mapType1.getIsKeyContainsNull() || mapType2.getIsKeyContainsNull(),
mapType1.getIsValueContainsNull() || mapType2.getIsValueContainsNull());
} else if (t1.isStructType() && t2.isStructType()) {
StructType structType1 = (StructType) t1;
StructType structType2 = (StructType) t2;
ArrayList<StructField> fieldsLeft = structType1.getFields();
ArrayList<StructField> fieldsRight = structType2.getFields();
ArrayList<StructField> fieldsRes = new ArrayList<>();

for (int i = 0; i < structType1.getFields().size(); ++i) {
StructField leftField = fieldsLeft.get(i);
StructField rightField = fieldsRight.get(i);
Type itemCompatibleType = Type.getAssignmentCompatibleType(leftField.getType(), rightField.getType(),
strict);
if (itemCompatibleType.isInvalid()) {
return itemCompatibleType;
}
fieldsRes.add(new StructField(StringUtils.isEmpty(leftField.getName()) ? rightField.getName()
: leftField.getName(),
itemCompatibleType, StringUtils.isEmpty(leftField.getComment()) ? rightField.getComment()
: leftField.getComment(), leftField.getContainsNull() || rightField.getContainsNull()));

}

return new StructType(fieldsRes);
} else if (t1.isComplexType() && t2.isNull()) {
return t1;
} else if (t1.isNull() && t2.isArrayType()) {
} else if (t1.isNull() && t2.isComplexType()) {
return t2;
}

Expand Down Expand Up @@ -2169,6 +2210,17 @@ public static boolean matchExactType(Type type1, Type type2, boolean ignorePreci
return false;
}
return matchExactType(((ArrayType) type2).getItemType(), ((ArrayType) type1).getItemType());
} else if (type2.isMapType()) {
// For types array, we also need to check contains null for case like
// cast(array<not_null(int)> as array<int>)
if (!((MapType) type2).getIsKeyContainsNull() == ((MapType) type1).getIsKeyContainsNull()) {
return false;
}
if (!((MapType) type2).getIsValueContainsNull() == ((MapType) type1).getIsValueContainsNull()) {
return false;
}
return matchExactType(((MapType) type2).getKeyType(), ((MapType) type1).getKeyType())
&& matchExactType(((MapType) type2).getValueType(), ((MapType) type1).getValueType());
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql_as --
text [{3, "home"}, {4, "work"}]

-- !sql_am --
txt [{6:"amory", 7:"now", 10:"commit"}]

-- !sql_as --
text [{3, "home"}, {4, "work"}]

-- !sql_am --
txt [{6:"amory", 7:"now", 10:"commit"}]

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.


import com.google.common.collect.Lists
import org.apache.commons.lang3.StringUtils
import org.codehaus.groovy.runtime.IOGroovyMethods

suite("test_nestedtypes_insert_into_select", "p0") {
sql "set enable_nereids_planner=false"
sql """ADMIN SET FRONTEND CONFIG ('disable_nested_complex_type' = 'false')"""

// create array - map
sql "DROP TABLE IF EXISTS amt;"
sql """
CREATE TABLE IF NOT EXISTS amt (col1 varchar(64) NULL, col2 array<map<int, string>>) DUPLICATE KEY(`col1`) DISTRIBUTED BY HASH(`col1`) PROPERTIES ("replication_num" = "1"); """

// create array struct
sql "DROP TABLE IF EXISTS ast;"
sql """ CREATE TABLE IF NOT EXISTS ast (col1 varchar(64) NULL, col2 array<struct<a:int,b:string>>) DUPLICATE KEY(`col1`) DISTRIBUTED BY HASH(`col1`) PROPERTIES ("replication_num" = "1"); """

// test insert into with literal
sql "INSERT INTO ast values ('text',[{3,'home'},{4,'work'}]);"

sql "INSERT INTO amt values ('txt',[{6:'amory', 7:'now', 10: 'commit'}]);"

order_qt_sql_as """ select * from ast; """
order_qt_sql_am """ select * from amt; """

test {
sql "insert into ast values ('text' , [named_struct('a',1,'b','home'),named_struct('a',2,'b','work')]);"
exception "errCode = 2, detailMessage = Sql parser can't convert the result to array, please check your sql."
}


sql "set enable_nereids_planner=true"
sql " set enable_fallback_to_original_planner=false"

// create array - map
sql "DROP TABLE IF EXISTS amt;"
sql """
CREATE TABLE IF NOT EXISTS amt (col1 varchar(64) NULL, col2 array<map<int, string>>) DUPLICATE KEY(`col1`) DISTRIBUTED BY HASH(`col1`) PROPERTIES ("replication_num" = "1"); """

// create array struct
sql "DROP TABLE IF EXISTS ast;"
sql """ CREATE TABLE IF NOT EXISTS ast (col1 varchar(64) NULL, col2 array<struct<a:int,b:string>>) DUPLICATE KEY(`col1`) DISTRIBUTED BY HASH(`col1`) PROPERTIES ("replication_num" = "1"); """

// test insert into with literal
sql "INSERT INTO ast values ('text',[{3,'home'},{4,'work'}]);"

sql "INSERT INTO amt values ('txt',[{6:'amory', 7:'now', 10: 'commit'}]);"

order_qt_sql_as """ select * from ast; """
order_qt_sql_am """ select * from amt; """

test {
sql "insert into ast values ('text' , [named_struct('a',1,'b','home'),named_struct('a',2,'b','work')]);"
exception "errCode = 2, detailMessage = Sql parser can't convert the result to array, please check your sql."
}
}