Skip to content

Commit 4b65ada

Browse files
committed
Add spec version to VariantLogicalType
1 parent 3450f1c commit 4b65ada

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

cpp/src/parquet/schema_test.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,8 @@ TEST(TestLogicalTypeOperation, LogicalTypeRepresentation) {
15801580
LogicalType::EdgeInterpolationAlgorithm::KARNEY),
15811581
"Geography(crs=srid:1234, algorithm=karney)",
15821582
R"({"Type": "Geography", "crs": "srid:1234", "algorithm": "karney"})"},
1583-
{LogicalType::Variant(), "Variant", R"({"Type": "Variant"})"},
1583+
{LogicalType::Variant(), "Variant(1)", R"({"Type": "Variant", "SpecVersion": 1})"},
1584+
{LogicalType::Variant(2), "Variant(2)", R"({"Type": "Variant", "SpecVersion": 2})"},
15841585
{LogicalType::None(), "None", R"({"Type": "None"})"},
15851586
};
15861587

@@ -2357,12 +2358,14 @@ TEST(TestLogicalTypeSerialization, Roundtrips) {
23572358
}
23582359

23592360
TEST(TestLogicalTypeSerialization, VariantSpecificationVersion) {
2360-
// Confirm that Variant logical type sets specification_version to 1 in thrift
2361-
// serialization
2361+
// Confirm that Variant logical type sets specification_version to expected value in
2362+
// thrift serialization
2363+
constexpr int8_t specVersion = 2;
23622364
auto metadata = PrimitiveNode::Make("metadata", Repetition::REQUIRED, Type::BYTE_ARRAY);
23632365
auto value = PrimitiveNode::Make("value", Repetition::REQUIRED, Type::BYTE_ARRAY);
2364-
NodePtr variant_node = GroupNode::Make("variant", Repetition::REQUIRED,
2365-
{metadata, value}, LogicalType::Variant());
2366+
NodePtr variant_node =
2367+
GroupNode::Make("variant", Repetition::REQUIRED, {metadata, value},
2368+
LogicalType::Variant(specVersion));
23662369

23672370
std::vector<format::SchemaElement> elements;
23682371
ToParquet(reinterpret_cast<GroupNode*>(variant_node.get()), &elements);
@@ -2372,9 +2375,9 @@ TEST(TestLogicalTypeSerialization, VariantSpecificationVersion) {
23722375
ASSERT_TRUE(elements[0].__isset.logicalType);
23732376
ASSERT_TRUE(elements[0].logicalType.__isset.VARIANT);
23742377

2375-
// Verify that specification_version is set to 1
2378+
// Verify that specification_version is set properly
23762379
ASSERT_TRUE(elements[0].logicalType.VARIANT.__isset.specification_version);
2377-
ASSERT_EQ(elements[0].logicalType.VARIANT.specification_version, 1);
2380+
ASSERT_EQ(elements[0].logicalType.VARIANT.specification_version, specVersion);
23782381
}
23792382

23802383
} // namespace schema

