Beispiel #1
0
 def test_template_query_templates_correctly_with_file(self):
     query = "{{ main }}"
     templated_query = template_query(
         query,
         connection_name="valid",
     )
     assert templated_query == "\n5"
Beispiel #2
0
    def _compute_sql_result(self, sql_query, database):
        connected_query = template_query(sql_query, connection_name=database)
        result = list(self.execute(connected_query, is_dict_return_enabled=False))

        try:
            return result[0][0]
        except Exception as e:
            LOGGER.warning("Running {sql_query} led to {e}.")
            return None
Beispiel #3
0
def run(sql, warehouse_name=None):
    """
    Runs sql queries against warehouse_name defined in ~/.whale/config/connections.yaml.
    If no warehouse_name is given, the first is used.
    """
    connection_dict = get_connection(warehouse_name=warehouse_name)
    connection = ConnectionConfigSchema(**connection_dict)
    engine, conf = configure_unscoped_sqlalchemy_engine(connection)
    engine.init(conf)
    sql = template_query(sql, connection_name=connection.name)
    LOGGER.info(f"Templated query:\n{sql}")

    result = engine.execute(sql, has_header=True)
    headers = next(result)
    table = list(result)
    df = pd.DataFrame(table, columns=headers)
    return df
Beispiel #4
0
def run(sql, warehouse_name=None, extra_macros: str = "") -> pd.DataFrame:
    """
    Runs sql queries against warehouse_name defined in
    ~/.whale/config/connections.yaml.  If no warehouse_name is given, the first
    is used.

    """
    connection = get_connection(warehouse_name=warehouse_name)
    engine, conf = configure_unscoped_sqlalchemy_engine(connection)
    engine.init(conf)
    sql = template_query(sql,
                         connection_name=connection.name,
                         extra_macros=extra_macros)
    LOGGER.info(f"Templated query:\n{sql}")
    sql = sql.replace("%", "%%")

    result = engine.execute(sql, has_header=True)
    headers = next(result)
    table = list(result)
    df = pd.DataFrame(table, columns=headers)
    return df
Beispiel #5
0
 def test_template_query_templates_empty_correctly(self):
     templated_query = template_query(VALID_TEMPLATE)
     assert templated_query == ""
Beispiel #6
0
 def test_template_query_is_no_op_without_jinja(self):
     templated_query = template_query(QUERY_WITHOUT_TEMPLATE)
     assert templated_query == QUERY_WITHOUT_TEMPLATE