diff --git a/src/main/java/io/github/willena/influxql/ast/expr/Call.java b/src/main/java/io/github/willena/influxql/ast/expr/Call.java index f888485..e24dbff 100644 --- a/src/main/java/io/github/willena/influxql/ast/expr/Call.java +++ b/src/main/java/io/github/willena/influxql/ast/expr/Call.java @@ -32,7 +32,6 @@ private Call(Builder builder) { name = builder.name; args = builder.args; ensureDefined("name", name); - ensureDefined("args", args); } /** diff --git a/src/main/java/io/github/willena/influxql/ast/extra/FunctionFactory.java b/src/main/java/io/github/willena/influxql/ast/extra/FunctionFactory.java index 68fc41f..d8d0bc9 100644 --- a/src/main/java/io/github/willena/influxql/ast/extra/FunctionFactory.java +++ b/src/main/java/io/github/willena/influxql/ast/extra/FunctionFactory.java @@ -1965,8 +1965,10 @@ private Predictors() { * @return the call */ public static Call holtWinters(Call call, IntegerLiteral n, IntegerLiteral s) { - if (call.getArgs() != null - && (call.getArgs().size() != 1 || !(call.getArgs().get(0) instanceof VarRef))) { + if (call == null + || call.getArgs() == null + || call.getArgs().size() != 1 + || !(call.getArgs().get(0) instanceof VarRef)) { throw new IllegalArgumentException("First argument of function must be a field"); } return new Call.Builder().function(HOLT_WINTERS_NAME).withArguments(call, n, s).build(); @@ -1981,8 +1983,10 @@ public static Call holtWinters(Call call, IntegerLiteral n, IntegerLiteral s) { * @return the call */ public static Call holtWintersWithFit(Call call, IntegerLiteral n, IntegerLiteral s) { - if (call.getArgs() != null - && (call.getArgs().size() != 1 || !(call.getArgs().get(0) instanceof VarRef))) { + if (call == null + || call.getArgs() == null + || call.getArgs().size() != 1 + || !(call.getArgs().get(0) instanceof VarRef)) { throw new IllegalArgumentException("First argument of function must be a field"); } return new Call.Builder() diff --git a/src/main/java/io/github/willena/influxql/ast/extra/RetentionPolicy.java b/src/main/java/io/github/willena/influxql/ast/extra/RetentionPolicy.java index a95d581..8e1d2f3 100644 --- a/src/main/java/io/github/willena/influxql/ast/extra/RetentionPolicy.java +++ b/src/main/java/io/github/willena/influxql/ast/extra/RetentionPolicy.java @@ -120,7 +120,7 @@ public Builder replication(Integer retentionPolicyReplication) { * @param retentionPolicyName the {@code retentionPolicyName} to set * @return a reference to this Builder */ - public Builder withRetentionPolicyName(String retentionPolicyName) { + public Builder retentionPolicyName(String retentionPolicyName) { this.retentionPolicyName = retentionPolicyName; return this; } diff --git a/src/main/java/io/github/willena/influxql/ast/field/SortField.java b/src/main/java/io/github/willena/influxql/ast/field/SortField.java index 37ad277..2863f3d 100644 --- a/src/main/java/io/github/willena/influxql/ast/field/SortField.java +++ b/src/main/java/io/github/willena/influxql/ast/field/SortField.java @@ -57,12 +57,9 @@ public static SortField desc(String name) { @Override public String toString() { var buf = new StringBuilder(); - if (name != null && !name.isEmpty()) { - // TODO: not sure about that - // buf.append(name); - buf.append(quoteIdentifier(name)); - buf.append(" "); - } + buf.append(quoteIdentifier(name)); + buf.append(" "); + if (ascending) { buf.append("ASC"); } else { diff --git a/src/main/java/io/github/willena/influxql/parser/InfluxqlAstAdapter.java b/src/main/java/io/github/willena/influxql/parser/InfluxqlAstAdapter.java index eb27330..e9dc5da 100644 --- a/src/main/java/io/github/willena/influxql/parser/InfluxqlAstAdapter.java +++ b/src/main/java/io/github/willena/influxql/parser/InfluxqlAstAdapter.java @@ -358,7 +358,7 @@ public CreateDatabaseStatement visitCreate_database_stmt( RetentionPolicy rp = new RetentionPolicy.Builder() - .withRetentionPolicyName( + .retentionPolicyName( Optional.ofNullable(ctx.retention_policy_name()) .map(e -> e.IDENTIFIER().getText()) .map(InfluxqlAstAdapter::trimIdentifierQuotes) @@ -974,16 +974,6 @@ public SelectStatement visitSelect_stmt(InfluxqlParser.Select_stmtContext ctx) { .build(); } - @Override - public Node visitAlias(InfluxqlParser.AliasContext ctx) { - return null; - } - - @Override - public Node visitBack_ref(InfluxqlParser.Back_refContext ctx) { - return null; - } - @Override public Dimension visitDimension(InfluxqlParser.DimensionContext ctx) { return Dimension.of(visitExpression(ctx.expression())); diff --git a/src/test/java/io/github/willena/influxql/ast/QueryTest.java b/src/test/java/io/github/willena/influxql/ast/QueryTest.java index 0b760e3..9abb379 100644 --- a/src/test/java/io/github/willena/influxql/ast/QueryTest.java +++ b/src/test/java/io/github/willena/influxql/ast/QueryTest.java @@ -232,7 +232,8 @@ void parse_multiple_statements() throws URISyntaxException, IOException { Map.entry( "SHOW TAG VALUES EXACT CARDINALITY WITH KEY = \"myTagKey\"", "SHOW TAG VALUES EXACT CARDINALITY WITH KEY = myTagKey"), - Map.entry("SHOW USERS", "SHOW USERS")); + Map.entry("SHOW USERS", "SHOW USERS"), + Map.entry("SHOW SHARDS", "SHOW SHARDS")); /** * Test statements from (List.of(VarRef.of("uuu")))) + .withArguments(StringLiteral.of("ddd")) + .build() + .toString()); } } diff --git a/src/test/java/io/github/willena/influxql/ast/expr/VarRefTest.java b/src/test/java/io/github/willena/influxql/ast/expr/VarRefTest.java index fbbe99e..867efcf 100644 --- a/src/test/java/io/github/willena/influxql/ast/expr/VarRefTest.java +++ b/src/test/java/io/github/willena/influxql/ast/expr/VarRefTest.java @@ -31,5 +31,6 @@ void of() { @Test void ofType() { assertEquals("tutu::string", VarRef.of("tutu", DataType.STRING).toString()); + assertEquals("tutu", VarRef.of("tutu", DataType.UNKNOWN).toString()); } } diff --git a/src/test/java/io/github/willena/influxql/ast/expr/literal/ListLiteralTest.java b/src/test/java/io/github/willena/influxql/ast/expr/literal/ListLiteralTest.java index 4ff92b7..a21f2d8 100644 --- a/src/test/java/io/github/willena/influxql/ast/expr/literal/ListLiteralTest.java +++ b/src/test/java/io/github/willena/influxql/ast/expr/literal/ListLiteralTest.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import java.time.Duration; import java.util.List; import org.junit.jupiter.api.Test; @@ -33,4 +34,22 @@ void listTest() { List.of(StringLiteral.of("A"), StringLiteral.of("B")), ListLiteral.of(List.of(StringLiteral.of("A"), StringLiteral.of("B"))).getValue()); } + + @Test + void listOfDuration() { + assertEquals( + "(1d, 1h)", ListLiteral.of(Duration.ofDays(1), Duration.ofHours(1)).toString()); + } + + @Test + void listOfNumber() { + assertEquals("(1.0, 2.0)", ListLiteral.of(1, 2).toString()); + } + + @Test + void listOfLiteral() { + assertEquals( + "('A', 'B')", + ListLiteral.of(StringLiteral.of("A"), StringLiteral.of("B")).toString()); + } } diff --git a/src/test/java/io/github/willena/influxql/ast/expr/literal/NullLiteralTest.java b/src/test/java/io/github/willena/influxql/ast/expr/literal/NullLiteralTest.java new file mode 100644 index 0000000..e8501bf --- /dev/null +++ b/src/test/java/io/github/willena/influxql/ast/expr/literal/NullLiteralTest.java @@ -0,0 +1,32 @@ +/* + * InfluxQL Java package + * Copyright 2024 Guillaume VILLENA also known as "Willena" on GitHub + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.willena.influxql.ast.expr.literal; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class NullLiteralTest { + + @Test + void of() { + NullLiteral nullLiteral = NullLiteral.of(); + assertEquals("null", nullLiteral.toString()); + assertNull(nullLiteral.getValue()); + } +} diff --git a/src/test/java/io/github/willena/influxql/ast/extra/FunctionFactoryTest.java b/src/test/java/io/github/willena/influxql/ast/extra/FunctionFactoryTest.java index 2bf106b..662d273 100644 --- a/src/test/java/io/github/willena/influxql/ast/extra/FunctionFactoryTest.java +++ b/src/test/java/io/github/willena/influxql/ast/extra/FunctionFactoryTest.java @@ -365,6 +365,15 @@ void transformerAtan2() { FunctionFactory.Selectors.sample( VarRef.of("oo"), IntegerLiteral.of(1))) .toString()); + + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Transformations.atan2( + FunctionFactory.Selectors.first(VarRef.of("oo")), + FunctionFactory.Selectors.sample( + VarRef.of("oo"), IntegerLiteral.of(1))) + .toString()); } @Test @@ -473,6 +482,14 @@ void transformerDerivative() { VarRef.of("oo"), IntegerLiteral.of(1)), DurationLiteral.of(Duration.ofHours(1))) .toString()); + + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Transformations.derivative( + FunctionFactory.Selectors.sample( + VarRef.of("oo"), IntegerLiteral.of(1))) + .toString()); } @Test @@ -545,6 +562,13 @@ void transformerElapsed() { VarRef.of("oo"), IntegerLiteral.of(1)), DurationLiteral.of(Duration.ofHours(1))) .toString()); + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Transformations.elapsed( + FunctionFactory.Selectors.sample( + VarRef.of("oo"), IntegerLiteral.of(1))) + .toString()); } @Test @@ -746,6 +770,13 @@ void transformerNonNegativeDerivative() { VarRef.of("oo"), IntegerLiteral.of(1)), DurationLiteral.of(Duration.ofHours(1))) .toString()); + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Transformations.nonNegativeDerivative( + FunctionFactory.Selectors.sample( + VarRef.of("oo"), IntegerLiteral.of(1))) + .toString()); } @Test @@ -882,6 +913,28 @@ void predictorsHoltWinters() { FunctionFactory.Aggregations.count(Wildcard.wildcard()), IntegerLiteral.of(1), IntegerLiteral.of(1))); + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Predictors.holtWinters( + new Call.Builder().function("tutu").build(), + IntegerLiteral.of(1), + IntegerLiteral.of(1))); + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Predictors.holtWinters( + null, IntegerLiteral.of(1), IntegerLiteral.of(1))); + assertThrows( + IllegalArgumentException.class, + () -> + FunctionFactory.Predictors.holtWinters( + new Call.Builder() + .function("tutu") + .withArguments(NumberLiteral.of(1), NumberLiteral.of(2)) + .build(), + IntegerLiteral.of(1), + IntegerLiteral.of(1))); assertEquals( "HOLT_WINTERS(COUNT(rrr), 1, 1)", FunctionFactory.Predictors.holtWinters( diff --git a/src/test/java/io/github/willena/influxql/ast/extra/RetentionPolicyTest.java b/src/test/java/io/github/willena/influxql/ast/extra/RetentionPolicyTest.java index 74717c7..be3c08b 100644 --- a/src/test/java/io/github/willena/influxql/ast/extra/RetentionPolicyTest.java +++ b/src/test/java/io/github/willena/influxql/ast/extra/RetentionPolicyTest.java @@ -17,7 +17,7 @@ package io.github.willena.influxql.ast.extra; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import java.time.Duration; import org.junit.jupiter.api.Test; @@ -28,7 +28,7 @@ void retentionPolicy() { RetentionPolicy rp = new RetentionPolicy.Builder() .duration(Duration.ofSeconds(1)) - .withRetentionPolicyName("test") + .retentionPolicyName("test") .shardDuration(Duration.ofHours(1)) .replication(10) .build(); @@ -38,4 +38,13 @@ void retentionPolicy() { assertEquals(Duration.ofHours(1), rp.getRetentionPolicyShardGroupDuration()); assertEquals(10, rp.getRetentionPolicyReplication()); } + + @Test + void isEmpty() { + assertTrue(new RetentionPolicy.Builder().build().isEmpty()); + assertFalse(new RetentionPolicy.Builder().retentionPolicyName("nn").build().isEmpty()); + assertFalse(new RetentionPolicy.Builder().duration(Duration.ZERO).build().isEmpty()); + assertFalse(new RetentionPolicy.Builder().shardDuration(Duration.ZERO).build().isEmpty()); + assertFalse(new RetentionPolicy.Builder().replication(2).build().isEmpty()); + } } diff --git a/src/test/java/io/github/willena/influxql/ast/field/FieldTest.java b/src/test/java/io/github/willena/influxql/ast/field/FieldTest.java index 5d5ba91..fc7f454 100644 --- a/src/test/java/io/github/willena/influxql/ast/field/FieldTest.java +++ b/src/test/java/io/github/willena/influxql/ast/field/FieldTest.java @@ -27,6 +27,14 @@ class FieldTest { @Test void testField() { assertEquals("fieldName", Field.field("fieldName").toString()); + assertEquals("fieldName", Field.field("fieldName").toString()); + assertEquals( + "fieldName", + new Field.Builder() + .withExpr(VarRef.of("fieldName")) + .withAlias("") + .build() + .toString()); assertEquals("fieldName::integer", Field.field("fieldName", DataType.INTEGER).toString()); }