Skip to content

Commit

Permalink
chore: Remove Expr (#171)
Browse files Browse the repository at this point in the history
This is a huge refactoring to remove the Expr type so that the AST is
now represented as the same structure as the SQL standard and types have
been moved the relevant files.
  • Loading branch information
elliotchance authored Oct 31, 2023
1 parent e46e29e commit 6d9a2d5
Show file tree
Hide file tree
Showing 91 changed files with 6,103 additions and 4,175 deletions.
2 changes: 1 addition & 1 deletion docs/v-client-library-docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ struct VirtualTable
pub struct VirtualTable {
create_table_sql string
create_table_stmt CreateTableStmt
create_table_stmt TableDefinition
data VirtualTableProviderFn [required]
mut:
is_done bool
Expand Down
21 changes: 14 additions & 7 deletions generate-grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ def parse_grammar(grammar):
# If there is a parsing function we replace the current rule with a
# subrule:
#
# <term> /* Expr */ ::=
# <term> /* Term */ ::=
# <factor>
# | <term> <asterisk> <factor> -> binary_expr
# | <term> <solidus> <factor> -> binary_expr
# | <term> <asterisk> <factor> -> term_1
# | <term> <solidus> <factor> -> term_2
#
# Becomes:
#
Expand All @@ -334,7 +334,7 @@ def parse_grammar(grammar):
# can capture all the children and invoke the parsing function.
if '->' in rule:
rule, parse_function = rule.split(' -> ')
new_rule = name[:-1] + ': ' + str(rule_number + 1) + '>'
new_rule = name[:-1] + ': #' + str(rule_number + 1) + '>'
lines.append(new_rule + ' ::= ' + rule)
rules[rule_number] = new_rule
parse_functions[new_rule] = (parse_function.strip(), re.findall('(<.*?>|[^\s]+)', rule))
Expand Down Expand Up @@ -375,7 +375,7 @@ def rule_name(name):
return name

def var_name(name):
return "rule_" + rule_name(name).lower().replace('<', '').replace('-', '_').replace(':', '').replace('>', '_').replace(' ', '_').replace('^', '_')
return "rule_" + rule_name(name).lower().replace('<', '').replace('#', '_').replace('-', '_').replace(':', '').replace('>', '_').replace(' ', '_').replace('^', '_')

# Generate grammar file
grammar_file = open('vsql/grammar.v', mode='w')
Expand Down Expand Up @@ -459,11 +459,18 @@ def var_name(name):
for rule in sorted(parse_functions.keys(), key=lambda s: s.lower()):
grammar_file.write("\t\t'" + rule + "' {\n")
function_name, terms = parse_functions[rule]
grammar_file.write("\t\t\treturn [EarleyValue(parse_" + function_name + "(")
if function_name == function_name.lower():
grammar_file.write("\t\t\treturn [EarleyValue(parse_" + function_name + "(")
else:
grammar_file.write("\t\t\treturn [EarleyValue(" + function_name + "(")
grammar_file.write(', '.join([
'children[' + str(i) + '] as ' + grammar_types[t]
for i, t in enumerate(terms)
if t in grammar_types and grammar_types[t] != '']) + ") !)]\n")
if t in grammar_types and grammar_types[t] != '']))
if function_name == function_name.lower():
grammar_file.write(") !)]\n")
else:
grammar_file.write("))]\n")

grammar_file.write("\t\t}\n")

Expand Down
9 changes: 6 additions & 3 deletions tests/values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ EXPLAIN SELECT * FROM (VALUES ROW(1, 'foo', TRUE));
-- EXPLAIN: EXPR ($1.COL1 NUMERIC, $1.COL2 CHARACTER VARYING(3), $1.COL3 BOOLEAN)

EXPLAIN SELECT * FROM (VALUES 1, 'foo', TRUE) AS t1 (abc, col2, "f");
-- error 42601: syntax error: ROW provides the wrong number of columns for the correlation
-- EXPLAIN: T1:
-- EXPLAIN: VALUES (ABC NUMERIC, COL2 NUMERIC, "f" NUMERIC) = ROW(1), ROW('foo'), ROW(TRUE)
-- EXPLAIN: TABLE T1 (ABC NUMERIC, COL2 NUMERIC, "f" NUMERIC)
-- EXPLAIN: EXPR (T1.ABC NUMERIC, T1.COL2 NUMERIC, T1."f" NUMERIC)

