This is an automated email from the ASF dual-hosted git repository. hui pushed a commit to branch lmh/fixHaving in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit aadf0511d09aea5717ac310d42090564f08c8b8a Author: Minghui Liu <[email protected]> AuthorDate: Tue Feb 7 10:51:50 2023 +0800 add UT --- .../db/mpp/plan/expression/ExpressionFactory.java | 103 +++++++++++++++++++++ .../mpp/plan/analyze/ExpressionAnalyzerTest.java | 83 +++++++++++++++++ 2 files changed, 186 insertions(+) diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/ExpressionFactory.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/ExpressionFactory.java new file mode 100644 index 0000000000..170e21db1a --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/ExpressionFactory.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iotdb.db.mpp.plan.expression; + +import org.apache.iotdb.common.rpc.thrift.TAggregationType; +import org.apache.iotdb.commons.exception.IllegalPathException; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.db.mpp.plan.expression.binary.AdditionExpression; +import org.apache.iotdb.db.mpp.plan.expression.binary.GreaterThanExpression; +import org.apache.iotdb.db.mpp.plan.expression.binary.LogicAndExpression; +import org.apache.iotdb.db.mpp.plan.expression.binary.LogicOrExpression; +import org.apache.iotdb.db.mpp.plan.expression.leaf.ConstantOperand; +import org.apache.iotdb.db.mpp.plan.expression.leaf.TimeSeriesOperand; +import org.apache.iotdb.db.mpp.plan.expression.leaf.TimestampOperand; +import org.apache.iotdb.db.mpp.plan.expression.multi.FunctionExpression; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; + +public class ExpressionFactory { + + public static PartialPath path(String pathStr) throws IllegalPathException { + return new PartialPath(pathStr); + } + + public static TimeSeriesOperand timeSeries(String pathStr) throws IllegalPathException { + PartialPath path = new PartialPath(pathStr); + return new TimeSeriesOperand(path); + } + + public static ConstantOperand constant(TSDataType dataType, String valueString) { + return new ConstantOperand(dataType, valueString); + } + + public static ConstantOperand intValue(String valueString) { + return new ConstantOperand(TSDataType.INT32, valueString); + } + + public static TimestampOperand time() { + return new TimestampOperand(); + } + + public static FunctionExpression function( + String functionName, + LinkedHashMap<String, String> functionAttributes, + Expression... inputExpression) { + return new FunctionExpression(functionName, functionAttributes, Arrays.asList(inputExpression)); + } + + public static FunctionExpression function(String functionName, Expression... inputExpression) { + return new FunctionExpression( + functionName, new LinkedHashMap<>(), Arrays.asList(inputExpression)); + } + + public static FunctionExpression count(Expression inputExpression) { + return new FunctionExpression( + TAggregationType.COUNT.toString(), + new LinkedHashMap<>(), + Collections.singletonList(inputExpression)); + } + + public static FunctionExpression sum(Expression inputExpression) { + return new FunctionExpression( + TAggregationType.SUM.toString(), + new LinkedHashMap<>(), + Collections.singletonList(inputExpression)); + } + + public static LogicAndExpression and(Expression leftExpression, Expression rightExpression) { + return new LogicAndExpression(leftExpression, rightExpression); + } + + public static LogicOrExpression or(Expression leftExpression, Expression rightExpression) { + return new LogicOrExpression(leftExpression, rightExpression); + } + + public static AdditionExpression add(Expression leftExpression, Expression rightExpression) { + return new AdditionExpression(leftExpression, rightExpression); + } + + public static GreaterThanExpression gt(Expression leftExpression, Expression rightExpression) { + return new GreaterThanExpression(leftExpression, rightExpression); + } +} diff --git a/server/src/test/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzerTest.java b/server/src/test/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzerTest.java new file mode 100644 index 0000000000..464a5d6378 --- /dev/null +++ b/server/src/test/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzerTest.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iotdb.db.mpp.plan.analyze; + +import org.apache.iotdb.commons.exception.IllegalPathException; +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.path.PathPatternTree; +import org.apache.iotdb.db.mpp.common.schematree.ISchemaTree; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.and; +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.count; +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.gt; +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.intValue; +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.path; +import static org.apache.iotdb.db.mpp.plan.expression.ExpressionFactory.timeSeries; +import static org.junit.Assert.assertEquals; + +public class ExpressionAnalyzerTest { + + @Test + public void testRemoveWildcardInFilter() throws IllegalPathException { + ISchemaTree fakeSchemaTree = new FakeSchemaFetcherImpl().fetchSchema(new PathPatternTree()); + List<PartialPath> prefixPaths = Arrays.asList(path("root.sg.d1"), path("root.sg.d2")); + + assertEquals( + Arrays.asList( + gt(timeSeries("root.sg.d1.s1"), intValue("1")), + gt(timeSeries("root.sg.d2.s1"), intValue("1")), + gt(timeSeries("root.sg.d1.s2"), intValue("1")), + gt(timeSeries("root.sg.d2.s2"), intValue("1"))), + ExpressionAnalyzer.removeWildcardInFilter( + and(gt(timeSeries("s1"), intValue("1")), gt(timeSeries("s2"), intValue("1"))), + prefixPaths, + fakeSchemaTree, + true)); + + assertEquals( + Arrays.asList( + count( + and( + gt(timeSeries("root.sg.d1.s1"), intValue("1")), + gt(timeSeries("root.sg.d1.s2"), intValue("1")))), + count( + and( + gt(timeSeries("root.sg.d1.s1"), intValue("1")), + gt(timeSeries("root.sg.d2.s2"), intValue("1")))), + count( + and( + gt(timeSeries("root.sg.d2.s1"), intValue("1")), + gt(timeSeries("root.sg.d1.s2"), intValue("1")))), + count( + and( + gt(timeSeries("root.sg.d2.s1"), intValue("1")), + gt(timeSeries("root.sg.d2.s2"), intValue("1"))))), + ExpressionAnalyzer.removeWildcardInFilter( + count(and(gt(timeSeries("s1"), intValue("1")), gt(timeSeries("s2"), intValue("1")))), + prefixPaths, + fakeSchemaTree, + true)); + } +}
