Skip to content

Parse failure of schema.table with INSERT/UPDATE #89

Description

@icio

Similarly to #31, the INSERT/UPDATE query parsing errors when targeting schemaname.tablename references.

For example, the following executes successfully in SQLite3:

sqlite> CREATE TABLE main.tableau (n1, n2);
sqlite> INSERT INTO main.tableau VALUES (1, 2), (3, 4);
sqlite> UPDATE main.tableau SET n2=n1;
sqlite> DELETE FROM main.tableau WHERE n2 > 1;
sqlite> SELECT * FROM main.tableau;
+----+----+
| n1 | n2 |
+----+----+
| 1  | 1  |
+----+----+

However, the queries after the INSERT and UPDATE queries fail to parse with github.com/rqlite/sql: https://go.dev/play/p/LrTg9SXOODx

package main

import (
	"fmt"
	"strings"

	"github.com/rqlite/sql"
)

func main() {
	for _, q := range []string{
		`INSERT INTO main.tableau VALUES (1, 2), (3, 4);`,
		`UPDATE main.tableau SET n2=n1;`,
	} {
		p := sql.NewParser(strings.NewReader(q))
		s, err := p.ParseStatement()
		fmt.Printf("%q\n   err: %v\n  stmt: %#v\n", q, err, s)
	}
}

Output:

"INSERT INTO main.tableau VALUES (1, 2), (3, 4);"
   err: 1:17: expected VALUES, SELECT, or DEFAULT VALUES, found '.'
  stmt: &sql.InsertStatement{WithClause:(*sql.WithClause)(nil), Insert:sql.Pos{Offset:0, Line:1, Column:1}, Replace:sql.Pos{Offset:0, Line:0, Column:0}, InsertOr:sql.Pos{Offset:0, Line:0, Column:0}, InsertOrReplace:sql.Pos{Offset:0, Line:0, Column:0}, InsertOrRollback:sql.Pos{Offset:0, Line:0, Column:0}, InsertOrAbort:sql.Pos{Offset:0, Line:0, Column:0}, InsertOrFail:sql.Pos{Offset:0, Line:0, Column:0}, InsertOrIgnore:sql.Pos{Offset:0, Line:0, Column:0}, Into:sql.Pos{Offset:7, Line:1, Column:8}, Table:(*sql.Ident)(0xc0000b0180), As:sql.Pos{Offset:0, Line:0, Column:0}, Alias:(*sql.Ident)(nil), ColumnsLparen:sql.Pos{Offset:0, Line:0, Column:0}, Columns:[]*sql.Ident(nil), ColumnsRparen:sql.Pos{Offset:0, Line:0, Column:0}, Values:sql.Pos{Offset:0, Line:0, Column:0}, ValueLists:[]*sql.ExprList(nil), Select:(*sql.SelectStatement)(nil), Default:sql.Pos{Offset:0, Line:0, Column:0}, DefaultValues:sql.Pos{Offset:0, Line:0, Column:0}, UpsertClause:(*sql.UpsertClause)(nil), ReturningClause:(*sql.ReturningClause)(nil)}
"UPDATE main.tableau SET n2=n1;"
   err: 1:12: expected unqualified table name, found '.'
  stmt: &sql.UpdateStatement{WithClause:(*sql.WithClause)(nil), Update:sql.Pos{Offset:0, Line:1, Column:1}, UpdateOr:sql.Pos{Offset:0, Line:0, Column:0}, UpdateOrReplace:sql.Pos{Offset:0, Line:0, Column:0}, UpdateOrRollback:sql.Pos{Offset:0, Line:0, Column:0}, UpdateOrAbort:sql.Pos{Offset:0, Line:0, Column:0}, UpdateOrFail:sql.Pos{Offset:0, Line:0, Column:0}, UpdateOrIgnore:sql.Pos{Offset:0, Line:0, Column:0}, Table:(*sql.QualifiedTableName)(0xc0000f6000), Set:sql.Pos{Offset:0, Line:0, Column:0}, Assignments:[]*sql.Assignment(nil), Where:sql.Pos{Offset:0, Line:0, Column:0}, WhereExpr:sql.Expr(nil), ReturningClause:(*sql.ReturningClause)(nil)}
  • INSERT gets parsed into a sql.InsertStatement which does not appear to be schema-aware. sql.CreateTableStatment has separate Schema and Name fields to identify the table, which InsertStatement could copy as they're using the same language constructs:

    Image Image
  • UPDATE gets parsed into a sql.UpdateTableStatement which has a Table *sql.QualifiedTableName field, despite the "expected unqualified table name" error. This seems like a straightforward bug.

    Image Image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions