Skip to content

Commit

Permalink
Initializes PartiQLData and PartiQLValueLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedquinn committed Apr 23, 2024
1 parent 23cc28c commit 34e2adf
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.partiql.cli.format


// internal object ExplainFormatter {
//
// internal fun format(result: PartiQLResult.Explain.Domain): 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.PartiQLData
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 = PartiQLData.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.PartiQLData
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: PartiQLData) : PartiQLResult

public data class Error(public val cause: Throwable) : PartiQLResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 @@ -1215,7 +1216,7 @@ class PartiQLEngineDefaultTest {
throw returned.cause
}
}
val output = result.value
val output = PartiQLValueLoader.standard().loadSingleValue(result.value)
assert(expected == output) {
comparisonString(expected, output, plan.plan)
}
Expand Down Expand Up @@ -1284,7 +1285,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().loadSingleValue(result.value) to plan.plan
is PartiQLResult.Error -> throw result.cause
}
}
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
158 changes: 158 additions & 0 deletions partiql-types/src/main/java/org/partiql/value/PartiQLData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
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 PartiQLData} 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#loadSingleValue(PartiQLData)
* @see PartiQLValue
*/
public interface PartiQLData 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},
* {@link PartiQLValueType#CHAR}, or {@link PartiQLValueType#SYMBOL}.
*/
@NotNull
String getStringValue();

/**
* 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},
* {@link PartiQLValueType#BLOB}, or {@link PartiQLValueType#CLOB}.
*/
public byte[] getBytes();

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

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

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

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

/**
* 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#INT16}.
*/
public short getShortValue();

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

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

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

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

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

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

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

0 comments on commit 34e2adf

Please sign in to comment.