Skip to content

Commit ed49ac6

Browse files
Increase the precision of sum return type (#3974)
Signed-off-by: Heng Qian <qianheng@amazon.com> (cherry picked from commit db2a8bf) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f071b9b commit ed49ac6

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

core/src/main/java/org/opensearch/sql/executor/OpenSearchTypeSystem.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
package org.opensearch.sql.executor;
77

8+
import static org.apache.calcite.sql.type.SqlTypeName.APPROX_TYPES;
9+
import static org.apache.calcite.sql.type.SqlTypeName.INT_TYPES;
10+
811
import org.apache.calcite.rel.type.RelDataType;
912
import org.apache.calcite.rel.type.RelDataTypeFactory;
1013
import org.apache.calcite.rel.type.RelDataTypeSystem;
1114
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
15+
import org.apache.calcite.sql.type.BasicSqlType;
1216
import org.apache.calcite.sql.type.SqlTypeName;
1317

1418
public class OpenSearchTypeSystem extends RelDataTypeSystemImpl {
@@ -20,14 +24,30 @@ private OpenSearchTypeSystem() {}
2024
public RelDataType deriveAvgAggType(RelDataTypeFactory typeFactory, RelDataType argumentType) {
2125
if (SqlTypeName.DECIMAL == argumentType.getSqlTypeName()) {
2226
return typeFactory.createTypeWithNullability(highPrecision(typeFactory, argumentType), false);
23-
} else if (SqlTypeName.INT_TYPES.contains(argumentType.getSqlTypeName())) {
27+
} else if (INT_TYPES.contains(argumentType.getSqlTypeName())) {
2428
return typeFactory.createTypeWithNullability(
2529
typeFactory.createSqlType(SqlTypeName.DOUBLE), false);
2630
} else {
2731
return argumentType;
2832
}
2933
}
3034

35+
@Override
36+
public RelDataType deriveSumType(RelDataTypeFactory typeFactory, RelDataType argumentType) {
37+
argumentType = super.deriveSumType(typeFactory, argumentType);
38+
if (argumentType instanceof BasicSqlType) {
39+
SqlTypeName typeName = argumentType.getSqlTypeName();
40+
if (INT_TYPES.contains(typeName)) {
41+
return typeFactory.createTypeWithNullability(
42+
typeFactory.createSqlType(SqlTypeName.BIGINT), argumentType.isNullable());
43+
} else if (APPROX_TYPES.contains(typeName)) {
44+
return typeFactory.createTypeWithNullability(
45+
typeFactory.createSqlType(SqlTypeName.DOUBLE), argumentType.isNullable());
46+
}
47+
}
48+
return argumentType;
49+
}
50+
3151
/**
3252
* Compute a higher precision version of a type.
3353
*

integ-test/src/test/java/org/opensearch/sql/calcite/tpch/CalcitePPLTpchIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ public void testQ12() throws IOException {
249249
verifySchemaInOrder(
250250
actual,
251251
schema("l_shipmode", "string"),
252-
schema("high_line_count", "int"),
253-
schema("low_line_count", "int"));
252+
schema("high_line_count", "bigint"),
253+
schema("low_line_count", "bigint"));
254254
verifyDataRows(actual, rows("MAIL", 5, 5), rows("SHIP", 5, 10));
255255
}
256256

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
setup:
2+
- do:
3+
query.settings:
4+
body:
5+
transient:
6+
plugins.calcite.enabled : true
7+
plugins.calcite.fallback.allowed : false
8+
9+
- do:
10+
indices.create:
11+
index: tmp
12+
body:
13+
settings:
14+
number_of_shards: 1
15+
number_of_replicas: 0
16+
mappings:
17+
properties:
18+
demo:
19+
type: short
20+
21+
22+
---
23+
teardown:
24+
- do:
25+
query.settings:
26+
body:
27+
transient:
28+
plugins.calcite.enabled : false
29+
plugins.calcite.fallback.allowed : true
30+
31+
---
32+
"Handle sum results exceeds the max value of short":
33+
- skip:
34+
features:
35+
- headers
36+
- allowed_warnings
37+
- do:
38+
bulk:
39+
index: tmp
40+
refresh: true
41+
body:
42+
- '{"index": {}}'
43+
- '{"demo": 20000}'
44+
- '{"index": {}}'
45+
- '{"demo": 20000}'
46+
47+
- do:
48+
allowed_warnings:
49+
- 'Loading the fielddata on the _id field is deprecated and will be removed in future versions. If you require sorting or aggregating on this field you should also include the id in the body of your documents, and map this field as a keyword field that has [doc_values] enabled'
50+
headers:
51+
Content-Type: 'application/json'
52+
ppl:
53+
body:
54+
query: 'source=tmp | stats sum(demo)'
55+
- match: {"total": 1}
56+
- match: {"schema": [{"name": "sum(demo)", "type": "bigint"}]}
57+
- match: {"datarows": [[40000]]}

0 commit comments

Comments
 (0)