Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix square bracket handling in custom expressions (#82339)

Riley Thompsonc42cdf36122f
diff --git a/frontend/src/metabase/querying/expressions/diagnostics/diagnostics.unit.spec.ts b/frontend/src/metabase/querying/expressions/diagnostics/diagnostics.unit.spec.ts
index d358e15ec85..742b735731e 100644
--- a/frontend/src/metabase/querying/expressions/diagnostics/diagnostics.unit.spec.ts
+++ b/frontend/src/metabase/querying/expressions/diagnostics/diagnostics.unit.spec.ts
@@ -382,6 +382,13 @@ describe("diagnostics", () => {
it.each([`[`, `[]`])("reject missing field name for %s", (expression) => {
expect(err(expression)).toBe("Expected a field name");
});
+
+ it.each([`"]"`, `']'`, `concat("[", "]")`, `concat("[", [Total], "]")`])(
+ "should accept strings containing brackets, like %s (metabase#82328)",
+ (expression) => {
+ expect(err(expression)).toBeUndefined();
+ },
+ );
});
describe("bad tokens", () => {
diff --git a/frontend/src/metabase/querying/expressions/pratt/lexifier.unit.spec.ts b/frontend/src/metabase/querying/expressions/pratt/lexifier.unit.spec.ts
index 1c107f92a3c..eec1eca9a4a 100644
--- a/frontend/src/metabase/querying/expressions/pratt/lexifier.unit.spec.ts
+++ b/frontend/src/metabase/querying/expressions/pratt/lexifier.unit.spec.ts
@@ -328,6 +328,12 @@ describe("lexify", () => {
['"\\r\\n"', "\r\n"],
['"say \\"Hi\\""', 'say "Hi"'],
["'foo\\tbar'", "foo\tbar"],
+ [`"]"`, "]"],
+ [`']'`, "]"],
+ [`"["`, "["],
+ [`'['`, "["],
+ [`"[]"`, "[]"],
+ [`"foo ] bar"`, "foo ] bar"],
];
for (const [expression, value] of cases) {
@@ -398,6 +404,23 @@ describe("lexify", () => {
].map(asToken),
);
});
+
+ it("tokenizes strings containing brackets next to bracket identifiers (metabase#82328)", () => {
+ const tokens = lexify('concat("[", [My Column], "]")');
+ expect(tokens).toEqual(
+ [
+ { type: CALL, start: 0, text: "concat" },
+ { type: GROUP, start: 6, text: "(" },
+ { type: STRING, start: 7, text: '"["', value: "[" },
+ { type: COMMA, start: 10, text: "," },
+ { type: FIELD, start: 12, text: "[My Column]", value: "My Column" },
+ { type: COMMA, start: 23, text: "," },
+ { type: STRING, start: 25, text: '"]"', value: "]" },
+ { type: GROUP_CLOSE, start: 28, text: ")" },
+ { type: END_OF_INPUT, start: 29, text: "\n" },
+ ].map(asToken),
+ );
+ });
});
describe("identifiers", () => {
diff --git a/frontend/src/metabase/querying/expressions/tokenizer/tokens.ts b/frontend/src/metabase/querying/expressions/tokenizer/tokens.ts
index 538690c308a..bd87211608d 100644
--- a/frontend/src/metabase/querying/expressions/tokenizer/tokens.ts
+++ b/frontend/src/metabase/querying/expressions/tokenizer/tokens.ts
@@ -12,6 +12,9 @@ function char(char: string): number {
const BACKSLASH = char("\\");
const OPEN_BRACKET = char("[");
const CLOSE_BRACKET = char("]");
+const SINGLE_QUOTE = char("'");
+const DOUBLE_QUOTE = char('"');
+const STRING_QUOTES = new Set([SINGLE_QUOTE, DOUBLE_QUOTE]);
const NEW_LINE = char("\n");
const EOF = -1;
@@ -47,6 +50,12 @@ export const field = new ExternalTokenizer((input) => {
return;
}
+ // A quote starts a string literal so a `]` after it is part of the string, not
+ // the closing bracket of a field (#82328).
+ if (STRING_QUOTES.has(current)) {
+ return;
+ }
+
// We keep track of whether the token we are looking at was opened by a bracket.
const wasOpenedByBracket = current === OPEN_BRACKET;