Skip to content

fix: chaining multiple condition in LINQ query #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Features
1. [#528](https://github.com/influxdata/influxdb-client-csharp/pull/528): Add HttpClient as a part of InfluxDBClientOptions

### Bug Fixes
1. [#555](https://github.com/influxdata/influxdb-client-csharp/pull/555): Chaining multiple conditions in LINQ queries

### Dependencies
Update dependencies:

Expand Down
21 changes: 21 additions & 0 deletions Client.Linq.Test/ItInfluxDBQueryableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ orderby s.Timestamp
Assert.AreEqual(1, sensors.Count);
}

[Test]
public void QueryExampleNestedOr()
{
// https://github.com/influxdata/influxdb-client-csharp/issues/481
var query = (from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
// ReSharper disable All
where (((((s.SensorId == "id-1") || (s.SensorId == "id-no-5")) || (s.SensorId == "id-no-8")) ||
(s.SensorId == "id-no-10")) || (s.SensorId == "id-no-12"))
// ReSharper restore All
where s.Value > 12
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
select s)
.Take(2)
.Skip(2);

var sensors = query.ToList();

Assert.AreEqual(1, sensors.Count);
}

[Test]
public void QueryExampleCount()
{
Expand Down
10 changes: 8 additions & 2 deletions Client.Linq/Internal/QueryAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,18 @@ internal void AddLimitTailOffset(string limitOffsetAssignment)

internal void AddFilterByTags(string filter)
{
_filterByTags.Add(filter);
if (!string.IsNullOrEmpty(filter))
{
_filterByTags.Add(filter);
}
}

internal void AddFilterByFields(string filter)
{
_filterByFields.Add(filter);
if (!string.IsNullOrEmpty(filter))
{
_filterByFields.Add(filter);
}
}

internal void AddSubQueries(QueryAggregator aggregator)
Expand Down
64 changes: 36 additions & 28 deletions Client.Linq/Internal/QueryExpressionTreeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,62 +331,70 @@ internal static void NormalizeExpressions(List<IExpressionPart> parts)
var indexes = Enumerable.Range(0, parts.Count)
.Where(i => parts[i] is BinaryOperator)
.ToList();
var eliminators = new List<Tuple<int, Action>>();

foreach (var index in indexes)
{
// "( and )"
if (index >= 1 && parts[index - 1] is LeftParenthesis && parts[index + 1] is RightParenthesis)
{
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);

NormalizeExpressions(parts);
return;
eliminators.Add(Tuple.Create<int, Action>(1, () =>
{
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);
}));
continue;
}

// "( timestamp > )"
if (index >= 2 && parts[index - 2] is LeftParenthesis && parts[index + 1] is RightParenthesis)
{
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);
parts.RemoveAt(index - 2);

NormalizeExpressions(parts);
return;
eliminators.Add(Tuple.Create<int, Action>(1, () =>
{
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);
parts.RemoveAt(index - 2);
}));
continue;
}

// "( < timestamp )"
if (index >= 1 && parts[index - 1] is LeftParenthesis && parts[index + 2] is RightParenthesis)
{
parts.RemoveAt(index + 2);
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);

NormalizeExpressions(parts);
return;
eliminators.Add(Tuple.Create<int, Action>(10, () =>
{
parts.RemoveAt(index + 2);
parts.RemoveAt(index + 1);
parts.RemoveAt(index);
parts.RemoveAt(index - 1);
}));
continue;
}

// "( or (r["sensor_id"] != p4))"
if (index >= 1 && parts[index - 1] is LeftParenthesis && parts[index + 1] is LeftParenthesis)
{
parts.RemoveAt(index);

NormalizeExpressions(parts);
return;
eliminators.Add(Tuple.Create<int, Action>(1, () => { parts.RemoveAt(index); }));
continue;
}

// "(r["sensor_id"] != p4) or )"
if (index >= 1 && parts[index - 1] is RightParenthesis && parts[index + 1] is RightParenthesis)
{
parts.RemoveAt(index);

NormalizeExpressions(parts);
return;
eliminators.Add(Tuple.Create<int, Action>(1, () => { parts.RemoveAt(index); }));
continue;
}
}

if (eliminators.Count > 0)
{
eliminators.Sort((e1, e2) => e2.Item1.CompareTo(e1.Item1)); // descending order
eliminators[0].Item2();
NormalizeExpressions(parts);
return;
}
}

// Parenthesis
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy