Skip to content

Commit

Permalink
Fixes / adds validation to TupleBinding and StandaloneTuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanopticon committed Nov 30, 2023
1 parent 6f2fee6 commit 711a029
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ import org.vitrivr.cottontail.core.types.Value
*/
@Serializable
data class ColumnDef<T : Value>(val name: Name.ColumnName, val type: Types<T>, val nullable: Boolean = true, val primary: Boolean = false, val autoIncrement: Boolean = false) {

/**
* Validates a value with regard to this [ColumnDef] return a flag indicating whether validation was passed.
*
* @param value The value that should be validated.
* @return True if value passes validation, false otherwise.
*/
fun validate(value: Value?): Boolean
= ((value == null && this.nullable) || (value != null && this.type == value.type))

/**
* Creates and returns a [String] representation of this [ColumnDef].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class StandaloneTuple(override var tupleId: TupleId, override val columns: Array

init {
require(this.columns.size == this.values.size) { "Number of values and number of columns must be the same." }
for ((i, c) in this.columns.withIndex()) {
require(c.validate(this.values[i])) { "Value ${this.values[i]} is incompatible with column $c." }
for ((c, v) in this.columns.zip(this.values)) {
require(((v == null && c.nullable) || (v != null && c.type == v.type))) { "Value $v is incompatible with column $c." }
}
}

Expand Down Expand Up @@ -72,10 +72,7 @@ class StandaloneTuple(override var tupleId: TupleId, override val columns: Array
* @param index The index for which to retrieve the value.
* @return The value for the column index.
*/
override fun get(index: Int): Value? {
require(index in (0 until this.size)) { "The specified column $index is out of bounds." }
return this.values[index]
}
override fun get(index: Int): Value? = this.values[index]

/**
* Returns a [List] of [Value]s contained in this [StandaloneTuple].
Expand All @@ -91,8 +88,8 @@ class StandaloneTuple(override var tupleId: TupleId, override val columns: Array
* @param value The new [Value]
*/
override fun set(index: Int, value: Value?) {
require(index in (0 until this.size)) { "The specified column $index is out of bounds." }
require(this.columns[index].validate(value)) { "Provided value $value is incompatible with column ${this.columns[index]}." }
val column = this.columns[index]
require(((value == null && column.nullable) || (value != null && value.type == column.type))) { "Provided value $value is incompatible with column ${this.columns[index]}." }
this.values[index] = value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ import org.vitrivr.cottontail.core.types.Value
* A [Tuple] implementation that depends on the existence of [Binding]s for the [Value]s it contains. Used for inserts.
*
* @author Ralph Gasser
* @version 1.1.0
* @version 1.2.0
*/
class TupleBinding(override var tupleId: TupleId, override val columns: Array<ColumnDef<*>>, private val values: Array<Binding.Literal>, private val context: BindingContext) : MutableTuple {

init {
require(this.columns.size == this.values.size) { "Number of values and number of columns must be the same." }
for ((c, b) in this.columns.zip(this.values)) {
require(c.nullable == b.canBeNull) { "Value binding $b is incompatible with column $c." }
require(c.type == b.type) { "Value binding $b is incompatible with column $c." }
}
}

/**
* Creates a copy of this [TupleBinding]
*/
Expand Down

0 comments on commit 711a029

Please sign in to comment.