2025-02-13 06:15:54 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
FILE_DIR = os.path.dirname(__file__)
|
|
|
|
FIXTURES_DIR = os.path.join(FILE_DIR, "fixtures")
|
|
|
|
|
|
|
|
|
|
|
|
def _filter_comments(s):
|
2025-02-13 08:04:41 +01:00
|
|
|
return "\n".join([line for line in s.splitlines() if line and not line.startswith("--")])
|
2025-02-13 06:15:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _extract_meta(sql):
|
|
|
|
meta = {}
|
|
|
|
sql_lines = sql.split("\n")
|
|
|
|
i = 0
|
|
|
|
while sql_lines[i].startswith("#"):
|
|
|
|
key, val = sql_lines[i].split(":", maxsplit=1)
|
|
|
|
meta[key.lstrip("#").strip()] = val.strip()
|
|
|
|
i += 1
|
|
|
|
sql = "\n".join(sql_lines[i:])
|
|
|
|
return sql, meta
|
|
|
|
|
|
|
|
|
|
|
|
def assert_logger_contains(message, logger, level="error"):
|
2025-02-13 08:04:41 +01:00
|
|
|
output = "\n".join(str(args[0][0]) for args in getattr(logger, level).call_args_list)
|
2025-02-13 06:15:54 +01:00
|
|
|
assert message in output
|
|
|
|
|
|
|
|
|
|
|
|
def load_sql_fixtures(filename):
|
|
|
|
with open(os.path.join(FIXTURES_DIR, filename), encoding="utf-8") as f:
|
|
|
|
for sql in _filter_comments(f.read()).splitlines():
|
|
|
|
yield sql
|
|
|
|
|
|
|
|
|
|
|
|
def load_sql_fixture_pairs(filename):
|
|
|
|
with open(os.path.join(FIXTURES_DIR, filename), encoding="utf-8") as f:
|
|
|
|
statements = _filter_comments(f.read()).split(";")
|
|
|
|
|
|
|
|
size = len(statements)
|
|
|
|
|
|
|
|
for i in range(0, size, 2):
|
|
|
|
if i + 1 < size:
|
|
|
|
sql = statements[i].strip()
|
|
|
|
sql, meta = _extract_meta(sql)
|
|
|
|
expected = statements[i + 1].strip()
|
|
|
|
yield meta, sql, expected
|
|
|
|
|
|
|
|
|
2025-02-13 14:45:11 +01:00
|
|
|
def string_to_bool(string):
|
|
|
|
if string is None:
|
|
|
|
return False
|
|
|
|
if string in (True, False):
|
|
|
|
return string
|
|
|
|
return string and string.lower() in ("true", "1")
|
|
|
|
|
|
|
|
|
2025-02-13 14:48:46 +01:00
|
|
|
SKIP_INTEGRATION = string_to_bool(os.environ.get("SKIP_INTEGRATION", "0").lower())
|
|
|
|
|
2025-02-13 06:15:54 +01:00
|
|
|
TPCH_SCHEMA = {
|
|
|
|
"lineitem": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"l_orderkey": "bigint",
|
|
|
|
"l_partkey": "bigint",
|
|
|
|
"l_suppkey": "bigint",
|
|
|
|
"l_linenumber": "bigint",
|
|
|
|
"l_quantity": "double",
|
|
|
|
"l_extendedprice": "double",
|
|
|
|
"l_discount": "double",
|
|
|
|
"l_tax": "double",
|
2025-02-13 06:15:54 +01:00
|
|
|
"l_returnflag": "string",
|
|
|
|
"l_linestatus": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"l_shipdate": "string",
|
|
|
|
"l_commitdate": "string",
|
|
|
|
"l_receiptdate": "string",
|
2025-02-13 06:15:54 +01:00
|
|
|
"l_shipinstruct": "string",
|
|
|
|
"l_shipmode": "string",
|
|
|
|
"l_comment": "string",
|
|
|
|
},
|
|
|
|
"orders": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"o_orderkey": "bigint",
|
|
|
|
"o_custkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"o_orderstatus": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"o_totalprice": "double",
|
|
|
|
"o_orderdate": "string",
|
2025-02-13 06:15:54 +01:00
|
|
|
"o_orderpriority": "string",
|
|
|
|
"o_clerk": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"o_shippriority": "int",
|
2025-02-13 06:15:54 +01:00
|
|
|
"o_comment": "string",
|
|
|
|
},
|
|
|
|
"customer": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"c_custkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"c_name": "string",
|
|
|
|
"c_address": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"c_nationkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"c_phone": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"c_acctbal": "double",
|
2025-02-13 06:15:54 +01:00
|
|
|
"c_mktsegment": "string",
|
|
|
|
"c_comment": "string",
|
|
|
|
},
|
|
|
|
"part": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"p_partkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"p_name": "string",
|
|
|
|
"p_mfgr": "string",
|
|
|
|
"p_brand": "string",
|
|
|
|
"p_type": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"p_size": "int",
|
2025-02-13 06:15:54 +01:00
|
|
|
"p_container": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"p_retailprice": "double",
|
2025-02-13 06:15:54 +01:00
|
|
|
"p_comment": "string",
|
|
|
|
},
|
|
|
|
"supplier": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"s_suppkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"s_name": "string",
|
|
|
|
"s_address": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"s_nationkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"s_phone": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"s_acctbal": "double",
|
2025-02-13 06:15:54 +01:00
|
|
|
"s_comment": "string",
|
|
|
|
},
|
|
|
|
"partsupp": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"ps_partkey": "bigint",
|
|
|
|
"ps_suppkey": "bigint",
|
|
|
|
"ps_availqty": "int",
|
|
|
|
"ps_supplycost": "double",
|
2025-02-13 06:15:54 +01:00
|
|
|
"ps_comment": "string",
|
|
|
|
},
|
|
|
|
"nation": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"n_nationkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"n_name": "string",
|
2025-02-13 14:54:32 +01:00
|
|
|
"n_regionkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"n_comment": "string",
|
|
|
|
},
|
|
|
|
"region": {
|
2025-02-13 14:54:32 +01:00
|
|
|
"r_regionkey": "bigint",
|
2025-02-13 06:15:54 +01:00
|
|
|
"r_name": "string",
|
|
|
|
"r_comment": "string",
|
|
|
|
},
|
|
|
|
}
|