-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gaetano Guerriero
committed
Jan 28, 2022
1 parent
9c15f13
commit 8e43199
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Union | ||
|
||
|
||
def build_virtual_column(virtual_column: Union[VirtualColumnSpec, dict]) -> dict: | ||
if isinstance(virtual_column, VirtualColumnSpec): | ||
return virtual_column.build() | ||
|
||
return virtual_column | ||
|
||
|
||
class VirtualColumnSpec: | ||
def __init__(self, name: str, expression: str, output_type: str = None): | ||
self._name = name | ||
self._expression = expression | ||
self._output_type = output_type | ||
|
||
def build(self) -> dict: | ||
build = { | ||
"type": "expression", | ||
"name": self._name, | ||
"expression": self._expression, | ||
} | ||
if self._output_type is not None: | ||
build["outputType"] = self._output_type | ||
|
||
return build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from pydruid.utils.virtualcolumns import build_virtual_column, VirtualColumnSpec | ||
|
||
|
||
class BuildVirtualColumnTC: | ||
def test_with_virtual_column_spec(self) -> None: | ||
virtual_col_spec = VirtualColumnSpec("foo", "bar + 3") | ||
assert build_virtual_column(virtual_col_spec) == virtual_col_spec.build() | ||
|
||
def test_with_dict(self) -> None: | ||
virtual_column = { | ||
"type": "expression", | ||
"name": "doubleVote", | ||
"expression": "vote * 2", | ||
"outputType": "LONG", | ||
} | ||
assert build_virtual_column(virtual_column) == virtual_column | ||
|
||
|
||
class TestVirtualColumnSpec: | ||
def test_default(self) -> None: | ||
spec = VirtualColumnSpec("foo", "concat(bar + '123')") | ||
expected = { | ||
"type": "expression", | ||
"name": "foo", | ||
"expression": "concat(bar + '123')", | ||
} | ||
assert spec.build() == expected | ||
|
||
def test_output_type(self) -> None: | ||
spec = VirtualColumnSpec("foo", "bar * 3", "LONG") | ||
expected = { | ||
"type": "expression", | ||
"name": "foo", | ||
"expression": "bar * 3", | ||
"outputType": "LONG", | ||
} | ||
assert spec.build() == expected |