Skip to content

Commit

Permalink
Defines Path and Session
Browse files Browse the repository at this point in the history
  • Loading branch information
rchowell committed Jul 9, 2024
1 parent 7adc8a3 commit 7b13be1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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.spi.connector.ConnectorMetadata
import java.time.Instant
import org.partiql.planner.catalog.Session

/**
* PartiQLPlanner is responsible for transforming an AST into PartiQL's logical query plan.
Expand All @@ -32,24 +31,6 @@ public interface PartiQLPlanner {
public val problems: List<Problem>,
)

/**
* From [org.partiql.lang.planner.transforms]
*
* @property queryId
* @property userId
* @property currentCatalog
* @property currentDirectory
* @property catalogs
* @property instant
*/
public class Session(
public val queryId: String,
public val userId: String,
public val currentCatalog: String,
public val currentDirectory: List<String> = emptyList(),
public val catalogs: Map<String, ConnectorMetadata> = emptyMap(),
public val instant: Instant = Instant.now(),
)
public companion object {

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface Catalog {
/**
* List top-level tables.
*/
public fun listTables(): Collection<Name> = listTables(Namespace.empty())
public fun listTables(): Collection<Name> = listTables(Namespace.root())

/**
* List all tables under this namespace.
Expand All @@ -37,7 +37,7 @@ public interface Catalog {
/**
* List top-level namespaces from the catalog.
*/
public fun listNamespaces(): Collection<Namespace> = listNamespaces(Namespace.empty())
public fun listNamespaces(): Collection<Namespace> = listNamespaces(Namespace.root())

/**
* List all child namespaces from the namespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ public class Namespace private constructor(

public companion object {

private val EMPTY = Namespace(emptyArray())
private val ROOT = Namespace(emptyArray())

public fun empty(): Namespace = EMPTY
public fun root(): Namespace = ROOT

@JvmStatic
public fun of(vararg levels: String): Namespace {
if (levels.isEmpty()) {
return empty()
return root()
}
return Namespace(arrayOf(*levels))
}

@JvmStatic
public fun of(levels: Collection<String>): Namespace {
if (levels.isEmpty()) {
return empty()
return root()
}
return Namespace(levels.toTypedArray())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.partiql.planner.catalog

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

/**
* The routine resolution path, accessible via PATH.
*/
public class Path private constructor(
private val namespaces: List<Namespace>,
) : Iterable<Namespace> {

public companion object {

@JvmStatic
public fun of(vararg namespaces: Namespace): Path = Path(namespaces.toList())
}

public fun getLength(): Int {
return namespaces.size
}

public fun isEmpty(): Boolean {
return namespaces.isEmpty()
}

public operator fun get(index: Int): Namespace {
return namespaces[index]
}

override fun forEach(action: Consumer<in Namespace>?) {
namespaces.forEach(action)
}

override fun iterator(): Iterator<Namespace> {
return namespaces.iterator()
}

override fun spliterator(): Spliterator<Namespace> {
return namespaces.spliterator()
}

override fun toString(): String = "PATH: (${namespaces.joinToString()})"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.partiql.planner.catalog

/**
* Session is used for authorization and name resolution.
*/
public interface Session {

public companion object {

private val EMPTY = object : Session {
override fun getIdentity(): String = "unknown"
override fun getNamespace(): Namespace = Namespace.root()
}

@JvmStatic
public fun empty(): Session = EMPTY

@JvmStatic
public fun builder(): Builder = Builder()
}

/**
* Returns the caller identity as a string; accessible via CURRENT_USER.
*/
public fun getIdentity(): String

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

/**
* Returns the current [Path]; accessible via the PATH and CURRENT_PATH session variables.
*
* Default implementation returns the current namespace.
*/
public fun getPath(): Path = Path.of(getNamespace())

/**
* Arbitrary session properties that may be used in planning or custom plan passes.
*/
public fun getProperties(): Map<String, String> = emptyMap()

/**
* Java-style session builder.
*/
public class Builder {

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

public fun identity(identity: String): Builder = this.apply { this.identity = identity }

public fun namespace(namespace: Namespace): Builder = this.apply { this.namespace = namespace }

public fun property(name: String, value: String): Builder = this.apply { this.properties[name] = value }

public fun build(): Session = object : Session {
override fun getIdentity(): String = identity
override fun getNamespace(): Namespace = namespace
}
}
}

0 comments on commit 7b13be1

Please sign in to comment.