pick.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. import sys
  10. import pg8000
  11. import sqlparse
  12. from materialize.query_fitness.all_parts_essential import AllPartsEssential
  13. threshold = 0.5
  14. def main() -> None:
  15. conn = pg8000.connect(database="pstoev", password="pstoev")
  16. fitness_func = AllPartsEssential(conn=conn)
  17. for query in sys.stdin:
  18. fitness = fitness_func.fitness(query)
  19. if fitness > 0.5:
  20. dump_slt(conn, query)
  21. def dump_slt(conn: pg8000.Connection, query: str) -> None:
  22. query = sqlparse.format(query.rstrip(), reindent=True, keyword_case="upper")
  23. cursor = conn.cursor()
  24. cursor.execute("ROLLBACK")
  25. cursor.execute(query)
  26. row = cursor.fetchone()
  27. assert row is not None
  28. cols = len(row)
  29. colspec = "I" * cols
  30. print(
  31. f"""
  32. query {colspec} rowsort
  33. {query}
  34. ----
  35. 9999999999 values hashing to YY
  36. query T multiline
  37. EXPLAIN {query}
  38. ----
  39. EOF"""
  40. )
  41. if __name__ == "__main__":
  42. main()