Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Dec 27, 2024
1 parent a07a46f commit 5d1daa2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
22 changes: 11 additions & 11 deletions cmd/tests/catalogs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

set -e

# VSQL1_FILE="$(mktemp -d)/file1.abc.vsql" || exit 1
# VSQL2_FILE="$(mktemp -d)/file2.def.vsql" || exit 1
# TXT_FILE="$(mktemp).sql" || exit 1
VSQL1_FILE="$(mktemp -d)/file1.abc.vsql" || exit 1
VSQL2_FILE="$(mktemp -d)/file2.def.vsql" || exit 1
TXT_FILE="$(mktemp).sql" || exit 1

# echo 'CREATE TABLE foo (bar INT);' | $VSQL cli $VSQL1_FILE
# echo 'INSERT INTO foo (bar) VALUES (123);' | $VSQL cli $VSQL1_FILE
echo 'CREATE TABLE foo (bar INT);' | $VSQL cli $VSQL1_FILE
echo 'INSERT INTO foo (bar) VALUES (123);' | $VSQL cli $VSQL1_FILE

# echo 'CREATE TABLE foo (baz INT);' | $VSQL cli $VSQL2_FILE
# echo 'INSERT INTO foo (baz) VALUES (456);' | $VSQL cli $VSQL2_FILE
echo 'CREATE TABLE foo (baz INT);' | $VSQL cli $VSQL2_FILE
echo 'INSERT INTO foo (baz) VALUES (456);' | $VSQL cli $VSQL2_FILE

# echo 'SET CATALOG 'file1'; SELECT * FROM public.foo;' | $VSQL cli $VSQL1_FILE $VSQL2_FILE > $TXT_FILE
# echo 'SET CATALOG 'file2'; SELECT * FROM public.foo;' | $VSQL cli $VSQL1_FILE $VSQL2_FILE >> $TXT_FILE
echo "SET CATALOG 'file1'; SELECT * FROM public.foo;" | $VSQL cli $VSQL1_FILE $VSQL2_FILE > $TXT_FILE
echo "SET CATALOG 'file2'; SELECT * FROM public.foo;" | $VSQL cli $VSQL1_FILE $VSQL2_FILE >> $TXT_FILE

# grep -R "BAR: 123" $TXT_FILE
# grep -R "BAZ: 456" $TXT_FILE
grep -R "BAR: 123" $TXT_FILE
grep -R "BAZ: 456" $TXT_FILE
6 changes: 5 additions & 1 deletion cmd/vsql/cli.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn cli_command(cmd cli.Command) ! {

if raw_query != '' {
// TODO: This is a very poor way to handle multiple queries.
for query in raw_query.split(';') {
for i, query in raw_query.split(';') {
if query.trim_space() == '' {
continue
}
Expand Down Expand Up @@ -73,6 +73,10 @@ fn cli_command(cmd cli.Command) ! {
}

println('${total_rows} ${vsql.pluralize(total_rows, 'row')} (${time.ticks() - start} ms)')

if i > 0 {
println('')
}
}
} else {
// This means there is no more input and should only occur when the
Expand Down
26 changes: 3 additions & 23 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,16 @@ rebuild the entire docs you can use:
Parser & SQL Grammar
--------------------

To make changes to the SQL grammar you will need to modify the ``grammar.bnf``
file. These rules are partial or complete BNF rules from the
To make changes to the SQL grammar you will need to modify the ``*.y`` files.
These rules are partial or complete BNF rules from the
`2016 SQL standard <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html>`_.

Within ``grammar.bnf`` you will see that some of the rules have a parser
function which is a name after ``->``. The actual parser function will have
``parse_`` prefix added. You can find all the existing parse functions in the
``parse.v`` file.

If a rule does not have a parse function (no ``->``) then the value will be
passed up the chain which is the desired behavior in most cases. However, be
careful if there are multiple terms, you will need to provide a parse function
to return the correct term.

Each of the rules can have an optional type described in ``/* */`` before
``::=``. Rules that do not have a type will be ignored as parameters for parse
functions. Otherwise, these types are used in the generated code to make sure
the correct types are passed into the parse functions.

After making changes to ``grammar.bnf`` you will need to run:
After making changes to grammar file(s) you will need to run:

.. code-block:: sh
make grammar
Now, when running `v test .` you may receive errors for missing ``parse_``
functions, you should implement those now.

FIX THIS

Testing
-------

Expand Down
6 changes: 6 additions & 0 deletions vsql/lexer.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

module vsql

// YYSym allows for every possible type that needs to pass through $$ rules in
// the grammar. If V ever supports some kind of "any" type this would be a
// little easier.
type YYSym = AggregateFunction
| AggregateFunctionCount
| AlterSequenceGeneratorStatement
Expand Down Expand Up @@ -96,6 +99,9 @@ type YYSym = AggregateFunction
| map[string]UpdateSource
| string

// YYSymType is the yacc internal type for the stack that contains the symbols
// to reduce (pass to the code). The only requirement here is that `yys` is
// included as it contains the position of the token.
struct YYSymType {
mut:
v YYSym
Expand Down
14 changes: 14 additions & 0 deletions vsql/sql_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,17 @@ fn run_single_test(test SQLTest, verbose bool, filter_line int) ! {

assert expected == actual_trim
}

// Make sure any non-keywords (operators, literals, etc) are replaced in error
// messages.
fn test_cleanup_yacc_error() {
mut msg := ''
for tok_name in yy_toknames {
if tok_name.starts_with('OPERATOR_') || tok_name.starts_with('LITERAL_') {
msg += ' ' + tok_name
}
}
result := cleanup_yacc_error(msg)
assert !result.contains('OPERATOR_')
assert !result.contains('LITERAL_')
}
4 changes: 4 additions & 0 deletions vsql/std_6_7_column_reference.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%%

// ISO/IEC 9075-2:2016(E), 6.7, <column reference>
//
// Reference a column.

column_reference:
basic_identifier_chain {
$$.v = new_column_identifier(($1.v as IdentifierChain).identifier)!
Expand Down

0 comments on commit 5d1daa2

Please sign in to comment.