Skip to content

Commit

Permalink
chore(embedded/sql): improvements on SQL layer.
Browse files Browse the repository at this point in the history
    - Allow selecting generic expressions;
    - Refactor FnCall implementation;
    - Add support for INSERT INTO SELECT statements;
    - Add more core builtin functions;

Signed-off-by: Stefano Scafiti <stefano.scafiti96@gmail.com>
  • Loading branch information
ostafen committed Aug 13, 2024
1 parent 8f57d65 commit c0b6e01
Show file tree
Hide file tree
Showing 21 changed files with 2,129 additions and 886 deletions.
12 changes: 6 additions & 6 deletions embedded/document/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,10 @@ func (e *Engine) upsertDocuments(ctx context.Context, sqlTx *sql.SQLTx, collecti
ctx,
sqlTx,
[]sql.SQLStmt{
sql.NewUpserIntoStmt(
sql.NewUpsertIntoStmt(
collectionName,
colNames,
rows,
sql.NewValuesDataSource(rows),
isInsert,
nil,
),
Expand Down Expand Up @@ -879,7 +879,7 @@ func (e *Engine) ReplaceDocuments(ctx context.Context, username string, query *p
}

queryStmt := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, documentIdFieldName)},
[]sql.TargetEntry{{Exp: sql.NewColSelector(query.CollectionName, documentIdFieldName)}},
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
Expand Down Expand Up @@ -982,7 +982,7 @@ func (e *Engine) GetDocuments(ctx context.Context, query *protomodel.Query, offs
}

op := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, DocumentBLOBField)},
[]sql.TargetEntry{{Exp: sql.NewColSelector(query.CollectionName, DocumentBLOBField)}},
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func (e *Engine) CountDocuments(ctx context.Context, query *protomodel.Query, of
}

ds := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, table.Cols()[0].Name())},
[]sql.TargetEntry{{Exp: sql.NewColSelector(query.CollectionName, table.Cols()[0].Name())}},
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
Expand All @@ -1032,7 +1032,7 @@ func (e *Engine) CountDocuments(ctx context.Context, query *protomodel.Query, of
)

op := sql.NewSelectStmt(
[]sql.Selector{sql.NewAggColSelector(sql.COUNT, query.CollectionName, "*")},
[]sql.TargetEntry{{Exp: sql.NewAggColSelector(sql.COUNT, query.CollectionName, "*")}},
ds,
nil,
nil,
Expand Down
20 changes: 20 additions & 0 deletions embedded/sql/aggregated_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (v *CountValue) reduce(tx *SQLTx, row *Row, implicitTable string) (TypedVal
return nil, ErrUnexpected
}

func (v *CountValue) selectors() []Selector {
return nil
}

func (v *CountValue) reduceSelectors(row *Row, implicitTable string) ValueExp {
return nil
}
Expand Down Expand Up @@ -198,6 +202,10 @@ func (v *SumValue) reduce(tx *SQLTx, row *Row, implicitTable string) (TypedValue
return nil, ErrUnexpected
}

func (v *SumValue) selectors() []Selector {
return nil
}

func (v *SumValue) reduceSelectors(row *Row, implicitTable string) ValueExp {
return v
}
Expand Down Expand Up @@ -301,6 +309,10 @@ func (v *MinValue) reduce(tx *SQLTx, row *Row, implicitTable string) (TypedValue
return nil, ErrUnexpected
}

func (v *MinValue) selectors() []Selector {
return nil
}

func (v *MinValue) reduceSelectors(row *Row, implicitTable string) ValueExp {
return nil
}
Expand Down Expand Up @@ -404,6 +416,10 @@ func (v *MaxValue) reduce(tx *SQLTx, row *Row, implicitTable string) (TypedValue
return nil, ErrUnexpected
}

func (v *MaxValue) selectors() []Selector {
return nil
}

func (v *MaxValue) reduceSelectors(row *Row, implicitTable string) ValueExp {
return nil
}
Expand Down Expand Up @@ -516,6 +532,10 @@ func (v *AVGValue) reduce(tx *SQLTx, row *Row, implicitTable string) (TypedValue
return nil, ErrUnexpected
}

func (v *AVGValue) selectors() []Selector {
return nil
}

func (v *AVGValue) reduceSelectors(row *Row, implicitTable string) ValueExp {
return nil
}
Expand Down
1 change: 0 additions & 1 deletion embedded/sql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@ func (e *Engine) InferParameters(ctx context.Context, tx *SQLTx, sql string) (pa
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrParsingError, err)
}

return e.InferParametersPreparedStmts(ctx, tx, stmts)
}

Expand Down
Loading

0 comments on commit c0b6e01

Please sign in to comment.