Skip to content

Commit

Permalink
Updates catalog for Table.Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Jul 17, 2024
1 parent ca51d6d commit f64aa27
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 242 deletions.
39 changes: 18 additions & 21 deletions partiql-planner/api/partiql-planner.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public abstract interface class org/partiql/planner/PartiQLPlanner {

public final class org/partiql/planner/PartiQLPlanner$Builder {
public fun <init> ()V
public final fun addCatalog (Lorg/partiql/planner/catalog/Catalog;)Lorg/partiql/planner/PartiQLPlanner$Builder;
public final fun build ()Lorg/partiql/planner/PartiQLPlanner;
public final fun catalogs (Lorg/partiql/planner/catalog/Catalogs;)Lorg/partiql/planner/PartiQLPlanner$Builder;
public final fun signal (Z)Lorg/partiql/planner/PartiQLPlanner$Builder;
public static synthetic fun signal$default (Lorg/partiql/planner/PartiQLPlanner$Builder;ZILjava/lang/Object;)Lorg/partiql/planner/PartiQLPlanner$Builder;
}
Expand All @@ -27,34 +27,20 @@ public final class org/partiql/planner/PartiQLPlanner$Result {
}

public abstract interface class org/partiql/planner/catalog/Catalog {
public static final field Companion Lorg/partiql/planner/catalog/Catalog$Companion;
public static fun builder ()Lorg/partiql/planner/catalog/Catalog$Builder;
public abstract fun getName ()Ljava/lang/String;
public abstract fun getRoutines (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Name;)Ljava/util/Collection;
public abstract fun getTable (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Identifier;)Lorg/partiql/planner/catalog/Table;
public abstract fun getTable (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Name;)Lorg/partiql/planner/catalog/Table;
public abstract fun getTableHandle (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Identifier;)Lorg/partiql/planner/catalog/Table$Handle;
public abstract fun listNamespaces (Lorg/partiql/planner/catalog/Session;)Ljava/util/Collection;
public abstract fun listNamespaces (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Namespace;)Ljava/util/Collection;
public abstract fun listTables (Lorg/partiql/planner/catalog/Session;)Ljava/util/Collection;
public abstract fun listTables (Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Namespace;)Ljava/util/Collection;
}

public final class org/partiql/planner/catalog/Catalog$Builder {
public fun <init> ()V
public final fun build ()Lorg/partiql/planner/catalog/Catalog;
public final fun createTable (Ljava/lang/String;Lorg/partiql/types/PType;)Lorg/partiql/planner/catalog/Catalog$Builder;
public final fun createTable (Lorg/partiql/planner/catalog/Name;Lorg/partiql/types/PType;)Lorg/partiql/planner/catalog/Catalog$Builder;
public final fun name (Ljava/lang/String;)Lorg/partiql/planner/catalog/Catalog$Builder;
}

public final class org/partiql/planner/catalog/Catalog$Companion {
public final fun builder ()Lorg/partiql/planner/catalog/Catalog$Builder;
}

public final class org/partiql/planner/catalog/Catalog$DefaultImpls {
public static fun getRoutines (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Name;)Ljava/util/Collection;
public static fun getTable (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Identifier;)Lorg/partiql/planner/catalog/Table;
public static fun getTable (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Name;)Lorg/partiql/planner/catalog/Table;
public static fun getTableHandle (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Identifier;)Lorg/partiql/planner/catalog/Table$Handle;
public static fun listNamespaces (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;)Ljava/util/Collection;
public static fun listNamespaces (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;Lorg/partiql/planner/catalog/Namespace;)Ljava/util/Collection;
public static fun listTables (Lorg/partiql/planner/catalog/Catalog;Lorg/partiql/planner/catalog/Session;)Ljava/util/Collection;
Expand Down Expand Up @@ -145,17 +131,19 @@ public final class org/partiql/planner/catalog/Identifier$Part$Companion {
public final fun regular (Ljava/lang/String;)Lorg/partiql/planner/catalog/Identifier$Part;
}

public final class org/partiql/planner/catalog/Name {
public final class org/partiql/planner/catalog/Name : java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lorg/partiql/planner/catalog/Name$Companion;
public fun <init> (Lorg/partiql/planner/catalog/Namespace;Ljava/lang/String;)V
public fun equals (Ljava/lang/Object;)Z
public fun forEach (Ljava/util/function/Consumer;)V
public final fun getName ()Ljava/lang/String;
public final fun getNamespace ()Lorg/partiql/planner/catalog/Namespace;
public final fun getPath ()Ljava/util/List;
public final fun hasNamespace ()Z
public fun hashCode ()I
public fun iterator ()Ljava/util/Iterator;
public static final fun of (Ljava/util/Collection;)Lorg/partiql/planner/catalog/Name;
public static final fun of ([Ljava/lang/String;)Lorg/partiql/planner/catalog/Name;
public fun spliterator ()Ljava/util/Spliterator;
public fun toString ()Ljava/lang/String;
}

Expand All @@ -167,6 +155,7 @@ public final class org/partiql/planner/catalog/Name$Companion {
public final class org/partiql/planner/catalog/Namespace : java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lorg/partiql/planner/catalog/Namespace$Companion;
public synthetic fun <init> ([Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun concat ([Ljava/lang/String;)Lorg/partiql/planner/catalog/Namespace;
public fun equals (Ljava/lang/Object;)Z
public fun forEach (Ljava/util/function/Consumer;)V
public final fun get (I)Ljava/lang/String;
Expand Down Expand Up @@ -276,7 +265,8 @@ public final class org/partiql/planner/catalog/Routine$Scalar$DefaultImpls {
public abstract interface class org/partiql/planner/catalog/Session {
public static final field Companion Lorg/partiql/planner/catalog/Session$Companion;
public static fun builder ()Lorg/partiql/planner/catalog/Session$Builder;
public static fun empty ()Lorg/partiql/planner/catalog/Session;
public static fun empty (Ljava/lang/String;)Lorg/partiql/planner/catalog/Session;
public abstract fun getCatalog ()Ljava/lang/String;
public abstract fun getIdentity ()Ljava/lang/String;
public abstract fun getNamespace ()Lorg/partiql/planner/catalog/Namespace;
public abstract fun getPath ()Lorg/partiql/planner/catalog/Path;
Expand All @@ -286,14 +276,15 @@ public abstract interface class org/partiql/planner/catalog/Session {
public final class org/partiql/planner/catalog/Session$Builder {
public fun <init> ()V
public final fun build ()Lorg/partiql/planner/catalog/Session;
public final fun catalog (Ljava/lang/String;)Lorg/partiql/planner/catalog/Session$Builder;
public final fun identity (Ljava/lang/String;)Lorg/partiql/planner/catalog/Session$Builder;
public final fun namespace (Lorg/partiql/planner/catalog/Namespace;)Lorg/partiql/planner/catalog/Session$Builder;
public final fun property (Ljava/lang/String;Ljava/lang/String;)Lorg/partiql/planner/catalog/Session$Builder;
}

public final class org/partiql/planner/catalog/Session$Companion {
public final fun builder ()Lorg/partiql/planner/catalog/Session$Builder;
public final fun empty ()Lorg/partiql/planner/catalog/Session;
public final fun empty (Ljava/lang/String;)Lorg/partiql/planner/catalog/Session;
}

public final class org/partiql/planner/catalog/Session$DefaultImpls {
Expand Down Expand Up @@ -326,3 +317,9 @@ public final class org/partiql/planner/catalog/Table$DefaultImpls {
public static fun getSchema (Lorg/partiql/planner/catalog/Table;)Lorg/partiql/types/PType;
}

public final class org/partiql/planner/catalog/Table$Handle {
public final field namespace Lorg/partiql/planner/catalog/Namespace;
public final field table Lorg/partiql/planner/catalog/Table;
public fun <init> (Lorg/partiql/planner/catalog/Namespace;Lorg/partiql/planner/catalog/Table;)V
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.partiql.ast.Statement
import org.partiql.errors.Problem
import org.partiql.errors.ProblemCallback
import org.partiql.plan.PartiQLPlan
import org.partiql.planner.catalog.Catalog
import org.partiql.planner.catalog.Catalogs
import org.partiql.planner.catalog.Session
import org.partiql.planner.internal.PartiQLPlannerDefault
Expand Down Expand Up @@ -43,23 +44,20 @@ public interface PartiQLPlanner {
public class Builder {

private val flags: MutableSet<PlannerFlag> = mutableSetOf()
private var catalogs: Catalogs? = null
private var catalogs: Catalogs.Builder = Catalogs.builder()

/**
* Build the builder, return an implementation of a [PartiQLPlanner].
*
* @return
*/
public fun build(): PartiQLPlanner {
assert(catalogs != null) { "The `catalogs` field cannot be null, set with .catalogs(...)" }
return PartiQLPlannerDefault(catalogs!!, flags)
return PartiQLPlannerDefault(catalogs.build(), flags)
}

/**
* Adds a catalog provider to this planner builder.
* Adds a catalog to this planner builder.
*/
public fun catalogs(catalogs: Catalogs): Builder {
this.catalogs = catalogs
public fun addCatalog(catalog: Catalog): Builder {
catalogs.add(catalog)
return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.partiql.planner.catalog

import org.partiql.types.PType

/**
* Catalog interface for access to tables and routines.
*
Expand All @@ -23,7 +21,7 @@ public interface Catalog {

/**
* Given an [Identifier], returns a [Table.Handle] that corresponds to the longest-available requested path.
*
*
* For example, given a table named "Table" located within Catalog "AWS" and Namespace "a".b"."c", a user could
* call [getTableHandle] with the identifier "a"."b"."c"."Table". The returned [Table.Handle] will contain the table
* representation and the matching path: "a"."b"."c"."Table"
Expand All @@ -39,11 +37,6 @@ public interface Catalog {
*/
public fun getTableHandle(session: Session, identifier: Identifier): Table.Handle? = null

/**
* Creates a table with the given name (possibly in a namespace).
*/
public fun createTable(session: Session, name: Name, schema: PType)

/**
* List top-level tables.
*/
Expand Down Expand Up @@ -73,78 +66,4 @@ public interface Catalog {
* @return A collection of all [Routine]s in the current namespace with this name.
*/
public fun getRoutines(session: Session, name: Name): Collection<Routine> = emptyList()

/**
* Factory methods.
*/
public companion object {

/**
* Returns a default [Catalog] implementation based upon an in-memory tree.
*
* @param name The name of the catalog.
*/
@JvmStatic
public fun standard(name: String): Catalog = Standard(name)
}

/**
* A default [Catalog] implementation based upon an in-memory tree.
*/
private class Standard(val name: String) : Catalog {

private val root: Tree = Tree(null, mutableMapOf())

private class Tree(
private val table: Table?,
private val children: MutableMap<String, Tree>,
) {
fun contains(name: String) = children.contains(name)
fun get(name: String): Tree? = children[name]
fun getOrPut(name: String): Tree = children.getOrPut(name) { Tree(null, mutableMapOf()) }
}

override fun getName(): String = name

override fun getTable(session: Session, name: Name): Table? {
return null
}

override fun getTableHandle(session: Session, identifier: Identifier): Table.Handle? {
if (identifier.hasQualifier()) {
error("Catalog does not support qualified table names")
}
var match: Table? = null
val id = identifier.getIdentifier()
for (table in tree.values) {
if (id.matches(table.getName())) {
if (match == null) {
match = table
} else {
error("Ambiguous table name: $name")
}
}
}
return match
}

override fun createTable(session: Session, name: Name, schema: PType) {
TODO("Not yet implemented")
}

// TODO
override fun listTables(session: Session, namespace: Namespace): Collection<Name> {
return emptyList()
}

// TODO
override fun listNamespaces(session: Session, namespace: Namespace): Collection<Namespace> {
return emptyList()
}

// TODO
override fun getRoutines(session: Session, name: Name): Collection<Routine> {
return emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.partiql.planner.catalog

import java.util.Spliterator
import java.util.function.Consumer

/**
* A reference to a named object in a catalog.
*/
public class Name(
private val namespace: Namespace,
private val name: String,
) {
) : Iterable<String> {

/**
* Returns the unqualified name part.
Expand All @@ -24,15 +27,27 @@ public class Name(
public fun hasNamespace(): Boolean = !namespace.isEmpty()

/**
* Returns a list of strings representing the path of this name.
* Returns an iterator of strings for this name.
*/
public fun getPath(): List<String> {
private fun getParts(): List<String> {
val parts = mutableListOf<String>()
parts.addAll(namespace.getLevels())
parts.add(name)
return parts
}

override fun forEach(action: Consumer<in String>?) {
getParts().forEach(action)
}

override fun iterator(): Iterator<String> {
return getParts().iterator()
}

override fun spliterator(): Spliterator<String> {
return getParts().spliterator()
}

/**
* Compares two names including their namespaces and symbols.
*/
Expand Down Expand Up @@ -61,7 +76,7 @@ public class Name(
* Return the SQL name representation of this name — all parts delimited.
*/
override fun toString(): String {
return Identifier.delimited(getPath()).toString()
return Identifier.delimited(getParts()).toString()
}

public companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public interface Session {
public fun getIdentity(): String

/**
* Returns the current [Namespace]; accessible via the NAMESPACE session variable.
* Returns the current [Catalog]; accessible via the CURRENT_CATALOG session variable.
*/
public fun getCatalog(): String

/**
* Returns the current [Namespace]; accessible via the CURRENT_NAMESPACE session variable.
*/
public fun getNamespace(): Namespace

Expand All @@ -32,9 +37,13 @@ public interface Session {
*/
public companion object {

/**
* Returns an empty [Session] with the provided [catalog].
*/
@JvmStatic
public fun empty(): Session = object : Session {
public fun empty(catalog: String): Session = object : Session {
override fun getIdentity(): String = "unknown"
override fun getCatalog(): String = catalog
override fun getNamespace(): Namespace = Namespace.root()
}

Expand All @@ -48,6 +57,7 @@ public interface Session {
public class Builder {

private var identity: String = "unknown"
private var catalog: String? = null
private var namespace: Namespace = Namespace.root()
private var properties: MutableMap<String, String> = mutableMapOf()

Expand All @@ -56,6 +66,11 @@ public interface Session {
return this
}

public fun catalog(catalog: String?): Builder {
this.catalog = catalog
return this
}

public fun namespace(namespace: Namespace): Builder {
this.namespace = namespace
return this
Expand All @@ -68,6 +83,7 @@ public interface Session {

public fun build(): Session = object : Session {
override fun getIdentity(): String = identity
override fun getCatalog(): String = catalog!!
override fun getNamespace(): Namespace = namespace
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public interface Table {
* Note: This replaces ConnectorObjectHandle from versions < 1.0
*/
public class Handle(
public val namespace: Namespace,
public val table: Table,
@JvmField public val namespace: Namespace,
@JvmField public val table: Table,
)

/**
Expand Down
Loading

0 comments on commit f64aa27

Please sign in to comment.