Skip to content

Commit

Permalink
fix(parser): json index operator with alter table (#61)
Browse files Browse the repository at this point in the history
Turns out `name` is an Option and that `expr` can be a more complicated
object rather than just a string.

Previously we didn't have any tests that exercised the `expr` property.

I think the issues with clippy are related to: rust-lang/rust-clippy#2604

fixes: #59
  • Loading branch information
sbdchd authored Sep 9, 2020
1 parent 40d39a8 commit cb5a390
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ jobs:
- name: Install Clippy
run: rustup component add clippy

- name: Get Clippy Version
run: cargo clippy --version

- name: Lint
run: ./s/lint

Expand Down
5 changes: 4 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(clippy::match_wildcard_for_single_variants)]
#[allow(clippy::non_ascii_literal)]
#[allow(clippy::cast_sign_loss)]
mod reporter;
mod subcommand;
use crate::reporter::{
Expand Down Expand Up @@ -84,7 +87,7 @@ fn main() {
&mut handle,
&opts.paths,
is_stdin,
dump_ast_kind,
&dump_ast_kind,
));
} else {
match check_files(&opts.paths, is_stdin, opts.stdin_filepath, opts.exclude) {
Expand Down
9 changes: 4 additions & 5 deletions cli/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn dump_ast_for_paths<W: io::Write>(
f: &mut W,
paths: &[String],
is_stdin: bool,
dump_ast: DumpAstOption,
dump_ast: &DumpAstOption,
) -> Result<(), DumpAstError> {
let mut process_dump_ast = |sql: &str| -> Result<(), DumpAstError> {
match dump_ast {
Expand Down Expand Up @@ -193,8 +193,7 @@ fn fmt_gcc<W: io::Write>(
.iter()
.map(|v| {
match v {
ViolationMessage::Note(s) => s,
ViolationMessage::Help(s) => s,
ViolationMessage::Note(s) | ViolationMessage::Help(s) => s,
}
.to_string()
})
Expand Down Expand Up @@ -308,7 +307,7 @@ pub fn pretty_violations(
// 1-indexed
let lineno = sql[..start].lines().count() + 1;

let content = &sql[start..start + len + 1];
let content = &sql[start..=start + len];

// TODO(sbdchd): could remove the leading whitespace and comments to
// get cleaner reports
Expand Down Expand Up @@ -741,7 +740,7 @@ SELECT 1;
assert_debug_snapshot!(pretty_violations(violations, sql, filename));
}

/// pretty_violations was slicing the SQL improperly, trimming off the first
/// `pretty_violations` was slicing the SQL improperly, trimming off the first
/// letter.
#[test]
fn test_trimming_sql_newlines() {
Expand Down
13 changes: 6 additions & 7 deletions cli/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ fn get_github_private_key(
github_private_key: Option<String>,
github_private_key_base64: Option<String>,
) -> Result<String, SquawkError> {
match github_private_key {
Some(private_key) => Ok(private_key),
None => {
let key = github_private_key_base64.ok_or(SquawkError::GithubPrivateKeyMissing)?;
let bytes = base64::decode(key)?;
Ok(String::from_utf8(bytes)?)
}
if let Some(private_key) = github_private_key {
Ok(private_key)
} else {
let key = github_private_key_base64.ok_or(SquawkError::GithubPrivateKeyMissing)?;
let bytes = base64::decode(key)?;
Ok(String::from_utf8(bytes)?)
}
}

Expand Down
3 changes: 3 additions & 0 deletions github/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(clippy::doc_markdown)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::single_match_else)]
use jsonwebtoken::{Algorithm, EncodingKey, Header};
use log::info;
use reqwest::header::{ACCEPT, AUTHORIZATION};
Expand Down
4 changes: 2 additions & 2 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ impl Default for SortByNulls {
#[derive(Debug, Deserialize, Serialize)]
pub struct IndexElem {
/// name of attribute to index, or NULL
name: String,
name: Option<String>,
/// expression to index, or NULL
expr: Option<String>,
expr: Option<Value>,
/// name for index column; NULL = default
indexcolname: Option<String>,
/// name of collation; NIL = default
Expand Down
9 changes: 9 additions & 0 deletions parser/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,15 @@ ALTER TABLE foobar ALTER COLUMN value SET DEFAULT TO_JSON(false);
assert_debug_snapshot!(res);
}

#[test]
fn test_json_index_operator() {
let sql = r#"
CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_a_foo_bar" ON "a" ((foo->>'bar'));
"#;
let res = parse_sql_query(sql);
assert_debug_snapshot!(res);
}

#[test]
fn test_create_subscription_stmt() {
let sql = r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "field_name",
name: Some(
"field_name",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down Expand Up @@ -60,7 +62,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "field_name",
name: Some(
"field_name",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
source: parser/src/parse.rs
expression: res
---
Ok(
[
RawStmt(
RawStmt {
stmt: IndexStmt(
IndexStmt {
access_method: "btree",
idxname: "idx_a_foo_bar",
index_params: [
IndexElem(
IndexElem {
name: None,
expr: Some(
Object({
"A_Expr": Object({
"kind": Number(
0,
),
"lexpr": Object({
"ColumnRef": Object({
"fields": Array([
Object({
"String": Object({
"str": String(
"foo",
),
}),
}),
]),
"location": Number(
66,
),
}),
}),
"location": Number(
69,
),
"name": Array([
Object({
"String": Object({
"str": String(
"->>",
),
}),
}),
]),
"rexpr": Object({
"A_Const": Object({
"location": Number(
72,
),
"val": Object({
"String": Object({
"str": String(
"bar",
),
}),
}),
}),
}),
}),
}),
),
indexcolname: None,
collation: None,
opclass: [],
ordering: Default,
nulls_ordering: Default,
},
),
],
relation: RangeVar(
RangeVar {
catalogname: None,
schemaname: None,
relname: "a",
inh: true,
relpersistence: "p",
alias: None,
location: 60,
},
),
concurrent: true,
unique: false,
primary: false,
isconstraint: false,
deferrable: false,
initdeferred: false,
transformed: false,
if_not_exists: true,
table_space: None,
},
),
stmt_location: 0,
stmt_len: Some(
79,
),
},
),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "field_name",
name: Some(
"field_name",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down Expand Up @@ -73,7 +75,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "field_name",
name: Some(
"field_name",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "table_field",
name: Some(
"table_field",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "table_field",
name: Some(
"table_field",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ Ok(
index_params: [
IndexElem(
IndexElem {
name: "age",
name: Some(
"age",
),
expr: None,
indexcolname: None,
collation: None,
Expand Down
2 changes: 1 addition & 1 deletion s/lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
set -eu

cargo fmt -- --check
cargo clippy --all-targets --all-features -- -D clippy::nursery
cargo clippy --all-targets --all-features -- -D clippy::pedantic

0 comments on commit cb5a390

Please sign in to comment.