Skip to content
Draft
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
51 changes: 51 additions & 0 deletions internal/contentdata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,60 @@ func isParameterNameChar(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'
}

// applyInformationSchemaTimestampCasts works around googlesqlite modelling
// INFORMATION_SCHEMA.TABLES.creation_time as STRING. BigQuery exposes it as
// TIMESTAMP, so cast the column wherever a query reads it, which both types
// the result schema and makes timestamp comparisons work.
func applyInformationSchemaTimestampCasts(query string) string {
if !strings.Contains(strings.ToLower(query), "information_schema.tables") {
return query
}
var b strings.Builder
var quote byte
escaped := false
for i := 0; i < len(query); i++ {
c := query[i]
if quote != 0 {
b.WriteByte(c)
switch {
case escaped:
escaped = false
case c == '\\':
escaped = true
case c == quote:
quote = 0
}
continue
}
if c == '\'' || c == '"' || c == '`' {
quote = c
b.WriteByte(c)
continue
}
if isIdentStart(c) {
j := i + 1
for j < len(query) && isParameterNameChar(query[j]) {
j++
}
if strings.EqualFold(query[i:j], "creation_time") {
b.WriteString("CAST(" + query[i:j] + " AS TIMESTAMP)")
i = j - 1
continue
}
}
b.WriteByte(c)
}
return b.String()
}

func isIdentStart(c byte) bool {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'
}

func (r *Repository) Query(ctx context.Context, tx *connection.Tx, projectID, datasetID, query string, params []*bigqueryv2.QueryParameter) (*internaltypes.QueryResponse, error) {
tx.SetProjectAndDataset(projectID, datasetID)
query = applyDeclaredNumericParameterTypes(query, params)
query = applyInformationSchemaTimestampCasts(query)
if err := tx.ContentRepoMode(); err != nil {
return nil, err
}
Expand Down
44 changes: 44 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3821,6 +3821,50 @@ func TestNumericQueryParameter(t *testing.T) {
})
}

// TestInformationSchemaTablesCreationTimeIsTimestamp covers #9:
// creation_time must be typed TIMESTAMP so it can be compared with a
// timestamp literal.
func TestInformationSchemaTablesCreationTimeIsTimestamp(t *testing.T) {
const projectID = "test"
ctx := context.Background()
bqServer, err := server.New(server.TempStorage)
if err != nil {
t.Fatal(err)
}
if err := bqServer.Load(server.StructSource(
types.NewProject(projectID, types.NewDataset("dataset1",
types.NewTable("tbl_a", []*types.Column{types.NewColumn("a", types.INT64)}, nil))),
)); err != nil {
t.Fatal(err)
}
testServer := bqServer.TestServer()
defer func() {
testServer.Close()
bqServer.Stop(ctx)
}()

code, resp := httpJSON(t, http.MethodPost,
testServer.URL+"/projects/test/queries",
`{"query":"SELECT table_name, creation_time FROM test.dataset1.INFORMATION_SCHEMA.TABLES WHERE creation_time > TIMESTAMP('2020-01-01')","useLegacySql":false}`, nil)
if code != http.StatusOK {
t.Fatalf("creation_time query: %d (%v)", code, resp)
}
fields, ok := resp["schema"].(map[string]any)["fields"].([]any)
if !ok {
t.Fatalf("missing result schema: %v", resp["schema"])
}
for _, f := range fields {
field := f.(map[string]any)
if field["name"] == "creation_time" && field["type"] != "TIMESTAMP" {
t.Fatalf("creation_time type = %v, want TIMESTAMP", field["type"])
}
}
rows := queryRows(t, resp)
if len(rows) == 0 || rows[0][0] != "tbl_a" {
t.Fatalf("expected tbl_a in result rows, got %v", rows)
}
}

func TestUploadCSVHonorsFieldDelimiter(t *testing.T) {
const (
projectID = "test"
Expand Down