Skip to content

Commit

Permalink
Fix select spark version in staging (#388)
Browse files Browse the repository at this point in the history
## Changes
In staging, there are some spark versions that do not have a patch
version. We fall back to a different pattern with no patch component for
these. Additionally, we check if the builds are equal to one another
(which allows us to avoid comparing one < the other in the case where
they are both None).

Closes #352 and #378.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [x] Added a unit test with a format that was previously not supported.
- [x] Hand-wrote an example using select_spark_versions(). This failed
on `main` but passed on my branch when targeting a staging workspace.
  • Loading branch information
mgyucht authored Oct 6, 2023
1 parent bb3095b commit 4fb5e38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 10 additions & 5 deletions databricks/sdk/mixins/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class SemVer:
build: Optional[str] = None

# official https://semver.org/ recommendation: https://regex101.com/r/Ly7O1x/
# with addition of "x" wildcards for minor/patch versions
# with addition of "x" wildcards for minor/patch versions. Also, patch version may be omitted.
_pattern = re.compile(r"^"
r"(?P<major>0|[1-9]\d*)\.(?P<minor>x|0|[1-9]\d*)\.(?P<patch>x|0|[1-9x]\d*)"
r"(?P<major>0|[1-9]\d*)\.(?P<minor>x|0|[1-9]\d*)(\.(?P<patch>x|0|[1-9x]\d*))?"
r"(?:-(?P<pre_release>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?"
r"(?:\+(?P<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$")
Expand All @@ -40,10 +40,13 @@ def parse(cls, v: str) -> 'SemVer':
# patch and/or minor versions may be wildcards.
# for now, we're converting wildcards to zeroes.
minor = m.group('minor')
patch = m.group('patch')
try:
patch = m.group('patch')
except IndexError:
patch = 0
return SemVer(major=int(m.group('major')),
minor=0 if minor == 'x' else int(minor),
patch=0 if patch == 'x' else int(patch),
patch=0 if patch == 'x' or patch is None else int(patch),
pre_release=m.group('pre_release'),
build=m.group('build'))

Expand All @@ -58,7 +61,9 @@ def __lt__(self, other: 'SemVer'):
return self.patch < other.patch
if self.pre_release != other.pre_release:
return self.pre_release < other.pre_release
return self.build < other.build
if self.build != other.build:
return self.build < other.build
return False


class ClustersExt(compute.ClustersAPI):
Expand Down
10 changes: 6 additions & 4 deletions tests/test_compute_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from databricks.sdk.mixins.compute import SemVer


@pytest.mark.parametrize("given,expected", [('v0.0.4', SemVer(0, 0, 4)), ('v1.2.3', SemVer(1, 2, 3)),
('v12.1.x', SemVer(12, 1, 0)), ('v10.20.30', SemVer(10, 20, 30)),
('v1.1.2+meta', SemVer(1, 1, 2, build='meta')),
('v1.0.0-alpha', SemVer(1, 0, 0, pre_release='alpha'))])
@pytest.mark.parametrize("given,expected",
[('v0.0.4', SemVer(0, 0, 4)), ('v1.2.3', SemVer(1, 2, 3)),
('v12.1.x', SemVer(12, 1, 0)), ('v10.20.30', SemVer(10, 20, 30)),
('v1.1.2+meta', SemVer(1, 1, 2, build='meta')),
('v1.0.0-alpha', SemVer(1, 0, 0, pre_release='alpha')),
('8.x-snapshot-scala2.12', SemVer(8, 0, 0, pre_release='snapshot-scala2.12')), ])
def test_parse_semver(given, expected):
assert SemVer.parse(given) == expected

Expand Down

0 comments on commit 4fb5e38

Please sign in to comment.