Skip to content

fix: support aggregation with group-by on mixed tag and field columns#779

Open
oleksii-donets wants to merge 14 commits intodevelopmentfrom
feat/733-aggregation-on-multiple-columns
Open

fix: support aggregation with group-by on mixed tag and field columns#779
oleksii-donets wants to merge 14 commits intodevelopmentfrom
feat/733-aggregation-on-multiple-columns

Conversation

@oleksii-donets
Copy link
Contributor

@oleksii-donets oleksii-donets commented Mar 20, 2026

Applicable issues

Description of changes

Fix aggregation queries when group-by includes columns not present in the expressions list, when grouping by both tags and fields simultaneously, and when schema-defined columns are absent from actual data:

  • SqlQueryBuilder: Only include group-by columns in SQL SELECT when they appear in expressions, preventing column position mismatch in query results.
  • FluxQueryBuilder: Switch aggregation queries to use fieldsAsCols() pivot instead of field filtering, enabling group-by on field columns. Exclude group-by columns when selecting the count() target field to avoid Flux's "cannot aggregate columns that are part of the group key" error.
  • FluxQueryBuilder: Use _measurement as the column for count() instead of picking an arbitrary schema column. Previously, count() could pick a column (e.g. project_id) that was absent from the actual data in the queried time range, causing InfluxDB "column does not exist" errors. _measurement is always present after fieldsAsCols(), is a string type (Flux cannot aggregate time-type columns), and is never null.
  • Tests: Add shared aggregateWithFieldFilterAndGroupByTagAndField test to AbstractInfluxContainerTest, verifying both InfluxDB 2 and InfluxDB 3 engines handle field filtering with mixed tag+field group-by correctly.
  • Tests: Add mcp_analytics table to test configs and seed data WITHOUT project_id tag. New MissingColumnTests container tests verify count() works when schema-defined columns are absent from actual data.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

oleksii-donets and others added 9 commits March 20, 2026 09:37
…ueries (#768)

Replace strings.containsStr/hasPrefix/hasSuffix with regexp.compile + =~/!~
operators for CONTAINS, NOT_CONTAINS, STARTS_WITH, ENDS_WITH, and LIKE filters.
Regex is compiled into a separate variable before from() so InfluxDB can push
the filter down to the storage engine. Add container tests for all partial
search operators against both InfluxDB 2 and InfluxDB 3.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add (?i) flag to Flux regex patterns and use ILIKE instead of LIKE in
SQL queries for CONTAINS, NOT_CONTAINS, STARTS_WITH, ENDS_WITH, and
LIKE operators. Add case-insensitive container tests for both engines.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…nflux2 (#768)

Add new integration tests organized into @nested classes covering
filters ($eq, $ne, $or), pagination (limit/offset), and edge cases
(empty results, no-match count, expression aliases).

Fix InfluxEngine to return a default row with zeros for ungrouped
aggregation queries that produce empty results, aligning with
SQL-standard behavior and InfluxDB 3.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…r across sub-queries (#768)

The aggregation query builder was dropping the preamble (regex variable
declarations like _re0 = regexp.compile(...)) when combining sub-queries,
causing invalid Flux queries when using partial string filters (starts_with,
contains, etc.) with aggregations. Also made the regex counter shared across
all sub-queries to avoid variable name collisions.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Fix SqlQueryBuilder to only include expression columns in SELECT,
preventing position mismatch when group-by has extra columns not in
expressions. Fix FluxQueryBuilder to use fieldsAsCols pivot for
aggregation queries and exclude group-by columns when selecting the
count target field, enabling aggregation on field columns.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@ai-dial-actions
Copy link
Contributor

Dependency Review

The following issues were found:
  • ✅ 0 vulnerable package(s)
  • ✅ 0 package(s) with incompatible licenses
  • ✅ 0 package(s) with invalid SPDX license definitions
  • ⚠️ 2 package(s) with unknown licenses.
See the Details below.

License Issues

settings.gradle

PackageVersionLicenseIssue Type
com.gradle:common-custom-user-data-gradle-plugin2.1NullUnknown License
com.gradle:develocity-gradle-plugin4.3.1NullUnknown License

OpenSSF Scorecard

PackageVersionScoreDetails
maven/com.gradle:common-custom-user-data-gradle-plugin 2.1 UnknownUnknown
maven/com.gradle:develocity-gradle-plugin 4.3.1 UnknownUnknown

Scanned Files

  • settings.gradle

oleksii-donets and others added 2 commits March 23, 2026 10:14
After fieldsAsCols() pivot, tags are regular columns too, so they can
be used as count() targets. This reduces the chance of hitting a
"no eligible column" error when all fields are in the group key.
Exclude only _time (Flux cannot aggregate time-type columns) and
group-key columns.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…33-aggregation-on-multiple-columns

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@oleksii-donets oleksii-donets changed the base branch from development to feat/768-regex-pushdown-for-partial-search March 23, 2026 08:22
@ai-dial-actions
Copy link
Contributor

Dependency Review

The following issues were found:
  • ✅ 0 vulnerable package(s)
  • ✅ 0 package(s) with incompatible licenses
  • ✅ 0 package(s) with invalid SPDX license definitions
  • ⚠️ 2 package(s) with unknown licenses.
See the Details below.

License Issues

settings.gradle

PackageVersionLicenseIssue Type
com.gradle:common-custom-user-data-gradle-plugin2.1NullUnknown License
com.gradle:develocity-gradle-plugin4.3.1NullUnknown License

OpenSSF Scorecard

PackageVersionScoreDetails
maven/com.gradle:common-custom-user-data-gradle-plugin 2.1 UnknownUnknown
maven/com.gradle:develocity-gradle-plugin 4.3.1 UnknownUnknown

Scanned Files

  • settings.gradle

@ai-dial-actions
Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@ai-dial-actions
Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

count() previously picked an arbitrary schema column (e.g. project_id)
which may be absent from actual data in the queried time range, causing
InfluxDB "column does not exist" errors. Use _measurement instead — it
is always present after fieldsAsCols(), is a string type (Flux cannot
aggregate time-type columns), and is never null.

Add mcp_analytics container tests that seed data WITHOUT project_id tag
to verify count() works when schema-defined columns are absent.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@ai-dial-actions
Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Base automatically changed from feat/768-regex-pushdown-for-partial-search to development March 23, 2026 15:27
@oleksii-donets oleksii-donets dismissed KirylKurnosenka’s stale review March 23, 2026 15:27

The base branch was changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add MCP Analytics dashboard

4 participants