edgedb-net icon indicating copy to clipboard operation
edgedb-net copied to clipboard

QueryBuilder does not translate type converted properties correctly

Open quinchs opened this issue 3 years ago • 0 comments

Summary

With the introduction of type converters, the bridge between Expressions and EdgeQL starts to bend. Currently, the querybuilder correctly translates assignment/initializations expressions and shapes, but it falls short at expressions that are directly influenced by type converted properties.

Example:

public class User
{
    [EdgeDBTypeConverter(typeof(UlongToStringConverter))]
    public ulong SnowflakeId { get; set; }
}

...

ulong targetId = 12345;

var result = await QueryBuilder
    .Select<User>()
    .Filter(x => x.SnowflakeId  == targetId)
    .ExecuteAsync();

The expression x => x.SnowflakeId == targetId does not take into account that SnowflakeId is actually a string in EdgeDB, and treats targetId as a number, producing the following snippet:

.snowflake_id = <int64>$random_var_name

which errors due to the type difference of str and int64.

Potential solution

We can look to quantum mechanics for a solution, taking inspiration from the principle of entanglement. The query builder can find all expressions entangled with a particular type converted property and put the entangled references thru the same type converter to build the EdgeQL query.

With this approach, our above expression follows this logic:

  1. Binary expression is translated
  2. The member expression x.SnowflakeId is discovered as a type converted property.
  3. The expression tree is walked backwards from the MemberExpression and any values with the same shared type as the member expression are updated with the converted value.

I believe that a ExpressionVisitor can be used to achieve this, if not we can simply just rebuild the effected expressions.

quinchs avatar Nov 28 '22 14:11 quinchs