title: Pre-define Redshift table with Keys [Glue]
body: <p>I want to define manually the Redshift table before of my first write. This is because I want to leverage of <code>distkey</code> and <code>sortkey</code> on defined columns. The SQL query is going to be something like that:</p>
  
  <pre><code>my_sql_command = """
      create table if not exists my_db.my_schema.my_table(
          my_id                     VARCHAR(MAX) NOT NULL DISTKEY,
          type                      VARCHAR(MAX),
          my_timestamp  TIMESTAMP,
      )
      compound sortkey(my_timestamp, my_id);
      """
  </code></pre>
  
  <p>I'm calling this SQL string as a <code>preactions</code> parameter (mentioned <a href="https://aws.amazon.com/premiumsupport/knowledge-center/sql-commands-redshift-glue-job/" rel="nofollow noreferrer">here</a>, could't find any better documentation unfortunately) like this:</p>
  
  <pre><code>my_frame = DynamicFrame.fromDF(my_df, glue_context, "my_frame")
  
  glue_context.write_dynamic_frame.from_jdbc_conf(
          frame=my_frame, catalog_connection=params['db_connection_name'],
          connection_options={"preactions": my_sql_command, "dbtable": "my_schema.my_table", "database": "my_db"},
          redshift_tmp_dir="s3://my_bucket/", transformation_ctx="my_ctx")
  </code></pre>
  
  <p>But I am getting this error message:</p>
  
  <pre><code>py4j.protocol.Py4JJavaError: An error occurred while calling o227.pyWriteDynamicFrame.
  : java.sql.SQLException: [JDBC Driver]String index out of range: 0
  at java.lang.String.charAt(String.java:658)
  </code></pre>
  
  <p>which I really don't know how to interpret.</p>
  
  <p>What's causing this exception?</p>