Skip to content

Commit 39fe42a

Browse files
authored
Add EnableStoreDecimalAsInteger to WriterProperties (#572)
1 parent 43576f5 commit 39fe42a

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

cpp/WriterProperties.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,9 @@ extern "C"
162162
{
163163
TRYCATCH(*memory_pool = (*writer_properties)->memory_pool();)
164164
}
165+
166+
PARQUETSHARP_EXPORT ExceptionInfo* WriterProperties_Store_Decimal_As_Integer(const std::shared_ptr<WriterProperties>* writer_properties, bool* store_decimal_as_integer)
167+
{
168+
TRYCATCH(*store_decimal_as_integer = (*writer_properties)->store_decimal_as_integer();)
169+
}
165170
}

cpp/WriterPropertiesBuilder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,14 @@ extern "C"
231231
{
232232
TRYCATCH(builder->memory_pool(pool);)
233233
}
234+
235+
PARQUETSHARP_EXPORT ExceptionInfo* WriterPropertiesBuilder_Enable_Store_Decimal_As_Integer(WriterProperties::Builder* builder)
236+
{
237+
TRYCATCH(builder->enable_store_decimal_as_integer();)
238+
}
239+
240+
PARQUETSHARP_EXPORT ExceptionInfo* WriterPropertiesBuilder_Disable_Store_Decimal_As_Integer(WriterProperties::Builder* builder)
241+
{
242+
TRYCATCH(builder->disable_store_decimal_as_integer();)
243+
}
234244
}

csharp.test/TestWriterProperties.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void TestDefaultProperties()
2727
Assert.True(p.WritePageIndex);
2828
Assert.False(p.PageChecksumEnabled);
2929
Assert.That(p.MemoryPool.BackendName, Is.Not.Empty);
30+
Assert.False(p.StoreDecimalAsInteger);
3031
}
3132

3233
[Test]
@@ -46,6 +47,7 @@ public static void TestPropertiesBuilder()
4647
.DisableWritePageIndex()
4748
.EnablePageChecksum()
4849
.MemoryPool(MemoryPool.SystemMemoryPool())
50+
.EnableStoreDecimalAsInteger()
4951
.Build();
5052

5153
Assert.AreEqual("Meeeee!!!", p.CreatedBy);
@@ -61,6 +63,7 @@ public static void TestPropertiesBuilder()
6163
Assert.False(p.WritePageIndex);
6264
Assert.True(p.PageChecksumEnabled);
6365
Assert.AreEqual("system", p.MemoryPool.BackendName);
66+
Assert.True(p.StoreDecimalAsInteger);
6467
}
6568

6669
[Test]

csharp/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ ParquetSharp.ReaderProperties.ThriftContainerSizeLimit.get -> int
77
ParquetSharp.ReaderProperties.SetThriftContainerSizeLimit(int size) -> void
88
ParquetSharp.ReaderProperties.FooterReadSize.get -> long
99
ParquetSharp.ReaderProperties.SetFooterReadSize(long size) -> void
10+
ParquetSharp.WriterProperties.StoreDecimalAsInteger.get -> bool
11+
ParquetSharp.WriterPropertiesBuilder.EnableStoreDecimalAsInteger() -> ParquetSharp.WriterPropertiesBuilder!
12+
ParquetSharp.WriterPropertiesBuilder.DisableStoreDecimalAsInteger() -> ParquetSharp.WriterPropertiesBuilder!

csharp/WriterProperties.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ public SortingColumn[] SortingColumns()
264264
/// </summary>
265265
public MemoryPool MemoryPool => new MemoryPool(ExceptionInfo.Return<IntPtr>(Handle, WriterProperties_Memory_Pool));
266266

267+
/// <summary>
268+
/// Whether decimals with precision between 1 and 18 (inclusive) are stored as integers.
269+
/// </summary>
270+
public bool StoreDecimalAsInteger => ExceptionInfo.Return<bool>(Handle, WriterProperties_Store_Decimal_As_Integer);
271+
267272
internal readonly ParquetHandle Handle;
268273

269274
[DllImport(ParquetDll.Name)]
@@ -340,5 +345,8 @@ public SortingColumn[] SortingColumns()
340345

341346
[DllImport(ParquetDll.Name)]
342347
private static extern IntPtr WriterProperties_Memory_Pool(IntPtr writerProperties, out IntPtr memoryPool);
348+
349+
[DllImport(ParquetDll.Name)]
350+
private static extern IntPtr WriterProperties_Store_Decimal_As_Integer(IntPtr writerProperties, out bool storeDecimalAsInteger);
343351
}
344352
}

csharp/WriterPropertiesBuilder.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,37 @@ private void ApplyDefaults()
618618
});
619619
}
620620

621+
/// <summary>
622+
/// Allow decimals with precision between 1 and 18 (inclusive) to be stored as integers.
623+
/// By default, this is DISABLED and all decimal types annotate fixed_len_byte_array.
624+
///
625+
/// When enabled, the Parquet writer will use following physical types to store decimals:
626+
/// - int32: for precision between 1 and 9.
627+
/// - int64: for precision between 10 and 18.
628+
/// - fixed_len_byte_array: for precision > 18.
629+
///
630+
/// As a consequence, decimal columns stored in integer types are more compact.
631+
/// </summary>
632+
public WriterPropertiesBuilder EnableStoreDecimalAsInteger()
633+
{
634+
ExceptionInfo.Check(WriterPropertiesBuilder_Enable_Store_Decimal_As_Integer(_handle.IntPtr));
635+
GC.KeepAlive(_handle);
636+
return this;
637+
}
638+
639+
/// <summary>
640+
/// Disable decimal logical type with precision between 1 and 18 (inclusive) to be stored
641+
/// as integer physical type.
642+
///
643+
/// This is the default.
644+
/// </summary>
645+
public WriterPropertiesBuilder DisableStoreDecimalAsInteger()
646+
{
647+
ExceptionInfo.Check(WriterPropertiesBuilder_Disable_Store_Decimal_As_Integer(_handle.IntPtr));
648+
GC.KeepAlive(_handle);
649+
return this;
650+
}
651+
621652
[MethodImpl(MethodImplOptions.AggressiveInlining)]
622653
private static void OnDefaultProperty<T>(T? defaultPropertyValue, Action<T> setProperty)
623654
where T : struct
@@ -766,6 +797,12 @@ private static void OnDefaultRefProperty<T>(T? defaultPropertyValue, Action<T> s
766797
[DllImport(ParquetDll.Name)]
767798
private static extern IntPtr WriterPropertiesBuilder_Memory_Pool(IntPtr builder, IntPtr memoryPool);
768799

800+
[DllImport(ParquetDll.Name)]
801+
private static extern IntPtr WriterPropertiesBuilder_Enable_Store_Decimal_As_Integer(IntPtr builder);
802+
803+
[DllImport(ParquetDll.Name)]
804+
private static extern IntPtr WriterPropertiesBuilder_Disable_Store_Decimal_As_Integer(IntPtr builder);
805+
769806
private readonly ParquetHandle _handle;
770807
}
771808
}

0 commit comments

Comments
 (0)