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

Add <Cmp>TupleNamed for two-sided range-queries on tuples #219

Merged
merged 3 commits into from
Feb 17, 2022

Conversation

Drahflow
Copy link
Contributor

I found no other way to

sometable.SelectBuilder(colSomeCol).
    Where(qb.GtOrEqTupleNamed(colCreatedAtID, 2, "start")).
    Where(qb.LtTupleNamed(colCreatedAtID, 2, "end")).

i.e. do specify a range scan on a tuple column with both ends of the range given. With only qb.GtOrEqTuple and qb.LtTuple the autogenerated names are both derived from the column name and collide, thus it is not possible to specify different ends for the range.

Before merging this, please also consider that tuple-element syntax has some conflict with gocql, which produces "col[0]" in SliceMap, and not "col_0" as gocqlx wants it during tuple binding.

Some options I see:

  • Rename this PR's functions (or add yet more) with <Cmp>TupleIdx resp. <Cmp>TupleIdxNamed which autogenerate gocql compatible column names.
  • Accept API breakage :( and change tupleparam.
  • Introduce <Cmp>TupleNamedElements(column string, names ...string) which gives names for all elements (thereby also specifying count)

If you tell me which of these two options you'd like to see, I'll be preparing an according follow-up PR.

@mmatczuk
Copy link
Contributor

For the sake of completeness its fine to add the named variants to tuple compare functions - please add test cases.

The maximal benefit in this area would come from a dedicated API that would allow to operate on tuple filed level see example code below.

type TupleCmp struct {
	column string
	lit   []lit
	param []param
}

func (t *TupleCmp) IdxParam(...) *TupleCmp {
}

func (t *TupleCmp) IdxParamNamed(...) *TupleCmp {
}

func (t *TupleCmp) IdxLit(...) *TupleCmp {
}

func (t *TupleCmp) Eq() Cmp {
	
}
...

func Tuple(column string) *TupleCmp {
	return &TupleCmp{
		column: column,
		lit:   make([]lit, 4),
		param: make([]param, 4),
	}
}

As for

Before merging this, please also consider that tuple-element syntax has some conflict with gocql, which produces "col[0]" in SliceMap, and not "col_0" as gocqlx wants it during tuple binding.

I think that it's not possible to bind a slice to a tuple, but maybe you can prove me wrong.

@Drahflow
Copy link
Contributor Author

I think that it's not possible to bind a slice to a tuple, but maybe you can prove me wrong.

No idea about that, but I was referring to
https://github.com/gocql/gocql/blob/558dfae50b5d369de77dae132dbfa64968e3abd4/helpers.go#L290

... which produces entries like tuple_col[0] in the results fetched from cassandra.

So as of now (with or without this PR), input syntax for tuple elements is

q := c.Session.Query(
   tbl.SelectBuilder().
     Where(qb.eqTuple("tuple_col", 2)).
     ToCql(),
).BindMap(qb.M{
  "tuple_col_0": someValue,
  "tuple_col_1": someOtherValue,
})

but output syntax is

data := q.Iter().SliceMap()
for _, row := range data {
    fmt.Printf("%v %v\n",
      row["tuple_col[0]"],
      row["tuple_col[1]"],
    )
}

And that's quite an inconsistency.

@mmatczuk
Copy link
Contributor

I guess we can fix that.

@mmatczuk
Copy link
Contributor

Thanks!

@mmatczuk mmatczuk merged commit 8054b9d into scylladb:master Feb 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants