Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow hierarchic grid structure for JointGridContainer [#768](https://github.com/ie3-institute/PowerSystemDataModel/issues/768)
- Adding SQL id coordinate sources (``IdCoordinateSource``) [#689](https://github.com/ie3-institute/PowerSystemDataModel/issues/689)
- Added some standard asset types to documentation [#642](https://github.com/ie3-institute/PowerSystemDataModel/issues/642)
- Enhanced error handling of coordinate sources with invalid column names [#670](https://github.com/ie3-institute/PowerSystemDataModel/issues/670)

### Fixed
- Fixed wrong rated power unit hint [#804](https://github.com/ie3-institute/PowerSystemDataModel/issues/804)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public String getLatField() {
public String getLonField() {
return LONG_GEO;
}

@Override
public List<String> getFieldNames() {
return List.of(TID, COORDINATE_ID, "long_geo", "lat_geo", "long_rot", "lat_rot");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public String getLatField() {
public String getLonField() {
return LONG;
}

@Override
public List<String> getFieldNames() {
return List.of(COORDINATE_ID, LONG, LAT, "coordinate_type");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
*/
package edu.ie3.datamodel.io.factory.timeseries;

import edu.ie3.datamodel.exceptions.InvalidColumnNameException;
import edu.ie3.datamodel.io.factory.Factory;
import edu.ie3.datamodel.io.factory.SimpleFactoryData;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.locationtech.jts.geom.Point;

Expand All @@ -28,4 +31,27 @@ protected IdCoordinateFactory() {

/** @return the field id for the coordinate longitude */
public abstract String getLonField();

/** @return all fields */
public abstract List<String> getFieldNames();

public void checkForInvalidColumnNames(Set<String> columnNames) {
List<String> validColumnNames = getFieldNames();

if (!columnNames.containsAll(validColumnNames)) {
throw new InvalidColumnNameException(
"The provided column names "
+ columnNames
+ " does not match the expected column names "
+ validColumnNames
+ "!");
}

if (columnNames.size() != validColumnNames.size()) {
log.warn(
"The provided row has more column names than expected. Provided: {}, expected: {}.",
columnNames,
validColumnNames);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public String getLonField() {
"this is not supported by " + SqlIdCoordinateFactory.class + "!");
}

@Override
public List<String> getFieldNames() {
return List.of(COORDINATE_ID, COORDINATE);
}

public String getCoordinateField() {
return COORDINATE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ protected Stream<Map<String, String>> buildStreamWithFieldsToAttributesMap() {
try (BufferedReader reader = dataSource.connector.initIdCoordinateReader()) {
final String[] headline = dataSource.parseCsvRow(reader.readLine(), dataSource.csvSep);

// checking the column names of csv source
factory.checkForInvalidColumnNames(Set.of(headline));

// by default try-with-resources closes the reader directly when we leave this method (which
// is wanted to avoid a lock on the file), but this causes a closing of the stream as well.
// As we still want to consume the data at other places, we start a new stream instead of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public SqlIdCoordinateSource(
String dbPointColumnName =
dataSource.getDbColumnName(factory.getCoordinateField(), coordinateTableName);

// checking the column names of sql source
factory.checkForInvalidColumnNames(Set.of(dbIdColumnName, dbPointColumnName));

// setup queries
this.basicQuery = createBaseQueryString(dataSource.schemaName, coordinateTableName);
this.queryForPoint = createQueryForPoint(dbIdColumnName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.io.factory.timeseries

import edu.ie3.datamodel.exceptions.FactoryException
import edu.ie3.datamodel.exceptions.InvalidColumnNameException
import edu.ie3.datamodel.io.factory.SimpleFactoryData
import edu.ie3.util.geo.GeoUtils
import org.apache.commons.lang3.tuple.Pair
Expand Down Expand Up @@ -96,4 +98,16 @@ class CosmoIdCoordinateFactoryTest extends Specification {
assert it.value.equalsExact(expectedPair.value, 1E-6)
}
}

def "Throws an exception if an invalid column name is given" () {
given:
def invalidColumnNames = ["id", "lat", "lon"] as Set<String>
when:
factory.checkForInvalidColumnNames(invalidColumnNames)

then:
Exception ex = thrown()
ex.class == InvalidColumnNameException
ex.message == "The provided column names [id, lat, lon] does not match the expected column names [tid, id, long_geo, lat_geo, long_rot, lat_rot]!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package edu.ie3.datamodel.io.factory.timeseries

import edu.ie3.datamodel.exceptions.FactoryException
import edu.ie3.datamodel.exceptions.InvalidColumnNameException
import edu.ie3.datamodel.io.factory.SimpleFactoryData
import edu.ie3.util.geo.GeoUtils
import org.apache.commons.lang3.tuple.Pair
Expand Down Expand Up @@ -82,4 +84,16 @@ class IconIdCoordinateFactoryTest extends Specification {
assert it.value.equalsExact(expectedPair.value, 1E-6)
}
}

def "Throws an exception if an invalid column name is given" () {
given:
def invalidColumnNames = ["id", "lat", "lon"] as Set<String>
when:
factory.checkForInvalidColumnNames(invalidColumnNames)

then:
Exception ex = thrown()
ex.class == InvalidColumnNameException
ex.message == "The provided column names [id, lat, lon] does not match the expected column names [id, longitude, latitude, coordinate_type]!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package edu.ie3.datamodel.io.source.sql

import edu.ie3.datamodel.exceptions.InvalidColumnNameException
import edu.ie3.datamodel.io.connectors.SqlConnector
import edu.ie3.datamodel.io.factory.timeseries.SqlIdCoordinateFactory
import edu.ie3.test.helper.TestContainerHelper
Expand Down Expand Up @@ -187,4 +188,16 @@ class SqlIdCoordinateSourceIT extends Specification implements TestContainerHelp
expectedValues.contains(coordinateDistance.coordinateB)
}
}

def "Throws an exception if an invalid column name is given" () {
given:
def invalidColumnNames = ["id", "lat", "lon"] as Set<String>
when:
source.factory.checkForInvalidColumnNames(invalidColumnNames)

then:
Exception ex = thrown()
ex.class == InvalidColumnNameException
ex.message == "The provided column names [id, lat, lon] does not match the expected column names [id, coordinate]!"
}
}