@@ -65,6 +65,30 @@ private static String parseIndexValue(@Nullable IndexValue indexValue) {
6565 }
6666 }
6767
68+ /**
69+ * Get the expression index "identifier", if it exists, otherwise retrieve the generated column name.
70+ * The idea behind this is that whatever is returned from this method can be used verbatim to query the database;
71+ * it's either the expression index itself (new approach) or the virtual column (old approach).
72+ */
73+ @ Nullable
74+ public static String getIndexedExpressionOrColumn (@ Nonnull String assetType , @ Nonnull String aspect , @ Nonnull String path ,
75+ boolean nonDollarVirtualColumnsEnabled , @ Nonnull SchemaValidatorUtil schemaValidator ) {
76+ final String indexColumn = getGeneratedColumnName (assetType , aspect , path , nonDollarVirtualColumnsEnabled );
77+ final String tableName = getTableName (assetType );
78+
79+ // Check if an expression-based index exists... if it does, use that
80+ final String expressionIndexName = getExpressionIndexName (assetType , aspect , path );
81+ final String indexExpression = schemaValidator .getIndexExpression (tableName , expressionIndexName );
82+ if (indexExpression != null ) {
83+ log .info ("Using expression index '{}' in table '{}' with expression '{}'" , expressionIndexName , tableName , indexExpression );
84+ return indexExpression ;
85+ } else if (schemaValidator .columnExists (tableName , indexColumn )) {
86+ // (Pre-functional-index logic) Check for existence of (virtual) column
87+ return indexColumn ;
88+ } else {
89+ return null ;
90+ }
91+ }
6892
6993 /**
7094 * Parse {@link IndexSortCriterion} into SQL syntax.
@@ -74,19 +98,20 @@ private static String parseIndexValue(@Nullable IndexValue indexValue) {
7498 * @return SQL statement of sorting, e.g. ORDER BY ... DESC ..etc.
7599 */
76100 public static String parseSortCriteria (@ Nonnull String entityType , @ Nullable IndexSortCriterion indexSortCriterion ,
77- boolean nonDollarVirtualColumnsEnabled ) {
101+ boolean nonDollarVirtualColumnsEnabled , @ Nonnull SchemaValidatorUtil validator ) {
78102 if (indexSortCriterion == null ) {
79103 // Default to order by urn if user does not provide sort criterion.
80104 return "ORDER BY URN" ;
81105 }
82- final String indexColumn =
83- SQLSchemaUtils .getGeneratedColumnName (entityType , indexSortCriterion .getAspect (), indexSortCriterion .getPath (),
84- nonDollarVirtualColumnsEnabled );
106+
107+ final String indexedExpressionOrColumn =
108+ getIndexedExpressionOrColumn (entityType , indexSortCriterion .getAspect (), indexSortCriterion .getPath (),
109+ nonDollarVirtualColumnsEnabled , validator );
85110
86111 if (!indexSortCriterion .hasOrder ()) {
87- return "ORDER BY " + indexColumn ;
112+ return "ORDER BY " + indexedExpressionOrColumn ;
88113 } else {
89- return "ORDER BY " + indexColumn + " " + (indexSortCriterion .getOrder () == SortOrder .ASCENDING ? "ASC" : "DESC" );
114+ return "ORDER BY " + indexedExpressionOrColumn + " " + (indexSortCriterion .getOrder () == SortOrder .ASCENDING ? "ASC" : "DESC" );
90115 }
91116 }
92117
@@ -116,22 +141,16 @@ public static String parseIndexFilter(@Nonnull String entityType, @Nullable Inde
116141 if (pathParams != null ) {
117142 validateConditionAndValue (indexCriterion );
118143 final Condition condition = pathParams .getCondition ();
119- final String indexColumn = getGeneratedColumnName (entityType , aspect , pathParams .getPath (), nonDollarVirtualColumnsEnabled );
120- final String tableName = SQLSchemaUtils .getTableName (entityType );
121-
122- // NEW: Check if an expression-based index exists, if it does, use the new logic
123- final String expressionIndexName = getExpressionIndexName (entityType , aspect , pathParams .getPath ());
124- final String indexExpression = schemaValidator .getIndexExpression (tableName , expressionIndexName );
125- if (indexExpression != null ) {
126- log .debug ("Using expression index '{}' in table '{}' with expression '{}'" , expressionIndexName , tableName , indexExpression );
127- sqlFilters .add (parseSqlFilter (indexExpression , condition , pathParams .getValue ()));
128- } else if (schemaValidator .columnExists (tableName , indexColumn )) {
129- // (Pre-functional-index logic) Check for existence of (virtual) column
130- sqlFilters .add (parseSqlFilter (indexColumn , condition , pathParams .getValue ()));
131- } else {
132- // (Pre-functional-index logic) Skip filter if column doesn't exist
133- log .warn ("Skipping filter: virtual column '{}' not found in table '{}'" , indexColumn , tableName );
144+
145+ final String indexedExpressionOrColumn =
146+ getIndexedExpressionOrColumn (entityType , aspect , pathParams .getPath (), nonDollarVirtualColumnsEnabled , schemaValidator );
147+ if (indexedExpressionOrColumn == null ) {
148+ log .warn ("Skipping filter: Neither expression index nor virtual column found for Aspect '{}' and Path '{}' for Asset '{}'" ,
149+ aspect , pathParams .getPath (), entityType );
150+ continue ;
134151 }
152+
153+ sqlFilters .add (parseSqlFilter (indexedExpressionOrColumn , condition , pathParams .getValue ()));
135154 }
136155 }
137156 }
0 commit comments