# No column referenced
"select 1 from user"
{
  "Original": "select 1 from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select 1 from user",
    "FieldQuery": "select 1 from user where 1 != 1"
  }
}

# '*' expression for simple route
"select user.* from user"
{
  "Original": "select user.* from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select user.* from user",
    "FieldQuery": "select user.* from user where 1 != 1"
  }
}

# unqualified '*' expression for simple route
"select * from user"
{
  "Original": "select * from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user",
    "FieldQuery": "select * from user where 1 != 1"
  }
}

# qualified '*' expression for simple route
"select user.* from user"
{
  "Original": "select user.* from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select user.* from user",
    "FieldQuery": "select user.* from user where 1 != 1"
  }
}

# fully qualified '*' expression for simple route
"select user.user.* from user"
{
  "Original": "select user.user.* from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select user.* from user",
    "FieldQuery": "select user.* from user where 1 != 1"
  }
}

# invalid keyspace for '*' expression for simple route
"select a.user.* from user"
"cannot resolve a.user.* to keyspace user"

# RHS route referenced
"select user_extra.id from user join user_extra"
{
  "Original": "select user_extra.id from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select 1 from user",
      "FieldQuery": "select 1 from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user_extra.id from user_extra",
      "FieldQuery": "select user_extra.id from user_extra where 1 != 1"
    },
    "Cols": [
      1
    ]
  }
}

# Both routes referenced
"select user.col, user_extra.id from user join user_extra"
{
  "Original": "select user.col, user_extra.id from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.col from user",
      "FieldQuery": "select user.col from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user_extra.id from user_extra",
      "FieldQuery": "select user_extra.id from user_extra where 1 != 1"
    },
    "Cols": [
      -1,
      1
    ]
  }
}

# Expression with single-route reference
"select user.col, user_extra.id + user_extra.col from user join user_extra"
{
  "Original": "select user.col, user_extra.id + user_extra.col from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.col from user",
      "FieldQuery": "select user.col from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user_extra.id + user_extra.col from user_extra",
      "FieldQuery": "select user_extra.id + user_extra.col from user_extra where 1 != 1"
    },
    "Cols": [
      -1,
      1
    ]
  }
}

# Jumbled references
"select user.col, user_extra.id, user.col2 from user join user_extra"
{
  "Original": "select user.col, user_extra.id, user.col2 from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.col, user.col2 from user",
      "FieldQuery": "select user.col, user.col2 from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user_extra.id from user_extra",
      "FieldQuery": "select user_extra.id from user_extra where 1 != 1"
    },
    "Cols": [
      -1,
      1,
      -2
    ]
  }
}

# Aggregate on unsharded
"select count(*), col from unsharded"
{
  "Original": "select count(*), col from unsharded",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "select count(*), col from unsharded",
    "FieldQuery": "select count(*), col from unsharded where 1 != 1"
  }
}

# Aggregate on unique sharded
"select count(*), col from user where id = 1"
{
  "Original": "select count(*), col from user where id = 1",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select count(*), col from user where id = 1",
    "FieldQuery": "select count(*), col from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 1
  }
}

# Aggregate on sharded with colvindex
"select count(*), id from user"
{
  "Original": "select count(*), id from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select count(*), id from user",
    "FieldQuery": "select count(*), id from user where 1 != 1"
  }
}

# Non-aggregate function on sharded with no colvindex
"select fun(1), col from user"
{
  "Original": "select fun(1), col from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select fun(1), col from user",
    "FieldQuery": "select fun(1), col from user where 1 != 1"
  }
}

# Distinct
"select distinct col, id from user"
{
  "Original": "select distinct col, id from user",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select distinct col, id from user",
    "FieldQuery": "select col, id from user where 1 != 1"
  }
}

# Comments
"select /* comment */ user.col from user join user_extra"
{
  "Original": "select /* comment */ user.col from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select /* comment */ user.col from user",
      "FieldQuery": "select user.col from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select /* comment */ 1 from user_extra",
      "FieldQuery": "select 1 from user_extra where 1 != 1"
    },
    "Cols": [
      -1
    ]
  }
}

