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

Adds a built-in PartiQL System catalog, support for SQL-Path, and SPI cleanup #1670

Merged
merged 13 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 3 additions & 2 deletions partiql-spi/api/partiql-spi.api
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class org/partiql/spi/SourceLocations : java/util/Map {
public abstract interface class org/partiql/spi/catalog/Catalog {
public static final field Companion Lorg/partiql/spi/catalog/Catalog$Companion;
public static fun builder ()Lorg/partiql/spi/catalog/Catalog$Builder;
public static fun empty (Ljava/lang/String;)Lorg/partiql/spi/catalog/Catalog;
public abstract fun getAggregations (Lorg/partiql/spi/catalog/Session;Ljava/lang/String;)Ljava/util/Collection;
public abstract fun getFunctions (Lorg/partiql/spi/catalog/Session;Ljava/lang/String;)Ljava/util/Collection;
public abstract fun getName ()Ljava/lang/String;
Expand All @@ -85,6 +86,7 @@ public final class org/partiql/spi/catalog/Catalog$Builder {

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

public final class org/partiql/spi/catalog/Catalog$DefaultImpls {
Expand All @@ -97,7 +99,6 @@ public final class org/partiql/spi/catalog/Catalog$DefaultImpls {
public abstract interface class org/partiql/spi/catalog/Catalogs {
public static final field Companion Lorg/partiql/spi/catalog/Catalogs$Companion;
public static fun builder ()Lorg/partiql/spi/catalog/Catalogs$Builder;
public static fun empty ()Lorg/partiql/spi/catalog/Catalogs;
public abstract fun getCatalog (Ljava/lang/String;Z)Lorg/partiql/spi/catalog/Catalog;
public static fun of (Ljava/util/Collection;)Lorg/partiql/spi/catalog/Catalogs;
public static fun of ([Lorg/partiql/spi/catalog/Catalog;)Lorg/partiql/spi/catalog/Catalogs;
Expand All @@ -111,7 +112,6 @@ public final class org/partiql/spi/catalog/Catalogs$Builder {

public final class org/partiql/spi/catalog/Catalogs$Companion {
public final fun builder ()Lorg/partiql/spi/catalog/Catalogs$Builder;
public final fun empty ()Lorg/partiql/spi/catalog/Catalogs;
public final fun of (Ljava/util/Collection;)Lorg/partiql/spi/catalog/Catalogs;
public final fun of ([Lorg/partiql/spi/catalog/Catalog;)Lorg/partiql/spi/catalog/Catalogs;
}
Expand Down Expand Up @@ -261,6 +261,7 @@ public final class org/partiql/spi/catalog/Session$Builder {
public final fun namespace (Lorg/partiql/spi/catalog/Namespace;)Lorg/partiql/spi/catalog/Session$Builder;
public final fun namespace ([Ljava/lang/String;)Lorg/partiql/spi/catalog/Session$Builder;
public final fun property (Ljava/lang/String;Ljava/lang/String;)Lorg/partiql/spi/catalog/Session$Builder;
public final fun system (Lorg/partiql/spi/catalog/Catalog;)Lorg/partiql/spi/catalog/Session$Builder;
}

public final class org/partiql/spi/catalog/Session$Companion {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.partiql.spi.catalog;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.partiql.spi.function.Aggregation;
import org.partiql.spi.function.Builtins;
import org.partiql.spi.function.Function;

import java.util.Collection;

/**
* <p>
* This package-private class implements the PartiQL System Catalog.
* </p>
* <p>
* It provides the implementation for the PartiQL System Catalog, which is a built-in catalog
* that provides access to the PartiQL language and its built-in functions and aggregations.
* </p>
* @see Session.Builder
*/
final class PartiQLSystemCatalog implements Catalog {

@NotNull
private static final String NAME = "$system";

/**
* This is a package-private singleton.
*/
static PartiQLSystemCatalog INSTANCE = new PartiQLSystemCatalog();

@NotNull
@Override
public String getName() {
return NAME;
}

@Nullable
@Override
public Table getTable(@NotNull Session session, @NotNull Name name) {
return null;
}

@Nullable
@Override
public Table getTable(@NotNull Session session, @NotNull Identifier identifier) {
return null;
}

@NotNull
@Override
public Collection<Function> getFunctions(@NotNull Session session, @NotNull String name) {
return Builtins.INSTANCE.getFunctions(name);
}

@NotNull
@Override
public Collection<Aggregation> getAggregations(@NotNull Session session, @NotNull String name) {
return Builtins.INSTANCE.getAggregations(name);
}
}
13 changes: 13 additions & 0 deletions partiql-spi/src/main/kotlin/org/partiql/spi/catalog/Catalog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public interface Catalog {

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

/**
* @return an empty catalog with the given name
* @param name the catalog name
*/
@JvmStatic
public fun empty(name: String): Catalog {
return object : Catalog {
override fun getName(): String = name
override fun getTable(session: Session, name: Name): Table? = null
override fun getTable(session: Session, identifier: Identifier): Table? = null
}
}
}

/**
Expand Down
14 changes: 0 additions & 14 deletions partiql-spi/src/main/kotlin/org/partiql/spi/catalog/Catalogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ public interface Catalogs {
return builder().apply { catalogs.forEach { add(it) } }.build()
}

/**
* A catalog provider that always returns an empty catalog.
*/
@JvmStatic
public fun empty(): Catalogs = object : Catalogs {
private val EMPTY = object : Catalog {
override fun getName(): String = "empty"
override fun getTable(session: Session, name: Name): Table? = null
override fun getTable(session: Session, identifier: Identifier): Table? = null
}

override fun getCatalog(name: String, ignoreCase: Boolean): Catalog = EMPTY
}

@JvmStatic
public fun builder(): Builder = Builder()
}
Expand Down
29 changes: 23 additions & 6 deletions partiql-spi/src/main/kotlin/org/partiql/spi/catalog/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ public interface Session {
* Returns a [Session] with only the "empty" catalog implementation.
*/
@JvmStatic
public fun empty(): Session = object : Session {
override fun getIdentity(): String = "unknown"
override fun getCatalog(): String = "empty"
override fun getCatalogs(): Catalogs = Catalogs.empty()
override fun getNamespace(): Namespace = Namespace.empty()
public fun empty(): Session {
val catalog = Catalog.empty("empty")
return builder().catalog(catalog.getName()).catalogs(catalog).build()
johnedquinn marked this conversation as resolved.
Show resolved Hide resolved
}

@JvmStatic
Expand All @@ -64,6 +62,7 @@ public interface Session {

private var identity: String = "unknown"
private var catalog: String? = null
private var systemCatalog: Catalog = PartiQLSystemCatalog.INSTANCE
johnedquinn marked this conversation as resolved.
Show resolved Hide resolved
private var catalogs: Catalogs.Builder = Catalogs.builder()
private var namespace: Namespace = Namespace.empty()
private var properties: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -98,6 +97,16 @@ public interface Session {
return this
}

/**
* Adds and designates a catalog to always be on the SQL-Path. This [catalog] provides all built-in functions
* to the system at hand.
* If this is never invoked, a default system catalog is provided.
*/
public fun system(catalog: Catalog): Builder {
this.systemCatalog = catalog
return this
}

/**
* Adds catalogs to this session.
*/
Expand All @@ -110,16 +119,24 @@ public interface Session {

public fun build(): Session = object : Session {

private val _catalogs = catalogs.build()
private val _catalogs: Catalogs
private val systemCatalogNamespace: Namespace = Namespace.of(systemCatalog.getName())

init {
require(catalog != null) { "Session catalog must be set" }
catalogs.add(systemCatalog)
_catalogs = catalogs.build()
}

override fun getIdentity(): String = identity
override fun getCatalog(): String = catalog!!
override fun getCatalogs(): Catalogs = _catalogs
override fun getNamespace(): Namespace = namespace

override fun getPath(): Path {
val currentNamespace = getNamespace()
return Path.of(currentNamespace, systemCatalogNamespace)
}
}
}
}
Loading