Skip to content

Commit

Permalink
Automated commit of generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 31, 2024
1 parent 903f58b commit 439f65d
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,47 @@ public fun <T : Any> DataColumn<T>.convertToDouble(): DataColumn<Double> = conve

public fun <T : Any> DataColumn<T?>.convertToDouble(): DataColumn<Double?> = convertTo()

/** Parses a String column to Double considering locale (number format).
* If [locale] parameter is defined, it's number format is used for parsing.
* If [locale] parameter is null, the current system locale is used.
* If the column cannot be parsed, then the POSIX format is used. */
@JvmName("convertToDoubleFromString")
public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColumn<Double> =
convertToDouble(locale = locale, useFastDoubleParser = false)

/**
* Parse String column to Double considering locale (number format).
* Parses a String column to Double considering locale (number format).
* If [locale] parameter is defined, it's number format is used for parsing.
* If [locale] parameter is null, the current system locale is used. If column can not be parsed, then POSIX format is used.
* If [locale] parameter is null, the current system locale is used.
* If the column cannot be parsed, then the POSIX format is used.
* @param useFastDoubleParser whether to use the new _experimental_ FastDoubleParser, defaults to `false` for now.
*/
@JvmName("convertToDoubleFromString")
public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColumn<Double> =
this.castToNullable().convertToDouble(locale).castToNotNullable()
public fun DataColumn<String>.convertToDouble(
locale: Locale? = null,
useFastDoubleParser: Boolean,
): DataColumn<Double> = this.castToNullable().convertToDouble(locale, useFastDoubleParser).castToNotNullable()

/** Parses a String column to Double considering locale (number format).
* If [locale] parameter is defined, it's number format is used for parsing.
* If [locale] parameter is null, the current system locale is used.
* If the column cannot be parsed, then the POSIX format is used. */
@JvmName("convertToDoubleFromStringNullable")
public fun DataColumn<String?>.convertToDouble(locale: Locale? = null): DataColumn<Double?> =
convertToDouble(locale = locale, useFastDoubleParser = false)

