diff --git a/runtime/metricsview/astexpr.go b/runtime/metricsview/astexpr.go index 2af0b1a0fcd..d4cbb2b2370 100644 --- a/runtime/metricsview/astexpr.go +++ b/runtime/metricsview/astexpr.go @@ -248,7 +248,7 @@ func (b *sqlExprBuilder) writeBinaryCondition(exprs []*Expression, op Operator) if err != nil { return err } - b.writeString(")") + b.writeByte(')') return nil } @@ -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) @@ -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 } } @@ -311,7 +311,7 @@ func (b *sqlExprBuilder) writeBinaryConditionInner(left, right *Expression, left return err } - b.writeString(")") + b.writeByte(')') return nil } @@ -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 ") @@ -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 ", then NULL values are always excluded. We need to explicitly include it. diff --git a/runtime/metricsview/astsql.go b/runtime/metricsview/astsql.go index dbb4877d04b..dda8a31736f 100644 --- a/runtime/metricsview/astsql.go +++ b/runtime/metricsview/astsql.go @@ -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...) } diff --git a/runtime/pkg/metricssql/parser_test.go b/runtime/pkg/metricssql/parser_test.go index 35b5a3646e7..647c1ebefd4 100644 --- a/runtime/pkg/metricssql/parser_test.go +++ b/runtime/pkg/metricssql/parser_test.go @@ -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__"}, },