GitHub user rtreffer opened a pull request:
https://github.com/apache/spark/pull/6796
[SPARK-4176][WIP] Support decimal types with precision > 18 in parquet
This is my current WIP on SPARK-4176. It should be compatible with other
implementations of parquet.
https://github.com/Parquet/parquet-format/blob/master/LogicalTypes.md#decimal
```
For byte arrays, binary and fixed, the unscaled number must be encoded as
two's complement using big-endian byte order
(the most significant byte is the zeroth element)
```
This is the default encoding on bigint. It should thus be compatible with
other implementations, although it would be great if s.o. could test this.
I've tested this locally with powers of 2 up to 2^200 in the spark shell,
without errors but
Code I've used for (local) testing (on spark shell):
```scala
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row
import java.io.File
def decimalList(n : Int) = {
require(n > 0)
val one = Decimal(1)
val two = Decimal(2)
one.changePrecision(n,0)
two.changePrecision(n,0)
val list = scala.collection.mutable.ArrayBuffer(one)
while (list.length < n) {
val v = list(list.length - 1)
list += Decimal(v.toJavaBigDecimal.multiply(two.toJavaBigDecimal),n,0)
}
list.toList
}
def decimalRows(l : List[Decimal]) = l.zipWithIndex.map(e => Row(e._2,e._1))
def decimalRowRdd(l : List[Decimal]) = sc.parallelize(decimalRows(l))
def df(n : Int) = {
val data = decimalList(n)
val len = data.lastOption.get.toString.length
val schema = StructType(Array(
StructField("id", IntegerType, true),
StructField("value", DecimalType(len,0), true)))
sqlContext.createDataFrame(decimalRowRdd(data), schema)
}
def delete(filename : String) : Unit = {
val f = new File(filename)
if (!f.exists) return
if (f.isDirectory) f.listFiles.foreach(_.delete)
f.delete
}
def test(n : Int) = {
val src = df(n)
delete("/tmp/typetest")
src.save("/tmp/typetest")
val copy = sqlContext.load("/tmp/typetest")
src.collect().sortBy(_.getInt(0)).zip(copy.collect().sortBy(_.getInt(0))).foreach(e
=> {
if (e._1 != e._2) println(s"${e._1} != ${e._2}")
})
delete("/tmp/typetest")
println(s"Tested 1..2^${n - 1}")
}
```
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/rtreffer/spark
spark-4176-store-large-decimal-in-parquet
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/6796.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #6796
----
commit 32630dfa440f618021d5d0a1db86cb7a3ea5e2f4
Author: Rene Treffer <[email protected]>
Date: 2015-06-13T12:54:42Z
[SPARK-4176] Support decimal types with precision > 18
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]