Truncate large names, with an opaleye tag to avoid collisions#400
Conversation
|
Did you see this comment? #366 (comment) My impression is that this is already fixed, since we truncate and then add a unique tag |
I did see the comment, it doesn't fix another issue which is select aliases still being generated with too long names (we still see a bunch of warnings in our service, that's why I have been working on this). See the tests for details. (edit: I realized the tests don't show the behaviour, because they're using a 42 chars identifier, but it's there. I'll add another test.) |
|
Would it be possible to take the same approach as before here too then, ie, use a |
c7c050c to
d265e5a
Compare
Done |
|
Could you use the same helper here: Line 198 in 95ec308 Also is it necessary to make the |
Just to be sure: You mean extract the helper from that function and using that, right? Something like statementReturning :: Table Expr a
=> State Opaleye.Tag Doc -> Statement (Query a)
statementReturning pp = Statement $ do
(binding, query) <- lift $ do
doc <- pp
tag <- Opaleye.fresh
let
relation = Opaleye.tagWith tag "statement"
names = namesFromLabelsWithA (symbol tag) `evalState` Opaleye.start
columns = Just $ showNames names
query =
fromCols <$> each
TableSchema
{ name = fromString relation
, columns = names
}
returning = Returning (countRows query)
binding = Binding {..}
pure (binding, query)
tell (Endo (binding :))
pure $ Unmodified query
symbol :: Tag -> NonEmpty String -> State Opaleye.Tag String
symbol tag labels = do
subtag <- Opaleye.fresh
let
suffix = Opaleye.tagWith tag (Opaleye.tagWith subtag "")
pure $ take (63 - length suffix) label ++ suffix
where
label = fold (intersperse "/" labels)and use
That's fine, 63 seems to be the number in most postgres implementations that people are worried about. |
|
Yep that's the sort of thing. I'm not sure if these are exactly the same, but they seem quite similar. Whatever you think is best |
d265e5a to
18423bb
Compare
|
Done. I also renamed |
|
Sorry one last thing. Could you just make this the default (or maybe there's a reason not to?)? So don't change the interface of Rel8 at all on this PR. |
Two concerns:
|
|
I agree that it's unlikely that someone will change the default length. Yeah I think the nameFromLabels function should match internal usage. |
AI disclosure: Generated with Claude Code to figure out how to test the SQL string.
18423bb to
1e58aa9
Compare
|
Done. Let me know if there's anything else! |
TeofilC
left a comment
There was a problem hiding this comment.
Looks great thanks!
I'll try to cut a release soon btw
…thub#400) * Truncate large names, with a tag to avoid collisions * Add test for long label case AI disclosure: Generated with Claude Code to figure out how to test the SQL string.
See issue #366.
This PR adds a function
selectTruncatedthat limits the alias names to a specific size (> 29 characters), using SHA1 to generate a unique (modulo SHA1 collisions and a column with a deliberately colliding name) suffix.This allows us to make Postgres happy about column name size while at the same time keeping some debug information around.