# for update
"select user.col from user join user_extra for update"
{
  "Original": "select user.col from user join user_extra for update",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.col from user for update",
      "FieldQuery": "select user.col from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select 1 from user_extra for update",
      "FieldQuery": "select 1 from user_extra where 1 != 1"
    },
    "Cols": [
      -1
    ]
  }
}

# Field query should work for joins select bind vars
"select user.id, (select user.id+outm.m+unsharded.m from unsharded) from user join unsharded outm"
{
  "Original": "select user.id, (select user.id+outm.m+unsharded.m from unsharded) from user join unsharded outm",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.id from user",
      "FieldQuery": "select user.id from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectUnsharded",
      "Keyspace": {
        "Name": "main",
        "Sharded": false
      },
      "Query": "select (select :user_id + outm.m + unsharded.m from unsharded) from unsharded as outm",
      "FieldQuery": "select (select :user_id + outm.m + unsharded.m from unsharded where 1 != 1) from unsharded as outm where 1 != 1",
      "JoinVars": {
        "user_id": {}
      }
    },
    "Cols": [
      -1,
      1
    ],
    "Vars": {
      "user_id": 0
    }
  }
}

# Case preservation
"select user.Col, user_extra.Id from user join user_extra"
{
  "Original": "select user.Col, user_extra.Id from user join user_extra",
  "Instructions": {
    "Opcode": "Join",
    "Left": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user.Col from user",
      "FieldQuery": "select user.Col from user where 1 != 1"
    },
    "Right": {
      "Opcode": "SelectScatter",
      "Keyspace": {
        "Name": "user",
        "Sharded": true
      },
      "Query": "select user_extra.Id from user_extra",
      "FieldQuery": "select user_extra.Id from user_extra where 1 != 1"
    },
    "Cols": [
      -1,
      1
    ]
  }
}

# syntax error
"the quick brown fox"
"syntax error at position 4 near 'the'"

# Hex number is not treated as a simple value
"select * from user where id = 0x04"
{
  "Original": "select * from user where id = 0x04",
  "Instructions": {
    "Opcode": "SelectScatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where id = 0x04",
    "FieldQuery": "select * from user where 1 != 1"
  }
}

# Sharding Key Condition in Parenthesis
"select * from user where name ='abc' AND (id = 4) limit 5"
{
  "Original": "select * from user where name ='abc' AND (id = 4) limit 5",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where name = 'abc' and (id = 4) limit 5",
    "FieldQuery": "select * from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 4
  }
}

# Multiple parenthesized expressions
"select * from user where (id = 4) AND (name ='abc') limit 5"
{
  "Original": "select * from user where (id = 4) AND (name ='abc') limit 5",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where (id = 4) and (name = 'abc') limit 5",
    "FieldQuery": "select * from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 4
  }
}

# Column Aliasing with Table.Column
"select user0_.col as col0_ from user user0_ where id = 1 order by user0_.col desc limit 2"
{
  "Original": "select user0_.col as col0_ from user user0_ where id = 1 order by user0_.col desc limit 2",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select user0_.col as col0_ from user as user0_ where id = 1 order by user0_.col desc limit 2",
    "FieldQuery": "select user0_.col as col0_ from user as user0_ where 1 != 1",
    "Vindex": "user_index",
    "Values": 1
  }
}

# Column Aliasing with Column
"select user0_.col as col0_ from user user0_ where id = 1 order by col0_ desc limit 3"
{
  "Original": "select user0_.col as col0_ from user user0_ where id = 1 order by col0_ desc limit 3",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select user0_.col as col0_ from user as user0_ where id = 1 order by col0_ desc limit 3",
    "FieldQuery": "select user0_.col as col0_ from user as user0_ where 1 != 1",
    "Vindex": "user_index",
    "Values": 1
  }
}

