• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9919

25 Aug 2023 07:08AM UTC coverage: 47.802% (+0.007%) from 47.795%
#9919

push

travis_ci

web-flow
Remove some useless configs (#10950)

80023 of 167404 relevant lines covered (47.8%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.08
/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/SchemaUtils.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.iotdb.db.utils;
20

21
import org.apache.iotdb.common.rpc.thrift.TAggregationType;
22
import org.apache.iotdb.commons.exception.MetadataException;
23
import org.apache.iotdb.commons.path.PartialPath;
24
import org.apache.iotdb.db.queryengine.plan.statement.component.Ordering;
25
import org.apache.iotdb.db.utils.constant.SqlConstant;
26
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
27
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
28

29
import java.util.Arrays;
30
import java.util.Collections;
31
import java.util.EnumMap;
32
import java.util.HashSet;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Set;
36

37
public class SchemaUtils {
38

39
  private SchemaUtils() {}
40

41
  private static final Map<TSDataType, Set<TSEncoding>> schemaChecker =
1✔
42
      new EnumMap<>(TSDataType.class);
43

44
  static {
45
    Set<TSEncoding> booleanSet = new HashSet<>();
1✔
46
    booleanSet.add(TSEncoding.PLAIN);
1✔
47
    booleanSet.add(TSEncoding.RLE);
1✔
48
    schemaChecker.put(TSDataType.BOOLEAN, booleanSet);
1✔
49

50
    Set<TSEncoding> intSet = new HashSet<>();
1✔
51
    intSet.add(TSEncoding.PLAIN);
1✔
52
    intSet.add(TSEncoding.RLE);
1✔
53
    intSet.add(TSEncoding.TS_2DIFF);
1✔
54
    intSet.add(TSEncoding.GORILLA);
1✔
55
    intSet.add(TSEncoding.ZIGZAG);
1✔
56
    intSet.add(TSEncoding.CHIMP);
1✔
57
    intSet.add(TSEncoding.SPRINTZ);
1✔
58
    intSet.add(TSEncoding.RLBE);
1✔
59

60
    schemaChecker.put(TSDataType.INT32, intSet);
1✔
61
    schemaChecker.put(TSDataType.INT64, intSet);
1✔
62

63
    Set<TSEncoding> floatSet = new HashSet<>();
1✔
64
    floatSet.add(TSEncoding.PLAIN);
1✔
65
    floatSet.add(TSEncoding.RLE);
1✔
66
    floatSet.add(TSEncoding.TS_2DIFF);
1✔
67
    floatSet.add(TSEncoding.GORILLA_V1);
1✔
68
    floatSet.add(TSEncoding.GORILLA);
1✔
69
    floatSet.add(TSEncoding.CHIMP);
1✔
70
    floatSet.add(TSEncoding.SPRINTZ);
1✔
71
    floatSet.add(TSEncoding.RLBE);
1✔
72

73
    schemaChecker.put(TSDataType.FLOAT, floatSet);
1✔
74
    schemaChecker.put(TSDataType.DOUBLE, floatSet);
1✔
75

76
    Set<TSEncoding> textSet = new HashSet<>();
1✔
77
    textSet.add(TSEncoding.PLAIN);
1✔
78
    textSet.add(TSEncoding.DICTIONARY);
1✔
79
    schemaChecker.put(TSDataType.TEXT, textSet);
1✔
80
  }
1✔
81

82
  /**
83
   * If the datatype of 'aggregation' depends on 'measurementDataType' (min_value, max_value),
84
   * return 'measurementDataType' directly, or return a list whose elements are all the datatype of
85
   * 'aggregation' and its length is the same as 'measurementDataType'.
86
   *
87
   * @param measurementDataType raw measurement type
88
   * @param aggregation aggregation type
89
   * @return measurementDataType directly, or return a list whose elements are all the datatype of
90
   *     aggregation and its length is the same as 'measurementDataType'
91
   */
92
  public static List<TSDataType> getAggregatedDataTypes(
93
      List<TSDataType> measurementDataType, String aggregation) {
94
    TSDataType dataType = getAggregationType(aggregation);
1✔
95
    if (dataType != null) {
1✔
96
      return Collections.nCopies(measurementDataType.size(), dataType);
1✔
97
    }
98
    return measurementDataType;
1✔
99
  }
100

101
  public static TSDataType getSeriesTypeByPath(PartialPath path, String aggregation) {
102
    TSDataType dataType = getAggregationType(aggregation);
1✔
103
    if (dataType != null) {
1✔
104
      return dataType;
1✔
105
    } else {
106
      return path.getSeriesType();
1✔
107
    }
108
  }
109

110
  /**
111
   * @param aggregation aggregation function
112
   * @return the data type of the aggregation or null if it aggregation is null
113
   */
114
  public static TSDataType getAggregationType(String aggregation) {
115
    if (aggregation == null) {
1✔
116
      return null;
×
117
    }
118
    switch (aggregation.toLowerCase()) {
1✔
119
      case SqlConstant.MIN_TIME:
120
      case SqlConstant.MAX_TIME:
121
      case SqlConstant.COUNT:
122
      case SqlConstant.TIME_DURATION:
123
      case SqlConstant.COUNT_TIME:
124
        return TSDataType.INT64;
1✔
125
      case SqlConstant.AVG:
126
      case SqlConstant.SUM:
127
        return TSDataType.DOUBLE;
1✔
128
      case SqlConstant.LAST_VALUE:
129
      case SqlConstant.FIRST_VALUE:
130
      case SqlConstant.MIN_VALUE:
131
      case SqlConstant.MAX_VALUE:
132
      case SqlConstant.MODE:
133
      default:
134
        return null;
1✔
135
    }
136
  }
137

138
  /**
139
   * judge whether the order of aggregation calculation is consistent with the order of traversing
140
   * data
141
   */
142
  public static boolean isConsistentWithScanOrder(
143
      TAggregationType aggregationFunction, Ordering scanOrder) {
144
    boolean ascending = scanOrder == Ordering.ASC;
1✔
145
    switch (aggregationFunction) {
1✔
146
      case MIN_TIME:
147
      case FIRST_VALUE:
148
        return ascending;
×
149
      case MAX_TIME:
150
      case LAST_VALUE:
151
        return !ascending;
×
152
      case SUM:
153
      case MIN_VALUE:
154
      case MAX_VALUE:
155
      case EXTREME:
156
      case COUNT:
157
      case COUNT_TIME:
158
      case AVG:
159
      case TIME_DURATION:
160
        return true;
1✔
161
      default:
162
        throw new IllegalArgumentException(
×
163
            String.format("Invalid Aggregation function: %s", aggregationFunction));
×
164
    }
165
  }
166

167
  public static void checkDataTypeWithEncoding(TSDataType dataType, TSEncoding encoding)
168
      throws MetadataException {
169
    if (!schemaChecker.get(dataType).contains(encoding)) {
1✔
170
      throw new MetadataException(
1✔
171
          String.format("encoding %s does not support %s", encoding, dataType), true);
1✔
172
    }
173
  }
1✔
174

175
  public static List<TAggregationType> splitPartialAggregation(TAggregationType aggregationType) {
176
    switch (aggregationType) {
1✔
177
      case FIRST_VALUE:
178
        return Collections.singletonList(TAggregationType.MIN_TIME);
×
179
      case LAST_VALUE:
180
        return Collections.singletonList(TAggregationType.MAX_TIME);
×
181
      case AVG:
182
        return Arrays.asList(TAggregationType.COUNT, TAggregationType.SUM);
×
183
      case TIME_DURATION:
184
        return Arrays.asList(TAggregationType.MAX_TIME, TAggregationType.MIN_TIME);
×
185
      case SUM:
186
      case MIN_VALUE:
187
      case MAX_VALUE:
188
      case EXTREME:
189
      case COUNT:
190
      case COUNT_TIME:
191
      case MIN_TIME:
192
      case MAX_TIME:
193
      case COUNT_IF:
194
      case MODE:
195
        return Collections.emptyList();
1✔
196
      default:
197
        throw new IllegalArgumentException(
×
198
            String.format("Invalid Aggregation function: %s", aggregationType));
×
199
    }
200
  }
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc