@@ -45,7 +45,9 @@ use datafusion::arrow::record_batch::RecordBatch;
4545use datafusion:: datasource:: file_format:: file_compression_type:: FileCompressionType ;
4646use datafusion:: datasource:: MemTable ;
4747use datafusion:: datasource:: TableProvider ;
48- use datafusion:: execution:: context:: { SessionConfig , SessionContext , SessionState , TaskContext } ;
48+ use datafusion:: execution:: context:: {
49+ SQLOptions , SessionConfig , SessionContext , SessionState , TaskContext ,
50+ } ;
4951use datafusion:: execution:: disk_manager:: DiskManagerConfig ;
5052use datafusion:: execution:: memory_pool:: { FairSpillPool , GreedyMemoryPool , UnboundedMemoryPool } ;
5153use datafusion:: execution:: runtime_env:: { RuntimeConfig , RuntimeEnv } ;
@@ -210,6 +212,43 @@ impl PyRuntimeConfig {
210212 }
211213}
212214
215+ /// `PySQLOptions` allows you to specify options to the sql execution.
216+ #[ pyclass( name = "SQLOptions" , module = "datafusion" , subclass) ]
217+ #[ derive( Clone ) ]
218+ pub struct PySQLOptions {
219+ pub options : SQLOptions ,
220+ }
221+
222+ impl From < SQLOptions > for PySQLOptions {
223+ fn from ( options : SQLOptions ) -> Self {
224+ Self { options }
225+ }
226+ }
227+
228+ #[ pymethods]
229+ impl PySQLOptions {
230+ #[ new]
231+ fn new ( ) -> Self {
232+ let options = SQLOptions :: new ( ) ;
233+ Self { options }
234+ }
235+
236+ /// Should DDL data modification commands (e.g. `CREATE TABLE`) be run? Defaults to `true`.
237+ fn with_allow_ddl ( & self , allow : bool ) -> Self {
238+ Self :: from ( self . options . with_allow_ddl ( allow) )
239+ }
240+
241+ /// Should DML data modification commands (e.g. `INSERT and COPY`) be run? Defaults to `true`
242+ pub fn with_allow_dml ( & self , allow : bool ) -> Self {
243+ Self :: from ( self . options . with_allow_dml ( allow) )
244+ }
245+
246+ /// Should Statements such as (e.g. `SET VARIABLE and `BEGIN TRANSACTION` ...`) be run?. Defaults to `true`
247+ pub fn with_allow_statements ( & self , allow : bool ) -> Self {
248+ Self :: from ( self . options . with_allow_statements ( allow) )
249+ }
250+ }
251+
213252/// `PySessionContext` is able to plan and execute DataFusion plans.
214253/// It has a powerful optimizer, a physical planner for local execution, and a
215254/// multi-threaded execution engine to perform the execution.
@@ -285,6 +324,22 @@ impl PySessionContext {
285324 Ok ( PyDataFrame :: new ( df) )
286325 }
287326
327+ pub fn sql_with_options (
328+ & mut self ,
329+ query : & str ,
330+ options : Option < PySQLOptions > ,
331+ py : Python ,
332+ ) -> PyResult < PyDataFrame > {
333+ let options = if let Some ( options) = options {
334+ options. options
335+ } else {
336+ SQLOptions :: new ( )
337+ } ;
338+ let result = self . ctx . sql_with_options ( query, options) ;
339+ let df = wait_for_future ( py, result) . map_err ( DataFusionError :: from) ?;
340+ Ok ( PyDataFrame :: new ( df) )
341+ }
342+
288343 pub fn create_dataframe (
289344 & mut self ,
290345 partitions : PyArrowType < Vec < Vec < RecordBatch > > > ,
0 commit comments