Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer column type two #135

Merged
merged 6 commits into from
May 20, 2021
Merged

Infer column type two #135

merged 6 commits into from
May 20, 2021

Conversation

mathemancer
Copy link
Contributor

Fixes #92

This adds a function to infer the type of the column by attempting to alter the column to various types in a logical order.

Technical details

This has the side effect of losing data in the case where the column is altered to a type via some non-injective mapping. For example, altering the strings '0123' and '123' to a numeric will result in the value 123 in both cases. It's not possible to recover the original strings from the resulting value.

We'll add mechanisms to recover the original version of an altered column in future PRs.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the master branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@mathemancer mathemancer requested review from a team, kgodey and pavish May 19, 2021 13:58
Copy link
Contributor

@kgodey kgodey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock, please make requested changes before merging. Looks good overall!



@pytest.fixture
def temporary_testing_schema(engine_with_types):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using this fixture in multiple files, I recommend putting it in conftest.py in the root of this folder so that you can reuse it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with other reused fixtures.

Copy link
Contributor Author

@mathemancer mathemancer May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning was that these fixtures use code from mathesar that is tested by test files with no access to these fixtures. (This makes sure we're not using fixtures in a way that invalidates the tests that test the code they use. Do you know of a way to separate those out (or make it difficult/obvious when you shouldn't use a given fixture for testing? In particular, I'd want to find some way to guarantee that these fixtures won't be used for testing the db/types/install.py file at some point in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could organize the files differently so that any tests that can use these fixtures are in a single directory with the appropriate conftest.py and files that shouldn't use these fixtures are in a different directory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that seems like the obvious solution to me but I might be missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also put these fixtures in a non-default file (i.e., not a conftest.py file), then import it. It's kind of non-standard, but I think it'd be less confusing than partially mirroring the directory structure of the tested files with the structure of the test files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think that's probably my favorite solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that kind of reorganization, I think it'd merit its own PR to document what was done and why with the restructuring. @kgodey, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mathemancer I think importing a non-standard file makes sense, and doing it in a separate PR also makes sense. I was originally thinking you'd reorganize the real files as well, but I like your solution too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll double-check that the organization of the real files makes sense and adjust accordingly.

TEST_TABLE = "test_table"
TEST_COLUMN = "test_column"
metadata = MetaData(bind=engine)
print(value_list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a debug print?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, good eye.

)
break
except Exception:
logger.info(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be logger.error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception is an expected part of the control flow (it's how we know when a given type is inappropriate for a given column). I think info is reasonable for that. I do think I should remove the spot where I set the log level to INFO in this file, though (it should be set by the importer if they want a non-default level). What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you catch the specific exception you're expecting and add a comment explaining it's the expected flow?

I agree re: removing log level in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there's a bunch of different exceptions based on which type was casting to which other. I can try to figure out if they're sub-classes of something appropriate (I think python will catch the parent class in that case, I'll have to check)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, we'll use sqlalchemy.exc.DatabaseError. It's not super-precise, but it at least restricts to "you asked to do something and the DB said no".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

)
break
except Exception:
logger.info(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Type Inference 2: Determine a type for a column
2 participants