Skip to content

Commit

Permalink
feat: add as expression in query builder
Browse files Browse the repository at this point in the history
Signed-off-by: zhiheng123 <[email protected]>
  • Loading branch information
zhiheng123 committed Aug 29, 2024
1 parent ab3c943 commit 2be57fb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
55 changes: 55 additions & 0 deletions opengemini/query_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,58 @@ func TestQueryBuilderSelectWithWhereAndTimezone(t *testing.T) {

require.Equal(t, expectedQuery, query.Command)
}

func TestQueryBuilderSelectWithAsExpression(t *testing.T) {
qb := CreateQueryBuilder()

waterLevelField := NewFieldExpression("water_level")

locationCondition := NewComparisonCondition("location", Equals, "santa_monica")
startTimeCondition := NewComparisonCondition("time", GreaterThanOrEquals, "2019-08-18T00:00:00Z")
endTimeCondition := NewComparisonCondition("time", LessThanOrEquals, "2019-08-18T00:18:00Z")

finalCondition := NewCompositeCondition(And, locationCondition, startTimeCondition, endTimeCondition)

location, err := time.LoadLocation("America/Chicago")
require.NoError(t, err)

asWL := NewAsExpression("WL", waterLevelField)

query := qb.Select(asWL).
From("h2o_feet").
Where(finalCondition).
Timezone(location).
Build()

expectedQuery := `SELECT "water_level" AS "WL" FROM "h2o_feet" WHERE ("location" = 'santa_monica' AND "time" >= '2019-08-18T00:00:00Z' AND "time" <= '2019-08-18T00:18:00Z') TZ('America/Chicago')`

require.Equal(t, expectedQuery, query.Command)
}

func TestQueryBuilderSelectWithAggregate(t *testing.T) {
qb := CreateQueryBuilder()

waterLevelField := NewFieldExpression("water_level")
countWaterLevelField := NewFunctionExpression(FunctionCount, waterLevelField)

locationCondition := NewComparisonCondition("location", Equals, "santa_monica")
startTimeCondition := NewComparisonCondition("time", GreaterThanOrEquals, "2019-08-18T00:00:00Z")
endTimeCondition := NewComparisonCondition("time", LessThanOrEquals, "2019-08-18T00:18:00Z")

finalCondition := NewCompositeCondition(And, locationCondition, startTimeCondition, endTimeCondition)

location, err := time.LoadLocation("America/Chicago")
require.NoError(t, err)

asWL := NewAsExpression("WL", countWaterLevelField)

query := qb.Select(asWL).
From("h2o_feet").
Where(finalCondition).
Timezone(location).
Build()

expectedQuery := `SELECT COUNT("water_level") AS "WL" FROM "h2o_feet" WHERE ("location" = 'santa_monica' AND "time" >= '2019-08-18T00:00:00Z' AND "time" <= '2019-08-18T00:18:00Z') TZ('America/Chicago')`

require.Equal(t, expectedQuery, query.Command)
}
16 changes: 16 additions & 0 deletions opengemini/query_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ func NewFunctionExpression(function FunctionEnum, arguments ...Expression) *Func
}
}

type AsExpression struct {
Alias string
OriginExpr Expression
}

func (a *AsExpression) build() string {
return fmt.Sprintf("%s AS \"%s\"", a.OriginExpr.build(), a.Alias)
}

func NewAsExpression(alias string, expr Expression) *AsExpression {
return &AsExpression{
Alias : alias,
OriginExpr: expr,
}
}

type ArithmeticExpression struct {
Operator ArithmeticOperator
Operands []Expression
Expand Down

0 comments on commit 2be57fb

Please sign in to comment.