:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hogql): double backticks when escaping identifiers (#65524)
Robbied6d7c4bc9eae
diff --git a/common/hogvm/__tests__/__snapshots__/arrays.js b/common/hogvm/__tests__/__snapshots__/arrays.jsindex d843199f59fa..d7195f0bb881 100644--- a/common/hogvm/__tests__/__snapshots__/arrays.js+++ b/common/hogvm/__tests__/__snapshots__/arrays.js@@ -53,7 +53,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/between.js b/common/hogvm/__tests__/__snapshots__/between.jsindex e9ab9b0560d9..573339eac7c3 100644--- a/common/hogvm/__tests__/__snapshots__/between.js+++ b/common/hogvm/__tests__/__snapshots__/between.js@@ -31,7 +31,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/bytecodeStl.js b/common/hogvm/__tests__/__snapshots__/bytecodeStl.jsindex d7ba6dede498..864b2ab0daaa 100644--- a/common/hogvm/__tests__/__snapshots__/bytecodeStl.js+++ b/common/hogvm/__tests__/__snapshots__/bytecodeStl.js@@ -49,7 +49,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/catch.js b/common/hogvm/__tests__/__snapshots__/catch.jsindex 396c8eef2e31..6be312dba58e 100644--- a/common/hogvm/__tests__/__snapshots__/catch.js+++ b/common/hogvm/__tests__/__snapshots__/catch.js@@ -39,7 +39,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/catch2.js b/common/hogvm/__tests__/__snapshots__/catch2.jsindex acae01a1f57f..37aaa35825bc 100644--- a/common/hogvm/__tests__/__snapshots__/catch2.js+++ b/common/hogvm/__tests__/__snapshots__/catch2.js@@ -39,7 +39,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/crypto.js b/common/hogvm/__tests__/__snapshots__/crypto.jsindex d870579c46ac..09063b28dc3e 100644--- a/common/hogvm/__tests__/__snapshots__/crypto.js+++ b/common/hogvm/__tests__/__snapshots__/crypto.js@@ -37,7 +37,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/date.js b/common/hogvm/__tests__/__snapshots__/date.jsindex 71af223af0e5..dca27310c804 100644--- a/common/hogvm/__tests__/__snapshots__/date.js+++ b/common/hogvm/__tests__/__snapshots__/date.js@@ -74,7 +74,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/dateFormat.js b/common/hogvm/__tests__/__snapshots__/dateFormat.jsindex a8b96d923e0e..86a3eb51ea9a 100644--- a/common/hogvm/__tests__/__snapshots__/dateFormat.js+++ b/common/hogvm/__tests__/__snapshots__/dateFormat.js@@ -193,7 +193,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/dicts.js b/common/hogvm/__tests__/__snapshots__/dicts.jsindex d2eac1042360..90932d75fdfe 100644--- a/common/hogvm/__tests__/__snapshots__/dicts.js+++ b/common/hogvm/__tests__/__snapshots__/dicts.js@@ -36,7 +36,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.hoge b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.hogenew file mode 100644index 000000000000..87492f59a2f1--- /dev/null+++ b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.hoge@@ -0,0 +1,2 @@+["_H", 1, 32, "a`b", 33, 10, 32, "with space", 33, 20, 32, "a\\b", 33, 30, 42, 3, 36, 0, 32, "a`b", 45, 36, 0, 32,+"with space", 45, 36, 0, 32, "a\\b", 45, 2, "print", 3, 35, 35]diff --git a/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.js b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.jsnew file mode 100644index 000000000000..200d64f80437--- /dev/null+++ b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.js@@ -0,0 +1,46 @@+function print (...args) { console.log(...args.map(__printHogStringOutput)) }+function __printHogStringOutput(obj) { if (typeof obj === 'string') { return obj } return __printHogValue(obj) }+function __printHogValue(obj, marked = new Set()) {+ if (typeof obj === 'object' && obj !== null && obj !== undefined) {+ if (marked.has(obj) && !__isHogDateTime(obj) && !__isHogDate(obj) && !__isHogError(obj)) { return 'null'; }+ marked.add(obj);+ try {+ if (Array.isArray(obj)) {+ if (obj.__isHogTuple) { return obj.length < 2 ? `tuple(${obj.map((o) => __printHogValue(o, marked)).join(', ')})` : `(${obj.map((o) => __printHogValue(o, marked)).join(', ')})`; }+ return `[${obj.map((o) => __printHogValue(o, marked)).join(', ')}]`;+ }+ if (__isHogDateTime(obj)) { const millis = String(obj.dt); return `DateTime(${millis}${millis.includes('.') ? '' : '.0'}, ${__escapeString(obj.zone)})`; }+ if (__isHogDate(obj)) return `Date(${obj.year}, ${obj.month}, ${obj.day})`;+ if (__isHogError(obj)) { return `${String(obj.type)}(${__escapeString(obj.message)}${obj.payload ? `, ${__printHogValue(obj.payload, marked)}` : ''})`; }+ if (obj instanceof Map) { return `{${Array.from(obj.entries()).map(([key, value]) => `${__printHogValue(key, marked)}: ${__printHogValue(value, marked)}`).join(', ')}}`; }+ return `{${Object.entries(obj).map(([key, value]) => `${__printHogValue(key, marked)}: ${__printHogValue(value, marked)}`).join(', ')}}`;+ } finally {+ marked.delete(obj);+ }+ } else if (typeof obj === 'boolean') return obj ? 'true' : 'false';+ else if (obj === null || obj === undefined) return 'null';+ else if (typeof obj === 'string') return __escapeString(obj);+ if (typeof obj === 'function') return `fn<${__escapeIdentifier(obj.name || 'lambda')}(${obj.length})>`;+ return obj.toString();+}+function __isHogError(obj) {return obj && obj.__hogError__ === true}+function __isHogDateTime(obj) { return obj && obj.__hogDateTime__ === true }+function __isHogDate(obj) { return obj && obj.__hogDate__ === true }+function __getProperty(objectOrArray, key, nullish) {+ if ((nullish && !objectOrArray) || key === 0) { return null }+ if (Array.isArray(objectOrArray)) { return key > 0 ? objectOrArray[key - 1] : objectOrArray[objectOrArray.length + key] }+ else { return objectOrArray[key] }+}+function __escapeString(value) {+ const singlequoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', "'": "\\'" }+ return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;+}+function __escapeIdentifier(identifier) {+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }+ if (typeof identifier === 'number') return identifier.toString();+ if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;+ return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;+}++let obj = {"a`b": 10, "with space": 20, "a\\b": 30};+print(__getProperty(obj, "a`b", true), __getProperty(obj, "with space", true), __getProperty(obj, "a\\b", true));diff --git a/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.stdout b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.stdoutnew file mode 100644index 000000000000..659cfca5e81c--- /dev/null+++ b/common/hogvm/__tests__/__snapshots__/escapedIdentifiers.stdout@@ -0,0 +1 @@+10 20 30diff --git a/common/hogvm/__tests__/__snapshots__/exceptions.js b/common/hogvm/__tests__/__snapshots__/exceptions.jsindex 46cb9b053a3b..9a1e653077c3 100644--- a/common/hogvm/__tests__/__snapshots__/exceptions.js+++ b/common/hogvm/__tests__/__snapshots__/exceptions.js@@ -34,7 +34,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/functionVars.js b/common/hogvm/__tests__/__snapshots__/functionVars.jsindex 9f7ca6b8b4ea..5fd57a8b653f 100644--- a/common/hogvm/__tests__/__snapshots__/functionVars.js+++ b/common/hogvm/__tests__/__snapshots__/functionVars.js@@ -34,7 +34,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/functions.js b/common/hogvm/__tests__/__snapshots__/functions.jsindex 2241faa08aa4..c6a23e0feeab 100644--- a/common/hogvm/__tests__/__snapshots__/functions.js+++ b/common/hogvm/__tests__/__snapshots__/functions.js@@ -38,7 +38,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/hogqlx.js b/common/hogvm/__tests__/__snapshots__/hogqlx.jsindex 24b24f21e44f..bed7c4d2a6e6 100644--- a/common/hogvm/__tests__/__snapshots__/hogqlx.js+++ b/common/hogvm/__tests__/__snapshots__/hogqlx.js@@ -66,7 +66,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/ifElse.js b/common/hogvm/__tests__/__snapshots__/ifElse.jsindex 995143425a2e..861dbcbf3602 100644--- a/common/hogvm/__tests__/__snapshots__/ifElse.js+++ b/common/hogvm/__tests__/__snapshots__/ifElse.js@@ -31,7 +31,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/ifJump.js b/common/hogvm/__tests__/__snapshots__/ifJump.jsindex 027e1388c01d..97cd654abaf0 100644--- a/common/hogvm/__tests__/__snapshots__/ifJump.js+++ b/common/hogvm/__tests__/__snapshots__/ifJump.js@@ -36,7 +36,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/ip.js b/common/hogvm/__tests__/__snapshots__/ip.jsindex 09a4303bf27c..9f3fe178fe72 100644--- a/common/hogvm/__tests__/__snapshots__/ip.js+++ b/common/hogvm/__tests__/__snapshots__/ip.js@@ -108,7 +108,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/json.js b/common/hogvm/__tests__/__snapshots__/json.jsindex 19ee1a0c2537..4cfdd99f8d95 100644--- a/common/hogvm/__tests__/__snapshots__/json.js+++ b/common/hogvm/__tests__/__snapshots__/json.js@@ -85,7 +85,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/keysValues.js b/common/hogvm/__tests__/__snapshots__/keysValues.jsindex 0adcf886f2e1..09fd66a8a639 100644--- a/common/hogvm/__tests__/__snapshots__/keysValues.js+++ b/common/hogvm/__tests__/__snapshots__/keysValues.js@@ -34,7 +34,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/lambdas.js b/common/hogvm/__tests__/__snapshots__/lambdas.jsindex 67dd85fabb33..e44df512d201 100644--- a/common/hogvm/__tests__/__snapshots__/lambdas.js+++ b/common/hogvm/__tests__/__snapshots__/lambdas.js@@ -92,7 +92,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/loops.js b/common/hogvm/__tests__/__snapshots__/loops.jsindex 00fd42ebbf21..5da1e3089e50 100644--- a/common/hogvm/__tests__/__snapshots__/loops.js+++ b/common/hogvm/__tests__/__snapshots__/loops.js@@ -34,7 +34,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/mandelbrot.js b/common/hogvm/__tests__/__snapshots__/mandelbrot.jsindex 041a6f3bd826..eef7a38c0e6a 100644--- a/common/hogvm/__tests__/__snapshots__/mandelbrot.js+++ b/common/hogvm/__tests__/__snapshots__/mandelbrot.js@@ -34,7 +34,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/nulls.js b/common/hogvm/__tests__/__snapshots__/nulls.jsindex 3b6a6eacb07f..e3fcd93b9c52 100644--- a/common/hogvm/__tests__/__snapshots__/nulls.js+++ b/common/hogvm/__tests__/__snapshots__/nulls.js@@ -32,7 +32,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/operations.js b/common/hogvm/__tests__/__snapshots__/operations.jsindex 7bc248e68339..ad85996fc3ec 100644--- a/common/hogvm/__tests__/__snapshots__/operations.js+++ b/common/hogvm/__tests__/__snapshots__/operations.js@@ -87,7 +87,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/printLoops.js b/common/hogvm/__tests__/__snapshots__/printLoops.jsindex ecc3a7157441..e2f9f92c09b9 100644--- a/common/hogvm/__tests__/__snapshots__/printLoops.js+++ b/common/hogvm/__tests__/__snapshots__/printLoops.js@@ -92,7 +92,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/printLoops2.js b/common/hogvm/__tests__/__snapshots__/printLoops2.jsindex 500e3040667b..0c4c3e2f66a5 100644--- a/common/hogvm/__tests__/__snapshots__/printLoops2.js+++ b/common/hogvm/__tests__/__snapshots__/printLoops2.js@@ -92,7 +92,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/properties.js b/common/hogvm/__tests__/__snapshots__/properties.jsindex 5af787feb5cf..1522ffd8a861 100644--- a/common/hogvm/__tests__/__snapshots__/properties.js+++ b/common/hogvm/__tests__/__snapshots__/properties.js@@ -42,7 +42,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/recursion.js b/common/hogvm/__tests__/__snapshots__/recursion.jsindex 1eb354a7e504..341f87fb5168 100644--- a/common/hogvm/__tests__/__snapshots__/recursion.js+++ b/common/hogvm/__tests__/__snapshots__/recursion.js@@ -32,7 +32,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/scope.js b/common/hogvm/__tests__/__snapshots__/scope.jsindex 3dee55c5a397..9bb9a54659ee 100644--- a/common/hogvm/__tests__/__snapshots__/scope.js+++ b/common/hogvm/__tests__/__snapshots__/scope.js@@ -37,7 +37,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/stl.js b/common/hogvm/__tests__/__snapshots__/stl.jsindex b0ee66536a00..6eb8a98ea828 100644--- a/common/hogvm/__tests__/__snapshots__/stl.js+++ b/common/hogvm/__tests__/__snapshots__/stl.js@@ -308,7 +308,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/strings.js b/common/hogvm/__tests__/__snapshots__/strings.jsindex 9ce51632b4bd..05cc27caec58 100644--- a/common/hogvm/__tests__/__snapshots__/strings.js+++ b/common/hogvm/__tests__/__snapshots__/strings.js@@ -95,7 +95,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/tuples.js b/common/hogvm/__tests__/__snapshots__/tuples.jsindex 562adb3a729f..ff62e9e92c83 100644--- a/common/hogvm/__tests__/__snapshots__/tuples.js+++ b/common/hogvm/__tests__/__snapshots__/tuples.js@@ -37,7 +37,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/typeof.js b/common/hogvm/__tests__/__snapshots__/typeof.jsindex e4287e026eb3..7de471942a1c 100644--- a/common/hogvm/__tests__/__snapshots__/typeof.js+++ b/common/hogvm/__tests__/__snapshots__/typeof.js@@ -56,7 +56,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/upvalues.js b/common/hogvm/__tests__/__snapshots__/upvalues.jsindex 39182158f6bf..2ac9256cc447 100644--- a/common/hogvm/__tests__/__snapshots__/upvalues.js+++ b/common/hogvm/__tests__/__snapshots__/upvalues.js@@ -32,7 +32,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/__snapshots__/variables.js b/common/hogvm/__tests__/__snapshots__/variables.jsindex c6cb97d354c5..136806c26797 100644--- a/common/hogvm/__tests__/__snapshots__/variables.js+++ b/common/hogvm/__tests__/__snapshots__/variables.js@@ -31,7 +31,7 @@ function __escapeString(value) {return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/common/hogvm/__tests__/escapedIdentifiers.hog b/common/hogvm/__tests__/escapedIdentifiers.hognew file mode 100644index 000000000000..cdeec22aada8--- /dev/null+++ b/common/hogvm/__tests__/escapedIdentifiers.hog@@ -0,0 +1,3 @@+// escape_identifier output (doubled inner backticks, escaped backslashes) must resolve as member access in the Hog runtime.+let obj := {'a`b': 10, 'with space': 20, 'a\\b': 30}+print(obj.`a``b`, obj.`with space`, obj.`a\\b`)diff --git a/common/hogvm/python/stl/print.py b/common/hogvm/python/stl/print.pyindex 29dd4fc7de07..4c8d44197ac9 100644--- a/common/hogvm/python/stl/print.py+++ b/common/hogvm/python/stl/print.py@@ -15,7 +15,8 @@ escape_chars_map = {"\\": "\\\\",}singlequote_escape_chars_map = {**escape_chars_map, "'": "\\'"}-backquote_escape_chars_map = {**escape_chars_map, "`": "\\`"}+# The HogQL/Hog parsers only accept a doubled backtick inside a quoted identifier, not a backslash-escaped one.+backquote_escape_chars_map = {**escape_chars_map, "`": "``"}# Copied from clickhouse_driver.util.escape_paramdiff --git a/common/hogvm/python/test/test_print.py b/common/hogvm/python/test/test_print.pynew file mode 100644index 000000000000..3b10cfdcdfae--- /dev/null+++ b/common/hogvm/python/test/test_print.py@@ -0,0 +1,28 @@+from parameterized import parameterized++from posthog.hogql import ast+from posthog.hogql.parser import parse_expr++from common.hogvm.python.stl.print import escape_identifier+++class TestEscapeIdentifier:+ def test_doubles_embedded_backtick(self):+ # The HogQL/Hog parsers only accept a doubled backtick inside a quoted identifier, not a backslash-escaped one.+ assert escape_identifier("a`b") == "`a``b`"++ @parameterized.expand(+ [+ ("plain", "normal_id"),+ ("space", "a b"),+ ("single_backtick", "`"),+ ("embedded_backtick", "a`b"),+ ("backslash", "a\\b"),+ ("tab", "a\tb"),+ ("backtick_and_backslash", "a`\\b"),+ ]+ )+ def test_round_trips_through_parser(self, _name: str, identifier: str) -> None:+ node = parse_expr(escape_identifier(identifier))+ assert isinstance(node, ast.Field)+ assert node.chain == [identifier]diff --git a/common/hogvm/typescript/src/__tests__/print.test.ts b/common/hogvm/typescript/src/__tests__/print.test.tsnew file mode 100644index 000000000000..2c873362c2d4--- /dev/null+++ b/common/hogvm/typescript/src/__tests__/print.test.ts@@ -0,0 +1,14 @@+import { escapeIdentifier } from '../stl/print'++describe('hogvm print', () => {+ test('escapeIdentifier doubles embedded backticks', () => {+ // The HogQL/Hog parsers only accept a doubled backtick inside a quoted identifier, not a backslash-escaped one.+ expect(escapeIdentifier('a`b')).toBe('`a``b`')+ expect(escapeIdentifier('`')).toBe('````')+ })++ test('escapeIdentifier leaves simple identifiers unquoted', () => {+ expect(escapeIdentifier('normal_id')).toBe('normal_id')+ expect(escapeIdentifier('a b')).toBe('`a b`')+ })+})diff --git a/common/hogvm/typescript/src/stl/print.ts b/common/hogvm/typescript/src/stl/print.tsindex fa89cafde5b5..0a53f8d6e906 100644--- a/common/hogvm/typescript/src/stl/print.ts+++ b/common/hogvm/typescript/src/stl/print.ts@@ -17,9 +17,10 @@ const singlequoteEscapeCharsMap: Record<string, string> = {"'": "\\'",}+// The HogQL/Hog parsers only accept a doubled backtick inside a quoted identifier, not a backslash-escaped one.const backquoteEscapeCharsMap: Record<string, string> = {...escapeCharsMap,- '`': '\\`',+ '`': '``',}export function escapeString(value: string): string {diff --git a/frontend/src/queries/utils.test.ts b/frontend/src/queries/utils.test.tsindex 6188c2da2b9d..587c34b7130b 100644--- a/frontend/src/queries/utils.test.ts+++ b/frontend/src/queries/utils.test.ts@@ -13,6 +13,7 @@ import {convertDataTableNodeToDataVisualizationNode,escapeDottedHogQLIdentifier,escapeHogQLString,+ escapePropertyAsHogQLIdentifier,hogql,supportsBarValueStacking,} from './utils'@@ -101,6 +102,43 @@ describe('escapeHogQLString', () => {})})+describe('escapePropertyAsHogQLIdentifier', () => {+ it('leaves simple identifiers unquoted', () => {+ expect(escapePropertyAsHogQLIdentifier('browser')).toEqual('browser')+ expect(escapePropertyAsHogQLIdentifier('$browser')).toEqual('$browser')+ })++ it('double-quotes identifiers with special characters but no double quote', () => {+ expect(escapePropertyAsHogQLIdentifier('order items')).toEqual('"order items"')+ })++ it('backtick-wraps identifiers containing a double quote', () => {+ expect(escapePropertyAsHogQLIdentifier('a"b')).toEqual('`a"b`')+ })++ it('doubles inner backticks when an identifier has both a double quote and a backtick', () => {+ // Backslash-escaping (`a"b\`c`) would be rejected by the HogQL parser; doubling round-trips.+ expect(escapePropertyAsHogQLIdentifier('a"b`c')).toEqual('`a"b``c`')+ })++ it('escapes backslashes so they survive the parser instead of forming an escape sequence', () => {+ expect(escapePropertyAsHogQLIdentifier('a\\b')).toEqual('"a\\\\b"')+ expect(escapePropertyAsHogQLIdentifier('end\\')).toEqual('"end\\\\"')+ // A backslash alongside a double quote takes the backtick branch and still escapes both.+ expect(escapePropertyAsHogQLIdentifier('a"\\b')).toEqual('`a"\\\\b`')+ // A backslash immediately before an inner backtick (which forces the backtick branch).+ expect(escapePropertyAsHogQLIdentifier('a"b\\`c')).toEqual('`a"b\\\\``c`')+ })++ it('escapes control characters instead of emitting them raw', () => {+ expect(escapePropertyAsHogQLIdentifier('a\tb')).toEqual('"a\\tb"')+ expect(escapePropertyAsHogQLIdentifier('a\nb')).toEqual('"a\\nb"')+ expect(escapePropertyAsHogQLIdentifier('a\bb')).toEqual('"a\\bb"')+ // An embedded double quote forces the backtick branch; the control char is still escaped.+ expect(escapePropertyAsHogQLIdentifier('a"\tb')).toEqual('`a"\\tb`')+ })+})+describe('escapeDottedHogQLIdentifier', () => {it('leaves simple dotted identifiers unquoted', () => {expect(escapeDottedHogQLIdentifier('demo.orders')).toEqual('demo.orders')diff --git a/frontend/src/queries/utils.ts b/frontend/src/queries/utils.tsindex ef4201669893..8e5d00e41024 100644--- a/frontend/src/queries/utils.ts+++ b/frontend/src/queries/utils.ts@@ -733,6 +733,19 @@ export function trimQuotes(identifier: string): string {return identifier}+// Mirrors escape_chars_map in posthog/hogql/escape_sql.py: backslash and control chars must be escaped so a quoted identifier round-trips losslessly through the HogQL parser.+const HOGQL_IDENTIFIER_ESCAPE_MAP: Record<string, string> = {+ '\b': '\\b',+ '\f': '\\f',+ '\r': '\\r',+ '\n': '\\n',+ '\t': '\\t',+ '\0': '\\0',+ '\x07': '\\a',+ '\v': '\\v',+ '\\': '\\\\',+}+/** Make sure the property key is wrapped in quotes if it contains any special characters. */export function escapePropertyAsHogQLIdentifier(identifier: string): string {if (identifier.match(/^[A-Za-z_$][A-Za-z0-9_$]*$/)) {@@ -742,7 +755,9 @@ export function escapePropertyAsHogQLIdentifier(identifier: string): string {if (isQuoted(identifier)) {return identifier // This identifier is already quoted}- return !identifier.includes('"') ? `"${identifier}"` : `\`${identifier}\``+ // Escape backslashes and control chars, then wrap; double an inner backtick (the parser rejects a backslash-escaped delimiter). The double-quote path needs no quote escaping since it is only taken when the identifier has no `"`.+ const escaped = Array.from(identifier, (c) => HOGQL_IDENTIFIER_ESCAPE_MAP[c] || c).join('')+ return !identifier.includes('"') ? `"${escaped}"` : `\`${escaped.replaceAll('`', '``')}\``}/** Quote each segment of a dotted HogQL reference independently. */diff --git a/posthog/cdp/test/__snapshots__/test_site_functions.ambr b/posthog/cdp/test/__snapshots__/test_site_functions.ambrindex a48a39292998..205342ca5928 100644--- a/posthog/cdp/test/__snapshots__/test_site_functions.ambr+++ b/posthog/cdp/test/__snapshots__/test_site_functions.ambr@@ -213,7 +213,7 @@return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;@@ -415,7 +415,7 @@return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;diff --git a/posthog/hogql/compiler/javascript_stl.py b/posthog/hogql/compiler/javascript_stl.pyindex 1cb9b2760049..2e11a00ecdcc 100644--- a/posthog/hogql/compiler/javascript_stl.py+++ b/posthog/hogql/compiler/javascript_stl.py@@ -871,7 +871,7 @@ function __escapeString(value) {"__escapeIdentifier": ["""function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\\b': '\\\\b', '\\f': '\\\\f', '\\r': '\\\\r', '\\n': '\\\\n', '\\t': '\\\\t', '\\0': '\\\\0', '\\v': '\\\\v', '\\\\': '\\\\\\\\', '`': '\\\\`' }+ const backquoteEscapeCharsMap = { '\\b': '\\\\b', '\\f': '\\\\f', '\\r': '\\\\r', '\\n': '\\\\n', '\\t': '\\\\t', '\\0': '\\\\0', '\\v': '\\\\v', '\\\\': '\\\\\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\\``;diff --git a/posthog/hogql/escape_sql.py b/posthog/hogql/escape_sql.pyindex 041e17388e09..ae7f19b071e6 100644--- a/posthog/hogql/escape_sql.py+++ b/posthog/hogql/escape_sql.py@@ -22,7 +22,8 @@ escape_chars_map = {"\\": "\\\\",}singlequote_escape_chars_map = {**escape_chars_map, "'": "\\'"}-backquote_escape_chars_map = {**escape_chars_map, "`": "\\`"}+# The HogQL/ClickHouse parsers only accept a doubled backtick inside a quoted identifier, not a backslash-escaped one.+backquote_escape_chars_map = {**escape_chars_map, "`": "``"}def safe_identifier(identifier: str) -> str:diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex d91d68db011b..0e3df7f60938 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -604,7 +604,7 @@ class TestPrinter(BaseTest):HogQLContext(team_id=self.team.pk),"hogql",),- "properties.`$browser with a \\` tick`",+ "properties.`$browser with a `` tick`",)self.assertEqual(self._expr(@@ -612,7 +612,7 @@ class TestPrinter(BaseTest):HogQLContext(team_id=self.team.pk),"hogql",),- "properties.`$browser \\\\with a \\n\\` tick`",+ "properties.`$browser \\\\with a \\n`` tick`",)# "dot NUMBER" means "tuple access" in clickhouse. To access strings properties, wrap them in `backquotes`self.assertEqual(diff --git a/posthog/hogql/printer/test/test_sql_injection_defenses.py b/posthog/hogql/printer/test/test_sql_injection_defenses.pyindex 8dbd76424919..5b2ec9fe37f2 100644--- a/posthog/hogql/printer/test/test_sql_injection_defenses.py+++ b/posthog/hogql/printer/test/test_sql_injection_defenses.py@@ -198,9 +198,9 @@ class TestEscapePreservationOnParserOutput(BaseTest):self.assertGreaterEqual(len(context.values), 1)def test_identifier_with_backtick_is_backtick_escaped(self):- # HogQL accepts backtick-quoted identifiers with doubled inner backticks; printer re-emits backtick-wrapped with the inner backtick backslash-escaped.+ # The printer doubles the inner backtick so the identifier round-trips back through the parser.sql, _ = _print(self, parse_select("SELECT 1 AS `weird``name` FROM events"))- self.assertIn("`weird\\`name`", sql)+ self.assertIn("`weird``name`", sql)def test_identifier_with_percent_rejected(self):# `%` is reserved for parameter placeholders downstream; every identifier escape path rejects it.diff --git a/posthog/hogql/test/test_escape_sql.py b/posthog/hogql/test/test_escape_sql.pyindex f7be337a365c..7eb63697113e 100644--- a/posthog/hogql/test/test_escape_sql.py+++ b/posthog/hogql/test/test_escape_sql.py@@ -1,7 +1,10 @@from datetime import datetime-from posthog.test.base import BaseTest+from posthog.test.base import BaseTest, ClickhouseTestMixin+from parameterized import parameterized++from posthog.hogql import astfrom posthog.hogql.errors import QueryError, ResolutionErrorfrom posthog.hogql.escape_sql import (escape_clickhouse_identifier,@@ -10,9 +13,24 @@ from posthog.hogql.escape_sql import (escape_hogql_string,escape_postgres_identifier,)+from posthog.hogql.parser import parse_expr+from posthog.clickhouse.client.execute import sync_executefrom posthog.models.utils import UUIDT+_ROUNDTRIP_IDENTIFIER_SAMPLES = [+ "back`tick",+ "a``b",+ "`leading",+ "trailing`",+ "``",+ "a\\b",+ "a\\`b",+ "`a\\`b`",+ "with space",+ "a.b.c",+]+class TestPrintString(BaseTest):def test_sanitize_hogql_identifier(self):@@ -25,7 +43,7 @@ class TestPrintString(BaseTest):self.assertEqual(escape_hogql_identifier("a.b.c"), "`a.b.c`")self.assertEqual(escape_hogql_identifier("a-b-c"), "`a-b-c`")self.assertEqual(escape_hogql_identifier("a#$#"), "`a#$#`")- self.assertEqual(escape_hogql_identifier("back`tick"), "`back\\`tick`")+ self.assertEqual(escape_hogql_identifier("back`tick"), "`back``tick`")self.assertEqual(escape_hogql_identifier("single'quote"), "`single'quote`")self.assertEqual(escape_hogql_identifier('double"quote'), '`double"quote`')self.assertEqual(@@ -43,7 +61,7 @@ class TestPrintString(BaseTest):self.assertEqual(escape_clickhouse_identifier("a.b.c"), "`a.b.c`")self.assertEqual(escape_clickhouse_identifier("a-b-c"), "`a-b-c`")self.assertEqual(escape_clickhouse_identifier("a#$#"), "`a#$#`")- self.assertEqual(escape_clickhouse_identifier("back`tick"), "`back\\`tick`")+ self.assertEqual(escape_clickhouse_identifier("back`tick"), "`back``tick`")self.assertEqual(escape_clickhouse_identifier("single'quote"), "`single'quote`")self.assertEqual(escape_clickhouse_identifier('double"quote'), '`double"quote`')self.assertEqual(@@ -51,6 +69,21 @@ class TestPrintString(BaseTest):"`other escapes: \\b \\f \\n \\t \\0 \\a \\v \\\\`",)+ @parameterized.expand(+ [+ (f"{label}-{backend}-{i}", escape_fn, backend, sample)+ for label, escape_fn in [("hogql", escape_hogql_identifier), ("clickhouse", escape_clickhouse_identifier)]+ for backend in ["rust-py", "cpp-json"]+ for i, sample in enumerate(_ROUNDTRIP_IDENTIFIER_SAMPLES)+ ]+ )+ def test_identifier_roundtrips_through_production_parser(self, _name, escape_fn, backend, identifier):+ # Round-trips through the real parsers, not the lenient parse_string_literal_text; the clickhouse case still parses via the HogQL parser (shared grammar), not ClickHouse itself.+ escaped = escape_fn(identifier)+ node = parse_expr(escaped, backend=backend)+ assert isinstance(node, ast.Field), f"{identifier!r} escaped to {escaped!r} did not parse to a Field"+ self.assertEqual(node.chain, [identifier], f"{identifier!r} escaped to {escaped!r} did not round-trip")+def test_sanitize_postgres_identifier(self):self.assertEqual(escape_postgres_identifier("a"), "a")self.assertEqual(escape_postgres_identifier("$browser"), '"$browser"')@@ -177,3 +210,13 @@ class TestPrintString(BaseTest):with self.assertRaises(ResolutionError) as context:escape_clickhouse_string({"a": 1, "b": 2}) # type: ignoreself.assertTrue("SQLValueEscaper has no method visit_dict" in str(context.exception))+++class TestClickHouseIdentifierExecution(ClickhouseTestMixin, BaseTest):+ @parameterized.expand([(f"sample-{i}", sample) for i, sample in enumerate(_ROUNDTRIP_IDENTIFIER_SAMPLES)])+ def test_escaped_identifier_round_trips_through_clickhouse(self, _name, identifier):+ # ClickHouse, not just the HogQL parser, is the real consumer of escape_clickhouse_identifier:+ # it must parse the escaped alias and report the original name back.+ escaped = escape_clickhouse_identifier(identifier)+ _, columns = sync_execute(f"SELECT 1 AS {escaped}", with_column_types=True)+ self.assertEqual(columns[0][0], identifier)diff --git a/posthog/models/test/__snapshots__/test_remote_config.ambr b/posthog/models/test/__snapshots__/test_remote_config.ambrindex 8395e00db046..050bcff4442f 100644--- a/posthog/models/test/__snapshots__/test_remote_config.ambr+++ b/posthog/models/test/__snapshots__/test_remote_config.ambr@@ -330,7 +330,7 @@return `'${value.split('').map((c) => singlequoteEscapeCharsMap[c] || c).join('')}'`;}function __escapeIdentifier(identifier) {- const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '\\`' }+ const backquoteEscapeCharsMap = { '\b': '\\b', '\f': '\\f', '\r': '\\r', '\n': '\\n', '\t': '\\t', '\0': '\\0', '\v': '\\v', '\\': '\\\\', '`': '``' }if (typeof identifier === 'number') return identifier.toString();if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(identifier)) return identifier;return `\`${identifier.split('').map((c) => backquoteEscapeCharsMap[c] || c).join('')}\``;