Skip to content

Commit

Permalink
[2/2] Removes temp SqlFnProvider (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchowell authored Sep 20, 2024
1 parent 4fa1a76 commit 7a07d25
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 208 deletions.
52 changes: 26 additions & 26 deletions partiql-plan/api/partiql-plan.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public abstract class org/partiql/plan/Catalog$Item : org/partiql/plan/PlanNode

public final class org/partiql/plan/Catalog$Item$Agg : org/partiql/plan/Catalog$Item {
public static final field Companion Lorg/partiql/plan/Catalog$Item$Agg$Companion;
public final field aggregation Lorg/partiql/spi/fn/Aggregation;
public final field path Ljava/util/List;
public final field specific Ljava/lang/String;
public fun <init> (Ljava/util/List;Ljava/lang/String;)V
public fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;)V
public fun accept (Lorg/partiql/plan/visitor/PlanVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static final fun builder ()Lorg/partiql/plan/builder/CatalogItemAggBuilder;
public final fun component1 ()Ljava/util/List;
public final fun component2 ()Ljava/lang/String;
public final fun copy (Ljava/util/List;Ljava/lang/String;)Lorg/partiql/plan/Catalog$Item$Agg;
public static synthetic fun copy$default (Lorg/partiql/plan/Catalog$Item$Agg;Ljava/util/List;Ljava/lang/String;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Agg;
public final fun component2 ()Lorg/partiql/spi/fn/Aggregation;
public final fun copy (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;)Lorg/partiql/plan/Catalog$Item$Agg;
public static synthetic fun copy$default (Lorg/partiql/plan/Catalog$Item$Agg;Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Agg;
public fun equals (Ljava/lang/Object;)Z
public fun getChildren ()Ljava/util/List;
public fun hashCode ()I
Expand All @@ -46,15 +46,15 @@ public final class org/partiql/plan/Catalog$Item$Agg$Companion {

public final class org/partiql/plan/Catalog$Item$Fn : org/partiql/plan/Catalog$Item {
public static final field Companion Lorg/partiql/plan/Catalog$Item$Fn$Companion;
public final field function Lorg/partiql/spi/fn/Function;
public final field path Ljava/util/List;
public final field specific Ljava/lang/String;
public fun <init> (Ljava/util/List;Ljava/lang/String;)V
public fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Function;)V
public fun accept (Lorg/partiql/plan/visitor/PlanVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static final fun builder ()Lorg/partiql/plan/builder/CatalogItemFnBuilder;
public final fun component1 ()Ljava/util/List;
public final fun component2 ()Ljava/lang/String;
public final fun copy (Ljava/util/List;Ljava/lang/String;)Lorg/partiql/plan/Catalog$Item$Fn;
public static synthetic fun copy$default (Lorg/partiql/plan/Catalog$Item$Fn;Ljava/util/List;Ljava/lang/String;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Fn;
public final fun component2 ()Lorg/partiql/spi/fn/Function;
public final fun copy (Ljava/util/List;Lorg/partiql/spi/fn/Function;)Lorg/partiql/plan/Catalog$Item$Fn;
public static synthetic fun copy$default (Lorg/partiql/plan/Catalog$Item$Fn;Ljava/util/List;Lorg/partiql/spi/fn/Function;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Fn;
public fun equals (Ljava/lang/Object;)Z
public fun getChildren ()Ljava/util/List;
public fun hashCode ()I
Expand Down Expand Up @@ -163,8 +163,8 @@ public final class org/partiql/plan/PartiQLPlan$Companion {

public final class org/partiql/plan/Plan {
public static final fun catalog (Ljava/lang/String;Ljava/util/List;)Lorg/partiql/plan/Catalog;
public static final fun catalogItemAgg (Ljava/util/List;Ljava/lang/String;)Lorg/partiql/plan/Catalog$Item$Agg;
public static final fun catalogItemFn (Ljava/util/List;Ljava/lang/String;)Lorg/partiql/plan/Catalog$Item$Fn;
public static final fun catalogItemAgg (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;)Lorg/partiql/plan/Catalog$Item$Agg;
public static final fun catalogItemFn (Ljava/util/List;Lorg/partiql/spi/fn/Function;)Lorg/partiql/plan/Catalog$Item$Fn;
public static final fun catalogItemValue (Ljava/util/List;Lorg/partiql/types/PType;)Lorg/partiql/plan/Catalog$Item$Value;
public static final fun identifierQualified (Lorg/partiql/plan/Identifier$Symbol;Ljava/util/List;)Lorg/partiql/plan/Identifier$Qualified;
public static final fun identifierSymbol (Ljava/lang/String;Lorg/partiql/plan/Identifier$CaseSensitivity;)Lorg/partiql/plan/Identifier$Symbol;
Expand Down Expand Up @@ -1470,28 +1470,28 @@ public final class org/partiql/plan/builder/CatalogBuilder {

public final class org/partiql/plan/builder/CatalogItemAggBuilder {
public fun <init> ()V
public fun <init> (Ljava/util/List;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/util/List;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;)V
public synthetic fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun aggregation (Lorg/partiql/spi/fn/Aggregation;)Lorg/partiql/plan/builder/CatalogItemAggBuilder;
public final fun build ()Lorg/partiql/plan/Catalog$Item$Agg;
public final fun getAggregation ()Lorg/partiql/spi/fn/Aggregation;
public final fun getPath ()Ljava/util/List;
public final fun getSpecific ()Ljava/lang/String;
public final fun path (Ljava/util/List;)Lorg/partiql/plan/builder/CatalogItemAggBuilder;
public final fun setAggregation (Lorg/partiql/spi/fn/Aggregation;)V
public final fun setPath (Ljava/util/List;)V
public final fun setSpecific (Ljava/lang/String;)V
public final fun specific (Ljava/lang/String;)Lorg/partiql/plan/builder/CatalogItemAggBuilder;
}

public final class org/partiql/plan/builder/CatalogItemFnBuilder {
public fun <init> ()V
public fun <init> (Ljava/util/List;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/util/List;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Function;)V
public synthetic fun <init> (Ljava/util/List;Lorg/partiql/spi/fn/Function;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun build ()Lorg/partiql/plan/Catalog$Item$Fn;
public final fun function (Lorg/partiql/spi/fn/Function;)Lorg/partiql/plan/builder/CatalogItemFnBuilder;
public final fun getFunction ()Lorg/partiql/spi/fn/Function;
public final fun getPath ()Ljava/util/List;
public final fun getSpecific ()Ljava/lang/String;
public final fun path (Ljava/util/List;)Lorg/partiql/plan/builder/CatalogItemFnBuilder;
public final fun setFunction (Lorg/partiql/spi/fn/Function;)V
public final fun setPath (Ljava/util/List;)V
public final fun setSpecific (Ljava/lang/String;)V
public final fun specific (Ljava/lang/String;)Lorg/partiql/plan/builder/CatalogItemFnBuilder;
}

public final class org/partiql/plan/builder/CatalogItemValueBuilder {
Expand Down Expand Up @@ -1550,10 +1550,10 @@ public final class org/partiql/plan/builder/PlanBuilder {
public fun <init> ()V
public final fun catalog (Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog;
public static synthetic fun catalog$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog;
public final fun catalogItemAgg (Ljava/util/List;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog$Item$Agg;
public static synthetic fun catalogItemAgg$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/util/List;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Agg;
public final fun catalogItemFn (Ljava/util/List;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog$Item$Fn;
public static synthetic fun catalogItemFn$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/util/List;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Fn;
public final fun catalogItemAgg (Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog$Item$Agg;
public static synthetic fun catalogItemAgg$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/util/List;Lorg/partiql/spi/fn/Aggregation;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Agg;
public final fun catalogItemFn (Ljava/util/List;Lorg/partiql/spi/fn/Function;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog$Item$Fn;
public static synthetic fun catalogItemFn$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/util/List;Lorg/partiql/spi/fn/Function;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Fn;
public final fun catalogItemValue (Ljava/util/List;Lorg/partiql/types/PType;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Catalog$Item$Value;
public static synthetic fun catalogItemValue$default (Lorg/partiql/plan/builder/PlanBuilder;Ljava/util/List;Lorg/partiql/types/PType;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/partiql/plan/Catalog$Item$Value;
public final fun identifierQualified (Lorg/partiql/plan/Identifier$Symbol;Ljava/util/List;Lkotlin/jvm/functions/Function1;)Lorg/partiql/plan/Identifier$Qualified;
Expand Down
6 changes: 4 additions & 2 deletions partiql-plan/src/main/resources/partiql_plan.ion
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ imports::{
partiql_value::'org.partiql.value.PartiQLValue',
partiql_value_type::'org.partiql.types.PType',
static_type::'org.partiql.types.PType',
function::'org.partiql.spi.fn.Function',
aggregation::'org.partiql.spi.fn.Aggregation',
],
}

Expand All @@ -22,11 +24,11 @@ catalog::{
},
fn::{
path: list::[string],
specific: string,
function: function,
},
agg::{
path: list::[string],
specific: string,
aggregation: aggregation,
},
]
]
Expand Down
29 changes: 14 additions & 15 deletions partiql-planner/src/main/kotlin/org/partiql/planner/internal/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import org.partiql.spi.catalog.Catalogs
import org.partiql.spi.catalog.Identifier
import org.partiql.spi.catalog.Name
import org.partiql.spi.catalog.Session
import org.partiql.spi.fn.AggSignature
import org.partiql.spi.fn.SqlFnProvider
import org.partiql.spi.fn.Aggregation
import org.partiql.types.PType
import org.partiql.types.PType.Kind

Expand All @@ -38,18 +37,16 @@ import org.partiql.types.PType.Kind
*/
internal class Env(private val session: Session) {

/**
* Catalogs provider.
*/
private val catalogs: Catalogs = session.getCatalogs()

/**
* Current [Catalog] implementation; error if missing from the [Catalogs] provider.
*/
private val default: Catalog = catalogs.getCatalog(session.getCatalog()) ?: error("Default catalog does not exist")

/**
* A [SqlFnProvider] for looking up built-in functions.
*/
private val fns: SqlFnProvider = SqlFnProvider

/**
* Catalog lookup needs to search (3x) to handle schema-qualified and catalog-qualified use-cases.
*
Expand Down Expand Up @@ -114,7 +111,7 @@ internal class Env(private val session: Session) {
// 1. Search in the current catalog and namespace.
val catalog = default
val name = identifier.getIdentifier().getText().lowercase() // CASE-NORMALIZED LOWER
val variants = catalog.getFunctions(session, name).map { it.signature }
val variants = catalog.getFunctions(session, name).toList()
if (variants.isEmpty()) {
return null
}
Expand Down Expand Up @@ -149,7 +146,7 @@ internal class Env(private val session: Session) {
fn = refFn(
catalog = catalog.getName(),
name = Name.of(name),
signature = it.signature,
signature = it.function,
),
coercions = it.mapping.toList(),
)
Expand All @@ -162,7 +159,7 @@ internal class Env(private val session: Session) {
val ref = refFn(
catalog = catalog.getName(),
name = Name.of(name),
signature = match.signature,
signature = match.function,
)
// Apply the coercions as explicit casts
val coercions: List<Rex> = args.mapIndexed { i, arg ->
Expand All @@ -183,7 +180,7 @@ internal class Env(private val session: Session) {
// 1. Search in the current catalog and namespace.
val catalog = default
val name = path.lowercase()
val candidates = catalog.getAggregations(session, name).map { it.signature }
val candidates = catalog.getAggregations(session, name).toList()
if (candidates.isEmpty()) {
return null
}
Expand Down Expand Up @@ -243,15 +240,15 @@ internal class Env(private val session: Session) {
return rhs.takeLast(rhs.size - lhs.size)
}

private fun match(candidates: List<AggSignature>, args: List<PType>): Pair<AggSignature, Array<Ref.Cast?>>? {
private fun match(candidates: List<Aggregation>, args: List<PType>): Pair<Aggregation, Array<Ref.Cast?>>? {
// 1. Check for an exact match
for (candidate in candidates) {
if (candidate.matches(args)) {
return candidate to arrayOfNulls(args.size)
}
}
// 2. Look for best match.
var match: Pair<AggSignature, Array<Ref.Cast?>>? = null
var match: Pair<Aggregation, Array<Ref.Cast?>>? = null
for (candidate in candidates) {
val m = candidate.match(args) ?: continue
// TODO AggMatch comparison
Expand All @@ -269,7 +266,8 @@ internal class Env(private val session: Session) {
* Check if this function accepts the exact input argument types. Assume same arity.
*/

private fun AggSignature.matches(args: List<PType>): Boolean {
private fun Aggregation.matches(args: List<PType>): Boolean {
val parameters = getParameters()
for (i in args.indices) {
val a = args[i]
val p = parameters[i]
Expand All @@ -284,7 +282,8 @@ internal class Env(private val session: Session) {
* @param args
* @return
*/
private fun AggSignature.match(args: List<PType>): Pair<AggSignature, Array<Ref.Cast?>>? {
private fun Aggregation.match(args: List<PType>): Pair<Aggregation, Array<Ref.Cast?>>? {
val parameters = getParameters()
val mapping = arrayOfNulls<Ref.Cast?>(args.size)
for (i in args.indices) {
val arg = args[i]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.partiql.planner.internal

import org.partiql.spi.fn.FnSignature
import org.partiql.spi.fn.Function
import org.partiql.spi.fn.Parameter
import org.partiql.types.PType
import org.partiql.types.PType.Kind
Expand All @@ -11,17 +11,19 @@ import org.partiql.types.PType.Kind
* 1. Fewest args first
* 2. Parameters are compared left-to-right
*/
internal object FnComparator : Comparator<FnSignature> {
internal object FnComparator : Comparator<Function> {

override fun compare(fn1: FnSignature, fn2: FnSignature): Int {
override fun compare(fn1: Function, fn2: Function): Int {
val params1 = fn1.getParameters()
val params2 = fn2.getParameters()
// Compare number of arguments
if (fn1.parameters.size != fn2.parameters.size) {
return fn1.parameters.size - fn2.parameters.size
if (params1.size != params2.size) {
return params1.size - params2.size
}
// Compare operand type precedence
for (i in fn1.parameters.indices) {
val p1 = fn1.parameters[i]
val p2 = fn2.parameters[i]
for (i in params1.indices) {
val p1 = params1[i]
val p2 = params2[i]
val comparison = p1.compareTo(p2)
if (comparison != 0) return comparison
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.partiql.planner.internal

import org.partiql.planner.internal.ir.Ref
import org.partiql.spi.fn.FnSignature
import org.partiql.spi.fn.Function

/**
* Result of matching an unresolved function.
Expand All @@ -12,11 +12,11 @@ internal sealed class FnMatch {
/**
* Successful match of a static function call.
*
* @property signature
* @property function
* @property mapping
*/
data class Static(
val signature: FnSignature,
class Static(
val function: Function,
val mapping: Array<Ref.Cast?>,
) : FnMatch() {

Expand All @@ -31,15 +31,15 @@ internal sealed class FnMatch {

other as Static

if (signature != other.signature) return false
if (function != other.function) return false
if (!mapping.contentEquals(other.mapping)) return false
if (exact != other.exact) return false

return true
}

override fun hashCode(): Int {
var result = signature.hashCode()
var result = function.hashCode()
result = 31 * result + mapping.contentHashCode()
result = 31 * result + exact
return result
Expand All @@ -51,7 +51,5 @@ internal sealed class FnMatch {
*
* @property candidates Ordered list of potentially applicable functions to dispatch dynamically.
*/
data class Dynamic(
val candidates: List<Static>,
) : FnMatch()
class Dynamic(val candidates: List<Static>) : FnMatch()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.partiql.planner.internal.casts.Coercions
import org.partiql.planner.internal.ir.Ref
import org.partiql.planner.internal.typer.CompilerType
import org.partiql.planner.internal.typer.PlanTyper.Companion.toCType
import org.partiql.spi.fn.FnSignature
import org.partiql.spi.fn.Function
import org.partiql.types.PType.Kind

/**
Expand Down Expand Up @@ -34,9 +34,9 @@ internal object FnResolver {
* @param args
* @return
*/
fun resolve(variants: List<FnSignature>, args: List<CompilerType>): FnMatch? {
fun resolve(variants: List<Function>, args: List<CompilerType>): FnMatch? {
val candidates = variants
.filter { it.parameters.size == args.size }
.filter { it.getParameters().size == args.size }
.ifEmpty { return null }

// 1. Look for exact match
Expand All @@ -58,7 +58,7 @@ internal object FnResolver {
return resolveBestMatch(candidates, args)
}

private fun resolveBestMatch(candidates: List<FnSignature>, args: List<CompilerType>): FnMatch.Static? {
private fun resolveBestMatch(candidates: List<Function>, args: List<CompilerType>): FnMatch.Static? {
// 3. Discard functions that cannot be matched (via implicit coercion or exact matches)
val invocableMatches = match(candidates, args).ifEmpty { return null }
if (invocableMatches.size == 1) {
Expand Down Expand Up @@ -86,7 +86,7 @@ internal object FnResolver {
* @param args
* @return
*/
private fun match(candidates: List<FnSignature>, args: List<CompilerType>): List<MatchResult> {
private fun match(candidates: List<Function>, args: List<CompilerType>): List<MatchResult> {
val matches = mutableSetOf<MatchResult>()
for (candidate in candidates) {
val m = candidate.match(args) ?: continue
Expand Down Expand Up @@ -116,7 +116,8 @@ internal object FnResolver {
/**
* Check if this function accepts the exact input argument types. Assume same arity.
*/
private fun FnSignature.matchesExactly(args: List<CompilerType>): Boolean {
private fun Function.matchesExactly(args: List<CompilerType>): Boolean {
val parameters = getParameters()
for (i in args.indices) {
val a = args[i]
val p = parameters[i]
Expand All @@ -131,7 +132,8 @@ internal object FnResolver {
* @param args
* @return
*/
private fun FnSignature.match(args: List<CompilerType>): MatchResult? {
private fun Function.match(args: List<CompilerType>): MatchResult? {
val parameters = getParameters()
val mapping = arrayOfNulls<Ref.Cast?>(args.size)
var exactInputTypes: Int = 0
for (i in args.indices) {
Expand Down Expand Up @@ -170,7 +172,7 @@ internal object FnResolver {

private object MatchResultComparator : Comparator<MatchResult> {
override fun compare(o1: MatchResult, o2: MatchResult): Int {
return FnComparator.reversed().compare(o1.match.signature, o2.match.signature)
return FnComparator.reversed().compare(o1.match.function, o2.match.function)
}
}
}
Loading

0 comments on commit 7a07d25

Please sign in to comment.