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

[Insert] Handle formatting timestamps in repository #284

Merged
merged 1 commit into from
Mar 10, 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
28 changes: 20 additions & 8 deletions internal/contentdata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"database/sql"
"fmt"
"github.com/goccy/go-zetasqlite"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the order of imported package.
( Please apply gofmt )

"reflect"
"sort"
"strings"

"github.com/goccy/go-zetasqlite"
"go.uber.org/zap"
bigqueryv2 "google.golang.org/api/bigquery/v2"

Expand Down Expand Up @@ -371,16 +370,16 @@ func (r *Repository) AddTableData(ctx context.Context, tx *connection.Tx, projec
_ = tx.MetadataRepoMode()
}()

var columns []string
var columns []*types.Column
for _, col := range table.Columns {
columns = append(columns, col.Name)
columns = append(columns, col)
}
sort.Strings(columns)

placeholders := make([]string, 0, len(columns))
columnsWithEscape := make([]string, 0, len(columns))
for _, col := range columns {
placeholders = append(placeholders, "?")
columnsWithEscape = append(columnsWithEscape, fmt.Sprintf("`%s`", col))
columnsWithEscape = append(columnsWithEscape, fmt.Sprintf("`%s`", col.Name))
}

query := fmt.Sprintf(
Expand All @@ -397,15 +396,28 @@ func (r *Repository) AddTableData(ctx context.Context, tx *connection.Tx, projec

for _, data := range table.Data {
values := make([]interface{}, 0, len(table.Columns))

for _, column := range columns {
if value, found := data[column]; found {
if value, found := data[column.Name]; found {
isTimestampColumn := column.Type == types.TIMESTAMP
inputString, isInputString := value.(string)

if isInputString && isTimestampColumn {
parsedTimestamp, err := zetasqlite.TimeFromTimestampValue(inputString)
// If we could parse the timestamp, use it when inserting, otherwise fallback to the supplied value
if err == nil {
values = append(values, parsedTimestamp)
continue
}
}

values = append(values, value)
} else {
values = append(values, nil)
}
}

if _, err = stmt.ExecContext(ctx, values...); err != nil {
if _, err := stmt.ExecContext(ctx, values...); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func Format(schema *bigqueryv2.TableSchema, rows []*TableRow, useInt64Timestamp
for _, row := range rows {
cells := make([]*TableCell, 0, len(row.F))
for colIdx, cell := range row.F {
if schema.Fields[colIdx].Type == "TIMESTAMP" {
if schema.Fields[colIdx].Type == "TIMESTAMP" && cell.V != nil {
t, _ := zetasqlite.TimeFromTimestampValue(cell.V.(string))
microsec := t.UnixNano() / int64(time.Microsecond)
cells = append(cells, &TableCell{
Expand Down
Loading