Skip to content

Commit 2f5ec37

Browse files
liyafan82kou
authored andcommitted
ARROW-6247: [Java] Provide a common interface for float4 and float8 vectors
We want to provide an interface for floating point vectors (float4 & float8). This interface will make it convenient for many operations on a vector. With this interface, the client code will be greatly simplified, with many branches/switch removed. The design is similar to BaseIntVector (the interface for all integer vectors). We provide 3 methods for setting & getting floating point values: setWithPossibleTruncate setSafeWithPossibleTruncate getValueAsDouble Closes apache#5132 from liyafan82/fly_0820_float and squashes the following commits: 984d538 <liyafan82> Provide a common interface for float4 and float8 vectors Authored-by: liyafan82 <fan_li_ya@foxmail.com> Signed-off-by: Micah Kornfield <emkornfield@gmail.com>
1 parent 4d19168 commit 2f5ec37

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

vector/src/main/java/org/apache/arrow/vector/Float4Vector.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* float values which could be null. A validity buffer (bit vector) is
3737
* maintained to track which elements in the vector are null.
3838
*/
39-
public class Float4Vector extends BaseFixedWidthVector {
39+
public class Float4Vector extends BaseFixedWidthVector implements FloatingPointVector {
4040
public static final byte TYPE_WIDTH = 4;
4141
private final FieldReader reader;
4242

@@ -282,6 +282,20 @@ public static float get(final ArrowBuf buffer, final int index) {
282282
return buffer.getFloat(index * TYPE_WIDTH);
283283
}
284284

285+
@Override
286+
public void setWithPossibleTruncate(int index, double value) {
287+
set(index, (float) value);
288+
}
289+
290+
@Override
291+
public void setSafeWithPossibleTruncate(int index, double value) {
292+
setSafe(index, (float) value);
293+
}
294+
295+
@Override
296+
public double getValueAsDouble(int index) {
297+
return get(index);
298+
}
285299

286300
/*----------------------------------------------------------------*
287301
| |

vector/src/main/java/org/apache/arrow/vector/Float8Vector.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* double values which could be null. A validity buffer (bit vector) is
3737
* maintained to track which elements in the vector are null.
3838
*/
39-
public class Float8Vector extends BaseFixedWidthVector {
39+
public class Float8Vector extends BaseFixedWidthVector implements FloatingPointVector {
4040
public static final byte TYPE_WIDTH = 8;
4141
private final FieldReader reader;
4242

@@ -283,6 +283,20 @@ public static double get(final ArrowBuf buffer, final int index) {
283283
return buffer.getDouble(index * TYPE_WIDTH);
284284
}
285285

286+
@Override
287+
public void setWithPossibleTruncate(int index, double value) {
288+
set(index, value);
289+
}
290+
291+
@Override
292+
public void setSafeWithPossibleTruncate(int index, double value) {
293+
setSafe(index, value);
294+
}
295+
296+
@Override
297+
public double getValueAsDouble(int index) {
298+
return get(index);
299+
}
286300

287301
/*----------------------------------------------------------------*
288302
| |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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;
19+
20+
/**
21+
* The interface for vectors with floating point values.
22+
*/
23+
public interface FloatingPointVector {
24+
25+
/**
26+
* Sets the value at the given index, note this value may be truncated internally.
27+
* @param index the index to set.
28+
* @param value the value to set.
29+
*/
30+
void setWithPossibleTruncate(int index, double value);
31+
32+
/**
33+
* Sets the value at the given index, note this value may be truncated internally.
34+
* Any expansion/reallocation is handled automatically.
35+
* @param index the index to set.
36+
* @param value the value to set.
37+
*/
38+
void setSafeWithPossibleTruncate(int index, double value);
39+
40+
/**
41+
* Gets the value at the given index.
42+
* @param index the index to retrieve the value.
43+
* @return the value at the index.
44+
*/
45+
double getValueAsDouble(int index);
46+
}

0 commit comments

Comments
 (0)