cpp/src/parquet/types.cc

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,12 @@ std::shared_ptr<const LogicalType> LogicalType::FromThrift(
591591

592592
return GeographyLogicalType::Make(std::move(crs), algorithm);
593593
} else if (type.__isset.VARIANT) {
594-
return VariantLogicalType::Make();
594+
int8_t specVersion = VARIANT_SPEC_VERSION;
595+
if (type.VARIANT.__isset.specification_version) {
596+
specVersion = type.VARIANT.specification_version;
597+
}
598+
599+
return VariantLogicalType::Make(specVersion);
595600
} else {
596601
// Sentinel type for one we do not recognize
597602
return UndefinedLogicalType::Make();
@@ -659,8 +664,8 @@ std::shared_ptr<const LogicalType> LogicalType::Geography(
659664
return GeographyLogicalType::Make(std::move(crs), algorithm);
660665
}
661666

662-
std::shared_ptr<const LogicalType> LogicalType::Variant() {
663-
return VariantLogicalType::Make();
667+
std::shared_ptr<const LogicalType> LogicalType::Variant(int8_t specVersion) {
668+
return VariantLogicalType::Make(specVersion);
664669
}
665670

666671
std::shared_ptr<const LogicalType> LogicalType::None() { return NoLogicalType::Make(); }
@@ -1958,24 +1963,53 @@ class LogicalType::Impl::Variant final : public LogicalType::Impl::Incompatible,
19581963
public:
19591964
friend class VariantLogicalType;
19601965

1961-
OVERRIDE_TOSTRING(Variant)
1966+
std::string ToString() const override;
1967+
std::string ToJSON() const override;
1968+
format::LogicalType ToThrift() const override;
19621969

1963-
format::LogicalType ToThrift() const override {
1964-
format::LogicalType type;
1965-
format::VariantType variant_type;
1966-
variant_type.__set_specification_version(kSpecificationVersion);
1967-
type.__set_VARIANT(variant_type);
1968-
return type;
1969-
}
1970+
int8_t spec_version() const { return specVersion_; }
19701971

19711972
private:
1972-
Variant()
1973+
explicit Variant(const int8_t specVersion)
19731974
: LogicalType::Impl(LogicalType::Type::VARIANT, SortOrder::UNKNOWN),
1974-
LogicalType::Impl::Inapplicable() {}
1975-
static constexpr int8_t kSpecificationVersion = 1;
1975+
LogicalType::Impl::Inapplicable() {
1976+
this->specVersion_ = specVersion;
1977+
}
1978+
1979+
int8_t specVersion_;
19761980
};
19771981

1978-
GENERATE_MAKE(Variant)
1982+
int8_t VariantLogicalType::spec_version() const {
1983+
return (dynamic_cast<const LogicalType::Impl::Variant&>(*impl_)).spec_version();
1984+
}
1985+
1986+
std::string LogicalType::Impl::Variant::ToString() const {
1987+
std::stringstream type;
1988+
type << "Variant(" << static_cast<int>(specVersion_) << ")";
1989+
return type.str();
1990+
}
1991+
1992+
std::string LogicalType::Impl::Variant::ToJSON() const {
1993+
std::stringstream json;
1994+
json << R"({"Type": "Variant", "SpecVersion": )" << static_cast<int>(specVersion_)
1995+
<< "}";
1996+
1997+
return json.str();
1998+
}
1999+
2000+
format::LogicalType LogicalType::Impl::Variant::ToThrift() const {
2001+
format::LogicalType type;
2002+
format::VariantType variant_type;
2003+
variant_type.__set_specification_version(specVersion_);
2004+
type.__set_VARIANT(variant_type);
2005+
return type;
2006+
}
2007+
2008+
std::shared_ptr<const LogicalType> VariantLogicalType::Make(const int8_t specVersion) {
2009+
auto logical_type = std::shared_ptr<VariantLogicalType>(new VariantLogicalType());
2010+
logical_type->impl_.reset(new LogicalType::Impl::Variant(specVersion));
2011+
return logical_type;
2012+
}
19792013

19802014
class LogicalType::Impl::No final : public LogicalType::Impl::SimpleCompatible,
19812015
public LogicalType::Impl::UniversalApplicable {

cpp/src/parquet/types.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class PARQUET_EXPORT LogicalType {
178178
KARNEY = 5
179179
};
180180

181+
/// \brief The latest supported Variant specification version by this library
182+
static constexpr int8_t VARIANT_SPEC_VERSION = 1;
183+
181184
/// \brief If possible, return a logical type equivalent to the given legacy
182185
/// converted type (and decimal metadata if applicable).
183186
static std::shared_ptr<const LogicalType> FromConvertedType(
@@ -224,7 +227,8 @@ class PARQUET_EXPORT LogicalType {
224227
static std::shared_ptr<const LogicalType> BSON();
225228
static std::shared_ptr<const LogicalType> UUID();
226229
static std::shared_ptr<const LogicalType> Float16();
227-
static std::shared_ptr<const LogicalType> Variant();
230+
static std::shared_ptr<const LogicalType> Variant(
231+
int8_t specVersion = VARIANT_SPEC_VERSION);
228232

229233
static std::shared_ptr<const LogicalType> Geometry(std::string crs = "");
230234

@@ -495,7 +499,10 @@ class PARQUET_EXPORT GeographyLogicalType : public LogicalType {
495499
/// \brief Allowed for group nodes only.
496500
class PARQUET_EXPORT VariantLogicalType : public LogicalType {
497501
public:
498-
static std::shared_ptr<const LogicalType> Make();
502+
static std::shared_ptr<const LogicalType> Make(
503+
int8_t specVersion = VARIANT_SPEC_VERSION);
504+
505+
int8_t spec_version() const;
499506

500507
private:
501508
VariantLogicalType() = default;

0 commit comments

Comments
 (0)