Skip to content

Commit 77fb8db

Browse files
authored
feat: add fill flux (#376)
1 parent b81b7ec commit 77fb8db

File tree

5 files changed

+245
-1
lines changed

5 files changed

+245
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* Add TruncateTimeColumnFlux [FluxDSL]
1111
* Add ArrayFromFlux [FluxDSL]
1212
* Add UnionFlux [FluxDSL]
13-
13+
1. [376](https://github.com/influxdata/influxdb-client-java/pull/376) Add FillFlux [FluxDSL]
14+
1415
### Bug Fixes
1516
1. [#372](https://github.com/influxdata/influxdb-client-java/pull/372): Redact the `Authorization` HTTP header from log
1617

flux-dsl/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,20 @@ Flux flux = Flux
253253
.from("telegraf")
254254
.duplicate("host", "server");
255255
```
256+
257+
### fill
258+
259+
Replaces all null values in input tables with a non-null value [[doc](http://bit.ly/flux-spec#fill)].
260+
- `column` - The column to fill. Defaults to `_value`. [string]
261+
- `value` The constant value to use in place of nulls. The type must match the type of the valueColumn. [object]
262+
- `usePrevious` If set, then assign the value set in the previous non-null row. Cannot be used with value. [boolean]
263+
264+
```java
265+
Flux flux = Flux
266+
.from("telegraf")
267+
.fill(0.0);
268+
```
269+
256270
### filter
257271

258272
Filters the results using an expression [[doc](http://bit.ly/flux-spec#filter)].

flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java

+29
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.influxdb.query.dsl.functions.DropFlux;
4848
import com.influxdb.query.dsl.functions.DuplicateFlux;
4949
import com.influxdb.query.dsl.functions.ExpressionFlux;
50+
import com.influxdb.query.dsl.functions.FillFlux;
5051
import com.influxdb.query.dsl.functions.FilterFlux;
5152
import com.influxdb.query.dsl.functions.FirstFlux;
5253
import com.influxdb.query.dsl.functions.FromFlux;
@@ -815,6 +816,34 @@ public final DuplicateFlux duplicate(@Nonnull final String column, @Nonnull fina
815816
return new DuplicateFlux(this).withColumn(column).withAs(as);
816817
}
817818

819+
/**
820+
* Replaces all null values in input tables with a non-null value.
821+
*
822+
* <h3>The parameters had to be defined by:</h3>
823+
* <ul>
824+
* <li>{@link FillFlux#withUsePrevious(Boolean)}</li>
825+
* <li>{@link FillFlux#withColumn(String)}</li>
826+
* <li>{@link FillFlux#withValue(Object)}</li>
827+
* </ul>
828+
*
829+
* @return {@link FillFlux}
830+
*/
831+
@Nonnull
832+
public final FillFlux fill() {
833+
return new FillFlux(this);
834+
}
835+
836+
/**
837+
* Replaces all null values in input tables with a non-null value.
838+
*
839+
* @param value The constant value to use in place of nulls. The type must match the type of the valueColumn.
840+
* @return {@link FillFlux}
841+
*/
842+
@Nonnull
843+
public final FillFlux fill(@Nonnull final Object value) {
844+
return new FillFlux(this).withValue(value);
845+
}
846+
818847
/**
819848
* Returns the first result of the query.
820849
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.query.dsl.functions;
23+
24+
import javax.annotation.Nonnull;
25+
26+
import com.influxdb.query.dsl.Flux;
27+
import com.influxdb.utils.Arguments;
28+
29+
/**
30+
* Replaces all null values in input tables with a non-null value.
31+
* <a href="http://bit.ly/flux-spec#fill">See SPEC</a>.
32+
*
33+
* <h3>Example</h3>
34+
* <pre>
35+
* Flux flux = Flux
36+
* .from("telegraf")
37+
* .fill();
38+
* </pre>
39+
*/
40+
public final class FillFlux extends AbstractParametrizedFlux {
41+
42+
public FillFlux(@Nonnull final Flux source) {
43+
super(source);
44+
}
45+
46+
@Nonnull
47+
@Override
48+
protected String operatorName() {
49+
return "fill";
50+
}
51+
52+
/**
53+
* @param column The column to fill. Defaults to "_value".
54+
* @return this
55+
*/
56+
@Nonnull
57+
public FillFlux withColumn(@Nonnull final String column) {
58+
59+
Arguments.checkNonEmpty(column, "column");
60+
61+
this.withPropertyValueEscaped("column", column);
62+
63+
return this;
64+
}
65+
66+
/**
67+
* @param value The constant value to use in place of nulls. The type must match the type of the valueColumn.
68+
* @return this
69+
*/
70+
@Nonnull
71+
public FillFlux withValue(@Nonnull final Object value) {
72+
Arguments.checkNotNull(value, "value");
73+
74+
if (value instanceof String) {
75+
this.withPropertyValueEscaped("value", (String) value);
76+
} else {
77+
this.withPropertyValue("value", value);
78+
}
79+
80+
return this;
81+
}
82+
83+
/**
84+
* @param usePrevious If set, then assign the value set in the previous non-null row. Cannot be used with value.
85+
* @return this
86+
*/
87+
@Nonnull
88+
public FillFlux withUsePrevious(@Nonnull final Boolean usePrevious) {
89+
Arguments.checkNotNull(usePrevious, "usePrevious");
90+
91+
this.withPropertyValue("usePrevious", usePrevious);
92+
93+
return this;
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.query.dsl.functions;
23+
24+
import java.time.Instant;
25+
26+
import com.influxdb.query.dsl.Flux;
27+
import org.assertj.core.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.platform.runner.JUnitPlatform;
30+
import org.junit.runner.RunWith;
31+
32+
/**
33+
* @author Jakub Bednar (bednar@github) (09/10/2018 13:27)
34+
*/
35+
@RunWith(JUnitPlatform.class)
36+
class FillFluxTest {
37+
38+
@Test
39+
void fillString() {
40+
41+
Flux flux = Flux
42+
.from("telegraf")
43+
.fill("foo");
44+
45+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
46+
"\t|> fill(value:\"foo\")");
47+
}
48+
49+
@Test
50+
void fillBoolean() {
51+
52+
Flux flux = Flux
53+
.from("telegraf")
54+
.fill(true);
55+
56+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
57+
"\t|> fill(value:true)");
58+
}
59+
60+
@Test
61+
void fillInt() {
62+
63+
Flux flux = Flux
64+
.from("telegraf")
65+
.fill(42);
66+
67+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
68+
"\t|> fill(value:42)");
69+
}
70+
71+
@Test
72+
void fillFloat() {
73+
74+
Flux flux = Flux
75+
.from("telegraf")
76+
.fill(42.0);
77+
78+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
79+
"\t|> fill(value:42.0)");
80+
}
81+
82+
@Test
83+
void fillTime() {
84+
85+
Flux flux = Flux
86+
.from("telegraf")
87+
.fill(Instant.ofEpochMilli(0));
88+
89+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
90+
"\t|> fill(value:1970-01-01T00:00:00.000000000Z)");
91+
}
92+
93+
@Test
94+
void fillPrevious() {
95+
96+
Flux flux = Flux
97+
.from("telegraf")
98+
.fill()
99+
.withColumn("other")
100+
.withUsePrevious(true);
101+
102+
Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" +
103+
"\t|> fill(column:\"other\", usePrevious:true)");
104+
}
105+
}

0 commit comments

Comments
 (0)