forked from flyteorg/flytekit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Max Hoffman <[email protected]>
- Loading branch information
1 parent
bfdf77c
commit bbf0a25
Showing
11 changed files
with
318 additions
and
52 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 @@ | ||
from .schema import DoltConfig, DoltTable, DoltTableNameTransformer |
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,105 @@ | ||
import tempfile | ||
import typing | ||
from dataclasses import dataclass | ||
from typing import Type | ||
|
||
import dolt_integrations.core as dolt_int | ||
import doltcli as dolt | ||
import pandas | ||
from dataclasses_json import dataclass_json | ||
from google.protobuf.struct_pb2 import Struct | ||
|
||
from flytekit import FlyteContext | ||
from flytekit.extend import TypeEngine, TypeTransformer | ||
from flytekit.models import types as _type_models | ||
from flytekit.models.literals import Literal, Scalar | ||
from flytekit.models.types import LiteralType | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class DoltConfig: | ||
db_path: str | ||
tablename: typing.Optional[str] = None | ||
sql: typing.Optional[str] = None | ||
io_args: typing.Optional[dict] = None | ||
branch_conf: typing.Optional[dolt_int.Branch] = None | ||
meta_conf: typing.Optional[dolt_int.Meta] = None | ||
remote_conf: typing.Optional[dolt_int.Remote] = None | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class DoltTable: | ||
config: DoltConfig | ||
data: typing.Optional[pandas.DataFrame] = None | ||
|
||
|
||
class DoltTableNameTransformer(TypeTransformer[DoltTable]): | ||
def __init__(self): | ||
super().__init__(name="DoltTable", t=DoltTable) | ||
|
||
def get_literal_type(self, t: Type[DoltTable]) -> LiteralType: | ||
return LiteralType(simple=_type_models.SimpleType.STRUCT, metadata={}) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: DoltTable, | ||
python_type: typing.Type[DoltTable], | ||
expected: LiteralType, | ||
) -> Literal: | ||
|
||
if not isinstance(python_val, DoltTable): | ||
raise AssertionError(f"Value cannot be converted to a table: {python_val}") | ||
|
||
conf = python_val.config | ||
if python_val.data is not None and python_val.tablename is not None: | ||
db = dolt.Dolt(conf.db_path) | ||
with tempfile.NamedTemporaryFile() as f: | ||
python_val.data.to_csv(f.name, index=False) | ||
dolt_int.save( | ||
db=db, | ||
tablename=conf.tablename, | ||
filename=f.name, | ||
branch_conf=conf.branch_conf, | ||
meta_conf=conf.meta_conf, | ||
remote_conf=conf.remote_conf, | ||
save_args=conf.io_args, | ||
) | ||
|
||
s = Struct() | ||
s.update(python_val.to_dict()) | ||
return Literal(Scalar(generic=s)) | ||
|
||
def to_python_value( | ||
self, | ||
ctx: FlyteContext, | ||
lv: Literal, | ||
expected_python_type: typing.Type[DoltTable], | ||
) -> DoltTable: | ||
|
||
if not (lv and lv.scalar and lv.scalar.generic and lv.scalar.generic["config"]): | ||
return pandas.DataFrame() | ||
|
||
conf = DoltConfig(**lv.scalar.generic["config"]) | ||
db = dolt.Dolt(conf.db_path) | ||
|
||
with tempfile.NamedTemporaryFile() as f: | ||
dolt_int.load( | ||
db=db, | ||
tablename=conf.tablename, | ||
sql=conf.sql, | ||
filename=f.name, | ||
branch_conf=conf.branch_conf, | ||
meta_conf=conf.meta_conf, | ||
remote_conf=conf.remote_conf, | ||
load_args=conf.io_args, | ||
) | ||
df = pandas.read_csv(f) | ||
lv.data = df | ||
|
||
return lv | ||
|
||
|
||
TypeEngine.register(DoltTableNameTransformer()) |
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,12 @@ | ||
#!/bin/bash | ||
|
||
# Fetches and install Dolt. To be invoked by the Dockerfile | ||
|
||
# echo commands to the terminal output | ||
set -eox pipefail | ||
|
||
# Install Dolt | ||
|
||
apt-get update -y \ | ||
&& apt-get install curl \ | ||
&& sudo bash -c 'curl -L https://github.com/dolthub/dolt/releases/latest/download/install.sh | sudo bash' |
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,35 @@ | ||
from setuptools import setup | ||
|
||
PLUGIN_NAME = "dolt" | ||
|
||
microlib_name = f"flytekitplugins-{PLUGIN_NAME}" | ||
|
||
plugin_requires = ["flytekit>=0.16.0b0,<1.0.0", "dolt_integrations>=0.1.3"] | ||
|
||
__version__ = "0.0.0+develop" | ||
|
||
setup( | ||
name=microlib_name, | ||
version=__version__, | ||
author="dolthub", | ||
author_email="[email protected]", | ||
description="Dolt plugin for flytekit", | ||
namespace_packages=["flytekitplugins"], | ||
packages=[f"flytekitplugins.{PLUGIN_NAME}"], | ||
install_requires=plugin_requires, | ||
license="apache2", | ||
python_requires=">=3.7", | ||
classifiers=[ | ||
"Intended Audience :: Science/Research", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Topic :: Scientific/Engineering", | ||
"Topic :: Scientific/Engineering :: Artificial Intelligence", | ||
"Topic :: Software Development", | ||
"Topic :: Software Development :: Libraries", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
], | ||
scripts=["scripts/flytekit_install_dolt.sh"], | ||
) |
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
Empty file.
Oops, something went wrong.