Skip to content

Commit 4b008bb

Browse files
authored
[PLAT-396] allow dict optimization when lookup dim is in select clause (#8740)
* allow dict optimization when lookup dim is in select clause * parse validation * simplify * lint
1 parent fcd48ae commit 4b008bb

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

runtime/metricsview/ast.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type FieldNode struct {
110110
Expr string
111111
Unnest bool
112112
TreatNullAs string // only used for measures
113+
LookupMeta *lookupMeta
113114
}
114115

115116
// ExprNode represents an expression for a WHERE clause.
@@ -118,6 +119,13 @@ type ExprNode struct {
118119
Args []any
119120
}
120121

122+
type lookupMeta struct {
123+
table string
124+
keyExpr string
125+
keyCol string
126+
valueCol string
127+
}
128+
121129
// And returns a new node that is the AND of the current node and the given expression.
122130
func (n *ExprNode) And(expr string, args []any) *ExprNode {
123131
if expr == "" {
@@ -220,11 +228,28 @@ func NewAST(mv *runtimev1.MetricsViewSpec, sec MetricsViewSecurity, qry *Query,
220228
return nil, fmt.Errorf("failed to compile dimension %q expression: %w", dim.Name, err)
221229
}
222230

231+
var lkMeta *lookupMeta
232+
if dim.LookupTable != "" {
233+
var keyExpr string
234+
if dim.Column != "" {
235+
keyExpr = ast.Dialect.EscapeIdentifier(dim.Column)
236+
} else {
237+
keyExpr = dim.Expression
238+
}
239+
lkMeta = &lookupMeta{
240+
table: dim.LookupTable,
241+
keyExpr: keyExpr,
242+
keyCol: dim.LookupKeyColumn,
243+
valueCol: dim.LookupValueColumn,
244+
}
245+
}
246+
223247
f := FieldNode{
224248
Name: dim.Name,
225249
DisplayName: dim.DisplayName,
226250
Expr: expr,
227251
Unnest: dim.Unnest,
252+
LookupMeta: lkMeta,
228253
}
229254

230255
if dim.Unnest {

runtime/metricsview/astexpr.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ func (b *sqlExprBuilder) sqlForName(name string) (expr string, unnest bool, look
677677
if f.Name == name {
678678
// Note that we return "false" even though it may be an unnest dimension because it will already have been unnested since it's one of the dimensions included in the query.
679679
// So we can filter against it as if it's a normal dimension.
680-
return f.Expr, false, nil, nil
680+
return f.Expr, false, f.LookupMeta, nil
681681
}
682682
}
683683

@@ -692,19 +692,13 @@ func (b *sqlExprBuilder) sqlForName(name string) (expr string, unnest bool, look
692692
return "", false, nil, fmt.Errorf("invalid dimension reference %q: %w", name, err)
693693
}
694694

695-
if dim.Unnest && dim.LookupTable != "" {
696-
return "", false, nil, fmt.Errorf("dimension %q is unnested and also has a lookup. This is not supported", name)
697-
}
698-
699695
var lm *lookupMeta
700696
if dim.LookupTable != "" {
701697
var keyExpr string
702698
if dim.Column != "" {
703699
keyExpr = b.ast.Dialect.EscapeIdentifier(dim.Column)
704-
} else if dim.Expression != "" {
705-
keyExpr = dim.Expression
706700
} else {
707-
return "", false, nil, fmt.Errorf("dimension %q has a lookup table but no column or expression defined", name)
701+
keyExpr = dim.Expression
708702
}
709703
lm = &lookupMeta{
710704
table: dim.LookupTable,
@@ -754,13 +748,6 @@ func convertLikeExpressionToRegexExpression(like *Expression) (*Expression, erro
754748
return &Expression{Value: pattern}, nil
755749
}
756750

757-
type lookupMeta struct {
758-
table string
759-
keyExpr string
760-
keyCol string
761-
valueCol string
762-
}
763-
764751
// skipMetricsViewSecurity implements the MetricsViewSecurity interface in a way that allows all access.
765752
type skipMetricsViewSecurity struct{}
766753

runtime/parser/parse_metrics_view.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ func (p *Parser) parseMetricsView(node *Node) error {
379379
if strings.Contains(dim.Expression, "dictGet") {
380380
return fmt.Errorf("dictGet expression and lookup fields cannot be used together")
381381
}
382+
if dim.Unnest {
383+
return fmt.Errorf("unnest cannot be used with lookup fields")
384+
}
382385
}
383386

384387
// Validate the dimension name is unique

0 commit comments

Comments
 (0)