|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import sys |
| 19 | +import os |
| 20 | +from collections import namedtuple |
| 21 | + |
| 22 | +ExpressionInfo = namedtuple("ExpressionInfo", "className usage name extended") |
| 23 | + |
| 24 | + |
| 25 | +def _list_function_infos(jvm): |
| 26 | + """ |
| 27 | + Returns a list of function information via JVM. Sorts wrapped expression infos by name |
| 28 | + and returns them. |
| 29 | + """ |
| 30 | + |
| 31 | + jinfos = jvm.org.apache.spark.sql.api.python.PythonSQLUtils.listBuiltinFunctionInfos() |
| 32 | + infos = [] |
| 33 | + for jinfo in jinfos: |
| 34 | + name = jinfo.getName() |
| 35 | + usage = jinfo.getUsage() |
| 36 | + usage = usage.replace("_FUNC_", name) if usage is not None else usage |
| 37 | + extended = jinfo.getExtended() |
| 38 | + extended = extended.replace("_FUNC_", name) if extended is not None else extended |
| 39 | + infos.append(ExpressionInfo( |
| 40 | + className=jinfo.getClassName(), |
| 41 | + usage=usage, |
| 42 | + name=name, |
| 43 | + extended=extended)) |
| 44 | + return sorted(infos, key=lambda i: i.name) |
| 45 | + |
| 46 | + |
| 47 | +def _make_pretty_usage(usage): |
| 48 | + """ |
| 49 | + Makes the usage description pretty and returns a formatted string. |
| 50 | + Otherwise, returns None. |
| 51 | + """ |
| 52 | + |
| 53 | + if usage is not None and usage.strip() != "": |
| 54 | + usage = "\n".join(map(lambda u: u.strip(), usage.split("\n"))) |
| 55 | + return "%s\n\n" % usage |
| 56 | + |
| 57 | + |
| 58 | +def _make_pretty_extended(extended): |
| 59 | + """ |
| 60 | + Makes the extended description pretty and returns a formatted string. |
| 61 | + Otherwise, returns None. |
| 62 | + """ |
| 63 | + |
| 64 | + if extended is not None and extended.strip() != "": |
| 65 | + extended = "\n".join(map(lambda u: u.strip(), extended.split("\n"))) |
| 66 | + return "```%s```\n\n" % extended |
| 67 | + |
| 68 | + |
| 69 | +def generate_sql_markdown(jvm, path): |
| 70 | + """ |
| 71 | + Generates a markdown file after listing the function information. The output file |
| 72 | + is created in `path`. |
| 73 | + """ |
| 74 | + |
| 75 | + with open(path, 'w') as mdfile: |
| 76 | + for info in _list_function_infos(jvm): |
| 77 | + mdfile.write("### %s\n\n" % info.name) |
| 78 | + usage = _make_pretty_usage(info.usage) |
| 79 | + extended = _make_pretty_extended(info.extended) |
| 80 | + if usage is not None: |
| 81 | + mdfile.write(usage) |
| 82 | + if extended is not None: |
| 83 | + mdfile.write(extended) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + from pyspark.java_gateway import launch_gateway |
| 88 | + |
| 89 | + jvm = launch_gateway().jvm |
| 90 | + markdown_file_path = "%s/docs/index.md" % os.path.dirname(sys.argv[0]) |
| 91 | + generate_sql_markdown(jvm, markdown_file_path) |
0 commit comments