Skip to content

Commit 468bd36

Browse files
committed
Throw exceptions instead of returning nulls in PostgresArrayTypeExtractor
1 parent 447ace1 commit 468bd36

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/query/v1/parser/filter/nonjson/field/PostgresArrayTypeExtractor.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
import org.hypertrace.core.documentstore.expression.impl.JsonIdentifierExpression;
1212
import org.hypertrace.core.documentstore.parser.SelectTypeExpressionVisitor;
1313

14+
/**
15+
* Visitor to extract PostgreSQL array type information from {@link ArrayIdentifierExpression}.
16+
*
17+
* <p>This visitor is specifically designed to work ONLY with {@link ArrayIdentifierExpression}. Any
18+
* other expression type will throw {@link UnsupportedOperationException} to catch programming
19+
* errors early.
20+
*
21+
* <p>Returns:
22+
*
23+
* <ul>
24+
* <li>The PostgreSQL array type string (e.g., "text[]", "integer[]") if {@link ArrayType} is
25+
* specified
26+
* <li>{@code null} if {@link ArrayIdentifierExpression} is used without an explicit type
27+
* </ul>
28+
*/
1429
class PostgresArrayTypeExtractor implements SelectTypeExpressionVisitor {
1530

1631
public PostgresArrayTypeExtractor() {}
@@ -22,36 +37,44 @@ public String visit(ArrayIdentifierExpression expression) {
2237

2338
@Override
2439
public String visit(JsonIdentifierExpression expression) {
25-
return null; // JSON fields don't have array type
40+
throw unsupportedExpression("JsonIdentifierExpression");
2641
}
2742

2843
@Override
2944
public String visit(IdentifierExpression expression) {
30-
return null; // Regular identifiers don't have array type (backward compat fallback)
45+
throw new UnsupportedOperationException(
46+
"PostgresArrayTypeExtractor should only be used with ArrayIdentifierExpression. "
47+
+ "Use IdentifierExpression only for scalar fields, not arrays.");
3148
}
3249

3350
@Override
3451
public String visit(AggregateExpression expression) {
35-
return null;
52+
throw unsupportedExpression("AggregateExpression");
3653
}
3754

3855
@Override
3956
public String visit(ConstantExpression expression) {
40-
return null;
57+
throw unsupportedExpression("ConstantExpression");
4158
}
4259

4360
@Override
4461
public String visit(DocumentConstantExpression expression) {
45-
return null;
62+
throw unsupportedExpression("DocumentConstantExpression");
4663
}
4764

4865
@Override
4966
public String visit(FunctionExpression expression) {
50-
return null;
67+
throw unsupportedExpression("FunctionExpression");
5168
}
5269

5370
@Override
5471
public String visit(AliasedIdentifierExpression expression) {
55-
return null;
72+
throw unsupportedExpression("AliasedIdentifierExpression");
73+
}
74+
75+
private static UnsupportedOperationException unsupportedExpression(String expressionType) {
76+
return new UnsupportedOperationException(
77+
"PostgresArrayTypeExtractor should only be used with ArrayIdentifierExpression, not "
78+
+ expressionType);
5679
}
5780
}

document-store/src/test/java/org/hypertrace/core/documentstore/postgres/query/v1/parser/filter/nonjson/field/PostgresArrayTypeExtractorTest.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
56

67
import org.hypertrace.core.documentstore.expression.impl.AggregateExpression;
78
import org.hypertrace.core.documentstore.expression.impl.AliasedIdentifierExpression;
@@ -49,15 +50,13 @@ void testVisitArrayIdentifierExpression_withoutType() {
4950
@Test
5051
void testVisitJsonIdentifierExpression() {
5152
JsonIdentifierExpression expr = JsonIdentifierExpression.of("customAttr", "field");
52-
String result = extractor.visit(expr);
53-
assertNull(result);
53+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
5454
}
5555

5656
@Test
5757
void testVisitIdentifierExpression() {
5858
IdentifierExpression expr = IdentifierExpression.of("item");
59-
String result = extractor.visit(expr);
60-
assertNull(result);
59+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
6160
}
6261

6362
@Test
@@ -66,24 +65,21 @@ void testVisitAggregateExpression() {
6665
AggregateExpression.of(
6766
org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT,
6867
IdentifierExpression.of("item"));
69-
String result = extractor.visit(expr);
70-
assertNull(result);
68+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
7169
}
7270

7371
@Test
7472
void testVisitConstantExpression() {
7573
ConstantExpression expr = ConstantExpression.of("test");
76-
String result = extractor.visit(expr);
77-
assertNull(result);
74+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
7875
}
7976

8077
@Test
8178
void testVisitDocumentConstantExpression() {
8279
ConstantExpression.DocumentConstantExpression expr =
8380
(ConstantExpression.DocumentConstantExpression)
8481
ConstantExpression.of((org.hypertrace.core.documentstore.Document) null);
85-
String result = extractor.visit(expr);
86-
assertNull(result);
82+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
8783
}
8884

8985
@Test
@@ -93,15 +89,13 @@ void testVisitFunctionExpression() {
9389
.operator(FunctionOperator.LENGTH)
9490
.operand(IdentifierExpression.of("item"))
9591
.build();
96-
String result = extractor.visit(expr);
97-
assertNull(result);
92+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
9893
}
9994

10095
@Test
10196
void testVisitAliasedIdentifierExpression() {
10297
AliasedIdentifierExpression expr =
10398
AliasedIdentifierExpression.builder().name("item").contextAlias("i").build();
104-
String result = extractor.visit(expr);
105-
assertNull(result);
99+
assertThrows(UnsupportedOperationException.class, () -> extractor.visit(expr));
106100
}
107101
}

0 commit comments

Comments
 (0)