Skip to content

Commit

Permalink
fix(core): float cast to double (IGinX-THU#430)
Browse files Browse the repository at this point in the history
java中float类型直接隐式转换成double类型时会多出很多小数位,使用 BigDecimal类进行处理。
  • Loading branch information
jzl18thu authored Sep 14, 2024
1 parent a6e98dd commit e7559b4
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import cn.edu.tsinghua.iginx.engine.shared.data.Value;
import cn.edu.tsinghua.iginx.engine.shared.data.read.Row;
import cn.edu.tsinghua.iginx.thrift.DataType;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
Expand Down Expand Up @@ -53,7 +54,8 @@ public static Value transformToDouble(Value value) {
dVal = value.getLongV().doubleValue();
break;
case FLOAT:
dVal = value.getFloatV().doubleValue();
BigDecimal bd = new BigDecimal(value.getFloatV().toString());
dVal = bd.doubleValue();
break;
case DOUBLE:
dVal = value.getDoubleV();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ public void testShowColumns() {
+ "Total line number = 2\n";
SQLTestTools.executeAndCompare(session, statement, expected);

statement = "SHOW COLUMNS tm.*;";
statement = "SHOW COLUMNS tm.wf05.wt01.*;";
expected =
"Columns:\n"
+ "+------------------------+--------+\n"
Expand All @@ -821,7 +821,7 @@ public void testShowColumns() {
// test dummy query for data out of initial key range (should be visible)
protected void testDummyKeyRange() {
String statement;
statement = "select * from mn where key < 1;";
statement = "select * from mn.wf01.wt01 where key < 1;";
String expected =
"Columns:\n"
+ "+------------------------+--------+\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class Constant {
public static final List<String> READ_ONLY_PATH_LIST =
Arrays.asList("tm.wf05.wt01.status", "tm.wf05.wt01.temperature");

public static final List<String> READ_ONLY_FLOAT_PATH_LIST =
Collections.singletonList("tm.wf05.wt02.float");

public static final List<String> READ_ONLY_EXTEND_PATH_LIST =
Arrays.asList("a.a.c.status", "a.a.c.temperature");

Expand Down Expand Up @@ -115,6 +118,9 @@ public class Constant {
public static List<List<Object>> READ_ONLY_VALUES_LIST =
Arrays.asList(Arrays.asList(55555555L, 10012.01), Arrays.asList(66666666L, 99123.99));

public static List<List<Object>> READ_ONLY_FLOAT_VALUES_LIST =
Arrays.asList(Collections.singletonList(22.33F), Collections.singletonList(44.55F));

public static List<List<Object>> READ_ONLY_EXTEND_VALUES_LIST =
Collections.singletonList(Arrays.asList(9999999L, 152346.1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ private void testShowDummyColumns() {
+ "| a.b.c.d.1\\txt| BINARY|\n"
+ "| a.e.2\\txt| BINARY|\n"
+ "| a.f.g.3\\txt| BINARY|\n"
+ "| a.floatTest\\parquet| BINARY|\n"
+ "| a.floatTest\\parquet.floatValue| FLOAT|\n"
+ "| a.other.MT cars\\parquet| BINARY|\n"
+ "| a.other.MT cars\\parquet.am| INTEGER|\n"
+ "| a.other.MT cars\\parquet.carb| INTEGER|\n"
Expand Down Expand Up @@ -180,7 +182,7 @@ private void testShowDummyColumns() {
+ "| a.other.price\\parquet.price| LONG|\n"
+ "| a.other.price\\parquet.stories| LONG|\n"
+ "+--------------------------------------+--------+\n"
+ "Total line number = 36\n";
+ "Total line number = 38\n";
SQLTestTools.executeAndCompare(session, statement, expected);
}

Expand Down Expand Up @@ -304,5 +306,29 @@ private void testQueryParquets() {
+ "+---+---------------------------+\n"
+ "Total line number = 10\n";
SQLTestTools.executeAndCompare(session, statement, expect);

// test float value compare
statement = "select floatValue from `a.floatTest\\parquet` where floatValue >= 22.33;";
expect =
"ResultSets:\n"
+ "+---+------------------------------+\n"
+ "|key|a.floatTest\\parquet.floatValue|\n"
+ "+---+------------------------------+\n"
+ "| 0| 22.33|\n"
+ "| 1| 44.55|\n"
+ "+---+------------------------------+\n"
+ "Total line number = 2\n";
SQLTestTools.executeAndCompare(session, statement, expect);

statement = "select floatValue from `a.floatTest\\parquet` where floatValue = 44.55;";
expect =
"ResultSets:\n"
+ "+---+------------------------------+\n"
+ "|key|a.floatTest\\parquet.floatValue|\n"
+ "+---+------------------------------+\n"
+ "| 1| 44.55|\n"
+ "+---+------------------------------+\n"
+ "Total line number = 1\n";
SQLTestTools.executeAndCompare(session, statement, expect);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private void writeSpecificDirectoriesAndFiles() {
// │ └── g
// │ └── 3.txt
// ├── Iris.parquet
// ├── floatTest.parquet
// └── other
// ├── MT cars.parquet
// └── price.parquet
Expand All @@ -167,6 +168,8 @@ private void writeSpecificDirectoriesAndFiles() {
String parquetResourceDir = "dummy/parquet/";
copyFileFromResource(
parquetResourceDir + "Iris.parquet", Paths.get("test", "a", "Iris.parquet"));
copyFileFromResource(
parquetResourceDir + "floatTest.parquet", Paths.get("test", "a", "floatTest.parquet"));
copyFileFromResource(
parquetResourceDir + "MT cars.parquet", Paths.get("test", "a", "other", "MT cars.parquet"));
copyFileFromResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import static org.junit.Assert.fail;

import cn.edu.tsinghua.iginx.integration.expansion.BaseCapacityExpansionIT;
import cn.edu.tsinghua.iginx.integration.expansion.constant.Constant;
import cn.edu.tsinghua.iginx.integration.expansion.utils.SQLTestTools;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -64,4 +68,20 @@ private void changeParams(int port, String oldPw, String newPw) {
fail("Fail to update iotdb params.");
}
}

@Override
protected void testQuerySpecialHistoryData() {
testFloatData();
}

/** 测试float类型数据 */
private void testFloatData() {
String statement = "select wt02.float from tm.wf05 where wt02.float <= 44.55;";
List<String> pathList = Constant.READ_ONLY_FLOAT_PATH_LIST;
List<List<Object>> valuesList = Constant.READ_ONLY_FLOAT_VALUES_LIST;
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
statement = "select wt02.float from tm.wf05 where wt02.float = 44.55;";
valuesList = Arrays.asList(Arrays.asList(44.55F));
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package cn.edu.tsinghua.iginx.integration.expansion.iotdb;

import cn.edu.tsinghua.iginx.integration.expansion.BaseHistoryDataGenerator;
import cn.edu.tsinghua.iginx.integration.expansion.constant.Constant;
import cn.edu.tsinghua.iginx.thrift.DataType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -45,6 +47,7 @@ public class IoTDB12HistoryDataGenerator extends BaseHistoryDataGenerator {
put("DOUBLE", "DOUBLE");
put("BINARY", "TEXT");
put("INTEGER", "INT32");
put("FLOAT", "FLOAT");
}
};

Expand Down Expand Up @@ -111,6 +114,16 @@ public void writeHistoryData(
writeHistoryData(port, pathList, dataTypeList, new ArrayList<>(), valuesList);
}

@Override
public void writeSpecialHistoryData() {
// write float value
writeHistoryData(
Constant.readOnlyPort,
Constant.READ_ONLY_FLOAT_PATH_LIST,
new ArrayList<>(Collections.singletonList(DataType.FLOAT)),
Constant.READ_ONLY_FLOAT_VALUES_LIST);
}

@Override
public void clearHistoryDataForGivenPort(int port) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import cn.edu.tsinghua.iginx.integration.controller.Controller;
import cn.edu.tsinghua.iginx.integration.expansion.BaseCapacityExpansionIT;
import cn.edu.tsinghua.iginx.integration.expansion.constant.Constant;
import cn.edu.tsinghua.iginx.integration.expansion.utils.SQLTestTools;
import cn.edu.tsinghua.iginx.integration.tool.ConfLoader;
import cn.edu.tsinghua.iginx.integration.tool.DBConf;
import cn.edu.tsinghua.iginx.thrift.StorageEngineType;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -74,4 +77,20 @@ private void changeParams(int port, String oldPw, String newPw) {
fail("Fail to update mysql params.");
}
}

@Override
protected void testQuerySpecialHistoryData() {
testFloatData();
}

/** 测试float类型数据 */
private void testFloatData() {
String statement = "select wt02.float from tm.wf05 where wt02.float <= 44.55;";
List<String> pathList = Constant.READ_ONLY_FLOAT_PATH_LIST;
List<List<Object>> valuesList = Constant.READ_ONLY_FLOAT_VALUES_LIST;
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
statement = "select wt02.float from tm.wf05 where wt02.float = 44.55;";
valuesList = Arrays.asList(Arrays.asList(44.55F));
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import cn.edu.tsinghua.iginx.thrift.DataType;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -174,6 +175,16 @@ public void writeHistoryData(
writeHistoryData(port, pathList, dataTypeList, new ArrayList<>(), valuesList);
}

@Override
public void writeSpecialHistoryData() {
// write float value
writeHistoryData(
Constant.readOnlyPort,
Constant.READ_ONLY_FLOAT_PATH_LIST,
new ArrayList<>(Collections.singletonList(DataType.FLOAT)),
Constant.READ_ONLY_FLOAT_VALUES_LIST);
}

@Override
public void clearHistoryDataForGivenPort(int port) {
Connection conn = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import cn.edu.tsinghua.iginx.thrift.StorageEngineType;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -57,6 +59,7 @@ public PostgreSQLCapacityExpansionIT() {
protected void testQuerySpecialHistoryData() {
testRepeatsQuery();
testConcatFunction();
testFloatData();
}

/** 执行一个简单查询1000次,测试是否会使连接池耗尽,来验证PG的dummy查询是否正确释放连接 */
Expand Down Expand Up @@ -144,6 +147,17 @@ protected void testConcatFunction() {
}
}

/** 测试float类型数据 */
private void testFloatData() {
String statement = "select wt02.float from tm.wf05 where wt02.float <= 44.55;";
List<String> pathList = Constant.READ_ONLY_FLOAT_PATH_LIST;
List<List<Object>> valuesList = Constant.READ_ONLY_FLOAT_VALUES_LIST;
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
statement = "select wt02.float from tm.wf05 where wt02.float = 44.55;";
valuesList = Arrays.asList(Arrays.asList(44.55F));
SQLTestTools.executeAndCompare(session, statement, pathList, valuesList);
}

@Override
protected void updateParams(int port) {
changeParams(port, "postgres", "newPassword");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ public void writeHistoryData(
writeHistoryData(port, pathList, dataTypeList, new ArrayList<>(), valuesList);
}

@Override
public void writeSpecialHistoryData() {
// write float value
writeHistoryData(
Constant.readOnlyPort,
Constant.READ_ONLY_FLOAT_PATH_LIST,
new ArrayList<>(Collections.singletonList(DataType.FLOAT)),
Constant.READ_ONLY_FLOAT_VALUES_LIST);
}

@Override
public void clearHistoryDataForGivenPort(int port) {
Connection conn = null;
Expand Down
Binary file not shown.

0 comments on commit e7559b4

Please sign in to comment.