Skip to content

Commit

Permalink
HBASE-28835 Make connector support for Decimal type (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyyyyyyy authored Oct 3, 2024
1 parent 8c9de32 commit abf69b1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object Utils {
val newArray = new Array[Byte](length)
System.arraycopy(src, offset, newArray, 0, length)
newArray
case _: DecimalType => Bytes.toBigDecimal(src, offset, length)
// TODO: SparkSqlSerializer.deserialize[Any](src)
case _ => throw new Exception(s"unsupported data type ${f.dt}")
}
Expand All @@ -79,6 +80,7 @@ object Utils {
case DateType | TimestampType => Bytes.toBytes(input.asInstanceOf[java.util.Date].getTime)
case StringType => Bytes.toBytes(input.toString)
case BinaryType => input.asInstanceOf[Array[Byte]]
case _: DecimalType => Bytes.toBytes(input.asInstanceOf[java.math.BigDecimal])
case _ => throw new Exception(s"unsupported data type ${field.dt}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class DefaultSourceSuite
val columnFamily = "c"

val timestamp = 1234567890000L
val decimal = new java.math.BigDecimal("1234.56")

var sqlContext: SQLContext = null
var df: DataFrame = null
Expand Down Expand Up @@ -235,6 +236,7 @@ class DefaultSourceSuite
Bytes.toBytes("timestamp"),
Bytes.toBytes(timestamp))
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("string"), Bytes.toBytes("string"))
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("decimal"), Bytes.toBytes(decimal))
t3Table.put(put)
} finally {
t3Table.close()
Expand Down Expand Up @@ -990,7 +992,8 @@ class DefaultSourceSuite
|"DOUBLE_FIELD":{"cf":"c", "col":"double", "type":"double"},
|"DATE_FIELD":{"cf":"c", "col":"date", "type":"date"},
|"TIMESTAMP_FIELD":{"cf":"c", "col":"timestamp", "type":"timestamp"},
|"STRING_FIELD":{"cf":"c", "col":"string", "type":"string"}
|"STRING_FIELD":{"cf":"c", "col":"string", "type":"string"},
|"DECIMAL_FIELD":{"cf":"c", "col":"decimal", "type":"decimal(10,2)"}
|}
|}""".stripMargin
df = sqlContext.load(
Expand All @@ -1004,7 +1007,7 @@ class DefaultSourceSuite
"SELECT binary_field, boolean_field, " +
"byte_field, short_field, int_field, long_field, " +
"float_field, double_field, date_field, timestamp_field, " +
"string_field FROM hbaseTestMapping")
"string_field, decimal_field FROM hbaseTestMapping")
.collect()

assert(results.length == 1)
Expand All @@ -1030,6 +1033,7 @@ class DefaultSourceSuite
assert(Math.abs(result.get(8).asInstanceOf[Date].getTime - timestamp) <= 86400000)
assert(result.get(9).asInstanceOf[Timestamp].getTime == timestamp)
assert(result.get(10) == "string")
assert(result.get(11) == decimal)
}

def writeCatalog = s"""{
Expand Down

0 comments on commit abf69b1

Please sign in to comment.