-
Notifications
You must be signed in to change notification settings - Fork 30
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
5 changed files
with
84 additions
and
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package bigquery | ||
|
||
import ( | ||
"fmt" | ||
"maps" | ||
"slices" | ||
|
||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
) | ||
|
||
func buildDistinctDates(colName string, rows []map[string]any) ([]string, error) { | ||
dateMap := make(map[string]bool) | ||
for _, row := range rows { | ||
val, isOk := row[colName] | ||
if !isOk { | ||
return nil, fmt.Errorf("column %q does not exist in row: %v", colName, row) | ||
} | ||
|
||
_time, err := ext.ParseDateFromAny(val) | ||
if err != nil { | ||
return nil, fmt.Errorf("column %q is not a time column, value: %v, err: %w", colName, val, err) | ||
} | ||
|
||
dateMap[_time.Format(ext.PostgresDateFormat)] = true | ||
} | ||
|
||
return slices.Collect(maps.Keys(dateMap)), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bigquery | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDistinctDates(t *testing.T) { | ||
{ | ||
// Invalid date | ||
dates, err := buildDistinctDates("ts", []map[string]any{ | ||
{"ts": time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
{"ts": nil}, | ||
}) | ||
assert.ErrorContains(t, err, `column "ts" is not a time column`) | ||
assert.Empty(t, dates) | ||
} | ||
{ | ||
// No dates | ||
dates, err := buildDistinctDates("", nil) | ||
assert.NoError(t, err) | ||
assert.Empty(t, dates) | ||
} | ||
{ | ||
// One date | ||
dates, err := buildDistinctDates("ts", []map[string]any{ | ||
{"ts": time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{"2020-01-01"}, dates) | ||
} | ||
{ | ||
// Two dates | ||
dates, err := buildDistinctDates("ts", []map[string]any{ | ||
{"ts": time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
{"ts": time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{"2020-01-01", "2020-01-02"}, dates) | ||
} | ||
{ | ||
// Three days, two unique | ||
dates, err := buildDistinctDates("ts", []map[string]any{ | ||
{"ts": time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
{"ts": time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
{"ts": time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)}, | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{"2020-01-01", "2020-01-02"}, dates) | ||
} | ||
} |
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