Skip to content
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

Removes sexp and symbol from PType and Datum #1633

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
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 @@ -350,7 +350,7 @@ internal class StandardCompiler(strategies: List<Strategy>) : PartiQLCompiler {
// Compile the candidates
val candidates = Array(functions.size) {
val fn = functions[it]
val fnArity = fn.parameters.size
val fnArity = fn.getParameters().size
if (arity == -1) {
// set first
arity = fnArity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal object ValueUtility {
*/
fun Datum.getText(): String {
return when (this.type.kind) {
PType.Kind.STRING, PType.Kind.SYMBOL, PType.Kind.CHAR -> this.string
PType.Kind.STRING, PType.Kind.CHAR -> this.string
else -> throw TypeCheckException("Expected text, but received ${this.type}.")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal class RelOpExclude(
*/
private fun Datum.exclude(exclusions: List<Exclusion.Item>): Datum = when (this.type.kind) {
PType.Kind.ROW, PType.Kind.STRUCT -> this.structExclude(exclusions)
PType.Kind.BAG, PType.Kind.ARRAY, PType.Kind.SEXP -> this.collExclude(exclusions)
PType.Kind.BAG, PType.Kind.ARRAY -> this.collExclude(exclusions)
else -> this
}

Expand Down Expand Up @@ -140,7 +140,6 @@ internal class RelOpExclude(
return when (type.kind) {
PType.Kind.ARRAY -> Datum.array(coll)
PType.Kind.BAG -> Datum.bag(coll)
PType.Kind.SEXP -> Datum.sexp(coll)
else -> error("Collection type required")
}
}
Expand Down Expand Up @@ -173,7 +172,7 @@ internal class RelOpExclude(
// apply exclusions to subtree
var value = element
// apply collection index exclusions at deeper levels for lists and sexps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove reference to sexps here

Suggested change
// apply collection index exclusions at deeper levels for lists and sexps
// apply collection index exclusions at deeper levels for lists

if (type.kind == PType.Kind.ARRAY || type.kind == PType.Kind.SEXP) {
if (type.kind == PType.Kind.ARRAY) {
branches.getCollIndex(i)?.let {
value = value.exclude(it.getItems())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class RelOpIterate(
close()
throw TypeCheckException()
}
PType.Kind.ARRAY, PType.Kind.SEXP -> r.iterator()
PType.Kind.ARRAY -> r.iterator()
else -> {
close()
throw TypeCheckException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class RelOpIteratePermissive(
isIndexable = false
r.iterator()
}
PType.Kind.ARRAY, PType.Kind.SEXP -> r.iterator()
PType.Kind.ARRAY -> r.iterator()
else -> {
isIndexable = false
iterator { yield(r) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class RelOpScan(
override fun open(env: Environment) {
val r = expr.eval(env.push(Row()))
records = when (r.type.kind) {
PType.Kind.ARRAY, PType.Kind.BAG, PType.Kind.SEXP -> RecordValueIterator(r.iterator())
PType.Kind.ARRAY, PType.Kind.BAG -> RecordValueIterator(r.iterator())
else -> {
close()
throw TypeCheckException("Unexpected type for scan: ${r.type}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class RelOpScanPermissive(
override fun open(env: Environment) {
val r = expr.eval(env.push(Row()))
records = when (r.type.kind) {
PType.Kind.BAG, PType.Kind.ARRAY, PType.Kind.SEXP -> RecordValueIterator(r.iterator())
PType.Kind.BAG, PType.Kind.ARRAY -> RecordValueIterator(r.iterator())
else -> iterator { yield(Row(arrayOf(r))) }
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.partiql.eval.internal.operator.rex.ExprCallDynamic.Candidate
import org.partiql.eval.internal.operator.rex.ExprCallDynamic.CoercionFamily.DYNAMIC
import org.partiql.eval.internal.operator.rex.ExprCallDynamic.CoercionFamily.UNKNOWN
import org.partiql.spi.function.Function
import org.partiql.spi.function.Parameter
import org.partiql.spi.value.Datum
import org.partiql.types.PType
import org.partiql.value.PartiQLValue
Expand All @@ -31,7 +32,7 @@ import org.partiql.value.PartiQLValue
*/
internal class ExprCallDynamic(
private val name: String,
private val functions: Array<Function.Instance>,
private val functions: Array<Function>,
private val args: Array<ExprValue>
) : ExprValue {

Expand All @@ -48,7 +49,7 @@ internal class ExprCallDynamic(
*
* TODO actually make this an array instead of lists.
*/
private val paramTypes: List<List<PType>> = functions.map { c -> c.parameters.toList() }
private val paramTypes: List<List<Parameter>> = functions.map { c -> c.getParameters().toList() }

/**
* @property paramFamilies is a two-dimensional array.
Expand All @@ -58,7 +59,7 @@ internal class ExprCallDynamic(
*
* TODO actually make this an array instead of lists.
*/
private val paramFamilies: List<List<CoercionFamily>> = functions.map { c -> c.parameters.map { p -> family(p.kind) } }
private val paramFamilies: List<List<CoercionFamily>> = functions.map { c -> c.getParameters().map { p -> family(p.getType().kind) } }

/**
* A memoization cache for the [match] function.
Expand Down Expand Up @@ -92,7 +93,7 @@ internal class ExprCallDynamic(
for (paramIndex in paramIndices) {
val argType = args[paramIndex]
val paramType = paramTypes[candidateIndex][paramIndex]
if (paramType == argType) { currentExactMatches++ }
if (paramType.getMatch(argType) == argType) { currentExactMatches++ }
val argFamily = argFamilies[paramIndex]
val paramFamily = paramFamilies[candidateIndex][paramIndex]
if (paramFamily != argFamily && argFamily != CoercionFamily.UNKNOWN && paramFamily != CoercionFamily.DYNAMIC) { return@forEach }
Expand All @@ -102,7 +103,7 @@ internal class ExprCallDynamic(
exactMatches = currentExactMatches
}
}
return if (currentMatch == null) null else Candidate(functions[currentMatch!!])
return if (currentMatch == null) null else Candidate(functions[currentMatch!!].getInstance(args.toTypedArray()))
}

/**
Expand Down Expand Up @@ -145,7 +146,6 @@ internal class ExprCallDynamic(
PType.Kind.REAL -> CoercionFamily.NUMBER
PType.Kind.DOUBLE -> CoercionFamily.NUMBER
PType.Kind.DECIMAL -> CoercionFamily.NUMBER
PType.Kind.DECIMAL_ARBITRARY -> CoercionFamily.NUMBER
PType.Kind.STRING -> CoercionFamily.STRING
PType.Kind.BOOL -> CoercionFamily.BOOLEAN
PType.Kind.TIMEZ -> CoercionFamily.TIME
Expand All @@ -155,13 +155,11 @@ internal class ExprCallDynamic(
PType.Kind.DATE -> CoercionFamily.DATE
PType.Kind.STRUCT -> CoercionFamily.STRUCTURE
PType.Kind.ARRAY -> CoercionFamily.COLLECTION
PType.Kind.SEXP -> CoercionFamily.COLLECTION
PType.Kind.BAG -> CoercionFamily.COLLECTION
PType.Kind.ROW -> CoercionFamily.STRUCTURE
PType.Kind.CHAR -> CoercionFamily.STRING
PType.Kind.VARCHAR -> CoercionFamily.STRING
PType.Kind.DYNAMIC -> DYNAMIC // TODO: REMOVE
PType.Kind.SYMBOL -> CoercionFamily.STRING
PType.Kind.BLOB -> CoercionFamily.BINARY
PType.Kind.CLOB -> CoercionFamily.STRING
PType.Kind.UNKNOWN -> UNKNOWN // TODO: REMOVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ internal class ExprPathIndex(
val input = root.eval(env)
val iterator = when (input.type.kind) {
PType.Kind.BAG,
PType.Kind.ARRAY,
PType.Kind.SEXP -> input.iterator()
PType.Kind.ARRAY -> input.iterator()
else -> throw TypeCheckException("expected collection, found ${input.type.kind}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ExprStructPermissive(private val fields: List<ExprStructField>) :
return null
}
return when (this.type.kind) {
PType.Kind.STRING, PType.Kind.SYMBOL, PType.Kind.CHAR -> this.string
PType.Kind.STRING, PType.Kind.CHAR -> this.string
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import org.partiql.value.missingValue
import org.partiql.value.nullValue
import org.partiql.value.stringValue
import org.partiql.value.structValue
import org.partiql.value.symbolValue
import java.io.ByteArrayOutputStream
import java.math.BigDecimal
import kotlin.test.assertEquals
Expand Down Expand Up @@ -123,12 +122,6 @@ class PartiQLEvaluatorTest {
decimalValue(BigDecimal.valueOf(3000, 1)),
)
),
SuccessTestCase(
input = """
CAST(20 AS SYMBOL);
""".trimIndent(),
expected = symbolValue("20"),
),
// TODO: Use Datum for assertions. Currently, PartiQLValue doesn't support parameterized CHAR/VARCHAR
// SuccessTestCase(
// input = """
Expand Down Expand Up @@ -1396,7 +1389,7 @@ class PartiQLEvaluatorTest {
try {
val (strictResult, _) = run(mode = Mode.STRICT())
when (strictResult.type.kind) {
PType.Kind.BAG, PType.Kind.ARRAY, PType.Kind.SEXP -> strictResult.toList()
PType.Kind.BAG, PType.Kind.ARRAY -> strictResult.toList()
else -> strictResult
}
} catch (e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.jupiter.params.provider.MethodSource
import org.partiql.eval.Environment
import org.partiql.eval.internal.helpers.ValueUtility.check
import org.partiql.spi.function.Function
import org.partiql.spi.function.Parameter
import org.partiql.spi.value.Datum
import org.partiql.spi.value.Datum.array
import org.partiql.spi.value.Datum.bag
Expand Down Expand Up @@ -62,12 +63,29 @@ class ExprCallDynamicTest {
)

@OptIn(PartiQLValueExperimental::class)
internal val functions: Array<Function.Instance> = params.mapIndexed { index, it ->
object : Function.Instance(
returns = PType.integer(),
parameters = arrayOf(it.first.toPType(), it.second.toPType())
) {
override fun invoke(args: Array<Datum>): Datum = integer(index)
internal val functions: Array<Function> = params.mapIndexed { index, it ->
object : Function {

override fun getName(): String {
return "example"
}

override fun getParameters(): Array<Parameter> {
return arrayOf(Parameter("lhs", it.first.toPType()), Parameter("rhs", it.second.toPType()))
}

override fun getReturnType(args: Array<PType>): PType {
return PType.integer()
}

override fun getInstance(args: Array<PType>): Function.Instance {
return object : Function.Instance(
returns = PType.integer(),
parameters = arrayOf(it.first.toPType(), it.second.toPType())
) {
override fun invoke(args: Array<Datum>): Datum = integer(index)
}
}
}
}.toTypedArray()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,8 @@ internal class PartiQLParserDefault : PartiQLParser {
}

override fun visitSequenceConstructor(ctx: GeneratedParser.SequenceConstructorContext) = translate(ctx) {
error("Sequence constructor not supported")
val expressions = visitOrEmpty<Expr>(ctx.expr())
exprArray(expressions)
}

private fun PathStep.copy(next: PathStep?) = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public interface PlanFactory {
* @param args
* @return
*/
public fun rexCallDynamic(name: String, functions: List<Function.Instance>, args: List<Rex>): RexCallDynamic =
public fun rexCallDynamic(name: String, functions: List<Function>, args: List<Rex>): RexCallDynamic =
RexCallDynamicImpl(name, functions, args)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface RexCallDynamic : Rex {
/**
* Returns the functions to dispatch to.
*/
public fun getFunctions(): List<Function.Instance>
public fun getFunctions(): List<Function>

/**
* Returns the list of function arguments.
Expand All @@ -33,7 +33,7 @@ public interface RexCallDynamic : Rex {
*/
internal class RexCallDynamicImpl(
private var name: String,
private var functions: List<Function.Instance>,
private var functions: List<Function>,
private var args: List<Rex>,
) : RexCallDynamic {

Expand All @@ -42,7 +42,7 @@ internal class RexCallDynamicImpl(

override fun getName(): String = name

override fun getFunctions(): List<Function.Instance> = functions
override fun getFunctions(): List<Function> = functions

override fun getArgs(): List<Rex> = args

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ internal object FnComparator : Comparator<Function> {
Kind.DECIMAL,
Kind.REAL,
Kind.DOUBLE,
Kind.DECIMAL_ARBITRARY, // Arbitrary precision decimal has a higher precedence than FLOAT
Kind.CHAR,
Kind.VARCHAR,
Kind.SYMBOL,
Kind.STRING,
Kind.CLOB,
Kind.BLOB,
Expand All @@ -70,7 +68,6 @@ internal object FnComparator : Comparator<Function> {
Kind.TIMESTAMP,
Kind.TIMESTAMPZ,
Kind.ARRAY,
Kind.SEXP,
Kind.BAG,
Kind.ROW,
Kind.STRUCT,
Expand Down
Loading
Loading