-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,571 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
# Data taken from https://www.nyc.gov/site/tlc/about/tlc-trip-record-data.page | ||
|
||
import io | ||
|
||
import pandas as pd | ||
import requests | ||
import io | ||
|
||
response = requests.get('https://d37ci6vzurychx.cloudfront.net/trip-data/green_tripdata_2023-01.parquet') | ||
pd.read_parquet(io.BytesIO(response.content)).to_json('green_tripdata_2023-01.jsonl', orient='records', lines=True) | ||
|
||
pd.read_parquet(io.BytesIO(response.content)).to_json('green_tripdata_2023-01.jsonl', orient='records', lines=True) | ||
|
||
df = pd.read_parquet(io.BytesIO(response.content)) | ||
df.to_csv('green_tripdata_2023-01.csv', index=False) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package appendable | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
"github.com/kevmo314/appendable/pkg/protocol" | ||
) | ||
|
||
type CSVHandler struct { | ||
io.ReadSeeker | ||
} | ||
|
||
func (c CSVHandler) Synchronize(f *IndexFile) error { | ||
var headers []string | ||
var err error | ||
|
||
fromNewIndexFile := false | ||
|
||
isHeader := false | ||
|
||
if len(f.Indexes) == 0 { | ||
isHeader = true | ||
fromNewIndexFile = true | ||
} else { | ||
for _, index := range f.Indexes { | ||
headers = append(headers, index.FieldName) | ||
} | ||
} | ||
|
||
scanner := bufio.NewScanner(f.data) | ||
|
||
for i := 0; scanner.Scan(); i++ { | ||
line := scanner.Bytes() | ||
|
||
existingCount := len(f.EndByteOffsets) | ||
|
||
// append a data range | ||
var start uint64 | ||
if len(f.EndByteOffsets) > 0 { | ||
start = f.EndByteOffsets[existingCount-1] | ||
} | ||
|
||
f.EndByteOffsets = append(f.EndByteOffsets, start+uint64(len(line))+1) | ||
|
||
f.Checksums = append(f.Checksums, xxhash.Sum64(line)) | ||
|
||
if isHeader { | ||
dec := csv.NewReader(bytes.NewReader(line)) | ||
headers, err = dec.Read() | ||
if err != nil { | ||
return fmt.Errorf("failed to parse CSV header: %w", err) | ||
} | ||
isHeader = false | ||
continue | ||
} | ||
|
||
dec := csv.NewReader(bytes.NewReader(line)) | ||
f.handleCSVLine(dec, headers, []string{}, uint64(existingCount)-1, start) | ||
} | ||
|
||
if fromNewIndexFile && len(f.EndByteOffsets) > 0 { | ||
f.EndByteOffsets = f.EndByteOffsets[1:] | ||
f.Checksums = f.Checksums[1:] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func fieldRankCsvField(fieldValue any) int { | ||
switch fieldValue.(type) { | ||
case nil: | ||
return 1 | ||
case bool: | ||
return 2 | ||
case int, int8, int16, int32, int64, float32, float64: | ||
return 3 | ||
case string: | ||
return 4 | ||
default: | ||
panic("unknown type") | ||
} | ||
} | ||
|
||
func inferCSVField(fieldValue string) (interface{}, protocol.FieldType) { | ||
|
||
if fieldValue == "" { | ||
fmt.Printf("sir this is empty") | ||
return nil, protocol.FieldTypeNull | ||
} | ||
|
||
if i, err := strconv.Atoi(fieldValue); err == nil { | ||
return i, protocol.FieldTypeNumber | ||
} | ||
|
||
if f, err := strconv.ParseFloat(fieldValue, 64); err == nil { | ||
return f, protocol.FieldTypeNumber | ||
} | ||
|
||
if b, err := strconv.ParseBool(fieldValue); err == nil { | ||
return b, protocol.FieldTypeBoolean | ||
} | ||
|
||
return fieldValue, protocol.FieldTypeString | ||
} | ||
|
||
func (i *IndexFile) handleCSVLine(dec *csv.Reader, headers []string, path []string, dataIndex, dataOffset uint64) error { | ||
|
||
record, err := dec.Read() | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to read CSV record at index %d: %w", dataIndex, err) | ||
} | ||
|
||
cumulativeLength := uint64(0) | ||
|
||
for fieldIndex, fieldValue := range record { | ||
if fieldIndex >= len(headers) { | ||
return fmt.Errorf("field index %d is out of bounds with header", fieldIndex) | ||
} | ||
|
||
fieldName := headers[fieldIndex] | ||
name := strings.Join(append(path, fieldName), ".") | ||
|
||
fieldOffset := dataOffset + cumulativeLength | ||
fieldLength := uint64(len(fieldValue)) | ||
|
||
value, fieldType := inferCSVField(fieldValue) | ||
|
||
switch fieldType { | ||
case protocol.FieldTypeBoolean, protocol.FieldTypeString, protocol.FieldTypeNumber: | ||
tree := i.Indexes[i.findIndex(name, value)].IndexRecords | ||
|
||
tree[value] = append(tree[value], protocol.IndexRecord{ | ||
DataNumber: dataIndex, | ||
FieldStartByteOffset: uint64(fieldOffset), | ||
FieldLength: int(fieldLength), | ||
}) | ||
|
||
case protocol.FieldTypeNull: | ||
for j := range i.Indexes { | ||
if i.Indexes[j].FieldName == name { | ||
i.Indexes[j].FieldType |= protocol.FieldTypeNull | ||
} | ||
} | ||
|
||
default: | ||
return fmt.Errorf("unexpected type '%T'", value) | ||
} | ||
|
||
cumulativeLength += fieldLength | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.