From b2f20cd70089daaa67f6c65b57420cba28f2ae7a Mon Sep 17 00:00:00 2001 From: expobrain Date: Tue, 24 Nov 2026 00:12:32 +0000 Subject: [PATCH] fix(types): type INFORMATION_SCHEMA.TABLES.creation_time as TIMESTAMP (#9) googlesqlite models creation_time as a STRING column. Cast it to TIMESTAMP when a query reads it from INFORMATION_SCHEMA.TABLES, which fixes both the reported column type and comparisons against timestamp literals. Ownership note: the underlying schema lives in googlesqlite; until it is fixed upstream, the emulator applies the cast at query rewrite time. Refs #9 --- internal/contentdata/repository.go | 51 ++++++++++++++++++++++++++++++ server/server_test.go | 44 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/internal/contentdata/repository.go b/internal/contentdata/repository.go index 6438a07d..7843150e 100644 --- a/internal/contentdata/repository.go +++ b/internal/contentdata/repository.go @@ -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 } diff --git a/server/server_test.go b/server/server_test.go index dbec5938..f0c71ee2 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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"