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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.vector
.complex
;
20 import static org
.apache
.arrow
.util
.Preconditions
.checkArgument
;
22 import java
.util
.List
;
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
;
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
43 public class MapVector
extends ListVector
{
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";
50 * Construct an empty MapVector with no data. Child vectors must be added subsequently.
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.
57 public static MapVector
empty(String name
, BufferAllocator allocator
, boolean keysSorted
) {
58 return new MapVector(name
, allocator
, FieldType
.nullable(new Map(keysSorted
)), null);
62 * Construct a MapVector instance.
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.
69 public MapVector(String name
, BufferAllocator allocator
, FieldType fieldType
, CallBack callBack
) {
70 super(name
, allocator
, fieldType
, callBack
);
71 defaultDataVectorName
= DATA_VECTOR_NAME
;
75 * Initialize child vectors of the map from the given list of fields.
77 * @param children List of fields that will be children of this MapVector.
80 public void initializeChildrenFromFields(List
<Field
> children
) {
81 checkArgument(children
.size() == 1, "Maps have one List child. Found: %s", children
);
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
);
90 Field keyField
= structField
.getChildren().get(0);
91 checkArgument(!keyField
.isNullable(), "Map data key type should be a non-nullable");
93 AddOrGetResult
<FieldVector
> addOrGetVector
= addOrGetVector(structField
.getFieldType());
94 checkArgument(addOrGetVector
.isCreated(), "Child vector already existed: %s", addOrGetVector
.getVector());
96 addOrGetVector
.getVector().initializeChildrenFromFields(structField
.getChildren());
100 * Get the writer for this MapVector instance.
103 public UnionMapWriter
getWriter() {
104 return new UnionMapWriter(this);
108 * Get the reader for this MapVector instance.
111 public UnionMapReader
getReader() {
112 if (reader
== null) {
113 reader
= new UnionMapReader(this);
115 return (UnionMapReader
) reader
;
119 public MinorType
getMinorType() {
120 return MinorType
.MAP
;