Skip to content

Commit 1eac097

Browse files
authored
Merge pull request #54 from Recidiviz/dan/fix-table-creation-metadata
Store Table `lastModifiedTime` / `creationTime` timestamps as Unix milliseconds
2 parents b380e8f + 36a93a8 commit 1eac097

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

server/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,7 @@ type tablesInsertRequest struct {
32623262
}
32633263

32643264
func createTableMetadata(ctx context.Context, tx *connection.Tx, server *Server, project *metadata.Project, dataset *metadata.Dataset, table *bigqueryv2.Table) (*bigqueryv2.Table, *ServerError) {
3265-
now := time.Now().Unix()
3265+
now := time.Now().UnixMilli()
32663266
table.Id = fmt.Sprintf("%s:%s.%s", project.ID, dataset.ID, table.TableReference.TableId)
32673267
table.CreationTime = now
32683268
table.LastModifiedTime = uint64(now)

test/python/emulator_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,3 +1123,35 @@ def test_time_min_max(self) -> None:
11231123
}
11241124
],
11251125
)
1126+
1127+
def test_table_metadata_timestamp_format(self) -> None:
1128+
"""Tests resolution of https://github.com/goccy/bigquery-emulator/issues/432
1129+
1130+
Table metadata timestamps (creationTime, lastModifiedTime) should be in
1131+
Unix milliseconds (13 digits), not Unix seconds (10 digits), to match
1132+
the real BigQuery API behavior.
1133+
"""
1134+
address = BigQueryAddress(dataset_id=_DATASET_1, table_id=_TABLE_1)
1135+
self.create_mock_table(
1136+
address,
1137+
schema=[
1138+
bigquery.SchemaField(
1139+
"a",
1140+
field_type=bigquery.enums.SqlTypeNames.INTEGER.value,
1141+
mode="REQUIRED",
1142+
),
1143+
],
1144+
)
1145+
1146+
# Get the table metadata
1147+
table = self.client.get_table(self._table_ref_for_address(address))
1148+
1149+
# Verify that created timestamp is set and in a reasonable range
1150+
self.assertIsNotNone(table.created)
1151+
self.assertIsNotNone(table.modified)
1152+
1153+
# The timestamps should be datetime objects that correspond to a recent time
1154+
# (not 1970 which would happen if milliseconds were interpreted as seconds)
1155+
now = datetime.datetime.now()
1156+
self.assertGreaterEqual(table.created.year, now.year)
1157+
self.assertGreaterEqual(table.modified.year, now.year)

types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (t *Table) ToBigqueryV2(projectID, datasetID string) *bigqueryv2.Table {
3737
for i, col := range t.Columns {
3838
fields[i] = col.TableFieldSchema()
3939
}
40-
now := time.Now().Unix()
40+
now := time.Now().UnixMilli()
4141
return &bigqueryv2.Table{
4242
Type: "TABLE",
4343
Kind: "bigquery#table",

0 commit comments

Comments
 (0)