Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
129019: schemachanger: support NaN/Inf as a default value in a function r=rafiss a=rafiss

fixes cockroachdb#129017
Release note (bug fix): Fixed a bug where NaN or Inf could not be used as the default value for a parameter in CREATE FUNCTION statements.

129075: util: fix return values for cidr http r=jeffswenson a=andrewbaptist

Previously the cidr http check did not return true when it completed. This adds the true return and additionally adds logging for other failure cases.

Epic: none

Release note: None

Co-authored-by: Rafi Shamim <[email protected]>
Co-authored-by: Andrew Baptist <[email protected]>
  • Loading branch information
3 people committed Aug 15, 2024
3 parents 7983e80 + a675cc7 + 037b558 commit ab496aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf_params
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,10 @@ CREATE FUNCTION my_sum(a INT, b INT DEFAULT (SELECT 1)) RETURNS INT LANGUAGE SQL
statement error pgcode 0A000 subqueries are not allowed in DEFAULT expressions
CREATE FUNCTION my_sum(a INT, b INT DEFAULT 1 + (SELECT 2 FROM (VALUES (NULL)))) RETURNS INT LANGUAGE SQL AS $$ SELECT a + b; $$;

# Verify that 'NaN' can be used as a default.
statement ok
CREATE FUNCTION f_nan(a INT, b INT DEFAULT 2, c FLOAT DEFAULT 'NaN':::FLOAT) RETURNS INT LANGUAGE SQL AS $$ SELECT a + b; $$;

statement ok
CREATE FUNCTION my_sum(a INT, b INT, c INT) RETURNS INT LANGUAGE SQL AS $$ SELECT a + b + c; $$;

Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/schemachanger/scbuild/ast_annotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func newAstAnnotator(original tree.Statement) (*astAnnotator, error) {
// Clone the original tree by re-parsing the input back into an AST. We need
// to keep tagged dollar quotes in case they're necessary to parse the
// original statement.
statement, err := parser.ParseOne(tree.AsStringWithFlags(original, tree.FmtTagDollarQuotes))
formatted := tree.AsStringWithFlags(original, tree.FmtTagDollarQuotes|tree.FmtParsableNumerics)
statement, err := parser.ParseOne(formatted)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/util/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func NewLookup(st *settings.Values) *Lookup {
c.changed = make(chan time.Duration)

cidrMappingUrl.SetOnChange(st, func(ctx context.Context) {
log.Infof(ctx, "url changed to '%s'", cidrMappingUrl.Get(st))
// Reset the lastUpdate time so that the URL is always reloaded even if
// the new file/URL has an older timestamp.
c.lastUpdate.Store(time.Time{})
Expand All @@ -85,6 +86,7 @@ func NewLookup(st *settings.Values) *Lookup {
// and the setting is changed before we register the callback and the
// ticker will not be reset to the new value.
cidrRefreshInterval.SetOnChange(c.st, func(ctx context.Context) {
log.Infof(ctx, "refresh interval changed to '%s'", cidrRefreshInterval.Get(c.st))
c.changed <- cidrRefreshInterval.Get(c.st)
})
return c
Expand Down Expand Up @@ -136,10 +138,10 @@ func hexString(b []byte) string {
// refresh is called to update the CIDR mapping. It checks if the URL has been
// recently updated and if so, it will reload the mapping.
func (c *Lookup) refresh(ctx context.Context) {
url := cidrMappingUrl.Get(c.st)
// Check if the URL is updated
if c.isUpdated(ctx, cidrMappingUrl.Get(c.st)) {
if c.isUpdated(ctx, url) {
// Set the URL
url := cidrMappingUrl.Get(c.st)
if err := c.setURL(ctx, url); err != nil {
log.Errorf(ctx, "error setting CIDR URL to '%s': %v", url, err)
}
Expand All @@ -157,6 +159,7 @@ func (c *Lookup) isUpdated(ctx context.Context, rawURL string) bool {
// Get the file information
fileInfo, err := os.Stat(filePath)
if err != nil {
log.Warningf(ctx, "error running stat on file '%s', %v", rawURL, err)
return false
}

Expand All @@ -172,25 +175,29 @@ func (c *Lookup) isUpdated(ctx context.Context, rawURL string) bool {
// Send a HEAD request to the URL
resp, err := client.Head(rawURL)
if err != nil {
log.Warningf(ctx, "error running head on url '%s', %v", rawURL, err)
return false
}
defer resp.Body.Close()

// Get the Last-Modified header from the response
lastModified := resp.Header.Get("Last-Modified")
if lastModified == "" {
log.Warningf(ctx, "no last modified header on '%s', %v", rawURL, err)
return false
}

// Parse the Last-Modified header
modTime, err := http.ParseTime(lastModified)
if err != nil {
log.Warningf(ctx, "can't parse time %s '%s', %v", modTime, rawURL, err)
return false
}

// Compare the modification time with lastUpdate
if modTime.After(c.lastUpdate.Load().(time.Time)) {
c.lastUpdate.Store(modTime)
return true
}
} else if rawURL == "" {
if c.lastUpdate.Load().(time.Time).IsZero() {
Expand Down

0 comments on commit ab496aa

Please sign in to comment.