Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bq partitioning for additional columns #5293

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions warehouse/integrations/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,67 @@ func TestIntegration(t *testing.T) {
require.ElementsMatch(t, groupsRecords, whth.UploadJobGroupsAppendRecords(userIDFormat, sourceID, destinationID, destType))
},
},
{
name: "Append mode (partitionColumn: timestamp, partitionType: hour)",
tables: []string{"identifies", "users", "tracks", "product_track", "pages", "screens", "aliases", "_groups"},
warehouseEventsMap2: whth.EventsCountMap{
// For all tables we will be appending because of preferAppend config
"identifies": 8, "users": 2, "tracks": 8, "product_track": 8, "pages": 8, "screens": 8, "aliases": 8, "_groups": 8,
},
stagingFilePath1: "../testdata/upload-job.events-1.json",
stagingFilePath2: "../testdata/upload-job.events-1.json",
useSameUserID: true,
postLoading: func(t testing.TB, ctx context.Context, db *bigquery.Client, namespace string) {
t.Helper()

checkTables := []string{"identifies", "users", "tracks", "product_track", "pages", "screens", "aliases", "_groups"}

tables := listTables(t, ctx, db, namespace)
filteredTables := lo.Filter(tables, func(table *bigquery.TableMetadata, _ int) bool {
return lo.Contains(checkTables, table.Name)
})
for _, table := range filteredTables {
require.NotNil(t, table.TimePartitioning)
require.Equal(t, "timestamp", table.TimePartitioning.Field)
require.Equal(t, bigquery.HourPartitioningType, table.TimePartitioning.Type)
}
partitions := listPartitions(t, ctx, db, namespace)
filteredPartitions := lo.Filter(partitions, func(table lo.Tuple2[string, string], _ int) bool {
return lo.Contains(checkTables, table.A)
})
for _, partition := range filteredPartitions {
require.Equal(t, partition.B, "2023051204")
}
},
configOverride: map[string]any{
"partitionColumn": "timestamp",
"partitionType": "hour",
},
verifySchema: func(t testing.TB, db *bigquery.Client, namespace string) {
t.Helper()
schema := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT t.table_name, c.column_name, c.data_type FROM %[1]s.INFORMATION_SCHEMA.TABLES as t LEFT JOIN %[1]s.INFORMATION_SCHEMA.COLUMNS as c ON (t.table_name = c.table_name) WHERE (t.table_type != 'VIEW') AND ( c.column_name != '_PARTITIONTIME' OR c.column_name IS NULL );`, namespace))
require.Equal(t, expectedUploadJobSchema, whth.ConvertRecordsToSchema(schema))
},
verifyRecords: func(t testing.TB, db *bigquery.Client, sourceID, destinationID, namespace, jobRunID, taskRunID string) {
t.Helper()
identifiesRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT %s, %s, context_traits_logins, _as, name, logins, email, original_timestamp, context_ip, context_traits_as, timestamp, received_at, context_destination_type, sent_at, context_source_type, context_traits_between, context_source_id, context_traits_name, context_request_ip, _between, context_traits_email, context_destination_id, id FROM %s.%s ORDER BY id;`, userIDSQL, uuidTSSQL, namespace, "identifies"))
require.ElementsMatch(t, identifiesRecords, whth.UploadJobIdentifiesAppendRecords(userIDFormat, sourceID, destinationID, destType))
usersRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT context_source_id, context_destination_type, context_request_ip, context_traits_name, context_traits_between, _as, logins, sent_at, context_traits_logins, context_ip, _between, context_traits_email, timestamp, context_destination_id, email, context_traits_as, context_source_type, SUBSTRING(id, 1, 9), %s, received_at, name, original_timestamp FROM %s.%s ORDER BY id;`, uuidTSSQL, namespace, "users"))
require.ElementsMatch(t, usersRecords, whth.UploadJobUsersAppendRecordsUsingUsersLoadFiles(userIDFormat, sourceID, destinationID, destType))
tracksRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT original_timestamp, context_destination_id, context_destination_type, %s, context_source_type, timestamp, id, event, sent_at, context_ip, event_text, context_source_id, context_request_ip, received_at, %s FROM %s.%s ORDER BY id;`, uuidTSSQL, userIDSQL, namespace, "tracks"))
require.ElementsMatch(t, tracksRecords, whth.UploadJobTracksAppendRecords(userIDFormat, sourceID, destinationID, destType))
productTrackRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT timestamp, %s, product_id, received_at, context_source_id, sent_at, context_source_type, context_ip, context_destination_type, original_timestamp, context_request_ip, context_destination_id, %s, _as, review_body, _between, review_id, event_text, id, event, rating FROM %s.%s ORDER BY id;`, userIDSQL, uuidTSSQL, namespace, "product_track"))
require.ElementsMatch(t, productTrackRecords, whth.UploadJobProductTrackAppendRecords(userIDFormat, sourceID, destinationID, destType))
pagesRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT %s, context_source_id, id, title, timestamp, context_source_type, _as, received_at, context_destination_id, context_ip, context_destination_type, name, original_timestamp, _between, context_request_ip, sent_at, url, %s FROM %s.%s ORDER BY id;`, userIDSQL, uuidTSSQL, namespace, "pages"))
require.ElementsMatch(t, pagesRecords, whth.UploadJobPagesAppendRecords(userIDFormat, sourceID, destinationID, destType))
screensRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT context_destination_type, url, context_source_type, title, original_timestamp, %s, _between, context_ip, name, context_request_ip, %s, context_source_id, id, received_at, context_destination_id, timestamp, sent_at, _as FROM %s.%s ORDER BY id;`, userIDSQL, uuidTSSQL, namespace, "screens"))
require.ElementsMatch(t, screensRecords, whth.UploadJobScreensAppendRecords(userIDFormat, sourceID, destinationID, destType))
aliasesRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT context_source_id, context_destination_id, context_ip, sent_at, id, %s, %s, previous_id, original_timestamp, context_source_type, received_at, context_destination_type, context_request_ip, timestamp FROM %s.%s ORDER BY id;`, userIDSQL, uuidTSSQL, namespace, "aliases"))
require.ElementsMatch(t, aliasesRecords, whth.UploadJobAliasesAppendRecords(userIDFormat, sourceID, destinationID, destType))
groupsRecords := bqhelper.RetrieveRecordsFromWarehouse(t, db, fmt.Sprintf(`SELECT context_destination_type, id, _between, plan, original_timestamp, %s, context_source_id, sent_at, %s, group_id, industry, context_request_ip, context_source_type, timestamp, employees, _as, context_destination_id, received_at, name, context_ip FROM %s.%s ORDER BY id;`, uuidTSSQL, userIDSQL, namespace, "_groups"))
require.ElementsMatch(t, groupsRecords, whth.UploadJobGroupsAppendRecords(userIDFormat, sourceID, destinationID, destType))
},
},
{
name: "Append mode (partitionColumn: received_at, partitionType: hour)",
tables: []string{"identifies", "users", "tracks", "product_track", "pages", "screens", "aliases", "_groups"},
Expand Down
9 changes: 6 additions & 3 deletions warehouse/integrations/bigquery/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ var (
)

var supportedPartitionColumnMap = map[string]struct{}{
"_PARTITIONTIME": {},
"loaded_at": {},
"received_at": {},
"_PARTITIONTIME": {},
"loaded_at": {},
"received_at": {},
"sent_at": {},
"timestamp": {},
"original_timestamp": {},
}

var supportedPartitionTypeMap = map[string]bigquery.TimePartitioningType{
Expand Down
23 changes: 22 additions & 1 deletion warehouse/integrations/bigquery/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,28 @@ func TestBigQuery_AvoidPartitionDecorator(t *testing.T) {
avoidPartitionDecorator: true,
},
{
name: "ingenstion partition",
name: "sent_at partition",
destConfig: map[string]interface{}{
"partitionColumn": "sent_at",
},
avoidPartitionDecorator: true,
},
{
name: "timestamp partition",
destConfig: map[string]interface{}{
"partitionColumn": "timestamp",
},
avoidPartitionDecorator: true,
},
{
name: "original_timestamp partition",
destConfig: map[string]interface{}{
"partitionColumn": "received_at",
},
avoidPartitionDecorator: true,
},
{
name: "ingestion partition",
destConfig: map[string]interface{}{
"partitionColumn": "_PARTITIONTIME",
},
Expand Down
Loading