Skip to content

Commit 5e76e5e

Browse files
committed
Introduces abstract types for flat collections, address other comments
1 parent 3083cc1 commit 5e76e5e

File tree

12 files changed

+192
-605
lines changed

12 files changed

+192
-605
lines changed

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/ArrayIdentifierExpression.java

Lines changed: 19 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.hypertrace.core.documentstore.expression.impl;
22

3-
import java.util.Optional;
43
import lombok.EqualsAndHashCode;
54
import org.hypertrace.core.documentstore.parser.SelectTypeExpressionVisitor;
65

@@ -14,132 +13,66 @@
1413
@EqualsAndHashCode(callSuper = true)
1514
public class ArrayIdentifierExpression extends IdentifierExpression {
1615

17-
private final FlatCollectionDataType arrayElementType;
16+
private final DataType arrayElementType;
1817

19-
public ArrayIdentifierExpression(String name) {
20-
this(name, null);
18+
ArrayIdentifierExpression(String name) {
19+
this(name, DataType.UNSPECIFIED);
2120
}
2221

23-
// Package-private: used internally by factory methods
24-
ArrayIdentifierExpression(String name, FlatCollectionDataType arrayElementType) {
25-
super(name, null);
22+
ArrayIdentifierExpression(String name, DataType arrayElementType) {
23+
super(name);
2624
this.arrayElementType = arrayElementType;
2725
}
2826

2927
public static ArrayIdentifierExpression of(String name) {
3028
return new ArrayIdentifierExpression(name);
3129
}
3230

33-
// Package-private: used internally by factory methods
34-
static ArrayIdentifierExpression of(String name, FlatCollectionDataType arrayElementType) {
31+
static ArrayIdentifierExpression of(String name, DataType arrayElementType) {
3532
return new ArrayIdentifierExpression(name, arrayElementType);
3633
}
3734

3835
public static ArrayIdentifierExpression ofStrings(final String name) {
39-
return of(name, PostgresDataType.TEXT);
36+
return of(name, DataType.STRING);
4037
}
4138

4239
public static ArrayIdentifierExpression ofInts(final String name) {
43-
return of(name, PostgresDataType.INTEGER);
40+
return of(name, DataType.INTEGER);
4441
}
4542

4643
public static ArrayIdentifierExpression ofLongs(final String name) {
47-
return of(name, PostgresDataType.BIGINT);
48-
}
49-
50-
public static ArrayIdentifierExpression ofShorts(final String name) {
51-
return of(name, PostgresDataType.SMALLINT);
44+
return of(name, DataType.LONG);
5245
}
5346

5447
public static ArrayIdentifierExpression ofFloats(final String name) {
55-
return of(name, PostgresDataType.FLOAT);
48+
return of(name, DataType.FLOAT);
5649
}
5750

5851
public static ArrayIdentifierExpression ofDoubles(final String name) {
59-
return of(name, PostgresDataType.DOUBLE);
60-
}
61-
62-
public static ArrayIdentifierExpression ofDecimals(final String name) {
63-
return of(name, PostgresDataType.NUMERIC);
52+
return of(name, DataType.DOUBLE);
6453
}
6554

6655
public static ArrayIdentifierExpression ofBooleans(final String name) {
67-
return of(name, PostgresDataType.BOOLEAN);
68-
}
69-
70-
public static ArrayIdentifierExpression ofTimestamps(final String name) {
71-
return of(name, PostgresDataType.TIMESTAMP);
56+
return of(name, DataType.BOOLEAN);
7257
}
7358

7459
public static ArrayIdentifierExpression ofTimestampsTz(final String name) {
75-
return of(name, PostgresDataType.TIMESTAMPTZ);
60+
return of(name, DataType.TIMESTAMPTZ);
7661
}
7762

7863
public static ArrayIdentifierExpression ofDates(final String name) {
79-
return of(name, PostgresDataType.DATE);
80-
}
81-
82-
public static ArrayIdentifierExpression ofUuids(final String name) {
83-
return of(name, PostgresDataType.UUID);
84-
}
85-
86-
public static ArrayIdentifierExpression ofBytesArray(final String name) {
87-
return of(name, PostgresDataType.BYTEA);
88-
}
89-
90-
// Package-private: used internally by getPostgresArrayTypeString()
91-
Optional<FlatCollectionDataType> getArrayElementType() {
92-
return Optional.ofNullable(arrayElementType);
64+
return of(name, DataType.DATE);
9365
}
9466

9567
/**
96-
* Returns the PostgreSQL array type string for this expression, if the element type is specified.
68+
* Returns the data type of array elements.
9769
*
98-
* <p>Examples:
70+
* <p>This is used by database-specific type extractors to generate appropriate type casts.
9971
*
100-
* <ul>
101-
* <li>TEXT → "text[]"
102-
* <li>INTEGER → "integer[]"
103-
* <li>BOOLEAN → "boolean[]"
104-
* <li>FLOAT → "real[]"
105-
* <li>DOUBLE → "double precision[]"
106-
* </ul>
107-
*
108-
* @return Optional containing the PostgreSQL array type string, or empty if no type is specified
109-
* @throws IllegalArgumentException if the element type is not a PostgresDataType
72+
* @return The element DataType (UNSPECIFIED if no type was explicitly set)
11073
*/
111-
public Optional<String> getPostgresArrayTypeString() {
112-
return getArrayElementType().map(this::toPostgresArrayType);
113-
}
114-
115-
private String toPostgresArrayType(FlatCollectionDataType type) {
116-
if (!(type instanceof PostgresDataType)) {
117-
throw new IllegalArgumentException(
118-
"Only PostgresDataType is currently supported, got: " + type.getClass());
119-
}
120-
PostgresDataType postgresType = (PostgresDataType) type;
121-
String baseType = postgresType.getPgTypeName();
122-
123-
// Map internal type names to SQL array type names
124-
switch (baseType) {
125-
case "int4":
126-
return "integer[]";
127-
case "int8":
128-
return "bigint[]";
129-
case "int2":
130-
return "smallint[]";
131-
case "float4":
132-
return "real[]";
133-
case "float8":
134-
return "double precision[]";
135-
case "bool":
136-
return "boolean[]";
137-
case "bytea":
138-
return "bytea[]";
139-
default:
140-
// For most types, just append []
141-
return baseType + "[]";
142-
}
74+
public DataType getElementDataType() {
75+
return arrayElementType;
14376
}
14477

14578
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hypertrace.core.documentstore.expression.impl;
2+
3+
/**
4+
* Database-agnostic data types for explicit type annotation in queries.
5+
*
6+
* <p>This enum provides type metadata for {@link IdentifierExpression} and {@link
7+
* ArrayIdentifierExpression} fields in flat collections, enabling type-safe query generation
8+
* without runtime type inference.
9+
*
10+
* <p>These types are mapped to database-specific types at query parsing time. For example, when
11+
* generating PostgreSQL queries, {@code STRING} maps to {@code text}, {@code INTEGER} maps to
12+
* {@code int4}, etc.
13+
*
14+
* @see ArrayIdentifierExpression
15+
* @see IdentifierExpression
16+
*/
17+
public enum DataType {
18+
UNSPECIFIED,
19+
STRING,
20+
INTEGER,
21+
LONG,
22+
FLOAT,
23+
DOUBLE,
24+
BOOLEAN,
25+
TIMESTAMPTZ,
26+
DATE
27+
}

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/FlatCollectionDataType.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/IdentifierExpression.java

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*
1919
* <p>Example: IdentifierExpression.of("col1");
2020
*
21-
* <p>For flat relational collections, you can optionally provide a {@link FlatCollectionDataType}
22-
* to enable type-safe query generation without runtime type inference:
21+
* <p>For flat relational collections, you can optionally provide a {@link DataType} to enable
22+
* type-safe query generation without runtime type inference:
2323
*
24-
* <p>Example: IdentifierExpression.of("price", PostgresType.INTEGER);
24+
* <p>Example: IdentifierExpression.ofInt("price");
2525
*/
2626
@Value
2727
@NonFinal
@@ -30,75 +30,56 @@ public class IdentifierExpression
3030
implements GroupTypeExpression, SelectTypeExpression, SortTypeExpression {
3131

3232
String name;
33-
// Type information of this identifier for flat collections, optional
34-
FlatCollectionDataType flatCollectionDataType;
33+
// Type information of this identifier for flat collections, this is optional to maintain backward
34+
// compatibility
35+
DataType dataType;
36+
37+
IdentifierExpression(String name) {
38+
this.name = name;
39+
this.dataType = DataType.UNSPECIFIED;
40+
}
3541

3642
public static IdentifierExpression of(final String name) {
3743
Preconditions.checkArgument(name != null && !name.isBlank(), "name is null or blank");
38-
return new IdentifierExpression(name, null);
44+
return new IdentifierExpression(name);
3945
}
4046

4147
// Package-private: used internally by factory methods
42-
static IdentifierExpression of(
43-
final String name, final FlatCollectionDataType flatCollectionDataType) {
48+
static IdentifierExpression of(final String name, final DataType dataType) {
4449
Preconditions.checkArgument(name != null && !name.isBlank(), "name is null or blank");
45-
return new IdentifierExpression(name, flatCollectionDataType);
50+
return new IdentifierExpression(name, dataType);
4651
}
4752

4853
public static IdentifierExpression ofString(final String name) {
49-
return of(name, PostgresDataType.TEXT);
54+
return of(name, DataType.STRING);
5055
}
5156

5257
public static IdentifierExpression ofInt(final String name) {
53-
return of(name, PostgresDataType.INTEGER);
58+
return of(name, DataType.INTEGER);
5459
}
5560

5661
public static IdentifierExpression ofLong(final String name) {
57-
return of(name, PostgresDataType.BIGINT);
58-
}
59-
60-
public static IdentifierExpression ofShort(final String name) {
61-
return of(name, PostgresDataType.SMALLINT);
62+
return of(name, DataType.LONG);
6263
}
6364

6465
public static IdentifierExpression ofFloat(final String name) {
65-
return of(name, PostgresDataType.FLOAT);
66+
return of(name, DataType.FLOAT);
6667
}
6768

6869
public static IdentifierExpression ofDouble(final String name) {
69-
return of(name, PostgresDataType.DOUBLE);
70-
}
71-
72-
public static IdentifierExpression ofDecimal(final String name) {
73-
return of(name, PostgresDataType.NUMERIC);
70+
return of(name, DataType.DOUBLE);
7471
}
7572

7673
public static IdentifierExpression ofBoolean(final String name) {
77-
return of(name, PostgresDataType.BOOLEAN);
78-
}
79-
80-
public static IdentifierExpression ofTimestamp(final String name) {
81-
return of(name, PostgresDataType.TIMESTAMP);
74+
return of(name, DataType.BOOLEAN);
8275
}
8376

8477
public static IdentifierExpression ofTimestampTz(final String name) {
85-
return of(name, PostgresDataType.TIMESTAMPTZ);
78+
return of(name, DataType.TIMESTAMPTZ);
8679
}
8780

8881
public static IdentifierExpression ofDate(final String name) {
89-
return of(name, PostgresDataType.DATE);
90-
}
91-
92-
public static IdentifierExpression ofUuid(final String name) {
93-
return of(name, PostgresDataType.UUID);
94-
}
95-
96-
public static IdentifierExpression ofJsonb(final String name) {
97-
return of(name, PostgresDataType.JSONB);
98-
}
99-
100-
public static IdentifierExpression ofBytes(final String name) {
101-
return of(name, PostgresDataType.BYTEA);
82+
return of(name, DataType.DATE);
10283
}
10384

10485
@Override

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/JsonFieldType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public enum JsonFieldType {
99
NUMBER_ARRAY,
1010
BOOLEAN_ARRAY,
1111
OBJECT_ARRAY,
12-
OBJECT
12+
OBJECT,
13+
UNSPECIFIED
1314
}

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/JsonIdentifierExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class JsonIdentifierExpression extends IdentifierExpression {
2121

2222
String columnName; // e.g., "customAttr" (the top-level JSONB column)
2323
List<String> jsonPath; // e.g., ["myAttribute", "nestedField"]
24-
JsonFieldType fieldType; // Optional: PRIMITIVE or ARRAY for optimization
24+
JsonFieldType fieldType;
2525

2626
public static JsonIdentifierExpression of(final String columnName) {
2727
throw new IllegalArgumentException(

document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/PostgresDataType.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)