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 all commits
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
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
10 changes: 10 additions & 0 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ public static boolean canCastTo(ArrayType type, ArrayType targetType) {
return Type.canCastTo(type.getItemType(), targetType.getItemType());
}

public static Type getAssignmentCompatibleType(ArrayType t1, ArrayType t2, boolean strict) {
Type itemCompatibleType = Type.getAssignmentCompatibleType(t1.getItemType(), t2.getItemType(), strict);

if (itemCompatibleType.isInvalid()) {
return ScalarType.INVALID;
}

return new ArrayType(itemCompatibleType, t1.getContainsNull() || t2.getContainsNull());
}

@Override
public void toThrift(TTypeDesc container) {
TTypeNode node = new TTypeNode();
Expand Down
15 changes: 15 additions & 0 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ public static boolean canCastTo(MapType type, MapType targetType) {
|| targetType.getValueType().isStringType() && type.getValueType().isStringType());
}

public static Type getAssignmentCompatibleType(MapType t1, MapType t2, boolean strict) {
Type keyCompatibleType = Type.getAssignmentCompatibleType(t1.getKeyType(), t2.getKeyType(), strict);
if (keyCompatibleType.isInvalid()) {
return ScalarType.INVALID;
}
Type valCompatibleType = Type.getAssignmentCompatibleType(t1.getValueType(), t2.getValueType(), strict);
if (valCompatibleType.isInvalid()) {
return ScalarType.INVALID;
}

return new MapType(keyCompatibleType, valCompatibleType,
t1.getIsKeyContainsNull() || t2.getIsKeyContainsNull(),
t1.getIsValueContainsNull() || t2.getIsValueContainsNull());
}

@Override
public boolean supportSubType(Type subType) {
for (Type supportedType : Type.getMapSubTypes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -110,6 +111,29 @@ public static boolean canCastTo(StructType type, StructType targetType) {
return true;
}

public static Type getAssignmentCompatibleType(StructType t1, StructType t2, boolean strict) {
ArrayList<StructField> fieldsLeft = t1.getFields();
ArrayList<StructField> fieldsRight = t2.getFields();
ArrayList<StructField> fieldsRes = new ArrayList<>();

for (int i = 0; i < t1.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 ScalarType.INVALID;
}
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);
}

@Override
public boolean isSupported() {
for (StructField f : fields) {
Expand Down
27 changes: 10 additions & 17 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 @@ -777,19 +777,14 @@ public static Type getAssignmentCompatibleType(Type t1, Type t2, boolean strict)
}

if (t1.isArrayType() && t2.isArrayType()) {
ArrayType arrayType1 = (ArrayType) t1;
ArrayType arrayType2 = (ArrayType) t2;
Type itemCompatibleType = Type.getAssignmentCompatibleType(arrayType1.getItemType(),
arrayType2.getItemType(), strict);

if (itemCompatibleType.isInvalid()) {
return itemCompatibleType;
}

return new ArrayType(itemCompatibleType, arrayType1.getContainsNull() || arrayType2.getContainsNull());
} else if (t1.isArrayType() && t2.isNull()) {
return ArrayType.getAssignmentCompatibleType((ArrayType) t1, (ArrayType) t2, strict);
} else if (t1.isMapType() && t2.isMapType()) {
return MapType.getAssignmentCompatibleType((MapType) t1, (MapType) t2, strict);
} else if (t1.isStructType() && t2.isStructType()) {
return StructType.getAssignmentCompatibleType((StructType) t1, (StructType) t2, strict);
} 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 @@ -2165,17 +2160,15 @@ public static boolean matchExactType(Type type1, Type type2, boolean ignorePreci
} else if (type2.isArrayType()) {
// For types array, we also need to check contains null for case like
// cast(array<not_null(int)> as array<int>)
if (!((ArrayType) type2).getContainsNull() == ((ArrayType) type1).getContainsNull()) {
if (((ArrayType) type2).getContainsNull() != ((ArrayType) type1).getContainsNull()) {
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()) {
if (((MapType) type2).getIsKeyContainsNull() != ((MapType) type1).getIsKeyContainsNull()) {
return false;
}
if (!((MapType) type2).getIsValueContainsNull() == ((MapType) type1).getIsValueContainsNull()) {
if (((MapType) type2).getIsValueContainsNull() != ((MapType) type1).getIsValueContainsNull()) {
return false;
}
return matchExactType(((MapType) type2).getKeyType(), ((MapType) type1).getKeyType())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql_as --
text [{"a": 3, "b": "home"}, {"a": 4, "b": "work"}]

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 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'}]);"

order_qt_sql_as """ select * from ast; """

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 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'}]);"

order_qt_sql_as """ select * from ast; """

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."
}
}