Skip to content

Commit

Permalink
Add decimal support in SQL engine (finos#3019)
Browse files Browse the repository at this point in the history
Add decimal support in SQL engine with tests / example
  • Loading branch information
gs-sids authored Aug 15, 2024
1 parent 6d52683 commit 233cc9b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Database demo::H2DemoDataBase
Table EmployeeTable
(
id INTEGER PRIMARY KEY,
ratings DECIMAL(10,2),
firm_id INTEGER,
name VARCHAR(200),
country_id INTEGER,
Expand Down Expand Up @@ -103,6 +104,35 @@ Service demo::H2PersonService
}
}

Service demo::H2PersonRatings
{
pattern: '/personRatings';
owners:
[
'anonymous1',
'anonymous2'
];
documentation: '';
autoActivateUpdates: true;
execution: Single
{
query: |demo::employee.all()->project(
[
x|$x.id,
x|$x.name,
x|$x.ratings
],
[
'Id',
'Name',
'Ratings'
]
);
mapping: demo::DemoRelationalMapping;
runtime: demo::H2DemoRuntime;
}
}

Service demo::H2PersonServiceParameterized
{
pattern: '/personServiceForNames';
Expand Down Expand Up @@ -172,6 +202,7 @@ Enum demo::employeeType
Class demo::employee
{
id: Integer[1];
ratings: Decimal[1];
name: String[1];
type: demo::employeeType[1];
startDate: Date[1];
Expand All @@ -190,6 +221,7 @@ Mapping demo::DemoRelationalMapping
)
~mainTable [demo::H2DemoDataBase]EmployeeTable
id: [demo::H2DemoDataBase]EmployeeTable.id,
ratings: [demo::H2DemoDataBase]EmployeeTable.ratings,
name: [demo::H2DemoDataBase]EmployeeTable.name,
type: EnumerationMapping EmployeeType: [demo::H2DemoDataBase]EmployeeTable.type,
startDate: [demo::H2DemoDataBase]EmployeeTable.start_date
Expand All @@ -211,7 +243,7 @@ RelationalDatabaseConnection demo::H2DemoConnection
specification: LocalH2
{
testDataSetupSqls: [
'Drop table if exists EmployeeTable;\nCreate Table EmployeeTable(id INTEGER PRIMARY KEY,firm_id INTEGER,name VARCHAR(200),country_id INTEGER, type VARCHAR(25), start_date DATE);\nInsert into EmployeeTable (id, firm_id, name, country_id, type, start_date) values (101,202, \'Alice\', 303, \'Type1\', \'2023-08-24\');\nInsert into EmployeeTable (id, firm_id, name, country_id, type, start_date) values (102,203, \'Bob\', 304, \'Type2\', \'2022-08-24\');\nInsert into EmployeeTable (id, firm_id, name, country_id, type, start_date) values (103,204, \'Curtis\', 305, \'Type2\', \'2022-07-24\');\nInsert into EmployeeTable (id, firm_id, name, country_id, type, start_date) values (104,205, \'Danielle\', 306, \'Type1\', \'2022-07-23\');\n'
'Drop table if exists EmployeeTable;\nCreate Table EmployeeTable(id INTEGER PRIMARY KEY,ratings DECIMAL,firm_id INTEGER,name VARCHAR(200),country_id INTEGER, type VARCHAR(25), start_date DATE);\nInsert into EmployeeTable (id, ratings, firm_id, name, country_id, type, start_date) values (101,9.1,202, \'Alice\', 303, \'Type1\', \'2023-08-24\');\nInsert into EmployeeTable (id, ratings, firm_id, name, country_id, type, start_date) values (102,9.2,203, \'Bob\', 304, \'Type2\', \'2022-08-24\');\nInsert into EmployeeTable (id, ratings, firm_id, name, country_id, type, start_date) values (103,9.3,204, \'Curtis\', 305, \'Type2\', \'2022-07-24\');\nInsert into EmployeeTable (id, ratings, firm_id, name, country_id, type, start_date) values (104,9.4,205, \'Danielle\', 306, \'Type1\', \'2022-07-23\');\n'
];
};
auth: DefaultH2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class LegendDataType
public static final String INTEGER = "Integer";
public static final String FLOAT = "Float";
public static final String NUMBER = "Number";
public static final String DECIMAL = "Decimal";
public static final String BOOLEAN = "Boolean";

public static final String STRING = "String";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public Object getObject(int i) throws Exception
});
case FLOAT:
case NUMBER:
case DECIMAL:
return extractValue(value, legendColumn, Number.class, "DECIMAL (FLOAT/DOUBLE)",
f ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public int getColumnType(int i) throws Exception
return Types.BIGINT;
case FLOAT:
case NUMBER:
case DECIMAL:
return Types.DOUBLE;
case BOOLEAN:
return Types.BOOLEAN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class PGTypes
.put(Types.FLOAT, RealType.INSTANCE)
.put(Types.DOUBLE, DoubleType.INSTANCE)
.put(Types.NUMERIC, NumericType.INSTANCE)
.put(Types.DECIMAL, DoubleType.INSTANCE)
//.put(Types.TIMESTAMP_WITH_TIMEZONE, TimeTZType.INSTANCE)
//.put(Types.TIMESTAMP, TimestampZType.INSTANCE)
.put(Types.TIMESTAMP, TimestampType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ public void testTableFunctionSyntax() throws SQLException
}
}

@Test
public void testTableFunctionwithDecimal() throws SQLException
{
try (
Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:" + testPostgresServer.getLocalAddress().getPort() + "/postgres",
"dummy", "dummy");
PreparedStatement statement = connection.prepareStatement("SELECT * FROM service('/personRatings')");
ResultSet resultSet = statement.executeQuery()
)
{
int rows = 0;
while (resultSet.next())
{
rows++;
}
Assert.assertEquals(4, rows);
}
}

@Test
public void testSelectWithoutTable() throws SQLException
{
Expand Down Expand Up @@ -368,4 +387,4 @@ public static void tearDown()
testPostgresServer.stopListening();
testPostgresServer.shutDown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ public void testFloatInvalidData() throws Exception
validate(FLOAT, "\"fooooo\"", null, null);
}

@Test
public void testDecimal() throws Exception
{
validate(DECIMAL, "5.5", "float8", 5.5D);
validate(DECIMAL, "null", "float8", null);
validate(DECIMAL, "2645198855588.533433343434", "float8", 2645198855588.533433343434D);
}


@Test()
public void testDecimalInvalidData() throws Exception
{
expectedException.expect(Exception.class);
expectedException.expectMessage("ERROR: Unexpected data type for value 'foooo....' in column 'column1'." +
" Expected data type 'java.lang.Number', actual data type 'java.lang.String'");
validate(DECIMAL, "\"fooooo\"", null, null);
}

@Test
public void testInteger() throws Exception
{
Expand Down

0 comments on commit 233cc9b

Please sign in to comment.