diff --git a/pkg-r/NEWS.md b/pkg-r/NEWS.md index 7594c765..ded02d63 100644 --- a/pkg-r/NEWS.md +++ b/pkg-r/NEWS.md @@ -1,9 +1,12 @@ # querychat (development version) + * Added support for Snowflake Semantic Views. When connected to Snowflake via DBI, querychat automatically discovers available Semantic Views and includes their definitions in the system prompt. This helps the LLM generate correct queries using the `SEMANTIC_VIEW()` table function with certified business metrics and dimensions. (#200) * `QueryChat$new()` now supports deferred data source. Pass `data_source = NULL` at initialization time, then provide the actual data source via the `data_source` parameter of `$server()` or by setting the `$data_source` property. This enables use cases where the data source depends on session-specific authentication or per-user database connections. (#202) +* `DBISource` now uses database-agnostic SQL for column and type detection, replacing `LIMIT` syntax with `WHERE 1=0` and `dbFetch(n=1)`. This fixes compatibility with SQL Server and other databases that don't support `LIMIT`. (#112, #197) + # querychat 0.2.0 * The update tool now requires that the SQL query returns all columns from the original data source, ensuring that the dashboard can display the complete data frame after filtering or sorting. If the query does not return all columns, an informative error message will be provided. (#180) diff --git a/pkg-r/R/DBISource.R b/pkg-r/R/DBISource.R index 389592d6..ee5ca452 100644 --- a/pkg-r/R/DBISource.R +++ b/pkg-r/R/DBISource.R @@ -68,10 +68,11 @@ DBISource <- R6::R6Class( self$table_name <- table_name # Store original column names for validation + # Use WHERE 1=0 instead of LIMIT 0 for SQL Server compatibility private$colnames <- colnames(DBI::dbGetQuery( conn, sprintf( - "SELECT * FROM %s LIMIT 0", + "SELECT * FROM %s WHERE 1=0", DBI::dbQuoteIdentifier(conn, table_name) ) )) @@ -219,12 +220,14 @@ get_schema_impl <- function( text_columns <- character(0) # Get sample of data to determine types + # Use dbFetch(n=1) instead of LIMIT 1 for SQL Server compatibility sample_query <- paste0( "SELECT * FROM ", - DBI::dbQuoteIdentifier(conn, table_name), - " LIMIT 1" + DBI::dbQuoteIdentifier(conn, table_name) ) - sample_data <- DBI::dbGetQuery(conn, prep_query(sample_query)) + rs <- DBI::dbSendQuery(conn, prep_query(sample_query)) + sample_data <- DBI::dbFetch(rs, n = 1) + DBI::dbClearResult(rs) for (col in columns) { col_class <- class(sample_data[[col]])[1]