Exemple #1
0
def create_file_format(compression: str, compression_map: Dict[str, str],
                       cursor: SnowflakeCursor) -> str:
    file_format_name = (
        '"' + "".join(random.choice(string.ascii_lowercase)
                      for _ in range(5)) + '"')
    file_format_sql = (
        f"CREATE FILE FORMAT {file_format_name} "
        f"/* Python:snowflake.connector.pandas_tools.write_pandas() */ "
        f"TYPE=PARQUET COMPRESSION={compression_map[compression]}")
    logger.debug(f"creating file format with '{file_format_sql}'")
    cursor.execute(file_format_sql, _is_internal=True)
    return file_format_name
def put(
    csr: SnowflakeCursor,
    file_path: str,
    stage_path: str,
    from_path: bool,
    sql_options: str | None = "",
    **kwargs,
) -> SnowflakeCursor:
    """Execute PUT <file> <stage> <options> query with given cursor.

    Args:
        csr: Snowflake cursor object.
        file_path: Path to the target file in local system; Or <filename>.<extension> when from_path is False.
        stage_path: Destination path of file on the stage.
        from_path: Whether the target file is fetched with given path, specify file_stream=<IO> if False.
        sql_options: Optional arguments to the PUT command.
        **kwargs: Optional arguments passed to SnowflakeCursor.execute()

    Returns:
        A result class with the results in it. This can either be json, or an arrow result class.
    """
    sql = "put 'file://{file}' @{stage} {sql_options}"
    if from_path:
        kwargs.pop("file_stream", None)
    else:
        # PUT from stream
        file_path = os.path.basename(file_path)
    if kwargs.pop("commented", False):
        sql = "--- test comments\n" + sql
    sql = sql.format(
        file=file_path.replace("\\", "\\\\"), stage=stage_path, sql_options=sql_options
    )
    return csr.execute(sql, **kwargs)
Exemple #3
0
def create_temporary_sfc_stage(cursor: SnowflakeCursor) -> str:
    stage_name = "".join(
        random.choice(string.ascii_lowercase) for _ in range(5))
    create_stage_sql = (
        "create temporary stage /* Python:snowflake.connector.pandas_tools.write_pandas() */ "
        '"{stage_name}"').format(stage_name=stage_name)
    logger.debug(f"creating stage with '{create_stage_sql}'")
    result_cursor = cursor.execute(create_stage_sql, _is_internal=True)
    if result_cursor is None:
        raise SnowflakeQueryUnknownError(create_stage_sql)
    result_cursor.fetchall()
    return stage_name