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

Commit

Permalink
Fix createTable function (#21)
Browse files Browse the repository at this point in the history
* Fix createTable function

missing dropTableIfExists

* Update context.py

optinal drop table or error if exists
  • Loading branch information
Ben-Epstein authored Sep 11, 2019
1 parent 64995ff commit 4711379
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions splicemachine/spark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,16 @@ def _dropTableIfExists(self, schema, table):
print('Creating table {schema}.{table}'.format(schema=schema,table=table))
self.execute('DROP TABLE IF EXISTS {schema}.{table}'.format(schema=schema,table=table))

def createTable(self, dataframe, schema_table_name, new_schema=True, types = None):
def createTable(self, dataframe, schema_table_name, new_schema=True, drop_table=False, types = None):
'''
Creates a schema.table from a dataframe
:param schema_table_name: String full table name in the format "schema.table_name"
If only a table name is provided (ie no '.' in the string) schema SPLICE will be assumed
If this table exists in the database already, it will be DROPPED and a new one will be created
:param dataframe: The dataframe that the table will be created for
: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]
: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
:param drop_table: An optinal boolean to drop the table if it exists. [DEFAULT False]
: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
If None or if any types are missing, types will be assumed automatically from the dataframe schema as follows:
BooleanType: BOOLEAN
ByteType: TINYINT
Expand All @@ -326,7 +327,11 @@ def createTable(self, dataframe, schema_table_name, new_schema=True, types = Non
'''
db_schema = self._generateDBSchema(dataframe, types=types)
schema, table = self._getCreateTableSchema(schema_table_name, new_schema=new_schema)

# Make sure table doesn't exists already
if(not drop_table and self.tableExists(schema_table_name):
return('ERROR: Table already exists. Please drop it or set drop_table option to True')

self._dropTableIfExists(schema,table)
sql = 'CREATE TABLE {schema}.{table}(\n'.format(schema=schema,table=table)
for name,type in db_schema:
sql += '{} {},\n'.format(name,type)
Expand Down

0 comments on commit 4711379

Please sign in to comment.