@@ -367,6 +367,115 @@ describe('ClickHouseDatasource', () => {
367367 } ) ;
368368 } ) ;
369369
370+ describe ( 'Hide Table Name In AdHoc Filters' , ( ) => {
371+ it ( 'should return only column names when hideTableNameInAdhocFilters is true' , async ( ) => {
372+ jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => 'foo' ) ;
373+ const ds = cloneDeep ( mockDatasource ) ;
374+ ds . settings . jsonData . hideTableNameInAdhocFilters = true ;
375+ const frame = arrayToDataFrame ( [ { name : 'foo' , type : 'String' , table : 'table' } ] ) ;
376+ jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
377+
378+ const keys = await ds . getTagKeys ( ) ;
379+ expect ( keys ) . toEqual ( [ { text : 'foo' } ] ) ;
380+ } ) ;
381+
382+ it ( 'should return table.column when hideTableNameInAdhocFilters is false' , async ( ) => {
383+ jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => 'foo' ) ;
384+ const ds = cloneDeep ( mockDatasource ) ;
385+ ds . settings . jsonData . hideTableNameInAdhocFilters = false ;
386+ const frame = arrayToDataFrame ( [ { name : 'foo' , type : 'String' , table : 'table' } ] ) ;
387+ jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
388+
389+ const keys = await ds . getTagKeys ( ) ;
390+ expect ( keys ) . toEqual ( [ { text : 'table.foo' } ] ) ;
391+ } ) ;
392+
393+ it ( 'should return table.column when hideTableNameInAdhocFilters is undefined (default)' , async ( ) => {
394+ jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => 'foo' ) ;
395+ const ds = cloneDeep ( mockDatasource ) ;
396+ ds . settings . jsonData . hideTableNameInAdhocFilters = undefined ;
397+ const frame = arrayToDataFrame ( [ { name : 'foo' , type : 'String' , table : 'table' } ] ) ;
398+ jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
399+
400+ const keys = await ds . getTagKeys ( ) ;
401+ expect ( keys ) . toEqual ( [ { text : 'table.foo' } ] ) ;
402+ } ) ;
403+
404+ it ( 'should fetch tag values with column name when hideTableNameInAdhocFilters is true' , async ( ) => {
405+ const spyOnReplace = jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => 'foo' ) ;
406+ const ds = cloneDeep ( mockDatasource ) ;
407+ ds . settings . jsonData . hideTableNameInAdhocFilters = true ;
408+ const frame = arrayToDataFrame ( [ { bar : 'value1' } , { bar : 'value2' } ] ) ;
409+ const spyOnQuery = jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
410+
411+ const values = await ds . getTagValues ( { key : 'bar' } ) ;
412+ expect ( spyOnReplace ) . toHaveBeenCalled ( ) ;
413+ const expected = { rawSql : 'select distinct bar from foo limit 1000' } ;
414+
415+ expect ( spyOnQuery ) . toHaveBeenCalledWith (
416+ expect . objectContaining ( { targets : expect . arrayContaining ( [ expect . objectContaining ( expected ) ] ) } )
417+ ) ;
418+
419+ expect ( values ) . toEqual ( [ { text : 'value1' } , { text : 'value2' } ] ) ;
420+ } ) ;
421+
422+ it ( 'should fetch tag values with table.column format when hideTableNameInAdhocFilters is false' , async ( ) => {
423+ const spyOnReplace = jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => '$clickhouse_adhoc_query' ) ;
424+ const ds = cloneDeep ( mockDatasource ) ;
425+ ds . settings . jsonData . defaultDatabase = undefined ;
426+ ds . settings . jsonData . hideTableNameInAdhocFilters = false ;
427+ const frame = arrayToDataFrame ( [ { bar : 'value1' } , { bar : 'value2' } ] ) ;
428+ const spyOnQuery = jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
429+
430+ const values = await ds . getTagValues ( { key : 'foo.bar' } ) ;
431+ expect ( spyOnReplace ) . toHaveBeenCalled ( ) ;
432+ const expected = { rawSql : 'select distinct bar from foo limit 1000' } ;
433+
434+ expect ( spyOnQuery ) . toHaveBeenCalledWith (
435+ expect . objectContaining ( { targets : expect . arrayContaining ( [ expect . objectContaining ( expected ) ] ) } )
436+ ) ;
437+
438+ expect ( values ) . toEqual ( [ { text : 'value1' } , { text : 'value2' } ] ) ;
439+ } ) ;
440+
441+ it ( 'should handle nested column names with dots when hideTableNameInAdhocFilters is true' , async ( ) => {
442+ const spyOnReplace = jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => 'foo' ) ;
443+ const ds = cloneDeep ( mockDatasource ) ;
444+ ds . settings . jsonData . hideTableNameInAdhocFilters = true ;
445+ const frame = arrayToDataFrame ( [ { 'nested.field' : 'value1' } , { 'nested.field' : 'value2' } ] ) ;
446+ const spyOnQuery = jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
447+
448+ const values = await ds . getTagValues ( { key : 'nested.field' } ) ;
449+ expect ( spyOnReplace ) . toHaveBeenCalled ( ) ;
450+ const expected = { rawSql : 'select distinct nested.field from foo limit 1000' } ;
451+
452+ expect ( spyOnQuery ) . toHaveBeenCalledWith (
453+ expect . objectContaining ( { targets : expect . arrayContaining ( [ expect . objectContaining ( expected ) ] ) } )
454+ ) ;
455+
456+ expect ( values ) . toEqual ( [ { text : 'value1' } , { text : 'value2' } ] ) ;
457+ } ) ;
458+
459+ it ( 'should handle nested column names with dots when hideTableNameInAdhocFilters is false' , async ( ) => {
460+ const spyOnReplace = jest . spyOn ( templateSrvMock , 'replace' ) . mockImplementation ( ( ) => '$clickhouse_adhoc_query' ) ;
461+ const ds = cloneDeep ( mockDatasource ) ;
462+ ds . settings . jsonData . defaultDatabase = undefined ;
463+ ds . settings . jsonData . hideTableNameInAdhocFilters = false ;
464+ const frame = arrayToDataFrame ( [ { 'nested.field' : 'value1' } , { 'nested.field' : 'value2' } ] ) ;
465+ const spyOnQuery = jest . spyOn ( ds , 'query' ) . mockImplementation ( ( _request ) => of ( { data : [ frame ] } ) ) ;
466+
467+ const values = await ds . getTagValues ( { key : 'foo.nested.field' } ) ;
468+ expect ( spyOnReplace ) . toHaveBeenCalled ( ) ;
469+ const expected = { rawSql : 'select distinct nested.field from foo limit 1000' } ;
470+
471+ expect ( spyOnQuery ) . toHaveBeenCalledWith (
472+ expect . objectContaining ( { targets : expect . arrayContaining ( [ expect . objectContaining ( expected ) ] ) } )
473+ ) ;
474+
475+ expect ( values ) . toEqual ( [ { text : 'value1' } , { text : 'value2' } ] ) ;
476+ } ) ;
477+ } ) ;
478+
370479 describe ( 'Conditional All' , ( ) => {
371480 it ( 'should replace $__conditionalAll with 1=1 when all is selected' , async ( ) => {
372481 const rawSql = 'select stuff from table where $__conditionalAll(fieldVal in ($fieldVal), $fieldVal);' ;
0 commit comments