/**
* Parse String column to Double considering locale (number format).
* Parses a String column to Double considering locale (number format).
* If [locale] parameter is defined, it's number format is used for parsing.
* If [locale] parameter is null, the current system locale is used. If column can not be parsed, then POSIX format is used.
* If [locale] parameter is null, the current system locale is used.
* If the column cannot be parsed, then the POSIX format is used.
* @param useFastDoubleParser whether to use the new _experimental_ FastDoubleParser, defaults to `false` for now.
*/
@JvmName("convertToDoubleFromStringNullable")
public fun DataColumn<String?>.convertToDouble(locale: Locale? = null): DataColumn<Double?> {
public fun DataColumn<String?>.convertToDouble(
locale: Locale? = null,
useFastDoubleParser: Boolean,
): DataColumn<Double?> {
fun applyParser(parser: (String) -> Double?): DataColumn<Double?> {
var currentRow = 0
try {
Expand All @@ -220,14 +245,14 @@ public fun DataColumn<String?>.convertToDouble(locale: Locale? = null): DataColu
}

return if (locale != null) {
val explicitParser = Parsers.getDoubleParser(locale)
val explicitParser = Parsers.getDoubleParser(locale, useFastDoubleParser)
applyParser(explicitParser)
} else {
try {
val defaultParser = Parsers.getDoubleParser()
val defaultParser = Parsers.getDoubleParser(useFastDoubleParser = useFastDoubleParser)
applyParser(defaultParser)
} catch (e: TypeConversionException) {
val posixParser = Parsers.getDoubleParser(Locale.forLanguageTag("C.UTF-8"))
val posixParser = Parsers.getDoubleParser(Locale.forLanguageTag("C.UTF-8"), useFastDoubleParser)
applyParser(posixParser)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.jetbrains.kotlinx.dataframe.impl.api.StringParser
import org.jetbrains.kotlinx.dataframe.impl.api.parseImpl
import org.jetbrains.kotlinx.dataframe.impl.api.tryParseImpl
import org.jetbrains.kotlinx.dataframe.typeClass
import org.jetbrains.kotlinx.dataframe.util.PARSER_OPTIONS
import org.jetbrains.kotlinx.dataframe.util.PARSER_OPTIONS_COPY
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -40,13 +42,68 @@ public interface GlobalParserOptions {
public var locale: Locale
}

/**
* ### Options for parsing [String]`?` columns
*
* @param locale locale to use for parsing dates and numbers, defaults to the System default locale.
* If specified instead of [dateTimeFormatter], it will be used in combination with [dateTimePattern]
* to create a [DateTimeFormatter]. Just providing [locale] will not allow you to parse
* locale-specific dates!
* @param dateTimeFormatter a [DateTimeFormatter] to use for parsing dates, if not specified, it will be created
* from [dateTimePattern] and [locale]. If neither [dateTimeFormatter] nor [dateTimePattern] are specified,
* [DateTimeFormatter.ISO_LOCAL_DATE_TIME] will be used.
* @param dateTimePattern a pattern to use for parsing dates. If specified instead of [dateTimeFormatter],
* it will be used to create a [DateTimeFormatter].
* @param nullStrings a set of strings that should be treated as `null` values. By default, it's
* ["null", "NULL", "NA", "N/A"].
* @param useFastDoubleParser whether to use the new _experimental_ FastDoubleParser, defaults to `false` for now.
*/
public data class ParserOptions(
val locale: Locale? = null,
// TODO, migrate to kotlinx.datetime.format.DateTimeFormat? https://github.com/Kotlin/dataframe/issues/876
val dateTimeFormatter: DateTimeFormatter? = null,
val dateTimePattern: String? = null,
val nullStrings: Set<String>? = null,
val useFastDoubleParser: Boolean = false,
) {

/** For binary compatibility. */
@Deprecated(
message = PARSER_OPTIONS,
level = DeprecationLevel.HIDDEN,
)
public constructor(
locale: Locale? = null,
dateTimeFormatter: DateTimeFormatter? = null,
dateTimePattern: String? = null,
nullStrings: Set<String>? = null,
) : this(
locale = locale,
dateTimeFormatter = dateTimeFormatter,
dateTimePattern = dateTimePattern,
nullStrings = nullStrings,
useFastDoubleParser = false,
)

/** For binary compatibility. */
@Deprecated(
message = PARSER_OPTIONS_COPY,
level = DeprecationLevel.HIDDEN,
)
public fun copy(
locale: Locale? = this.locale,
dateTimeFormatter: DateTimeFormatter? = this.dateTimeFormatter,
dateTimePattern: String? = this.dateTimePattern,
nullStrings: Set<String>? = this.nullStrings,
): ParserOptions =
ParserOptions(
locale = locale,
dateTimeFormatter = dateTimeFormatter,
dateTimePattern = dateTimePattern,
nullStrings = nullStrings,
useFastDoubleParser = useFastDoubleParser,
)

internal fun getDateTimeFormatter(): DateTimeFormatter? =
when {
dateTimeFormatter != null -> dateTimeFormatter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,6 @@ import kotlin.annotation.AnnotationTarget.TYPE
import kotlin.annotation.AnnotationTarget.TYPEALIAS
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER

/**
*
* &nbsp;&nbsp;&nbsp;&nbsp;
*
*/
internal interface LineBreak

/** &nbsp; */
internal interface QuarterIndent

/** &nbsp;&nbsp; */
internal interface HalfIndent

/** &nbsp;&nbsp;&nbsp;&nbsp; */
internal interface Indent

/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */
internal interface DoubleIndent

/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */
internal interface TripleIndent

/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */
internal interface QuadrupleIndent

/**
* Any `Documentable` annotated with this annotation will be excluded from the generated sources by
* the documentation processor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import org.jetbrains.kotlinx.dataframe.hasNulls
import org.jetbrains.kotlinx.dataframe.impl.canParse
import org.jetbrains.kotlinx.dataframe.impl.catchSilent
import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
import org.jetbrains.kotlinx.dataframe.impl.io.FastDoubleParser
import org.jetbrains.kotlinx.dataframe.impl.javaDurationCanParse
import org.jetbrains.kotlinx.dataframe.io.isURL
import org.jetbrains.kotlinx.dataframe.io.readJsonStr
import org.jetbrains.kotlinx.dataframe.values
import java.math.BigDecimal
import java.net.URL
import java.text.NumberFormat
import java.text.ParsePosition
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
Expand Down Expand Up @@ -275,29 +275,6 @@ internal object Parsers : GlobalParserOptions {
null
}

private fun String.parseDouble(format: NumberFormat) =
when (uppercase(Locale.getDefault())) {
"NAN" -> Double.NaN

"INF" -> Double.POSITIVE_INFINITY

"-INF" -> Double.NEGATIVE_INFINITY

"INFINITY" -> Double.POSITIVE_INFINITY

"-INFINITY" -> Double.NEGATIVE_INFINITY

else -> {
val parsePosition = ParsePosition(0)
val result: Double? = format.parse(this, parsePosition)?.toDouble()
if (parsePosition.index != this.length) {
null
} else {
result
}
}
}

inline fun <reified T : Any> stringParser(
catch: Boolean = false,
coveredBy: Set<KType> = emptySet(),
Expand All @@ -317,11 +294,15 @@ internal object Parsers : GlobalParserOptions {
): StringParserWithFormat<T> = StringParserWithFormat(typeOf<T>(), coveredBy, body)

private val parserToDoubleWithOptions = stringParserWithOptions { options ->
val numberFormat = NumberFormat.getInstance(options?.locale ?: Locale.getDefault())
val parser = { it: String -> it.parseDouble(numberFormat) }
val fastDoubleParser = FastDoubleParser(options ?: ParserOptions())
val parser = { it: String -> fastDoubleParser.parseOrNull(it) }
parser
}

private val posixDoubleParser = FastDoubleParser(
ParserOptions(locale = Locale.forLanguageTag("C.UTF-8")),
)

internal val parsersOrder = listOf(
// Int
stringParser<Int> { it.toIntOrNull() },
Expand Down Expand Up @@ -384,7 +365,7 @@ internal object Parsers : GlobalParserOptions {
// Double, with explicit number format or taken from current locale
parserToDoubleWithOptions,
// Double, with POSIX format
stringParser<Double> { it.parseDouble(NumberFormat.getInstance(Locale.forLanguageTag("C.UTF-8"))) },
stringParser<Double> { posixDoubleParser.parseOrNull(it) },
// Boolean
stringParser<Boolean> { it.toBooleanOrNull() },
// BigDecimal
Expand Down Expand Up @@ -449,9 +430,9 @@ internal object Parsers : GlobalParserOptions {
return parser.applyOptions(options)
}

internal fun getDoubleParser(locale: Locale? = null): (String) -> Double? {
internal fun getDoubleParser(locale: Locale? = null, useFastDoubleParser: Boolean): (String) -> Double? {
val options = if (locale != null) {
ParserOptions(locale = locale)
ParserOptions(locale = locale, useFastDoubleParser = useFastDoubleParser)
} else {
null
}
Expand Down
Loading

0 comments on commit 439f65d

Please sign in to comment.