]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/complex/MapVector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / complex / MapVector.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.vector.complex;
19
20 import static org.apache.arrow.util.Preconditions.checkArgument;
21
22 import java.util.List;
23
24 import org.apache.arrow.memory.BufferAllocator;
25 import org.apache.arrow.vector.AddOrGetResult;
26 import org.apache.arrow.vector.FieldVector;
27 import org.apache.arrow.vector.complex.impl.UnionMapReader;
28 import org.apache.arrow.vector.complex.impl.UnionMapWriter;
29 import org.apache.arrow.vector.types.Types;
30 import org.apache.arrow.vector.types.Types.MinorType;
31 import org.apache.arrow.vector.types.pojo.ArrowType.Map;
32 import org.apache.arrow.vector.types.pojo.Field;
33 import org.apache.arrow.vector.types.pojo.FieldType;
34 import org.apache.arrow.vector.util.CallBack;
35
36 /**
37 * A MapVector is used to store entries of key/value pairs. It is a container vector that is
38 * composed of a list of struct values with "key" and "value" fields. The MapVector is nullable,
39 * but if a map is set at a given index, there must be an entry. In other words, the StructVector
40 * data is non-nullable. Also for a given entry, the "key" is non-nullable, however the "value" can
41 * be null.
42 */
43 public class MapVector extends ListVector {
44
45 public static final String KEY_NAME = "key";
46 public static final String VALUE_NAME = "value";
47 public static final String DATA_VECTOR_NAME = "entries";
48
49 /**
50 * Construct an empty MapVector with no data. Child vectors must be added subsequently.
51 *
52 * @param name The name of the vector.
53 * @param allocator The allocator used for allocating/reallocating buffers.
54 * @param keysSorted True if the map keys have been pre-sorted.
55 * @return a new instance of MapVector.
56 */
57 public static MapVector empty(String name, BufferAllocator allocator, boolean keysSorted) {
58 return new MapVector(name, allocator, FieldType.nullable(new Map(keysSorted)), null);
59 }
60
61 /**
62 * Construct a MapVector instance.
63 *
64 * @param name The name of the vector.
65 * @param allocator The allocator used for allocating/reallocating buffers.
66 * @param fieldType The type definition of the MapVector.
67 * @param callBack A schema change callback.
68 */
69 public MapVector(String name, BufferAllocator allocator, FieldType fieldType, CallBack callBack) {
70 super(name, allocator, fieldType, callBack);
71 defaultDataVectorName = DATA_VECTOR_NAME;
72 }
73
74 /**
75 * Initialize child vectors of the map from the given list of fields.
76 *
77 * @param children List of fields that will be children of this MapVector.
78 */
79 @Override
80 public void initializeChildrenFromFields(List<Field> children) {
81 checkArgument(children.size() == 1, "Maps have one List child. Found: %s", children);
82
83 Field structField = children.get(0);
84 MinorType minorType = Types.getMinorTypeForArrowType(structField.getType());
85 checkArgument(minorType == MinorType.STRUCT && !structField.isNullable(),
86 "Map data should be a non-nullable struct type");
87 checkArgument(structField.getChildren().size() == 2,
88 "Map data should be a struct with 2 children. Found: %s", children);
89
90 Field keyField = structField.getChildren().get(0);
91 checkArgument(!keyField.isNullable(), "Map data key type should be a non-nullable");
92
93 AddOrGetResult<FieldVector> addOrGetVector = addOrGetVector(structField.getFieldType());
94 checkArgument(addOrGetVector.isCreated(), "Child vector already existed: %s", addOrGetVector.getVector());
95
96 addOrGetVector.getVector().initializeChildrenFromFields(structField.getChildren());
97 }
98
99 /**
100 * Get the writer for this MapVector instance.
101 */
102 @Override
103 public UnionMapWriter getWriter() {
104 return new UnionMapWriter(this);
105 }
106
107 /**
108 * Get the reader for this MapVector instance.
109 */
110 @Override
111 public UnionMapReader getReader() {
112 if (reader == null) {
113 reader = new UnionMapReader(this);
114 }
115 return (UnionMapReader) reader;
116 }
117
118 @Override
119 public MinorType getMinorType() {
120 return MinorType.MAP;
121 }
122 }