|
| 1 | +diff --git a/pkg/sql/lexbase/encode.go b/pkg/sql/lexbase/encode.go |
| 2 | +index 4f7bc03..5b31fd0 100644 |
| 3 | +--- a/pkg/sql/lexbase/encode.go |
| 4 | ++++ b/pkg/sql/lexbase/encode.go |
| 5 | +@@ -51,6 +51,13 @@ const ( |
| 6 | + // as Oracle is case insensitive if object name is not quoted. |
| 7 | + EncAlwaysQuoted |
| 8 | + |
| 9 | ++ // EncSkipEscapeString indicates that the string should not be escaped, |
| 10 | ++ // and non-ASCII characters are allowed. |
| 11 | ++ // More specifically, it means that the tree.DString won't be wrapped |
| 12 | ++ // with e'text' as the prefix and suffix, and single quotes within |
| 13 | ++ // the string will not be escaped with a backslash. |
| 14 | ++ EncSkipEscapeString |
| 15 | ++ |
| 16 | + // EncFirstFreeFlagBit needs to remain unused; it is used as base |
| 17 | + // bit offset for tree.FmtFlags. |
| 18 | + EncFirstFreeFlagBit |
| 19 | +@@ -147,6 +154,7 @@ func EscapeSQLString(in string) string { |
| 20 | + func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) { |
| 21 | + // See http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html |
| 22 | + start := 0 |
| 23 | ++ skipEscape := flags.HasFlags(EncSkipEscapeString) |
| 24 | + escapedString := false |
| 25 | + bareStrings := flags.HasFlags(EncBareStrings) |
| 26 | + // Loop through each unicode code point. |
| 27 | +@@ -165,7 +173,13 @@ func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) { |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | +- if !escapedString { |
| 32 | ++ // If non-ASCII characters are allowed, |
| 33 | ++ // skip escaping and write the original UTF-8 character. |
| 34 | ++ if r > maxPrintableChar && skipEscape { |
| 35 | ++ continue |
| 36 | ++ } |
| 37 | ++ |
| 38 | ++ if !skipEscape && !escapedString { |
| 39 | + buf.WriteString("e'") // begin e'xxx' string |
| 40 | + escapedString = true |
| 41 | + } |
| 42 | +@@ -177,17 +191,19 @@ func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) { |
| 43 | + } else { |
| 44 | + start = i + ln |
| 45 | + } |
| 46 | +- stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'') |
| 47 | ++ // If we skip escaping, we don't write the slash char before the |
| 48 | ++ // quote char, so we will write the quote char directly. |
| 49 | ++ stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'', !skipEscape) |
| 50 | + } |
| 51 | + |
| 52 | +- quote := !escapedString && !bareStrings |
| 53 | ++ quote := !escapedString && !bareStrings && !skipEscape |
| 54 | + if quote { |
| 55 | + buf.WriteByte('\'') // begin 'xxx' string if nothing was escaped |
| 56 | + } |
| 57 | + if start < len(in) { |
| 58 | + buf.WriteString(in[start:]) |
| 59 | + } |
| 60 | +- if escapedString || quote { |
| 61 | ++ if !skipEscape && (escapedString || quote) { |
| 62 | + buf.WriteByte('\'') |
| 63 | + } |
| 64 | + } |
| 65 | +diff --git a/pkg/sql/sem/tree/expr.go b/pkg/sql/sem/tree/expr.go |
| 66 | +index 05d64e1..dabff21 100644 |
| 67 | +--- a/pkg/sql/sem/tree/expr.go |
| 68 | ++++ b/pkg/sql/sem/tree/expr.go |
| 69 | +@@ -1364,6 +1364,19 @@ const ( |
| 70 | + OrderedSetAgg |
| 71 | + ) |
| 72 | + |
| 73 | ++// onlyNameFunc is the list of function who can be compiled with only the |
| 74 | ++// name. This is for PG compatibility, where examples such as `CURRENT_TIMESTAMP()` |
| 75 | ++// is not allowed, but `CURRENT_TIMESTAMP` is allowed. |
| 76 | ++var onlyNameFunc = map[string]bool{ |
| 77 | ++ "current_timestamp": true, |
| 78 | ++} |
| 79 | ++ |
| 80 | ++func isOnlyNameFunc(f *FuncExpr) bool { |
| 81 | ++ funcStr := f.Func.String() |
| 82 | ++ _, ok := onlyNameFunc[funcStr] |
| 83 | ++ return ok |
| 84 | ++} |
| 85 | ++ |
| 86 | + // Format implements the NodeFormatter interface. |
| 87 | + func (node *FuncExpr) Format(ctx *FmtCtx) { |
| 88 | + var typ string |
| 89 | +@@ -1385,41 +1398,43 @@ func (node *FuncExpr) Format(ctx *FmtCtx) { |
| 90 | + ctx.FormatNode(&node.Func) |
| 91 | + }) |
| 92 | + |
| 93 | +- ctx.WriteByte('(') |
| 94 | +- ctx.WriteString(typ) |
| 95 | +- ctx.FormatNode(&node.Exprs) |
| 96 | +- if node.AggType == GeneralAgg && len(node.OrderBy) > 0 { |
| 97 | +- ctx.WriteByte(' ') |
| 98 | +- ctx.FormatNode(&node.OrderBy) |
| 99 | +- } |
| 100 | +- ctx.WriteByte(')') |
| 101 | +- if ctx.HasFlags(FmtParsable) && node.typ != nil { |
| 102 | +- if node.fnProps.AmbiguousReturnType { |
| 103 | +- // There's no type annotation available for tuples. |
| 104 | +- // TODO(jordan,knz): clean this up. AmbiguousReturnType should be set only |
| 105 | +- // when we should and can put an annotation here. #28579 |
| 106 | +- if node.typ.Family() != types.TupleFamily { |
| 107 | +- ctx.WriteString(":::") |
| 108 | +- ctx.Buffer.WriteString(node.typ.SQLString()) |
| 109 | ++ if !(ctx.HasFlags(FmtFuncOnlyName) && isOnlyNameFunc(node)) { |
| 110 | ++ ctx.WriteByte('(') |
| 111 | ++ ctx.WriteString(typ) |
| 112 | ++ ctx.FormatNode(&node.Exprs) |
| 113 | ++ if node.AggType == GeneralAgg && len(node.OrderBy) > 0 { |
| 114 | ++ ctx.WriteByte(' ') |
| 115 | ++ ctx.FormatNode(&node.OrderBy) |
| 116 | ++ } |
| 117 | ++ ctx.WriteByte(')') |
| 118 | ++ if ctx.HasFlags(FmtParsable) && node.typ != nil { |
| 119 | ++ if node.fnProps.AmbiguousReturnType { |
| 120 | ++ // There's no type annotation available for tuples. |
| 121 | ++ // TODO(jordan,knz): clean this up. AmbiguousReturnType should be set only |
| 122 | ++ // when we should and can put an annotation here. #28579 |
| 123 | ++ if node.typ.Family() != types.TupleFamily { |
| 124 | ++ ctx.WriteString(":::") |
| 125 | ++ ctx.Buffer.WriteString(node.typ.SQLString()) |
| 126 | ++ } |
| 127 | + } |
| 128 | + } |
| 129 | +- } |
| 130 | +- if node.AggType == OrderedSetAgg && len(node.OrderBy) > 0 { |
| 131 | +- ctx.WriteString(" WITHIN GROUP (") |
| 132 | +- ctx.FormatNode(&node.OrderBy) |
| 133 | +- ctx.WriteString(")") |
| 134 | +- } |
| 135 | +- if node.Filter != nil { |
| 136 | +- ctx.WriteString(" FILTER (WHERE ") |
| 137 | +- ctx.FormatNode(node.Filter) |
| 138 | +- ctx.WriteString(")") |
| 139 | +- } |
| 140 | +- if window := node.WindowDef; window != nil { |
| 141 | +- ctx.WriteString(" OVER ") |
| 142 | +- if window.Name != "" { |
| 143 | +- ctx.FormatNode(&window.Name) |
| 144 | +- } else { |
| 145 | +- ctx.FormatNode(window) |
| 146 | ++ if node.AggType == OrderedSetAgg && len(node.OrderBy) > 0 { |
| 147 | ++ ctx.WriteString(" WITHIN GROUP (") |
| 148 | ++ ctx.FormatNode(&node.OrderBy) |
| 149 | ++ ctx.WriteString(")") |
| 150 | ++ } |
| 151 | ++ if node.Filter != nil { |
| 152 | ++ ctx.WriteString(" FILTER (WHERE ") |
| 153 | ++ ctx.FormatNode(node.Filter) |
| 154 | ++ ctx.WriteString(")") |
| 155 | ++ } |
| 156 | ++ if window := node.WindowDef; window != nil { |
| 157 | ++ ctx.WriteString(" OVER ") |
| 158 | ++ if window.Name != "" { |
| 159 | ++ ctx.FormatNode(&window.Name) |
| 160 | ++ } else { |
| 161 | ++ ctx.FormatNode(window) |
| 162 | ++ } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +diff --git a/pkg/sql/sem/tree/format.go b/pkg/sql/sem/tree/format.go |
| 167 | +index 6832c7e..df15c15 100644 |
| 168 | +--- a/pkg/sql/sem/tree/format.go |
| 169 | ++++ b/pkg/sql/sem/tree/format.go |
| 170 | +@@ -195,6 +195,10 @@ const ( |
| 171 | + // FmtSkipAsOfSystemTimeClauses prevents the formatter from printing AS OF |
| 172 | + // SYSTEM TIME clauses. |
| 173 | + FmtSkipAsOfSystemTimeClauses |
| 174 | ++ |
| 175 | ++ // FmtFuncOnlyName instructs the formating for a function only print |
| 176 | ++ // its name, without printing the body of the function nor with the brackets. |
| 177 | ++ FmtFuncOnlyName |
| 178 | + ) |
| 179 | + |
| 180 | + const genericArityIndicator = "__more__" |
| 181 | +diff --git a/pkg/util/stringencoding/string_encoding.go b/pkg/util/stringencoding/string_encoding.go |
| 182 | +index fcb7fac..afa0090 100644 |
| 183 | +--- a/pkg/util/stringencoding/string_encoding.go |
| 184 | ++++ b/pkg/util/stringencoding/string_encoding.go |
| 185 | +@@ -73,6 +73,7 @@ func init() { |
| 186 | + |
| 187 | + // EncodeEscapedChar is used internally to write out a character from a larger |
| 188 | + // string that needs to be escaped to a buffer. |
| 189 | ++// If slashedQuoteChar is true, it will write a backslash before the quoteChar. |
| 190 | + func EncodeEscapedChar( |
| 191 | + buf *bytes.Buffer, |
| 192 | + entireString string, |
| 193 | +@@ -80,6 +81,7 @@ func EncodeEscapedChar( |
| 194 | + currentByte byte, |
| 195 | + currentIdx int, |
| 196 | + quoteChar byte, |
| 197 | ++ slashedQuoteChar bool, |
| 198 | + ) { |
| 199 | + ln := utf8.RuneLen(currentRune) |
| 200 | + if currentRune == utf8.RuneError { |
| 201 | +@@ -94,10 +96,15 @@ func EncodeEscapedChar( |
| 202 | + } else if ln == 1 { |
| 203 | + // For single-byte runes, do the same as encodeSQLBytes. |
| 204 | + if encodedChar := EncodeMap[currentByte]; encodedChar != DontEscape { |
| 205 | +- buf.WriteByte('\\') |
| 206 | ++ if slashedQuoteChar { |
| 207 | ++ buf.WriteByte('\\') |
| 208 | ++ } |
| 209 | ++ |
| 210 | + buf.WriteByte(encodedChar) |
| 211 | + } else if currentByte == quoteChar { |
| 212 | +- buf.WriteByte('\\') |
| 213 | ++ if slashedQuoteChar { |
| 214 | ++ buf.WriteByte('\\') |
| 215 | ++ } |
| 216 | + buf.WriteByte(quoteChar) |
| 217 | + } else { |
| 218 | + // Escape non-printable characters. |
0 commit comments