Custom Query Parameterization Does Not Work with PARTITION BY Clause in Spotfire

book

Article ID: KB0138776

calendar_today

Updated On:

Products Versions
Spotfire Analyst 14.6.0

Description

When using custom query parameterization in Spotfire, parameters are passed to the data source as bind values (literal values), not as substitutions into the SQL text as column names or other identifiers.

If a parameter, such as ?LineByField, is used directly in a PARTITION BY clause (e.g., PARTITION BY ?LineByField), the database interprets the parameter's value as a constant string. For example, if ?LineByField has the value 'location', the database receives PARTITION BY 'location'. This results in all rows falling into a single partition because every row has the same constant partition value, so calculations run across the entire table rather than by the intended column. This behavior is expected and documented.

In contrast, parameterization works as expected in a WHERE clause (e.g., location = ?LineByField) because the parameter is compared to a column as a value, which is a supported use case for bind values.

Environment

Parameters in custom queries:
https://docs.tibco.com/pub/sfire-analyst/14.6.0/doc/html/en-US/TIB_sfire_client/client/topics/en-US/connectors_custom_query_parameters.html

Resolution

General Workaround: Using a CASE Expression
A parameter cannot be used directly as a column name in the PARTITION BY clause. Instead, use the parameter's value inside a CASE expression to return the desired column for partitioning.

Example Custom Query:
sql select quarter, agein_cobm, agein_amt, location, business_unit, sum(agein_cobm) over (partition by location order by quarter) as agein_cobm_runsum_loc, sum(agein_amt) over (partition by location order by quarter) as agein_amt_runsum_loc, sum(agein_cobm) over (partition by case when cast(?LineByField as varchar(50)) = 'location' then location when cast(?LineByField as varchar(50)) = 'business_unit' then business_unit end order by quarter) as agein_cobm_runsum_parm, sum(agein_amt) over (partition by case when cast(?LineByField as varchar(50)) = 'location' then location when cast(?LineByField as varchar(50)) = 'business_unit' then business_unit end order by quarter) as agein_amt_runsum_parm from over_custom_expression_parameterized

Please note:

  • Keep the LineByField parameter as data type String on the Parameters tab.
  • The parameter value must exactly match the text in the CASE branches (for example, 'location' or 'business_unit').
  • The CAST around the parameter ensures the database treats it as a string. Some databases do not accept an untyped parameter inside a CASE expression. Adjust varchar(50) to your database's string type if needed.
  • All CASE branches must return the same data type. If you add a non-string column, cast it to a string as well.
  • To make more columns selectable, add one WHEN line per column.

PostgreSQL-Specific Workaround: Using JSON Functions
For PostgreSQL, you can avoid a lengthy CASE statement by looking up the column value using JSON functions.

Example Custom Query (PostgreSQL):
sql select quarter, agein_cobm, agein_amt, location, business_unit, sum(agein_cobm) over (partition by to_jsonb(t) ->> cast(?LineByField as text) order by quarter) as agein_cobm_runsum_parm, sum(agein_amt) over (partition by to_jsonb(t) ->> cast(?LineByField as text) order by quarter) as agein_amt_runsum_parm from over_custom_expression_parameterized t

How it works: to_jsonb(t) converts each row into a JSON object with column names as keys. The ->> operator then returns the value of the column whose name is passed in ?LineByField, and that value is used for partitioning. Every column in the table becomes selectable without listing it in the query.

Please note:

  • The parameter value must match the column name as stored in PostgreSQL. Unquoted column names are stored in lowercase, for example 'location'.
  • If the parameter does not match any column name, the expression returns NULL, and all rows fall into one partition, similar to the original issue.
  • The values are compared as text. This does not affect partitioning, as rows with the same value still fall into the same partition.
  • Converting each row to JSON adds some processing overhead. For large tables, test performance with your data volume.
  • This syntax is specific to PostgreSQL. Other databases may have similar JSON functions, but the syntax will differ.

Issue/Introduction

Custom query parameters in Spotfire are passed as literal bind values to the data source, not as SQL identifiers. This prevents their direct use as column names in the PARTITION BY clause, leading to incorrect partitioning. This article explains the expected behavior and provides workarounds.