test_file_common.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import codecs
  2. import os
  3. import pytest
  4. import re
  5. import cyson
  6. import yql.essentials.providers.common.proto.gateways_config_pb2 as gateways_config_pb2
  7. from google.protobuf import text_format
  8. from yql_utils import execute, get_supported_providers, get_tables, get_files, get_http_files, \
  9. get_pragmas, KSV_ATTR, is_xfail, get_param, YQLExecResult, yql_binary_path, do_custom_error_check
  10. from yqlrun import YQLRun
  11. from test_utils import get_parameters_json, replace_vars
  12. def get_gateways_config(http_files, yql_http_file_server, force_blocks=False, is_hybrid=False, allow_llvm=True):
  13. config = None
  14. if http_files or force_blocks or is_hybrid or not allow_llvm:
  15. config_message = gateways_config_pb2.TGatewaysConfig()
  16. if http_files:
  17. schema = config_message.Fs.CustomSchemes.add()
  18. schema.Pattern = 'http_test://(.*)'
  19. schema.TargetUrl = yql_http_file_server.compose_http_link('$1')
  20. if force_blocks:
  21. config_message.SqlCore.TranslationFlags.extend(['EmitAggApply', 'EmitTableSource'])
  22. flags = config_message.YqlCore.Flags.add()
  23. flags.Name = 'UseBlocks'
  24. if is_hybrid:
  25. activate_hybrid = config_message.Yt.DefaultSettings.add()
  26. activate_hybrid.Name = "HybridDqExecution"
  27. activate_hybrid.Value = "1"
  28. deactivate_dq = config_message.Dq.DefaultSettings.add()
  29. deactivate_dq.Name = "AnalyzeQuery"
  30. deactivate_dq.Value = "0"
  31. if not allow_llvm:
  32. flags = config_message.YqlCore.Flags.add()
  33. flags.Name = 'LLVM_OFF'
  34. config = text_format.MessageToString(config_message)
  35. return config
  36. def is_hybrid(provider):
  37. return provider == 'hybrid'
  38. def check_provider(provider, config):
  39. if provider == 'pure':
  40. return
  41. if provider not in get_supported_providers(config):
  42. pytest.skip('%s provider is not supported here' % provider)
  43. def get_sql_query(provider, suite, case, config, data_path=None, template='.sql'):
  44. pragmas = get_pragmas(config)
  45. if get_param('TARGET_PLATFORM'):
  46. if "yson" in case or "regexp" in case or "match" in case:
  47. pytest.skip('yson/match/regexp is not supported on non-default target platform')
  48. if get_param('TARGET_PLATFORM') and is_xfail(config):
  49. pytest.skip('xfail is not supported on non-default target platform')
  50. program_sql = os.path.join(data_path, suite, '%s%s' % (case, template))
  51. with codecs.open(program_sql, encoding='utf-8') as program_file_descr:
  52. sql_query = program_file_descr.read()
  53. if get_param('TARGET_PLATFORM'):
  54. if "Yson::" in sql_query:
  55. pytest.skip('yson udf is not supported on non-default target platform')
  56. if (provider + 'file can not' in sql_query) or (is_hybrid(provider) and ('ytfile can not' in sql_query)):
  57. pytest.skip(provider + ' can not execute this')
  58. pragmas.append(sql_query)
  59. sql_query = ';\n'.join(pragmas)
  60. if provider != 'yt' and provider != 'pure' and 'Javascript' in sql_query:
  61. pytest.skip('ScriptUdf')
  62. assert 'UseBlocks' not in sql_query, 'UseBlocks should not be used directly, only via ForceBlocks'
  63. return sql_query
  64. def run_file_no_cache(provider, suite, case, cfg, config, yql_http_file_server,
  65. yqlrun_binary=None, extra_args=[], force_blocks=False, allow_llvm=True, data_path=None,
  66. run_sql=True):
  67. check_provider(provider, config)
  68. sql_query = get_sql_query(provider, suite, case, config, data_path, template='.sql' if run_sql else '.yqls')
  69. sql_query = replace_vars(sql_query, "yqlrun_var")
  70. xfail = is_xfail(config)
  71. in_tables, out_tables = get_tables(suite, config, data_path, def_attr=KSV_ATTR) if provider != 'pure' else (None, None)
  72. files = get_files(suite, config, data_path)
  73. http_files = get_http_files(suite, config, data_path)
  74. http_files_urls = yql_http_file_server.register_files({}, http_files)
  75. if in_tables is not None:
  76. for table in in_tables:
  77. if cyson.loads(table.attr).get("type") == "document":
  78. content = table.content
  79. else:
  80. content = table.attr
  81. if provider != 'yt' and 'Javascript' in content:
  82. pytest.skip('ScriptUdf')
  83. parameters = get_parameters_json(suite, config, data_path)
  84. yqlrun = YQLRun(
  85. prov=provider,
  86. keep_temp=not re.search(r"yt\.ReleaseTempData", sql_query),
  87. binary=yqlrun_binary,
  88. gateway_config=get_gateways_config(http_files, yql_http_file_server, force_blocks=force_blocks, is_hybrid=is_hybrid(provider), allow_llvm=allow_llvm),
  89. extra_args=extra_args,
  90. udfs_dir=yql_binary_path('yql/essentials/tests/common/test_framework/udfs_deps')
  91. )
  92. res, tables_res = execute(
  93. yqlrun,
  94. program=sql_query,
  95. input_tables=in_tables,
  96. output_tables=out_tables,
  97. files=files,
  98. urls=http_files_urls,
  99. check_error=not xfail,
  100. run_sql=run_sql,
  101. verbose=True,
  102. parameters=parameters)
  103. fixed_stderr = res.std_err
  104. if xfail:
  105. assert res.execution_result.exit_code != 0
  106. do_custom_error_check(res, sql_query)
  107. fixed_stderr = None
  108. fixed_result = YQLExecResult(res.std_out,
  109. fixed_stderr,
  110. res.results,
  111. res.results_file,
  112. res.opt,
  113. res.opt_file,
  114. res.plan,
  115. res.plan_file,
  116. res.program,
  117. res.execution_result,
  118. None)
  119. return fixed_result, tables_res
  120. def run_file(provider, suite, case, cfg, config, yql_http_file_server, yqlrun_binary=None,
  121. extra_args=[], force_blocks=False, allow_llvm=True, data_path=None, run_sql=True):
  122. if (suite, case, cfg) not in run_file.cache:
  123. run_file.cache[(suite, case, cfg)] = \
  124. run_file_no_cache(provider, suite, case, cfg, config, yql_http_file_server,
  125. yqlrun_binary, extra_args, force_blocks=force_blocks, allow_llvm=allow_llvm,
  126. data_path=data_path, run_sql=run_sql)
  127. return run_file.cache[(suite, case, cfg)]
  128. run_file.cache = {}