WillAyd commented on code in PR #1288:
URL: https://github.com/apache/arrow-adbc/pull/1288#discussion_r1391650537


##########
c/driver/postgresql/postgres_copy_reader.h:
##########
@@ -1217,6 +1217,77 @@ class PostgresCopyIntervalFieldWriter : public 
PostgresCopyFieldWriter {
   }
 };
 
+
+// Inspiration for this taken from get_str_from_var in the pg source
+// src/backend/utils/adt/numeric.c
+class PostgresCopyNumericFieldWriter : public PostgresCopyFieldWriter {
+public:
+  ArrowErrorCode Write(ArrowBuffer* buffer, int64_t index, ArrowError* error) 
override {
+    struct ArrowDecimal decimal;
+    // TODO: these need to be inferred from the schema not hard coded
+    constexpr int16_t precision = 19;
+    constexpr int16_t scale = 8;
+    ArrowDecimalInit(&decimal, 128, precision, scale);
+    ArrowArrayViewGetDecimalUnsafe(array_view_, index, &decimal);
+    constexpr uint16_t kNumericPos = 0x0000;
+    constexpr uint16_t kNumericNeg = 0x4000;
+    constexpr int64_t kNBase = 10000;
+    // Number of decimal digits per Postgres digit
+    constexpr int kDecDigits = 4;
+
+    // TODO: need some kind of bounds check on this
+    int64_t decimal_int = ArrowDecimalGetIntUnsafe(&decimal);
+    // TODO: is -INT64_MIN possible? If so how do we handle?
+    if (decimal_int < 0) {
+      decimal_int = -decimal_int;
+    }
+    std::vector<int16_t> pg_digits;

Review Comment:
   Yea that's true and actually how postgres does it internally. 
   
   
https://github.com/postgres/postgres/blob/8680bae8463a0b213893ca6a1c5bb2c2530e823c/src/backend/utils/adt/numeric.c#L8026
   
   If we wanted to stack allocate I guess would just expand that out to 
whatever is required to store up to 4 decimal words?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to