Issue with Aliasing Fields in SQL Queries to create datasets of Solr backend as datasource in Apache Superset

Overview

Recently, while writing an SQL query for Solr as a data source in Apache Superset, I encountered a weird issue related to aliasing fields in SQL queries. This post details the problem and the workaround that resolved it.

The Problem

When selecting a field of type UserDefinedType in an SQL query, Superset throws an error.

SELECT 
    orderId AS `orderId`,       -- orderId is of UserDefinedType
    itemShippedDate AS `itemShippedDate`
FROM 
    enterpriseSearch
WHERE 
    docType = 'OISGIR' 
ORDER BY 
    reservedDatetime;

Running this above query results in an error in the Superset

Solution

After Investigating this issue, We found that when we are using aliases for the selected fields of type UserDefinedType, and assigning the same alias as the name of the actual attribute. Then, the superset will throw an error.

To avoid this kind of issue, a simple ready-to-go solution is not to give the same alias as the attribute name.

Here is the corrected query:

SELECT 
    orderId AS `order_Id`,       -- Using different alias this time
    itemShippedDate AS `itemShippedDate`
FROM 
    enterpriseSearch
WHERE 
    docType = 'OISGIR' 
ORDER BY 
    reservedDatetime;
1 Like

I will keep you updated what was the actual issue internally occurring in the superset, I think this error occurred due to a syntax error in the transformed Solr query (the query built from the SQL syntax).