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 1 commit
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
- `PartiQLValue` collections are backed by iterables rather than just sequences.

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,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 +459,7 @@ class ServiceLoaderUtil {
ExprToPartiQLValue(
it, ExprToPartiQLValueType(it)
)
}.asSequence()
}
)
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.LIST, ExprToPartiQLValueType(exprValue)
Expand All @@ -472,7 +472,7 @@ class ServiceLoaderUtil {
ExprToPartiQLValue(
it, ExprToPartiQLValueType(it)
)
}.asSequence()
}
)
else -> throw ExprToPartiQLValueTypeMismatchException(
PartiQLValueType.SEXP, ExprToPartiQLValueType(exprValue)
Expand All @@ -485,7 +485,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
22 changes: 11 additions & 11 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,14 @@ public sealed interface ScalarValue<T> : PartiQLValue {
}

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

public val elements: Sequence<T>?
public val elements: Collection<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 +388,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.elements!!
val rhs = other.elements!!
// 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 +421,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.elements!!
val rhs = other.elements!!
return lhs == rhs
}

Expand Down Expand Up @@ -452,8 +452,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.elements!!
val rhs = other.elements!!
return lhs == rhs
}

Expand All @@ -464,11 +464,11 @@ 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, Iterable<Pair<String, T>> {

override val type: PartiQLValueType = PartiQLValueType.STRUCT

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

override val isNull: Boolean
get() = fields == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import org.partiql.value.util.PartiQLValueVisitor

@OptIn(PartiQLValueExperimental::class)
internal class BagValueImpl<T : PartiQLValue>(
private val delegate: Sequence<T>?,
private val delegate: Iterable<T>?,
override val annotations: PersistentList<String>,
) : BagValue<T>() {

override val elements: Sequence<T>? = delegate
override val elements: Collection<T>? = delegate?.toList()

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

override fun copy(annotations: Annotations) = BagValueImpl(delegate, annotations.toPersistentList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import org.partiql.value.util.PartiQLValueVisitor

@OptIn(PartiQLValueExperimental::class)
internal class ListValueImpl<T : PartiQLValue>(
private val delegate: Sequence<T>?,
private val delegate: Iterable<T>?,
override val annotations: PersistentList<String>,
) : ListValue<T>() {

override val elements: Sequence<T>? = delegate
override val elements: Collection<T>? = delegate?.toList()

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

override fun copy(annotations: Annotations) = ListValueImpl(delegate, annotations.toPersistentList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import org.partiql.value.util.PartiQLValueVisitor

@OptIn(PartiQLValueExperimental::class)
internal class SexpValueImpl<T : PartiQLValue>(
private val delegate: Sequence<T>?,
private val delegate: Iterable<T>?,
override val annotations: PersistentList<String>,
) : SexpValue<T>() {

override val elements: Sequence<T>? = delegate
override val elements: Collection<T>? = delegate?.toList()

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

override fun copy(annotations: Annotations) = SexpValueImpl(delegate, annotations.toPersistentList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ import org.partiql.value.StructValue
import org.partiql.value.util.PartiQLValueVisitor

/**
* Implementation of a [StructValue<T>] backed by a Sequence.
* Implementation of a [StructValue<T>] backed by an iterator.
*
* @param T
* @property delegate
* @property annotations
*/
@OptIn(PartiQLValueExperimental::class)
internal class SequenceStructValueImpl<T : PartiQLValue>(
private val delegate: Sequence<Pair<String, T>>?,
internal class IterableStructValueImpl<T : PartiQLValue>(
private val delegate: Iterable<Pair<String, T>>?,
override val annotations: PersistentList<String>,
) : StructValue<T>() {

override val fields: Sequence<Pair<String, T>>? = delegate
override val fields: Collection<Pair<String, T>>? = delegate?.toList()

override operator fun get(key: String): T? {
if (delegate == null) {
Expand All @@ -51,7 +51,7 @@ internal class SequenceStructValueImpl<T : PartiQLValue>(
return delegate.filter { it.first == key }.map { it.second }.asIterable()
}

override fun copy(annotations: Annotations) = SequenceStructValueImpl(delegate, annotations.toPersistentList())
override fun copy(annotations: Annotations) = IterableStructValueImpl(delegate, annotations.toPersistentList())

override fun withAnnotations(annotations: Annotations): StructValue<T> = _withAnnotations(annotations)

Expand All @@ -73,12 +73,12 @@ internal class MultiMapStructValueImpl<T : PartiQLValue>(
override val annotations: PersistentList<String>,
) : StructValue<T>() {

override val fields: Sequence<Pair<String, T>>?
override val fields: Collection<Pair<String, T>>?
get() {
if (delegate == null) {
return null
}
return delegate.asSequence().map { f -> f.value.map { v -> f.key to v } }.flatten()
return delegate.entries.map { f -> f.value.map { v -> f.key to v } }.flatten()
}

override operator fun get(key: String): T? = getAll(key).firstOrNull()
Expand Down Expand Up @@ -112,12 +112,12 @@ internal class MapStructValueImpl<T : PartiQLValue>(
override val annotations: PersistentList<String>,
) : StructValue<T>() {

override val fields: Sequence<Pair<String, T>>?
override val fields: Collection<Pair<String, T>>?
get() {
if (delegate == null) {
return null
}
return delegate.asSequence().map { f -> f.key to f.value }
return delegate.entries.map { f -> f.key to f.value }
}

override operator fun get(key: String): T? {
Expand Down
Loading