Skip to content

Commit

Permalink
Simplified function delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Oct 10, 2018
1 parent cb21c10 commit db3f0bc
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 108 deletions.
6 changes: 4 additions & 2 deletions flux-dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ Flux flux = Flux
```java
Flux flux = Flux
.from("telegraf")
.rename("{col}_new");
.rename("\"{col}_new\"");
```

### sample
Expand Down Expand Up @@ -582,7 +582,9 @@ The To operation takes data from a stream and writes it to a bucket [[doc](http:
- `tagColumns` - The tag columns of the output. **Default:** All columns of type string, excluding all value columns and the `_field` column if present. [array of strings]
- `fieldFn` - Function that takes a record from the input table and returns an object. For each record from the input table fieldFn returns on object that maps output field key to output value. **Default:** `(r) => ({ [r._field]: r._value })` [function(record) object]
```java

Flux flux = Flux
.from("telegraf")
.to("my-bucket", "my-org");
```

### toBool
Expand Down
53 changes: 53 additions & 0 deletions flux-dsl/src/main/java/org/influxdata/flux/Flux.java
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,59 @@ public final Flux withPropertyValue(@Nonnull final String propertyName, @Nullabl
return this;
}

/**
* Add named function to current function.
*
* <pre>
* Flux flux = Flux
* .from("telegraf")
* .drop()
* .withFunction("fn", "col =~ free*");
* </pre>
*
* @param functionName name in Flux query
* @param function defined function
* @return a current function
*/
@Nonnull
public final Flux withFunction(@Nonnull final String functionName, @Nullable final Object function) {

Arguments.checkNonEmpty(functionName, "functionName");

this.functionsParameters.putFunctionValue(functionName, function);

return this;
}

/**
* Add named function to current function.
*
* <pre>
* Map&lt;String, Object&gt; parameters = new HashMap&lt;&gt;();
* parameters.put("function", "r._value * 10");
*
* Flux flux = Flux
* .from("telegraf")
* .range(-12L, ChronoUnit.HOURS)
* .map()
* .withFunctionNamed("fn: (r)", "function");
* </pre>
*
* @param functionName name in Flux query
* @param namedProperty name in named properties
* @return a current function
*/
@Nonnull
public final Flux withFunctionNamed(@Nonnull final String functionName, @Nonnull final String namedProperty) {

Arguments.checkNonEmpty(functionName, "functionName");
Arguments.checkNonEmpty(namedProperty, "namedProperty");

this.functionsParameters.putFunctionNamed(functionName, namedProperty);

return this;
}

/**
* Add string property value to current function that will be quoted (value =&gt; "value").
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,6 @@ public void appendActual(@Nonnull final Map<String, Object> parameters, @Nonnull
@Nonnull
protected abstract String operatorName();

/**
* For value property it is ": ", but for function it is "=&gt;".
*
* @param operatorName function name
* @return property value delimiter
* @see AbstractParametrizedFlux#propertyDelimiter(String)
*/
@Nonnull
protected String propertyDelimiter(@Nonnull final String operatorName) {
return ": ";
}

/**
* Possibility to customize function.
*
Expand Down Expand Up @@ -124,7 +112,7 @@ private boolean appendParameterTo(@Nonnull final String operatorName,
// n: 5
operator
.append(operatorName)
.append(propertyDelimiter(operatorName))
.append(functionsParameters.getDelimiter(operatorName))
.append(propertyValue);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,8 @@ public DropFlux withFunction(@Nonnull final String function) {

Arguments.checkNonEmpty(function, "Function");

this.withPropertyValue("fn: (col)", function);
this.withFunction("fn: (col)", function);

return this;
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {

switch (operatorName) {
case "fn: (col)":
return " => ";

default:
return super.propertyDelimiter(operatorName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ protected String operatorName() {
return "filter";
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {
return " => ";
}

/**
* @param restrictions filter restrictions
* @return this
Expand All @@ -84,7 +78,7 @@ public FilterFlux withRestrictions(@Nonnull final Restrictions restrictions) {

Arguments.checkNotNull(restrictions, "Restrictions are required");

this.withPropertyValue("fn: (r)", restrictions);
this.withFunction("fn: (r)", restrictions);

return this;
}
Expand Down
13 changes: 0 additions & 13 deletions flux-dsl/src/main/java/org/influxdata/flux/functions/JoinFlux.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,6 @@ protected void beforeAppendOperatorName(@Nonnull final StringBuilder operator,
});
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {

switch (operatorName) {
case "fn: (tables)":
return " => ";

default:
return super.propertyDelimiter(operatorName);
}
}

/**
* Map of table to join. Currently only two tables are allowed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,8 @@ public KeepFlux withFunction(@Nonnull final String function) {

Arguments.checkNonEmpty(function, "Function");

this.withPropertyValue("fn: (col)", function);
this.withFunction("fn: (col)", function);

return this;
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {

switch (operatorName) {
case "fn: (col)":
return " => ";

default:
return super.propertyDelimiter(operatorName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ protected String operatorName() {
return "map";
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {
return " => ";
}

/**
* @param function The function for map row of table. Example: "r._value * r._value".
* @return this
Expand All @@ -90,7 +84,7 @@ public MapFlux withFunction(@Nonnull final String function) {

Arguments.checkNonEmpty(function, "Function");

this.withPropertyValue("fn: (r)", function);
this.withFunction("fn: (r)", function);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
*
* Flux flux = Flux
* .from("telegraf")
* .rename("{col}_new");
* .rename("\"{col}_new\"");
* </pre>
*
* @author Jakub Bednar (bednar@github) (02/08/2018 11:48)
Expand Down Expand Up @@ -97,21 +97,8 @@ public RenameFlux withFunction(@Nonnull final String function) {

Arguments.checkNonEmpty(function, "Function");

this.withPropertyValueEscaped("fn: (col)", function);
this.withFunction("fn: (col)", function);

return this;
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {

switch (operatorName) {
case "fn: (col)":
return " => ";

default:
return super.propertyDelimiter(operatorName);
}
}
}
22 changes: 8 additions & 14 deletions flux-dsl/src/main/java/org/influxdata/flux/functions/ToFlux.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
* <li><b>fieldFn</b> - Function that takes a record from the input table and returns an object.</li>
* </ul>
*
* <h3>Example</h3>
* <pre>
* Flux flux = Flux
* .from("telegraf")
* .to("my-bucket", "my-org");
* </pre>
*
* @author Jakub Bednar (10/10/2018 07:58)
*/
public final class ToFlux extends AbstractParametrizedFlux {
Expand Down Expand Up @@ -193,21 +200,8 @@ public ToFlux withFieldFunction(@Nonnull final String fieldFn) {

Arguments.checkNonEmpty(fieldFn, "fieldFn");

this.withPropertyValue("fieldFn: (r)", fieldFn);
this.withFunction("fieldFn: (r)", fieldFn);

return this;
}

@Nonnull
@Override
protected String propertyDelimiter(@Nonnull final String operatorName) {

switch (operatorName) {
case "fieldFn: (r)":
return " => ";

default:
return super.propertyDelimiter(operatorName);
}
}
}
Loading

0 comments on commit db3f0bc

Please sign in to comment.