xiaokang commented on code in PR #15966:
URL: https://github.com/apache/doris/pull/15966#discussion_r1071929373


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java:
##########
@@ -396,37 +403,78 @@ public TColumn toThrift() {
         return tColumn;
     }
 
+
+    // here to make complex type column easy
+    private void setChildrenTColumn(Column children, TColumn tColumn) {
+        TColumn childrenTColumn = new TColumn();
+        childrenTColumn.setColumnName(children.name);
+
+        TColumnType childrenTColumnType = new TColumnType();
+        childrenTColumnType.setType(children.getDataType().toThrift());
+        childrenTColumnType.setLen(children.getStrLen());
+        childrenTColumnType.setPrecision(children.getPrecision());
+        childrenTColumnType.setScale(children.getScale());
+        childrenTColumnType.setIndexLen(children.getOlapColumnIndexSize());
+
+        childrenTColumn.setColumnType(childrenTColumnType);
+        childrenTColumn.setIsAllowNull(children.isAllowNull());
+        // TODO: If we don't set the aggregate type for children, the type 
will be
+        //  considered as TAggregationType::SUM after deserializing in BE.
+        //  For now, we make children inherit the aggregate type from their 
parent.
+        if (tColumn.getAggregationType() != null) {
+            childrenTColumn.setAggregationType(tColumn.getAggregationType());
+        }
+
+        tColumn.children_column.add(childrenTColumn);
+        toChildrenThrift(children, childrenTColumn);
+    }
+
+
     private void toChildrenThrift(Column column, TColumn tColumn) {
         if (column.type.isArrayType()) {
             Column children = column.getChildren().get(0);
-
-            TColumn childrenTColumn = new TColumn();
-            childrenTColumn.setColumnName(children.name);
-
-            TColumnType childrenTColumnType = new TColumnType();
-            childrenTColumnType.setType(children.getDataType().toThrift());
-            childrenTColumnType.setType(children.getDataType().toThrift());
-            childrenTColumnType.setLen(children.getStrLen());
-            childrenTColumnType.setPrecision(children.getPrecision());
-            childrenTColumnType.setScale(children.getScale());
-
-            childrenTColumnType.setIndexLen(children.getOlapColumnIndexSize());
-            childrenTColumn.setColumnType(childrenTColumnType);
-            childrenTColumn.setIsAllowNull(children.isAllowNull());
-            // TODO: If we don't set the aggregate type for children, the type 
will be
-            //  considered as TAggregationType::SUM after deserializing in BE.
-            //  For now, we make children inherit the aggregate type from 
their parent.
-            if (tColumn.getAggregationType() != null) {
-                
childrenTColumn.setAggregationType(tColumn.getAggregationType());
-            }
-
             tColumn.setChildrenColumn(new ArrayList<>());
-            tColumn.children_column.add(childrenTColumn);
-
-            toChildrenThrift(children, childrenTColumn);
+            setChildrenTColumn(children, tColumn);
+        } else if (column.type.isMapType()) {
+            Column k = column.getChildren().get(0);
+            Column v = column.getChildren().get(1);
+            tColumn.setChildrenColumn(new ArrayList<>());
+            setChildrenTColumn(k, tColumn);
+            setChildrenTColumn(v, tColumn);
         }
     }
 
+    //private void toChildrenThrift(Column column, TColumn tColumn) {

Review Comment:
   delete useless code



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MapType.java:
##########
@@ -98,4 +134,16 @@ public void toThrift(TTypeDesc container) {
         keyType.toThrift(container);
         valueType.toThrift(container);
     }
