Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
5dda93a0
Unverified
Commit
5dda93a0
authored
8 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Don't allow field names outside of aggregations
parent
a4ae98dc
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/lib/expressions/parser.js
+33
-29
33 additions, 29 deletions
frontend/src/metabase/lib/expressions/parser.js
with
33 additions
and
29 deletions
frontend/src/metabase/lib/expressions/parser.js
+
33
−
29
View file @
5dda93a0
...
...
@@ -64,8 +64,8 @@ class ExpressionsParser extends Parser {
let
$
=
this
;
// an expression without aggregations in it
$
.
RULE
(
"
expression
"
,
function
(
allow
Aggregation
s
=
false
)
{
return
$
.
SUBRULE
(
$
.
additionExpression
,
[
allow
Aggregation
s
])
$
.
RULE
(
"
expression
"
,
function
(
outside
Aggregation
=
false
)
{
return
$
.
SUBRULE
(
$
.
additionExpression
,
[
outside
Aggregation
])
});
// an expression with aggregations in it
...
...
@@ -76,11 +76,11 @@ class ExpressionsParser extends Parser {
// Lowest precedence thus it is first in the rule chain
// The precedence of binary expressions is determined by
// how far down the Parse Tree the binary expression appears.
$
.
RULE
(
"
additionExpression
"
,
(
allow
Aggregation
s
)
=>
{
let
value
=
$
.
SUBRULE
(
$
.
multiplicationExpression
,
[
allow
Aggregation
s
]);
$
.
RULE
(
"
additionExpression
"
,
(
outside
Aggregation
)
=>
{
let
value
=
$
.
SUBRULE
(
$
.
multiplicationExpression
,
[
outside
Aggregation
]);
$
.
MANY
(()
=>
{
const
op
=
$
.
CONSUME
(
AdditiveOperator
);
const
rhsVal
=
$
.
SUBRULE2
(
$
.
multiplicationExpression
,
[
allow
Aggregation
s
]);
const
rhsVal
=
$
.
SUBRULE2
(
$
.
multiplicationExpression
,
[
outside
Aggregation
]);
if
(
Array
.
isArray
(
value
)
&&
value
[
0
]
===
op
.
image
)
{
value
.
push
(
rhsVal
);
...
...
@@ -91,11 +91,11 @@ class ExpressionsParser extends Parser {
return
value
});
$
.
RULE
(
"
multiplicationExpression
"
,
(
allow
Aggregation
s
)
=>
{
let
value
=
$
.
SUBRULE
(
$
.
atomicExpression
,
[
allow
Aggregation
s
]);
$
.
RULE
(
"
multiplicationExpression
"
,
(
outside
Aggregation
)
=>
{
let
value
=
$
.
SUBRULE
(
$
.
atomicExpression
,
[
outside
Aggregation
]);
$
.
MANY
(()
=>
{
const
op
=
$
.
CONSUME
(
MultiplicativeOperator
);
const
rhsVal
=
$
.
SUBRULE2
(
$
.
atomicExpression
,
[
allow
Aggregation
s
]);
const
rhsVal
=
$
.
SUBRULE2
(
$
.
atomicExpression
,
[
outside
Aggregation
]);
if
(
Array
.
isArray
(
value
)
&&
value
[
0
]
===
op
.
image
)
{
value
.
push
(
rhsVal
);
...
...
@@ -106,7 +106,7 @@ class ExpressionsParser extends Parser {
return
value
});
$
.
RULE
(
"
aggregationExpression
"
,
(
allow
Aggregation
s
)
=>
{
$
.
RULE
(
"
aggregationExpression
"
,
(
outside
Aggregation
)
=>
{
const
agg
=
$
.
CONSUME
(
Aggregation
).
image
;
let
value
=
[
aggregationsMap
.
get
(
agg
)]
$
.
CONSUME
(
LParen
);
...
...
@@ -130,20 +130,22 @@ class ExpressionsParser extends Parser {
return
[
"
field-id
"
,
this
.
getFieldIdForName
(
fieldName
)];
});
$
.
RULE
(
"
atomicExpression
"
,
(
allow
Aggregation
s
)
=>
{
$
.
RULE
(
"
atomicExpression
"
,
(
outside
Aggregation
)
=>
{
return
$
.
OR
([
{
GATE
:
()
=>
allowAggregations
,
ALT
:
()
=>
$
.
SUBRULE
(
$
.
aggregationExpression
,
[
false
])
},
{
ALT
:
()
=>
$
.
SUBRULE
(
$
.
parenthesisExpression
,
[
allowAggregations
])
},
{
ALT
:
()
=>
$
.
SUBRULE
(
$
.
fieldExpression
,
[
allowAggregations
])
},
// aggregations not allowed inside other aggregations
{
GATE
:
()
=>
outsideAggregation
,
ALT
:
()
=>
$
.
SUBRULE
(
$
.
aggregationExpression
,
[
false
])
},
// fields not allowed outside aggregations
{
GATE
:
()
=>
!
outsideAggregation
,
ALT
:
()
=>
$
.
SUBRULE
(
$
.
fieldExpression
)
},
{
ALT
:
()
=>
$
.
SUBRULE
(
$
.
parenthesisExpression
,
[
outsideAggregation
])
},
{
ALT
:
()
=>
parseFloat
(
$
.
CONSUME
(
NumberLiteral
).
image
)
}
],
"
a number or field name
"
);
});
$
.
RULE
(
"
parenthesisExpression
"
,
(
allow
Aggregation
s
)
=>
{
$
.
RULE
(
"
parenthesisExpression
"
,
(
outside
Aggregation
)
=>
{
let
expValue
;
$
.
CONSUME
(
LParen
);
expValue
=
$
.
SUBRULE
(
$
.
expression
,
[
allow
Aggregation
s
]);
expValue
=
$
.
SUBRULE
(
$
.
expression
,
[
outside
Aggregation
]);
$
.
CONSUME
(
RParen
);
return
expValue
...
...
@@ -210,6 +212,10 @@ export function suggest(source, { index = source.length, fields } = {}) {
for
(
const
suggestion
of
syntacticSuggestions
)
{
const
{
nextTokenType
,
ruleStack
}
=
suggestion
;
// no nesting of aggregations or field references outside of aggregations
// we have a predicate in the grammar to prevent nested aggregations but chevrotain
// doesn't support predicates in content-assist mode, so we need this extra check
const
outsideAggregation
=
ruleStack
.
slice
(
0
,
-
1
).
indexOf
(
"
aggregationExpression
"
)
<
0
;
if
(
nextTokenType
===
MultiplicativeOperator
||
nextTokenType
===
AdditiveOperator
)
{
let
tokens
=
getSubTokenTypes
(
nextTokenType
);
...
...
@@ -238,25 +244,23 @@ export function suggest(source, { index = source.length, fields } = {}) {
postfixTrim
:
/^
\s
+/
});
}
else
if
(
nextTokenType
===
Identifier
||
nextTokenType
===
StringLiteral
)
{
finalSuggestions
.
push
(...
fields
.
map
(
field
=>
({
type
:
"
fields
"
,
name
:
field
.
display_name
,
text
:
/^
\w
+$/
.
test
(
field
.
display_name
)
?
field
.
display_name
+
"
"
:
// need a space to terminate identifier
JSON
.
stringify
(
field
.
display_name
),
prefixTrim
:
/
\w
+$/
,
postfixTrim
:
/^
\w
+
\s
*/
})))
if
(
!
outsideAggregation
)
{
finalSuggestions
.
push
(...
fields
.
map
(
field
=>
({
type
:
"
fields
"
,
name
:
field
.
display_name
,
text
:
/^
\w
+$/
.
test
(
field
.
display_name
)
?
field
.
display_name
+
"
"
:
// need a space to terminate identifier
JSON
.
stringify
(
field
.
display_name
),
prefixTrim
:
/
\w
+$/
,
postfixTrim
:
/^
\w
+
\s
*/
})))
}
}
else
if
(
nextTokenType
===
Aggregation
)
{
// no nesting of aggregations
// we have a predicate in the grammar to prevent nested aggregations but chevrotain
// doesn't support predicates in content-assist mode, so we need this extra check
if
(
ruleStack
.
slice
(
0
,
-
1
).
indexOf
(
"
aggregationExpression
"
)
<
0
)
{
if
(
outsideAggregation
)
{
let
tokens
=
getSubTokenTypes
(
nextTokenType
);
finalSuggestions
.
push
(...
tokens
.
map
(
token
=>
{
let
text
=
getTokenSource
(
token
);
let
arity
=
AGGREGATION_ARITY
.
get
(
text
);
console
.
log
(
"
arity
"
,
arity
,
text
)
return
{
type
:
"
aggregations
"
,
name
:
text
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment