]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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.util; | |
19 | ||
20 | import org.apache.arrow.vector.VectorSchemaRoot; | |
21 | import org.apache.arrow.vector.compare.TypeEqualsVisitor; | |
22 | ||
23 | /** | |
24 | * Utility to append {@link org.apache.arrow.vector.VectorSchemaRoot}s with the same schema. | |
25 | */ | |
26 | public class VectorSchemaRootAppender { | |
27 | ||
28 | /** | |
29 | * Appends a number of {@link VectorSchemaRoot}s. | |
30 | * @param checkSchema if we need to check schema for the vector schema roots. | |
31 | * @param targetRoot the vector schema root to be appended. | |
32 | * @param rootsToAppend the vector schema roots to append. | |
33 | * @throws IllegalArgumentException throws if we need to check schema, and checking schema fails. | |
34 | */ | |
35 | public static void append(boolean checkSchema, VectorSchemaRoot targetRoot, VectorSchemaRoot... rootsToAppend) { | |
36 | // create appenders | |
37 | VectorAppender[] appenders = new VectorAppender[targetRoot.getFieldVectors().size()]; | |
38 | for (int i = 0; i < appenders.length; i++) { | |
39 | appenders[i] = new VectorAppender(targetRoot.getVector(i)); | |
40 | } | |
41 | ||
42 | // create type checkers, if necessary | |
43 | TypeEqualsVisitor[] typeCheckers = null; | |
44 | if (checkSchema) { | |
45 | typeCheckers = new TypeEqualsVisitor[targetRoot.getFieldVectors().size()]; | |
46 | for (int i = 0; i < typeCheckers.length; i++) { | |
47 | typeCheckers[i] = new TypeEqualsVisitor(targetRoot.getVector(i), | |
48 | /* check name */ false, /* check meta data */ false); | |
49 | } | |
50 | } | |
51 | ||
52 | for (VectorSchemaRoot delta : rootsToAppend) { | |
53 | // check schema, if necessary | |
54 | if (checkSchema) { | |
55 | if (delta.getFieldVectors().size() != targetRoot.getFieldVectors().size()) { | |
56 | throw new IllegalArgumentException("Vector schema roots have different numbers of child vectors."); | |
57 | } | |
58 | for (int i = 0; i < typeCheckers.length; i++) { | |
59 | if (!typeCheckers[i].equals(delta.getVector(i))) { | |
60 | throw new IllegalArgumentException("Vector schema roots have different schemas."); | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | // append child vectors. | |
66 | for (int i = 0; i < appenders.length; i++) { | |
67 | delta.getVector(i).accept(appenders[i], null); | |
68 | } | |
69 | targetRoot.setRowCount(targetRoot.getRowCount() + delta.getRowCount()); | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * Appends a number of {@link VectorSchemaRoot}s. | |
75 | * This method performs schema checking before appending data. | |
76 | * @param targetRoot the vector schema root to be appended. | |
77 | * @param rootsToAppend the vector schema roots to append. | |
78 | * @throws IllegalArgumentException throws if we need to check schema, and checking schema fails. | |
79 | */ | |
80 | public static void append(VectorSchemaRoot targetRoot, VectorSchemaRoot... rootsToAppend) { | |
81 | append(true, targetRoot, rootsToAppend); | |
82 | } | |
83 | } |