-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1296 schema mapper type conversions (#1311)
* #1296 add TypeConverter class with simple tests and some default converters #1296 support for conversion from primitive to wrapper types for consistency #1296 add TypeConverterContainer and related tests Also some improvements and refactoring on previous changes #1296 improvements and refactoring on previous changes / TypeConverterContainer enhancements / TypeUtils introduced #1296 add 3 more default type converters + tests for DefaultTypeConverters * #1296 add type conversions capability to SchemaMapper * #1296 introduce SchemaMapperBuilder for easier instantiation - this commit also adds more DefaultTypeConverters and some missing tests for DataType.parseToDataType. * #1296 add todos, stuff that needs to be changed before final PR * #1296 add type conversion related SQL filter tests * #1296 add small improvements to TypeConverterContainer class * #1296 complete IgniteSqlFilterClause changes and add tests - also removes `convertExternalValueToString` from `SchemaMapper` and moves it to Filtering use-case as it has nothing to do with schema mapping logic. * #1296 move util.schema.typeConversion folder to util.types * #1296 improve and add pending cleanups to ExternalEntitySchema * #1296 fix failing tests in IgniteSqlFilteringTest * #1296 improve SqlFilterColumnValueParser to use opt-in SqlStringConverterContainer - this allows users to specify custom external-value -> sql-string converters. * #1296 add tests in IgniteSqlFilteringTest for missing Boolean data type * #1296 make FilterColumnValueParser independent of SQL use-case - it is now just a utility to convert column value (String) from ANTLR to external value (original external date type) that will then be used with external data source for filtering. - In the process makes it responsible only for one thing i.e. parse column value (String) to external value's data type. Should not care about how we convert the resulting external value to a String understood by SQL. That logic's been moved back to IgniteSqlFilterClause. - also renames SqlStringConverterContainer to less verbose ToSqlStringContainer. * #1296 make `parseToDataType` to return Option for easier usage with java * #1296 remove ToSqlStringContainer.scala
- Loading branch information
1 parent
dba74d9
commit d618f9c
Showing
32 changed files
with
1,156 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...e-plugin/src/main/scala/org/finos/vuu/feature/ignite/filter/FilterColumnValueParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.finos.vuu.feature.ignite.filter | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
import org.finos.vuu.core.table.{Column, DataType} | ||
import org.finos.vuu.feature.ignite.filter.FilterColumnValueParser.{ErrorMessage, ParsedResult} | ||
import org.finos.vuu.util.schema.{SchemaField, SchemaMapper} | ||
|
||
protected trait FilterColumnValueParser { | ||
def parse(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[Any]] | ||
def parse(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] | ||
} | ||
|
||
protected object FilterColumnValueParser { | ||
def apply(schemaMapper: SchemaMapper): FilterColumnValueParser = { | ||
new ColumnValueParser(schemaMapper) | ||
} | ||
|
||
case class ParsedResult[T](externalField: SchemaField, externalData: T) | ||
|
||
type ErrorMessage = String | ||
|
||
val STRING_DATA_TYPE: Class[String] = classOf[String] | ||
} | ||
|
||
private class ColumnValueParser(private val mapper: SchemaMapper) extends FilterColumnValueParser with StrictLogging { | ||
|
||
override def parse(columnName: String, columnValue: String): Either[ErrorMessage, ParsedResult[Any]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => RawColumnValueParser(f).parse(columnValue).map(ParsedResult(f, _)) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
override def parse(columnName: String, columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] = { | ||
mapper.externalSchemaField(columnName) match { | ||
case Some(f) => parseValues(RawColumnValueParser(f), columnValues) | ||
case None => Left(externalFieldNotFoundError(columnName)) | ||
} | ||
} | ||
|
||
private def parseValues(parser: RawColumnValueParser, | ||
columnValues: List[String]): Either[ErrorMessage, ParsedResult[List[Any]]] = { | ||
val (errors, parsedValues) = columnValues.partitionMap(parser.parse) | ||
val combinedError = errors.mkString("\n") | ||
|
||
if (parsedValues.isEmpty) { | ||
Left(combinedError) | ||
} else { | ||
if (errors.nonEmpty) logger.error( | ||
s"Failed to parse some of the column values corresponding to the column ${parser.column.name}: \n $combinedError" | ||
) | ||
Right(ParsedResult(parser.field, parsedValues)) | ||
} | ||
} | ||
|
||
private def externalFieldNotFoundError(columnName: String): String = | ||
s"Failed to find mapped external field for column `$columnName`" | ||
|
||
private case class RawColumnValueParser(field: SchemaField) { | ||
val column: Column = mapper.tableColumn(field.name).get | ||
|
||
def parse(columnValue: String): Either[ErrorMessage, Any] = { | ||
parseStringToColumnDataType(columnValue).flatMap(convertColumnValueToExternalFieldType) | ||
} | ||
|
||
private def parseStringToColumnDataType(value: String): Either[ErrorMessage, Any] = | ||
DataType.parseToDataType(value, column.dataType) match { | ||
case Some(v) => Right(v) | ||
case None => Left(s"Failed to parse column value '$value' to data type `${column.dataType}`.") | ||
} | ||
|
||
private def convertColumnValueToExternalFieldType(columnValue: Any): Either[ErrorMessage, Any] = | ||
mapper.toMappedExternalFieldType(column.name, columnValue) | ||
.toRight(s"Failed to convert column value `$columnValue` from `${column.dataType}` to external type `${field.dataType}`") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.