@@ -2033,6 +2033,168 @@ describe('SQL Generation', () => {
2033
2033
FROM
2034
2034
(select * from order where (type = ?)) AS "order" WHERE ("order".type = ?) AND ("order".category = ?)` ) ;
2035
2035
} ) ;
2036
+
2037
+ it ( 'view referencing cube with FILTER_PARAMS - multiple filters and complex query' , async ( ) => {
2038
+ /** @type {Compilers } */
2039
+ const viewCompiler = prepareYamlCompiler (
2040
+ createSchemaYaml ( {
2041
+ cubes : [ {
2042
+ name : 'Product' ,
2043
+ sql : 'select * from products where {FILTER_PARAMS.Product.category.filter(\'category\')} and {FILTER_PARAMS.Product.status.filter(\'status\')}' ,
2044
+ measures : [
2045
+ {
2046
+ name : 'count' ,
2047
+ type : 'count' ,
2048
+ } ,
2049
+ {
2050
+ name : 'revenue' ,
2051
+ sql : 'price' ,
2052
+ type : 'sum' ,
2053
+ }
2054
+ ] ,
2055
+ dimensions : [
2056
+ {
2057
+ name : 'category' ,
2058
+ sql : 'category' ,
2059
+ type : 'string'
2060
+ } ,
2061
+ {
2062
+ name : 'status' ,
2063
+ sql : 'status' ,
2064
+ type : 'string'
2065
+ } ,
2066
+ {
2067
+ name : 'name' ,
2068
+ sql : 'name' ,
2069
+ type : 'string'
2070
+ }
2071
+ ]
2072
+ } ] ,
2073
+ views : [ {
2074
+ name : 'product_analytics' ,
2075
+ cubes : [ {
2076
+ join_path : 'Product' ,
2077
+ prefix : true ,
2078
+ includes : [
2079
+ 'category' ,
2080
+ 'status' ,
2081
+ 'name' ,
2082
+ 'count' ,
2083
+ 'revenue'
2084
+ ]
2085
+ } ]
2086
+ } ]
2087
+ } )
2088
+ ) ;
2089
+
2090
+ await viewCompiler . compiler . compile ( ) ;
2091
+ const query = new PostgresQuery ( viewCompiler , {
2092
+ measures : [ 'product_analytics.Product_count' , 'product_analytics.Product_revenue' ] ,
2093
+ dimensions : [ 'product_analytics.Product_name' ] ,
2094
+ filters : [
2095
+ {
2096
+ member : 'product_analytics.Product_category' ,
2097
+ operator : 'equals' ,
2098
+ values : [ 'electronics' ] ,
2099
+ } ,
2100
+ {
2101
+ member : 'product_analytics.Product_status' ,
2102
+ operator : 'equals' ,
2103
+ values : [ 'active' ] ,
2104
+ } ,
2105
+ ] ,
2106
+ } ) ;
2107
+ const queryAndParams = query . buildSqlAndParams ( ) ;
2108
+ const queryString = queryAndParams [ 0 ] ;
2109
+
2110
+ expect ( queryString ) . toContain ( 'select * from products where (category = $1) and (status = $2)' ) ;
2111
+ expect ( queryString ) . toMatch ( / S E L E C T \s + " p r o d u c t " \. n a m e / ) ;
2112
+ expect ( queryString ) . toMatch ( / c o u n t \( \* \) / ) ;
2113
+ expect ( queryString ) . toMatch ( / s u m \( " p r o d u c t " \. p r i c e \) / ) ;
2114
+ expect ( queryString ) . toContain ( 'WHERE ("product".category = $3) AND ("product".status = $4)' ) ;
2115
+ expect ( queryAndParams [ 1 ] ) . toEqual ( [ 'electronics' , 'active' , 'electronics' , 'active' ] ) ;
2116
+ } ) ;
2117
+
2118
+ it ( 'cube with FILTER_PARAMS in measure filters - triggers backAlias collection' , async ( ) => {
2119
+ /** @type {Compilers } */
2120
+ const filterParamsCompiler = prepareYamlCompiler (
2121
+ createSchemaYaml ( {
2122
+ cubes : [ {
2123
+ name : 'Sales' ,
2124
+ sql : 'select * from sales' ,
2125
+ measures : [
2126
+ {
2127
+ name : 'count' ,
2128
+ type : 'count' ,
2129
+ } ,
2130
+ {
2131
+ name : 'filtered_revenue' ,
2132
+ sql : 'amount' ,
2133
+ type : 'sum' ,
2134
+ // This measure filter with FILTER_PARAMS should trigger backAlias collection
2135
+ // when evaluating symbols
2136
+ filters : [
2137
+ { sql : '{FILTER_PARAMS.Sales.category.filter(\'category\')}' }
2138
+ ]
2139
+ }
2140
+ ] ,
2141
+ dimensions : [
2142
+ {
2143
+ name : 'id' ,
2144
+ sql : 'id' ,
2145
+ type : 'number' ,
2146
+ primaryKey : true
2147
+ } ,
2148
+ {
2149
+ name : 'category' ,
2150
+ sql : 'category' ,
2151
+ type : 'string'
2152
+ } ,
2153
+ {
2154
+ name : 'region' ,
2155
+ sql : 'region' ,
2156
+ type : 'string'
2157
+ }
2158
+ ]
2159
+ } ] ,
2160
+ views : [ {
2161
+ name : 'sales_analytics' ,
2162
+ cubes : [ {
2163
+ join_path : 'Sales' ,
2164
+ prefix : true ,
2165
+ includes : [
2166
+ 'count' ,
2167
+ 'filtered_revenue' ,
2168
+ 'category' ,
2169
+ 'region'
2170
+ ]
2171
+ } ]
2172
+ } ]
2173
+ } )
2174
+ ) ;
2175
+
2176
+ await filterParamsCompiler . compiler . compile ( ) ;
2177
+
2178
+ const query = new PostgresQuery ( filterParamsCompiler , {
2179
+ measures : [ 'sales_analytics.Sales_filtered_revenue' ] ,
2180
+ dimensions : [ 'sales_analytics.Sales_region' ] ,
2181
+ filters : [
2182
+ {
2183
+ member : 'sales_analytics.Sales_category' ,
2184
+ operator : 'equals' ,
2185
+ values : [ 'electronics' ] ,
2186
+ } ,
2187
+ ] ,
2188
+ } ) ;
2189
+
2190
+ const queryAndParams = query . buildSqlAndParams ( ) ;
2191
+ const queryString = queryAndParams [ 0 ] ;
2192
+
2193
+ expect ( queryString ) . toContain ( 'CASE WHEN (((category = $1)))' ) ;
2194
+ expect ( queryString ) . toMatch ( / s u m .* C A S E W H E N / ) ;
2195
+ expect ( queryString ) . toContain ( 'WHERE ("sales".category = $2)' ) ;
2196
+ expect ( queryAndParams [ 1 ] ) . toEqual ( [ 'electronics' , 'electronics' ] ) ;
2197
+ } ) ;
2036
2198
} ) ;
2037
2199
2038
2200
describe ( 'FILTER_GROUP' , ( ) => {
0 commit comments