Skip to content

Commit

Permalink
Initializes PartiQLCursor and PartiQLValueLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedquinn committed May 16, 2024
1 parent 4f89c2d commit 3824d2f
Show file tree
Hide file tree
Showing 11 changed files with 815 additions and 14 deletions.
10 changes: 5 additions & 5 deletions partiql-eval/api/partiql-eval.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public final class org/partiql/eval/PartiQLResult$Error : org/partiql/eval/Parti
}

public final class org/partiql/eval/PartiQLResult$Value : org/partiql/eval/PartiQLResult {
public fun <init> (Lorg/partiql/value/PartiQLValue;)V
public final fun component1 ()Lorg/partiql/value/PartiQLValue;
public final fun copy (Lorg/partiql/value/PartiQLValue;)Lorg/partiql/eval/PartiQLResult$Value;
public static synthetic fun copy$default (Lorg/partiql/eval/PartiQLResult$Value;Lorg/partiql/value/PartiQLValue;ILjava/lang/Object;)Lorg/partiql/eval/PartiQLResult$Value;
public fun <init> (Lorg/partiql/value/PartiQLCursor;)V
public final fun component1 ()Lorg/partiql/value/PartiQLCursor;
public final fun copy (Lorg/partiql/value/PartiQLCursor;)Lorg/partiql/eval/PartiQLResult$Value;
public static synthetic fun copy$default (Lorg/partiql/eval/PartiQLResult$Value;Lorg/partiql/value/PartiQLCursor;ILjava/lang/Object;)Lorg/partiql/eval/PartiQLResult$Value;
public fun equals (Ljava/lang/Object;)Z
public final fun getValue ()Lorg/partiql/value/PartiQLValue;
public final fun getValue ()Lorg/partiql/value/PartiQLCursor;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.partiql.eval.internal.Compiler
import org.partiql.eval.internal.Environment
import org.partiql.eval.internal.Symbols
import org.partiql.plan.PartiQLPlan
import org.partiql.value.PartiQLCursor
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental

Expand Down Expand Up @@ -33,7 +34,8 @@ internal class PartiQLEngineDefault : PartiQLEngine {
return when (statement) {
is PartiQLStatement.Query -> try {
val value = statement.execute()
PartiQLResult.Value(value)
val data = PartiQLCursor.of(value)
PartiQLResult.Value(data)
} catch (ex: Exception) {
PartiQLResult.Error(ex)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.partiql.eval

import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLCursor
import org.partiql.value.PartiQLValueExperimental

public sealed interface PartiQLResult {

@OptIn(PartiQLValueExperimental::class)
public data class Value(public val value: PartiQLValue) : PartiQLResult
public data class Value(public val value: PartiQLCursor) : PartiQLResult

public data class Error(public val cause: Throwable) : PartiQLResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.partiql.types.StaticType
import org.partiql.value.CollectionValue
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueLoader
import org.partiql.value.bagValue
import org.partiql.value.boolValue
import org.partiql.value.decimalValue
Expand Down Expand Up @@ -1176,7 +1177,7 @@ class PartiQLEngineDefaultTest {
throw returned.cause
}
}
val output = result.value
val output = PartiQLValueLoader.standard().load(result.value)
assert(expected == output) {
comparisonString(expected, output, plan.plan)
}
Expand Down Expand Up @@ -1245,7 +1246,7 @@ class PartiQLEngineDefaultTest {
val plan = planner.plan(statement, session)
val prepared = engine.prepare(plan.plan, PartiQLEngine.Session(mapOf("memory" to connector), mode = mode))
when (val result = engine.execute(prepared)) {
is PartiQLResult.Value -> return result.value to plan.plan
is PartiQLResult.Value -> return PartiQLValueLoader.standard().load(result.value) to plan.plan
is PartiQLResult.Error -> throw result.cause
}
}
Expand Down
36 changes: 36 additions & 0 deletions partiql-types/api/partiql-types.api
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,37 @@ public final class org/partiql/value/PartiQL {
public static synthetic fun timestampValue$default (Lorg/partiql/value/datetime/Timestamp;Ljava/util/List;ILjava/lang/Object;)Lorg/partiql/value/TimestampValue;
}

public abstract interface class org/partiql/value/PartiQLCursor : java/lang/AutoCloseable, java/util/Iterator {
public abstract fun getBinaryValue ()[B
public abstract fun getBlobValue ()[B
public abstract fun getBoolValue ()Z
public abstract fun getByteValue ()B
public abstract fun getCharValue ()Ljava/lang/String;
public abstract fun getClobValue ()[B
public abstract fun getDateValue ()Lorg/partiql/value/datetime/Date;
public abstract fun getDecimalArbitraryValue ()Ljava/math/BigDecimal;
public abstract fun getDecimalValue ()Ljava/math/BigDecimal;
public abstract fun getFieldName ()Ljava/lang/String;
public abstract fun getFloat32Value ()F
public abstract fun getFloat64Value ()D
public abstract fun getInt16Value ()S
public abstract fun getInt32Value ()I
public abstract fun getInt64Value ()J
public abstract fun getInt8Value ()B
public abstract fun getIntValue ()Ljava/math/BigInteger;
public abstract fun getIntervalValue ()J
public abstract fun getStringValue ()Ljava/lang/String;
public abstract fun getSymbolValue ()Ljava/lang/String;
public abstract fun getTimeValue ()Lorg/partiql/value/datetime/Time;
public abstract fun getTimestampValue ()Lorg/partiql/value/datetime/Timestamp;
public abstract fun getType ()Lorg/partiql/value/PartiQLValueType;
public abstract fun isMissingValue ()Z
public abstract fun isNullValue ()Z
public static fun of (Lorg/partiql/value/PartiQLValue;)Lorg/partiql/value/PartiQLCursor;
public abstract fun stepIn ()V
public abstract fun stepOut ()V
}

public abstract interface annotation class org/partiql/value/PartiQLTimestampExperimental : java/lang/annotation/Annotation {
}

Expand Down Expand Up @@ -1141,6 +1172,11 @@ public final class org/partiql/value/PartiQLValueKt {
public static final fun toIon (Lorg/partiql/value/PartiQLValue;)Lcom/amazon/ionelement/api/IonElement;
}

public abstract interface class org/partiql/value/PartiQLValueLoader {
public abstract fun load (Lorg/partiql/value/PartiQLCursor;)Lorg/partiql/value/PartiQLValue;
public static fun standard ()Lorg/partiql/value/PartiQLValueLoader;
}

public final class org/partiql/value/PartiQLValueType : java/lang/Enum {
public static final field ANY Lorg/partiql/value/PartiQLValueType;
public static final field BAG Lorg/partiql/value/PartiQLValueType;
Expand Down
9 changes: 9 additions & 0 deletions partiql-types/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ dependencies {
implementation(Deps.kotlinxCollections)
}

// Need to add this as we have both Java and Kotlin sources. Dokka already handles multi-language projects. If
// Javadoc is enabled, we end up overwriting index.html (causing compilation errors).
tasks.withType<Javadoc>() {
enabled = false
}
tasks.withType<Jar>() {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

publish {
artifactId = "partiql-types"
name = "PartiQL Types"
Expand Down
187 changes: 187 additions & 0 deletions partiql-types/src/main/java/org/partiql/value/PartiQLCursor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.partiql.value;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
import org.jetbrains.annotations.NotNull;
import org.partiql.value.datetime.Date;
import org.partiql.value.datetime.Time;
import org.partiql.value.datetime.Timestamp;

/**
* Data representing a database result set, which is usually generated by executing a statement that queries the database.
* <p>
* A {@link PartiQLCursor} object maintains a cursor pointing to its current position in the underlying data. Initially the
* cursor is positioned before the first value. The {@link #next()} method moves the cursor to the next value. Please use
* {@link #hasNext()} before calling {@link #next()}.
*
* @see PartiQLValueLoader#load(PartiQLCursor)
* @see PartiQLValue
*/
public interface PartiQLCursor extends AutoCloseable, Iterator<PartiQLValueType> {

/**
* Positions the reader just before the contents of the current value, which must be a container (list, bag,
* sexp, or struct). There's no current value immediately after stepping in, so the next thing you'll want to do is call
* {@link #hasNext()} and {@link #next()} to move onto the first child value.
* <p>
* If the container itself is the null value, stepIn() shall fail. Please use {@link #isNullValue()} before
* invoking this.
* <p>
* At any time {@link #stepOut()} may be called to move the cursor back to (just after) the parent value, even if
* there are more children remaining.
*/
public void stepIn();

/**
* Positions the iterator after the current parent's value, moving up one level in the data hierarchy. There's no
* current value immediately after stepping out, so the next thing you'll want to do is call {@link #hasNext()} and
* {@link #next()} to move onto the following value.
*/
public void stepOut();

/**
* Determines whether the current value is a null value of any type (for example, null or null.int). It should be
* called before calling getters that return value types (int, long, boolean, double).
*/
public boolean isNullValue();

/**
* Determines whether the current value is the missing value. Similarly, one can invoke {@link #getType()}.
*/
public boolean isMissingValue();

/**
* @return the type of the data at the cursor.
*/
@NotNull
public PartiQLValueType getType();

/**
* @return the field name of the current value; or null if there is no valid current value or if the current value
* is not a field of a struct.
*/
public String getFieldName();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#STRING}.
*/
@NotNull
String getStringValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#CHAR}.
*/
@NotNull
String getCharValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#SYMBOL}.
*/
@NotNull
String getSymbolValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#BOOL}.
*/
public boolean getBoolValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#BINARY}.
*/
public byte[] getBinaryValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#BLOB}.
*/
public byte[] getBlobValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#CLOB}.
*/
public byte[] getClobValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#BYTE}.
*/
public byte getByteValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#DATE}.
*/
@NotNull
public Date getDateValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#TIME}.
*/
@NotNull
public Time getTimeValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#TIMESTAMP}.
*/
@NotNull
public Timestamp getTimestampValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INTERVAL}.
*/
@Deprecated
public long getIntervalValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INT8}.
*/
public byte getInt8Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INT16}.
*/
public short getInt16Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INT32}.
*/
public int getInt32Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INT64}.
*/
public long getInt64Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#INT}.
*/
@NotNull
public BigInteger getIntValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#FLOAT32}.
*/
public float getFloat32Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#FLOAT64}.
*/
public double getFloat64Value();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#DECIMAL}.
*/
@NotNull
public BigDecimal getDecimalValue();

/**
* This is only applicable when the current value's type is {@link PartiQLValueType#DECIMAL_ARBITRARY}.
*/
@NotNull
public BigDecimal getDecimalArbitraryValue();

/**
* Converts a {@link PartiQLValue} into {@link PartiQLCursor}.
*/
static PartiQLCursor of(PartiQLValue value) {
return new PartiQLCursorDefault(value);
}
}
Loading

0 comments on commit 3824d2f

Please sign in to comment.