This is an automated email from the ASF dual-hosted git repository. rong pushed a commit to branch iotdb-1022-v2 in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 63e5ece09d98d09a678b9bca33ef58e35e9e1800 Author: SteveYurongSu <[email protected]> AuthorDate: Tue May 11 11:42:44 2021 +0800 expressions for complex operations in select clauses --- .../iotdb/db/query/expression/Expression.java | 28 ++++ .../iotdb/db/query/expression/ResultColumn.java | 39 ++++++ .../expression/binary/AdditionExpression.java | 34 +++++ .../query/expression/binary/BinaryExpression.java | 52 +++++++ .../expression/binary/DivisionExpression.java | 34 +++++ .../query/expression/binary/ModuloExpression.java | 34 +++++ .../binary/MultiplicationExpression.java | 34 +++++ .../expression/binary/SubtractionExpression.java | 34 +++++ .../query/expression/unary/FunctionExpression.java | 150 +++++++++++++++++++++ .../db/query/expression/unary/MinusExpression.java | 47 +++++++ .../expression/unary/NumberLiteralOperand.java | 42 ++++++ .../query/expression/unary/TimeSeriesOperand.java | 57 ++++++++ 12 files changed, 585 insertions(+) diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/Expression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/Expression.java new file mode 100644 index 0000000..e4d06d2 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/Expression.java @@ -0,0 +1,28 @@ +/* + * 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.query.expression; + +import org.apache.iotdb.db.exception.metadata.MetadataException; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public interface Expression { + + TSDataType dataType() throws MetadataException; +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/ResultColumn.java b/server/src/main/java/org/apache/iotdb/db/query/expression/ResultColumn.java new file mode 100644 index 0000000..e62350e --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/ResultColumn.java @@ -0,0 +1,39 @@ +/* + * 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.query.expression; + +public class ResultColumn { + + private final Expression expression; + private final String alias; + + public ResultColumn(Expression expression, String alias) { + this.expression = expression; + this.alias = alias; + } + + public Expression getExpression() { + return expression; + } + + public String getResultColumnName() { + return alias != null ? alias : expression.toString(); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/AdditionExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/AdditionExpression.java new file mode 100644 index 0000000..5500ba4 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/AdditionExpression.java @@ -0,0 +1,34 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; + +public class AdditionExpression extends BinaryExpression { + + public AdditionExpression(Expression leftExpression, Expression rightExpression) { + super(leftExpression, rightExpression); + } + + @Override + protected String operator() { + return "+"; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/BinaryExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/BinaryExpression.java new file mode 100644 index 0000000..1106eac --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/BinaryExpression.java @@ -0,0 +1,52 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public abstract class BinaryExpression implements Expression { + + public BinaryExpression(Expression leftExpression, Expression rightExpression) { + this.leftExpression = leftExpression; + this.rightExpression = rightExpression; + } + + protected final Expression leftExpression; + protected final Expression rightExpression; + + /** + * The result data type of all arithmetic operations will be DOUBLE. + * + * <p>TODO: This is just a simple implementation and should be optimized later. + */ + @Override + public TSDataType dataType() { + return TSDataType.DOUBLE; + } + + @Override + public final String toString() { + return String.format( + "%s %s %s", leftExpression.toString(), operator(), rightExpression.toString()); + } + + protected abstract String operator(); +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/DivisionExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/DivisionExpression.java new file mode 100644 index 0000000..e611887 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/DivisionExpression.java @@ -0,0 +1,34 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; + +public class DivisionExpression extends BinaryExpression { + + public DivisionExpression(Expression leftExpression, Expression rightExpression) { + super(leftExpression, rightExpression); + } + + @Override + protected String operator() { + return "/"; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/ModuloExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/ModuloExpression.java new file mode 100644 index 0000000..94dd905 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/ModuloExpression.java @@ -0,0 +1,34 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; + +public class ModuloExpression extends BinaryExpression { + + public ModuloExpression(Expression leftExpression, Expression rightExpression) { + super(leftExpression, rightExpression); + } + + @Override + protected String operator() { + return "%"; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/MultiplicationExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/MultiplicationExpression.java new file mode 100644 index 0000000..ae3d17b --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/MultiplicationExpression.java @@ -0,0 +1,34 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; + +public class MultiplicationExpression extends BinaryExpression { + + public MultiplicationExpression(Expression leftExpression, Expression rightExpression) { + super(leftExpression, rightExpression); + } + + @Override + protected String operator() { + return "*"; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/binary/SubtractionExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/SubtractionExpression.java new file mode 100644 index 0000000..65bcf50 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/binary/SubtractionExpression.java @@ -0,0 +1,34 @@ +/* + * 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.query.expression.binary; + +import org.apache.iotdb.db.query.expression.Expression; + +public class SubtractionExpression extends BinaryExpression { + + public SubtractionExpression(Expression leftExpression, Expression rightExpression) { + super(leftExpression, rightExpression); + } + + @Override + protected String operator() { + return "-"; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/FunctionExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/FunctionExpression.java new file mode 100644 index 0000000..af6186c --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/FunctionExpression.java @@ -0,0 +1,150 @@ +/* + * 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.query.expression.unary; + +import org.apache.iotdb.db.exception.metadata.MetadataException; +import org.apache.iotdb.db.query.expression.Expression; +import org.apache.iotdb.tsfile.exception.NotImplementedException; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class FunctionExpression implements Expression { + + private final String functionName; + private final Map<String, String> functionAttributes; + + private List<Expression> expressions; + private List<TSDataType> dataTypes; + + private String expressionString; + private String parametersString; + + public FunctionExpression(String functionName) { + this.functionName = functionName; + functionAttributes = new LinkedHashMap<>(); + expressions = new ArrayList<>(); + } + + public FunctionExpression(String functionName, Map<String, String> functionAttributes, + List<Expression> expressions) { + this.functionName = functionName; + this.functionAttributes = functionAttributes; + this.expressions = expressions; + } + + public void addAttribute(String key, String value) { + functionAttributes.put(key, value); + } + + public void addExpression(Expression expression) { + expressions.add(expression); + } + + public void setExpressions(List<Expression> expressions) { + this.expressions = expressions; + } + + public String getFunctionName() { + return functionName; + } + + public Map<String, String> getFunctionAttributes() { + return functionAttributes; + } + + public List<Expression> getExpressions() { + return expressions; + } + + @Override + public TSDataType dataType() { + // TODO: the expression type is determined in runtime + throw new NotImplementedException(); + } + + public List<TSDataType> getDataTypes() throws MetadataException { + if (dataTypes == null) { + dataTypes = new ArrayList<>(); + for (Expression expression : expressions) { + dataTypes.add(expression.dataType()); + } + } + return dataTypes; + } + + @Override + public String toString() { + if (expressionString == null) { + expressionString = functionName + "(" + parametersString() + ")"; + } + return expressionString; + } + + /** + * Generates the parameter part of the udf column name. + * + * <p>Example: + * Full column name -> udf(root.sg.d.s1, sin(root.sg.d.s1), 'key1'='value1', 'key2'='value2') + * <p> + * The parameter part -> root.sg.d.s1, sin(root.sg.d.s1), 'key1'='value1', 'key2'='value2' + */ + private String parametersString() { + if (parametersString == null) { + StringBuilder builder = new StringBuilder(); + if (!expressions.isEmpty()) { + builder.append(expressions.get(0).toString()); + for (int i = 1; i < expressions.size(); ++i) { + builder.append(", ").append(expressions.get(i).toString()); + } + } + if (!functionAttributes.isEmpty()) { + if (!expressions.isEmpty()) { + builder.append(", "); + } + Iterator<Entry<String, String>> iterator = functionAttributes.entrySet().iterator(); + Entry<String, String> entry = iterator.next(); + builder + .append("\"") + .append(entry.getKey()) + .append("\"=\"") + .append(entry.getValue()) + .append("\""); + while (iterator.hasNext()) { + entry = iterator.next(); + builder + .append(", ") + .append("\"") + .append(entry.getKey()) + .append("\"=\"") + .append(entry.getValue()) + .append("\""); + } + } + parametersString = builder.toString(); + } + return parametersString; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/MinusExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/MinusExpression.java new file mode 100644 index 0000000..965e3c0 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/MinusExpression.java @@ -0,0 +1,47 @@ +/* + * 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.query.expression.unary; + +import org.apache.iotdb.db.exception.metadata.MetadataException; +import org.apache.iotdb.db.query.expression.Expression; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public class MinusExpression implements Expression { + + protected Expression expression; + + public MinusExpression(Expression expression) { + this.expression = expression; + } + + public Expression getExpression() { + return expression; + } + + @Override + public TSDataType dataType() throws MetadataException { + return expression.dataType(); + } + + @Override + public String toString() { + return "-" + expression.toString(); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NumberLiteralOperand.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NumberLiteralOperand.java new file mode 100644 index 0000000..8405947 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NumberLiteralOperand.java @@ -0,0 +1,42 @@ +/* + * 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.query.expression.unary; + +import org.apache.iotdb.db.query.expression.Expression; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public class NumberLiteralOperand implements Expression { + + protected double literal; + + public NumberLiteralOperand(double literal) { + this.literal = literal; + } + + @Override + public TSDataType dataType() { + return TSDataType.DOUBLE; + } + + @Override + public String toString() { + return String.valueOf(literal); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/TimeSeriesOperand.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/TimeSeriesOperand.java new file mode 100644 index 0000000..c72a4b2 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/TimeSeriesOperand.java @@ -0,0 +1,57 @@ +/* + * 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.query.expression.unary; + +import org.apache.iotdb.db.exception.metadata.MetadataException; +import org.apache.iotdb.db.metadata.PartialPath; +import org.apache.iotdb.db.query.expression.Expression; +import org.apache.iotdb.db.service.IoTDB; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public class TimeSeriesOperand implements Expression { + + protected PartialPath path; + protected TSDataType dataType; + + public TimeSeriesOperand(PartialPath path) { + this.path = path; + } + + public PartialPath getPath() { + return path; + } + + public void setPath(PartialPath path) { + this.path = path; + } + + @Override + public TSDataType dataType() throws MetadataException { + if (dataType == null) { + dataType = IoTDB.metaManager.getSeriesType(path); + } + return dataType; + } + + @Override + public String toString() { + return path.toString(); + } +}
