Skip to content

Commit

Permalink
feat: add fill flux (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy2003 authored Jul 16, 2022
1 parent b81b7ec commit 77fb8db
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Add TruncateTimeColumnFlux [FluxDSL]
* Add ArrayFromFlux [FluxDSL]
* Add UnionFlux [FluxDSL]

1. [376](https://github.com/influxdata/influxdb-client-java/pull/376) Add FillFlux [FluxDSL]

### Bug Fixes
1. [#372](https://github.com/influxdata/influxdb-client-java/pull/372): Redact the `Authorization` HTTP header from log

Expand Down
14 changes: 14 additions & 0 deletions flux-dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)].
Expand Down
29 changes: 29 additions & 0 deletions flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
* <h3>The parameters had to be defined by:</h3>
* <ul>
* <li>{@link FillFlux#withUsePrevious(Boolean)}</li>
* <li>{@link FillFlux#withColumn(String)}</li>
* <li>{@link FillFlux#withValue(Object)}</li>
* </ul>
*
* @return {@link FillFlux}
*/
@Nonnull
public final FillFlux fill() {
return new FillFlux(this);
}

/**
* Replaces all null values in input tables with a non-null value.
*
* @param value The constant value to use in place of nulls. The type must match the type of the valueColumn.
* @return {@link FillFlux}
*/
@Nonnull
public final FillFlux fill(@Nonnull final Object value) {
return new FillFlux(this).withValue(value);
}

/**
* Returns the first result of the query.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 javax.annotation.Nonnull;

import com.influxdb.query.dsl.Flux;
import com.influxdb.utils.Arguments;

/**
* Replaces all null values in input tables with a non-null value.
* <a href="http://bit.ly/flux-spec#fill">See SPEC</a>.
*
* <h3>Example</h3>
* <pre>
* Flux flux = Flux
* .from("telegraf")
* .fill();
* </pre>
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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)");
}
}

0 comments on commit 77fb8db

Please sign in to comment.