Skip to content

Commit

Permalink
mv
Browse files Browse the repository at this point in the history
  • Loading branch information
xitongsys committed Dec 20, 2017
1 parent 226faf0 commit b646f9c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 71 deletions.
62 changes: 0 additions & 62 deletions Common/Common.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package Common

import (
"fmt"
"github.com/xitongsys/parquet-go/ParquetType"
"github.com/xitongsys/parquet-go/parquet"
"math/big"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -462,63 +460,3 @@ func PathToStr(path []string) string {
func StrToPath(str string) []string {
return strings.Split(str, ".")
}

//order=LittleEndian or BigEndian; length is byte num
func StrIntToBinary(num string, order string, length int32, signed bool) string {
bignum := new(big.Int)
bignum.SetString(num, 10)
binStr := fmt.Sprintf("%b", bignum)
flag := 1
if binStr[:1] == "-" {
flag = -1
binStr = binStr[1:]
}
for len(binStr)%8 != 0 {
binStr = "0" + binStr
}

for int32(len(binStr)/8) < length {
binStr = "00000000" + binStr
}

ln := len(binStr)
if flag < 0 {
tmp := "1"
for i := 0; i < ln; i++ {
tmp = tmp + "0"
}
bn1, bn2, bn3 := new(big.Int), new(big.Int), new(big.Int)
bn1.SetString(tmp, 2)
bn2.SetString(binStr, 2)
bn3.Sub(bn1, bn2)
binStr = fmt.Sprintf("%b", bn3)
}

numBytes := []string{}
for i := 8; i < len(binStr); i += 8 {
numBytes = append(numBytes, binStr[i:i+8])
}
resBytes := []byte{}
for i := 0; i < len(numBytes); i++ {
s := numBytes[i]
var b byte
for j := 0; j < 8; j++ {
if s[j] == 1 {
b = b | 1<<uint32(7-j)
}
}
resBytes = append(resBytes, b)
}
if order == "LittleEndian" {
i, j := 0, len(resBytes)-1
for i < j {
tmp := resBytes[i]
resBytes[i] = resBytes[j]
resBytes[j] = tmp
i++
j--
}
}

return string(resBytes)
}
79 changes: 72 additions & 7 deletions ParquetType/ParquetType.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/xitongsys/parquet-go/parquet"
"log"
"math/big"
)

//base type
Expand Down Expand Up @@ -196,14 +197,18 @@ func StrToParquetType(s string, typeName string) interface{} {
var v DOUBLE
fmt.Sscanf(s, "%f", &v)
return v
} else if typeName == "BYTE_ARRAY" || typeName == "UTF8" || typeName == "INTERVAL" || typeName == "DECIMAL" {
var v BYTE_ARRAY
v = BYTE_ARRAY(s)
return v
} else if typeName == "BYTE_ARRAY" || typeName == "UTF8" {
return BYTE_ARRAY(s)

} else if typeName == "FIXED_LEN_BYTE_ARRAY" {
var v FIXED_LEN_BYTE_ARRAY
v = FIXED_LEN_BYTE_ARRAY(s)
return v
return FIXED_LEN_BYTE_ARRAY(s)

} else if typeName == "INTERVAL" {
return FIXED_LEN_BYTE_ARRAY(StrIntToBinary(s, "LittleEndian", 12, false))

} else if typeName == "DECIMAL" {
return BYTE_ARRAY(StrIntToBinary(s, "BigEndian", 0, true))

} else {
log.Printf("Type Error: %v ", typeName)
return nil
Expand Down Expand Up @@ -276,3 +281,63 @@ func GoTypeToParquetType(src interface{}, pT *parquet.Type, cT *parquet.Converte
return nil
}
}

//order=LittleEndian or BigEndian; length is byte num
func StrIntToBinary(num string, order string, length int32, signed bool) string {
bignum := new(big.Int)
bignum.SetString(num, 10)
binStr := fmt.Sprintf("%b", bignum)
flag := 1
if binStr[:1] == "-" {
flag = -1
binStr = binStr[1:]
}
for len(binStr)%8 != 0 {
binStr = "0" + binStr
}

for int32(len(binStr)/8) < length {
binStr = "00000000" + binStr
}

ln := len(binStr)
if flag < 0 {
tmp := "1"
for i := 0; i < ln; i++ {
tmp = tmp + "0"
}
bn1, bn2, bn3 := new(big.Int), new(big.Int), new(big.Int)
bn1.SetString(tmp, 2)
bn2.SetString(binStr, 2)
bn3.Sub(bn1, bn2)
binStr = fmt.Sprintf("%b", bn3)
}

numBytes := []string{}
for i := 8; i < len(binStr); i += 8 {
numBytes = append(numBytes, binStr[i:i+8])
}
resBytes := []byte{}
for i := 0; i < len(numBytes); i++ {
s := numBytes[i]
var b byte
for j := 0; j < 8; j++ {
if s[j] == 1 {
b = b | 1<<uint32(7-j)
}
}
resBytes = append(resBytes, b)
}
if order == "LittleEndian" {
i, j := 0, len(resBytes)-1
for i < j {
tmp := resBytes[i]
resBytes[i] = resBytes[j]
resBytes[j] = tmp
i++
j--
}
}

return string(resBytes)
}
4 changes: 2 additions & 2 deletions example/type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/xitongsys/parquet-go/Common"
"github.com/xitongsys/parquet-go/ParquetFile"
"github.com/xitongsys/parquet-go/ParquetReader"
"github.com/xitongsys/parquet-go/ParquetType"
"github.com/xitongsys/parquet-go/ParquetWriter"
"log"
)
Expand Down Expand Up @@ -67,7 +67,7 @@ func main() {
TimestampMillis: int64(i),
TimestampMicros: int64(i),
Interval: "012345678912",
Decimal: Common.StrIntToBinary("12345", "BigEndian", 0, true),
Decimal: ParquetType.StrIntToBinary("12345", "BigEndian", 0, true),
}
pw.Write(tp)
}
Expand Down

0 comments on commit b646f9c

Please sign in to comment.