# Booleans and parenthesis
"select * from user where (id = 1) AND name = true limit 5"
{
  "Original": "select * from user where (id = 1) AND name = true limit 5",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where (id = 1) and name = true limit 5",
    "FieldQuery": "select * from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 1
  }
}

# Column as boolean-ish
"select * from user where (id = 1) AND name limit 5"
{
  "Original": "select * from user where (id = 1) AND name limit 5",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where (id = 1) and name limit 5",
    "FieldQuery": "select * from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 1
  }
}

# PK as fake boolean, and column as boolean-ish
"select * from user where (id = 5) AND name = true limit 5"
{
  "Original": "select * from user where (id = 5) AND name = true limit 5",
  "Instructions": {
    "Opcode": "SelectEqualUnique",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "Query": "select * from user where (id = 5) and name = true limit 5",
    "FieldQuery": "select * from user where 1 != 1",
    "Vindex": "user_index",
    "Values": 5
  }
}

"select * from (select col1, col2 from unsharded where id = 1 union select col1, col2 from unsharded where id = 3) a"
{
  "Original": "select * from (select col1, col2 from unsharded where id = 1 union select col1, col2 from unsharded where id = 3) a",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "select * from (select col1, col2 from unsharded where id = 1 union select col1, col2 from unsharded where id = 3) as a",
    "FieldQuery": "select * from (select col1, col2 from unsharded where 1 != 1 union select col1, col2 from unsharded where 1 != 1) as a where 1 != 1"
  }
}

"select id, name from unsharded where id in (select id from unsharded where id = 1 union select id from unsharded where id = 3)"
{
  "Original": "select id, name from unsharded where id in (select id from unsharded where id = 1 union select id from unsharded where id = 3)",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "select id, name from unsharded where id in (select id from unsharded where id = 1 union select id from unsharded where id = 3)",
    "FieldQuery": "select id, name from unsharded where 1 != 1"
  }
}

"(select id from unsharded) union (select id from unsharded_auto) order by id limit 5"
{
  "Original": "(select id from unsharded) union (select id from unsharded_auto) order by id limit 5",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "(select id from unsharded) union (select id from unsharded_auto) order by id asc limit 5",
    "FieldQuery": "(select id from unsharded where 1 != 1) union (select id from unsharded_auto where 1 != 1)"
  }
}

"select id from unsharded union select id from unsharded_auto union select id from unsharded_auto where id in (132)"
{
  "Original": "select id from unsharded union select id from unsharded_auto union select id from unsharded_auto where id in (132)",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "select id from unsharded union select id from unsharded_auto union select id from unsharded_auto where id in (132)",
    "FieldQuery": "select id from unsharded where 1 != 1 union select id from unsharded_auto where 1 != 1 union select id from unsharded_auto where 1 != 1"
  }
}

"(select id from unsharded union select id from unsharded_auto) union (select id from unsharded_auto union select name from unsharded)"
{
  "Original": "(select id from unsharded union select id from unsharded_auto) union (select id from unsharded_auto union select name from unsharded)",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "(select id from unsharded union select id from unsharded_auto) union (select id from unsharded_auto union select name from unsharded)",
    "FieldQuery": "(select id from unsharded where 1 != 1 union select id from unsharded_auto where 1 != 1) union (select id from unsharded_auto where 1 != 1 union select name from unsharded where 1 != 1)"
  }
}

"(select id from unsharded order by id asc limit 1) union (select id from unsharded order by id desc limit 1) order by id asc limit 1"
{
  "Original": "(select id from unsharded order by id asc limit 1) union (select id from unsharded order by id desc limit 1) order by id asc limit 1",
  "Instructions": {
    "Opcode": "SelectUnsharded",
    "Keyspace": {
      "Name": "main",
      "Sharded": false
    },
    "Query": "(select id from unsharded order by id asc limit 1) union (select id from unsharded order by id desc limit 1) order by id asc limit 1",
    "FieldQuery": "(select id from unsharded where 1 != 1) union (select id from unsharded where 1 != 1)"
  }
}
