Skip to content

Commit af43ec6

Browse files
committed
Change timestamp unit for the loader script
Previously, this script used to convert the timestamp column types of a table to milliseconds. This commit ensures that it matches the current version of ModelarDB where timestamps are read in microseconds. In addition, the commit ensures that not only schema is casted, but also timestamps are casted to microseconds in the process.
1 parent 607def1 commit af43ec6

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

Apache-Parquet-Loader/main.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pyarrow
66
from pyarrow import parquet
77
from pyarrow import flight
8+
import pyarrow.compute as pc
89

910

1011
# Helper Functions.
@@ -19,7 +20,7 @@ def create_time_series_table(flight_client, table_name, schema, error_bound):
1920
# as spaces and punctuation.
2021
columns = []
2122
for field in schema:
22-
if field.type == pyarrow.timestamp("ms"):
23+
if field.type == pyarrow.timestamp("us"):
2324
columns.append(f"`{field.name}` TIMESTAMP")
2425
elif field.type == pyarrow.float32():
2526
columns.append(f"`{field.name}` FIELD({error_bound}%)")
@@ -42,28 +43,29 @@ def read_parquet_file_or_folder(path):
4243

4344
# Ensure the schema only uses supported types.
4445
columns = []
45-
column_names = []
46+
fields = []
47+
4648
for field in arrow_table.schema:
47-
column_names.append(field.name)
49+
col = arrow_table[field.name]
4850

49-
if field.type == pyarrow.float16() or field.type == pyarrow.float64():
51+
if field.type in [pyarrow.float16(), pyarrow.float64()]:
5052
# Ensure fields are float32 as others are not supported.
51-
columns.append((field.name, pyarrow.float32()))
53+
col = pc.cast(col, pyarrow.float32())
54+
fields.append(pyarrow.field(field.name, pyarrow.float32()))
5255
elif field.type in [
5356
pyarrow.timestamp("s"),
54-
pyarrow.timestamp("us"),
57+
pyarrow.timestamp("ms"),
5558
pyarrow.timestamp("ns"),
5659
]:
57-
# Ensure timestamps are timestamp[ms] as others are not supported.
58-
columns.append((field.name, pyarrow.timestamp("ms")))
60+
# Ensure timestamps are timestamp[us] as others are not supported.
61+
col = pc.cast(col, pyarrow.timestamp("us"))
62+
fields.append(pyarrow.field(field.name, pyarrow.timestamp("us")))
5963
else:
60-
columns.append((field.name, field.type))
61-
62-
safe_schema = pyarrow.schema(columns)
64+
fields.append(field)
6365

64-
# Cast the columns to the supported types.
65-
arrow_table = arrow_table.rename_columns(column_names)
66-
return arrow_table.cast(safe_schema)
66+
columns.append(col)
67+
# Create a new table with the supported types.
68+
return pyarrow.Table.from_arrays(columns, schema=pyarrow.schema(fields))
6769

6870

6971
def do_put_arrow_table(flight_client, table_name, arrow_table):

0 commit comments

Comments
 (0)