Skip to content

Commit 7f34544

Browse files
committed
Simplifies iteration and structs
1 parent 6db2563 commit 7f34544

File tree

15 files changed

+193
-196
lines changed

15 files changed

+193
-196
lines changed

partiql-eval/api/partiql-eval.api

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
public abstract interface class org/partiql/eval/PQLValue {
1+
public abstract interface class org/partiql/eval/PQLValue : java/lang/Iterable {
22
public static fun bagValue (Ljava/lang/Iterable;)Lorg/partiql/eval/PQLValue;
33
public static fun boolValue (Z)Lorg/partiql/eval/PQLValue;
4-
public fun getBagValues ()Ljava/util/Iterator;
4+
public fun get (Ljava/lang/String;)Lorg/partiql/eval/PQLValue;
55
public fun getBinaryValue ()[B
66
public fun getBlobValue ()[B
77
public fun getBoolValue ()Z
@@ -11,25 +11,25 @@ public abstract interface class org/partiql/eval/PQLValue {
1111
public fun getDateValue ()Lorg/partiql/value/datetime/Date;
1212
public fun getDecimalArbitraryValue ()Ljava/math/BigDecimal;
1313
public fun getDecimalValue ()Ljava/math/BigDecimal;
14+
public fun getFields ()Ljava/util/Iterator;
1415
public fun getFloat32Value ()F
1516
public fun getFloat64Value ()D
17+
public fun getInsensitive (Ljava/lang/String;)Lorg/partiql/eval/PQLValue;
1618
public fun getInt16Value ()S
1719
public fun getInt32Value ()I
1820
public fun getInt64Value ()J
1921
public fun getInt8Value ()B
2022
public fun getIntValue ()Ljava/math/BigInteger;
2123
public fun getIntervalValue ()J
22-
public fun getListValues ()Ljava/util/Iterator;
23-
public fun getSexpValues ()Ljava/util/Iterator;
2424
public fun getStringValue ()Ljava/lang/String;
25-
public fun getStructFields ()Ljava/util/Iterator;
2625
public fun getSymbolValue ()Ljava/lang/String;
2726
public fun getTimeValue ()Lorg/partiql/value/datetime/Time;
2827
public fun getTimestampValue ()Lorg/partiql/value/datetime/Timestamp;
2928
public abstract fun getType ()Lorg/partiql/value/PartiQLValueType;
3029
public static fun int32Value (I)Lorg/partiql/eval/PQLValue;
3130
public static fun int64Value (J)Lorg/partiql/eval/PQLValue;
3231
public abstract fun isNull ()Z
32+
public fun iterator ()Ljava/util/Iterator;
3333
public static fun listValue (Ljava/lang/Iterable;)Lorg/partiql/eval/PQLValue;
3434
public static fun missingValue ()Lorg/partiql/eval/PQLValue;
3535
public static fun nullValue ()Lorg/partiql/eval/PQLValue;

partiql-eval/src/main/java/org/partiql/eval/BagValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public boolean isNull() {
2323
}
2424

2525
@Override
26-
@NotNull
27-
public Iterator<PQLValue> getBagValues() {
26+
public Iterator<PQLValue> iterator() {
2827
return _value.iterator();
2928
}
3029

partiql-eval/src/main/java/org/partiql/eval/ListValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.partiql.value.PartiQLValueType;
55

6-
import java.util.Collections;
76
import java.util.Iterator;
87

98
/**
@@ -25,7 +24,7 @@ public boolean isNull() {
2524

2625
@Override
2726
@NotNull
28-
public Iterator<PQLValue> getListValues() {
27+
public Iterator<PQLValue> iterator() {
2928
return _value.iterator();
3029
}
3130

partiql-eval/src/main/java/org/partiql/eval/NullValue.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,9 @@ public float getFloat32Value() {
179179
}
180180
}
181181

182-
@NotNull
183-
@Override
184-
public Iterator<PQLValue> getBagValues() {
185-
if (_type == PartiQLValueType.BAG) {
186-
throw new NullPointerException();
187-
} else {
188-
throw new UnsupportedOperationException();
189-
}
190-
}
191-
192-
@NotNull
193-
@Override
194-
public Iterator<PQLValue> getListValues() {
195-
if (_type == PartiQLValueType.LIST) {
196-
throw new NullPointerException();
197-
} else {
198-
throw new UnsupportedOperationException();
199-
}
200-
}
201-
202-
@NotNull
203182
@Override
204-
public Iterator<PQLValue> getSexpValues() {
205-
if (_type == PartiQLValueType.SEXP) {
183+
public Iterator<PQLValue> iterator() {
184+
if (_type == PartiQLValueType.BAG || _type == PartiQLValueType.LIST || _type == PartiQLValueType.SEXP) {
206185
throw new NullPointerException();
207186
} else {
208187
throw new UnsupportedOperationException();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.partiql.eval;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.partiql.value.PartiQLValue;
5+
6+
import java.util.Iterator;
7+
8+
class PQLToPartiQLIterable implements Iterable<PartiQLValue> {
9+
10+
@NotNull
11+
private final Iterable<PQLValue> _values;
12+
13+
PQLToPartiQLIterable(@NotNull PQLValue value) {
14+
this._values = value;
15+
}
16+
17+
@Override
18+
public Iterator<PartiQLValue> iterator() {
19+
Iterator<PQLValue> iter = _values.iterator();
20+
return new Iterator<PartiQLValue>() {
21+
22+
@Override
23+
public boolean hasNext() {
24+
return iter.hasNext();
25+
}
26+
27+
@Override
28+
public PartiQLValue next() {
29+
return iter.next().toPartiQLValue();
30+
}
31+
};
32+
}
33+
}

partiql-eval/src/main/java/org/partiql/eval/PQLToPartiQLIterator.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.partiql.eval;
2+
3+
import kotlin.Pair;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.partiql.value.PartiQLValue;
6+
7+
import java.util.Iterator;
8+
9+
class PQLToPartiQLStruct implements Iterable<Pair<String, PartiQLValue>> {
10+
11+
@NotNull
12+
PQLValue _value;
13+
14+
PQLToPartiQLStruct(@NotNull PQLValue value) {
15+
this._value = value;
16+
}
17+
18+
@Override
19+
public Iterator<Pair<String, PartiQLValue>> iterator() {
20+
Iterator<StructField> _fields = _value.getFields();
21+
22+
return new Iterator<Pair<String, PartiQLValue>>() {
23+
@Override
24+
public boolean hasNext() {
25+
return _fields.hasNext();
26+
}
27+
28+
@Override
29+
public Pair<String, PartiQLValue> next() {
30+
StructField field = _fields.next();
31+
return new Pair<>(field.getName(), field.getValue().toPartiQLValue());
32+
}
33+
};
34+
}
35+
}

partiql-eval/src/main/java/org/partiql/eval/PQLValue.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import kotlin.NotImplementedError;
44
import kotlin.Pair;
55
import org.jetbrains.annotations.NotNull;
6-
import org.partiql.spi.BindingName;
76
import org.partiql.value.PartiQL;
87
import org.partiql.value.PartiQLValue;
98
import org.partiql.value.PartiQLValueType;
@@ -347,7 +346,7 @@ default Iterator<StructField> getFields() {
347346
* {@link #isNull()} returns false before attempting to invoke this method.
348347
*/
349348
@NotNull
350-
default Iterable<StructField> get(@NotNull String name) {
349+
default PQLValue get(@NotNull String name) {
351350
throw new UnsupportedOperationException();
352351
}
353352

@@ -361,7 +360,7 @@ default Iterable<StructField> get(@NotNull String name) {
361360
* {@link #isNull()} returns false before attempting to invoke this method.
362361
*/
363362
@NotNull
364-
default Iterable<StructField> getInsensitive(@NotNull String name) {
363+
default PQLValue getInsensitive(@NotNull String name) {
365364
throw new UnsupportedOperationException();
366365
}
367366

@@ -420,35 +419,13 @@ default PartiQLValue toPartiQLValue() {
420419
case INTERVAL:
421420
return this.isNull() ? PartiQL.intervalValue(null) : PartiQL.intervalValue(this.getIntervalValue());
422421
case BAG:
423-
return this.isNull() ? PartiQL.bagValue((Iterable<? extends PartiQLValue>) null) : PartiQL.bagValue(
424-
new IterableFromIteratorSupplier<>(() -> new PQLToPartiQLIterator(this.iterator()))
425-
);
422+
return this.isNull() ? PartiQL.bagValue((Iterable<? extends PartiQLValue>) null) : PartiQL.bagValue(new PQLToPartiQLIterable(this));
426423
case LIST:
427-
return this.isNull() ? PartiQL.listValue((Iterable<? extends PartiQLValue>) null) : PartiQL.listValue(
428-
new IterableFromIteratorSupplier<>(() -> new PQLToPartiQLIterator(this.iterator()))
429-
);
424+
return this.isNull() ? PartiQL.listValue((Iterable<? extends PartiQLValue>) null) : PartiQL.listValue(new PQLToPartiQLIterable(this));
430425
case SEXP:
431-
return this.isNull() ? PartiQL.sexpValue((Iterable<? extends PartiQLValue>) null) : PartiQL.sexpValue(
432-
new IterableFromIteratorSupplier<>(() -> new PQLToPartiQLIterator(this.iterator()))
433-
);
426+
return this.isNull() ? PartiQL.sexpValue((Iterable<? extends PartiQLValue>) null) : PartiQL.sexpValue(new PQLToPartiQLIterable(this));
434427
case STRUCT:
435-
return this.isNull() ? PartiQL.structValue((Iterable<? extends Pair<String, ? extends PartiQLValue>>) null) : PartiQL.structValue(
436-
new IterableFromIteratorSupplier<>(() -> {
437-
Iterator<StructField> _fields = this.getFields();
438-
return new Iterator<Pair<String, PartiQLValue>>() {
439-
@Override
440-
public boolean hasNext() {
441-
return _fields.hasNext();
442-
}
443-
444-
@Override
445-
public Pair<String, PartiQLValue> next() {
446-
StructField field = _fields.next();
447-
return new Pair<>(field.getName(), field.getValue().toPartiQLValue());
448-
}
449-
};
450-
})
451-
);
428+
return this.isNull() ? PartiQL.structValue((Iterable<? extends Pair<String, ? extends PartiQLValue>>) null) : PartiQL.structValue(new PQLToPartiQLStruct(this));
452429
case NULL:
453430
return PartiQL.nullValue();
454431
case MISSING:
@@ -480,9 +457,8 @@ static PQLValue of(PartiQLValue value) {
480457
org.partiql.value.Int8Value int8Value = (org.partiql.value.Int8Value) value;
481458
return new Int8Value(Objects.requireNonNull(int8Value.getValue()));
482459
case STRUCT:
483-
@SuppressWarnings("unchecked")
484-
org.partiql.value.StructValue<PartiQLValue> STRUCTValue = (org.partiql.value.StructValue<PartiQLValue>) value;
485-
return new StructValue(new StructFieldIterableWrapper(Objects.requireNonNull(STRUCTValue.getEntries())));
460+
@SuppressWarnings("unchecked") org.partiql.value.StructValue<PartiQLValue> STRUCTValue = (org.partiql.value.StructValue<PartiQLValue>) value;
461+
return new StructValue(new PartiQLToPQLStruct(Objects.requireNonNull(STRUCTValue)));
486462
case STRING:
487463
org.partiql.value.StringValue STRINGValue = (org.partiql.value.StringValue) value;
488464
return new StringValue(Objects.requireNonNull(STRINGValue.getValue()));
@@ -496,26 +472,23 @@ static PQLValue of(PartiQLValue value) {
496472
org.partiql.value.Int16Value INT16Value = (org.partiql.value.Int16Value) value;
497473
return new Int16Value(Objects.requireNonNull(INT16Value.getValue()));
498474
case SEXP:
499-
@SuppressWarnings("unchecked")
500-
org.partiql.value.SexpValue<PartiQLValue> sexpValue = (org.partiql.value.SexpValue<PartiQLValue>) value;
501-
return new SexpValue(new PartiQLValueIterableWrapper(Objects.requireNonNull(sexpValue)));
475+
@SuppressWarnings("unchecked") org.partiql.value.SexpValue<PartiQLValue> sexpValue = (org.partiql.value.SexpValue<PartiQLValue>) value;
476+
return new SexpValue(new PartiQLToPQLIterable(Objects.requireNonNull(sexpValue)));
502477
case LIST:
503-
@SuppressWarnings("unchecked")
504-
org.partiql.value.ListValue<PartiQLValue> LISTValue = (org.partiql.value.ListValue<PartiQLValue>) value;
505-
return new ListValue(new PartiQLValueIterableWrapper(Objects.requireNonNull(LISTValue)));
478+
@SuppressWarnings("unchecked") org.partiql.value.ListValue<PartiQLValue> LISTValue = (org.partiql.value.ListValue<PartiQLValue>) value;
479+
return new ListValue(new PartiQLToPQLIterable(Objects.requireNonNull(LISTValue)));
506480
case BOOL:
507481
org.partiql.value.BoolValue BOOLValue = (org.partiql.value.BoolValue) value;
508482
return new BoolValue(Objects.requireNonNull(BOOLValue.getValue()));
509483
case INT:
510484
org.partiql.value.IntValue INTValue = (org.partiql.value.IntValue) value;
511485
return new IntValue(Objects.requireNonNull(INTValue.getValue()));
512486
case BAG:
513-
@SuppressWarnings("unchecked")
514-
org.partiql.value.BagValue<PartiQLValue> BAGValue = (org.partiql.value.BagValue<PartiQLValue>) value;
515-
return new BagValue(new PartiQLValueIterableWrapper(Objects.requireNonNull(BAGValue)));
487+
@SuppressWarnings("unchecked") org.partiql.value.BagValue<PartiQLValue> BAGValue = (org.partiql.value.BagValue<PartiQLValue>) value;
488+
return new BagValue(new PartiQLToPQLIterable(Objects.requireNonNull(BAGValue)));
516489
case BINARY:
517490
org.partiql.value.BinaryValue BINARYValue = (org.partiql.value.BinaryValue) value;
518-
return new BinaryValue(Objects.requireNonNull(BINARYValue.getValue().toByteArray()));
491+
return new BinaryValue(Objects.requireNonNull(Objects.requireNonNull(BINARYValue.getValue()).toByteArray()));
519492
case DATE:
520493
org.partiql.value.DateValue DATEValue = (org.partiql.value.DateValue) value;
521494
return new DateValue(Objects.requireNonNull(DATEValue.getValue()));

partiql-eval/src/main/java/org/partiql/eval/PartiQLValueIterableWrapper.java renamed to partiql-eval/src/main/java/org/partiql/eval/PartiQLToPQLIterable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import org.partiql.value.CollectionValue;
44
import org.partiql.value.PartiQLValue;
55

6-
class PartiQLValueIterableWrapper implements Iterable<PQLValue> {
6+
class PartiQLToPQLIterable implements Iterable<PQLValue> {
77

88
private final CollectionValue<PartiQLValue> _value;
99

10-
PartiQLValueIterableWrapper(CollectionValue<PartiQLValue> value) {
10+
PartiQLToPQLIterable(CollectionValue<PartiQLValue> value) {
1111
_value = value;
1212
}
1313

partiql-eval/src/main/java/org/partiql/eval/StructFieldIterableWrapper.java renamed to partiql-eval/src/main/java/org/partiql/eval/PartiQLToPQLStruct.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import org.jetbrains.annotations.NotNull;
55
import org.partiql.value.PartiQLValue;
66

7-
class StructFieldIterableWrapper implements Iterable<StructField> {
7+
class PartiQLToPQLStruct implements Iterable<StructField> {
88

99
private final Iterable<Pair<String, PartiQLValue>> _value;
1010

11-
StructFieldIterableWrapper(Iterable<Pair<String, PartiQLValue>> value) {
12-
_value = value;
11+
PartiQLToPQLStruct(org.partiql.value.StructValue<PartiQLValue> value) {
12+
_value = value.getEntries();
1313
}
1414

1515
@Override

partiql-eval/src/main/java/org/partiql/eval/SexpValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.partiql.value.PartiQLValueType;
55

6-
import java.util.Collections;
76
import java.util.Iterator;
87

98
/**
@@ -25,7 +24,7 @@ public boolean isNull() {
2524

2625
@Override
2726
@NotNull
28-
public Iterator<PQLValue> getSexpValues() {
27+
public Iterator<PQLValue> iterator() {
2928
return _value.iterator();
3029
}
3130

0 commit comments

Comments
 (0)