Skip to content

Commit 216406b

Browse files
committed
patch to unwind text array exp for postgres
1 parent e869f86 commit 216406b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/query/v1/vistors/PostgresFromTypeExpressionVisitor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class PostgresFromTypeExpressionVisitor implements FromTypeExpressionVisi
2424
private static final String PRESERVE_NULL_AND_EMPTY_TABLE_QUERY_FMT =
2525
"%s as (SELECT * from %s %s LEFT JOIN LATERAL %s %s on TRUE)";
2626
private static final String UNWIND_EXP_FMT = "jsonb_array_elements(%s)";
27+
private static final String UNWIND_TEXT_ARRAY_EXP_FMT = "unnest(%s)";
2728
private static final String UNWIND_EXP_ALIAS_FMT = "p%s(%s)";
2829

2930
private PostgresQueryParser postgresQueryParser;
@@ -52,9 +53,17 @@ public String visit(UnnestExpression unnestExpression) {
5253
String preTable = "table" + preIndex;
5354
String newTable = "table" + nextIndex;
5455
String tableAlias = "t" + preIndex;
55-
String unwindExpr = String.format(UNWIND_EXP_FMT, transformedFieldName);
5656
String unwindExprAlias = String.format(UNWIND_EXP_ALIAS_FMT, nextIndex, pgColumnName);
5757

58+
// patch if the collection has non-json fields
59+
String flatStructureCollection = postgresQueryParser.getFlatStructureCollectionName();
60+
String unwindExpr =
61+
(flatStructureCollection != null
62+
&& flatStructureCollection.equals(
63+
postgresQueryParser.getTableIdentifier().getTableName()))
64+
? String.format(UNWIND_TEXT_ARRAY_EXP_FMT, transformedFieldName)
65+
: String.format(UNWIND_EXP_FMT, transformedFieldName);
66+
5867
String fmt =
5968
unnestExpression.isPreserveNullAndEmptyArrays()
6069
? PRESERVE_NULL_AND_EMPTY_TABLE_QUERY_FMT

0 commit comments

Comments
 (0)