+
+    @Override
+    public TColumnType toColumnTypeThrift() {
+        TColumnType thrift = new TColumnType();
+        thrift.type = PrimitiveType.MAP.toThrift();

Review Comment:
   no need to process keyType and valType?



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java:
##########
@@ -0,0 +1,179 @@
+// 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.
+
+package org.apache.doris.analysis;
+
+import org.apache.doris.catalog.MapType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.thrift.TExprNode;
+import org.apache.doris.thrift.TExprNodeType;
+import org.apache.doris.thrift.TTypeDesc;
+import org.apache.doris.thrift.TTypeNode;
+
+import org.apache.commons.lang.StringUtils;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+// INSERT INTO table_map VALUES ({'key1':1, 'key2':10, 'k3':100}), 
({'key1':2,'key2':20}), ({'key1':3,'key2':30});
+// MapLiteral is one row-based literal
+public class MapLiteral extends LiteralExpr {
+
+    public MapLiteral() {
+        type = new MapType(Type.NULL, Type.NULL);
+        children = new ArrayList<>();
+    }
+
+    public MapLiteral(LiteralExpr... exprs) throws AnalysisException {
+        Type keyType = Type.NULL;
+        Type valueType = Type.NULL;
+        children = new ArrayList<>();
+        int idx = 0;
+        for (LiteralExpr expr : exprs) {
+            if (idx % 2 == 0) {
+                if (keyType == Type.NULL) {
+                    keyType = expr.getType();
+                } else {
+                    keyType = Type.getAssignmentCompatibleType(keyType, 
expr.getType(), false);
+                }
+                if (keyType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            } else {
+                if (valueType == Type.NULL) {
+                    valueType = expr.getType();
+                } else {
+                    valueType = Type.getAssignmentCompatibleType(valueType, 
expr.getType(), false);
+                }
+                if (valueType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            }
+            children.add(expr);
+            ++ idx;
+        }
+
+        type = new MapType(keyType, valueType);
+    }
+
+    protected MapLiteral(MapLiteral other) {
+        super(other);
+    }
+
+    @Override
+    public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
+        if (!targetType.isMapType()) {
+            return super.uncheckedCastTo(targetType);
+        }
+        MapLiteral literal = new MapLiteral(this);
+        Type keyType = ((MapType) targetType).getKeyType();
+        Type valueType = ((MapType) targetType).getValueType();
+
+        for (int i = 0; i < children.size(); ++ i) {
+            Expr child = children.get(i);
+            if ((i & 1) == 0) {

Review Comment:
   use a consistent way to check key/value index



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java:
##########
@@ -0,0 +1,179 @@
+// 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.
+
+package org.apache.doris.analysis;
+
+import org.apache.doris.catalog.MapType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.thrift.TExprNode;
+import org.apache.doris.thrift.TExprNodeType;
+import org.apache.doris.thrift.TTypeDesc;
+import org.apache.doris.thrift.TTypeNode;
+
+import org.apache.commons.lang.StringUtils;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+// INSERT INTO table_map VALUES ({'key1':1, 'key2':10, 'k3':100}), 
({'key1':2,'key2':20}), ({'key1':3,'key2':30});
+// MapLiteral is one row-based literal
+public class MapLiteral extends LiteralExpr {
+
+    public MapLiteral() {
+        type = new MapType(Type.NULL, Type.NULL);
+        children = new ArrayList<>();
+    }
+
+    public MapLiteral(LiteralExpr... exprs) throws AnalysisException {
+        Type keyType = Type.NULL;
+        Type valueType = Type.NULL;
+        children = new ArrayList<>();
+        int idx = 0;
+        for (LiteralExpr expr : exprs) {
+            if (idx % 2 == 0) {
+                if (keyType == Type.NULL) {
+                    keyType = expr.getType();
+                } else {
+                    keyType = Type.getAssignmentCompatibleType(keyType, 
expr.getType(), false);
+                }
+                if (keyType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            } else {
+                if (valueType == Type.NULL) {
+                    valueType = expr.getType();
+                } else {
+                    valueType = Type.getAssignmentCompatibleType(valueType, 
expr.getType(), false);
+                }
+                if (valueType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            }
+            children.add(expr);
+            ++ idx;
+        }
+
+        type = new MapType(keyType, valueType);
+    }
+
+    protected MapLiteral(MapLiteral other) {
+        super(other);
+    }
+
+    @Override
+    public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
+        if (!targetType.isMapType()) {
+            return super.uncheckedCastTo(targetType);
+        }
+        MapLiteral literal = new MapLiteral(this);
+        Type keyType = ((MapType) targetType).getKeyType();
+        Type valueType = ((MapType) targetType).getValueType();
+
+        for (int i = 0; i < children.size(); ++ i) {
+            Expr child = children.get(i);
+            if ((i & 1) == 0) {
+                literal.children.set(i, child.uncheckedCastTo(keyType));
+            } else {
+                literal.children.set(i, child.uncheckedCastTo(valueType));
+            }
+        }
+        literal.setType(targetType);
+        return literal;
+    }
+
+    @Override
+    public void checkValueValid() throws AnalysisException {
+        for (Expr e : children) {
+            e.checkValueValid();
+        }
+    }
+
+    @Override
+    protected String toSqlImpl() {
+        List<String> list = new ArrayList<>(children.size());
+        for (int i = 0; i < children.size(); i += 2) {
+            list.add(children.get(i).toSqlImpl() + ":" + children.get(i + 
1).toSqlImpl());
+        }
+        return "MAP{" + StringUtils.join(list, ", ") + "}";
+    }
+
+    @Override
+    protected void toThrift(TExprNode msg) {
+        msg.node_type = TExprNodeType.MAP_LITERAL;
+        TTypeDesc container = new TTypeDesc();
+        container.setTypes(new ArrayList<TTypeNode>());
+        type.toThrift(container);
+        msg.setType(container);

Review Comment:
   no key/value data?



##########
fe/fe-core/src/main/java/org/apache/doris/mysql/MysqlColType.java:
##########
@@ -52,7 +52,8 @@ public enum MysqlColType {
     MYSQL_TYPE_BLOB(252, "BLOB"),
     MYSQL_TYPE_VARSTRING(253, "VAR STRING"),
     MYSQL_TYPE_STRING(254, "STRING"),
-    MYSQL_TYPE_GEOMETRY(255, "GEOMETRY");
+    MYSQL_TYPE_GEOMETRY(255, "GEOMETRY"),
+    MYSQL_TYPE_MAP(256, "MAP");

Review Comment:
   we can use a larger value, eg. 400, instead of continous value to avoid 
conflict with new data type in mysql



##########
fe/fe-core/src/main/java/org/apache/doris/planner/SetOperationNode.java:
##########
@@ -185,6 +185,9 @@ public void finalize(Analyzer analyzer) throws 
UserException {
         // corresponding output slot isn't being materialized)
         materializedResultExprLists.clear();
         Preconditions.checkState(resultExprLists.size() == children.size());
+        if (analyzer.getDescTbl().getTupleDesc(tupleId) == null) {

Review Comment:
   what's the effect of this?



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/MapLiteral.java:
##########
@@ -0,0 +1,179 @@
+// 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.
+
+package org.apache.doris.analysis;
+
+import org.apache.doris.catalog.MapType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.thrift.TExprNode;
+import org.apache.doris.thrift.TExprNodeType;
+import org.apache.doris.thrift.TTypeDesc;
+import org.apache.doris.thrift.TTypeNode;
+
+import org.apache.commons.lang.StringUtils;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+// INSERT INTO table_map VALUES ({'key1':1, 'key2':10, 'k3':100}), 
({'key1':2,'key2':20}), ({'key1':3,'key2':30});
+// MapLiteral is one row-based literal
+public class MapLiteral extends LiteralExpr {
+
+    public MapLiteral() {
+        type = new MapType(Type.NULL, Type.NULL);
+        children = new ArrayList<>();
+    }
+
+    public MapLiteral(LiteralExpr... exprs) throws AnalysisException {
+        Type keyType = Type.NULL;
+        Type valueType = Type.NULL;
+        children = new ArrayList<>();
+        int idx = 0;
+        for (LiteralExpr expr : exprs) {
+            if (idx % 2 == 0) {
+                if (keyType == Type.NULL) {
+                    keyType = expr.getType();
+                } else {
+                    keyType = Type.getAssignmentCompatibleType(keyType, 
expr.getType(), false);
+                }
+                if (keyType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            } else {
+                if (valueType == Type.NULL) {
+                    valueType = expr.getType();
+                } else {
+                    valueType = Type.getAssignmentCompatibleType(valueType, 
expr.getType(), false);
+                }
+                if (valueType == Type.INVALID) {
+                    throw new AnalysisException("Invalid element type in Map");
+                }
+            }
+            children.add(expr);
+            ++ idx;
+        }
+
+        type = new MapType(keyType, valueType);
+    }
+
+    protected MapLiteral(MapLiteral other) {
+        super(other);
+    }
+
+    @Override
+    public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
+        if (!targetType.isMapType()) {
+            return super.uncheckedCastTo(targetType);
+        }
+        MapLiteral literal = new MapLiteral(this);
+        Type keyType = ((MapType) targetType).getKeyType();
+        Type valueType = ((MapType) targetType).getValueType();
+
+        for (int i = 0; i < children.size(); ++ i) {
+            Expr child = children.get(i);
+            if ((i & 1) == 0) {
+                literal.children.set(i, child.uncheckedCastTo(keyType));
+            } else {
+                literal.children.set(i, child.uncheckedCastTo(valueType));
+            }
+        }
+        literal.setType(targetType);
+        return literal;
+    }
+
+    @Override
+    public void checkValueValid() throws AnalysisException {
+        for (Expr e : children) {
+            e.checkValueValid();
+        }
+    }
+
+    @Override
+    protected String toSqlImpl() {
+        List<String> list = new ArrayList<>(children.size());
+        for (int i = 0; i < children.size(); i += 2) {
+            list.add(children.get(i).toSqlImpl() + ":" + children.get(i + 
1).toSqlImpl());
+        }
+        return "MAP{" + StringUtils.join(list, ", ") + "}";
+    }
+
+    @Override
+    protected void toThrift(TExprNode msg) {
+        msg.node_type = TExprNodeType.MAP_LITERAL;
+        TTypeDesc container = new TTypeDesc();
+        container.setTypes(new ArrayList<TTypeNode>());
+        type.toThrift(container);
+        msg.setType(container);
+    }
+
+    @Override
+    public Expr clone() {
+        return new MapLiteral(this);
+    }
+
+    @Override
+    public boolean isMinValue() {
+        return false;
+    }
+
+    @Override
+    public int compareLiteral(LiteralExpr expr) {
+        return 0;
+    }
+
+    @Override
+    public void readFields(DataInput in) throws IOException {
+        super.readFields(in);
+        int size = in.readInt();
+        children = new ArrayList<>(size);
+        for (int i = 0; i < size; i++) {
+            children.add(Expr.readIn(in));
+        }
+    }
+
+    public static MapLiteral read(DataInput in) throws IOException {
+        MapLiteral literal = new MapLiteral();
+        literal.readFields(in);
+        return literal;
+    }
+
+    @Override
+    public void write(DataOutput out) throws IOException {
+        super.write(out);
+        out.writeInt(children.size());
+        for (Expr e : children) {
+            Expr.writeTo(e, out);
+        }
+    }
+
+    @Override
+    public String getStringValue() {

Review Comment:
   should be the same as toSqlImpl()



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to