EXPLAIN SELECT * FROM (VALUES ROW(1, 'foo', TRUE)) AS t1 (abc, col2, "f");
-- EXPLAIN: T1:
Expand All @@ -37,7 +40,7 @@ EXPLAIN SELECT * FROM (VALUES ROW(1, 'foo', TRUE)) AS t1 (abc, col2, "f");
-- EXPLAIN: EXPR (T1.ABC NUMERIC, T1.COL2 CHARACTER VARYING(3), T1."f" BOOLEAN)

SELECT * FROM (VALUES 1, 'foo', TRUE) AS t1 (abc, col2, "f");
-- error 42601: syntax error: ROW provides the wrong number of columns for the correlation
-- error 42601: syntax error: unknown column: T1.COL2

SELECT * FROM (VALUES ROW(1, 'foo', TRUE)) AS t1 (abc, col2, "f");
-- ABC: 1 COL2: foo f: TRUE
Expand Down Expand Up @@ -103,7 +106,7 @@ SELECT * FROM (VALUES 1, 2) AS t1 (foo);
-- FOO: 2

SELECT * FROM (VALUES 1, 2) AS t1 (foo, bar, baz);
-- error 42601: syntax error: ROW provides the wrong number of columns for the correlation
-- error 42601: syntax error: unknown column: T1.BAR

SELECT * FROM (VALUES ROW(1, 2), ROW(3, 4)) AS t1 (foo, bar);
-- FOO: 1 BAR: 2
Expand Down
30 changes: 13 additions & 17 deletions vsql/alter_sequence.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module vsql

import time

fn execute_alter_sequence(mut c Connection, stmt AlterSequenceStmt, elapsed_parse time.Duration) !Result {
fn execute_alter_sequence(mut c Connection, stmt AlterSequenceGeneratorStatement, elapsed_parse time.Duration) !Result {
t := start_timer()

c.open_write_connection()!
Expand All @@ -24,38 +24,34 @@ fn execute_alter_sequence(mut c Connection, stmt AlterSequenceStmt, elapsed_pars
// Not possible.
}
SequenceGeneratorRestartOption {
if option.restart_value is NoExpr {
sequence.current_value = sequence.reset() - sequence.increment_by
if v := option.restart_value {
sequence.current_value = (v.eval(mut c, Row{}, map[string]Value{})!).as_int() - sequence.increment_by
} else {
sequence.current_value = (eval_as_value(mut c, Row{}, option.restart_value,
map[string]Value{})!).as_int() - sequence.increment_by
sequence.current_value = sequence.reset() - sequence.increment_by
}
}
SequenceGeneratorIncrementByOption {
sequence.increment_by = (eval_as_value(mut c, Row{}, option.increment_by,
map[string]Value{})!).as_int()
sequence.increment_by = (option.increment_by.eval(mut c, Row{}, map[string]Value{})!).as_int()

// Since we increment the value after it's returned we need to correct
// for the current value that takes into account the difference of the
// new INCREMENT BY.
sequence.current_value += (sequence.increment_by - old_sequence.increment_by) - 1
}
SequenceGeneratorMinvalueOption {
if option.min_value is NoExpr {
sequence.has_min_value = false
} else {
sequence.min_value = (eval_as_value(mut c, Row{}, option.min_value,
map[string]Value{})!).as_int()
if v := option.min_value {
sequence.min_value = (v.eval(mut c, Row{}, map[string]Value{})!).as_int()
sequence.has_min_value = true
} else {
sequence.has_min_value = false
}
}
SequenceGeneratorMaxvalueOption {
if option.max_value is NoExpr {
sequence.has_min_value = false
} else {
sequence.max_value = (eval_as_value(mut c, Row{}, option.max_value,
map[string]Value{})!).as_int()
if v := option.max_value {
sequence.max_value = (v.eval(mut c, Row{}, map[string]Value{})!).as_int()
sequence.has_max_value = true
} else {
sequence.has_min_value = false
}
}
SequenceGeneratorCycleOption {
Expand Down
Loading

0 comments on commit 6d9a2d5

Please sign in to comment.