Skip to content

Use Iterable for PartiQLValue collections rather than Sequence #1305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Thank you to all who have contributed!
- The new plan is fully resolved and typed.
- Operators will be converted to function call.
- Changes the return type of `filter_distinct` to a list if input collection is list
- Changes the `PartiQLValue` collections to implement Iterable rather than Sequence, allowing for multiple consumption.

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,28 +268,42 @@ class ServiceLoaderUtil {
PartiQLValueType.INTERVAL -> TODO()

PartiQLValueType.BAG -> {
(partiqlValue as? BagValue<*>)?.elements?.map { PartiQLtoExprValue(it) }?.let { newBag(it) }
?: ExprValue.nullValue
if (partiqlValue.isNull) {
ExprValue.nullValue
} else {
newBag((partiqlValue as? BagValue<*>)!!.map { PartiQLtoExprValue(it) })
}
}

PartiQLValueType.LIST -> {
(partiqlValue as? ListValue<*>)?.elements?.map { PartiQLtoExprValue(it) }?.let { newList(it) }
?: ExprValue.nullValue
if (partiqlValue.isNull) {
ExprValue.nullValue
} else {
newList((partiqlValue as? ListValue<*>)!!.map { PartiQLtoExprValue(it) })
}
}

PartiQLValueType.SEXP -> {
(partiqlValue as? SexpValue<*>)?.elements?.map { PartiQLtoExprValue(it) }?.let { newSexp(it) }
?: ExprValue.nullValue
if (partiqlValue.isNull) {
ExprValue.nullValue
} else {
newSexp((partiqlValue as? SexpValue<*>)!!.map { PartiQLtoExprValue(it) })
}
}

PartiQLValueType.STRUCT -> {
(partiqlValue as? StructValue<*>)?.fields?.map {
PartiQLtoExprValue(it.second).namedValue(
newString(
it.first
if (partiqlValue.isNull) {
ExprValue.nullValue
} else {
val entries = (partiqlValue as? StructValue<*>)!!.entries
entries.map {
PartiQLtoExprValue(it.second).namedValue(
newString(
it.first
)
)
)
}?.let { newStruct(it, StructOrdering.ORDERED) } ?: ExprValue.nullValue
}.let { newStruct(it, StructOrdering.ORDERED) }
}
}

PartiQLValueType.DECIMAL -> TODO()
Expand Down Expand Up @@ -447,7 +461,7 @@ class ServiceLoaderUtil {
PartiQLValueType.INTERVAL -> TODO()
PartiQLValueType.BAG -> when (exprValue.type) {
ExprValueType.NULL -> bagValue(null)
ExprValueType.BAG -> bagValue(exprValue.map { ExprToPartiQLValue(it, ExprToPartiQLValueType(it)) }.asSequence())
ExprValueType.BAG -> bagValue(exprValue.map { ExprToPartiQLValue(it, ExprToPartiQLValueType(it)) })
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.BAG, ExprToPartiQLValueType(exprValue)
)
Expand All @@ -459,7 +473,7 @@ class ServiceLoaderUtil {
ExprToPartiQLValue(
it, ExprToPartiQLValueType(it)
)
}.asSequence()
}
)
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.LIST, ExprToPartiQLValueType(exprValue)
Expand All @@ -472,7 +486,7 @@ class ServiceLoaderUtil {
ExprToPartiQLValue(
it, ExprToPartiQLValueType(it)
)
}.asSequence()
}
)
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.SEXP, ExprToPartiQLValueType(exprValue)
Expand All @@ -485,7 +499,7 @@ class ServiceLoaderUtil {
Pair(
it.name?.stringValue() ?: "", ExprToPartiQLValue(it, ExprToPartiQLValueType(it))
)
}.asSequence()
}
)
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.STRUCT, ExprToPartiQLValueType(exprValue)
Expand Down
84 changes: 73 additions & 11 deletions partiql-types/src/main/kotlin/org/partiql/value/PartiQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import org.partiql.value.impl.Int64ValueImpl
import org.partiql.value.impl.Int8ValueImpl
import org.partiql.value.impl.IntValueImpl
import org.partiql.value.impl.IntervalValueImpl
import org.partiql.value.impl.IterableStructValueImpl
import org.partiql.value.impl.ListValueImpl
import org.partiql.value.impl.MapStructValueImpl
import org.partiql.value.impl.MissingValueImpl
import org.partiql.value.impl.MultiMapStructValueImpl
import org.partiql.value.impl.NullValueImpl
import org.partiql.value.impl.SequenceStructValueImpl
import org.partiql.value.impl.SexpValueImpl
import org.partiql.value.impl.StringValueImpl
import org.partiql.value.impl.SymbolValueImpl
Expand Down Expand Up @@ -344,10 +344,25 @@ public fun intervalValue(
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> bagValue(
elements: Sequence<T>?,
elements: Iterable<T>?,
annotations: Annotations = emptyList(),
): BagValue<T> = BagValueImpl(elements, annotations.toPersistentList())

/**
* BAG type value.
*
* @param T
* @param elements
* @param annotations
* @return
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> bagValue(
vararg elements: T,
annotations: Annotations = emptyList(),
): BagValue<T> = BagValueImpl(elements.asIterable(), annotations.toPersistentList())

/**
* LIST type value.
*
Expand All @@ -359,10 +374,25 @@ public fun <T : PartiQLValue> bagValue(
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> listValue(
elements: Sequence<T>?,
elements: Iterable<T>?,
annotations: Annotations = emptyList(),
): ListValue<T> = ListValueImpl(elements, annotations.toPersistentList())

/**
* LIST type value.
*
* @param T
* @param elements
* @param annotations
* @return
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> listValue(
vararg elements: T,
annotations: Annotations = emptyList(),
): ListValue<T> = ListValueImpl(elements.asIterable(), annotations.toPersistentList())

/**
* SEXP type value.
*
Expand All @@ -374,12 +404,42 @@ public fun <T : PartiQLValue> listValue(
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> sexpValue(
elements: Sequence<T>?,
elements: Iterable<T>?,
annotations: Annotations = emptyList(),
): SexpValue<T> = SexpValueImpl(elements, annotations.toPersistentList())

/**
* STRUCT type value.
* SEXP type value.
*
* @param T
* @param elements
* @param annotations
* @return
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> sexpValue(
vararg elements: T,
annotations: Annotations = emptyList(),
): SexpValue<T> = SexpValueImpl(elements.asIterable(), annotations.toPersistentList())

/**
* Create a PartiQL struct value backed by an iterable of key-value field pairs.
*
* @param T
* @param fields
* @param annotations
* @return
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> structValue(
fields: Iterable<Pair<String, T>>?,
annotations: Annotations = emptyList(),
): StructValue<T> = IterableStructValueImpl(fields, annotations.toPersistentList())

/**
* Create a PartiQL struct value backed by an iterable of key-value field pairs.
*
* @param T
* @param fields
Expand All @@ -389,12 +449,13 @@ public fun <T : PartiQLValue> sexpValue(
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> structValue(
fields: Sequence<Pair<String, T>>?,
vararg fields: Pair<String, T>,
annotations: Annotations = emptyList(),
): StructValue<T> = SequenceStructValueImpl(fields, annotations.toPersistentList())
): StructValue<T> = IterableStructValueImpl(fields.toList(), annotations.toPersistentList())

/**
* STRUCT type value.
* Create a PartiQL struct value backed by a multimap of keys with a list of values. This supports having multiple
* values per key, while improving lookup performance compared to using an iterable.
*
* @param T
* @param fields
Expand All @@ -403,13 +464,14 @@ public fun <T : PartiQLValue> structValue(
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> structValueWithDuplicates(
public fun <T : PartiQLValue> structValueMultiMap(
fields: Map<String, Iterable<T>>?,
annotations: Annotations = emptyList(),
): StructValue<T> = MultiMapStructValueImpl(fields, annotations.toPersistentList())

/**
* STRUCT type value.
* Create a PartiQL struct value backed by a map of keys with a list of values. This does not support having multiple
* values per key, but uses a Java HashMap for quicker lookup than an iterable backed StructValue.
*
* @param T
* @param fields
Expand All @@ -418,7 +480,7 @@ public fun <T : PartiQLValue> structValueWithDuplicates(
*/
@JvmOverloads
@PartiQLValueExperimental
public fun <T : PartiQLValue> structValueNoDuplicates(
public fun <T : PartiQLValue> structValueMap(
fields: Map<String, T>?,
annotations: Annotations = emptyList(),
): StructValue<T> = MapStructValueImpl(fields, annotations.toPersistentList())
Expand Down
38 changes: 16 additions & 22 deletions partiql-types/src/main/kotlin/org/partiql/value/PartiQLValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ public sealed interface ScalarValue<T> : PartiQLValue {
}

@PartiQLValueExperimental
public sealed interface CollectionValue<T : PartiQLValue> : PartiQLValue, Sequence<T> {

public val elements: Sequence<T>?
public sealed interface CollectionValue<T : PartiQLValue> : PartiQLValue, Iterable<T> {

override val isNull: Boolean
get() = elements == null

override fun iterator(): Iterator<T> = elements!!.iterator()
override fun iterator(): Iterator<T>

override fun copy(annotations: Annotations): CollectionValue<T>

Expand Down Expand Up @@ -388,8 +385,8 @@ public abstract class BagValue<T : PartiQLValue> : CollectionValue<T> {
if (this.isNull || other.isNull) return this.isNull == other.isNull

// both not null, compare values
val lhs = this.elements!!.toList()
val rhs = other.elements!!.toList()
val lhs = this.toList()
val rhs = other.toList()
// this is incorrect as it assumes ordered-ness, but we don't have a sort or hash yet
return lhs == rhs
}
Expand Down Expand Up @@ -421,8 +418,8 @@ public abstract class ListValue<T : PartiQLValue> : CollectionValue<T> {
if (this.isNull || other.isNull) return this.isNull == other.isNull

// both not null, compare values
val lhs = this.elements!!.toList()
val rhs = other.elements!!.toList()
val lhs = this.toList()
val rhs = other.toList()
return lhs == rhs
}

Expand Down Expand Up @@ -452,8 +449,8 @@ public abstract class SexpValue<T : PartiQLValue> : CollectionValue<T> {
if (this.isNull || other.isNull) return this.isNull == other.isNull

// both not null, compare values
val lhs = this.elements!!.toList()
val rhs = other.elements!!.toList()
val lhs = this.toList()
val rhs = other.toList()
return lhs == rhs
}

Expand All @@ -464,16 +461,15 @@ public abstract class SexpValue<T : PartiQLValue> : CollectionValue<T> {
}

@PartiQLValueExperimental
public abstract class StructValue<T : PartiQLValue> : PartiQLValue, Sequence<Pair<String, T>> {
public abstract class StructValue<T : PartiQLValue> : PartiQLValue {

override val type: PartiQLValueType = PartiQLValueType.STRUCT

public abstract val fields: Sequence<Pair<String, T>>?
public abstract val fields: Iterable<String>

override val isNull: Boolean
get() = fields == null
public abstract val values: Iterable<T>

override fun iterator(): Iterator<Pair<String, T>> = fields!!.iterator()
public abstract val entries: Iterable<Pair<String, T>>

public abstract operator fun get(key: String): T?

Expand All @@ -486,9 +482,7 @@ public abstract class StructValue<T : PartiQLValue> : PartiQLValue, Sequence<Pai
abstract override fun withoutAnnotations(): StructValue<T>

/**
* See equality of IonElement StructElementImpl
*
* https://github.com/amazon-ion/ion-element-kotlin/blob/master/src/com/amazon/ionelement/impl/StructElementImpl.kt
* Checks equality of struct entries, ignoring ordering.
*
* @param other
* @return
Expand All @@ -502,15 +496,15 @@ public abstract class StructValue<T : PartiQLValue> : PartiQLValue, Sequence<Pai
if (this.isNull || other.isNull) return this.isNull == other.isNull

// both not null, compare fields
val lhs = this.fields!!.groupBy({ it.first }, { it.second })
val rhs = other.fields!!.groupBy({ it.first }, { it.second })
val lhs = this.entries.asIterable().groupBy({ it.first }, { it.second })
val rhs = other.entries.asIterable().groupBy({ it.first }, { it.second })

// check size
if (lhs.size != rhs.size) return false
if (lhs.keys != rhs.keys) return false

// check values
lhs.forEach { (key, values) ->
lhs.entries.forEach { (key, values) ->
val lGroup: Map<PartiQLValue, Int> = values.groupingBy { it }.eachCount()
val rGroup: Map<PartiQLValue, Int> = rhs[key]!!.groupingBy { it }.eachCount()
if (lGroup != rGroup) return false
Expand Down
24 changes: 12 additions & 12 deletions partiql-types/src/main/kotlin/org/partiql/value/helpers/ToIon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,31 +258,31 @@ internal object ToIon : PartiQLValueBaseVisitor<IonElement, Unit>() {
override fun visitInterval(v: IntervalValue, ctx: Unit): IonElement = TODO("Not Yet supported")

override fun visitBag(v: BagValue<*>, ctx: Unit): IonElement = v.annotate {
when (val elements = v.elements) {
null -> ionNull(ElementType.LIST)
else -> ionListOf(elements.map { it.accept(ToIon, Unit) }.toList())
when (v.isNull) {
true -> ionNull(ElementType.LIST)
else -> ionListOf(v.map { it.accept(ToIon, Unit) }.toList())
}
}.withAnnotations(BAG_ANNOTATION)

override fun visitList(v: ListValue<*>, ctx: Unit): IonElement = v.annotate {
when (val elements = v.elements) {
null -> ionNull(ElementType.LIST)
else -> ionListOf(elements.map { it.accept(ToIon, Unit) }.toList())
when (v.isNull) {
true -> ionNull(ElementType.LIST)
else -> ionListOf(v.map { it.accept(ToIon, Unit) }.toList())
}
}

override fun visitSexp(v: SexpValue<*>, ctx: Unit): IonElement = v.annotate {
when (val elements = v.elements) {
null -> ionNull(ElementType.SEXP)
else -> ionSexpOf(elements.map { it.accept(ToIon, Unit) }.toList())
when (v.isNull) {
true -> ionNull(ElementType.SEXP)
else -> ionSexpOf(v.map { it.accept(ToIon, Unit) }.toList())
}
}

override fun visitStruct(v: StructValue<*>, ctx: Unit): IonElement = v.annotate {
when (val fields = v.fields) {
null -> ionNull(ElementType.STRUCT)
when (v.isNull) {
true -> ionNull(ElementType.STRUCT)
else -> {
val ionFields = fields.map {
val ionFields = entries.map {
val fk = it.first
val fv = it.second.accept(ToIon, ctx)
field(fk, fv)
Expand Down
Loading