prograph 1.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
"""The string functions (SPARQL 1.1 17.4.3) and REGEX, over the plan's cells (m2-functions.md 3).
|
|
2
|
+
|
|
3
|
+
STRSTARTS, STRENDS, CONTAINS, REGEX (booleans), STRLEN (an integer), UCASE, LCASE, SUBSTR,
|
|
4
|
+
STRBEFORE and STRAFTER (the first argument's kind, its tag kept).
|
|
5
|
+
|
|
6
|
+
An argument is a simple literal, an xsd:string or a language-tagged literal; anything else is an
|
|
7
|
+
error (17.4.3.1). Two arguments are compatible (17.4.3.1.2) when both are simple, both tagged with
|
|
8
|
+
one tag, or the first tagged and the second simple. Where the slots fix an argument's kind,
|
|
9
|
+
datatype and tag the question is decided here, at planning (an error is then the expression's
|
|
10
|
+
unbound value); where rows differ it is a condition over the cells' parts, and a row that fails
|
|
11
|
+
it has NULL, which is unbound in a BIND and false in a FILTER (17.2). Characters are code points
|
|
12
|
+
(F&O 5.1); the comparisons are text comparisons, never LIKE.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from decimal import ROUND_FLOOR, Decimal
|
|
19
|
+
from typing import Dict, List, Optional, Sequence
|
|
20
|
+
|
|
21
|
+
from prograph.plan.ir import expr as E
|
|
22
|
+
from prograph.plan.ir.facts import NotBuilt, expr_slot
|
|
23
|
+
from prograph.plan.ir.slot import (
|
|
24
|
+
BOOLEAN, NOTHING, ROW, UNKNOWN, Bound, Slot, literal_slot,
|
|
25
|
+
)
|
|
26
|
+
from prograph.plan.ir.terms import LiteralTerm
|
|
27
|
+
from prograph.plan.ir.xpath_regex import InvalidPattern, translate
|
|
28
|
+
from prograph.plan.sql import rel_algebra as R
|
|
29
|
+
from prograph.rules import home, module_home
|
|
30
|
+
from prograph.schema import xsd
|
|
31
|
+
from prograph.schema.source_map import LITERAL
|
|
32
|
+
|
|
33
|
+
module_home("string-functions", "SPARQL's string functions and their arguments",
|
|
34
|
+
terms=("STRSTARTS", "STRENDS", "CONTAINS", "SUBSTR", "STRLEN", "UCASE", "LCASE",
|
|
35
|
+
"STRBEFORE", "STRAFTER", "REGEX", "compatible arguments", "code points"))
|
|
36
|
+
|
|
37
|
+
BOOLEANS = frozenset({"STRSTARTS", "STRENDS", "CONTAINS", "REGEX", "LANGMATCHES"})
|
|
38
|
+
SPLITS = frozenset({"STRBEFORE", "STRAFTER"})
|
|
39
|
+
HASHES = frozenset({"MD5", "SHA1", "SHA256", "SHA384", "SHA512"})
|
|
40
|
+
STRING_CALLS = BOOLEANS | SPLITS | HASHES | {"STRLEN", "UCASE", "LCASE", "SUBSTR", "CONCAT",
|
|
41
|
+
"ENCODE_FOR_URI", "REPLACE"}
|
|
42
|
+
_PAIRS = frozenset({"STRSTARTS", "STRENDS", "CONTAINS", "STRBEFORE", "STRAFTER"})
|
|
43
|
+
STRINGS = frozenset({xsd.STRING, xsd.RDF_LANGSTRING})
|
|
44
|
+
_NUMBERS = frozenset(xsd.INTEGER_DATATYPES | {xsd.INTEGER, xsd.DECIMAL, xsd.FLOAT, xsd.DOUBLE})
|
|
45
|
+
_INTEGERS = frozenset(xsd.INTEGER_DATATYPES | {xsd.INTEGER})
|
|
46
|
+
|
|
47
|
+
YES, NO, MAYBE, UNBOUND = "yes", "no", "maybe", "unbound"
|
|
48
|
+
NONE, TAG, ROWS = "none", "tag", "row"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# -- what the slots fix -------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
def status(slot: Slot) -> str:
|
|
54
|
+
"""Whether every present cell of `slot` is a string argument (YES), none
|
|
55
|
+
is (NO), rows differ (MAYBE), or no row has a cell (UNBOUND)."""
|
|
56
|
+
if slot.present is Bound.NEVER:
|
|
57
|
+
return UNBOUND
|
|
58
|
+
if not slot.terms:
|
|
59
|
+
raise NotBuilt(f"a string function over a {'/'.join(sorted(slot.kinds))} value")
|
|
60
|
+
if UNKNOWN in slot.datatypes:
|
|
61
|
+
raise NotBuilt(f"a string function over a column with no recorded type ({slot.reason})")
|
|
62
|
+
if LITERAL not in slot.kinds or not slot.datatypes & STRINGS:
|
|
63
|
+
return NO
|
|
64
|
+
return YES if slot.kinds == {LITERAL} and slot.datatypes <= STRINGS else MAYBE
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def tag_kind(slot: Slot) -> str:
|
|
68
|
+
"""The tags of `slot`'s string cells: none, one tag, or rows decide."""
|
|
69
|
+
strs = slot.datatypes & STRINGS
|
|
70
|
+
if xsd.RDF_LANGSTRING not in strs:
|
|
71
|
+
return NONE
|
|
72
|
+
return TAG if strs == {xsd.RDF_LANGSTRING} and slot.language not in (None, ROW) else ROWS
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def compatible_statically(a: Slot, b: Slot) -> Optional[bool]:
|
|
76
|
+
"""Whether two string arguments are compatible (17.4.3.1.2) in every
|
|
77
|
+
row; None where rows decide."""
|
|
78
|
+
ka, kb = tag_kind(a), tag_kind(b)
|
|
79
|
+
if kb == NONE:
|
|
80
|
+
return True
|
|
81
|
+
if kb == TAG:
|
|
82
|
+
if ka == NONE:
|
|
83
|
+
return False
|
|
84
|
+
if ka == TAG:
|
|
85
|
+
return a.language.lower() == b.language.lower()
|
|
86
|
+
return None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass(frozen=True)
|
|
90
|
+
class Plan:
|
|
91
|
+
"""What the slots decide for a call: `error` if every row is an error,
|
|
92
|
+
`firm` if no row is, and the slot of the value."""
|
|
93
|
+
slot: Slot
|
|
94
|
+
error: bool
|
|
95
|
+
firm: bool
|
|
96
|
+
pattern: Optional[object] = None # the translated pattern of a REGEX
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def regex_flags(e: E.Call) -> str:
|
|
100
|
+
"""REGEX's flags: a constant string (`i`, `s` or both are built)."""
|
|
101
|
+
if len(e.args) < 3:
|
|
102
|
+
return ""
|
|
103
|
+
flags = e.args[2]
|
|
104
|
+
if isinstance(flags, E.Const) and isinstance(flags.value, LiteralTerm) \
|
|
105
|
+
and flags.value.datatype == xsd.STRING and flags.value.language is None:
|
|
106
|
+
return flags.value.lexical
|
|
107
|
+
raise NotBuilt("REGEX with flags that are not a constant string")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _regex_pattern(e: E.Call):
|
|
111
|
+
"""The translated pattern of a REGEX, None where the pattern is an error
|
|
112
|
+
(17.4.3.14: no XPath regex, or not a string); `NotBuilt` where it is
|
|
113
|
+
not a constant or outside the subset."""
|
|
114
|
+
pattern = e.args[1]
|
|
115
|
+
if not (isinstance(pattern, E.Const) and isinstance(pattern.value, LiteralTerm)):
|
|
116
|
+
raise NotBuilt("REGEX with a pattern that is not a constant (the engines' regexes differ "
|
|
117
|
+
"from XPath's, so each pattern is rewritten at planning)")
|
|
118
|
+
term = pattern.value
|
|
119
|
+
flags = regex_flags(e)
|
|
120
|
+
if term.datatype != xsd.STRING or term.language is not None:
|
|
121
|
+
return None
|
|
122
|
+
try:
|
|
123
|
+
return translate(term.lexical, flags)
|
|
124
|
+
except InvalidPattern:
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def plan(e: E.Call, slots: Sequence[Slot]) -> Plan:
|
|
129
|
+
"""What `e` is over arguments of `slots`."""
|
|
130
|
+
name = e.name
|
|
131
|
+
count = {"STRLEN": 1, "UCASE": 1, "LCASE": 1}.get(name)
|
|
132
|
+
if count is not None and len(slots) != count or name in _PAIRS and len(slots) != 2:
|
|
133
|
+
raise NotBuilt(f"{name} with {len(slots)} arguments")
|
|
134
|
+
if name in ("SUBSTR", "REGEX") and len(slots) not in (2, 3):
|
|
135
|
+
raise NotBuilt(f"{name} with {len(slots)} arguments")
|
|
136
|
+
if name == "CONCAT":
|
|
137
|
+
return _concat_plan(slots)
|
|
138
|
+
if name in HASHES | {"ENCODE_FOR_URI", "LANGMATCHES", "REPLACE"}:
|
|
139
|
+
return _other_plan(e, slots)
|
|
140
|
+
strings = slots[:2] if name in _PAIRS else slots[:1]
|
|
141
|
+
pattern = _regex_pattern(e) if name == "REGEX" else None
|
|
142
|
+
states = [status(s) for s in strings]
|
|
143
|
+
numbers = _number_states(slots[1:]) if name == "SUBSTR" else []
|
|
144
|
+
error = any(s in (NO, UNBOUND) for s in states) or NO in numbers or UNBOUND in numbers \
|
|
145
|
+
or (name == "REGEX" and pattern is None)
|
|
146
|
+
if not error and name in _PAIRS and compatible_statically(*strings) is False:
|
|
147
|
+
error = True
|
|
148
|
+
firm = (not error and all(s == YES for s in states) and not
|
|
149
|
+
any(s.present is not Bound.ALWAYS for s in slots[:len(strings) + len(numbers)])
|
|
150
|
+
and all(n == YES for n in numbers)
|
|
151
|
+
and (name not in _PAIRS or compatible_statically(*strings) is True))
|
|
152
|
+
bound = Bound.ALWAYS if firm else Bound.MAYBE
|
|
153
|
+
if name in BOOLEANS:
|
|
154
|
+
return Plan(BOOLEAN, error, firm, pattern)
|
|
155
|
+
if name == "STRLEN":
|
|
156
|
+
return Plan(literal_slot(xsd.INTEGER, bound, native=True), error, firm)
|
|
157
|
+
if error:
|
|
158
|
+
return Plan(NOTHING, True, False)
|
|
159
|
+
first = strings[0]
|
|
160
|
+
dts = first.datatypes & STRINGS
|
|
161
|
+
if name in SPLITS and xsd.RDF_LANGSTRING in dts:
|
|
162
|
+
dts = dts | {xsd.STRING} # no match gives a simple literal
|
|
163
|
+
lang = first.language if xsd.RDF_LANGSTRING in dts else None
|
|
164
|
+
slot = Slot(kinds=frozenset({LITERAL}), datatypes=frozenset(dts), language=lang,
|
|
165
|
+
bound=bound, native=xsd.STRING if dts == {xsd.STRING} else None)
|
|
166
|
+
return Plan(slot, False, firm)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _concat_plan(slots: Sequence[Slot]) -> Plan:
|
|
170
|
+
"""CONCAT (17.4.3.12, errata-query-4): every argument a string literal;
|
|
171
|
+
the result tagged where every argument has one same tag, else a simple
|
|
172
|
+
literal; an argument whose tag rows decide is not built."""
|
|
173
|
+
states = [status(s) for s in slots]
|
|
174
|
+
if any(st in (NO, UNBOUND) for st in states):
|
|
175
|
+
return Plan(NOTHING, True, False)
|
|
176
|
+
kinds = [tag_kind(s) for s in slots]
|
|
177
|
+
firm = all(st == YES for st in states) and all(s.present is Bound.ALWAYS for s in slots)
|
|
178
|
+
bound = Bound.ALWAYS if firm else Bound.MAYBE
|
|
179
|
+
if ROWS in kinds and NONE not in kinds:
|
|
180
|
+
# Tags rows decide: the result is tagged in a row where every argument has the same tag
|
|
181
|
+
# (`_concat_tag`), else a simple literal.
|
|
182
|
+
return Plan(Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({xsd.STRING, xsd.RDF_LANGSTRING}),
|
|
183
|
+
language=ROW, bound=bound), False, firm)
|
|
184
|
+
tags = {s.language.lower() for s, k in zip(slots, kinds) if k == TAG}
|
|
185
|
+
if all(k == TAG for k in kinds) and len(tags) == 1:
|
|
186
|
+
return Plan(literal_slot(xsd.RDF_LANGSTRING, bound, language=slots[0].language), False, firm)
|
|
187
|
+
return Plan(literal_slot(xsd.STRING, bound, native=True), False, firm)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _simple(slot: Slot) -> str:
|
|
191
|
+
"""Whether every present cell of `slot` is a simple literal or an xsd:string (YES), none is (NO),
|
|
192
|
+
rows differ (MAYBE), or no row has a cell (UNBOUND)."""
|
|
193
|
+
st = status(slot)
|
|
194
|
+
if st in (NO, UNBOUND):
|
|
195
|
+
return st
|
|
196
|
+
if xsd.STRING not in slot.datatypes:
|
|
197
|
+
return NO
|
|
198
|
+
return YES if st == YES and slot.datatypes == {xsd.STRING} else MAYBE
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _dialect_has(template: str) -> bool:
|
|
202
|
+
"""Whether the dialect being written has `template` (a function some engines lack)."""
|
|
203
|
+
from prograph.plan.ir.lower import DIALECT
|
|
204
|
+
from prograph.plan.sql.dialect import get_dialect
|
|
205
|
+
name = DIALECT.get()
|
|
206
|
+
return name is None or get_dialect(name).function_map.get(template) is not None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _other_plan(e: E.Call, slots: Sequence[Slot]) -> Plan:
|
|
210
|
+
"""ENCODE_FOR_URI (17.4.3.11) and REPLACE (17.4.3.15) of a string literal, a hash (17.4.6) of a
|
|
211
|
+
simple literal or xsd:string, langMatches (17.4.3.13) of two simple literals."""
|
|
212
|
+
name = e.name
|
|
213
|
+
want = {"LANGMATCHES": 2, "ENCODE_FOR_URI": 1}.get(name, 1)
|
|
214
|
+
if name == "REPLACE":
|
|
215
|
+
if len(slots) not in (3, 4):
|
|
216
|
+
raise NotBuilt(f"REPLACE with {len(slots)} arguments")
|
|
217
|
+
pieces = replace_pieces(e)
|
|
218
|
+
if pieces is None:
|
|
219
|
+
return Plan(NOTHING, True, False)
|
|
220
|
+
states = [status(slots[0])]
|
|
221
|
+
else:
|
|
222
|
+
if len(slots) != want:
|
|
223
|
+
raise NotBuilt(f"{name} with {len(slots)} arguments")
|
|
224
|
+
if name in HASHES and not _dialect_has(name):
|
|
225
|
+
raise NotBuilt(f"{name} on this engine (no such function)")
|
|
226
|
+
pieces = None
|
|
227
|
+
states = [_simple(s) for s in slots] if name in HASHES | {"LANGMATCHES"} else [status(slots[0])]
|
|
228
|
+
error = any(st in (NO, UNBOUND) for st in states)
|
|
229
|
+
used = slots[:len(states)]
|
|
230
|
+
firm = not error and all(st == YES for st in states) and all(s.present is Bound.ALWAYS for s in used)
|
|
231
|
+
bound = Bound.ALWAYS if firm else Bound.MAYBE
|
|
232
|
+
if name == "LANGMATCHES":
|
|
233
|
+
return Plan(BOOLEAN, error, firm)
|
|
234
|
+
if error:
|
|
235
|
+
return Plan(NOTHING, True, False)
|
|
236
|
+
if name == "REPLACE":
|
|
237
|
+
first = slots[0]
|
|
238
|
+
dts = first.datatypes & STRINGS
|
|
239
|
+
lang = first.language if xsd.RDF_LANGSTRING in dts else None
|
|
240
|
+
return Plan(Slot(kinds=frozenset({LITERAL}), datatypes=frozenset(dts), language=lang, bound=bound,
|
|
241
|
+
native=xsd.STRING if dts == {xsd.STRING} else None), False, firm, pieces)
|
|
242
|
+
return Plan(literal_slot(xsd.STRING, bound, native=True), False, firm)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@home("xpath-replace", "REPLACE's pattern and replacement for each engine",
|
|
246
|
+
terms=("REPLACE", "fn:replace", "$1", "replacement string", "FORX0003", "FORX0004"))
|
|
247
|
+
def replace_pieces(e: E.Call):
|
|
248
|
+
r"""REPLACE's pattern (translated as REGEX's, `translate`) and replacement for each engine, as
|
|
249
|
+
(Translated, java, re2, pg); None where the call is an error (17.4.3.15, F&O 7.6.3): a pattern or
|
|
250
|
+
replacement that is no simple literal, a pattern that is no XPath regex or matches the empty
|
|
251
|
+
string (FORX0003), a replacement with `\` or `$` not escaping `\`, `$` or naming a group
|
|
252
|
+
(FORX0004). `NotBuilt` where an argument is not a constant, or a group past 9 (RE2, PostgreSQL)."""
|
|
253
|
+
import re as _re
|
|
254
|
+
consts = []
|
|
255
|
+
for x in e.args[1:]:
|
|
256
|
+
if not (isinstance(x, E.Const) and isinstance(x.value, LiteralTerm)):
|
|
257
|
+
raise NotBuilt("REPLACE with a pattern, replacement or flags that is not a constant")
|
|
258
|
+
if x.value.datatype != xsd.STRING or x.value.language is not None:
|
|
259
|
+
return None
|
|
260
|
+
consts.append(x.value.lexical)
|
|
261
|
+
pattern, replacement = consts[0], consts[1]
|
|
262
|
+
flags = consts[2] if len(consts) > 2 else ""
|
|
263
|
+
try:
|
|
264
|
+
t = translate(pattern, flags)
|
|
265
|
+
except InvalidPattern:
|
|
266
|
+
return None
|
|
267
|
+
py = t.java.replace("\\z", "\\Z").replace("(?iu)", "(?i)")
|
|
268
|
+
try:
|
|
269
|
+
groups = _re.compile(py).groups
|
|
270
|
+
if _re.search(py, "") is not None:
|
|
271
|
+
return None # FORX0003: the pattern matches the empty string
|
|
272
|
+
except _re.error:
|
|
273
|
+
raise NotBuilt(f"REPLACE with the pattern {pattern!r}")
|
|
274
|
+
tokens, i = [], 0
|
|
275
|
+
while i < len(replacement):
|
|
276
|
+
ch = replacement[i]
|
|
277
|
+
if ch == "\\":
|
|
278
|
+
if i + 1 < len(replacement) and replacement[i + 1] in "\\$":
|
|
279
|
+
tokens.append(replacement[i + 1]); i += 2; continue
|
|
280
|
+
return None
|
|
281
|
+
if ch == "$":
|
|
282
|
+
j = i + 1
|
|
283
|
+
while j < len(replacement) and replacement[j].isdigit() and int(replacement[i + 1:j + 1]) <= groups:
|
|
284
|
+
j += 1
|
|
285
|
+
if j == i + 1:
|
|
286
|
+
if j < len(replacement) and replacement[j].isdigit():
|
|
287
|
+
tokens.append(("empty",)); i = j + 1; continue # a group the pattern lacks: ""
|
|
288
|
+
return None
|
|
289
|
+
tokens.append(("group", int(replacement[i + 1:j]))); i = j; continue
|
|
290
|
+
tokens.append(ch); i += 1
|
|
291
|
+
java, re2, pg = "", "", ""
|
|
292
|
+
for tok in tokens:
|
|
293
|
+
if isinstance(tok, tuple):
|
|
294
|
+
if tok[0] == "empty":
|
|
295
|
+
continue # $n past the pattern's groups: the empty string (F&O 7.6.3)
|
|
296
|
+
n = tok[1]
|
|
297
|
+
if n > 9:
|
|
298
|
+
raise NotBuilt("REPLACE naming a group past 9")
|
|
299
|
+
java += f"${n}"
|
|
300
|
+
re2 += f"\\{n}"
|
|
301
|
+
pg += "\\&" if n == 0 else f"\\{n}"
|
|
302
|
+
else:
|
|
303
|
+
java += ("\\" + tok) if tok in "\\$" else tok
|
|
304
|
+
re2 += "\\\\" if tok == "\\" else tok
|
|
305
|
+
pg += "\\\\" if tok == "\\" else tok
|
|
306
|
+
return t, java, re2, pg
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _number_states(slots: Sequence[Slot]) -> List[str]:
|
|
310
|
+
out = []
|
|
311
|
+
for s in slots:
|
|
312
|
+
if s.present is Bound.NEVER:
|
|
313
|
+
out.append(UNBOUND)
|
|
314
|
+
elif not s.terms or UNKNOWN in s.datatypes:
|
|
315
|
+
raise NotBuilt("SUBSTR over a start or length with no recorded type")
|
|
316
|
+
elif s.kinds != {LITERAL} or not s.datatypes & _INTEGERS:
|
|
317
|
+
# 17.4.3.3 takes xsd:integer; numeric promotion never narrows a
|
|
318
|
+
# decimal or a double to one, so 1.5 is a type error (Oxigraph;
|
|
319
|
+
# Jena rounds it, departing alone).
|
|
320
|
+
out.append(NO)
|
|
321
|
+
elif s.datatypes <= _INTEGERS and s.datatype is not None:
|
|
322
|
+
out.append(YES)
|
|
323
|
+
else:
|
|
324
|
+
raise NotBuilt("SUBSTR over a start or length of several datatypes")
|
|
325
|
+
return out
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def string_slot(e: E.Call, slots) -> Slot:
|
|
329
|
+
"""The slot of a string function's value over rows of `slots`."""
|
|
330
|
+
return plan(e, [expr_slot(a, slots) for a in e.args]).slot
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
# -- the lowering --------------------------------------------------------------------------
|
|
334
|
+
|
|
335
|
+
def string_boolean(e: E.Call, cells) -> R.Expr:
|
|
336
|
+
return _call(e, cells).value
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def string_cell(e: E.Call, cells):
|
|
340
|
+
return _call(e, cells)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _call(e: E.Call, cells):
|
|
344
|
+
from prograph.plan.ir import lower as L
|
|
345
|
+
args = [L.expr_cell(a, cells) for a in e.args]
|
|
346
|
+
p = plan(e, [a.slot for a in args])
|
|
347
|
+
if p.error:
|
|
348
|
+
return L.Cell(p.slot, L.NULL)
|
|
349
|
+
name = e.name
|
|
350
|
+
if name == "CONCAT":
|
|
351
|
+
ok = L.all_of([_valid(a) for a in args])
|
|
352
|
+
value = L.lexical(args[0])
|
|
353
|
+
for a in args[1:]:
|
|
354
|
+
value = L.fn("STR_CONCAT", value, L.lexical(a))
|
|
355
|
+
guard = (lambda x: x) if ok is L.TRUE else (lambda x: L.case(ok, x))
|
|
356
|
+
if p.slot.language == ROW:
|
|
357
|
+
lang = _concat_tag(args, L)
|
|
358
|
+
dt = L.case(L.is_null(lang, negated=True), L.text(xsd.RDF_LANGSTRING), L.text(xsd.STRING))
|
|
359
|
+
return L.Cell(p.slot, guard(value), dt=guard(dt), lang=guard(lang))
|
|
360
|
+
return L.Cell(p.slot, guard(value))
|
|
361
|
+
if name in HASHES | {"ENCODE_FOR_URI", "LANGMATCHES", "REPLACE"}:
|
|
362
|
+
return _other_call(name, args, p, L)
|
|
363
|
+
first = args[0]
|
|
364
|
+
ok_parts = [_valid(first)]
|
|
365
|
+
t1 = L.lexical(first)
|
|
366
|
+
if name in _PAIRS:
|
|
367
|
+
second = args[1]
|
|
368
|
+
ok_parts += [_valid(second), _compatible(first, second)]
|
|
369
|
+
t2 = L.lexical(second)
|
|
370
|
+
ok = L.all_of(ok_parts)
|
|
371
|
+
|
|
372
|
+
def guarded(value: R.Expr) -> R.Expr:
|
|
373
|
+
return value if ok is L.TRUE else L.case(ok, value)
|
|
374
|
+
|
|
375
|
+
if name in ("STRSTARTS", "STRENDS", "CONTAINS"):
|
|
376
|
+
sql = {"STRSTARTS": "STR_STARTS", "STRENDS": "STR_ENDS", "CONTAINS": "STR_CONTAINS"}[name]
|
|
377
|
+
return L.Cell(p.slot, guarded(L.fn(sql, t1, t2)))
|
|
378
|
+
if name == "REGEX":
|
|
379
|
+
t = p.pattern
|
|
380
|
+
return L.Cell(p.slot, guarded(L.fn("REGEX_MATCH", t1, L.text(t.java), L.text(t.re2),
|
|
381
|
+
L.text(t.pg))))
|
|
382
|
+
if name == "STRLEN":
|
|
383
|
+
return L.Cell(p.slot, guarded(L.fn("CAST_LONG", L.fn("STR_LEN", t1))))
|
|
384
|
+
if name in ("UCASE", "LCASE"):
|
|
385
|
+
value = L.fn("STR_UPPER" if name == "UCASE" else "STR_LOWER", t1)
|
|
386
|
+
return _tagged(p.slot, first, ok, value, matched=None)
|
|
387
|
+
if name == "SUBSTR":
|
|
388
|
+
value = _substr(t1, args[1:], L)
|
|
389
|
+
return _tagged(p.slot, first, ok, value, matched=None)
|
|
390
|
+
# STRBEFORE, STRAFTER
|
|
391
|
+
pos = L.fn("STR_POS", t1, t2)
|
|
392
|
+
found = R.CompareExpr(left=pos, op=">", right=L.integer(0))
|
|
393
|
+
if name == "STRBEFORE":
|
|
394
|
+
taken = L.fn("SUBSTR_CP", t1, L.integer(1),
|
|
395
|
+
L.case(found, L.fn("-", pos, L.integer(1)), L.integer(0)))
|
|
396
|
+
else:
|
|
397
|
+
taken = L.fn("SUBSTR_FROM", t1, L.fn("+", pos, L.fn("STR_LEN", t2)))
|
|
398
|
+
value = L.case(found, taken, R.NotExpr(child=found), L.text(""))
|
|
399
|
+
return _tagged(p.slot, first, ok, value, matched=found)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _concat_tag(args, L) -> R.Expr:
|
|
403
|
+
"""CONCAT's tag in a row (17.4.3.12): the first argument's where every argument has a tag and all
|
|
404
|
+
are one tag without regard to case, else NULL (a simple literal)."""
|
|
405
|
+
tags = [L.text(a.slot.language) if tag_kind(a.slot) == TAG else L.part(a, L.LANG) for a in args]
|
|
406
|
+
lowered = [L.fn("STR_LOWER", t) for t in tags]
|
|
407
|
+
same = L.all_of([L.is_null(t, negated=True) for t in tags]
|
|
408
|
+
+ [L.eq(lowered[0], x) for x in lowered[1:]])
|
|
409
|
+
return L.case(same, tags[0])
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def _other_call(name: str, args, p: Plan, L):
|
|
413
|
+
"""The cells of `_other_plan`'s functions."""
|
|
414
|
+
first = args[0]
|
|
415
|
+
if name in HASHES:
|
|
416
|
+
ok = _valid_simple(first)
|
|
417
|
+
value = L.fn(name, L.lexical(first))
|
|
418
|
+
return L.Cell(p.slot, value if ok is L.TRUE else L.case(ok, value))
|
|
419
|
+
if name == "ENCODE_FOR_URI":
|
|
420
|
+
ok = _valid(first)
|
|
421
|
+
value = L.fn("ENCODE_FOR_URI", L.lexical(first))
|
|
422
|
+
return L.Cell(p.slot, value if ok is L.TRUE else L.case(ok, value))
|
|
423
|
+
if name == "LANGMATCHES":
|
|
424
|
+
# RFC 4647 basic filtering (17.4.3.13): "*" any tag but ""; else the range, or the range
|
|
425
|
+
# and "-" at the tag's start, case aside.
|
|
426
|
+
tag, rng = (L.fn("STR_LOWER", L.lexical(a)) for a in args)
|
|
427
|
+
star = R.CompareExpr(left=tag, op="!=", right=L.text(""))
|
|
428
|
+
prefix = L.fn("STR_STARTS", tag, L.fn("STR_CONCAT", rng, L.text("-")))
|
|
429
|
+
value = L.case(L.eq(rng, L.text("*")), star, L.any_of([L.eq(tag, rng), prefix]))
|
|
430
|
+
ok = L.all_of([_valid_simple(a) for a in args])
|
|
431
|
+
return L.Cell(p.slot, value if ok is L.TRUE else L.case(ok, value))
|
|
432
|
+
# REPLACE: the first argument's tag kept.
|
|
433
|
+
t, java, re2, pg = p.pattern
|
|
434
|
+
value = L.fn("REGEX_REPLACE", L.lexical(first), L.text(t.java), L.text(t.re2), L.text(t.pg),
|
|
435
|
+
L.text(java), L.text(re2), L.text(pg))
|
|
436
|
+
return _tagged(p.slot, first, _valid(first), value, matched=None)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def _valid_simple(cell):
|
|
440
|
+
"""The condition that `cell` is a simple literal or an xsd:string in this row."""
|
|
441
|
+
from prograph.plan.ir import lower as L
|
|
442
|
+
slot = cell.slot
|
|
443
|
+
if _simple(slot) == YES:
|
|
444
|
+
return L.TRUE
|
|
445
|
+
conds = []
|
|
446
|
+
if slot.kind_varies:
|
|
447
|
+
conds.append(L.eq(L.part(cell, L.KIND), L.text(LITERAL)))
|
|
448
|
+
if slot.datatypes != {xsd.STRING}:
|
|
449
|
+
conds.append(L.eq(L.fn("COALESCE", L.part(cell, L.DT), L.text(xsd.STRING)), L.text(xsd.STRING)))
|
|
450
|
+
return L.all_of(conds)
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def _tagged(slot: Slot, first, ok, value, matched):
|
|
454
|
+
"""The cell of a result that keeps the first argument's tag (`matched`:
|
|
455
|
+
only where the condition holds, else a simple literal)."""
|
|
456
|
+
from prograph.plan.ir import lower as L
|
|
457
|
+
value = value if ok is L.TRUE else L.case(ok, value)
|
|
458
|
+
parts: Dict[str, R.Expr] = {}
|
|
459
|
+
if L.varies(slot, L.DT):
|
|
460
|
+
dt = L.part(first, L.DT)
|
|
461
|
+
if matched is not None:
|
|
462
|
+
tagged = L.is_null(L.part(first, L.LANG), negated=True)
|
|
463
|
+
dt = L.case(L.all_of([matched, tagged]), L.text(xsd.RDF_LANGSTRING), L.text(xsd.STRING))
|
|
464
|
+
parts[L.DT] = L.case(ok, dt) if ok is not L.TRUE else dt
|
|
465
|
+
if L.varies(slot, L.LANG):
|
|
466
|
+
lang = L.part(first, L.LANG)
|
|
467
|
+
if matched is not None:
|
|
468
|
+
lang = L.case(matched, lang)
|
|
469
|
+
parts[L.LANG] = L.case(ok, lang) if ok is not L.TRUE else lang
|
|
470
|
+
return L.Cell(slot, value, **parts)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def _valid(cell):
|
|
474
|
+
"""The condition that `cell` is a string argument in this row (TRUE where
|
|
475
|
+
its slot fixes it)."""
|
|
476
|
+
from prograph.plan.ir import lower as L
|
|
477
|
+
slot = cell.slot
|
|
478
|
+
if status(slot) == YES:
|
|
479
|
+
return L.TRUE
|
|
480
|
+
conds = []
|
|
481
|
+
if slot.kind_varies:
|
|
482
|
+
conds.append(L.eq(L.part(cell, L.KIND), L.text(LITERAL)))
|
|
483
|
+
if not slot.datatypes <= STRINGS:
|
|
484
|
+
dt = L.part(cell, L.DT)
|
|
485
|
+
conds.append(L.any_of([L.eq(dt, L.text(xsd.STRING)), L.eq(dt, L.text(xsd.RDF_LANGSTRING))]))
|
|
486
|
+
return L.all_of(conds)
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def _compatible(a, b):
|
|
490
|
+
"""Whether two string arguments are compatible in this row (17.4.3.1.2)."""
|
|
491
|
+
from prograph.plan.ir import lower as L
|
|
492
|
+
fixed = compatible_statically(a.slot, b.slot)
|
|
493
|
+
if fixed is not None:
|
|
494
|
+
return L.TRUE if fixed else L.FALSE
|
|
495
|
+
lang2 = L.part(b, L.LANG)
|
|
496
|
+
return L.any_of([L.is_null(lang2), L.eq(L.part(a, L.LANG), lang2)])
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
def _substr(text_sql: R.Expr, numbers, L):
|
|
500
|
+
"""SUBSTR's value (17.4.3.3 -> fn:substring): the characters at positions
|
|
501
|
+
p with round(start) <= p < round(start) + round(length), written with an
|
|
502
|
+
explicit clamp, not the engine's own reading of a start below 1."""
|
|
503
|
+
start = _rounded(numbers[0], L)
|
|
504
|
+
length = _rounded(numbers[1], L) if len(numbers) > 1 else None
|
|
505
|
+
if isinstance(start, int) and (length is None or isinstance(length, int)):
|
|
506
|
+
lo = max(start, 1)
|
|
507
|
+
if length is None:
|
|
508
|
+
return L.fn("SUBSTR_FROM", text_sql, L.integer(lo))
|
|
509
|
+
n = start + length - lo
|
|
510
|
+
return L.text("") if n <= 0 else L.fn("SUBSTR_CP", text_sql, L.integer(lo), L.integer(n))
|
|
511
|
+
s = L.integer(start) if isinstance(start, int) else start
|
|
512
|
+
lo = L.case(R.CompareExpr(left=s, op="<", right=L.integer(1)), L.integer(1), s)
|
|
513
|
+
if length is None:
|
|
514
|
+
return L.fn("SUBSTR_FROM", text_sql, lo)
|
|
515
|
+
ln = L.integer(length) if isinstance(length, int) else length
|
|
516
|
+
n = L.fn("-", L.fn("+", s, ln), lo)
|
|
517
|
+
return L.case(R.CompareExpr(left=n, op=">", right=L.integer(0)),
|
|
518
|
+
L.fn("SUBSTR_CP", text_sql, lo, n),
|
|
519
|
+
R.CompareExpr(left=n, op="<=", right=L.integer(0)), L.text(""))
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _rounded(cell, L):
|
|
523
|
+
"""round(x) of a numeric argument (XPath rounds half toward +INF): a
|
|
524
|
+
Python int where the argument is a constant, else SQL."""
|
|
525
|
+
value = cell.value
|
|
526
|
+
if isinstance(value, R.LitExpr) and value.value is not None:
|
|
527
|
+
raw = value.value if isinstance(value.value, str) else str(value.value)
|
|
528
|
+
return int((Decimal(raw) + Decimal("0.5")).to_integral_value(ROUND_FLOOR))
|
|
529
|
+
dt = cell.slot.datatype
|
|
530
|
+
number = L._number(cell)
|
|
531
|
+
if dt == xsd.DECIMAL:
|
|
532
|
+
half = R.LitExpr(value="0.5", sql="0.5")
|
|
533
|
+
return L.fn("CAST_LONG", L.fn("FLOOR", L.fn("+", number, half)))
|
|
534
|
+
return number
|