Skip to content

Commit 7bc802d

Browse files
committed
use defaultvariantvalue
1 parent 29e1568 commit 7bc802d

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (2023) The Delta Lake Project Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.delta.kernel.defaults.internal.data.value;
17+
18+
import java.util.Arrays;
19+
20+
import io.delta.kernel.data.VariantValue;
21+
22+
/**
23+
* Default implementation of a Delta kernel VariantValue.
24+
*/
25+
public class DefaultVariantValue implements VariantValue {
26+
private final byte[] value;
27+
private final byte[] metadata;
28+
29+
public DefaultVariantValue(byte[] value, byte[] metadata) {
30+
this.value = value;
31+
this.metadata = metadata;
32+
}
33+
34+
@Override
35+
public byte[] getValue() {
36+
return value;
37+
}
38+
39+
@Override
40+
public byte[] getMetadata() {
41+
return metadata;
42+
}
43+
44+
public String debugString() {
45+
return "VariantValue{value=" + Arrays.toString(value) +
46+
", metadata=" + Arrays.toString(metadata) + '}';
47+
}
48+
/**
49+
* Compare two variants in bytes. The variant equality is more complex than it, and we haven't
50+
* supported it in the user surface yet. This method is only intended for tests.
51+
*/
52+
@Override
53+
public boolean equals(Object other) {
54+
if (other instanceof DefaultVariantValue) {
55+
return Arrays.equals(value, ((DefaultVariantValue) other).getValue()) &&
56+
Arrays.equals(metadata, ((DefaultVariantValue) other).getMetadata());
57+
} else {
58+
return false;
59+
}
60+
}
61+
}

kernel/kernel-defaults/src/main/java/io/delta/kernel/defaults/internal/data/vector/DefaultVariantVector.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.delta.kernel.types.DataType;
2424

2525
import static io.delta.kernel.internal.util.Preconditions.checkArgument;
26+
import io.delta.kernel.defaults.internal.data.value.DefaultVariantValue;
2627

2728
/**
2829
* {@link io.delta.kernel.data.ColumnVector} implementation for variant type data.
@@ -68,20 +69,22 @@ public VariantValue getVariant(int rowId) {
6869
if (isNullAt(rowId)) {
6970
return null;
7071
}
71-
return new VariantValue() {
72-
private final byte[] value = valueVector.getBinary(rowId);
73-
private final byte[] metadata = metadataVector.getBinary(rowId);
72+
// return new VariantValue() {
73+
// private final byte[] value = valueVector.getBinary(rowId);
74+
// private final byte[] metadata = metadataVector.getBinary(rowId);
7475

75-
@Override
76-
public byte[] getValue() {
77-
return value;
78-
}
76+
// @Override
77+
// public byte[] getValue() {
78+
// return value;
79+
// }
7980

80-
@Override
81-
public byte[] getMetadata() {
82-
return metadata;
83-
}
84-
};
81+
// @Override
82+
// public byte[] getMetadata() {
83+
// return metadata;
84+
// }
85+
// };
86+
return new DefaultVariantValue(
87+
valueVector.getBinary(rowId), metadataVector.getBinary(rowId));
8588
}
8689

8790
/**

kernel/kernel-defaults/src/test/scala/io/delta/kernel/defaults/utils/TestRow.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.apache.spark.sql.{types => sparktypes}
2020
import org.apache.spark.sql.{Row => SparkRow}
2121
import org.apache.spark.unsafe.types.VariantVal
2222
import io.delta.kernel.data.{ArrayValue, ColumnVector, MapValue, Row}
23+
import io.delta.kernel.defaults.internal.data.value.DefaultVariantValue
2324
import io.delta.kernel.types._
2425

2526
import java.time.LocalDate
@@ -104,9 +105,7 @@ object TestRow {
104105
case _: ArrayType => arrayValueToScalaSeq(row.getArray(i))
105106
case _: MapType => mapValueToScalaMap(row.getMap(i))
106107
case _: StructType => TestRow(row.getStruct(i))
107-
case _: VariantType =>
108-
val kernelVariant = row.getVariant(i)
109-
new VariantVal(kernelVariant.getValue(), kernelVariant.getMetadata())
108+
case _: VariantType => row.getVariant(i)
110109
case _ => throw new UnsupportedOperationException("unrecognized data type")
111110
}
112111
}.toSeq)
@@ -168,7 +167,9 @@ object TestRow {
168167
decodeCellValue(mapType.keyType, k) -> decodeCellValue(mapType.valueType, v)
169168
}
170169
case _: sparktypes.StructType => TestRow(row.getStruct(i))
171-
case _: sparktypes.VariantType => row.getAs[VariantVal](i)
170+
case _: sparktypes.VariantType =>
171+
val sparkVariant = row.getAs[VariantVal](i)
172+
new DefaultVariantValue(sparkVariant.getValue(), sparkVariant.getMetadata())
172173
case _ => throw new UnsupportedOperationException("unrecognized data type")
173174
}
174175
})

kernel/kernel-defaults/src/test/scala/io/delta/kernel/defaults/utils/TestUtils.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import org.apache.hadoop.shaded.org.apache.commons.io.FileUtils
4141
import org.apache.spark.sql.SparkSession
4242
import org.apache.spark.sql.{types => sparktypes}
4343
import org.apache.spark.sql.catalyst.plans.SQLHelper
44-
import org.apache.spark.unsafe.types.VariantVal
4544
import org.scalatest.Assertions
4645

4746
trait TestUtils extends Assertions with SQLHelper {
@@ -415,7 +414,6 @@ trait TestUtils extends Assertions with SQLHelper {
415414
java.lang.Double.doubleToRawLongBits(a) == java.lang.Double.doubleToRawLongBits(b)
416415
case (a: Float, b: Float) =>
417416
java.lang.Float.floatToRawIntBits(a) == java.lang.Float.floatToRawIntBits(b)
418-
case (a: VariantVal, b: VariantVal) => a.debugString() == b.debugString()
419417
case (a, b) =>
420418
if (!a.equals(b)) {
421419
val sds = 200;

0 commit comments

Comments
 (0)