schema_check.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from fate_arch.computing import is_table
  17. from federatedml.util import LOGGER
  18. def check_schema(input_schema, output_schema):
  19. LOGGER.debug(f"input schema: {input_schema} -> output schema: {output_schema}")
  20. if output_schema is None:
  21. raise EnvironmentError(
  22. f"output_schema is None while input data has schema.")
  23. input_header = input_schema.get("header", None)
  24. output_header = output_schema.get("header", None)
  25. if input_header is not None and output_header is None:
  26. raise EnvironmentError(
  27. f"output header is None while input data has header.")
  28. def assert_schema_consistent(func):
  29. def _func(*args, **kwargs):
  30. input_schema = None
  31. all_args = []
  32. all_args.extend(args)
  33. all_args.extend(kwargs.values())
  34. for arg in all_args:
  35. if is_table(arg):
  36. input_schema = arg.schema
  37. break
  38. result = func(*args, **kwargs)
  39. if input_schema is not None:
  40. # single data set
  41. if is_table(result) and result.count() > 0:
  42. output_schema = result.schema
  43. check_schema(input_schema, output_schema)
  44. # multiple data sets
  45. elif type(result).__name__ in ["list", "tuple"]:
  46. for output_data in result:
  47. if is_table(output_data) and output_data.count() > 0:
  48. output_schema = output_data.schema
  49. check_schema(input_schema, output_schema)
  50. return result
  51. return _func