Swapnil Jokhakar
04/11/2024, 2:26 PMSELECT
dim_field_1,
LOOKUP(CONCAT(dim_field_1, ''), 'lookup_1') AS "lookup_1_val",
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_2') AS "lookup_2_val",
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_3') AS "lookup_3_val",
dim_field_2,
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_4') AS "lookup_4_val",
(SUM(cost)) AS "cost"
FROM test_table
WHERE
dim_field_2 IN (2040)
AND __time >= '2024-03-01 00:00:00' AND __time <= '2024-03-31 23:59:59'
AND dim_field_1 IN ('1608826050', '1608826054', '1608826048', '1608818855', '1608818825', '1609091475', '1608818895', '1608818852', '1608818865', '1608818753', '1608818775', '1608818724', '1609027835', '1608273535', '1608916347', '1608268938', '1608818799', '1608818766', '1608818852', '1609569765', '1608273487', '1609347997', '1609477098', '1608826096', '1609559940', '1608916420')
GROUP BY
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_3'),
dim_field_1,
LOOKUP(CONCAT(dim_field_1, ''), 'lookup_1'),
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_3'),
LOOKUP(CONCAT(dim_field_2, ''), 'lookup_4'),
dim_field_2
In above query I have added all fields available in the select clause except the field on with aggregation function in group by clause in the different order then the field added to the select clause and in this case this query returns records with 1:M mapping between dim_field_2 field (key) and lookup_2 value, if I change the order of the group by and use the same order in which fields are added in the select clause then query returns records without including 1:M mapping. This seems to be an odd behavior. Could anyone please help here to understand the reason why the above query is returning records with 1:M mapping between dim_field_2 field (key) and lookup_2 value?
I have also tried to query the lookup_2 by adding the filter on value field in whare clause with the list of values which I am getting as 1:M mapping in the above query result and this query return result with 1:1 mapping only, this is an expected behavior as Druid Lookups can have 1:1 mapping between the key and value.Swapnil Jokhakar
04/12/2024, 6:59 AMJohn Kowtko
04/12/2024, 11:46 AMSwapnil Jokhakar
04/12/2024, 1:21 PMHowever since you are already grouping by dim_field_1 and dim_field_2, then from a SQL perspective putting the lookup() functions also in the group by clause is redundant.Yes this is correct, actually this query is not manually prepared and instead of that it is prepared using and application workflow based on the user input and as per my understanding that is why it includes the lookup fields in group by too.
What happens when you only group by dim_field_1 and dim_field_2?Query returns does not return the records with 1:M mapping dim_field_2 field (key) and lookup_2 value. Also in Druid lookup_2 for the dim_field_2 field (key) only unique values are present however still somehow Druid Query I have shared in the first message returns records with 1:M mapping dim_field_2 field (key) and lookup_2 value. Could you please let me know this could be possible?
John Kowtko
04/16/2024, 12:31 PMSwapnil Jokhakar
04/17/2024, 8:34 AMSwapnil Jokhakar
04/17/2024, 3:11 PMCan you tell me why the CONCAT(field, '') expression?• Lookup key fields are of BIGINT type and CONCAT() seems to be used to convert these values to CHAR to allow use these key field values in LOOKUP() method which accepts CHARACTER.
If you remove the CONCAT() function from the expressions does it change the behavior?• I have removed CONCAT() function and used CAST(dim_field_1 as CHAR) in select and group by fields list and with this the query returned without 1:M mapping records. Could you please let me know what could be the possible reason for this change in behavior?
if NULLs are enabled in this database then CONCAT(null, anything) should evaluate to NULL, which in turn will fail the lookup.• In Druid Docs: https://druid.apache.org/docs/latest/querying/sql-data-types/#null-values I have found that
druid.generic.useDefaultValueForNull property controls Druid's NULL handling mode and for the prior to Druid 28.0.0, druid.generic.useDefaultValueForNull = true , in our case we are using Druid version 0.21.1. So, I may need to check what value is set for this property? and then understand how it will impact the Druid table key field which we are using as a key to fetch value from lookup? Could you please check this and let me know if I am missing something here?Swapnil Jokhakar
04/18/2024, 3:01 PM"injective": true is set for the lookup_2 even though this lookup has same value for > 1 key fields. Regarding injective I read in Druid Documentation: https://druid.apache.org/docs/latest/querying/lookups/#injective-lookups and found following points:
• lookup can be considered as injective only lookup must satisfy "one-to-one lookup", All values in the lookup table must be unique. That is, no two keys can map to the same value.
• Druid does not verify whether lookups satisfy these required properties. Druid may return incorrect query results if you set injective: true for a lookup table that is not actually a one-to-one lookup.
Could this be the reason why the Druid query is returning records with 1:M mapping even though such key value pair is not event present in the lookup?
Also could you please check the observations share in above comment and share your feedabck?John Kowtko
04/19/2024, 12:02 PMSwapnil Jokhakar
04/19/2024, 12:24 PM"injective": true could be possibly creating the issue because it has been set for the lookup with 1:M mapping between key and value? If yes then I believe the following behavior are supporting it and still bit difficult to understand the possible cause:
• Why lookup_2 returns the value which does not even exist in the lookup?
• Why query returns records without 1:M mapping occurrences after using CAST(dim_field_1 as CHAR) instead of CONCAT() function in select and group by fields list?
Could you please check above points and share your feedback?Swapnil Jokhakar
04/23/2024, 3:28 PMJohn Kowtko
04/23/2024, 4:29 PMSwapnil Jokhakar
04/23/2024, 4:36 PM"injective": false for the lookup_2 with 1:M mapping between key and value field and check the results and get back if with this change still query will return records with 1:M mapping in the result.Swapnil Jokhakar
04/23/2024, 4:45 PM"injective": true could possibly create this issue because of query rewrite optimization?
• As per the Druid Doc query query rewrite optimization will possibly impact lookup fields added to the where clause and will not impact the fields added to group by clause, then could you please let me know whether "injective": true can create any issue with the query I have shared or not because in this query lookup_2 field is added to the group by clause and not to the where clause.
• Why query returns records without 1:M mapping occurrences after using CAST(dim_field_1 as CHAR) instead of CONCAT() function in select and group by fields list?
ThanksJohn Kowtko
04/23/2024, 9:17 PM