Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,13 @@ trait ElasticConversion {
/** SCHEMA-2 (D4, D6-D8) -- the hits of a statement read through its plan ([[HitReading]]): each
* `_source` key the projection touches, each computed column's `fields` entry and each UNNEST
* element field by its column's reader, before any re-read as a temporal; every other key as it
* always was. A date column the hit's `_ignored` names follows Elasticsearch's verdict, per
* value: a cell of one value is NULL, an array keeps the elements its format reads and answers
* the others NULL ([[HitReading.onIgnored]]). A several-element cell a scalar column refuses
* ([[MultiValued.Fail]]) fails the statement, naming the document.
* always was. A top-level column the hit's `_ignored` names follows Elasticsearch's verdict, per
* value, for every kind that takes `ignore_malformed` -- a date, a number, an `ip`, a
* `geo_point`, a `boolean` (SCHEMA-17 fix R-172) --: a cell of one value is NULL, an array keeps
* the elements the kind's own parse accepts and answers the others NULL
* ([[HitReading.onIgnored]], the one lookup); a keyword or a wildcard over `ignore_above` keeps
* its text. A several-element cell a scalar column refuses ([[MultiValued.Fail]]) fails the
* statement, naming the document.
*/
private def parseHitsByPlan(
hits: List[JsonNode],
Expand Down
374 changes: 348 additions & 26 deletions core/src/main/scala/app/softnetwork/elastic/client/HitReading.scala

Large diffs are not rendered by default.

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions documentation/client/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,21 @@ In a column not declared a list, a one-element array is its element and an empty
several values fail the statement (*since `0.25.0`*, see
[Columns holding several values](../sql/dql_statements.md#columns-holding-several-values)); a column declared
`ARRAY<X>` reads a single value as a list of one. A date field whose format core cannot read
for sure (a zone name, a region id) is a VARCHAR, its text as stored; a date value Elasticsearch
ignored (`ignore_malformed`) is NULL, and the other values of the same array keep theirs. An
integral column reads a fractional number or a numeric text as Elasticsearch indexed it, truncated
toward zero. A value a typed column cannot read is answered as stored: a value Elasticsearch did not
index (a number outside its type's range, a `long` text with surrounding spaces) is never narrowed.
for sure (a zone name, a region id) is a VARCHAR, its text as stored. A value Elasticsearch ignored
as malformed (`ignore_malformed`: the hit's `_ignored` names the field) in a top-level field is
NULL: a date, and *since `0.25.0`* a number, an `ip`, a `geo_point` and a `boolean` (where the
Elasticsearch version accepts the option on it), verified on Elasticsearch 7.17 and later; a
`scaled_float` from 8.6, the first version that names it in `_ignored` (before 8.6 its value is
answered as stored). In an array, and in a column declared `ARRAY<X>`, the values Elasticsearch
indexed keep theirs and only the ones it ignored are NULL. A `keyword` or a `wildcard` over its
`ignore_above`, which `_ignored` names too, keeps its text. An integral column reads a fractional
number or a numeric text as Elasticsearch indexed it, truncated toward zero. A value a typed column
cannot read is answered as stored, never narrowed: a value this rule does not reach (an object's
sub-field, though `_ignored` names it; a value Elasticsearch refused without naming it, such as a
`scaled_float` before 8.6), and a number outside a column declared narrower than its field (an
`INT` over a `long`). *Since `0.25.0`*, the JDBC driver's
getters and an Arrow cell of a narrower type fail on such a number, naming it (`Value 3000000000 is
out of range for INT`), where they answered a wrapped value (see [JDBC](jdbc.md#supported-sql)).

Aggregation values follow the same table:

Expand Down
7 changes: 7 additions & 0 deletions documentation/client/jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ computed column the engine declares `DOUBLE` (`x * 2` over a `DOUBLE` column) is
`Integer`s, is reported as `INTEGER`. A value the engine answers as stored keeps its own class (see
[Value classes](gateway.md#value-classes)), and a column of no known type (`ANY`) can carry whole
and decimal numbers alike: there, the column is reported as the type of its first value.
`getByte`, `getShort`, `getInt`, `getLong` and `getFloat` throw `SQLException` on a number outside
their type (*since engine `0.25.0`*: `Value 3000000000 is out of range for INT`) instead of
answering a wrapped value: `getInt` of a `BIGINT` past the integer range, a value answered as stored
in a column declared narrower than its field (an `INT` over a `long`), an `INT` sum past the
integer range, an `unsigned_long` above 2^63 - 1 through `getLong`, a `DOUBLE` beyond the largest
`REAL` through `getFloat`. A fractional number is still truncated toward zero (`getInt` of `42.9` is
`42`), and NaN or an infinity is no integer.
A set operation's columns follow the declared types. On a JOIN result (*since
arrow-extensions `0.3.4`*) the columns carry DuckDB's types, an integer getter throws `SQLException`
on a value outside its type's range instead of answering a wrapped value, `getBigDecimal` of a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class RestHighLevelClientIgnoredValueReadingSpec extends IgnoredValueReadingSpec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class JavaClientIgnoredValueReadingSpec extends IgnoredValueReadingSpec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

class JavaClientIgnoredValueReadingSpec extends IgnoredValueReadingSpec
125 changes: 108 additions & 17 deletions sql/src/main/scala/app/softnetwork/elastic/sql/type/ValueCoercion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ object ValueCoercion {
}

def coerceToInt(value: Any): java.lang.Integer = value match {
case null => null
case n: Number => n.intValue()
case s: String => java.lang.Integer.valueOf(s)
case b: Boolean => if (b) 1 else 0
case null => null
// SCHEMA-17 fix R-177: the canonical class as it is, any other number within INT's range
case i: java.lang.Integer => i
case n: Number => java.lang.Integer.valueOf(whole(n, IntRange).toInt)
case s: String => java.lang.Integer.valueOf(s)
case b: Boolean => if (b) 1 else 0
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to INT")
}

def coerceToLong(value: Any): java.lang.Long = value match {
case null => null
case n: Number => n.longValue()
case s: String => java.lang.Long.valueOf(s)
case b: Boolean => if (b) 1L else 0L
case null => null
case l: java.lang.Long => l
case n: Number => java.lang.Long.valueOf(whole(n, BigIntRange))
case s: String => java.lang.Long.valueOf(s)
case b: Boolean => if (b) 1L else 0L
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to BIGINT")
}
Expand All @@ -98,9 +101,10 @@ object ValueCoercion {
}

def coerceToFloat(value: Any): java.lang.Float = value match {
case null => null
case n: Number => n.floatValue()
case s: String => java.lang.Float.valueOf(s)
case null => null
case f: java.lang.Float => f
case n: Number => java.lang.Float.valueOf(real(n))
case s: String => java.lang.Float.valueOf(s)
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to REAL")
}
Expand All @@ -115,17 +119,19 @@ object ValueCoercion {
}

def coerceToByte(value: Any): java.lang.Byte = value match {
case null => null
case n: Number => n.byteValue()
case s: String => java.lang.Byte.valueOf(s)
case null => null
case b: java.lang.Byte => b
case n: Number => java.lang.Byte.valueOf(whole(n, TinyIntRange).toByte)
case s: String => java.lang.Byte.valueOf(s)
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to TINYINT")
}

def coerceToShort(value: Any): java.lang.Short = value match {
case null => null
case n: Number => n.shortValue()
case s: String => java.lang.Short.valueOf(s)
case null => null
case s: java.lang.Short => s
case n: Number => java.lang.Short.valueOf(whole(n, SmallIntRange).toShort)
case s: String => java.lang.Short.valueOf(s)
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to SMALLINT")
}
Expand Down Expand Up @@ -224,4 +230,89 @@ object ValueCoercion {
case _ =>
throw new java.sql.SQLException(s"Cannot convert ${value.getClass.getName} to TIMESTAMP")
}

// ─── Range guard (SCHEMA-17 fix R-177) ──────────────────────────────────────
//
// [[coerceToByte]], [[coerceToShort]], [[coerceToInt]], [[coerceToLong]] and [[coerceToFloat]]
// never narrow a number silently: one outside the target type's range throws
// `SQLException("Value <v> is out of range for <TYPE>")`, as DuckDB's conversion does, where
// `intValue()` & co. wrapped it (JDBC `getInt`, arrow's `Int32` cell: 3000000000 read
// -1294967296). A value within the range converts as it always did, its canonical class first
// and as it is (no new allocation); a fractional value is truncated toward zero as before, and
// it is the truncated value that must fit (2147483647.9 is 2147483647 for INT).

/** The range of an integral SQL type, for [[whole]]: `bits` the most a two's-complement value of
* it holds besides its sign; `below` / `above` the double values strictly between which a number
* truncated toward zero is in range (below `Long.MinValue`, the next double down).
*/
private final class IntegralRange(val name: String, val min: Long, val max: Long, val bits: Int) {
val below: Double = if (min == Long.MinValue) Math.nextDown(min.toDouble) else min - 1d
val above: Double = max + 1d
val belowDecimal: java.math.BigDecimal =
java.math.BigDecimal.valueOf(min).subtract(java.math.BigDecimal.ONE)
val aboveDecimal: java.math.BigDecimal =
java.math.BigDecimal.valueOf(max).add(java.math.BigDecimal.ONE)
}

private val TinyIntRange = new IntegralRange("TINYINT", Byte.MinValue, Byte.MaxValue, 7)
private val SmallIntRange = new IntegralRange("SMALLINT", Short.MinValue, Short.MaxValue, 15)
private val IntRange = new IntegralRange("INT", Int.MinValue, Int.MaxValue, 31)
private val BigIntRange = new IntegralRange("BIGINT", Long.MinValue, Long.MaxValue, 63)

/** The largest REAL, exactly, as a decimal and as an integer (it is a whole number). */
private val RealMaxDecimal: java.math.BigDecimal =
new java.math.BigDecimal(Float.MaxValue.toDouble)
private val RealMinDecimal: java.math.BigDecimal = RealMaxDecimal.negate()
private val RealMaxInteger: java.math.BigInteger = RealMaxDecimal.toBigInteger
private val RealMinInteger: java.math.BigInteger = RealMaxInteger.negate()

private def outOfRange(value: Number, sqlType: String): java.sql.SQLException =
new java.sql.SQLException(s"Value $value is out of range for $sqlType")

/** `n` truncated toward zero, where that is within `range`; else the out-of-range failure. A
* whole number of the JDK is judged exactly, a `BigInteger` by its bit length, a `BigDecimal`
* against the bounds (a fraction of magnitude below 1 is 0, never divided by its scale's power
* of ten); a `Double`, a `Float` and any other number by its double value, so NaN and the
* infinities are out of range.
*/
private def whole(n: Number, range: IntegralRange): Long = n match {
case _: java.lang.Integer | _: java.lang.Long | _: java.lang.Short | _: java.lang.Byte |
_: java.util.concurrent.atomic.AtomicInteger | _: java.util.concurrent.atomic.AtomicLong |
_: java.util.concurrent.atomic.LongAdder | _: java.util.concurrent.atomic.LongAccumulator =>
val l = n.longValue()
if (l < range.min || l > range.max) throw outOfRange(n, range.name) else l
case b: java.math.BigInteger =>
if (b.bitLength() > range.bits) throw outOfRange(n, range.name) else b.longValue()
case b: scala.math.BigInt => whole(b.bigInteger, range)
case d: java.math.BigDecimal =>
if (d.compareTo(range.belowDecimal) <= 0 || d.compareTo(range.aboveDecimal) >= 0)
throw outOfRange(n, range.name)
else if (d.precision().toLong - d.scale() <= 0) 0L
else d.longValue()
case d: scala.math.BigDecimal => whole(d.bigDecimal, range)
case _ =>
val d = n.doubleValue()
if (d > range.below && d < range.above) n.longValue() else throw outOfRange(n, range.name)
}

/** `n` as a REAL, where it is within ±`Float.MaxValue`; else the out-of-range failure. NaN and
* the infinities of a floating number stay what they are; a whole number or a decimal beyond the
* bounds, which `floatValue()` turned into an infinity, fails.
*/
private def real(n: Number): Float = n match {
case b: java.math.BigInteger =>
if (b.compareTo(RealMaxInteger) > 0 || b.compareTo(RealMinInteger) < 0)
throw outOfRange(n, "REAL")
else b.floatValue()
case b: scala.math.BigInt => real(b.bigInteger)
case d: java.math.BigDecimal =>
if (d.compareTo(RealMaxDecimal) > 0 || d.compareTo(RealMinDecimal) < 0)
throw outOfRange(n, "REAL")
else d.floatValue()
case d: scala.math.BigDecimal => real(d.bigDecimal)
case _ =>
val d = n.doubleValue()
if (java.lang.Double.isFinite(d) && Math.abs(d) > Float.MaxValue) throw outOfRange(n, "REAL")
else n.floatValue()
}
}
Loading
Loading