Skip to content

Commit

Permalink
CTEs as pointers (#533)
Browse files Browse the repository at this point in the history
I think CTEs should be pointers just like other nodes in the AST.


@trzysiek  please take a look
  • Loading branch information
nablaone authored Jul 16, 2024
1 parent fae3e38 commit b5a5e9d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions quesma/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (q *Query) CopyAggregationFields(qwa Query) {
q.SelectCommand.OrderBy = make([]OrderByExpr, len(qwa.SelectCommand.OrderBy))
copy(q.SelectCommand.OrderBy, qwa.SelectCommand.OrderBy)

q.SelectCommand.CTEs = make([]SelectCommand, len(qwa.SelectCommand.CTEs))
q.SelectCommand.CTEs = make([]*SelectCommand, len(qwa.SelectCommand.CTEs))
copy(q.SelectCommand.CTEs, qwa.SelectCommand.CTEs)

q.Aggregators = make([]Aggregator, len(qwa.Aggregators))
Expand Down Expand Up @@ -125,7 +125,7 @@ func (q *Query) NewSelectExprWithRowNumber(selectFields []Expr, groupByFields []
"ROW_NUMBER", nil, groupByFields, orderByExpr,
), RowNumberColumnName))

return *NewSelectCommand(selectFields, nil, nil, q.SelectCommand.FromClause, whereClause, []Expr{}, 0, 0, false, []SelectCommand{})
return *NewSelectCommand(selectFields, nil, nil, q.SelectCommand.FromClause, whereClause, []Expr{}, 0, 0, false, []*SelectCommand{})
}

// Aggregator is always initialized as "empty", so with SplitOverHowManyFields == 0, Keyed == false, Filters == false.
Expand Down
4 changes: 2 additions & 2 deletions quesma/model/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type SelectCommand struct {
Limit int // LIMIT clause, noLimit (0) means no limit
SampleLimit int // LIMIT, but before grouping, 0 means no limit

CTEs []SelectCommand // Common Table Expressions, so these parts of query: WITH cte_1 AS SELECT ..., cte_2 AS SELECT ...
CTEs []*SelectCommand // Common Table Expressions, so these parts of query: WITH cte_1 AS SELECT ..., cte_2 AS SELECT ...
}

func NewSelectCommand(columns, groupBy []Expr, orderBy []OrderByExpr, from, where Expr, limitBy []Expr,
limit, sampleLimit int, isDistinct bool, CTEs []SelectCommand) *SelectCommand {
limit, sampleLimit int, isDistinct bool, CTEs []*SelectCommand) *SelectCommand {
return &SelectCommand{
IsDistinct: isDistinct,

Expand Down
6 changes: 3 additions & 3 deletions quesma/optimize/trunc_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ func (v *truncateDateVisitor) VisitSelectCommand(e model.SelectCommand) interfac
whereClause = e.WhereClause.Accept(v).(model.Expr)
}

var ctes []model.SelectCommand
var ctes []*model.SelectCommand
if e.CTEs != nil {
ctes = make([]model.SelectCommand, 0)
ctes = make([]*model.SelectCommand, 0)
for _, cte := range e.CTEs {
ctes = append(ctes, cte.Accept(v).(model.SelectCommand))
ctes = append(ctes, cte.Accept(v).(*model.SelectCommand))
}
}

Expand Down
2 changes: 1 addition & 1 deletion quesma/queryparser/aggregation_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func (cw *ClickhouseQueryTranslator) parseAggregation(prevAggr *aggrQueryBuilder
// we can reduce nr of ORDER BYs in CTEs. Last 2 seem to be always enough. Proper ordering is done anyway in the outer SELECT.
cte.SelectCommand.OrderBy = cte.SelectCommand.OrderBy[len(cte.SelectCommand.OrderBy)-2:]
}
currentAggr.SelectCommand.CTEs = append(currentAggr.SelectCommand.CTEs, cte.SelectCommand)
currentAggr.SelectCommand.CTEs = append(currentAggr.SelectCommand.CTEs, &cte.SelectCommand)
}

// TODO what happens if there's all: filters, range, and subaggregations at current level?
Expand Down
2 changes: 1 addition & 1 deletion quesma/queryparser/query_util/query_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func BuildHitsQuery(ctx context.Context, tableName string, fieldName string, que
}
return &model.Query{
SelectCommand: *model.NewSelectCommand([]model.Expr{col}, nil, query.OrderBy, model.NewTableRef(tableName),
query.WhereClause, []model.Expr{}, applySizeLimit(ctx, limit), 0, false, []model.SelectCommand{}),
query.WhereClause, []model.Expr{}, applySizeLimit(ctx, limit), 0, false, []*model.SelectCommand{}),
TableName: tableName,
}
}
Expand Down

0 comments on commit b5a5e9d

Please sign in to comment.