Skip to content

Commit c96d5f4

Browse files
committed
limit union
1 parent fde71d2 commit c96d5f4

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

indexer

16.1 KB
Binary file not shown.

internal/storage/clickhouse.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,24 @@ func (c *ClickHouseConnector) buildUnionQuery(table, columns string, qf QueryFil
612612
toQuery += " WHERE to_address = '" + strings.ToLower(qf.WalletAddress) + "'"
613613
}
614614

615+
// Apply ORDER BY to both queries for consistent results
616+
if qf.SortBy != "" {
617+
fromQuery += fmt.Sprintf(" ORDER BY %s %s", qf.SortBy, qf.SortOrder)
618+
toQuery += fmt.Sprintf(" ORDER BY %s %s", qf.SortBy, qf.SortOrder)
619+
}
620+
621+
// Apply LIMIT to each individual query to avoid loading too much data
622+
// We use a higher limit to ensure we get enough results after UNION
623+
individualLimit := qf.Limit * 2 // Double the limit to account for potential duplicates
624+
if qf.Page >= 0 && qf.Limit > 0 {
625+
offset := qf.Page * qf.Limit
626+
fromQuery += fmt.Sprintf(" LIMIT %d OFFSET %d", individualLimit, offset)
627+
toQuery += fmt.Sprintf(" LIMIT %d OFFSET %d", individualLimit, offset)
628+
} else if qf.Limit > 0 {
629+
fromQuery += fmt.Sprintf(" LIMIT %d", individualLimit)
630+
toQuery += fmt.Sprintf(" LIMIT %d", individualLimit)
631+
}
632+
615633
// Combine with UNION
616634
unionQuery := fmt.Sprintf("(%s) UNION ALL (%s)", fromQuery, toQuery)
617635

@@ -631,17 +649,30 @@ func (c *ClickHouseConnector) addPostQueryClauses(query string, qf QueryFilter)
631649
}
632650
}
633651

634-
// Add ORDER BY clause
635-
if qf.SortBy != "" {
636-
query += fmt.Sprintf(" ORDER BY %s %s", qf.SortBy, qf.SortOrder)
637-
}
652+
// For UNION queries, ORDER BY and LIMIT are already applied to individual queries
653+
// For standard queries, apply ORDER BY and LIMIT
654+
if !strings.Contains(query, "UNION ALL") {
655+
// Add ORDER BY clause
656+
if qf.SortBy != "" {
657+
query += fmt.Sprintf(" ORDER BY %s %s", qf.SortBy, qf.SortOrder)
658+
}
638659

639-
// Add limit clause
640-
if qf.Page >= 0 && qf.Limit > 0 {
641-
offset := qf.Page * qf.Limit
642-
query += fmt.Sprintf(" LIMIT %d OFFSET %d", qf.Limit, offset)
643-
} else if qf.Limit > 0 {
644-
query += fmt.Sprintf(" LIMIT %d", qf.Limit)
660+
// Add limit clause
661+
if qf.Page >= 0 && qf.Limit > 0 {
662+
offset := qf.Page * qf.Limit
663+
query += fmt.Sprintf(" LIMIT %d OFFSET %d", qf.Limit, offset)
664+
} else if qf.Limit > 0 {
665+
query += fmt.Sprintf(" LIMIT %d", qf.Limit)
666+
}
667+
} else {
668+
// For UNION queries, we need to apply final LIMIT after the UNION
669+
// This ensures we get exactly the requested number of results
670+
if qf.Page >= 0 && qf.Limit > 0 {
671+
offset := qf.Page * qf.Limit
672+
query = fmt.Sprintf("SELECT * FROM (%s) LIMIT %d OFFSET %d", query, qf.Limit, offset)
673+
} else if qf.Limit > 0 {
674+
query = fmt.Sprintf("SELECT * FROM (%s) LIMIT %d", query, qf.Limit)
675+
}
645676
}
646677

647678
// Add settings at the very end

0 commit comments

Comments
 (0)