Skip to content
This repository was archived by the owner on Apr 15, 2022. It is now read-only.

Commit 4711379

Browse files
authored
Fix createTable function (#21)
* Fix createTable function missing dropTableIfExists * Update context.py optinal drop table or error if exists
1 parent 64995ff commit 4711379

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

splicemachine/spark/context.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,16 @@ def _dropTableIfExists(self, schema, table):
301301
print('Creating table {schema}.{table}'.format(schema=schema,table=table))
302302
self.execute('DROP TABLE IF EXISTS {schema}.{table}'.format(schema=schema,table=table))
303303

304-
def createTable(self, dataframe, schema_table_name, new_schema=True, types = None):
304+
def createTable(self, dataframe, schema_table_name, new_schema=True, drop_table=False, types = None):
305305
'''
306306
Creates a schema.table from a dataframe
307307
:param schema_table_name: String full table name in the format "schema.table_name"
308308
If only a table name is provided (ie no '.' in the string) schema SPLICE will be assumed
309309
If this table exists in the database already, it will be DROPPED and a new one will be created
310310
:param dataframe: The dataframe that the table will be created for
311311
:param new_schema: A boolean to create a new schema. If True, the function will create a new schema before creating the table. If the schema already exists, set to False [DEFAULT True]
312-
:param types: A dictionary of type {string: string} containing column names and their respective SQL types. The values of the dictionary MUST be valid SQL types. See https://doc.splicemachine.com/sqlref_datatypes_intro.html
312+
:param drop_table: An optinal boolean to drop the table if it exists. [DEFAULT False]
313+
:param types: An optional dictionary of type {string: string} containing column names and their respective SQL types. The values of the dictionary MUST be valid SQL types. See https://doc.splicemachine.com/sqlref_datatypes_intro.html
313314
If None or if any types are missing, types will be assumed automatically from the dataframe schema as follows:
314315
BooleanType: BOOLEAN
315316
ByteType: TINYINT
@@ -326,7 +327,11 @@ def createTable(self, dataframe, schema_table_name, new_schema=True, types = Non
326327
'''
327328
db_schema = self._generateDBSchema(dataframe, types=types)
328329
schema, table = self._getCreateTableSchema(schema_table_name, new_schema=new_schema)
329-
330+
# Make sure table doesn't exists already
331+
if(not drop_table and self.tableExists(schema_table_name):
332+
return('ERROR: Table already exists. Please drop it or set drop_table option to True')
333+
334+
self._dropTableIfExists(schema,table)
330335
sql = 'CREATE TABLE {schema}.{table}(\n'.format(schema=schema,table=table)
331336
for name,type in db_schema:
332337
sql += '{} {},\n'.format(name,type)

0 commit comments

Comments
 (0)