diff --git a/CHANGELOG.md b/CHANGELOG.md index 99802656239..c8a0d2bcb41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ * Add TruncateTimeColumnFlux [FluxDSL] * Add ArrayFromFlux [FluxDSL] * Add UnionFlux [FluxDSL] - +1. [...](https://github.com/influxdata/influxdb-client-java/pull/...) Add FillFlux [FluxDSL] + ### Bug Fixes 1. [#372](https://github.com/influxdata/influxdb-client-java/pull/372): Redact the `Authorization` HTTP header from log diff --git a/flux-dsl/README.md b/flux-dsl/README.md index b550b0f215e..db84f462015 100644 --- a/flux-dsl/README.md +++ b/flux-dsl/README.md @@ -253,6 +253,20 @@ Flux flux = Flux .from("telegraf") .duplicate("host", "server"); ``` + +### fill + +Replaces all null values in input tables with a non-null value [[doc](http://bit.ly/flux-spec#fill)]. +- `column` - The column to fill. Defaults to `_value`. [string] +- `value` The constant value to use in place of nulls. The type must match the type of the valueColumn. [object] +- `usePrevious` If set, then assign the value set in the previous non-null row. Cannot be used with value. [boolean] + +```java +Flux flux = Flux + .from("telegraf") + .fill(0.0); +``` + ### filter Filters the results using an expression [[doc](http://bit.ly/flux-spec#filter)]. diff --git a/flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java b/flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java index 876c0fb2474..e620b888e50 100644 --- a/flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java +++ b/flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java @@ -47,6 +47,7 @@ import com.influxdb.query.dsl.functions.DropFlux; import com.influxdb.query.dsl.functions.DuplicateFlux; import com.influxdb.query.dsl.functions.ExpressionFlux; +import com.influxdb.query.dsl.functions.FillFlux; import com.influxdb.query.dsl.functions.FilterFlux; import com.influxdb.query.dsl.functions.FirstFlux; import com.influxdb.query.dsl.functions.FromFlux; @@ -815,6 +816,34 @@ public final DuplicateFlux duplicate(@Nonnull final String column, @Nonnull fina return new DuplicateFlux(this).withColumn(column).withAs(as); } + /** + * Replaces all null values in input tables with a non-null value. + * + *
+ * Flux flux = Flux + * .from("telegraf") + * .fill(); + *+ */ +public final class FillFlux extends AbstractParametrizedFlux { + + public FillFlux(@Nonnull final Flux source) { + super(source); + } + + @Nonnull + @Override + protected String operatorName() { + return "fill"; + } + + /** + * @param column The column to fill. Defaults to "_value". + * @return this + */ + @Nonnull + public FillFlux withColumn(@Nonnull final String column) { + + Arguments.checkNonEmpty(column, "column"); + + this.withPropertyValueEscaped("column", column); + + return this; + } + + /** + * @param value The constant value to use in place of nulls. The type must match the type of the valueColumn. + * @return this + */ + @Nonnull + public FillFlux withValue(@Nonnull final Object value) { + Arguments.checkNotNull(value, "value"); + + if (value instanceof String) { + this.withPropertyValueEscaped("value", (String) value); + } else { + this.withPropertyValue("value", value); + } + + return this; + } + + /** + * @param usePrevious If set, then assign the value set in the previous non-null row. Cannot be used with value. + * @return this + */ + @Nonnull + public FillFlux withUsePrevious(@Nonnull final Boolean usePrevious) { + Arguments.checkNotNull(usePrevious, "usePrevious"); + + this.withPropertyValue("usePrevious", usePrevious); + + return this; + } +} diff --git a/flux-dsl/src/test/java/com/influxdb/query/dsl/functions/FillFluxTest.java b/flux-dsl/src/test/java/com/influxdb/query/dsl/functions/FillFluxTest.java new file mode 100644 index 00000000000..60d9564fa76 --- /dev/null +++ b/flux-dsl/src/test/java/com/influxdb/query/dsl/functions/FillFluxTest.java @@ -0,0 +1,105 @@ +/* + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.influxdb.query.dsl.functions; + +import java.time.Instant; + +import com.influxdb.query.dsl.Flux; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +/** + * @author Jakub Bednar (bednar@github) (09/10/2018 13:27) + */ +@RunWith(JUnitPlatform.class) +class FillFluxTest { + + @Test + void fillString() { + + Flux flux = Flux + .from("telegraf") + .fill("foo"); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(value:\"foo\")"); + } + + @Test + void fillBoolean() { + + Flux flux = Flux + .from("telegraf") + .fill(true); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(value:true)"); + } + + @Test + void fillInt() { + + Flux flux = Flux + .from("telegraf") + .fill(42); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(value:42)"); + } + + @Test + void fillFloat() { + + Flux flux = Flux + .from("telegraf") + .fill(42.0); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(value:42.0)"); + } + + @Test + void fillTime() { + + Flux flux = Flux + .from("telegraf") + .fill(Instant.ofEpochMilli(0)); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(value:1970-01-01T00:00:00.000000000Z)"); + } + + @Test + void fillPrevious() { + + Flux flux = Flux + .from("telegraf") + .fill() + .withColumn("other") + .withUsePrevious(true); + + Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\")\n" + + "\t|> fill(column:\"other\", usePrevious:true)"); + } +}