Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow query options to appear in any order any number of times #283

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 59 additions & 20 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,59 @@ func (*SetOp) iSelectStatement() {}
func (*ParenSelect) iSelectStatement() {}
func (*ValuesStatement) iSelectStatement() {}

type QueryOpts struct {
All bool
Distinct bool
StraightJoinHint bool
SQLCalcFoundRows bool
SQLCache bool
SQLNoCache bool
}

func (q *QueryOpts) merge(other QueryOpts) error {
q.All = q.All || other.All
q.Distinct = q.Distinct || other.Distinct
q.StraightJoinHint = q.StraightJoinHint || other.StraightJoinHint
q.SQLCalcFoundRows = q.SQLCalcFoundRows || other.SQLCalcFoundRows
q.SQLCache = q.SQLCache || other.SQLCache
q.SQLNoCache = q.SQLNoCache || other.SQLNoCache

if q.Distinct && q.All {
return errors.New("incorrect usage of DISTINCT and ALL")
}

if q.SQLCache && q.SQLNoCache {
return errors.New("incorrect usage of SQL_CACHE and SQL_NO_CACHE")
}

return nil
}

func (q QueryOpts) Format(buf *TrackedBuffer) {
if q.All {
buf.Myprintf("%s", AllStr)
}
if q.Distinct {
buf.Myprintf("%s", DistinctStr)
}
if q.StraightJoinHint {
buf.Myprintf("%s", StraightJoinHintStr)
}
if q.SQLCalcFoundRows {
buf.Myprintf("%s", SQLCalcFoundRowsStr)
}
if q.SQLCache {
buf.Myprintf("%s", SQLCacheStr)
}
if q.SQLNoCache {
buf.Myprintf("%s", SQLNoCacheStr)
}
}

// Select represents a SELECT statement.
type Select struct {
Cache string
CalcFoundRows bool
Comments Comments
Distinct string
Hints string
QueryOpts QueryOpts
With *With
SelectExprs SelectExprs
From TableExprs
Expand All @@ -491,10 +537,14 @@ type Select struct {
Into *Into
}

// Select.Distinct
// Select.QueryOpts
const (
DistinctStr = "distinct "
StraightJoinHint = "straight_join "
DistinctStr = "distinct "
AllStr = "all "
StraightJoinHintStr = "straight_join "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need these consts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're used in the Format() method, and some other parts of sql.y still rely on DistinctStr

SQLCalcFoundRowsStr = "sql_calc_found_rows "
SQLCacheStr = "sql_cache "
SQLNoCacheStr = "sql_no_cache "
)

// Select.Lock
Expand All @@ -504,12 +554,6 @@ const (
ForUpdateSkipLockedStr = " for update skip locked"
)

// Select.Cache
const (
SQLCacheStr = "sql_cache "
SQLNoCacheStr = "sql_no_cache "
)

// AddOrder adds an order by element
func (node *Select) AddOrder(order *Order) {
node.OrderBy = append(node.OrderBy, order)
Expand Down Expand Up @@ -549,14 +593,9 @@ func (node *Select) SetLimit(limit *Limit) {

// Format formats the node.
func (node *Select) Format(buf *TrackedBuffer) {
calcFoundRows := ""
if node.CalcFoundRows {
calcFoundRows = "sql_calc_found_rows "
}

buf.Myprintf("%vselect %v%s%s%s%s%v",
buf.Myprintf("%vselect %v%v%v",
node.With,
node.Comments, node.Cache, calcFoundRows, node.Distinct, node.Hints, node.SelectExprs,
node.Comments, &node.QueryOpts, node.SelectExprs,
)

if node.From != nil {
Expand Down
51 changes: 45 additions & 6 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,42 @@ var (
output: "select * from t1 where col in (select 1 union select 2)",
}, {
input: "select * from t1 where exists (select a from t2 union select b from t3)",
}, {
},
{
input: "select /* distinct */ distinct 1 from t",
}, {
},
{
input: "select all col from t",
output: "select col from t",
}, {
output: "select all col from t",
},
{
input: "select /* straight_join */ straight_join 1 from t",
}, {
},
{
input: "select sql_calc_found_rows distinct * from t",
output: "select distinct sql_calc_found_rows * from t",
},
{
input: "select distinct sql_calc_found_rows * from t",
output: "select distinct sql_calc_found_rows * from t",
},
{
input: "select distinct sql_calc_found_rows distinct * from t",
output: "select distinct sql_calc_found_rows * from t",
},
{
input: "select sql_cache distinct sql_calc_found_rows straight_join * from t",
output: "select distinct straight_join sql_calc_found_rows sql_cache * from t",
},
{
input: "select straight_join sql_calc_found_rows all sql_no_cache * from t",
output: "select all straight_join sql_calc_found_rows sql_no_cache * from t",
},
{
input: "select sql_cache distinct sql_calc_found_rows straight_join straight_join sql_calc_found_rows distinct sql_cache * from t",
output: "select distinct straight_join sql_calc_found_rows sql_cache * from t",
},
{
input: "select /* for update */ 1 from t for update",
}, {
input: "select /* skip locked */ 1 from t for update skip locked",
Expand Down Expand Up @@ -2125,7 +2153,7 @@ var (
useSelectExpressionLiteral: true,
}, {
input: "select /* cache and sql_calc_rows directive */ sql_no_cache sql_calc_found_rows 'foo' from t",
output: "select /* cache and sql_calc_rows directive */ sql_no_cache sql_calc_found_rows 'foo' from t",
output: "select /* cache and sql_calc_rows directive */ sql_calc_found_rows sql_no_cache 'foo' from t",
useSelectExpressionLiteral: true,
}, {
input: "select binary 'a' = 'A' from t",
Expand Down Expand Up @@ -4418,6 +4446,14 @@ func TestInvalid(t *testing.T) {
input: "select * from t join lateral (select * from t2)",
err: "Every derived table must have its own alias",
},
{
input: "select distinct all * from t",
err: "incorrect usage of DISTINCT and ALL",
},
{
input: "select sql_cache sql_no_cache * from t",
err: "incorrect usage of SQL_CACHE and SQL_NO_CACHE",
},
}

for _, tcase := range invalidSQL {
Expand Down Expand Up @@ -4753,6 +4789,9 @@ func runParseTestCaseWithParserOptions(t *testing.T, tcase parseTest, options Pa
tcase.output = tcase.input
}
tree, err := ParseWithOptions(tcase.input, options)
if err != nil {
panic(tcase.input)
}
require.NoError(t, err)

assertTestcaseOutput(t, tcase, tree)
Expand Down
Loading