MCPcopy Create free account
hub / github.com/google-research/language / parse_sql

Function parse_sql

language/compgen/nqg/tasks/spider/sql_parser.py:346–371  ·  view source on GitHub ↗

Parse SQL string and return Node representing parse tree.

(sql, verbose=False)

Source from the content-addressed store, hash-verified

344
345
346def parse_sql(sql, verbose=False):
347 """Parse SQL string and return Node representing parse tree."""
348 tokens = sql_tokenizer.tokenize_sql(sql)
349
350 rules = []
351 for rule_string in RULES:
352 rules.append(Rule.from_string(rule_string))
353
354 # Add rule `T -> token` for every non-keyword token in input.
355 for token in set(tokens):
356 if token not in KEYWORDS:
357 rules.append(Rule("T", token))
358
359 # Re-split on spaces.
360 tokens = " ".join(tokens).split(" ")
361 if verbose:
362 print("Parsing SQL:")
363 for idx, token in enumerate(tokens):
364 print("%s - %s" % (idx, token))
365
366 nodes = _run_parser(tokens, rules, verbose=verbose)
367 if not nodes:
368 raise ValueError("No parse for SQL: %s" % sql)
369 elif len(nodes) > 1:
370 raise ValueError("Multiple parses for SQL: %s (%s)" % (sql, nodes))
371 return nodes[0]

Callers

nothing calls this directly

Calls 4

_run_parserFunction · 0.85
splitMethod · 0.80
RuleClass · 0.70
from_stringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…