Skip to content

Commit 634132e

Browse files
xhochywesm
authored andcommitted
PARQUET-621: Add flag to indicate if decimalmetadata is set
Author: Uwe L. Korn <uwelk@xhochy.com> Closes apache#109 from xhochy/parquet-621 and squashes the following commits: 3850a0f [Uwe L. Korn] Add unit test dc1552f [Uwe L. Korn] Add flag to indicate if decimalmetadata is set Change-Id: I7cb412aa8165af4cae1b1aca7360a01129cfe456
1 parent cd8906c commit 634132e

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

cpp/src/parquet/schema/schema-converter-test.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ class TestSchemaFlatten : public ::testing::Test {
158158
std::vector<format::SchemaElement> elements_;
159159
};
160160

161+
TEST_F(TestSchemaFlatten, DecimalMetadata) {
162+
// Checks that DecimalMetadata is only set for DecimalTypes
163+
NodePtr node = PrimitiveNode::Make(
164+
"decimal", Repetition::REQUIRED, Type::INT64, LogicalType::DECIMAL, -1, 8, 4);
165+
NodePtr group =
166+
GroupNode::Make("group", Repetition::REPEATED, {node}, LogicalType::LIST);
167+
Flatten(reinterpret_cast<GroupNode*>(group.get()));
168+
ASSERT_EQ("decimal", elements_[1].name);
169+
ASSERT_TRUE(elements_[1].__isset.precision);
170+
ASSERT_TRUE(elements_[1].__isset.scale);
171+
172+
elements_.clear();
173+
// Not for integers with no logical type
174+
group =
175+
GroupNode::Make("group", Repetition::REPEATED, {Int64("int64")}, LogicalType::LIST);
176+
Flatten(reinterpret_cast<GroupNode*>(group.get()));
177+
ASSERT_EQ("int64", elements_[1].name);
178+
ASSERT_FALSE(elements_[0].__isset.precision);
179+
ASSERT_FALSE(elements_[0].__isset.scale);
180+
}
181+
161182
TEST_F(TestSchemaFlatten, NestedExample) {
162183
SchemaElement elt;
163184
std::vector<SchemaElement> elements;

cpp/src/parquet/schema/types.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ PrimitiveNode::PrimitiveNode(const std::string& name, Repetition::type repetitio
8686
physical_type_(type),
8787
type_length_(length) {
8888
std::stringstream ss;
89+
decimal_metadata_.isset = false;
8990
// Check if the physical and logical types match
9091
// Mapping referred from Apache parquet-mr as on 2016-02-22
9192
switch (logical_type) {
9293
case LogicalType::NONE:
9394
// Logical type not set
9495
// Clients should be able to read these values
96+
decimal_metadata_.isset = true;
9597
decimal_metadata_.precision = precision;
9698
decimal_metadata_.scale = scale;
9799
break;
@@ -123,6 +125,7 @@ PrimitiveNode::PrimitiveNode(const std::string& name, Repetition::type repetitio
123125
ss << " cannot be greater than precision " << precision;
124126
throw ParquetException(ss.str());
125127
}
128+
decimal_metadata_.isset = true;
126129
decimal_metadata_.precision = precision;
127130
decimal_metadata_.scale = scale;
128131
break;
@@ -299,8 +302,10 @@ void PrimitiveNode::ToParquet(void* opaque_element) const {
299302
element->__set_type(ToThrift(physical_type_));
300303
// FIXME: SchemaFlattener does this for us: element->__set_field_id(id_);
301304
element->__set_type_length(type_length_);
302-
element->__set_precision(decimal_metadata_.precision);
303-
element->__set_scale(decimal_metadata_.scale);
305+
if (decimal_metadata_.isset) {
306+
element->__set_precision(decimal_metadata_.precision);
307+
element->__set_scale(decimal_metadata_.scale);
308+
}
304309
}
305310

306311
} // namespace schema

cpp/src/parquet/schema/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct ListEncoding {
7070
};
7171

7272
struct DecimalMetadata {
73+
bool isset;
7374
int32_t scale;
7475
int32_t precision;
7576
};

0 commit comments

Comments
 (0)