CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
34 KiB
34 KiB
In [1]:
import os
import shutil
def find_tblgen():
path = os.environ.get("LLVM_TBLGEN_EXECUTABLE")
if path is not None and os.path.isfile(path) and os.access(path, os.X_OK):
return path
else:
path = shutil.which("llvm-tblgen")
if path is None:
raise OSError("llvm-tblgen not found")
return path
_ = find_tblgen()In [2]:
import subprocess
import tempfile
import json
def run_tblgen(src):
# Passing to stdin requires a file like object.
with tempfile.TemporaryFile("w+") as f:
f.write(src)
f.seek(0)
got = subprocess.run(
[find_tblgen(), "--dump-json"],
stdin=f,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
)
if got.stderr:
raise RuntimeError("llvm-tblgen failed with stderr: " + got.stderr)
return json.loads(got.stdout)
print(json.dumps(run_tblgen("class Foo {}"), indent=4)){
"!instanceof": {
"Foo": []
},
"!tablegen_json_version": 1
}
In [3]:
query_tblgen = """\
def all;
def fields;
def none;
def eq;
def ne;
def gt;
def ge;
def and;
def or;
"""In [4]:
query_tblgen += """\
class Query <string table, dag query_fields = (all), dag condition = (none)> {
string TableName = table;
dag Fields = query_fields;
dag WhereClause = condition;
list<string> OrderedBy = [];
}
"""In [5]:
full_tblgen = query_tblgen + """\
def : Query<"Customer">;
def : Query<"Orders", (fields "Person", "Amount")>;
def : Query<"Customer", (fields "Affiliation"),
(eq "Name", "Mary Blackburn":$str)>;
def : Query<"Orders", (fields "ProductName"),
(gt "Amount", 8)>;
def : Query<"Orders", (fields "ProductName":$name, "Person"),
(and (gt "Amount", 8), (ne "Person", 1))> {
let OrderedBy = ["$name"];
}
"""In [6]:
full_json = run_tblgen(full_tblgen)
print(json.dumps(full_json, indent=4)){
"!instanceof": {
"Query": [
"anonymous_0",
"anonymous_1",
"anonymous_2",
"anonymous_3",
"anonymous_4"
]
},
"!tablegen_json_version": 1,
"all": {
"!anonymous": false,
"!fields": [],
"!name": "all",
"!superclasses": []
},
"and": {
"!anonymous": false,
"!fields": [],
"!name": "and",
"!superclasses": []
},
"anonymous_0": {
"!anonymous": true,
"!fields": [],
"!name": "anonymous_0",
"!superclasses": [
"Query"
],
"Fields": {
"args": [],
"kind": "dag",
"operator": {
"def": "all",
"kind": "def",
"printable": "all"
},
"printable": "(all)"
},
"OrderedBy": [],
"TableName": "Customer",
"WhereClause": {
"args": [],
"kind": "dag",
"operator": {
"def": "none",
"kind": "def",
"printable": "none"
},
"printable": "(none)"
}
},
"anonymous_1": {
"!anonymous": true,
"!fields": [],
"!name": "anonymous_1",
"!superclasses": [
"Query"
],
"Fields": {
"args": [
[
"Person",
null
],
[
"Amount",
null
]
],
"kind": "dag",
"operator": {
"def": "fields",
"kind": "def",
"printable": "fields"
},
"printable": "(fields \"Person\", \"Amount\")"
},
"OrderedBy": [],
"TableName": "Orders",
"WhereClause": {
"args": [],
"kind": "dag",
"operator": {
"def": "none",
"kind": "def",
"printable": "none"
},
"printable": "(none)"
}
},
"anonymous_2": {
"!anonymous": true,
"!fields": [],
"!name": "anonymous_2",
"!superclasses": [
"Query"
],
"Fields": {
"args": [
[
"Affiliation",
null
]
],
"kind": "dag",
"operator": {
"def": "fields",
"kind": "def",
"printable": "fields"
},
"printable": "(fields \"Affiliation\")"
},
"OrderedBy": [],
"TableName": "Customer",
"WhereClause": {
"args": [
[
"Name",
null
],
[
"Mary Blackburn",
"str"
]
],
"kind": "dag",
"operator": {
"def": "eq",
"kind": "def",
"printable": "eq"
},
"printable": "(eq \"Name\", \"Mary Blackburn\":$str)"
}
},
"anonymous_3": {
"!anonymous": true,
"!fields": [],
"!name": "anonymous_3",
"!superclasses": [
"Query"
],
"Fields": {
"args": [
[
"ProductName",
null
]
],
"kind": "dag",
"operator": {
"def": "fields",
"kind": "def",
"printable": "fields"
},
"printable": "(fields \"ProductName\")"
},
"OrderedBy": [],
"TableName": "Orders",
"WhereClause": {
"args": [
[
"Amount",
null
],
[
8,
null
]
],
"kind": "dag",
"operator": {
"def": "gt",
"kind": "def",
"printable": "gt"
},
"printable": "(gt \"Amount\", 8)"
}
},
"anonymous_4": {
"!anonymous": true,
"!fields": [],
"!name": "anonymous_4",
"!superclasses": [
"Query"
],
"Fields": {
"args": [
[
"ProductName",
"name"
],
[
"Person",
null
]
],
"kind": "dag",
"operator": {
"def": "fields",
"kind": "def",
"printable": "fields"
},
"printable": "(fields \"ProductName\":$name, \"Person\")"
},
"OrderedBy": [
"$name"
],
"TableName": "Orders",
"WhereClause": {
"args": [
[
{
"args": [
[
"Amount",
null
],
[
8,
null
]
],
"kind": "dag",
"operator": {
"def": "gt",
"kind": "def",
"printable": "gt"
},
"printable": "(gt \"Amount\", 8)"
},
null
],
[
{
"args": [
[
"Person",
null
],
[
1,
null
]
],
"kind": "dag",
"operator": {
"def": "ne",
"kind": "def",
"printable": "ne"
},
"printable": "(ne \"Person\", 1)"
},
null
]
],
"kind": "dag",
"operator": {
"def": "and",
"kind": "def",
"printable": "and"
},
"printable": "(and (gt \"Amount\", 8), (ne \"Person\", 1))"
}
},
"eq": {
"!anonymous": false,
"!fields": [],
"!name": "eq",
"!superclasses": []
},
"fields": {
"!anonymous": false,
"!fields": [],
"!name": "fields",
"!superclasses": []
},
"ge": {
"!anonymous": false,
"!fields": [],
"!name": "ge",
"!superclasses": []
},
"gt": {
"!anonymous": false,
"!fields": [],
"!name": "gt",
"!superclasses": []
},
"ne": {
"!anonymous": false,
"!fields": [],
"!name": "ne",
"!superclasses": []
},
"none": {
"!anonymous": false,
"!fields": [],
"!name": "none",
"!superclasses": []
},
"or": {
"!anonymous": false,
"!fields": [],
"!name": "or",
"!superclasses": []
}
}
In [7]:
print(full_json["!instanceof"]){'Query': ['anonymous_0', 'anonymous_1', 'anonymous_2', 'anonymous_3', 'anonymous_4']}
In [8]:
print(full_json["anonymous_0"]["!superclasses"])['Query']
In [9]:
print(full_json["anonymous_0"]["Fields"]){'args': [], 'kind': 'dag', 'operator': {'def': 'all', 'kind': 'def', 'printable': 'all'}, 'printable': '(all)'}
In [10]:
def find_all_queries(j):
queries = []
for key in j:
# ! means it is some metadata, not a def.
if not key.startswith("!"):
value = full_json[key]
# If we inherit from Query.
if "Query" in value["!superclasses"]:
queries.append(value)
return queries
queries = find_all_queries(full_json)
print([q["!name"] for q in queries])['anonymous_0', 'anonymous_1', 'anonymous_2', 'anonymous_3', 'anonymous_4']
In [11]:
def emit_operator(operator):
return {
'gt': ' > ',
'ge': ' >= ',
'lt': ' < ',
'le': ' <= ',
'ne': ' <> ',
'eq': ' = ',
'or': ' OR ',
'and': ' AND '
}[operator]
print(emit_operator('and'))AND
In [12]:
def emit_fields(args):
# Return a comma separated list of arg names.
return ", ".join([arg[0] for arg in args])
print(emit_fields([["Abc", None], ["Def", None]]))Abc, Def
In [13]:
from collections.abc import Mapping
def emit_where_clause(where_clause):
output = ""
num_args = len(where_clause["args"])
for idx, arg in enumerate(where_clause["args"]):
arg_name, arg_type = arg
if isinstance(arg_name, Mapping):
# This is a nested where clause.
output += emit_where_clause(arg_name)
else:
# This is some condition.
if arg_type == "str":
# String types must be emitted with "" around them.
output += '"' + arg_name + '"'
else:
output += str(arg_name)
# If this is not the last arg, emit the condition.
if idx != (num_args-1):
output += emit_operator(where_clause["operator"]["def"])
return output
print(emit_where_clause({
"args": [["Name",None],
["Mary Blackburn", "str"]],
"kind": "dag",
"operator": {
"def": "eq",
"kind": "def",
"printable": "eq"
}}))Name = "Mary Blackburn"
In [14]:
def emit_ordered_by(ordered_by, field_tag_map):
# No ORDER BY statement to emit.
if not ordered_by:
return ""
output = "\n ORDER BY "
num_ordered_by = len(ordered_by)
for idx, field_name in enumerate(ordered_by):
# If it is a tag
if field_name.startswith('$'):
# Find the corresponding field name
tag_name = field_name[1:]
field_name = field_tag_map.get(tag_name)
if field_name is None:
raise RuntimeError('Unrecognized tag "{}"'.format(
tag_name))
# Separate each tag after the first with ", ".
if idx != 0:
output += ", "
output += field_name
return output
print(emit_ordered_by(["$abc", "$def"], {'abc':"ABC", 'def':"DEF"}))ORDER BY ABC, DEF
In [15]:
def build_tag_map(arguments):
# Args are [Name, Tag]. Reverse this so we have [Tag, Name].
# Add each one to a dictionary where Tag is the key and Name is the value.
return dict([reversed(a) for a in arguments])
print(build_tag_map([["ABC", "abc"], ["DEF", "def"]])){'abc': 'ABC', 'def': 'DEF'}
In [16]:
def emit_query(q):
fields_init = q["Fields"]
field_op_name = fields_init["operator"]["def"]
if not field_op_name in ["all", "fields"]:
raise RuntimeError("Invalid dag operator " + field_op_name)
field_tag_map = build_tag_map(fields_init["args"])
where_clause = q["WhereClause"]
has_where = where_clause["operator"]["def"] != "none"
ret = "SELECT "
if field_op_name == "all":
ret += "*"
ret += emit_fields(fields_init["args"])
ret += " FROM " + q["TableName"]
if has_where:
ret += "\n WHERE " + emit_where_clause(where_clause)
ret += emit_ordered_by(q["OrderedBy"], field_tag_map)
ret += ";"
return retIn [17]:
for q in queries:
print(emit_query(q) + "\n")SELECT * FROM Customer; SELECT Person, Amount FROM Orders; SELECT Affiliation FROM Customer WHERE Name = "Mary Blackburn"; SELECT ProductName FROM Orders WHERE Amount > 8; SELECT ProductName, Person FROM Orders WHERE Amount > 8 AND Person <> 1 ORDER BY ProductName;