Skip to content

Commit 6bae289

Browse files
committed
feat: allow aliases in spread embeddings
1 parent b7d0a1f commit 6bae289

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/PostgREST/ApiRequest/QueryParams.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ pTreePath = do
291291
-- Right [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SelectRelation {selRelation = "client", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SelectRelation {selRelation = "nested", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []}]}]}]
292292
--
293293
-- >>> P.parse pFieldForest "" "*,...client(*),other(*)"
294-
-- Right [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SpreadRelation {selRelation = "client", selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []}]},Node {rootLabel = SelectRelation {selRelation = "other", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []}]}]
294+
-- Right [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SpreadRelation {selRelation = "client", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []}]},Node {rootLabel = SelectRelation {selRelation = "other", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selAggregateFunction = Nothing, selAggregateCast = Nothing, selCast = Nothing, selAlias = Nothing}, subForest = []}]}]
295295
--
296296
-- >>> P.parse pFieldForest "" ""
297297
-- Right []
@@ -555,10 +555,13 @@ pFieldSelect = lexeme $ try (do
555555
-- Parse spread relations in select
556556
--
557557
-- >>> P.parse pSpreadRelationSelect "" "...rel(*)"
558-
-- Right (SpreadRelation {selRelation = "rel", selHint = Nothing, selJoinType = Nothing})
558+
-- Right (SpreadRelation {selRelation = "rel", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing})
559+
--
560+
-- >>> P.parse pSpreadRelationSelect "" "...alias:rel(*)"
561+
-- Right (SpreadRelation {selRelation = "rel", selAlias = Just "alias", selHint = Nothing, selJoinType = Nothing})
559562
--
560563
-- >>> P.parse pSpreadRelationSelect "" "...rel!hint!inner(*)"
561-
-- Right (SpreadRelation {selRelation = "rel", selHint = Just "hint", selJoinType = Just JTInner})
564+
-- Right (SpreadRelation {selRelation = "rel", selAlias = Nothing, selHint = Just "hint", selJoinType = Just JTInner})
562565
--
563566
-- >>> P.parse pSpreadRelationSelect "" "rel(*)"
564567
-- Left (line 1, column 1):
@@ -575,10 +578,11 @@ pFieldSelect = lexeme $ try (do
575578
-- unexpected '>'
576579
pSpreadRelationSelect :: Parser SelectItem
577580
pSpreadRelationSelect = lexeme $ do
578-
name <- string "..." >> pFieldName
581+
alias <- string "..." >> optionMaybe ( try(pFieldName <* aliasSeparator) )
582+
name <- pFieldName
579583
(hint, jType) <- pEmbedParams
580584
try (void $ lookAhead (string "("))
581-
return $ SpreadRelation name hint jType
585+
return $ SpreadRelation name alias hint jType
582586

583587
pEmbedParams :: Parser (Maybe Hint, Maybe JoinType)
584588
pEmbedParams = do

src/PostgREST/ApiRequest/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ data SelectItem
6464
-- | The value in `/tbl?select=...another_tbl(*)`
6565
| SpreadRelation
6666
{ selRelation :: FieldName
67+
, selAlias :: Maybe Alias
6768
, selHint :: Maybe Hint
6869
, selJoinType :: Maybe JoinType
6970
}

src/PostgREST/Plan.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ initReadRequest ctx@ResolverContext{qi=QualifiedIdentifier{..}} =
367367
SpreadRelation{..} ->
368368
Node q $
369369
foldr (treeEntry nxtDepth)
370-
(Node defReadPlan{from=QualifiedIdentifier qiSchema selRelation, relName=selRelation, relHint=selHint, relJoinType=selJoinType, depth=nxtDepth, relIsSpread=True} [])
370+
(Node defReadPlan{from=QualifiedIdentifier qiSchema selRelation, relName=selRelation, relAlias=selAlias, relHint=selHint, relJoinType=selJoinType, depth=nxtDepth, relIsSpread=True} [])
371371
fldForest:rForest
372372
SelectField{..} ->
373373
Node q{select=CoercibleSelectField (resolveOutputField ctx{qi=from q} selField) selAggregateFunction selAggregateCast selCast selAlias:select q} rForest

0 commit comments

Comments
 (0)