Skip to content

Commit

Permalink
Fix parens
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller committed Aug 9, 2024
1 parent f482895 commit d66a28f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
14 changes: 7 additions & 7 deletions runtime/metricsview/astexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (b *sqlExprBuilder) writeBinaryCondition(exprs []*Expression, op Operator)
if err != nil {
return err
}
b.writeString(")")
b.writeByte(')')
return nil
}

Expand Down Expand Up @@ -283,7 +283,7 @@ func (b *sqlExprBuilder) writeBinaryConditionInner(left, right *Expression, left
return fmt.Errorf("invalid binary condition operator %q", op)
}

b.writeString("(")
b.writeByte('(')

if leftOverride != "" {
b.writeParenthesizedString(leftOverride)
Expand All @@ -298,10 +298,10 @@ func (b *sqlExprBuilder) writeBinaryConditionInner(left, right *Expression, left
// "dim = NULL" should be written as "dim IS NULL"
// "dim != NULL" should be written as "dim IS NOT NULL"
if op == OperatorEq {
b.writeString(" IS NULL")
b.writeString(" IS NULL)")
return nil
} else if op == OperatorNeq {
b.writeString(" IS NOT NULL")
b.writeString(" IS NOT NULL)")
return nil
}
}
Expand All @@ -311,7 +311,7 @@ func (b *sqlExprBuilder) writeBinaryConditionInner(left, right *Expression, left
return err
}

b.writeString(")")
b.writeByte(')')

return nil
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func (b *sqlExprBuilder) writeILikeCondition(left, right *Expression, leftOverri
if b.ast.dialect.RequiresCastForLike() {
b.writeString("::TEXT")
}
b.writeString(")")
b.writeByte(')')

if not {
b.writeString(" NOT LIKE ")
Expand All @@ -380,7 +380,7 @@ func (b *sqlExprBuilder) writeILikeCondition(left, right *Expression, leftOverri
if b.ast.dialect.RequiresCastForLike() {
b.writeString("::TEXT")
}
b.writeString(")")
b.writeByte(')')
}

// When you have "dim NOT ILIKE <val>", then NULL values are always excluded. We need to explicitly include it.
Expand Down
6 changes: 4 additions & 2 deletions runtime/metricsview/astsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ func (b *sqlBuilder) writeSelect(n *SelectNode) error {
if wroteWhere {
b.out.WriteString(" AND (")
} else {
b.out.WriteString(" WHERE (")
b.out.WriteString(" WHERE ")
}
b.out.WriteString(n.Where.Expr)
b.out.WriteString(")")
if wroteWhere {
b.out.WriteString(")")
}
b.args = append(b.args, n.Where.Args...)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/pkg/metricssql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestCompile(t *testing.T) {
},
{
"select pub, dom, measure_0 as \"click rate\" from ad_bids_metrics where (pub is not null and dom is null) or (pub = '__default__')",
"SELECT (\"publisher\") AS \"pub\", (\"domain\") AS \"dom\", (count(*)) AS \"measure_0\" FROM \"ad_bids\" WHERE ((((\"publisher\") IS NOT NULL AND (\"domain\") IS NULL) OR (\"publisher\") = ?)) GROUP BY 1, 2",
"SELECT (\"publisher\") AS \"pub\", (\"domain\") AS \"dom\", (count(*)) AS \"measure_0\" FROM \"ad_bids\" WHERE ((((\"publisher\") IS NOT NULL) AND ((\"domain\") IS NULL)) OR ((\"publisher\") = ?)) GROUP BY 1, 2",
mv,
[]any{"__default__"},
},
Expand Down

0 comments on commit d66a28f

Please sign in to comment.