Skip to content

Commit

Permalink
[v1] Rename SPI's type Field to PTypeField
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Jan 24, 2025
1 parent da32279 commit 028bf0b
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.jupiter.params.provider.MethodSource
import org.partiql.eval.Mode
import org.partiql.eval.compiler.PartiQLCompiler
import org.partiql.spi.types.PType
import org.partiql.spi.types.PTypeField
import org.partiql.spi.value.Datum
import org.partiql.spi.value.Field
import org.partiql.value.PartiQLValue
Expand Down Expand Up @@ -1437,7 +1438,7 @@ class PartiQLEvaluatorTest {
globals = listOf(
Global(
name = "t",
type = PType.row(org.partiql.spi.types.Field.of("a", PType.integer())),
type = PType.row(PTypeField.of("a", PType.integer())),
value = Datum.row(Field.of("a", Datum.integer(3)))
),
)
Expand Down
10 changes: 5 additions & 5 deletions partiql-plan/api/partiql-plan.api
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,12 @@ public abstract class org/partiql/plan/rel/RelSort : org/partiql/plan/rel/RelBas
public final class org/partiql/plan/rel/RelType {
public static final field ORDERED I
public fun getDegree ()I
public fun getField (I)Lorg/partiql/spi/types/Field;
public fun getField (Ljava/lang/String;)Lorg/partiql/spi/types/Field;
public fun getFields ()[Lorg/partiql/spi/types/Field;
public fun getField (I)Lorg/partiql/spi/types/PTypeField;
public fun getField (Ljava/lang/String;)Lorg/partiql/spi/types/PTypeField;
public fun getFields ()[Lorg/partiql/spi/types/PTypeField;
public fun isOrdered ()Z
public static fun of ([Lorg/partiql/spi/types/Field;)Lorg/partiql/plan/rel/RelType;
public static fun of ([Lorg/partiql/spi/types/Field;I)Lorg/partiql/plan/rel/RelType;
public static fun of ([Lorg/partiql/spi/types/PTypeField;)Lorg/partiql/plan/rel/RelType;
public static fun of ([Lorg/partiql/spi/types/PTypeField;I)Lorg/partiql/plan/rel/RelType;
}

public abstract class org/partiql/plan/rel/RelUnion : org/partiql/plan/rel/RelBase {
Expand Down
18 changes: 9 additions & 9 deletions partiql-plan/src/main/java/org/partiql/plan/rel/RelType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.partiql.plan.rel;

import org.jetbrains.annotations.NotNull;
import org.partiql.spi.types.Field;
import org.partiql.spi.types.PTypeField;

/**
* Analogous to a ROW type, consider cardinality estimates or other hint mechanisms.
Expand All @@ -10,21 +10,21 @@ public final class RelType {

public static final int ORDERED = 0x01;

private final Field[] fields;
private final PTypeField[] fields;
private final boolean ordered;

private RelType(Field[] fields, boolean ordered) {
private RelType(PTypeField[] fields, boolean ordered) {
this.fields = fields;
this.ordered = ordered;
}

@NotNull
public static RelType of(Field... fields) {
public static RelType of(PTypeField... fields) {
return of(fields, 0);
}

@NotNull
public static RelType of(Field[] fields, int properties) {
public static RelType of(PTypeField[] fields, int properties) {
boolean ordered = (properties & ORDERED) != 0;
return new RelType(fields, ordered);
}
Expand All @@ -38,21 +38,21 @@ public int getDegree() {
}

@NotNull
public Field[] getFields() {
public PTypeField[] getFields() {
return fields;
}

@NotNull
public Field getField(int index) {
public PTypeField getField(int index) {
if (index < 0 || index >= fields.length) {
throw new IllegalArgumentException("field index out of bounds: " + index);
}
return fields[index]; // bounds check?
}

@NotNull
public Field getField(String name) {
for (Field field : fields) {
public PTypeField getField(String name) {
for (PTypeField field : fields) {
if (field.getName().equals(name)) {
return field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import org.partiql.planner.internal.ir.Rel
import org.partiql.planner.internal.ir.SetQuantifier
import org.partiql.planner.internal.ir.visitor.PlanBaseVisitor
import org.partiql.spi.errors.PErrorListener
import org.partiql.spi.types.Field
import org.partiql.spi.types.PType
import org.partiql.spi.types.PTypeField
import org.partiql.planner.internal.ir.PartiQLPlan as IPlan
import org.partiql.planner.internal.ir.PlanNode as INode
import org.partiql.planner.internal.ir.Rel as IRel
Expand Down Expand Up @@ -226,7 +226,7 @@ internal class PlanTransform(private val flags: Set<PlannerFlag>) {

override fun visitRel(node: IRel, ctx: PType): org.partiql.plan.rel.Rel {
val o = visitRelOp(node.op, ctx)
val fields = node.type.schema.map { Field.of(it.name, it.type) }.toTypedArray()
val fields = node.type.schema.map { PTypeField.of(it.name, it.type) }.toTypedArray()
val properties = if (node.type.props.contains(Rel.Prop.ORDERED)) RelType.ORDERED else 0
o.type = RelType.of(fields, properties)
return o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ internal class CompilerType(
internal val isMissingValue: Boolean = false
) : PType(_delegate.code()) {
fun getDelegate(): PType = _delegate
override fun getFields(): MutableCollection<Field> {
override fun getFields(): MutableCollection<PTypeField> {
return _delegate.fields.map { field ->
when (field) {
is Field -> field
else -> Field(field.name, CompilerType(field.type))
is PTypeField -> field
else -> PTypeField(field.name, CompilerType(field.type))
}
}.toMutableList()
}
Expand Down Expand Up @@ -53,16 +53,16 @@ internal class CompilerType(
return _delegate.toString()
}

internal class Field(
internal class PTypeField(
private val _name: String,
private val _type: CompilerType
) : org.partiql.spi.types.Field {
) : org.partiql.spi.types.PTypeField {
override fun getName(): String = _name
override fun getType(): CompilerType = _type

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is org.partiql.spi.types.Field) return false
if (other !is org.partiql.spi.types.PTypeField) return false
val nameMatches = _name == other.name
val typeMatches = _type == other.type
return nameMatches && typeMatches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import org.partiql.spi.Context
import org.partiql.spi.catalog.Identifier
import org.partiql.spi.errors.PError
import org.partiql.spi.errors.PErrorListener
import org.partiql.spi.types.Field
import org.partiql.spi.types.PType
import org.partiql.spi.types.PTypeField
import org.partiql.spi.value.Datum
import kotlin.math.max

Expand Down Expand Up @@ -149,7 +149,7 @@ internal class PlanTyper(private val env: Env, config: Context) {
}
fields.forEachIndexed { index, field -> fieldTypes[index].add(field.type.toCType()) }
}
val newFields = fieldTypes.mapIndexed { i, types -> Field.of(fieldNames[i], anyOfLiterals(types)!!) }
val newFields = fieldTypes.mapIndexed { i, types -> PTypeField.of(fieldNames[i], anyOfLiterals(types)!!) }
return PType.row(newFields)
}

Expand Down Expand Up @@ -986,18 +986,18 @@ internal class PlanTyper(private val env: Env, config: Context) {
rexOpStructField(k, v)
}
var structIsClosed = true
val structTypeFields = mutableListOf<CompilerType.Field>()
val structTypeFields = mutableListOf<CompilerType.PTypeField>()
for (field in fields) {
val keyOp = field.k.op
// TODO: Check key type
if (keyOp !is Rex.Op.Lit || !keyOp.value.isTextValue()) {
structIsClosed = false
continue
}
structTypeFields.add(CompilerType.Field(keyOp.value.string, field.v.type))
structTypeFields.add(CompilerType.PTypeField(keyOp.value.string, field.v.type))
}
val type = when (structIsClosed) {
true -> CompilerType(PType.row(structTypeFields as Collection<Field>))
true -> CompilerType(PType.row(structTypeFields as Collection<PTypeField>))
false -> CompilerType(PType.struct())
}
return rex(type, rexOpStruct(fields))
Expand Down Expand Up @@ -1185,7 +1185,7 @@ internal class PlanTyper(private val env: Env, config: Context) {
* unique attributes.
*/
private fun calculateTupleUnionOutputType(args: List<CompilerType>): CompilerType? {
val fields = mutableListOf<CompilerType.Field>()
val fields = mutableListOf<CompilerType.PTypeField>()
var structIsOpen = false
var containsDynamic = false
var containsNonStruct = false
Expand All @@ -1205,7 +1205,7 @@ internal class PlanTyper(private val env: Env, config: Context) {
containsNonStruct -> null
containsDynamic -> CompilerType(PType.dynamic())
structIsOpen -> CompilerType(PType.struct())
else -> CompilerType(PType.row(fields as Collection<Field>))
else -> CompilerType(PType.row(fields as Collection<PTypeField>))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ internal object TypeUtils {
val output = fields.mapNotNull { field ->
val newField = if (substeps.isEmpty()) {
if (lastStepOptional) {
CompilerType.Field(field.name, field.type)
CompilerType.PTypeField(field.name, field.type)
} else {
null
}
} else {
val k = field.name
val v = field.type.exclude(substeps, lastStepOptional)
CompilerType.Field(k, v)
CompilerType.PTypeField(k, v)
}
when (type) {
is Rel.Op.Exclude.Type.StructSymbol -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import org.partiql.spi.Context
import org.partiql.spi.catalog.Catalog
import org.partiql.spi.catalog.Session
import org.partiql.spi.catalog.Table
import org.partiql.spi.types.Field
import org.partiql.spi.types.PType
import org.partiql.spi.types.PTypeField
import org.partiql.types.BagType
import org.partiql.types.StaticType
import org.partiql.types.StructType
Expand All @@ -34,8 +34,8 @@ internal class PlannerPErrorReportingTests {
.define(Table.empty("atomic", PType.smallint()))
.define(Table.empty("collection_no_missing_atomic", PType.bag(PType.smallint())))
.define(Table.empty("collection_contain_missing_atomic", PType.bag(PType.smallint())))
.define(Table.empty("struct_no_missing", PType.row(listOf(Field.of("f1", PType.smallint())))))
.define(Table.empty("struct_with_missing", PType.row(listOf(Field.of("f1", PType.smallint())))))
.define(Table.empty("struct_no_missing", PType.row(listOf(PTypeField.of("f1", PType.smallint())))))
.define(Table.empty("struct_with_missing", PType.row(listOf(PTypeField.of("f1", PType.smallint())))))
.build()

private val session = Session.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ class PlanTyperTest {
)

private val LITERAL_STRUCT_1_FIRST_KEY_TYPE = PType.row(
listOf(CompilerType.Field("sEcoNd_KEY", INT4)),
listOf(CompilerType.PTypeField("sEcoNd_KEY", INT4)),
).toCType()

private val LITERAL_STRUCT_1_TYPED: Rex
get() {
val topLevelStruct = PType.row(
listOf(CompilerType.Field("FiRsT_KeY", LITERAL_STRUCT_1_FIRST_KEY_TYPE)),
listOf(CompilerType.PTypeField("FiRsT_KeY", LITERAL_STRUCT_1_FIRST_KEY_TYPE)),
).toCType()
return rex(
type = topLevelStruct,
Expand All @@ -95,17 +95,17 @@ class PlanTyperTest {

private val ORDERED_DUPLICATES_STRUCT = PType.row(
listOf(
CompilerType.Field("definition", STRING),
CompilerType.Field("definition", DOUBLE_PRECISION),
CompilerType.Field("DEFINITION", DECIMAL),
CompilerType.PTypeField("definition", STRING),
CompilerType.PTypeField("definition", DOUBLE_PRECISION),
CompilerType.PTypeField("DEFINITION", DECIMAL),
),
).toCType()

private val DUPLICATES_STRUCT = PType.row(
listOf(
CompilerType.Field("definition", STRING),
CompilerType.Field("definition", DOUBLE_PRECISION),
CompilerType.Field("DEFINITION", DECIMAL),
CompilerType.PTypeField("definition", STRING),
CompilerType.PTypeField("definition", DOUBLE_PRECISION),
CompilerType.PTypeField("DEFINITION", DECIMAL),
),
).toCType()

Expand Down
Loading

0 comments on commit 028bf0b

Please sign in to comment.